Merge remote branch 'mine/ext-python'
[vuplus_xbmc] / xbmc / network / GUIDialogNetworkSetup.cpp
1 /*
2  *      Copyright (C) 2005-2008 Team XBMC
3  *      http://www.xbmc.org
4  *
5  *  This Program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2, or (at your option)
8  *  any later version.
9  *
10  *  This Program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with XBMC; see the file COPYING.  If not, write to
17  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
18  *  http://www.gnu.org/copyleft/gpl.html
19  *
20  */
21
22 #include "GUIDialogNetworkSetup.h"
23 #include "guilib/GUISpinControlEx.h"
24 #include "dialogs/GUIDialogNumeric.h"
25 #include "dialogs/GUIDialogKeyboard.h"
26 #include "dialogs/GUIDialogFileBrowser.h"
27 #include "guilib/GUIWindowManager.h"
28 #include "guilib/GUIEditControl.h"
29 #include "utils/URIUtils.h"
30 #include "URL.h"
31 #include "guilib/LocalizeStrings.h"
32
33 #define CONTROL_PROTOCOL        10
34 #define CONTROL_SERVER_ADDRESS  11
35 #define CONTROL_SERVER_BROWSE   12
36 #define CONTROL_PORT_NUMBER     13
37 #define CONTROL_USERNAME        14
38 #define CONTROL_PASSWORD        15
39 #define CONTROL_REMOTE_PATH     16
40 #define CONTROL_OK              18
41 #define CONTROL_CANCEL          19
42
43 CGUIDialogNetworkSetup::CGUIDialogNetworkSetup(void)
44     : CGUIDialog(WINDOW_DIALOG_NETWORK_SETUP, "DialogNetworkSetup.xml")
45 {
46   m_protocol = NET_PROTOCOL_SMB;
47   m_confirmed = false;
48 }
49
50 CGUIDialogNetworkSetup::~CGUIDialogNetworkSetup()
51 {
52 }
53
54 bool CGUIDialogNetworkSetup::OnAction(const CAction &action)
55 {
56   if (action.GetID() == ACTION_PREVIOUS_MENU)
57     m_confirmed = false;
58   return CGUIDialog::OnAction(action);
59 }
60
61 bool CGUIDialogNetworkSetup::OnMessage(CGUIMessage& message)
62 {
63   switch ( message.GetMessage() )
64   {
65   case GUI_MSG_CLICKED:
66     {
67       int iControl = message.GetSenderId();
68       if (iControl == CONTROL_PROTOCOL)
69       {
70         m_server.Empty();
71         m_path.Empty();
72         m_username.Empty();
73         m_password.Empty();
74         OnProtocolChange();
75       }
76       else if (iControl == CONTROL_SERVER_BROWSE)
77         OnServerBrowse();
78       else if (iControl == CONTROL_SERVER_ADDRESS)
79         OnEditChanged(iControl, m_server);
80       else if (iControl == CONTROL_REMOTE_PATH)
81         OnEditChanged(iControl, m_path);
82       else if (iControl == CONTROL_PORT_NUMBER)
83         OnEditChanged(iControl, m_port);
84       else if (iControl == CONTROL_USERNAME)
85         OnEditChanged(iControl, m_username);
86       else if (iControl == CONTROL_PASSWORD)
87         OnEditChanged(iControl, m_password);
88       else if (iControl == CONTROL_OK)
89         OnOK();
90       else if (iControl == CONTROL_CANCEL)
91         OnCancel();
92       return true;
93     }
94     break;
95   }
96   return CGUIDialog::OnMessage(message);
97 }
98
99 // \brief Show CGUIDialogNetworkSetup dialog and prompt for a new network address.
100 // \return True if the network address is valid, false otherwise.
101 bool CGUIDialogNetworkSetup::ShowAndGetNetworkAddress(CStdString &path)
102 {
103   CGUIDialogNetworkSetup *dialog = (CGUIDialogNetworkSetup *)g_windowManager.GetWindow(WINDOW_DIALOG_NETWORK_SETUP);
104   if (!dialog) return false;
105   dialog->Initialize();
106   dialog->SetPath(path);
107   dialog->DoModal();
108   path = dialog->ConstructPath();
109   return dialog->IsConfirmed();
110 }
111
112 void CGUIDialogNetworkSetup::OnInitWindow()
113 {
114   // replace our buttons with edits
115   ChangeButtonToEdit(CONTROL_SERVER_ADDRESS);
116   ChangeButtonToEdit(CONTROL_REMOTE_PATH);
117   ChangeButtonToEdit(CONTROL_USERNAME);
118   ChangeButtonToEdit(CONTROL_PORT_NUMBER);
119   ChangeButtonToEdit(CONTROL_PASSWORD);
120
121   // start as unconfirmed
122   m_confirmed = false;
123
124   CGUIDialog::OnInitWindow();
125   // Add our protocols
126   CGUISpinControlEx *pSpin = (CGUISpinControlEx *)GetControl(CONTROL_PROTOCOL);
127   if (!pSpin)
128     return;
129
130   pSpin->Clear();
131   pSpin->AddLabel(g_localizeStrings.Get(20171), NET_PROTOCOL_SMB);
132   pSpin->AddLabel(g_localizeStrings.Get(20256), NET_PROTOCOL_HTSP);
133   pSpin->AddLabel(g_localizeStrings.Get(20257), NET_PROTOCOL_VTP);
134   pSpin->AddLabel(g_localizeStrings.Get(20258), NET_PROTOCOL_MYTH);
135   pSpin->AddLabel(g_localizeStrings.Get(21331), NET_PROTOCOL_TUXBOX);
136   pSpin->AddLabel(g_localizeStrings.Get(20301), NET_PROTOCOL_HTTPS);
137   pSpin->AddLabel(g_localizeStrings.Get(20300), NET_PROTOCOL_HTTP);
138   pSpin->AddLabel(g_localizeStrings.Get(20254), NET_PROTOCOL_DAVS);
139   pSpin->AddLabel(g_localizeStrings.Get(20253), NET_PROTOCOL_DAV);
140   pSpin->AddLabel(g_localizeStrings.Get(20173), NET_PROTOCOL_FTP);
141   pSpin->AddLabel(g_localizeStrings.Get(20174), NET_PROTOCOL_DAAP);
142   pSpin->AddLabel(g_localizeStrings.Get(20175), NET_PROTOCOL_UPNP);
143   pSpin->AddLabel(g_localizeStrings.Get(20304), NET_PROTOCOL_RSS);
144
145   pSpin->SetValue(m_protocol);
146   OnProtocolChange();
147 }
148
149 void CGUIDialogNetworkSetup::OnServerBrowse()
150 {
151   // open a filebrowser dialog with the current address
152   VECSOURCES shares;
153   CStdString path = ConstructPath();
154   // get the share as the base path
155   CMediaSource share;
156   CStdString basePath = path;
157   CStdString tempPath;
158   while (URIUtils::GetParentPath(basePath, tempPath))
159     basePath = tempPath;
160   share.strPath = basePath;
161   // don't include the user details in the share name
162   CURL url(share.strPath);
163   share.strName = url.GetWithoutUserDetails();
164   shares.push_back(share);
165   if (CGUIDialogFileBrowser::ShowAndGetDirectory(shares, g_localizeStrings.Get(1015), path))
166   {
167     SetPath(path);
168     UpdateButtons();
169   }
170 }
171
172 void CGUIDialogNetworkSetup::OnOK()
173 {
174   m_confirmed = true;
175   Close();
176 }
177
178 void CGUIDialogNetworkSetup::OnCancel()
179 {
180   m_confirmed = false;
181   Close();
182 }
183
184 void CGUIDialogNetworkSetup::OnProtocolChange()
185 {
186   CGUISpinControlEx *pSpin = (CGUISpinControlEx *)GetControl(CONTROL_PROTOCOL);
187   if (!pSpin)
188     return;
189   m_protocol = (NET_PROTOCOL)pSpin->GetValue();
190   // set defaults for the port
191   if (m_protocol == NET_PROTOCOL_FTP)
192     m_port = "21";
193   else if (m_protocol == NET_PROTOCOL_HTTP || 
194            m_protocol == NET_PROTOCOL_RSS || 
195            m_protocol == NET_PROTOCOL_TUXBOX || 
196            m_protocol == NET_PROTOCOL_DAV)
197     m_port = "80";
198   else if (m_protocol == NET_PROTOCOL_HTTPS || m_protocol == NET_PROTOCOL_DAVS)
199     m_port = "443";
200   else if (m_protocol == NET_PROTOCOL_DAAP)
201     m_port = "3689";
202   else if (m_protocol == NET_PROTOCOL_HTSP)
203     m_port = "9982";
204   else if (m_protocol == NET_PROTOCOL_VTP)
205     m_port = "2004";
206   else if (m_protocol == NET_PROTOCOL_MYTH)
207     m_port = "6543";
208
209   UpdateButtons();
210 }
211
212 void CGUIDialogNetworkSetup::UpdateButtons()
213 {
214   // Address label
215   SET_CONTROL_LABEL2(CONTROL_SERVER_ADDRESS, m_server);
216   if (m_protocol == NET_PROTOCOL_SMB)
217   {
218     SET_CONTROL_LABEL(CONTROL_SERVER_ADDRESS, 1010);  // Server name
219   }
220   else
221   {
222     SET_CONTROL_LABEL(CONTROL_SERVER_ADDRESS, 1009);  // Server Address
223   }
224   if (m_protocol == NET_PROTOCOL_DAAP)
225     SendMessage(GUI_MSG_SET_TYPE, CONTROL_SERVER_ADDRESS, CGUIEditControl::INPUT_TYPE_IPADDRESS, 1016);
226   else
227     SendMessage(GUI_MSG_SET_TYPE, CONTROL_SERVER_ADDRESS, CGUIEditControl::INPUT_TYPE_TEXT, 1016);
228   // remote path
229   SET_CONTROL_LABEL2(CONTROL_REMOTE_PATH, m_path);
230   CONTROL_ENABLE_ON_CONDITION(CONTROL_REMOTE_PATH, m_protocol != NET_PROTOCOL_DAAP &&
231                                                    m_protocol != NET_PROTOCOL_UPNP &&
232                                                    m_protocol != NET_PROTOCOL_TUXBOX &&
233                                                    m_protocol != NET_PROTOCOL_HTSP &&
234                                                    m_protocol != NET_PROTOCOL_VTP &&
235                                                    m_protocol != NET_PROTOCOL_MYTH);
236   if (m_protocol == NET_PROTOCOL_FTP ||
237       m_protocol == NET_PROTOCOL_HTTP ||
238       m_protocol == NET_PROTOCOL_HTTPS ||
239       m_protocol == NET_PROTOCOL_RSS ||
240       m_protocol == NET_PROTOCOL_DAV ||
241       m_protocol == NET_PROTOCOL_DAVS)
242   {
243     SET_CONTROL_LABEL(CONTROL_REMOTE_PATH, 1011);  // Remote Path
244   }
245   else
246   {
247     SET_CONTROL_LABEL(CONTROL_REMOTE_PATH, 1012);  // Shared Folder
248   }
249   SendMessage(GUI_MSG_SET_TYPE, CONTROL_REMOTE_PATH, CGUIEditControl::INPUT_TYPE_TEXT, 1017);
250
251   // username
252   SET_CONTROL_LABEL2(CONTROL_USERNAME, m_username);
253   CONTROL_ENABLE_ON_CONDITION(CONTROL_USERNAME, m_protocol != NET_PROTOCOL_DAAP &&
254                                                 m_protocol != NET_PROTOCOL_VTP &&
255                                                 m_protocol != NET_PROTOCOL_UPNP);
256
257   SendMessage(GUI_MSG_SET_TYPE, CONTROL_USERNAME, CGUIEditControl::INPUT_TYPE_TEXT, 1019);
258
259   // port
260   SET_CONTROL_LABEL2(CONTROL_PORT_NUMBER, m_port);
261   CONTROL_ENABLE_ON_CONDITION(CONTROL_PORT_NUMBER, m_protocol == NET_PROTOCOL_FTP ||
262                                                    m_protocol == NET_PROTOCOL_HTTP ||
263                                                    m_protocol == NET_PROTOCOL_HTTPS ||
264                                                    m_protocol == NET_PROTOCOL_DAV ||
265                                                    m_protocol == NET_PROTOCOL_DAVS ||
266                                                    m_protocol == NET_PROTOCOL_TUXBOX ||
267                                                    m_protocol == NET_PROTOCOL_HTSP ||
268                                                    m_protocol == NET_PROTOCOL_VTP ||
269                                                    m_protocol == NET_PROTOCOL_MYTH ||
270                                                    m_protocol == NET_PROTOCOL_RSS ||
271                                                    m_protocol == NET_PROTOCOL_DAAP);
272
273   SendMessage(GUI_MSG_SET_TYPE, CONTROL_PORT_NUMBER, CGUIEditControl::INPUT_TYPE_NUMBER, 1018);
274
275   // password
276   SET_CONTROL_LABEL2(CONTROL_PASSWORD, m_password);
277   CONTROL_ENABLE_ON_CONDITION(CONTROL_PASSWORD, m_protocol != NET_PROTOCOL_DAAP &&
278                                                 m_protocol != NET_PROTOCOL_VTP &&
279                                                 m_protocol != NET_PROTOCOL_UPNP);
280
281   SendMessage(GUI_MSG_SET_TYPE, CONTROL_PASSWORD, CGUIEditControl::INPUT_TYPE_PASSWORD, 12326);
282
283   // TODO: FIX BETTER DAAP SUPPORT
284   // server browse should be disabled if we are in DAAP, FTP, HTTP, HTTPS, RSS, HTSP, VTP, TUXBOX, DAV or DAVS
285   CONTROL_ENABLE_ON_CONDITION(CONTROL_SERVER_BROWSE, !m_server.IsEmpty() || !(m_protocol == NET_PROTOCOL_FTP ||
286                                                                               m_protocol == NET_PROTOCOL_HTTP ||
287                                                                               m_protocol == NET_PROTOCOL_HTTPS ||
288                                                                               m_protocol == NET_PROTOCOL_DAV ||
289                                                                               m_protocol == NET_PROTOCOL_DAVS ||
290                                                                               m_protocol == NET_PROTOCOL_DAAP ||
291                                                                               m_protocol == NET_PROTOCOL_RSS ||
292                                                                               m_protocol == NET_PROTOCOL_HTSP ||
293                                                                               m_protocol == NET_PROTOCOL_VTP ||
294                                                                               m_protocol == NET_PROTOCOL_MYTH ||
295                                                                               m_protocol == NET_PROTOCOL_TUXBOX));
296 }
297
298 CStdString CGUIDialogNetworkSetup::ConstructPath() const
299 {
300   CURL url;
301   if (m_protocol == NET_PROTOCOL_SMB)
302     url.SetProtocol("smb");
303   else if (m_protocol == NET_PROTOCOL_FTP)
304     url.SetProtocol("ftp");
305   else if (m_protocol == NET_PROTOCOL_HTTP)
306     url.SetProtocol("http");
307   else if (m_protocol == NET_PROTOCOL_HTTPS)
308     url.SetProtocol("https");
309   else if (m_protocol == NET_PROTOCOL_DAV)
310     url.SetProtocol("dav");
311   else if (m_protocol == NET_PROTOCOL_DAVS)
312     url.SetProtocol("davs");
313   else if (m_protocol == NET_PROTOCOL_DAAP)
314     url.SetProtocol("daap");
315   else if (m_protocol == NET_PROTOCOL_UPNP)
316     url.SetProtocol("upnp");
317   else if (m_protocol == NET_PROTOCOL_TUXBOX)
318     url.SetProtocol("tuxbox");
319   else if (m_protocol == NET_PROTOCOL_RSS)
320     url.SetProtocol("rss");
321   else if (m_protocol == NET_PROTOCOL_HTSP)
322     url.SetProtocol("htsp");
323   else if (m_protocol == NET_PROTOCOL_VTP)
324     url.SetProtocol("vtp");
325   else if (m_protocol == NET_PROTOCOL_MYTH)
326     url.SetProtocol("myth");
327   if (!m_username.IsEmpty())
328   {
329     url.SetUserName(m_username);
330     if (!m_password.IsEmpty())
331       url.SetPassword(m_password);
332   }
333   if(!m_server.IsEmpty())
334     url.SetHostName(m_server);
335   if (((m_protocol == NET_PROTOCOL_FTP) ||
336        (m_protocol == NET_PROTOCOL_HTTP) ||
337        (m_protocol == NET_PROTOCOL_HTTPS) ||
338        (m_protocol == NET_PROTOCOL_DAV) ||
339        (m_protocol == NET_PROTOCOL_DAVS) ||
340        (m_protocol == NET_PROTOCOL_RSS) ||
341        (m_protocol == NET_PROTOCOL_DAAP && !m_server.IsEmpty()) ||
342        (m_protocol == NET_PROTOCOL_HTSP) ||
343        (m_protocol == NET_PROTOCOL_VTP) ||
344        (m_protocol == NET_PROTOCOL_MYTH) ||
345        (m_protocol == NET_PROTOCOL_TUXBOX))
346       && !m_port.IsEmpty() && atoi(m_port.c_str()) > 0)
347   {
348     url.SetPort(atoi(m_port));
349   }
350   if (!m_path.IsEmpty())
351     url.SetFileName(m_path);
352   return url.Get();
353 }
354
355 void CGUIDialogNetworkSetup::SetPath(const CStdString &path)
356 {
357   CURL url(path);
358   const CStdString &protocol = url.GetProtocol();
359   if (protocol == "smb")
360     m_protocol = NET_PROTOCOL_SMB;
361   else if (protocol == "ftp")
362     m_protocol = NET_PROTOCOL_FTP;
363   else if (protocol == "http")
364     m_protocol = NET_PROTOCOL_HTTP;
365   else if (protocol == "https")
366     m_protocol = NET_PROTOCOL_HTTPS;
367   else if (protocol == "dav")
368     m_protocol = NET_PROTOCOL_DAV;
369   else if (protocol == "davs")
370     m_protocol = NET_PROTOCOL_DAVS;
371   else if (protocol == "daap")
372     m_protocol = NET_PROTOCOL_DAAP;
373   else if (protocol == "upnp")
374     m_protocol = NET_PROTOCOL_UPNP;
375   else if (protocol == "tuxbox")
376     m_protocol = NET_PROTOCOL_TUXBOX;
377   else if (protocol == "htsp")
378     m_protocol = NET_PROTOCOL_HTSP;
379   else if (protocol == "vtp")
380     m_protocol = NET_PROTOCOL_VTP;
381   else if (protocol == "myth")
382     m_protocol = NET_PROTOCOL_MYTH;
383   else if (protocol == "rss")
384     m_protocol = NET_PROTOCOL_RSS;
385   else
386     m_protocol = NET_PROTOCOL_SMB;  // default to smb
387   m_username = url.GetUserName();
388   m_password = url.GetPassWord();
389   m_port.Format("%i", url.GetPort());
390   m_server = url.GetHostName();
391   m_path = url.GetFileName();
392 }
393