02830eba4cab1b046f6d06f5daebd5f0fe3f7e9f
[vuplus_xbmc] / xbmc / network / GUIDialogNetworkSetup.cpp
1 /*
2  *      Copyright (C) 2005-2013 Team XBMC
3  *      http://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, see
17  *  <http://www.gnu.org/licenses/>.
18  *
19  */
20
21 #include "GUIDialogNetworkSetup.h"
22 #include "guilib/GUISpinControlEx.h"
23 #include "dialogs/GUIDialogFileBrowser.h"
24 #include "guilib/GUIWindowManager.h"
25 #include "guilib/GUIEditControl.h"
26 #include "utils/URIUtils.h"
27 #include "utils/StringUtils.h"
28 #include "URL.h"
29 #include "guilib/LocalizeStrings.h"
30
31 #define CONTROL_PROTOCOL        10
32 #define CONTROL_SERVER_ADDRESS  11
33 #define CONTROL_SERVER_BROWSE   12
34 #define CONTROL_PORT_NUMBER     13
35 #define CONTROL_USERNAME        14
36 #define CONTROL_PASSWORD        15
37 #define CONTROL_REMOTE_PATH     16
38 #define CONTROL_OK              18
39 #define CONTROL_CANCEL          19
40
41 CGUIDialogNetworkSetup::CGUIDialogNetworkSetup(void)
42     : CGUIDialog(WINDOW_DIALOG_NETWORK_SETUP, "DialogNetworkSetup.xml")
43 {
44   m_protocol = NET_PROTOCOL_SMB;
45   m_confirmed = false;
46   m_loadType = KEEP_IN_MEMORY;
47 }
48
49 CGUIDialogNetworkSetup::~CGUIDialogNetworkSetup()
50 {
51 }
52
53 bool CGUIDialogNetworkSetup::OnBack(int actionID)
54 {
55   m_confirmed = false;
56   return CGUIDialog::OnBack(actionID);
57 }
58
59 bool CGUIDialogNetworkSetup::OnMessage(CGUIMessage& message)
60 {
61   switch ( message.GetMessage() )
62   {
63   case GUI_MSG_CLICKED:
64     {
65       int iControl = message.GetSenderId();
66       if (iControl == CONTROL_PROTOCOL)
67       {
68         m_server.Empty();
69         m_path.Empty();
70         m_username.Empty();
71         m_password.Empty();
72         OnProtocolChange();
73       }
74       else if (iControl == CONTROL_SERVER_BROWSE)
75         OnServerBrowse();
76       else if (iControl == CONTROL_SERVER_ADDRESS)
77         OnEditChanged(iControl, m_server);
78       else if (iControl == CONTROL_REMOTE_PATH)
79         OnEditChanged(iControl, m_path);
80       else if (iControl == CONTROL_PORT_NUMBER)
81         OnEditChanged(iControl, m_port);
82       else if (iControl == CONTROL_USERNAME)
83         OnEditChanged(iControl, m_username);
84       else if (iControl == CONTROL_PASSWORD)
85         OnEditChanged(iControl, m_password);
86       else if (iControl == CONTROL_OK)
87         OnOK();
88       else if (iControl == CONTROL_CANCEL)
89         OnCancel();
90       return true;
91     }
92     break;
93   }
94   return CGUIDialog::OnMessage(message);
95 }
96
97 // \brief Show CGUIDialogNetworkSetup dialog and prompt for a new network address.
98 // \return True if the network address is valid, false otherwise.
99 bool CGUIDialogNetworkSetup::ShowAndGetNetworkAddress(CStdString &path)
100 {
101   CGUIDialogNetworkSetup *dialog = (CGUIDialogNetworkSetup *)g_windowManager.GetWindow(WINDOW_DIALOG_NETWORK_SETUP);
102   if (!dialog) return false;
103   dialog->Initialize();
104   dialog->SetPath(path);
105   dialog->DoModal();
106   path = dialog->ConstructPath();
107   return dialog->IsConfirmed();
108 }
109
110 void CGUIDialogNetworkSetup::OnWindowLoaded()
111 {
112   // replace our buttons with edits
113   ChangeButtonToEdit(CONTROL_SERVER_ADDRESS);
114   ChangeButtonToEdit(CONTROL_REMOTE_PATH);
115   ChangeButtonToEdit(CONTROL_USERNAME);
116   ChangeButtonToEdit(CONTROL_PORT_NUMBER);
117   ChangeButtonToEdit(CONTROL_PASSWORD);
118
119   CGUIDialog::OnWindowLoaded();
120 }
121
122 void CGUIDialogNetworkSetup::OnInitWindow()
123 {
124   // start as unconfirmed
125   m_confirmed = false;
126
127   CGUIDialog::OnInitWindow();
128   // Add our protocols
129   CGUISpinControlEx *pSpin = (CGUISpinControlEx *)GetControl(CONTROL_PROTOCOL);
130   if (!pSpin)
131     return;
132
133   pSpin->Clear();
134 #ifdef HAS_FILESYSTEM_SMB
135   pSpin->AddLabel(g_localizeStrings.Get(20171), NET_PROTOCOL_SMB);
136 #endif
137   pSpin->AddLabel(g_localizeStrings.Get(20256), NET_PROTOCOL_HTSP);
138   pSpin->AddLabel(g_localizeStrings.Get(20257), NET_PROTOCOL_VTP);
139 #ifdef HAS_MYSQL
140   pSpin->AddLabel(g_localizeStrings.Get(20258), NET_PROTOCOL_MYTH);
141 #endif
142   pSpin->AddLabel(g_localizeStrings.Get(21331), NET_PROTOCOL_TUXBOX);
143   pSpin->AddLabel(g_localizeStrings.Get(20301), NET_PROTOCOL_HTTPS);
144   pSpin->AddLabel(g_localizeStrings.Get(20300), NET_PROTOCOL_HTTP);
145   pSpin->AddLabel(g_localizeStrings.Get(20254), NET_PROTOCOL_DAVS);
146   pSpin->AddLabel(g_localizeStrings.Get(20253), NET_PROTOCOL_DAV);
147   pSpin->AddLabel(g_localizeStrings.Get(20173), NET_PROTOCOL_FTP);
148   pSpin->AddLabel(g_localizeStrings.Get(20174), NET_PROTOCOL_DAAP);
149   pSpin->AddLabel(g_localizeStrings.Get(20175), NET_PROTOCOL_UPNP);
150   pSpin->AddLabel(g_localizeStrings.Get(20304), NET_PROTOCOL_RSS);
151 #ifdef HAS_FILESYSTEM_NFS
152   pSpin->AddLabel(g_localizeStrings.Get(20259), NET_PROTOCOL_NFS);
153 #endif
154 #ifdef HAS_FILESYSTEM_SFTP
155   pSpin->AddLabel(g_localizeStrings.Get(20260), NET_PROTOCOL_SFTP);
156 #endif
157 #ifdef HAS_FILESYSTEM_AFP
158   pSpin->AddLabel(g_localizeStrings.Get(20261), NET_PROTOCOL_AFP);
159 #endif
160
161   pSpin->SetValue(m_protocol);
162   OnProtocolChange();
163 }
164
165 void CGUIDialogNetworkSetup::OnDeinitWindow(int nextWindowID)
166 {
167   // clear protocol spinner
168   CGUISpinControlEx *pSpin = (CGUISpinControlEx *)GetControl(CONTROL_PROTOCOL);
169   if (pSpin)
170     pSpin->Clear();
171
172   CGUIDialog::OnDeinitWindow(nextWindowID);
173 }
174
175 void CGUIDialogNetworkSetup::OnServerBrowse()
176 {
177   // open a filebrowser dialog with the current address
178   VECSOURCES shares;
179   CStdString path = ConstructPath();
180   // get the share as the base path
181   CMediaSource share;
182   CStdString basePath = path;
183   CStdString tempPath;
184   while (URIUtils::GetParentPath(basePath, tempPath))
185     basePath = tempPath;
186   share.strPath = basePath;
187   // don't include the user details in the share name
188   CURL url(share.strPath);
189   share.strName = url.GetWithoutUserDetails();
190   shares.push_back(share);
191   if (CGUIDialogFileBrowser::ShowAndGetDirectory(shares, g_localizeStrings.Get(1015), path))
192   {
193     SetPath(path);
194     UpdateButtons();
195   }
196 }
197
198 void CGUIDialogNetworkSetup::OnOK()
199 {
200   m_confirmed = true;
201   Close();
202 }
203
204 void CGUIDialogNetworkSetup::OnCancel()
205 {
206   m_confirmed = false;
207   Close();
208 }
209
210 void CGUIDialogNetworkSetup::OnProtocolChange()
211 {
212   CGUISpinControlEx *pSpin = (CGUISpinControlEx *)GetControl(CONTROL_PROTOCOL);
213   if (!pSpin)
214     return;
215   m_protocol = (NET_PROTOCOL)pSpin->GetValue();
216   // set defaults for the port
217   if (m_protocol == NET_PROTOCOL_FTP)
218     m_port = "21";
219   else if (m_protocol == NET_PROTOCOL_HTTP || 
220            m_protocol == NET_PROTOCOL_RSS || 
221            m_protocol == NET_PROTOCOL_TUXBOX || 
222            m_protocol == NET_PROTOCOL_DAV)
223     m_port = "80";
224   else if (m_protocol == NET_PROTOCOL_HTTPS || m_protocol == NET_PROTOCOL_DAVS)
225     m_port = "443";
226   else if (m_protocol == NET_PROTOCOL_DAAP)
227     m_port = "3689";
228   else if (m_protocol == NET_PROTOCOL_HTSP)
229     m_port = "9982";
230   else if (m_protocol == NET_PROTOCOL_VTP)
231     m_port = "2004";
232   else if (m_protocol == NET_PROTOCOL_MYTH)
233     m_port = "6543";
234   else if (m_protocol == NET_PROTOCOL_SFTP)
235     m_port = "22";
236   else
237     m_port = "0";
238
239   UpdateButtons();
240 }
241
242 void CGUIDialogNetworkSetup::UpdateButtons()
243 {
244   // Address label
245   SET_CONTROL_LABEL2(CONTROL_SERVER_ADDRESS, m_server);
246   if (m_protocol == NET_PROTOCOL_SMB)
247   {
248     SET_CONTROL_LABEL(CONTROL_SERVER_ADDRESS, 1010);  // Server name
249   }
250   else
251   {
252     SET_CONTROL_LABEL(CONTROL_SERVER_ADDRESS, 1009);  // Server Address
253   }
254   if (m_protocol == NET_PROTOCOL_DAAP)
255     SendMessage(GUI_MSG_SET_TYPE, CONTROL_SERVER_ADDRESS, CGUIEditControl::INPUT_TYPE_IPADDRESS, 1016);
256   else
257     SendMessage(GUI_MSG_SET_TYPE, CONTROL_SERVER_ADDRESS, CGUIEditControl::INPUT_TYPE_TEXT, 1016);
258   // remote path
259   SET_CONTROL_LABEL2(CONTROL_REMOTE_PATH, m_path);
260   CONTROL_ENABLE_ON_CONDITION(CONTROL_REMOTE_PATH, m_protocol != NET_PROTOCOL_DAAP &&
261                                                    m_protocol != NET_PROTOCOL_UPNP &&
262                                                    m_protocol != NET_PROTOCOL_TUXBOX &&
263                                                    m_protocol != NET_PROTOCOL_HTSP &&
264                                                    m_protocol != NET_PROTOCOL_VTP &&
265                                                    m_protocol != NET_PROTOCOL_MYTH);
266   if (m_protocol == NET_PROTOCOL_FTP ||
267       m_protocol == NET_PROTOCOL_HTTP ||
268       m_protocol == NET_PROTOCOL_HTTPS ||
269       m_protocol == NET_PROTOCOL_RSS ||
270       m_protocol == NET_PROTOCOL_DAV ||
271       m_protocol == NET_PROTOCOL_DAVS||
272       m_protocol == NET_PROTOCOL_SFTP||
273       m_protocol == NET_PROTOCOL_NFS)
274   {
275     SET_CONTROL_LABEL(CONTROL_REMOTE_PATH, 1011);  // Remote Path
276   }
277   else
278   {
279     SET_CONTROL_LABEL(CONTROL_REMOTE_PATH, 1012);  // Shared Folder
280   }
281   SendMessage(GUI_MSG_SET_TYPE, CONTROL_REMOTE_PATH, CGUIEditControl::INPUT_TYPE_TEXT, 1017);
282
283   // username
284   SET_CONTROL_LABEL2(CONTROL_USERNAME, m_username);
285   CONTROL_ENABLE_ON_CONDITION(CONTROL_USERNAME, m_protocol != NET_PROTOCOL_DAAP &&
286                                                 m_protocol != NET_PROTOCOL_VTP &&
287                                                 m_protocol != NET_PROTOCOL_UPNP &&
288                                                 m_protocol != NET_PROTOCOL_NFS);
289
290   SendMessage(GUI_MSG_SET_TYPE, CONTROL_USERNAME, CGUIEditControl::INPUT_TYPE_TEXT, 1019);
291
292   // port
293   SET_CONTROL_LABEL2(CONTROL_PORT_NUMBER, m_port);
294   CONTROL_ENABLE_ON_CONDITION(CONTROL_PORT_NUMBER, m_protocol == NET_PROTOCOL_FTP ||
295                                                    m_protocol == NET_PROTOCOL_HTTP ||
296                                                    m_protocol == NET_PROTOCOL_HTTPS ||
297                                                    m_protocol == NET_PROTOCOL_DAV ||
298                                                    m_protocol == NET_PROTOCOL_DAVS ||
299                                                    m_protocol == NET_PROTOCOL_TUXBOX ||
300                                                    m_protocol == NET_PROTOCOL_HTSP ||
301                                                    m_protocol == NET_PROTOCOL_VTP ||
302                                                    m_protocol == NET_PROTOCOL_MYTH ||
303                                                    m_protocol == NET_PROTOCOL_RSS ||
304                                                    m_protocol == NET_PROTOCOL_DAAP ||
305                                                    m_protocol == NET_PROTOCOL_SFTP);
306
307   SendMessage(GUI_MSG_SET_TYPE, CONTROL_PORT_NUMBER, CGUIEditControl::INPUT_TYPE_NUMBER, 1018);
308
309   // password
310   SET_CONTROL_LABEL2(CONTROL_PASSWORD, m_password);
311   CONTROL_ENABLE_ON_CONDITION(CONTROL_PASSWORD, m_protocol != NET_PROTOCOL_DAAP &&
312                                                 m_protocol != NET_PROTOCOL_VTP &&
313                                                 m_protocol != NET_PROTOCOL_UPNP &&
314                                                 m_protocol != NET_PROTOCOL_NFS);
315
316   SendMessage(GUI_MSG_SET_TYPE, CONTROL_PASSWORD, CGUIEditControl::INPUT_TYPE_PASSWORD, 12326);
317
318   // TODO: FIX BETTER DAAP SUPPORT
319   // server browse should be disabled if we are in DAAP, FTP, HTTP, HTTPS, RSS, HTSP, VTP, TUXBOX, DAV or DAVS
320   CONTROL_ENABLE_ON_CONDITION(CONTROL_SERVER_BROWSE, !m_server.IsEmpty() || !(m_protocol == NET_PROTOCOL_FTP ||
321                                                                               m_protocol == NET_PROTOCOL_HTTP ||
322                                                                               m_protocol == NET_PROTOCOL_HTTPS ||
323                                                                               m_protocol == NET_PROTOCOL_DAV ||
324                                                                               m_protocol == NET_PROTOCOL_DAVS ||
325                                                                               m_protocol == NET_PROTOCOL_DAAP ||
326                                                                               m_protocol == NET_PROTOCOL_RSS ||
327                                                                               m_protocol == NET_PROTOCOL_HTSP ||
328                                                                               m_protocol == NET_PROTOCOL_VTP ||
329                                                                               m_protocol == NET_PROTOCOL_MYTH ||
330                                                                               m_protocol == NET_PROTOCOL_TUXBOX||
331                                                                               m_protocol == NET_PROTOCOL_SFTP ||
332                                                                               m_protocol == NET_PROTOCOL_AFP));
333 }
334
335 CStdString CGUIDialogNetworkSetup::ConstructPath() const
336 {
337   CURL url;
338   if (m_protocol == NET_PROTOCOL_SMB)
339     url.SetProtocol("smb");
340   else if (m_protocol == NET_PROTOCOL_FTP)
341     url.SetProtocol("ftp");
342   else if (m_protocol == NET_PROTOCOL_HTTP)
343     url.SetProtocol("http");
344   else if (m_protocol == NET_PROTOCOL_HTTPS)
345     url.SetProtocol("https");
346   else if (m_protocol == NET_PROTOCOL_DAV)
347     url.SetProtocol("dav");
348   else if (m_protocol == NET_PROTOCOL_DAVS)
349     url.SetProtocol("davs");
350   else if (m_protocol == NET_PROTOCOL_DAAP)
351     url.SetProtocol("daap");
352   else if (m_protocol == NET_PROTOCOL_UPNP)
353     url.SetProtocol("upnp");
354   else if (m_protocol == NET_PROTOCOL_TUXBOX)
355     url.SetProtocol("tuxbox");
356   else if (m_protocol == NET_PROTOCOL_RSS)
357     url.SetProtocol("rss");
358   else if (m_protocol == NET_PROTOCOL_HTSP)
359     url.SetProtocol("htsp");
360   else if (m_protocol == NET_PROTOCOL_VTP)
361     url.SetProtocol("vtp");
362   else if (m_protocol == NET_PROTOCOL_MYTH)
363     url.SetProtocol("myth");
364   else if (m_protocol == NET_PROTOCOL_NFS)
365     url.SetProtocol("nfs");
366   else if (m_protocol == NET_PROTOCOL_SFTP)
367     url.SetProtocol("sftp");
368   else if (m_protocol == NET_PROTOCOL_AFP)
369     url.SetProtocol("afp");
370     
371   if (!m_username.IsEmpty())
372   {
373     url.SetUserName(m_username);
374     if (!m_password.IsEmpty())
375       url.SetPassword(m_password);
376   }
377   if(!m_server.IsEmpty())
378     url.SetHostName(m_server);
379   if (((m_protocol == NET_PROTOCOL_FTP) ||
380        (m_protocol == NET_PROTOCOL_HTTP) ||
381        (m_protocol == NET_PROTOCOL_HTTPS) ||
382        (m_protocol == NET_PROTOCOL_DAV) ||
383        (m_protocol == NET_PROTOCOL_DAVS) ||
384        (m_protocol == NET_PROTOCOL_RSS) ||
385        (m_protocol == NET_PROTOCOL_DAAP && !m_server.IsEmpty()) ||
386        (m_protocol == NET_PROTOCOL_HTSP) ||
387        (m_protocol == NET_PROTOCOL_VTP) ||
388        (m_protocol == NET_PROTOCOL_MYTH) ||
389        (m_protocol == NET_PROTOCOL_TUXBOX) ||
390        (m_protocol == NET_PROTOCOL_SFTP) ||
391        (m_protocol == NET_PROTOCOL_NFS))
392       && !m_port.IsEmpty() && atoi(m_port.c_str()) > 0)
393   {
394     url.SetPort(atoi(m_port));
395   }
396   if (!m_path.IsEmpty())
397     url.SetFileName(m_path);
398   return url.Get();
399 }
400
401 void CGUIDialogNetworkSetup::SetPath(const CStdString &path)
402 {
403   CURL url(path);
404   const CStdString &protocol = url.GetProtocol();
405   if (protocol == "smb")
406     m_protocol = NET_PROTOCOL_SMB;
407   else if (protocol == "ftp")
408     m_protocol = NET_PROTOCOL_FTP;
409   else if (protocol == "http")
410     m_protocol = NET_PROTOCOL_HTTP;
411   else if (protocol == "https")
412     m_protocol = NET_PROTOCOL_HTTPS;
413   else if (protocol == "dav")
414     m_protocol = NET_PROTOCOL_DAV;
415   else if (protocol == "davs")
416     m_protocol = NET_PROTOCOL_DAVS;
417   else if (protocol == "daap")
418     m_protocol = NET_PROTOCOL_DAAP;
419   else if (protocol == "upnp")
420     m_protocol = NET_PROTOCOL_UPNP;
421   else if (protocol == "tuxbox")
422     m_protocol = NET_PROTOCOL_TUXBOX;
423   else if (protocol == "htsp")
424     m_protocol = NET_PROTOCOL_HTSP;
425   else if (protocol == "vtp")
426     m_protocol = NET_PROTOCOL_VTP;
427   else if (protocol == "myth")
428     m_protocol = NET_PROTOCOL_MYTH;
429   else if (protocol == "rss")
430     m_protocol = NET_PROTOCOL_RSS;
431   else if (protocol == "nfs")
432     m_protocol = NET_PROTOCOL_NFS;
433   else if (protocol == "sftp" || protocol == "ssh")
434     m_protocol = NET_PROTOCOL_SFTP;
435   else if (protocol == "afp")
436     m_protocol = NET_PROTOCOL_AFP;
437   else
438     m_protocol = NET_PROTOCOL_SMB;  // default to smb
439   m_username = url.GetUserName();
440   m_password = url.GetPassWord();
441   m_port = StringUtils::Format("%i", url.GetPort());
442   m_server = url.GetHostName();
443   m_path = url.GetFileName();
444   URIUtils::RemoveSlashAtEnd(m_path);
445 }
446