📄 compressdialog.cpp
字号:
// CompressDialog.cpp
#include "StdAfx.h"
#include "resource.h"
#include "Common/Defs.h"
#include "Common/StringConvert.h"
#include "Common/IntToString.h"
#include "Windows/CommonDialog.h"
#include "Windows/FileDir.h"
#include "Windows/FileName.h"
#include "Windows/ResourceString.h"
#include "Windows/System.h"
#include "../FileManager/HelpUtils.h"
#include "../FileManager/SplitUtils.h"
#include "../FileManager/FormatUtils.h"
#include "../Explorer/MyMessages.h"
#include "../Common/ZipRegistry.h"
#include "CompressDialog.h"
#ifndef _UNICODE
extern bool g_IsNT;
#endif
#ifdef LANG
#include "../FileManager/LangUtils.h"
#endif
#include "CompressDialogRes.h"
#define MY_SIZE_OF_ARRAY(x) (sizeof(x) / sizeof(x[0]))
#ifdef LANG
static CIDLangPair kIDLangPairs[] =
{
{ IDC_STATIC_COMPRESS_ARCHIVE, 0x02000D01 },
{ IDC_STATIC_COMPRESS_FORMAT, 0x02000D03 },
{ IDC_STATIC_COMPRESS_LEVEL, 0x02000D0B },
{ IDC_STATIC_COMPRESS_METHOD, 0x02000D04 },
{ IDC_STATIC_COMPRESS_DICTIONARY, 0x02000D0C },
{ IDC_STATIC_COMPRESS_ORDER, 0x02000D0D },
{ IDC_STATIC_COMPRESS_MEMORY, 0x02000D0E },
{ IDC_STATIC_COMPRESS_MEMORY_DE, 0x02000D0F },
{ IDC_STATIC_COMPRESS_THREADS, 0x02000D12 },
{ IDC_STATIC_COMPRESS_SOLID, 0x02000D13 },
{ IDC_STATIC_COMPRESS_VOLUME, 0x02000D40 },
{ IDC_STATIC_COMPRESS_PARAMETERS, 0x02000D06 },
{ IDC_STATIC_COMPRESS_UPDATE_MODE, 0x02000D02 },
{ IDC_STATIC_COMPRESS_OPTIONS, 0x02000D07 },
{ IDC_COMPRESS_SFX, 0x02000D08 },
{ IDC_COMPRESS_SHARED, 0x02000D16 },
{ IDC_COMPRESS_ENCRYPTION, 0x02000D10 },
{ IDC_STATIC_COMPRESS_PASSWORD1, 0x02000B01 },
{ IDC_STATIC_COMPRESS_PASSWORD2, 0x02000B03 },
{ IDC_COMPRESS_CHECK_SHOW_PASSWORD, 0x02000B02 },
{ IDC_STATIC_COMPRESS_ENCRYPTION_METHOD, 0x02000D11 },
{ IDC_COMPRESS_CHECK_ENCRYPT_FILE_NAMES, 0x02000D0A },
{ IDOK, 0x02000702 },
{ IDCANCEL, 0x02000710 },
{ IDHELP, 0x02000720 }
};
#endif
using namespace NWindows;
using namespace NFile;
using namespace NName;
using namespace NDirectory;
static const int kHistorySize = 20;
static LPCWSTR kExeExt = L".exe";
static LPCWSTR k7zFormat = L"7z";
struct CLevelInfo
{
UInt32 ResourceID;
UInt32 LangID;
};
enum ELevel
{
kStore = 0,
kFastest = 1,
kFast = 3,
kNormal = 5,
kMaximum = 7,
kUltra = 9
};
static const CLevelInfo g_Levels[] =
{
{ IDS_METHOD_STORE, 0x02000D81 },
{ IDS_METHOD_FASTEST, 0x02000D85 },
{ 0, 0 },
{ IDS_METHOD_FAST, 0x02000D84 },
{ 0, 0 },
{ IDS_METHOD_NORMAL, 0x02000D82 },
{ 0, 0 },
{ IDS_METHOD_MAXIMUM, 0x02000D83 },
{ 0, 0 },
{ IDS_METHOD_ULTRA, 0x02000D86 }
};
enum EMethodID
{
kCopy,
kLZMA,
kPPMd,
kBZip2,
kDeflate,
kDeflate64
};
static const LPCWSTR kMethodsNames[] =
{
L"Copy",
L"LZMA",
L"PPMd",
L"BZip2",
L"Deflate",
L"Deflate64"
};
static const EMethodID g_7zMethods[] =
{
kLZMA,
kPPMd,
kBZip2
};
static const EMethodID g_7zSfxMethods[] =
{
kCopy,
kLZMA,
kPPMd
};
static EMethodID g_ZipMethods[] =
{
kDeflate,
kDeflate64,
kBZip2,
kLZMA
};
static EMethodID g_GZipMethods[] =
{
kDeflate
};
static EMethodID g_BZip2Methods[] =
{
kBZip2
};
struct CFormatInfo
{
LPCWSTR Name;
UInt32 LevelsMask;
const EMethodID *MathodIDs;
int NumMethods;
bool Filter;
bool Solid;
bool MultiThread;
bool SFX;
bool Encrypt;
bool EncryptFileNames;
};
#define METHODS_PAIR(x) x, MY_SIZE_OF_ARRAY(x)
static const CFormatInfo g_Formats[] =
{
{
L"",
(1 << 0) | (1 << 1) | (1 << 3) | (1 << 5) | (1 << 7) | (1 << 9),
0, 0,
false, false, false, false, false, false
},
{
k7zFormat,
(1 << 0) | (1 << 1) | (1 << 3) | (1 << 5) | (1 << 7) | (1 << 9),
METHODS_PAIR(g_7zMethods),
true, true, true, true, true, true
},
{
L"Zip",
(1 << 0) | (1 << 1) | (1 << 3) | (1 << 5) | (1 << 7) | (1 << 9),
METHODS_PAIR(g_ZipMethods),
false, false, true, false, true, false
},
{
L"GZip",
(1 << 1) | (1 << 5) | (1 << 7) | (1 << 9),
METHODS_PAIR(g_GZipMethods),
false, false, false, false, false, false
},
{
L"BZip2",
(1 << 1) | (1 << 3) | (1 << 5) | (1 << 7) | (1 << 9),
METHODS_PAIR(g_BZip2Methods),
false, false, true, false, false
},
{
L"Tar",
(1 << 0),
0, 0,
false, false, false, false, false, false
}
};
static bool IsMethodSupportedBySfx(int methodID)
{
for (int i = 0; i < MY_SIZE_OF_ARRAY(g_7zSfxMethods); i++)
if (methodID == g_7zSfxMethods[i])
return true;
return false;
};
static UInt64 GetMaxRamSizeForProgram()
{
UInt64 physSize = NSystem::GetRamSize();
const UInt64 kMinSysSize = (1 << 24);
if (physSize <= kMinSysSize)
physSize = 0;
else
physSize -= kMinSysSize;
const UInt64 kMinUseSize = (1 << 25);
if (physSize < kMinUseSize)
physSize = kMinUseSize;
return physSize;
}
bool CCompressDialog::OnInit()
{
#ifdef LANG
LangSetWindowText(HWND(*this), 0x02000D00);
LangSetDlgItemsText(HWND(*this), kIDLangPairs, MY_SIZE_OF_ARRAY(kIDLangPairs) );
#endif
_password1Control.Attach(GetItem(IDC_COMPRESS_EDIT_PASSWORD1));
_password2Control.Attach(GetItem(IDC_COMPRESS_EDIT_PASSWORD2));
_password1Control.SetText(Info.Password);
_password2Control.SetText(Info.Password);
_encryptionMethod.Attach(GetItem(IDC_COMPRESS_COMBO_ENCRYPTION_METHOD));
m_ArchivePath.Attach(GetItem(IDC_COMPRESS_COMBO_ARCHIVE));
m_Format.Attach(GetItem(IDC_COMPRESS_COMBO_FORMAT));
m_Level.Attach(GetItem(IDC_COMPRESS_COMBO_LEVEL));
m_Method.Attach(GetItem(IDC_COMPRESS_COMBO_METHOD));
m_Dictionary.Attach(GetItem(IDC_COMPRESS_COMBO_DICTIONARY));
m_Order.Attach(GetItem(IDC_COMPRESS_COMBO_ORDER));
m_Solid.Attach(GetItem(IDC_COMPRESS_COMBO_SOLID));
m_NumThreads.Attach(GetItem(IDC_COMPRESS_COMBO_THREADS));
m_UpdateMode.Attach(GetItem(IDC_COMPRESS_COMBO_UPDATE_MODE));
m_Volume.Attach(GetItem(IDC_COMPRESS_COMBO_VOLUME));
m_Params.Attach(GetItem(IDC_COMPRESS_EDIT_PARAMETERS));
AddVolumeItems(m_Volume);
ReadCompressionInfo(m_RegistryInfo);
CheckButton(IDC_COMPRESS_CHECK_SHOW_PASSWORD, m_RegistryInfo.ShowPassword);
CheckButton(IDC_COMPRESS_CHECK_ENCRYPT_FILE_NAMES, m_RegistryInfo.EncryptHeaders);
UpdatePasswordControl();
Info.ArchiverInfoIndex = 0;
int i;
for(i = 0; i < m_ArchiverInfoList.Size(); i++)
{
const CArcInfoEx &ai = m_ArchiverInfoList[i];
m_Format.AddString(ai.Name);
if (ai.Name.CompareNoCase(m_RegistryInfo.ArchiveType) == 0)
Info.ArchiverInfoIndex = i;
}
m_Format.SetCurSel(Info.ArchiverInfoIndex);
SetArchiveName(Info.ArchiveName);
SetLevel();
SetParams();
for(i = 0; i < m_RegistryInfo.HistoryArchives.Size() && i < kHistorySize; i++)
m_ArchivePath.AddString(m_RegistryInfo.HistoryArchives[i]);
m_UpdateMode.AddString(LangString(IDS_COMPRESS_UPDATE_MODE_ADD, 0x02000DA1));
m_UpdateMode.AddString(LangString(IDS_COMPRESS_UPDATE_MODE_UPDATE, 0x02000DA2));
m_UpdateMode.AddString(LangString(IDS_COMPRESS_UPDATE_MODE_FRESH, 0x02000DA3));
m_UpdateMode.AddString(LangString(IDS_COMPRESS_UPDATE_MODE_SYNCHRONIZE, 0x02000DA4));
m_UpdateMode.SetCurSel(0);
SetSolidBlockSize();
SetNumThreads();
TCHAR s[40] = { TEXT('/'), TEXT(' '), 0 };
ConvertUInt64ToString(NSystem::GetNumberOfProcessors(), s + 2);
SetItemText(IDC_COMPRESS_HARDWARE_THREADS, s);
CheckButton(IDC_COMPRESS_SFX, Info.SFXMode);
CheckButton(IDC_COMPRESS_SHARED, Info.OpenShareForWrite);
CheckControlsEnable();
OnButtonSFX();
SetEncryptionMethod();
SetMemoryUsage();
return CModalDialog::OnInit();
}
namespace NCompressDialog
{
bool CInfo::GetFullPathName(UString &result) const
{
NDirectory::MySetCurrentDirectory(CurrentDirPrefix);
return MyGetFullPathName(ArchiveName, result);
}
}
void CCompressDialog::UpdatePasswordControl()
{
bool showPassword = IsShowPasswordChecked();
TCHAR c = showPassword ? 0: TEXT('*');
_password1Control.SetPasswordChar(c);
_password2Control.SetPasswordChar(c);
UString password;
_password1Control.GetText(password);
_password1Control.SetText(password);
_password2Control.GetText(password);
_password2Control.SetText(password);
int cmdShow = showPassword ? SW_HIDE : SW_SHOW;
ShowItem(IDC_STATIC_COMPRESS_PASSWORD2, cmdShow);
_password2Control.Show(cmdShow);
}
bool CCompressDialog::OnButtonClicked(int buttonID, HWND buttonHWND)
{
switch(buttonID)
{
case IDC_COMPRESS_BUTTON_SET_ARCHIVE:
{
OnButtonSetArchive();
return true;
}
case IDC_COMPRESS_SFX:
{
OnButtonSFX();
SetMemoryUsage();
return true;
}
case IDC_COMPRESS_CHECK_SHOW_PASSWORD:
{
UpdatePasswordControl();
return true;
}
}
return CModalDialog::OnButtonClicked(buttonID, buttonHWND);
}
void CCompressDialog::CheckSFXControlsEnable()
{
const CFormatInfo &fi = g_Formats[GetStaticFormatIndex()];
bool enable = fi.SFX;
if (enable)
{
int methodID = GetMethodID();
enable = (methodID == -1 || IsMethodSupportedBySfx(methodID));
}
if (!enable)
CheckButton(IDC_COMPRESS_SFX, false);
EnableItem(IDC_COMPRESS_SFX, enable);
}
void CCompressDialog::CheckVolumeEnable()
{
bool isSFX = IsSFX();
m_Volume.Enable(!isSFX);
if (isSFX)
m_Volume.SetText(TEXT(""));
}
void CCompressDialog::CheckControlsEnable()
{
const CFormatInfo &fi = g_Formats[GetStaticFormatIndex()];
Info.SolidIsSpecified = fi.Solid;
bool multiThreadEnable = fi.MultiThread;
Info.MultiThreadIsAllowed = multiThreadEnable;
Info.EncryptHeadersIsAllowed = fi.EncryptFileNames;
EnableItem(IDC_COMPRESS_COMBO_SOLID, fi.Solid);
EnableItem(IDC_COMPRESS_COMBO_THREADS, multiThreadEnable);
CheckSFXControlsEnable();
CheckVolumeEnable();
EnableItem(IDC_COMPRESS_ENCRYPTION, fi.Encrypt);
EnableItem(IDC_STATIC_COMPRESS_PASSWORD1, fi.Encrypt);
EnableItem(IDC_STATIC_COMPRESS_PASSWORD2, fi.Encrypt);
EnableItem(IDC_COMPRESS_EDIT_PASSWORD1, fi.Encrypt);
EnableItem(IDC_COMPRESS_EDIT_PASSWORD2, fi.Encrypt);
EnableItem(IDC_COMPRESS_CHECK_SHOW_PASSWORD, fi.Encrypt);
EnableItem(IDC_STATIC_COMPRESS_ENCRYPTION_METHOD, fi.Encrypt);
EnableItem(IDC_COMPRESS_COMBO_ENCRYPTION_METHOD, fi.Encrypt);
EnableItem(IDC_COMPRESS_CHECK_ENCRYPT_FILE_NAMES, fi.EncryptFileNames);
ShowItem(IDC_COMPRESS_CHECK_ENCRYPT_FILE_NAMES, fi.EncryptFileNames ? SW_SHOW : SW_HIDE);
}
bool CCompressDialog::IsSFX()
{
CWindow sfxButton = GetItem(IDC_COMPRESS_SFX);
return sfxButton.IsEnabled() && IsButtonCheckedBool(IDC_COMPRESS_SFX);
}
void CCompressDialog::OnButtonSFX()
{
SetMethod();
UString fileName;
m_ArchivePath.GetText(fileName);
int dotPos = fileName.ReverseFind(L'.');
int slashPos = fileName.ReverseFind(WCHAR_PATH_SEPARATOR);
if (dotPos < 0 || dotPos <= slashPos)
dotPos = -1;
bool isSFX = IsSFX();
if (isSFX)
{
if (dotPos >= 0)
fileName = fileName.Left(dotPos);
fileName += kExeExt;
m_ArchivePath.SetText(fileName);
}
else
{
if (dotPos >= 0)
{
UString ext = fileName.Mid(dotPos);
if (ext.CompareNoCase(kExeExt) == 0)
{
fileName = fileName.Left(dotPos);
m_ArchivePath.SetText(fileName);
}
}
SetArchiveName2(false); // it's for OnInit
}
CheckVolumeEnable();
}
void CCompressDialog::OnButtonSetArchive()
{
UString fileName;
m_ArchivePath.GetText(fileName);
fileName.Trim();
Info.ArchiveName = fileName;
UString fullFileName;
if (!Info.GetFullPathName(fullFileName))
{
fullFileName = Info.ArchiveName;
return;
}
UString title = LangString(IDS_COMPRESS_SET_ARCHIVE_DIALOG_TITLE, 0x02000D90);
UString s = LangString(IDS_OPEN_TYPE_ALL_FILES, 0x02000DB1);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -