summaryrefslogtreecommitdiff
path: root/cpp/demo/IcePatch2/MFC/PatchClientDlg.cpp
blob: 641e2792869d878aacd66f26a4b8015da3ffc915 (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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
// **********************************************************************
//
// Copyright (c) 2003-2006 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************

#include "stdafx.h"
#include "PatchClient.h"
#include "PatchClientDlg.h"

#include <IcePatch2/ClientUtil.h>
#include <IcePatch2/Util.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

using namespace std;

class DialogPatcherFeedback : public IcePatch2::PatcherFeedback
{
public:

    DialogPatcherFeedback(CPatchDlg* dialog) :
	_dialog(dialog),
        _filesPatched(0)
    {
	assert(_dialog);
    }

    virtual bool
    noFileSummary(const string& reason)
    {
	return IDYES == AfxMessageBox(L"Cannot load file summary. Perform a thorough patch?", MB_YESNO|MB_ICONSTOP);
    }

    virtual bool
    checksumStart()
    {
	return _dialog->checksumStart();
    }

    virtual bool
    checksumProgress(const string& path)
    {
	return _dialog->checksumProgress(path);
    }

    virtual bool
    checksumEnd()
    {
	return _dialog->checksumEnd();
    }

    virtual bool
    fileListStart()
    {
	return _dialog->fileListStart();
    }

    virtual bool
    fileListProgress(Ice::Int percent)
    {
	return _dialog->fileListProgress(percent);
    }

    virtual bool
    fileListEnd()
    {
	return _dialog->fileListEnd();
    }

    virtual bool
    patchStart(const string& path, Ice::Long size, Ice::Long totalProgress, Ice::Long totalSize)
    {
	return _dialog->patchStart(path, size, totalProgress, totalSize);
    }

    virtual bool
    patchProgress(Ice::Long progress, Ice::Long size, Ice::Long totalProgress, Ice::Long totalSize)
    {
	return _dialog->patchProgress(progress, size, totalProgress, totalSize);
    }

    virtual bool
    patchEnd()
    {
	++_filesPatched;
	return _dialog->patchEnd();
    }

    virtual int
    filesPatched() const
    {
	return _filesPatched;
    }

private:

    CPatchDlg* const _dialog;
    int _filesPatched;
};

typedef IceUtil::Handle<DialogPatcherFeedback> DialogPatcherFeedbackPtr;

CPatchDlg::CPatchDlg(const Ice::CommunicatorPtr& communicator, CWnd* pParent /*=NULL*/) :
    CDialog(CPatchDlg::IDD, pParent), _communicator(communicator), _isCancel(false), _isPatch(false)
{
    _hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

bool
CPatchDlg::checksumStart()
{
    _status->SetWindowText(CString(L" Calculating checksums..."));

    _progress->SetRange(0, 0);
    _progress->SetPos(0);

    processMessages();
    return !_isCancel;
}

bool
CPatchDlg::checksumProgress(const string& path)
{
    // TODO: indicate busy progress
 
    CString file;
    file.Format(L" %s", IceUtil::stringToWstring(IcePatch2::getBasename(path)).c_str());
    _file->SetWindowText(file);

    processMessages();
    return !_isCancel;
}

bool
CPatchDlg::checksumEnd()
{
    processMessages();
    return !_isCancel;
}

bool
CPatchDlg::fileListStart()
{
    _status->SetWindowText(CString(L" Retrieving file list..."));

    _progress->SetRange(0, 100);
    _progress->SetPos(0);

    processMessages();
    return !_isCancel;
}

bool
CPatchDlg::fileListProgress(Ice::Int pcnt)
{
    CString percent;
    percent.Format(L"%d%%", pcnt);
    _percent->SetWindowText(percent);

    _progress->SetPos(pcnt);

    processMessages();
    return !_isCancel;
}

bool
CPatchDlg::fileListEnd()
{
    processMessages();
    return !_isCancel;
}

bool
CPatchDlg::patchStart(const string& path, Ice::Long size, Ice::Long totalProgress, Ice::Long totalSize)
{
    if(!_isPatch)
    {
	_startTime = IceUtil::Time::now();
	_status->SetWindowText(CString(L" Patching..."));
	_speed->SetWindowText(CString(L" 0.0 KB/s"));
	_isPatch = true;
    }

    CString file;
    file.Format(L" %s", IceUtil::stringToWstring(IcePatch2::getBasename(path)).c_str());
    _file->SetWindowText(file);

    return patchProgress(0, size, totalProgress, totalSize);
}

bool
CPatchDlg::patchProgress(Ice::Long, Ice::Long, Ice::Long totalProgress, Ice::Long totalSize)
{
    IceUtil::Time elapsed = IceUtil::Time::now() - _startTime;
    if(elapsed.toSeconds() > 0)
    {
	CString speed;
	speed.Format(L" %s/s", convertSize(totalProgress / elapsed.toSeconds()));
	_speed->SetWindowText(speed);
    }

    int pcnt = 100;
    if(totalSize > 0)
    {
	pcnt = static_cast<int>(totalProgress * 100 / totalSize);
    }
    CString percent;
    percent.Format(L"%d%%", pcnt);
    _percent->SetWindowText(percent);

    CString total;
    total.Format(L" %s / %s", convertSize(totalProgress), convertSize(totalSize));
    _total->SetWindowText(total);

    _progress->SetPos(pcnt);

    processMessages();
    return !_isCancel;
}

bool
CPatchDlg::patchEnd()
{
    processMessages();
    return !_isCancel;
}

void
CPatchDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CPatchDlg, CDialog)
    ON_WM_PAINT()
    ON_WM_QUERYDRAGICON()
    //}}AFX_MSG_MAP
    ON_BN_CLICKED(IDC_SELECTDIR, OnSelectDir)
    ON_BN_CLICKED(IDC_STARTPATCH, OnStartPatch)
    ON_BN_CLICKED(IDC_CANCELPATCH, OnCancel)
END_MESSAGE_MAP()

BOOL
CPatchDlg::OnInitDialog()
{
    CDialog::OnInitDialog();

    // Set the icon for this dialog.  The framework does this automatically
    // when the application's main window is not a dialog
    SetIcon(_hIcon, TRUE);            // Set big icon
    SetIcon(_hIcon, FALSE);        // Set small icon

    //
    // Retrieve the controls.
    //
    _path = (CEdit*)GetDlgItem(IDC_PATH);
    _thorough = (CButton*)GetDlgItem(IDC_THOROUGH);
    _remove = (CButton*)GetDlgItem(IDC_ORPHAN);
    _select = (CButton*)GetDlgItem(IDC_SELECTDIR);
    _start = (CButton*)GetDlgItem(IDC_STARTPATCH);
    _cancel = (CButton*)GetDlgItem(IDC_CANCELPATCH);
    _file = (CStatic*)GetDlgItem(IDC_FILE);
    _total = (CStatic*)GetDlgItem(IDC_TOTAL);
    _speed = (CStatic*)GetDlgItem(IDC_SPEED);
    _status = (CStatic*)GetDlgItem(IDC_STATUSBAR);
    _percent = (CStatic*)GetDlgItem(IDC_PERCENT);
    _progress = (CProgressCtrl*)GetDlgItem(IDC_PROGRESS);

    //
    // Set the patch directory and thorough flag from properties.
    //
    Ice::PropertiesPtr properties = _communicator->getProperties();
    CString path = IceUtil::stringToWstring(properties->getPropertyWithDefault("IcePatch2.Directory", "")).c_str();
    _path->SetWindowText(path);

    CString thorough = IceUtil::stringToWstring(properties->getPropertyWithDefault("IcePatch2.Thorough", "0")).c_str();
    _thorough->SetCheck(thorough != "0");

    CString remove = IceUtil::stringToWstring(properties->getPropertyWithDefault("IcePatch2.Remove", "0")).c_str();
    _remove->SetCheck(remove != "0");

    //
    // Indicate ready status.
    //
    reset(L" Ready");

    return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
// to draw the icon.  For MFC applications using the document/view model,
// this is automatically done for you by the framework.

void
CPatchDlg::OnPaint() 
{
    if(IsIconic())
    {
        CPaintDC dc(this); // device context for painting

        SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

        // Center icon in client rectangle
        int cxIcon = GetSystemMetrics(SM_CXICON);
        int cyIcon = GetSystemMetrics(SM_CYICON);
        CRect rect;
        GetClientRect(&rect);
        int x = (rect.Width() - cxIcon + 1) / 2;
        int y = (rect.Height() - cyIcon + 1) / 2;

        // Draw the icon
        dc.DrawIcon(x, y, _hIcon);
    }
    else
    {
        CDialog::OnPaint();
    }
}

// The system calls this function to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR
CPatchDlg::OnQueryDragIcon()
{
    return static_cast<HCURSOR>(_hIcon);
}

void
CPatchDlg::OnSelectDir()
{
    BROWSEINFO info = { 0 };
    info.lpszTitle = _T("Select Patch Directory");
    LPITEMIDLIST pidl = SHBrowseForFolder(&info);
    if(pidl != 0)
    {
	//
        // Get the name of the selected folder.
	//
        TCHAR path[MAX_PATH];
        if(SHGetPathFromIDList(pidl, path))
        {
	    _path->SetWindowText(path);
        }

	//
        // Free up memory used.
	//
        IMalloc * imalloc = 0;
        if(SUCCEEDED(SHGetMalloc(&imalloc)))
        {
            imalloc->Free(pidl);
            imalloc->Release();
        }
    }
}

void
CPatchDlg::OnStartPatch()
{
    try
    {
	Ice::PropertiesPtr properties = _communicator->getProperties();

	//
	// Set the patch directory.
	// 
	CString path;
	_path->GetWindowText(path);
	if(path.IsEmpty())
	{
	    AfxMessageBox(CString(L"Please select a patch directory."), MB_OK|MB_ICONEXCLAMATION);
	    return;
	}
	properties->setProperty("IcePatch2.Directory", IceUtil::wstringToString(wstring(path)));

	//
	// Set the thorough patch flag.
	//
	string thorough = _thorough->GetCheck() == BST_CHECKED ? "1" : "0";
	properties->setProperty("IcePatch2.Thorough", thorough);

	//
	// Set the remove orphan flag.
	//
	string remove = _remove->GetCheck() == BST_CHECKED ? "1" : "0";
	properties->setProperty("IcePatch2.Remove", remove);

        DialogPatcherFeedbackPtr feedback = new DialogPatcherFeedback(this);
	IcePatch2::PatcherPtr patcher = new IcePatch2::Patcher(_communicator, feedback);

	//
	// Disable a few controls during the patch process.
	//
	_path->EnableWindow(false);
	_select->EnableWindow(false);
	_thorough->EnableWindow(false);
	_remove->EnableWindow(false);
	_start->EnableWindow(false);

	//
	// Patch
	//
	bool aborted = !patcher->prepare();
	if(!aborted)
	{
	    aborted = !patcher->patch("");
	}
	if(!aborted)
	{
	    patcher->finish();
	}

	//
	// Reset and indicate the completion status.
	//
	reset(aborted ? L" Aborted" : L" Completed");
    }
    catch(const IceUtil::Exception& ex)
    {
        handleException(ex);
    }
    catch(const string& ex)
    {
        AfxMessageBox(CString(ex.c_str()), MB_OK|MB_ICONEXCLAMATION);
    }
}

void
CPatchDlg::OnCancel()
{
    _isCancel = true;
    _status->SetWindowText(CString(L" Canceled"));
    CDialog::OnCancel();
}

void
CPatchDlg::reset(const CString& status)
{
    _isPatch = false;

    _path->EnableWindow(true);
    _select->EnableWindow(true);
    _thorough->EnableWindow(true);
    _remove->EnableWindow(true);
    _start->EnableWindow(true);

    _status->SetWindowText(status);
    _file->SetWindowText(CString());
    _total->SetWindowText(CString());
    _speed->SetWindowText(CString());
    _percent->SetWindowText(CString());

    _progress->SetPos(0);
}

void 
CPatchDlg::processMessages()
{
    MSG msg;
    while(::PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
}

void
CPatchDlg::handleException(const IceUtil::Exception& e)
{
    try
    {
        e.ice_throw();
    }
    catch(const IceUtil::Exception& ex)
    {
        ostringstream ostr;
        ostr << ex;
        string s = ostr.str();
        AfxMessageBox(CString(s.c_str()), MB_OK|MB_ICONEXCLAMATION);
    }

    reset(L" Ready");
}

CString
CPatchDlg::convertSize(Ice::Long size) const
{
    CString units;
    double start = static_cast<double>(size);
    double final = start / gigabyte;
    if(final >= 1)
    {
        units = L"GB";
    }
    else
    {
	final = start / megabyte;
	if(final >= 1)
	{
	    units = L"MB";
	}
	else
	{
	    final = start / kilobyte;
	    if(final >= 1)
	    {
		units = L"KB";
	    }
	    else
	    {
		final = start;
		units = L"B";
	    }
	}
    }

    CString convert;
    convert.Format(L"%.1f %s", final, units);
    return convert;
}