00001
00002
00003
00004
00005
00006 #include "stdafx.h"
00007 #include "Web Interface.h"
00008 #include "Web InterfaceDlg.h"
00009 #include "fsAutorunMgr.h"
00010
00011 #include <afxpriv.h>
00012 #define DELETE_EXCEPTION(e) do { e->Delete(); } while (0)
00013
00014 #ifdef _DEBUG
00015 #define new DEBUG_NEW
00016 #undef THIS_FILE
00017 static char THIS_FILE[] = __FILE__;
00018 #endif
00019
00020 #define WM_TRAYMSG (WM_APP + 100)
00021
00022 CWebInterfaceDlg::CWebInterfaceDlg(CWnd* pParent )
00023 : CDialog(CWebInterfaceDlg::IDD, pParent),
00024 m_http (&m_fdmServer)
00025 {
00026
00027
00028
00029 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
00030
00031 m_nShutdownMsg = RegisterWindowMessage ("ODM - remote control server - shutdown");
00032 }
00033
00034 void CWebInterfaceDlg::DoDataExchange(CDataExchange* pDX)
00035 {
00036 CDialog::DoDataExchange(pDX);
00037
00038
00039
00040 }
00041
00042 BEGIN_MESSAGE_MAP(CWebInterfaceDlg, CDialog)
00043
00044 ON_WM_QUERYDRAGICON()
00045 ON_WM_DESTROY()
00046 ON_COMMAND(ID_CONFIGURE, OnConfigure)
00047 ON_COMMAND(ID_SHUTDOWN, OnShutdown)
00048 ON_BN_CLICKED(IDC_OPEN_IN_BROWSER, OnOpenInBrowser)
00049 ON_MESSAGE (WM_TRAYMSG, OnTrayMsg)
00050 ON_BN_CLICKED(IDC_USEPWD, OnUsepwd)
00051
00052 END_MESSAGE_MAP()
00053
00054 BOOL CWebInterfaceDlg::OnInitDialog()
00055 {
00056 CDialog::OnInitDialog();
00057
00058 SetIcon(m_hIcon, TRUE);
00059 SetIcon(m_hIcon, FALSE);
00060
00061 UINT nIdIcon = IDR_MAINFRAME;
00062 _TrayMgr.Create (m_hWnd, &nIdIcon, 1, "ODM remote control server", WM_TRAYMSG);
00063
00064 m_http.set_Port (AfxGetApp ()->GetProfileInt ("Network", "Port", 80));
00065 SetDlgItemInt (IDC_PORT, m_http.get_Port ());
00066
00067 m_http.Start ();
00068
00069 UpdateAddress ();
00070
00071 fsAutorunMgr am;
00072 CheckDlgButton (IDC_AUTORUN, am.IsAutoStart () ? BST_CHECKED : BST_UNCHECKED);
00073
00074 CString strU = AfxGetApp ()->GetProfileString ("Network", "Login", "");
00075 CString strP = AfxGetApp ()->GetProfileString ("Network", "Password", "");
00076
00077 if (strU.IsEmpty () == FALSE)
00078 {
00079 CheckDlgButton (IDC_USEPWD, BST_CHECKED);
00080 SetDlgItemText (IDC_USER, strU);
00081 SetDlgItemText (IDC_PWD, strP);
00082 }
00083
00084 UpdateEnabled ();
00085
00086 return TRUE;
00087 }
00088
00089 HCURSOR CWebInterfaceDlg::OnQueryDragIcon()
00090 {
00091 return (HCURSOR) m_hIcon;
00092 }
00093
00094 void CWebInterfaceDlg::OnDestroy()
00095 {
00096 CDialog::OnDestroy();
00097
00098 _TrayMgr.Remove ();
00099 }
00100
00101 LRESULT CWebInterfaceDlg::OnTrayMsg(WPARAM, LPARAM lp)
00102 {
00103 switch (lp)
00104 {
00105 case WM_MOUSEMOVE:
00106 break;
00107
00108 case WM_LBUTTONDOWN:
00109 OnConfigure ();
00110 break;
00111
00112 case WM_RBUTTONUP:
00113 ShowTrayMenu ();
00114 break;
00115 }
00116
00117 return 0;
00118 }
00119
00120 void CWebInterfaceDlg::ShowTrayMenu()
00121 {
00122 CMenu menu;
00123 menu.LoadMenu (IDM_TRAY);
00124 CMenu *pPopup = menu.GetSubMenu (0);
00125
00126 SetForegroundWindow ();
00127
00128 pPopup->SetDefaultItem (ID_CONFIGURE);
00129
00130 CPoint pt;
00131 GetCursorPos (&pt);
00132
00133 pPopup->TrackPopupMenu (TPM_RIGHTBUTTON | TPM_BOTTOMALIGN | TPM_RIGHTALIGN, pt.x, pt.y, this);
00134
00135 menu.DestroyMenu ();
00136 }
00137
00138 void CWebInterfaceDlg::OnConfigure()
00139 {
00140 if (IsWindowVisible ())
00141 {
00142 ShowWindow (SW_HIDE);
00143 }
00144 else
00145 {
00146 ShowWindow (SW_SHOW);
00147 ShowWindow (SW_RESTORE);
00148 SetForegroundWindow ();
00149 }
00150 }
00151
00152 void CWebInterfaceDlg::OnShutdown()
00153 {
00154 EndDialog (IDOK);
00155 }
00156
00157 void CWebInterfaceDlg::OnOK()
00158 {
00159 UINT nPort = GetDlgItemInt (IDC_PORT);
00160 if (nPort > 0xffff)
00161 {
00162 SetDlgItemInt (IDC_PORT, m_http.get_Port (), FALSE);
00163 return;
00164 }
00165
00166 if (nPort != m_http.get_Port ())
00167 {
00168 AfxGetApp ()->WriteProfileInt ("Network", "Port", nPort);
00169
00170 m_http.Shutdown ();
00171 m_http.set_Port (nPort);
00172 m_http.Start ();
00173
00174 UpdateAddress ();
00175 }
00176
00177 fsAutorunMgr am;
00178 am.SetAutoStart (IsDlgButtonChecked (IDC_AUTORUN) == BST_CHECKED);
00179
00180 CString strU, strP;
00181 if (IsDlgButtonChecked (IDC_USEPWD) == BST_CHECKED)
00182 {
00183 GetDlgItemText (IDC_USER, strU);
00184 GetDlgItemText (IDC_PWD, strP);
00185 AfxGetApp ()->WriteProfileString ("Network", "Login", strU);
00186 AfxGetApp ()->WriteProfileString ("Network", "Password", strP);
00187 }
00188 else
00189 {
00190 AfxGetApp ()->WriteProfileString ("Network", "Login", "");
00191 AfxGetApp ()->WriteProfileString ("Network", "Password", "");
00192 }
00193
00194 ShowWindow (SW_HIDE);
00195 }
00196
00197 void CWebInterfaceDlg::OnCancel()
00198 {
00199 ShowWindow (SW_HIDE);
00200 }
00201
00202 int CWebInterfaceDlg::DoModal()
00203 {
00204
00205 ASSERT(m_lpszTemplateName != NULL || m_hDialogTemplate != NULL ||
00206 m_lpDialogTemplate != NULL);
00207
00208
00209 LPCDLGTEMPLATE lpDialogTemplate = m_lpDialogTemplate;
00210 HGLOBAL hDialogTemplate = m_hDialogTemplate;
00211 HINSTANCE hInst = AfxGetResourceHandle();
00212 if (m_lpszTemplateName != NULL)
00213 {
00214 hInst = AfxFindResourceHandle(m_lpszTemplateName, RT_DIALOG);
00215 HRSRC hResource = ::FindResource(hInst, m_lpszTemplateName, RT_DIALOG);
00216 hDialogTemplate = LoadResource(hInst, hResource);
00217 }
00218 if (hDialogTemplate != NULL)
00219 lpDialogTemplate = (LPCDLGTEMPLATE)LockResource(hDialogTemplate);
00220
00221
00222 if (lpDialogTemplate == NULL)
00223 return -1;
00224
00225
00226 HWND hWndParent = PreModal();
00227 AfxUnhookWindowCreate();
00228 BOOL bEnableParent = FALSE;
00229 if (hWndParent != NULL && ::IsWindowEnabled(hWndParent))
00230 {
00231 ::EnableWindow(hWndParent, FALSE);
00232 bEnableParent = TRUE;
00233 }
00234
00235 TRY
00236 {
00237
00238 AfxHookWindowCreate(this);
00239 if (CreateDlgIndirect(lpDialogTemplate,
00240 CWnd::FromHandle(hWndParent), hInst))
00241 {
00242 if (m_nFlags & WF_CONTINUEMODAL)
00243 {
00244
00245 DWORD dwFlags = 0;
00246 if (GetStyle() & DS_NOIDLEMSG)
00247 dwFlags |= MLF_NOIDLEMSG;
00248 VERIFY(RunModalLoop(dwFlags) == m_nModalResult);
00249 }
00250
00251
00252 if (m_hWnd != NULL)
00253 SetWindowPos(NULL, 0, 0, 0, 0, SWP_HIDEWINDOW|
00254 SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE|SWP_NOZORDER);
00255 }
00256 }
00257 CATCH_ALL(e)
00258 {
00259 DELETE_EXCEPTION(e);
00260 m_nModalResult = -1;
00261 }
00262 END_CATCH_ALL
00263
00264 if (bEnableParent)
00265 ::EnableWindow(hWndParent, TRUE);
00266 if (hWndParent != NULL && ::GetActiveWindow() == m_hWnd)
00267 ::SetActiveWindow(hWndParent);
00268
00269
00270 DestroyWindow();
00271 PostModal();
00272
00273
00274 if (m_lpszTemplateName != NULL || m_hDialogTemplate != NULL)
00275 UnlockResource(hDialogTemplate);
00276 if (m_lpszTemplateName != NULL)
00277 FreeResource(hDialogTemplate);
00278
00279 return m_nModalResult;
00280 }
00281
00282 void CWebInterfaceDlg::UpdateAddress()
00283 {
00284 char szName [10000];
00285 gethostname (szName, 10000);
00286
00287 hostent *he = gethostbyname (szName);
00288
00289 int nPort = GetDlgItemInt (IDC_PORT);
00290
00291 CString str;
00292 if (nPort == 80)
00293 {
00294 str.Format ("http://%d.%d.%d.%d/", (int)(BYTE)he->h_addr_list [0][0],
00295 (int)(BYTE)he->h_addr_list [0][1], (int)(BYTE)he->h_addr_list [0][2],
00296 (int)(BYTE)he->h_addr_list [0][3]);
00297 }
00298 else
00299 {
00300 str.Format ("http://%d.%d.%d.%d:%d/", (int)(BYTE)he->h_addr_list [0][0],
00301 (int)(BYTE)he->h_addr_list [0][1], (int)(BYTE)he->h_addr_list [0][2],
00302 (int)(BYTE)he->h_addr_list [0][3], nPort);
00303 }
00304
00305 SetDlgItemText (IDC_SERV_ADDR, str);
00306 }
00307
00308 void fsOpenUrlInBrowser (LPCSTR pszUrl)
00309 {
00310 char szReg [100];
00311 char szBrowser [1000];
00312 DWORD dwBrowserLen = 1000;
00313
00314 if (strnicmp (pszUrl, "http", 4) == 0)
00315 strcpy (szReg, "http");
00316 else if (strnicmp (pszUrl, "https", 5) == 0)
00317 strcpy (szReg, "https");
00318 else
00319 strcpy (szReg, "ftp");
00320
00321 strcat (szReg, "\\shell\\open\\command");
00322
00323 HKEY hReg;
00324
00325 if (ERROR_SUCCESS != RegOpenKeyEx (HKEY_CLASSES_ROOT, szReg, 0, KEY_QUERY_VALUE, &hReg))
00326 goto _lErr;
00327
00328 if (ERROR_SUCCESS != RegQueryValueEx (hReg, NULL, NULL, NULL, (LPBYTE) szBrowser, &dwBrowserLen))
00329 {
00330 RegCloseKey (hReg);
00331 goto _lErr;
00332 }
00333
00334 RegCloseKey (hReg);
00335
00336 strlwr (szBrowser);
00337 LPSTR pszExe;
00338 pszExe = strstr (szBrowser, ".exe");
00339
00340 if (pszExe == NULL)
00341 goto _lErr;
00342
00343 pszExe [4] = 0;
00344
00345 if (32 >= (int)ShellExecute (HWND_DESKTOP, "open", szBrowser [0] == '"' ? szBrowser+1 : szBrowser, pszUrl, NULL, SW_SHOW))
00346 goto _lErr;
00347
00348 return;
00349
00350 _lErr:
00351 if (32 >= (int) ShellExecute (HWND_DESKTOP, "open", pszUrl, NULL, NULL, SW_SHOW))
00352 MessageBox (NULL, pszUrl, "failed to open", MB_ICONERROR);
00353 }
00354
00355 void CWebInterfaceDlg::OnOpenInBrowser()
00356 {
00357 OnOK ();
00358 CString str;
00359 GetDlgItemText (IDC_SERV_ADDR, str);
00360 fsOpenUrlInBrowser (str);
00361 }
00362
00363 LRESULT CWebInterfaceDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
00364 {
00365 if (message == m_nShutdownMsg)
00366 {
00367 fsAutorunMgr am;
00368 am.SetAutoStart (FALSE);
00369 EndDialog (IDOK);
00370 return 0;
00371 }
00372
00373 return CDialog::WindowProc(message, wParam, lParam);
00374 }
00375
00376 void CWebInterfaceDlg::OnUsepwd()
00377 {
00378 UpdateEnabled ();
00379 }
00380
00381 void CWebInterfaceDlg::UpdateEnabled()
00382 {
00383 BOOL b = IsDlgButtonChecked (IDC_USEPWD) == BST_CHECKED;
00384 GetDlgItem (IDC__S0)->EnableWindow (b);
00385 GetDlgItem (IDC__S1)->EnableWindow (b);
00386 GetDlgItem (IDC_USER)->EnableWindow (b);
00387 GetDlgItem (IDC_PWD)->EnableWindow (b);
00388 }