PPageWebServer.cpp

00001 /* 
00002  *      Copyright (C) 2003-2005 Gabest
00003  *      http://www.gabest.org
00004  *
00005  *  This Program is free software; you can redistribute it and/or modify
00006  *  it under the terms of the GNU General Public License as published by
00007  *  the Free Software Foundation; either version 2, or (at your option)
00008  *  any later version.
00009  *   
00010  *  This Program is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00013  *  GNU General Public License for more details.
00014  *   
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with GNU Make; see the file COPYING.  If not, write to
00017  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
00018  *  http://www.gnu.org/copyleft/gpl.html
00019  *
00020  */
00021 
00022 // PPageWebServer.cpp : implementation file
00023 //
00024 
00025 #include "stdafx.h"
00026 #include "mplayerc.h"
00027 #include "MainFrm.h"
00028 #include "PPageWebServer.h"
00029 
00030 // CPPageWebServer dialog
00031 
00032 IMPLEMENT_DYNAMIC(CPPageWebServer, CPPageBase)
00033 CPPageWebServer::CPPageWebServer()
00034         : CPPageBase(CPPageWebServer::IDD, CPPageWebServer::IDD)
00035         , m_fEnableWebServer(FALSE)
00036         , m_nWebServerPort(0)
00037         , m_launch(_T("http://localhost:13579/"))
00038         , m_fWebServerPrintDebugInfo(FALSE)
00039         , m_fWebServerUseCompression(FALSE)
00040         , m_fWebRoot(FALSE)
00041         , m_WebRoot(_T(""))
00042         , m_fWebServerLocalhostOnly(FALSE)
00043         , m_WebServerCGI(_T(""))
00044         , m_WebDefIndex(_T(""))
00045 {
00046 }
00047 
00048 CPPageWebServer::~CPPageWebServer()
00049 {
00050 }
00051 
00052 void CPPageWebServer::DoDataExchange(CDataExchange* pDX)
00053 {
00054         __super::DoDataExchange(pDX);
00055         DDX_Check(pDX, IDC_CHECK1, m_fEnableWebServer);
00056         DDX_Text(pDX, IDC_EDIT1, m_nWebServerPort);
00057         DDX_Control(pDX, IDC_EDIT1, m_nWebServerPortCtrl);
00058         DDX_Control(pDX, IDC_STATIC1, m_launch);
00059         DDX_Check(pDX, IDC_CHECK2, m_fWebServerPrintDebugInfo);
00060         DDX_Check(pDX, IDC_CHECK3, m_fWebServerUseCompression);
00061         DDX_Check(pDX, IDC_CHECK4, m_fWebRoot);
00062         DDX_Text(pDX, IDC_EDIT2, m_WebRoot);
00063         DDX_Check(pDX, IDC_CHECK5, m_fWebServerLocalhostOnly);
00064         DDX_Text(pDX, IDC_EDIT3, m_WebServerCGI);
00065         DDX_Text(pDX, IDC_EDIT9, m_WebDefIndex);
00066 }
00067 
00068 BOOL CPPageWebServer::PreTranslateMessage(MSG* pMsg)
00069 {
00070         if(pMsg->message == WM_LBUTTONDOWN && pMsg->hwnd == m_launch.m_hWnd)
00071         {
00072                 UpdateData();
00073 
00074                 AppSettings& s = AfxGetAppSettings();
00075 
00076                 if(CMainFrame* pWnd = (CMainFrame*)AfxGetMainWnd())
00077                 {
00078                         if(m_fEnableWebServer)
00079                         {
00080                                 if(s.nWebServerPort != m_nWebServerPort)
00081                                 {
00082                                         AfxMessageBox(_T("Press apply first, before testing the new settings!"), MB_OK);
00083                                         return TRUE;
00084                                 }
00085                         }
00086                 }
00087         }
00088 
00089         return CPPageBase::PreTranslateMessage(pMsg);
00090 }
00091 
00092 BOOL CPPageWebServer::OnInitDialog()
00093 {
00094         __super::OnInitDialog();
00095 
00096         AppSettings& s = AfxGetAppSettings();
00097 
00098         m_fEnableWebServer = s.fEnableWebServer;
00099         m_nWebServerPort = s.nWebServerPort;
00100         m_fWebServerPrintDebugInfo = s.fWebServerPrintDebugInfo;
00101         m_fWebServerLocalhostOnly = s.fWebServerLocalhostOnly;
00102         m_fWebServerUseCompression = s.fWebServerUseCompression;
00103         m_fWebRoot = s.WebRoot.Find('*') < 0;
00104         m_WebRoot = s.WebRoot;
00105         m_WebRoot.TrimLeft(_T("*"));
00106         m_WebDefIndex = s.WebDefIndex;
00107         m_WebServerCGI = s.WebServerCGI;
00108 
00109         UpdateData(FALSE);
00110 
00111         OnEnChangeEdit1();
00112 
00113         return TRUE;  // return TRUE unless you set the focus to a control
00114         // EXCEPTION: OCX Property Pages should return FALSE
00115 }
00116 
00117 BOOL CPPageWebServer::OnApply()
00118 {
00119         UpdateData();
00120 
00121         AppSettings& s = AfxGetAppSettings();
00122 
00123         CString NewWebRoot = m_WebRoot;
00124         if(!m_fWebRoot) NewWebRoot = _T("*") + NewWebRoot;
00125         
00126         bool fRestart = s.nWebServerPort != m_nWebServerPort
00127                 || s.WebRoot != NewWebRoot || s.WebServerCGI != m_WebServerCGI;
00128 
00129         s.fEnableWebServer = !!m_fEnableWebServer;
00130         s.nWebServerPort = m_nWebServerPort;
00131         s.fWebServerPrintDebugInfo = !!m_fWebServerPrintDebugInfo;
00132         s.fWebServerLocalhostOnly = !!m_fWebServerLocalhostOnly;
00133         s.fWebServerUseCompression = !!m_fWebServerUseCompression;
00134         s.WebRoot = NewWebRoot;
00135         s.WebDefIndex = m_WebDefIndex;
00136         s.WebServerCGI = m_WebServerCGI;
00137 
00138         if(CMainFrame* pWnd = (CMainFrame*)AfxGetMainWnd())
00139         {
00140                 if(m_fEnableWebServer)
00141                 {
00142                         if(fRestart) pWnd->StopWebServer();
00143                         pWnd->StartWebServer(m_nWebServerPort);
00144                 }
00145                 else
00146                 {
00147                         pWnd->StopWebServer();
00148                 }
00149         }
00150 
00151         return __super::OnApply();
00152 }
00153 
00154 CString CPPageWebServer::GetMPCDir()
00155 {
00156         CString dir;
00157         GetModuleFileName(AfxGetInstanceHandle(), dir.GetBuffer(MAX_PATH), MAX_PATH);
00158         dir.ReleaseBuffer();
00159         CPath path(dir);
00160         path.RemoveFileSpec();
00161         return (LPCTSTR)path;
00162 }
00163 
00164 CString CPPageWebServer::GetCurWebRoot()
00165 {
00166         CString WebRoot;
00167         GetDlgItem(IDC_EDIT2)->GetWindowText(WebRoot);
00168         WebRoot.Replace('/', '\\');
00169 
00170         CPath path;
00171         path.Combine(GetMPCDir(), WebRoot);
00172         return path.IsDirectory() ? (LPCTSTR)path : _T("");
00173 }
00174 
00175 static int __stdcall BrowseCtrlCallback(HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData)
00176 {
00177   if(uMsg == BFFM_INITIALIZED && lpData)
00178           ::SendMessage(hwnd, BFFM_SETSELECTION, TRUE, lpData);
00179   return 0;
00180 }
00181 
00182 bool CPPageWebServer::PickDir(CString& dir)
00183 {
00184         TCHAR buff[MAX_PATH];
00185 
00186         BROWSEINFO bi;
00187         bi.hwndOwner = m_hWnd;
00188         bi.pidlRoot = NULL;
00189         bi.pszDisplayName = buff;
00190         bi.lpszTitle = _T("Select the directory");
00191         bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_VALIDATE | BIF_USENEWUI;
00192         bi.lpfn = BrowseCtrlCallback;
00193         bi.lParam = (LPARAM)(LPCTSTR)dir;
00194         bi.iImage = 0; 
00195 
00196         LPITEMIDLIST iil;
00197         if(iil = SHBrowseForFolder(&bi))
00198         {
00199                 SHGetPathFromIDList(iil, buff);
00200                 dir = buff;
00201                 return true;
00202         }
00203 
00204         return false;
00205 }
00206 
00207 BEGIN_MESSAGE_MAP(CPPageWebServer, CPPageBase)
00208         ON_EN_CHANGE(IDC_EDIT1, OnEnChangeEdit1)
00209         ON_BN_CLICKED(IDC_BUTTON1, OnBnClickedButton1)
00210         ON_BN_CLICKED(IDC_BUTTON2, OnBnClickedButton2)
00211         ON_UPDATE_COMMAND_UI(IDC_BUTTON1, OnUpdateButton2)
00212 END_MESSAGE_MAP()
00213 
00214 
00215 // CPPageWebServer message handlers
00216 
00217 
00218 void CPPageWebServer::OnEnChangeEdit1()
00219 {
00220         UpdateData();
00221 
00222         CString link;
00223         link.Format(_T("http://localhost:%d/"), m_nWebServerPort);
00224         m_launch.m_link = link;
00225 
00226         SetModified();
00227 }
00228 
00229 void CPPageWebServer::OnBnClickedButton1()
00230 {
00231         CString dir = GetCurWebRoot();
00232         if(PickDir(dir))
00233         {
00234                 CPath path;
00235                 if(path.RelativePathTo(GetMPCDir(), FILE_ATTRIBUTE_DIRECTORY, dir, FILE_ATTRIBUTE_DIRECTORY))
00236                         dir = (LPCTSTR)path;
00237                 m_WebRoot = dir;
00238                 UpdateData(FALSE);
00239         }
00240 }
00241 
00242 void CPPageWebServer::OnBnClickedButton2()
00243 {
00244         CString dir;
00245         if(PickDir(dir))
00246         {
00247                 dir += _T("\\");
00248                 CWebServer::Deploy(dir);
00249         }
00250 }
00251 
00252 void CPPageWebServer::OnUpdateButton2(CCmdUI* pCmdUI)
00253 {
00254         pCmdUI->Enable(GetDlgItem(IDC_EDIT2)->GetWindowTextLength() > 0);
00255 }

Generated on Tue Dec 13 14:47:02 2005 for guliverkli by  doxygen 1.4.5