Welcome to mirror list, hosted at ThFree Co, Russian Federation.

PPageWebServer.cpp « mpc-hc « src - github.com/mpc-hc/mpc-hc.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6da605395ccd86fdd60551924bddb07a60559fba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
 * (C) 2003-2006 Gabest
 * (C) 2006-2015 see Authors.txt
 *
 * This file is part of MPC-HC.
 *
 * MPC-HC is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * MPC-HC is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "stdafx.h"
#include "mplayerc.h"
#include "MainFrm.h"
#include "PPageWebServer.h"
#include "SysVersion.h"
#include "PathUtils.h"
#include <afxglobals.h>
#include "WebServer.h"


// CPPageWebServer dialog

IMPLEMENT_DYNAMIC(CPPageWebServer, CPPageBase)
CPPageWebServer::CPPageWebServer()
    : CPPageBase(CPPageWebServer::IDD, CPPageWebServer::IDD)
    , m_fEnableWebServer(FALSE)
    , m_nWebServerPort(0)
    , m_launch(_T("http://localhost:13579/"))
    , m_fWebServerUseCompression(FALSE)
    , m_fWebServerLocalhostOnly(FALSE)
    , m_bWebUIEnablePreview(FALSE)
    , m_fWebServerPrintDebugInfo(FALSE)
    , m_fWebRoot(FALSE)
{
}

CPPageWebServer::~CPPageWebServer()
{
}

void CPPageWebServer::DoDataExchange(CDataExchange* pDX)
{
    __super::DoDataExchange(pDX);
    DDX_Check(pDX, IDC_CHECK1, m_fEnableWebServer);
    DDX_Text(pDX, IDC_EDIT1, m_nWebServerPort);
    DDX_Control(pDX, IDC_EDIT1, m_nWebServerPortCtrl);
    DDV_MinMaxInt(pDX, m_nWebServerPort, 1, 65535);
    DDX_Control(pDX, IDC_STATIC1, m_launch);
    DDX_Check(pDX, IDC_CHECK3, m_fWebServerUseCompression);
    DDX_Check(pDX, IDC_CHECK5, m_fWebServerLocalhostOnly);
    DDX_Check(pDX, IDC_CHECK6, m_bWebUIEnablePreview);
    DDX_Check(pDX, IDC_CHECK2, m_fWebServerPrintDebugInfo);
    DDX_Check(pDX, IDC_CHECK4, m_fWebRoot);
    DDX_Text(pDX, IDC_EDIT2, m_WebRoot);
    DDX_Text(pDX, IDC_EDIT3, m_WebServerCGI);
    DDX_Text(pDX, IDC_EDIT9, m_WebDefIndex);
}

BOOL CPPageWebServer::PreTranslateMessage(MSG* pMsg)
{
    if (pMsg->message == WM_LBUTTONDOWN && pMsg->hwnd == m_launch.m_hWnd) {
        UpdateData();

        if (m_fEnableWebServer) {
            if (AfxGetAppSettings().nWebServerPort != m_nWebServerPort) {
                AfxMessageBox(IDS_WEBSERVER_ERROR_TEST, MB_ICONEXCLAMATION | MB_OK, 0);
                return TRUE;
            }
        }
    }

    return CPPageBase::PreTranslateMessage(pMsg);
}

BOOL CPPageWebServer::OnInitDialog()
{
    __super::OnInitDialog();

    const CAppSettings& s = AfxGetAppSettings();

    m_fEnableWebServer = s.fEnableWebServer;
    m_nWebServerPort = s.nWebServerPort;
    m_fWebServerUseCompression = s.fWebServerUseCompression;
    m_fWebServerLocalhostOnly = s.fWebServerLocalhostOnly;
    m_bWebUIEnablePreview = s.bWebUIEnablePreview;
    m_fWebServerPrintDebugInfo = s.fWebServerPrintDebugInfo;
    m_fWebRoot = s.strWebRoot.Find('*') < 0;
    m_WebRoot = s.strWebRoot;
    m_WebRoot.TrimLeft(_T('*'));
    m_WebDefIndex = s.strWebDefIndex;
    m_WebServerCGI = s.strWebServerCGI;

    m_launch.EnableWindow(m_fEnableWebServer);

    UpdateData(FALSE);

    OnEnChangeEdit1();

    return TRUE;  // return TRUE unless you set the focus to a control
    // EXCEPTION: OCX Property Pages should return FALSE
}

BOOL CPPageWebServer::OnApply()
{
    UpdateData();

    CAppSettings& s = AfxGetAppSettings();

    CString NewWebRoot = m_WebRoot;
    if (!m_fWebRoot) {
        NewWebRoot = _T("*") + NewWebRoot;
    }

    bool fRestart = s.nWebServerPort != m_nWebServerPort
                    || s.strWebRoot != NewWebRoot || s.strWebServerCGI != m_WebServerCGI;

    s.fEnableWebServer = !!m_fEnableWebServer;
    s.nWebServerPort = m_nWebServerPort;
    s.fWebServerUseCompression = !!m_fWebServerUseCompression;
    s.fWebServerLocalhostOnly = !!m_fWebServerLocalhostOnly;
    s.bWebUIEnablePreview = !!m_bWebUIEnablePreview;
    s.fWebServerPrintDebugInfo = !!m_fWebServerPrintDebugInfo;
    s.strWebRoot = NewWebRoot;
    s.strWebDefIndex = m_WebDefIndex;
    s.strWebServerCGI = m_WebServerCGI;

    if (CMainFrame* pMainFrame = AfxGetMainFrame()) {
        if (m_fEnableWebServer) {
            if (fRestart) {
                pMainFrame->StopWebServer();
            }
            pMainFrame->StartWebServer(m_nWebServerPort);
        } else {
            pMainFrame->StopWebServer();
        }
    }

    m_launch.EnableWindow(m_fEnableWebServer);

    return __super::OnApply();
}

CString CPPageWebServer::GetCurWebRoot()
{
    CString WebRoot;
    GetDlgItem(IDC_EDIT2)->GetWindowText(WebRoot);
    WebRoot.Replace('/', '\\');

    CPath path;
    path.Combine(PathUtils::GetProgramPath(), WebRoot);
    return path.IsDirectory() ? (LPCTSTR)path : _T("");
}

static int __stdcall BrowseCtrlCallback(HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData)
{
    if (uMsg == BFFM_INITIALIZED && lpData) {
        ::SendMessage(hwnd, BFFM_SETSELECTION, TRUE, lpData);
    }
    return 0;
}

bool CPPageWebServer::PickDir(CString& dir)
{
    CString strTitle(StrRes(IDS_PPAGEWEBSERVER_0));
    bool success = false;

    if (SysVersion::IsVistaOrLater()) {
        CFileDialog dlg(TRUE);
        IFileOpenDialog* openDlgPtr = dlg.GetIFileOpenDialog();

        if (openDlgPtr != nullptr) {
            openDlgPtr->SetTitle(strTitle);
            openDlgPtr->SetOptions(FOS_PICKFOLDERS | FOS_FORCEFILESYSTEM | FOS_PATHMUSTEXIST);

            if (!dir.IsEmpty()) {
                CComPtr<IShellItem> psiFolder;
                if (SUCCEEDED(afxGlobalData.ShellCreateItemFromParsingName(dir, nullptr, IID_PPV_ARGS(&psiFolder)))) {
                    openDlgPtr->SetFolder(psiFolder);
                }
            }

            if (SUCCEEDED(openDlgPtr->Show(m_hWnd))) {
                dir = dlg.GetFolderPath();
                success = true;
            }

            openDlgPtr->Release();
        }
    } else {
        TCHAR buff[MAX_PATH];

        BROWSEINFO bi;
        bi.hwndOwner = m_hWnd;
        bi.pidlRoot = nullptr;
        bi.pszDisplayName = buff;
        bi.lpszTitle = strTitle;
        bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_VALIDATE | BIF_USENEWUI;
        bi.lpfn = BrowseCtrlCallback;
        bi.lParam = (LPARAM)(LPCTSTR)dir;
        bi.iImage = 0;

        PIDLIST_ABSOLUTE iil = SHBrowseForFolder(&bi);
        if (iil) {
            SHGetPathFromIDList(iil, buff);
            dir = buff;
            success = true;
        }
    }

    return success;
}

BEGIN_MESSAGE_MAP(CPPageWebServer, CPPageBase)
    ON_EN_CHANGE(IDC_EDIT1, OnEnChangeEdit1)
    ON_BN_CLICKED(IDC_BUTTON1, OnBnClickedButton1)
    ON_BN_CLICKED(IDC_BUTTON2, OnBnClickedButton2)
    ON_UPDATE_COMMAND_UI(IDC_BUTTON1, OnUpdateButton2)
    ON_BN_CLICKED(IDC_CHECK6, OnEnablePreviewChecked)
END_MESSAGE_MAP()


// CPPageWebServer message handlers

void CPPageWebServer::OnEnChangeEdit1()
{
    UpdateData();

    CString link;
    link.Format(_T("http://localhost:%d/"), m_nWebServerPort);
    m_launch.m_link = link;

    SetModified();
}

void CPPageWebServer::OnBnClickedButton1()
{
    CString dir = GetCurWebRoot();
    if (PickDir(dir)) {
        CPath path;
        if (path.RelativePathTo(PathUtils::GetProgramPath(), FILE_ATTRIBUTE_DIRECTORY, dir, FILE_ATTRIBUTE_DIRECTORY)) {
            dir = (LPCTSTR)path;
        }
        m_WebRoot = dir;

        UpdateData(FALSE);
        SetModified();
    }
}

void CPPageWebServer::OnBnClickedButton2()
{
    CString dir;
    if (PickDir(dir)) {
        dir += _T("\\");
        CWebServer::Deploy(dir);
    }
}

void CPPageWebServer::OnUpdateButton2(CCmdUI* pCmdUI)
{
    pCmdUI->Enable(GetDlgItem(IDC_EDIT2)->GetWindowTextLength() > 0);
}

void CPPageWebServer::OnEnablePreviewChecked()
{
    if (IsDlgButtonChecked(IDC_CHECK6)
            && (MessageBox(ResStr(IDS_WEBUI_PREVIEW_WARNING), nullptr, MB_ICONQUESTION | MB_YESNO | MB_DEFBUTTON2) == IDNO)) {
        CheckDlgButton(IDC_CHECK6, BST_UNCHECKED);
    } else {
        SetModified();
    }
}