other-licenses/7zstub/src/7zip/FileManager/Resource/ProgressDialog/ProgressDialog.cpp

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 // ProgressDialog.cpp
michael@0 2
michael@0 3 #include "StdAfx.h"
michael@0 4 #include "resource.h"
michael@0 5 #include "ProgressDialog.h"
michael@0 6 #include "Common/IntToString.h"
michael@0 7 #include "Common/IntToString.h"
michael@0 8
michael@0 9 using namespace NWindows;
michael@0 10
michael@0 11 static const UINT_PTR kTimerID = 3;
michael@0 12 static const UINT kTimerElapse = 50;
michael@0 13
michael@0 14 #ifdef LANG
michael@0 15 #include "../../LangUtils.h"
michael@0 16 #endif
michael@0 17
michael@0 18 #ifdef LANG
michael@0 19 static CIDLangPair kIDLangPairs[] =
michael@0 20 {
michael@0 21 { IDCANCEL, 0x02000711 }
michael@0 22 };
michael@0 23 #endif
michael@0 24
michael@0 25 #ifndef _SFX
michael@0 26 CProgressDialog::~CProgressDialog()
michael@0 27 {
michael@0 28 AddToTitle(TEXT(""));
michael@0 29 }
michael@0 30 void CProgressDialog::AddToTitle(LPCWSTR s)
michael@0 31 {
michael@0 32 if (MainWindow != 0)
michael@0 33 ::MySetWindowText(MainWindow, UString(s) + MainTitle);
michael@0 34 }
michael@0 35 #endif
michael@0 36
michael@0 37
michael@0 38
michael@0 39 bool CProgressDialog::OnInit()
michael@0 40 {
michael@0 41 _range = UINT64(-1);
michael@0 42 _prevPercentValue = -1;
michael@0 43
michael@0 44 #ifdef LANG
michael@0 45 // LangSetWindowText(HWND(*this), 0x02000C00);
michael@0 46 LangSetDlgItemsText(HWND(*this), kIDLangPairs, sizeof(kIDLangPairs) / sizeof(kIDLangPairs[0]));
michael@0 47 #endif
michael@0 48
michael@0 49 m_ProgressBar.Attach(GetItem(IDC_PROGRESS1));
michael@0 50 _timer = SetTimer(kTimerID, kTimerElapse);
michael@0 51 _dialogCreatedEvent.Set();
michael@0 52 SetText(_title);
michael@0 53 return CModalDialog::OnInit();
michael@0 54 }
michael@0 55
michael@0 56 void CProgressDialog::OnCancel()
michael@0 57 {
michael@0 58 ProgressSynch.SetStopped(true);
michael@0 59 }
michael@0 60
michael@0 61 void CProgressDialog::SetRange(UINT64 range)
michael@0 62 {
michael@0 63 _range = range;
michael@0 64 _peviousPos = (UInt64)(Int64)-1;
michael@0 65 _converter.Init(range);
michael@0 66 m_ProgressBar.SetRange32(0 , _converter.Count(range)); // Test it for 100%
michael@0 67 }
michael@0 68
michael@0 69 void CProgressDialog::SetPos(UINT64 pos)
michael@0 70 {
michael@0 71 bool redraw = true;
michael@0 72 if (pos < _range && pos > _peviousPos)
michael@0 73 {
michael@0 74 UINT64 posDelta = pos - _peviousPos;
michael@0 75 if (posDelta < (_range >> 10))
michael@0 76 redraw = false;
michael@0 77 }
michael@0 78 if(redraw)
michael@0 79 {
michael@0 80 m_ProgressBar.SetPos(_converter.Count(pos)); // Test it for 100%
michael@0 81 _peviousPos = pos;
michael@0 82 }
michael@0 83 }
michael@0 84
michael@0 85 bool CProgressDialog::OnTimer(WPARAM timerID, LPARAM callback)
michael@0 86 {
michael@0 87 if (ProgressSynch.GetPaused())
michael@0 88 return true;
michael@0 89 UINT64 total, completed;
michael@0 90 ProgressSynch.GetProgress(total, completed);
michael@0 91 if (total != _range)
michael@0 92 SetRange(total);
michael@0 93 SetPos(completed);
michael@0 94
michael@0 95 if (total == 0)
michael@0 96 total = 1;
michael@0 97
michael@0 98 int percentValue = (int)(completed * 100 / total);
michael@0 99 if (percentValue != _prevPercentValue)
michael@0 100 {
michael@0 101 wchar_t s[64];
michael@0 102 ConvertUInt64ToString(percentValue, s);
michael@0 103 UString title = s;
michael@0 104 title += L"% ";
michael@0 105 SetText(title + _title);
michael@0 106 #ifndef _SFX
michael@0 107 AddToTitle(title + MainAddTitle);
michael@0 108 #endif
michael@0 109 _prevPercentValue = percentValue;
michael@0 110 }
michael@0 111 return true;
michael@0 112 }
michael@0 113
michael@0 114
michael@0 115 ////////////////////
michael@0 116 // CU64ToI32Converter
michael@0 117
michael@0 118 static const UINT64 kMaxIntValue = 0x7FFFFFFF;
michael@0 119
michael@0 120 void CU64ToI32Converter::Init(UINT64 range)
michael@0 121 {
michael@0 122 _numShiftBits = 0;
michael@0 123 while(range > kMaxIntValue)
michael@0 124 {
michael@0 125 range >>= 1;
michael@0 126 _numShiftBits++;
michael@0 127 }
michael@0 128 }
michael@0 129
michael@0 130 int CU64ToI32Converter::Count(UINT64 aValue)
michael@0 131 {
michael@0 132 return int(aValue >> _numShiftBits);
michael@0 133 }
michael@0 134
michael@0 135 const UINT CProgressDialog::kCloseMessage = WM_USER + 1;
michael@0 136
michael@0 137 bool CProgressDialog::OnMessage(UINT message, WPARAM wParam, LPARAM lParam)
michael@0 138 {
michael@0 139 switch(message)
michael@0 140 {
michael@0 141 case kCloseMessage:
michael@0 142 {
michael@0 143 KillTimer(_timer);
michael@0 144 _timer = 0;
michael@0 145 End(0);
michael@0 146 return true;
michael@0 147 }
michael@0 148 case WM_SETTEXT:
michael@0 149 {
michael@0 150 if (_timer == 0)
michael@0 151 return true;
michael@0 152 }
michael@0 153 }
michael@0 154 return CModalDialog::OnMessage(message, wParam, lParam);
michael@0 155 }
michael@0 156
michael@0 157 bool CProgressDialog::OnButtonClicked(int buttonID, HWND buttonHWND)
michael@0 158 {
michael@0 159 switch(buttonID)
michael@0 160 {
michael@0 161 case IDCANCEL:
michael@0 162 {
michael@0 163 bool paused = ProgressSynch.GetPaused();;
michael@0 164 ProgressSynch.SetPaused(true);
michael@0 165 int res = ::MessageBoxW(HWND(*this),
michael@0 166 L"Are you sure you want to cancel?",
michael@0 167 _title, MB_YESNO);
michael@0 168 ProgressSynch.SetPaused(paused);
michael@0 169 if (res == IDNO)
michael@0 170 return true;
michael@0 171 break;
michael@0 172 }
michael@0 173 }
michael@0 174 return CModalDialog::OnButtonClicked(buttonID, buttonHWND);
michael@0 175 }

mercurial