Skip to content

Commit

Permalink
Remove duplicated code in SkinInstaller.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
poiru committed Dec 26, 2015
1 parent 1b054fc commit e6fc0f0
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 108 deletions.
14 changes: 8 additions & 6 deletions Library/DialogInstall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
* obtain one at <https://www.gnu.org/licenses/gpl-2.0.html>. */

#include "StdAfx.h"
#include "SkinInstaller.h"
#include "DialogInstall.h"
#include "../Common/StringUtil.h"
#include "SkinInstaller.h"
#include "resource.h"
#include "System.h"
#include "../Version.h"

#define WM_DELAYED_CLOSE WM_APP + 0
Expand Down Expand Up @@ -143,7 +145,7 @@ INT_PTR DialogInstall::OnInitDialog(WPARAM wParam, LPARAM lParam)
HICON hIcon = (HICON)LoadImage(GetModuleHandle(nullptr), MAKEINTRESOURCE(IDI_SKININSTALLER), IMAGE_ICON, 16, 16, LR_SHARED);
SendMessage(m_Window, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);

if (GetOSPlatform() >= OSPLATFORM_VISTA)
if (IsWindowsVistaOrGreater())
{
SetDialogFont();
}
Expand Down Expand Up @@ -375,7 +377,7 @@ bool DialogInstall::ReadPackage()
return false;
}

m_PackageUnzFile = unzOpen(ConvertToAscii(fileName).c_str());
m_PackageUnzFile = unzOpen(StringUtil::Narrow(fileName).c_str());
if (!m_PackageUnzFile)
{
return false;
Expand Down Expand Up @@ -688,7 +690,7 @@ bool DialogInstall::InstallPackage()
fo.pFrom = to.c_str();
SHFileOperation(&fo);

if (!CopyFiles(from, to, true))
if (!System::CopyFiles(from, to, true))
{
m_ErrorMessage = L"Unable to move to:\n";
m_ErrorMessage += to;
Expand Down Expand Up @@ -1374,7 +1376,7 @@ void DialogInstall::TabInstall::Initialize()

DWORD extendedFlags = LVS_EX_CHECKBOXES | LVS_EX_LABELTIP | LVS_EX_FULLROWSELECT;

if (GetOSPlatform() >= OSPLATFORM_VISTA)
if (IsWindowsVistaOrGreater())
{
extendedFlags |= LVS_EX_DOUBLEBUFFER;
SetWindowTheme(item, L"explorer", nullptr);
Expand All @@ -1400,7 +1402,7 @@ void DialogInstall::TabInstall::Initialize()
LVGROUP lvg;
lvg.cbSize = sizeof(LVGROUP);
lvg.mask = LVGF_HEADER | LVGF_GROUPID | LVGF_STATE;
lvg.state = (GetOSPlatform() >= OSPLATFORM_VISTA) ? LVGS_COLLAPSIBLE : LVGS_NORMAL;
lvg.state = IsWindowsVistaOrGreater() ? LVGS_COLLAPSIBLE : LVGS_NORMAL;

LVITEM lvi;
lvi.mask = LVIF_TEXT | LVIF_GROUPID | LVIF_PARAM;
Expand Down
12 changes: 6 additions & 6 deletions Library/DialogPackage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
* obtain one at <https://www.gnu.org/licenses/gpl-2.0.html>. */

#include "StdAfx.h"
#include "DialogPackage.h"
#include "../Common/StringUtil.h"
#include "SkinInstaller.h"
#include "DialogPackage.h"
#include "DialogInstall.h"
#include "resource.h"
#include "../Version.h"
Expand Down Expand Up @@ -112,7 +112,7 @@ INT_PTR DialogPackage::OnInitDialog(WPARAM wParam, LPARAM lParam)
HICON hIcon = (HICON)LoadImage(GetModuleHandle(nullptr), MAKEINTRESOURCE(IDI_SKININSTALLER), IMAGE_ICON, 16, 16, LR_SHARED);
SendMessage(m_Window, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);

if (GetOSPlatform() >= OSPLATFORM_VISTA)
if (IsWindowsVistaOrGreater())
{
SetDialogFont();
}
Expand Down Expand Up @@ -258,7 +258,7 @@ bool DialogPackage::CreatePackage()
m_AllowNonAsciiFilenames = DialogInstall::CompareVersions(m_MinimumRainmeter, L"3.0.1") != -1;

// Create archive and add options file and header bitmap
m_ZipFile = zipOpen(ConvertToAscii(m_TargetFile.c_str()).c_str(), APPEND_STATUS_CREATE);
m_ZipFile = zipOpen(StringUtil::Narrow(m_TargetFile.c_str()).c_str(), APPEND_STATUS_CREATE);

auto cleanup = [&]()->bool
{
Expand Down Expand Up @@ -751,7 +751,7 @@ INT_PTR CALLBACK DialogPackage::SelectPluginDlgProc(HWND hWnd, UINT uMsg, WPARAM

bool x32 = LOWORD(wParam) == IDC_PACKAGESELECTPLUGIN_32BITBROWSE_BUTTON;

LOADED_IMAGE* loadedImage = ImageLoad(ConvertToAscii(buffer).c_str(), nullptr);
LOADED_IMAGE* loadedImage = ImageLoad(StringUtil::Narrow(buffer).c_str(), nullptr);
if (loadedImage)
{
WORD machine = loadedImage->FileHeader->FileHeader.Machine;
Expand Down Expand Up @@ -829,7 +829,7 @@ void DialogPackage::TabInfo::Initialize()

DWORD extendedFlags = LVS_EX_LABELTIP | LVS_EX_FULLROWSELECT;

if (GetOSPlatform() >= OSPLATFORM_VISTA)
if (IsWindowsVistaOrGreater())
{
extendedFlags |= LVS_EX_DOUBLEBUFFER;
SetWindowTheme(item, L"explorer", nullptr);
Expand All @@ -851,7 +851,7 @@ void DialogPackage::TabInfo::Initialize()
LVGROUP lvg;
lvg.cbSize = sizeof(LVGROUP);
lvg.mask = LVGF_HEADER | LVGF_GROUPID | LVGF_STATE;
lvg.state = (GetOSPlatform() >= OSPLATFORM_VISTA) ? LVGS_COLLAPSIBLE : LVGS_NORMAL;
lvg.state = IsWindowsVistaOrGreater() ? LVGS_COLLAPSIBLE : LVGS_NORMAL;
lvg.iGroupId = 0;
lvg.pszHeader = L"Skin";
ListView_InsertGroup(item, -1, &lvg);
Expand Down
84 changes: 0 additions & 84 deletions Library/SkinInstaller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,87 +239,3 @@ bool IsRunning(const WCHAR* name, HANDLE* hMutex)
return false;
}
}

bool CopyFiles(const std::wstring& strFrom, const std::wstring& strTo, bool bMove)
{
std::wstring tmpFrom(strFrom), tmpTo(strTo);

// The strings must end with double nul
tmpFrom.append(1, L'\0');
tmpTo.append(1, L'\0');

SHFILEOPSTRUCT fo =
{
nullptr,
(UINT)(bMove ? FO_MOVE : FO_COPY),
tmpFrom.c_str(),
tmpTo.c_str(),
FOF_NO_UI | FOF_NOCONFIRMATION | FOF_ALLOWUNDO
};

return SHFileOperation(&fo) == 0;
}

OSPLATFORM GetOSPlatform()
{
OSVERSIONINFOEX osvi = {sizeof(OSVERSIONINFOEX)};
if (GetVersionEx((OSVERSIONINFO*)&osvi))
{
if (osvi.dwMajorVersion == 5)
{
// Not checking for osvi.dwMinorVersion >= 1 because Rainmeter won't run on pre-XP
return OSPLATFORM_XP;
}
else if (osvi.dwMajorVersion == 6)
{
if (osvi.dwMinorVersion == 0)
{
return OSPLATFORM_VISTA; // Vista, Server 2008
}
else
{
return OSPLATFORM_7; // 7, Server 2008R2
}
}
else // newer OS
{
return OSPLATFORM_7;
}
}

return OSPLATFORM_UNKNOWN;
}

std::string ConvertToAscii(LPCTSTR str)
{
std::string szAscii;

if (str && *str)
{
int strLen = (int)wcslen(str);
int bufLen = WideCharToMultiByte(CP_ACP, 0, str, strLen, nullptr, 0, nullptr, nullptr);
if (bufLen > 0)
{
szAscii.resize(bufLen);
WideCharToMultiByte(CP_ACP, 0, str, strLen, &szAscii[0], bufLen, nullptr, nullptr);
}
}
return szAscii;
}

std::wstring ConvertToWide(LPCSTR str)
{
std::wstring szWide;

if (str && *str)
{
int strLen = (int)strlen(str);
int bufLen = MultiByteToWideChar(CP_ACP, 0, str, strLen, nullptr, 0);
if (bufLen > 0)
{
szWide.resize(bufLen);
MultiByteToWideChar(CP_ACP, 0, str, strLen, &szWide[0], bufLen);
}
}
return szWide;
}
12 changes: 0 additions & 12 deletions Library/SkinInstaller.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,10 @@ struct OsNameVersion
const WCHAR* version;
};

enum OSPLATFORM
{
OSPLATFORM_UNKNOWN = 0,
OSPLATFORM_XP,
OSPLATFORM_VISTA,
OSPLATFORM_7
};

HINSTANCE GetInstanceHandle();

bool CloseRainmeterIfActive();
OSPLATFORM GetOSPlatform();

bool IsRunning(const WCHAR* name, HANDLE* hMutex);
bool CopyFiles(const std::wstring& strFrom, const std::wstring& strTo, bool bMove = false);
std::string ConvertToAscii(LPCTSTR str);
std::wstring ConvertToWide(LPCSTR str);

#endif

0 comments on commit e6fc0f0

Please sign in to comment.