~ubuntu-branches/ubuntu/precise/p7zip/precise-updates

« back to all changes in this revision

Viewing changes to CPP/7zip/UI/Common/ZipRegistry.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Mohammed Adnène Trojette
  • Date: 2009-02-14 20:12:27 UTC
  • mfrom: (1.1.11 upstream) (2.1.3 sid)
  • Revision ID: james.westby@ubuntu.com-20090214201227-go63qxm9ozfdma60
Tags: 4.65~dfsg.1-1
* New upstream release.
* Remove wx2.8 Build-Depends added by mistakes (7zG is not yet
  intended to be built).
* Use dh_clean without -k.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// ZipRegistry.cpp
 
2
 
 
3
#include "StdAfx.h"
 
4
 
 
5
#include "ZipRegistry.h"
 
6
 
 
7
#include "Common/IntToString.h"
 
8
#include "Common/StringConvert.h"
 
9
 
 
10
#include "Windows/Synchronization.h"
 
11
#include "Windows/Registry.h"
 
12
 
 
13
#include "Windows/FileDir.h"
 
14
 
 
15
using namespace NWindows;
 
16
using namespace NRegistry;
 
17
 
 
18
static const TCHAR *kCUBasePath = TEXT("Software") TEXT(STRING_PATH_SEPARATOR) TEXT("7-ZIP");
 
19
 
 
20
static NSynchronization::CCriticalSection g_RegistryOperationsCriticalSection;
 
21
 
 
22
//////////////////////
 
23
// ExtractionInfo
 
24
 
 
25
static const TCHAR *kExtractionKeyName = TEXT("Extraction");
 
26
 
 
27
static const TCHAR *kExtractionPathHistoryKeyName = TEXT("PathHistory");
 
28
static const TCHAR *kExtractionExtractModeValueName = TEXT("ExtarctMode");
 
29
static const TCHAR *kExtractionOverwriteModeValueName = TEXT("OverwriteMode");
 
30
static const TCHAR *kExtractionShowPasswordValueName = TEXT("ShowPassword");
 
31
 
 
32
static CSysString GetKeyPath(const CSysString &path)
 
33
{
 
34
  return CSysString(kCUBasePath) + CSysString(CHAR_PATH_SEPARATOR) + path;
 
35
}
 
36
 
 
37
void SaveExtractionInfo(const NExtract::CInfo &info)
 
38
{
 
39
  NSynchronization::CCriticalSectionLock lock(g_RegistryOperationsCriticalSection);
 
40
  CKey extractionKey;
 
41
  extractionKey.Create(HKEY_CURRENT_USER, GetKeyPath(kExtractionKeyName));
 
42
  extractionKey.RecurseDeleteKey(kExtractionPathHistoryKeyName);
 
43
  {
 
44
    CKey pathHistoryKey;
 
45
    pathHistoryKey.Create(extractionKey, kExtractionPathHistoryKeyName);
 
46
    for(int i = 0; i < info.Paths.Size(); i++)
 
47
    {
 
48
      wchar_t numberString[16];
 
49
      ConvertUInt64ToString(i, numberString);
 
50
      pathHistoryKey.SetValue(numberString, info.Paths[i]);
 
51
    }
 
52
  }
 
53
  extractionKey.SetValue(kExtractionExtractModeValueName, UInt32(info.PathMode));
 
54
  extractionKey.SetValue(kExtractionOverwriteModeValueName, UInt32(info.OverwriteMode));
 
55
  extractionKey.SetValue(kExtractionShowPasswordValueName, info.ShowPassword);
 
56
}
 
57
 
 
58
void ReadExtractionInfo(NExtract::CInfo &info)
 
59
{
 
60
  info.Paths.Clear();
 
61
  info.PathMode = NExtract::NPathMode::kCurrentPathnames;
 
62
  info.OverwriteMode = NExtract::NOverwriteMode::kAskBefore;
 
63
  info.ShowPassword = false;
 
64
 
 
65
  NSynchronization::CCriticalSectionLock lock(g_RegistryOperationsCriticalSection);
 
66
  CKey extractionKey;
 
67
  if(extractionKey.Open(HKEY_CURRENT_USER, GetKeyPath(kExtractionKeyName), KEY_READ) != ERROR_SUCCESS)
 
68
    return;
 
69
  
 
70
  {
 
71
    CKey pathHistoryKey;
 
72
    if(pathHistoryKey.Open(extractionKey, kExtractionPathHistoryKeyName, KEY_READ) ==
 
73
        ERROR_SUCCESS)
 
74
    {
 
75
      for (;;)
 
76
      {
 
77
        wchar_t numberString[16];
 
78
        ConvertUInt64ToString(info.Paths.Size(), numberString);
 
79
        UString path;
 
80
        if (pathHistoryKey.QueryValue(numberString, path) != ERROR_SUCCESS)
 
81
          break;
 
82
        info.Paths.Add(path);
 
83
      }
 
84
    }
 
85
  }
 
86
  UInt32 extractModeIndex;
 
87
  if (extractionKey.QueryValue(kExtractionExtractModeValueName, extractModeIndex) == ERROR_SUCCESS)
 
88
  {
 
89
    switch (extractModeIndex)
 
90
    {
 
91
      case NExtract::NPathMode::kFullPathnames:
 
92
      case NExtract::NPathMode::kCurrentPathnames:
 
93
      case NExtract::NPathMode::kNoPathnames:
 
94
        info.PathMode = NExtract::NPathMode::EEnum(extractModeIndex);
 
95
        break;
 
96
    }
 
97
  }
 
98
  UInt32 overwriteModeIndex;
 
99
  if (extractionKey.QueryValue(kExtractionOverwriteModeValueName, overwriteModeIndex) == ERROR_SUCCESS)
 
100
  {
 
101
    switch (overwriteModeIndex)
 
102
    {
 
103
      case NExtract::NOverwriteMode::kAskBefore:
 
104
      case NExtract::NOverwriteMode::kWithoutPrompt:
 
105
      case NExtract::NOverwriteMode::kSkipExisting:
 
106
      case NExtract::NOverwriteMode::kAutoRename:
 
107
      case NExtract::NOverwriteMode::kAutoRenameExisting:
 
108
        info.OverwriteMode = NExtract::NOverwriteMode::EEnum(overwriteModeIndex);
 
109
        break;
 
110
    }
 
111
  }
 
112
  if (extractionKey.QueryValue(kExtractionShowPasswordValueName,
 
113
      info.ShowPassword) != ERROR_SUCCESS)
 
114
    info.ShowPassword = false;
 
115
}
 
116
 
 
117
///////////////////////////////////
 
118
// CompressionInfo
 
119
 
 
120
static const TCHAR *kCompressionKeyName = TEXT("Compression");
 
121
 
 
122
static const TCHAR *kCompressionHistoryArchivesKeyName = TEXT("ArcHistory");
 
123
static const TCHAR *kCompressionLevelValueName = TEXT("Level");
 
124
static const TCHAR *kCompressionLastFormatValueName = TEXT("Archiver");
 
125
static const TCHAR *kCompressionShowPasswordValueName = TEXT("ShowPassword");
 
126
static const TCHAR *kCompressionEncryptHeadersValueName = TEXT("EncryptHeaders");
 
127
 
 
128
static const TCHAR *kCompressionOptionsKeyName = TEXT("Options");
 
129
// static const TCHAR *kSolid = TEXT("Solid");
 
130
// static const TCHAR *kMultiThread = TEXT("Multithread");
 
131
 
 
132
static const WCHAR *kCompressionOptions = L"Options";
 
133
static const TCHAR *kCompressionLevel = TEXT("Level");
 
134
static const WCHAR *kCompressionMethod = L"Method";
 
135
static const WCHAR *kEncryptionMethod = L"EncryptionMethod";
 
136
static const TCHAR *kCompressionDictionary = TEXT("Dictionary");
 
137
static const TCHAR *kCompressionOrder = TEXT("Order");
 
138
static const TCHAR *kCompressionNumThreads = TEXT("NumThreads");
 
139
static const TCHAR *kCompressionBlockSize = TEXT("BlockSize");
 
140
 
 
141
 
 
142
static void SetRegString(CKey &key, const WCHAR *name, const UString &value)
 
143
{
 
144
  if (value.IsEmpty())
 
145
    key.DeleteValue(name);
 
146
  else
 
147
    key.SetValue(name, value);
 
148
}
 
149
 
 
150
static void SetRegUInt32(CKey &key, const TCHAR *name, UInt32 value)
 
151
{
 
152
  if (value == (UInt32)-1)
 
153
    key.DeleteValue(name);
 
154
  else
 
155
    key.SetValue(name, value);
 
156
}
 
157
 
 
158
static void GetRegString(CKey &key, const WCHAR *name, UString &value)
 
159
{
 
160
  if (key.QueryValue(name, value) != ERROR_SUCCESS)
 
161
    value.Empty();
 
162
}
 
163
 
 
164
static void GetRegUInt32(CKey &key, const TCHAR *name, UInt32 &value)
 
165
{
 
166
  if (key.QueryValue(name, value) != ERROR_SUCCESS)
 
167
    value = UInt32(-1);
 
168
}
 
169
 
 
170
void SaveCompressionInfo(const NCompression::CInfo &info)
 
171
{
 
172
  NSynchronization::CCriticalSectionLock lock(g_RegistryOperationsCriticalSection);
 
173
 
 
174
  CKey compressionKey;
 
175
  compressionKey.Create(HKEY_CURRENT_USER, GetKeyPath(kCompressionKeyName));
 
176
  compressionKey.RecurseDeleteKey(kCompressionHistoryArchivesKeyName);
 
177
  {
 
178
    CKey historyArchivesKey;
 
179
    historyArchivesKey.Create(compressionKey, kCompressionHistoryArchivesKeyName);
 
180
    for(int i = 0; i < info.HistoryArchives.Size(); i++)
 
181
    {
 
182
      wchar_t numberString[16];
 
183
      ConvertUInt64ToString(i, numberString);
 
184
      historyArchivesKey.SetValue(numberString, info.HistoryArchives[i]);
 
185
    }
 
186
  }
 
187
 
 
188
  // compressionKey.SetValue(kSolid, info.Solid);
 
189
  // compressionKey.SetValue(kMultiThread, info.MultiThread);
 
190
  compressionKey.RecurseDeleteKey(kCompressionOptionsKeyName);
 
191
  {
 
192
    CKey optionsKey;
 
193
    optionsKey.Create(compressionKey, kCompressionOptionsKeyName);
 
194
    for(int i = 0; i < info.FormatOptionsVector.Size(); i++)
 
195
    {
 
196
      const NCompression::CFormatOptions &fo = info.FormatOptionsVector[i];
 
197
      CKey formatKey;
 
198
      formatKey.Create(optionsKey, fo.FormatID);
 
199
      
 
200
      SetRegString(formatKey, kCompressionOptions, fo.Options);
 
201
      SetRegString(formatKey, kCompressionMethod, fo.Method);
 
202
      SetRegString(formatKey, kEncryptionMethod, fo.EncryptionMethod);
 
203
 
 
204
      SetRegUInt32(formatKey, kCompressionLevel, fo.Level);
 
205
      SetRegUInt32(formatKey, kCompressionDictionary, fo.Dictionary);
 
206
      SetRegUInt32(formatKey, kCompressionOrder, fo.Order);
 
207
      SetRegUInt32(formatKey, kCompressionBlockSize, fo.BlockLogSize);
 
208
      SetRegUInt32(formatKey, kCompressionNumThreads, fo.NumThreads);
 
209
    }
 
210
  }
 
211
 
 
212
  compressionKey.SetValue(kCompressionLevelValueName, UInt32(info.Level));
 
213
  compressionKey.SetValue(kCompressionLastFormatValueName, GetSystemString(info.ArchiveType));
 
214
 
 
215
  compressionKey.SetValue(kCompressionShowPasswordValueName, info.ShowPassword);
 
216
  compressionKey.SetValue(kCompressionEncryptHeadersValueName, info.EncryptHeaders);
 
217
  // compressionKey.SetValue(kCompressionMaximizeValueName, info.Maximize);
 
218
}
 
219
 
 
220
void ReadCompressionInfo(NCompression::CInfo &info)
 
221
{
 
222
  info.HistoryArchives.Clear();
 
223
 
 
224
  // info.Solid = true;
 
225
  // info.MultiThread = IsMultiProcessor();
 
226
  info.FormatOptionsVector.Clear();
 
227
 
 
228
  info.Level = 5;
 
229
  info.ArchiveType = L"7z";
 
230
  // definedStatus.Maximize = false;
 
231
  info.ShowPassword = false;
 
232
  info.EncryptHeaders = false;
 
233
 
 
234
  NSynchronization::CCriticalSectionLock lock(g_RegistryOperationsCriticalSection);
 
235
  CKey compressionKey;
 
236
 
 
237
  if(compressionKey.Open(HKEY_CURRENT_USER,
 
238
      GetKeyPath(kCompressionKeyName), KEY_READ) != ERROR_SUCCESS)
 
239
    return;
 
240
  
 
241
  {
 
242
    CKey historyArchivesKey;
 
243
    if(historyArchivesKey.Open(compressionKey, kCompressionHistoryArchivesKeyName, KEY_READ) ==
 
244
        ERROR_SUCCESS)
 
245
    {
 
246
      for (;;)
 
247
      {
 
248
        wchar_t numberString[16];
 
249
        ConvertUInt64ToString(info.HistoryArchives.Size(), numberString);
 
250
        UString path;
 
251
        if (historyArchivesKey.QueryValue(numberString, path) != ERROR_SUCCESS)
 
252
          break;
 
253
        info.HistoryArchives.Add(path);
 
254
      }
 
255
    }
 
256
  }
 
257
 
 
258
  
 
259
  /*
 
260
  bool solid = false;
 
261
  if (compressionKey.QueryValue(kSolid, solid) == ERROR_SUCCESS)
 
262
    info.Solid = solid;
 
263
  bool multiThread = false;
 
264
  if (compressionKey.QueryValue(kMultiThread, multiThread) == ERROR_SUCCESS)
 
265
    info.MultiThread = multiThread;
 
266
  */
 
267
 
 
268
  {
 
269
    CKey optionsKey;
 
270
    if(optionsKey.Open(compressionKey, kCompressionOptionsKeyName, KEY_READ) ==
 
271
        ERROR_SUCCESS)
 
272
    {
 
273
      CSysStringVector formatIDs;
 
274
      optionsKey.EnumKeys(formatIDs);
 
275
      for(int i = 0; i < formatIDs.Size(); i++)
 
276
      {
 
277
        CKey formatKey;
 
278
        NCompression::CFormatOptions fo;
 
279
        fo.FormatID = formatIDs[i];
 
280
        if(formatKey.Open(optionsKey, fo.FormatID, KEY_READ) == ERROR_SUCCESS)
 
281
        {
 
282
          GetRegString(formatKey, kCompressionOptions, fo.Options);
 
283
          GetRegString(formatKey, kCompressionMethod, fo.Method);
 
284
          GetRegString(formatKey, kEncryptionMethod, fo.EncryptionMethod);
 
285
 
 
286
          GetRegUInt32(formatKey, kCompressionLevel, fo.Level);
 
287
          GetRegUInt32(formatKey, kCompressionDictionary, fo.Dictionary);
 
288
          GetRegUInt32(formatKey, kCompressionOrder, fo.Order);
 
289
          GetRegUInt32(formatKey, kCompressionBlockSize, fo.BlockLogSize);
 
290
          GetRegUInt32(formatKey, kCompressionNumThreads, fo.NumThreads);
 
291
 
 
292
          info.FormatOptionsVector.Add(fo);
 
293
        }
 
294
 
 
295
      }
 
296
    }
 
297
  }
 
298
 
 
299
  UInt32 level;
 
300
  if (compressionKey.QueryValue(kCompressionLevelValueName, level) == ERROR_SUCCESS)
 
301
    info.Level = level;
 
302
  CSysString archiveType;
 
303
  if (compressionKey.QueryValue(kCompressionLastFormatValueName, archiveType) == ERROR_SUCCESS)
 
304
    info.ArchiveType = GetUnicodeString(archiveType);
 
305
  if (compressionKey.QueryValue(kCompressionShowPasswordValueName,
 
306
      info.ShowPassword) != ERROR_SUCCESS)
 
307
    info.ShowPassword = false;
 
308
  if (compressionKey.QueryValue(kCompressionEncryptHeadersValueName,
 
309
      info.EncryptHeaders) != ERROR_SUCCESS)
 
310
    info.EncryptHeaders = false;
 
311
  /*
 
312
  if (compressionKey.QueryValue(kCompressionLevelValueName, info.Maximize) == ERROR_SUCCESS)
 
313
    definedStatus.Maximize = true;
 
314
  */
 
315
}
 
316
 
 
317
 
 
318
///////////////////////////////////
 
319
// WorkDirInfo
 
320
 
 
321
static const TCHAR *kOptionsInfoKeyName = TEXT("Options");
 
322
 
 
323
static const TCHAR *kWorkDirTypeValueName = TEXT("WorkDirType");
 
324
static const WCHAR *kWorkDirPathValueName = L"WorkDirPath";
 
325
static const TCHAR *kTempRemovableOnlyValueName = TEXT("TempRemovableOnly");
 
326
static const TCHAR *kCascadedMenuValueName = TEXT("CascadedMenu");
 
327
static const TCHAR *kContextMenuValueName = TEXT("ContextMenu");
 
328
 
 
329
void SaveWorkDirInfo(const NWorkDir::CInfo &info)
 
330
{
 
331
  NSynchronization::CCriticalSectionLock lock(g_RegistryOperationsCriticalSection);
 
332
  CKey optionsKey;
 
333
  optionsKey.Create(HKEY_CURRENT_USER, GetKeyPath(kOptionsInfoKeyName));
 
334
  optionsKey.SetValue(kWorkDirTypeValueName, UInt32(info.Mode));
 
335
  optionsKey.SetValue(kWorkDirPathValueName, info.Path);
 
336
  optionsKey.SetValue(kTempRemovableOnlyValueName, info.ForRemovableOnly);
 
337
}
 
338
 
 
339
void ReadWorkDirInfo(NWorkDir::CInfo &info)
 
340
{
 
341
  info.SetDefault();
 
342
 
 
343
  NSynchronization::CCriticalSectionLock lock(g_RegistryOperationsCriticalSection);
 
344
  CKey optionsKey;
 
345
  if(optionsKey.Open(HKEY_CURRENT_USER, GetKeyPath(kOptionsInfoKeyName), KEY_READ) != ERROR_SUCCESS)
 
346
    return;
 
347
 
 
348
  UInt32 dirType;
 
349
  if (optionsKey.QueryValue(kWorkDirTypeValueName, dirType) != ERROR_SUCCESS)
 
350
    return;
 
351
  switch (dirType)
 
352
  {
 
353
    case NWorkDir::NMode::kSystem:
 
354
    case NWorkDir::NMode::kCurrent:
 
355
    case NWorkDir::NMode::kSpecified:
 
356
      info.Mode = NWorkDir::NMode::EEnum(dirType);
 
357
  }
 
358
  UString sysWorkDir;
 
359
  if (optionsKey.QueryValue(kWorkDirPathValueName, sysWorkDir) != ERROR_SUCCESS)
 
360
  {
 
361
    info.Path.Empty();
 
362
    if (info.Mode == NWorkDir::NMode::kSpecified)
 
363
      info.Mode = NWorkDir::NMode::kSystem;
 
364
  }
 
365
  info.Path = GetUnicodeString(sysWorkDir);
 
366
  if (optionsKey.QueryValue(kTempRemovableOnlyValueName, info.ForRemovableOnly) != ERROR_SUCCESS)
 
367
    info.SetForRemovableOnlyDefault();
 
368
}
 
369
 
 
370
static void SaveOption(const TCHAR *value, bool enabled)
 
371
{
 
372
  NSynchronization::CCriticalSectionLock lock(g_RegistryOperationsCriticalSection);
 
373
  CKey optionsKey;
 
374
  optionsKey.Create(HKEY_CURRENT_USER, GetKeyPath(kOptionsInfoKeyName));
 
375
  optionsKey.SetValue(value, enabled);
 
376
}
 
377
 
 
378
static bool ReadOption(const TCHAR *value, bool defaultValue)
 
379
{
 
380
  NSynchronization::CCriticalSectionLock lock(g_RegistryOperationsCriticalSection);
 
381
  CKey optionsKey;
 
382
  if(optionsKey.Open(HKEY_CURRENT_USER, GetKeyPath(kOptionsInfoKeyName), KEY_READ) != ERROR_SUCCESS)
 
383
    return defaultValue;
 
384
  bool enabled;
 
385
  if (optionsKey.QueryValue(value, enabled) != ERROR_SUCCESS)
 
386
    return defaultValue;
 
387
  return enabled;
 
388
}
 
389
 
 
390
void SaveCascadedMenu(bool show)
 
391
  { SaveOption(kCascadedMenuValueName, show); }
 
392
bool ReadCascadedMenu()
 
393
  { return ReadOption(kCascadedMenuValueName, true); }
 
394
 
 
395
 
 
396
static void SaveValue(const TCHAR *value, UInt32 valueToWrite)
 
397
{
 
398
  NSynchronization::CCriticalSectionLock lock(g_RegistryOperationsCriticalSection);
 
399
  CKey optionsKey;
 
400
  optionsKey.Create(HKEY_CURRENT_USER, GetKeyPath(kOptionsInfoKeyName));
 
401
  optionsKey.SetValue(value, valueToWrite);
 
402
}
 
403
 
 
404
static bool ReadValue(const TCHAR *value, UInt32 &result)
 
405
{
 
406
  NSynchronization::CCriticalSectionLock lock(g_RegistryOperationsCriticalSection);
 
407
  CKey optionsKey;
 
408
  if(optionsKey.Open(HKEY_CURRENT_USER, GetKeyPath(kOptionsInfoKeyName), KEY_READ) != ERROR_SUCCESS)
 
409
    return false;
 
410
  return (optionsKey.QueryValue(value, result) == ERROR_SUCCESS);
 
411
}
 
412
 
 
413
void SaveContextMenuStatus(UInt32 value)
 
414
  { SaveValue(kContextMenuValueName, value); }
 
415
 
 
416
bool ReadContextMenuStatus(UInt32 &value)
 
417
  { return  ReadValue(kContextMenuValueName, value); }