2 * Copyright (C) 2005-2008 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, write to
17 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
18 * http://www.gnu.org/copyleft/gpl.html
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"
31 #include "guilib/LocalizeStrings.h"
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
41 #define CONTROL_CANCEL 19
43 CGUIDialogNetworkSetup::CGUIDialogNetworkSetup(void)
44 : CGUIDialog(WINDOW_DIALOG_NETWORK_SETUP, "DialogNetworkSetup.xml")
46 m_protocol = NET_PROTOCOL_SMB;
50 CGUIDialogNetworkSetup::~CGUIDialogNetworkSetup()
54 bool CGUIDialogNetworkSetup::OnAction(const CAction &action)
56 if (action.GetID() == ACTION_PREVIOUS_MENU)
58 return CGUIDialog::OnAction(action);
61 bool CGUIDialogNetworkSetup::OnMessage(CGUIMessage& message)
63 switch ( message.GetMessage() )
67 int iControl = message.GetSenderId();
68 if (iControl == CONTROL_PROTOCOL)
76 else if (iControl == CONTROL_SERVER_BROWSE)
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)
90 else if (iControl == CONTROL_CANCEL)
96 return CGUIDialog::OnMessage(message);
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)
103 CGUIDialogNetworkSetup *dialog = (CGUIDialogNetworkSetup *)g_windowManager.GetWindow(WINDOW_DIALOG_NETWORK_SETUP);
104 if (!dialog) return false;
105 dialog->Initialize();
106 dialog->SetPath(path);
108 path = dialog->ConstructPath();
109 return dialog->IsConfirmed();
112 void CGUIDialogNetworkSetup::OnInitWindow()
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);
121 // start as unconfirmed
124 CGUIDialog::OnInitWindow();
126 CGUISpinControlEx *pSpin = (CGUISpinControlEx *)GetControl(CONTROL_PROTOCOL);
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);
145 pSpin->SetValue(m_protocol);
149 void CGUIDialogNetworkSetup::OnServerBrowse()
151 // open a filebrowser dialog with the current address
153 CStdString path = ConstructPath();
154 // get the share as the base path
156 CStdString basePath = path;
158 while (URIUtils::GetParentPath(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))
172 void CGUIDialogNetworkSetup::OnOK()
178 void CGUIDialogNetworkSetup::OnCancel()
184 void CGUIDialogNetworkSetup::OnProtocolChange()
186 CGUISpinControlEx *pSpin = (CGUISpinControlEx *)GetControl(CONTROL_PROTOCOL);
189 m_protocol = (NET_PROTOCOL)pSpin->GetValue();
190 // set defaults for the port
191 if (m_protocol == NET_PROTOCOL_FTP)
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)
198 else if (m_protocol == NET_PROTOCOL_HTTPS || m_protocol == NET_PROTOCOL_DAVS)
200 else if (m_protocol == NET_PROTOCOL_DAAP)
202 else if (m_protocol == NET_PROTOCOL_HTSP)
204 else if (m_protocol == NET_PROTOCOL_VTP)
206 else if (m_protocol == NET_PROTOCOL_MYTH)
212 void CGUIDialogNetworkSetup::UpdateButtons()
215 SET_CONTROL_LABEL2(CONTROL_SERVER_ADDRESS, m_server);
216 if (m_protocol == NET_PROTOCOL_SMB)
218 SET_CONTROL_LABEL(CONTROL_SERVER_ADDRESS, 1010); // Server name
222 SET_CONTROL_LABEL(CONTROL_SERVER_ADDRESS, 1009); // Server Address
224 if (m_protocol == NET_PROTOCOL_DAAP)
225 SendMessage(GUI_MSG_SET_TYPE, CONTROL_SERVER_ADDRESS, CGUIEditControl::INPUT_TYPE_IPADDRESS, 1016);
227 SendMessage(GUI_MSG_SET_TYPE, CONTROL_SERVER_ADDRESS, CGUIEditControl::INPUT_TYPE_TEXT, 1016);
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)
243 SET_CONTROL_LABEL(CONTROL_REMOTE_PATH, 1011); // Remote Path
247 SET_CONTROL_LABEL(CONTROL_REMOTE_PATH, 1012); // Shared Folder
249 SendMessage(GUI_MSG_SET_TYPE, CONTROL_REMOTE_PATH, CGUIEditControl::INPUT_TYPE_TEXT, 1017);
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);
257 SendMessage(GUI_MSG_SET_TYPE, CONTROL_USERNAME, CGUIEditControl::INPUT_TYPE_TEXT, 1019);
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);
273 SendMessage(GUI_MSG_SET_TYPE, CONTROL_PORT_NUMBER, CGUIEditControl::INPUT_TYPE_NUMBER, 1018);
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);
281 SendMessage(GUI_MSG_SET_TYPE, CONTROL_PASSWORD, CGUIEditControl::INPUT_TYPE_PASSWORD, 12326);
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));
298 CStdString CGUIDialogNetworkSetup::ConstructPath() const
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())
329 url.SetUserName(m_username);
330 if (!m_password.IsEmpty())
331 url.SetPassword(m_password);
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)
348 url.SetPort(atoi(m_port));
350 if (!m_path.IsEmpty())
351 url.SetFileName(m_path);
355 void CGUIDialogNetworkSetup::SetPath(const CStdString &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;
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();