Upload 2.0.2
[physicsfs] / lzma / CPP / 7zip / UI / Console / UpdateCallbackConsole.cpp
1 // UpdateCallbackConsole.cpp
2
3 #include "StdAfx.h"
4
5 #include "UpdateCallbackConsole.h"
6
7 #include "Windows/Error.h"
8 #ifdef COMPRESS_MT
9 #include "Windows/Synchronization.h"
10 #endif
11
12 #include "ConsoleClose.h"
13 #include "UserInputUtils.h"
14
15 using namespace NWindows;
16
17 #ifdef COMPRESS_MT
18 static NSynchronization::CCriticalSection g_CriticalSection;
19 #define MT_LOCK NSynchronization::CCriticalSectionLock lock(g_CriticalSection);
20 #else
21 #define MT_LOCK
22 #endif
23
24 static const wchar_t *kEmptyFileAlias = L"[Content]";
25
26 static const char *kCreatingArchiveMessage = "Creating archive ";
27 static const char *kUpdatingArchiveMessage = "Updating archive ";
28 static const char *kScanningMessage = "Scanning";
29
30
31 HRESULT CUpdateCallbackConsole::OpenResult(const wchar_t *name, HRESULT result)
32 {
33   (*OutStream) << endl;
34   if (result != S_OK)
35     (*OutStream) << "Error: " << name << " is not supported archive" << endl;
36   return S_OK;
37 }
38
39 HRESULT CUpdateCallbackConsole::StartScanning()
40 {
41   (*OutStream) << kScanningMessage;
42   return S_OK;
43 }
44
45 HRESULT CUpdateCallbackConsole::CanNotFindError(const wchar_t *name, DWORD systemError)
46 {
47   CantFindFiles.Add(name);
48   CantFindCodes.Add(systemError);
49   // m_PercentPrinter.ClosePrint();
50   if (!m_WarningsMode)
51   {
52     (*OutStream) << endl << endl;
53     m_PercentPrinter.PrintNewLine();
54     m_WarningsMode = true;
55   }
56   m_PercentPrinter.PrintString(name);
57   m_PercentPrinter.PrintString(":  WARNING: ");
58   m_PercentPrinter.PrintString(NError::MyFormatMessageW(systemError));
59   m_PercentPrinter.PrintNewLine();
60   return S_OK;
61 }
62
63 HRESULT CUpdateCallbackConsole::FinishScanning()
64 {
65   (*OutStream) << endl << endl;
66   return S_OK;
67 }
68
69 HRESULT CUpdateCallbackConsole::StartArchive(const wchar_t *name, bool updating)
70 {
71   if(updating)
72     (*OutStream) << kUpdatingArchiveMessage;
73   else
74     (*OutStream) << kCreatingArchiveMessage; 
75   if (name != 0)
76     (*OutStream) << name;
77   else
78     (*OutStream) << "StdOut";
79   (*OutStream) << endl << endl;
80   return S_OK;
81 }
82
83 HRESULT CUpdateCallbackConsole::FinishArchive()
84 {
85   (*OutStream) << endl;
86   return S_OK;
87 }
88
89 HRESULT CUpdateCallbackConsole::CheckBreak()
90 {
91   if (NConsoleClose::TestBreakSignal())
92     return E_ABORT;
93   return S_OK;
94 }
95
96 HRESULT CUpdateCallbackConsole::Finilize()
97 {
98   MT_LOCK
99   if (m_NeedBeClosed)
100   {
101     if (EnablePercents)
102     {
103       m_PercentPrinter.ClosePrint();
104     }
105     if (!StdOutMode && m_NeedNewLine)
106     {
107       m_PercentPrinter.PrintNewLine();
108       m_NeedNewLine = false;
109     }
110     m_NeedBeClosed = false;
111   }
112   return S_OK;
113 }
114
115 HRESULT CUpdateCallbackConsole::SetNumFiles(UInt64 /* numFiles */)
116 {
117   return S_OK;
118 }
119
120 HRESULT CUpdateCallbackConsole::SetTotal(UInt64 size)
121 {
122   MT_LOCK
123   if (EnablePercents)
124     m_PercentPrinter.SetTotal(size);
125   return S_OK;
126 }
127
128 HRESULT CUpdateCallbackConsole::SetCompleted(const UInt64 *completeValue)
129 {
130   MT_LOCK
131   if (completeValue != NULL)
132   {
133     if (EnablePercents)
134     {
135       m_PercentPrinter.SetRatio(*completeValue);
136       m_PercentPrinter.PrintRatio();
137       m_NeedBeClosed = true;
138     }
139   }
140   if (NConsoleClose::TestBreakSignal())
141     return E_ABORT;
142   return S_OK;
143 }
144
145 HRESULT CUpdateCallbackConsole::SetRatioInfo(const UInt64 * /* inSize */, const UInt64 * /* outSize */)
146 {
147   /*
148   if (NConsoleClose::TestBreakSignal())
149     return E_ABORT;
150   */
151   return S_OK;
152 }
153
154 HRESULT CUpdateCallbackConsole::GetStream(const wchar_t *name, bool isAnti)
155 {
156   MT_LOCK
157   if (StdOutMode)
158     return S_OK;
159   if(isAnti)
160     m_PercentPrinter.PrintString("Anti item    ");
161   else
162     m_PercentPrinter.PrintString("Compressing  ");
163   if (name[0] == 0)
164     name = kEmptyFileAlias;
165   m_PercentPrinter.PrintString(name);
166   if (EnablePercents)
167     m_PercentPrinter.RePrintRatio();
168   return S_OK;
169 }
170
171 HRESULT CUpdateCallbackConsole::OpenFileError(const wchar_t *name, DWORD systemError)
172 {
173   MT_LOCK
174   FailedCodes.Add(systemError);
175   FailedFiles.Add(name);
176   // if (systemError == ERROR_SHARING_VIOLATION)
177   {
178     m_PercentPrinter.ClosePrint();
179     m_PercentPrinter.PrintNewLine();
180     m_PercentPrinter.PrintString("WARNING: ");
181     m_PercentPrinter.PrintString(NError::MyFormatMessageW(systemError));
182     return S_FALSE;
183   }
184   // return systemError;
185 }
186
187 HRESULT CUpdateCallbackConsole::SetOperationResult(Int32 )
188 {
189   m_NeedBeClosed = true;
190   m_NeedNewLine = true;
191   return S_OK;  
192 }
193
194 HRESULT CUpdateCallbackConsole::CryptoGetTextPassword2(Int32 *passwordIsDefined, BSTR *password)
195 {
196   if (!PasswordIsDefined) 
197   {
198     if (AskPassword)
199     {
200       Password = GetPassword(OutStream); 
201       PasswordIsDefined = true;
202     }
203   }
204   *passwordIsDefined = BoolToInt(PasswordIsDefined);
205   CMyComBSTR tempName(Password);
206   *password = tempName.Detach();
207   return S_OK;
208 }