2 * Copyright (C) 2005-2013 Team XBMC
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)
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.
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/>.
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"
29 #include "guilib/LocalizeStrings.h"
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
39 #define CONTROL_CANCEL 19
41 CGUIDialogNetworkSetup::CGUIDialogNetworkSetup(void)
42 : CGUIDialog(WINDOW_DIALOG_NETWORK_SETUP, "DialogNetworkSetup.xml")
44 m_protocol = NET_PROTOCOL_SMB;
46 m_loadType = KEEP_IN_MEMORY;
49 CGUIDialogNetworkSetup::~CGUIDialogNetworkSetup()
53 bool CGUIDialogNetworkSetup::OnBack(int actionID)
56 return CGUIDialog::OnBack(actionID);
59 bool CGUIDialogNetworkSetup::OnMessage(CGUIMessage& message)
61 switch ( message.GetMessage() )
65 int iControl = message.GetSenderId();
66 if (iControl == CONTROL_PROTOCOL)
74 else if (iControl == CONTROL_SERVER_BROWSE)
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)
88 else if (iControl == CONTROL_CANCEL)
94 return CGUIDialog::OnMessage(message);
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)
101 CGUIDialogNetworkSetup *dialog = (CGUIDialogNetworkSetup *)g_windowManager.GetWindow(WINDOW_DIALOG_NETWORK_SETUP);
102 if (!dialog) return false;
103 dialog->Initialize();
104 dialog->SetPath(path);
106 path = dialog->ConstructPath();
107 return dialog->IsConfirmed();
110 void CGUIDialogNetworkSetup::OnWindowLoaded()
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);
119 CGUIDialog::OnWindowLoaded();
122 void CGUIDialogNetworkSetup::OnInitWindow()
124 // start as unconfirmed
127 CGUIDialog::OnInitWindow();
129 CGUISpinControlEx *pSpin = (CGUISpinControlEx *)GetControl(CONTROL_PROTOCOL);
134 #ifdef HAS_FILESYSTEM_SMB
135 pSpin->AddLabel(g_localizeStrings.Get(20171), NET_PROTOCOL_SMB);
137 pSpin->AddLabel(g_localizeStrings.Get(20256), NET_PROTOCOL_HTSP);
138 pSpin->AddLabel(g_localizeStrings.Get(20257), NET_PROTOCOL_VTP);
140 pSpin->AddLabel(g_localizeStrings.Get(20258), NET_PROTOCOL_MYTH);
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);
154 #ifdef HAS_FILESYSTEM_SFTP
155 pSpin->AddLabel(g_localizeStrings.Get(20260), NET_PROTOCOL_SFTP);
157 #ifdef HAS_FILESYSTEM_AFP
158 pSpin->AddLabel(g_localizeStrings.Get(20261), NET_PROTOCOL_AFP);
161 pSpin->SetValue(m_protocol);
165 void CGUIDialogNetworkSetup::OnDeinitWindow(int nextWindowID)
167 // clear protocol spinner
168 CGUISpinControlEx *pSpin = (CGUISpinControlEx *)GetControl(CONTROL_PROTOCOL);
172 CGUIDialog::OnDeinitWindow(nextWindowID);
175 void CGUIDialogNetworkSetup::OnServerBrowse()
177 // open a filebrowser dialog with the current address
179 CStdString path = ConstructPath();
180 // get the share as the base path
182 CStdString basePath = path;
184 while (URIUtils::GetParentPath(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))
198 void CGUIDialogNetworkSetup::OnOK()
204 void CGUIDialogNetworkSetup::OnCancel()
210 void CGUIDialogNetworkSetup::OnProtocolChange()
212 CGUISpinControlEx *pSpin = (CGUISpinControlEx *)GetControl(CONTROL_PROTOCOL);
215 m_protocol = (NET_PROTOCOL)pSpin->GetValue();
216 // set defaults for the port
217 if (m_protocol == NET_PROTOCOL_FTP)
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)
224 else if (m_protocol == NET_PROTOCOL_HTTPS || m_protocol == NET_PROTOCOL_DAVS)
226 else if (m_protocol == NET_PROTOCOL_DAAP)
228 else if (m_protocol == NET_PROTOCOL_HTSP)
230 else if (m_protocol == NET_PROTOCOL_VTP)
232 else if (m_protocol == NET_PROTOCOL_MYTH)
234 else if (m_protocol == NET_PROTOCOL_SFTP)
242 void CGUIDialogNetworkSetup::UpdateButtons()
245 SET_CONTROL_LABEL2(CONTROL_SERVER_ADDRESS, m_server);
246 if (m_protocol == NET_PROTOCOL_SMB)
248 SET_CONTROL_LABEL(CONTROL_SERVER_ADDRESS, 1010); // Server name
252 SET_CONTROL_LABEL(CONTROL_SERVER_ADDRESS, 1009); // Server Address
254 if (m_protocol == NET_PROTOCOL_DAAP)
255 SendMessage(GUI_MSG_SET_TYPE, CONTROL_SERVER_ADDRESS, CGUIEditControl::INPUT_TYPE_IPADDRESS, 1016);
257 SendMessage(GUI_MSG_SET_TYPE, CONTROL_SERVER_ADDRESS, CGUIEditControl::INPUT_TYPE_TEXT, 1016);
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)
275 SET_CONTROL_LABEL(CONTROL_REMOTE_PATH, 1011); // Remote Path
279 SET_CONTROL_LABEL(CONTROL_REMOTE_PATH, 1012); // Shared Folder
281 SendMessage(GUI_MSG_SET_TYPE, CONTROL_REMOTE_PATH, CGUIEditControl::INPUT_TYPE_TEXT, 1017);
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);
290 SendMessage(GUI_MSG_SET_TYPE, CONTROL_USERNAME, CGUIEditControl::INPUT_TYPE_TEXT, 1019);
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);
307 SendMessage(GUI_MSG_SET_TYPE, CONTROL_PORT_NUMBER, CGUIEditControl::INPUT_TYPE_NUMBER, 1018);
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);
316 SendMessage(GUI_MSG_SET_TYPE, CONTROL_PASSWORD, CGUIEditControl::INPUT_TYPE_PASSWORD, 12326);
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.empty() || !(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));
335 CStdString CGUIDialogNetworkSetup::ConstructPath() const
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");
371 if (!m_username.empty())
373 url.SetUserName(m_username);
374 if (!m_password.empty())
375 url.SetPassword(m_password);
377 if(!m_server.empty())
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.empty()) ||
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.empty() && atoi(m_port.c_str()) > 0)
394 url.SetPort(atoi(m_port));
397 url.SetFileName(m_path);
401 void CGUIDialogNetworkSetup::SetPath(const CStdString &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;
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);