1 // UpdateCallbackFar.cpp
2 
3 #include "StdAfx.h"
4 
5 #ifndef _7ZIP_ST
6 #include "../../../Windows/Synchronization.h"
7 #endif
8 
9 #include "../../../Common/StringConvert.h"
10 
11 #include "FarUtils.h"
12 #include "UpdateCallbackFar.h"
13 
14 using namespace NWindows;
15 using namespace NFar;
16 
17 #ifndef _7ZIP_ST
18 static NSynchronization::CCriticalSection g_CriticalSection;
19 #define MT_LOCK NSynchronization::CCriticalSectionLock lock(g_CriticalSection);
20 #else
21 #define MT_LOCK
22 #endif
23 
CheckBreak2()24 static HRESULT CheckBreak2()
25 {
26   return WasEscPressed() ? E_ABORT : S_OK;
27 }
28 
29 
ScanProgress(UInt64 numFolders,UInt64 numFiles,UInt64 totalSize,const wchar_t * path,Int32)30 STDMETHODIMP CUpdateCallback100Imp::ScanProgress(UInt64 numFolders, UInt64 numFiles, UInt64 totalSize, const wchar_t *path, Int32 /* isDir */)
31 {
32   MT_LOCK
33 
34   if (_percent)
35   {
36     _percent->FilesTotal = numFolders + numFiles;
37     _percent->Total = totalSize;
38     _percent->Command = "Scanning";
39     _percent->FileName = path;
40     _percent->Print();
41     _percent->Print();
42   }
43   return CheckBreak2();
44 }
45 
ScanError(const wchar_t * path,HRESULT errorCode)46 STDMETHODIMP CUpdateCallback100Imp::ScanError(const wchar_t *path, HRESULT errorCode)
47 {
48   if (ShowSysErrorMessage(errorCode, path) == -1)
49     return E_ABORT;
50   return CheckBreak2();
51 }
52 
SetNumFiles(UInt64 numFiles)53 STDMETHODIMP CUpdateCallback100Imp::SetNumFiles(UInt64 numFiles)
54 {
55   MT_LOCK
56 
57   if (_percent)
58   {
59     _percent->FilesTotal = numFiles;
60     _percent->Print();
61   }
62   return CheckBreak2();
63 }
64 
65 
SetTotal(const UInt64 *,const UInt64 *)66 STDMETHODIMP CUpdateCallback100Imp::SetTotal(const UInt64 * /* files */, const UInt64 * /* bytes */)
67 {
68   return S_OK;
69 }
70 
SetCompleted(const UInt64 *,const UInt64 *)71 STDMETHODIMP CUpdateCallback100Imp::SetCompleted(const UInt64 * /* files */, const UInt64 * /* bytes */)
72 {
73   MT_LOCK
74   return CheckBreak2();
75 }
76 
77 
78 
SetTotal(UInt64 size)79 STDMETHODIMP CUpdateCallback100Imp::SetTotal(UInt64 size)
80 {
81   MT_LOCK
82 
83   if (_percent)
84   {
85     _percent->Total = size;
86     _percent->Print();
87   }
88   return CheckBreak2();
89 }
90 
SetCompleted(const UInt64 * completeValue)91 STDMETHODIMP CUpdateCallback100Imp::SetCompleted(const UInt64 *completeValue)
92 {
93   MT_LOCK
94 
95   if (_percent)
96   {
97     if (completeValue)
98       _percent->Completed = *completeValue;
99     _percent->Print();
100   }
101   return CheckBreak2();
102 }
103 
CompressOperation(const wchar_t * name)104 STDMETHODIMP CUpdateCallback100Imp::CompressOperation(const wchar_t *name)
105 {
106   MT_LOCK
107 
108   if (_percent)
109   {
110     _percent->Command = "Adding";
111     _percent->FileName = name;
112     _percent->Print();
113   }
114   return CheckBreak2();;
115 }
116 
DeleteOperation(const wchar_t * name)117 STDMETHODIMP CUpdateCallback100Imp::DeleteOperation(const wchar_t *name)
118 {
119   MT_LOCK
120 
121   if (_percent)
122   {
123     _percent->Command = "Deleting";
124     _percent->FileName = name;
125     _percent->Print();
126   }
127   return CheckBreak2();;
128 }
129 
OperationResult(Int32)130 STDMETHODIMP CUpdateCallback100Imp::OperationResult(Int32 /* opRes */)
131 {
132   MT_LOCK
133 
134   if (_percent)
135   {
136     _percent->Files++;
137   }
138   return CheckBreak2();
139 }
140 
UpdateErrorMessage(const wchar_t * message)141 STDMETHODIMP CUpdateCallback100Imp::UpdateErrorMessage(const wchar_t *message)
142 {
143   MT_LOCK
144 
145   if (g_StartupInfo.ShowErrorMessage(UnicodeStringToMultiByte(message, CP_OEMCP)) == -1)
146     return E_ABORT;
147   return CheckBreak2();
148 }
149 
OpenFileError(const wchar_t * path,HRESULT errorCode)150 HRESULT CUpdateCallback100Imp::OpenFileError(const wchar_t *path, HRESULT errorCode)
151 {
152   if (ShowSysErrorMessage(errorCode, path) == -1)
153     return E_ABORT;
154   return CheckBreak2();
155 }
156 
ReadingFileError(const wchar_t * path,HRESULT errorCode)157 STDMETHODIMP CUpdateCallback100Imp::ReadingFileError(const wchar_t *path, HRESULT errorCode)
158 {
159   if (ShowSysErrorMessage(errorCode, path) == -1)
160     return E_ABORT;
161   return CheckBreak2();
162 }
163 
164 void SetExtractErrorMessage(Int32 opRes, Int32 encrypted, AString &s);
165 
ReportExtractResult(Int32 opRes,Int32 isEncrypted,const wchar_t * name)166 STDMETHODIMP CUpdateCallback100Imp::ReportExtractResult(Int32 opRes, Int32 isEncrypted, const wchar_t *name)
167 {
168   MT_LOCK
169 
170   if (opRes != NArchive::NExtract::NOperationResult::kOK)
171   {
172     AString s;
173     SetExtractErrorMessage(opRes, isEncrypted, s);
174     if (PrintErrorMessage(s, name) == -1)
175       return E_ABORT;
176   }
177 
178   return CheckBreak2();
179 }
180 
181 
ReportUpdateOperation(UInt32 op,const wchar_t * name,Int32)182 STDMETHODIMP CUpdateCallback100Imp::ReportUpdateOperation(UInt32 op, const wchar_t *name, Int32 /* isDir */)
183 {
184   const char *s;
185   switch (op)
186   {
187     case NUpdateNotifyOp::kAdd: s = "Adding"; break;
188     case NUpdateNotifyOp::kUpdate: s = "Updating"; break;
189     case NUpdateNotifyOp::kAnalyze: s = "Analyzing"; break;
190     case NUpdateNotifyOp::kReplicate: s = "Replicating"; break;
191     case NUpdateNotifyOp::kRepack: s = "Repacking"; break;
192     case NUpdateNotifyOp::kSkip: s = "Skipping"; break;
193     case NUpdateNotifyOp::kHeader: s = "Header creating"; break;
194     case NUpdateNotifyOp::kDelete: s = "Deleting"; break;
195     default: s = "Unknown operation";
196   }
197 
198   MT_LOCK
199 
200   if (_percent)
201   {
202     _percent->Command = s;
203     _percent->FileName.Empty();
204     if (name)
205       _percent->FileName = name;
206     _percent->Print();
207   }
208 
209   return CheckBreak2();;
210 }
211 
212 
213 extern HRESULT GetPassword(UString &password);
214 
CryptoGetTextPassword(BSTR * password)215 STDMETHODIMP CUpdateCallback100Imp::CryptoGetTextPassword(BSTR *password)
216 {
217   MT_LOCK
218 
219   *password = NULL;
220   if (!PasswordIsDefined)
221   {
222     RINOK(GetPassword(Password));
223     PasswordIsDefined = true;
224   }
225   return StringToBstr(Password, password);
226 }
227 
CryptoGetTextPassword2(Int32 * passwordIsDefined,BSTR * password)228 STDMETHODIMP CUpdateCallback100Imp::CryptoGetTextPassword2(Int32 *passwordIsDefined, BSTR *password)
229 {
230   MT_LOCK
231 
232   *password = NULL;
233   *passwordIsDefined = BoolToInt(PasswordIsDefined);
234   if (!PasswordIsDefined)
235     return S_OK;
236   return StringToBstr(Password, password);
237 }
238