From 2a7a7cab4cf7a0ffdb55006ccad0bbf6fc1da550 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 16 Sep 2012 15:44:28 +0530 Subject: [PATCH] Calibre portable: Allow renaming of the 'Calibre Library' folder to something else --- setup/installer/windows/freeze.py | 2 +- setup/installer/windows/portable.c | 43 ++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/setup/installer/windows/freeze.py b/setup/installer/windows/freeze.py index 01f4c115d6..a640a6fcd1 100644 --- a/setup/installer/windows/freeze.py +++ b/setup/installer/windows/freeze.py @@ -442,7 +442,7 @@ class Win32Freeze(Command, WixMixIn): '/RELEASE', '/ENTRY:wWinMainCRTStartup', '/OUT:'+exe, self.embed_resources(exe), - obj, 'User32.lib'] + obj, 'User32.lib', 'Shlwapi.lib'] self.run_builder(cmd) self.info('Creating portable installer') diff --git a/setup/installer/windows/portable.c b/setup/installer/windows/portable.c index a63c319cda..606057432f 100644 --- a/setup/installer/windows/portable.c +++ b/setup/installer/windows/portable.c @@ -8,6 +8,7 @@ #include +#include #include #include #include @@ -134,6 +135,46 @@ void launch_calibre(LPCTSTR exe, LPCTSTR config_dir, LPCTSTR library_dir) { } +static BOOL is_dots(LPCTSTR name) { + return _tcscmp(name, _T(".")) == 0 || _tcscmp(name, _T("..")) == 0; +} + +static void find_calibre_library(LPTSTR library_dir) { + TCHAR base[BUFSIZE] = {0}, buf[BUFSIZE] = {0}; + WIN32_FIND_DATA fdFile; + HANDLE hFind = NULL; + + _sntprintf_s(buf, BUFSIZE, _TRUNCATE, _T("%s\\metadata.db"), base); + + if (PathFileExists(buf)) return; // Calibre Library/metadata.db exists, we use it + + _tcscpy(base, library_dir); + PathRemoveFileSpec(base); + + _sntprintf_s(buf, BUFSIZE, _TRUNCATE, _T("%s\\*"), base); + + // Look for some other folder that contains a metadata.db file inside the Calibre Portable folder + if((hFind = FindFirstFileEx(buf, FindExInfoStandard, &fdFile, FindExSearchLimitToDirectories, NULL, 0)) + != INVALID_HANDLE_VALUE) { + do { + if(is_dots(fdFile.cFileName)) continue; + + if(fdFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { + _sntprintf_s(buf, BUFSIZE, _TRUNCATE, _T("%s\\%s\\metadata.db"), base, fdFile.cFileName); + if (PathFileExists(buf)) { + // some dir/metadata.db exists, we use it as the library + PathRemoveFileSpec(buf); + _tcscpy(library_dir, buf); + FindClose(hFind); + return; + } + } + } while(FindNextFile(hFind, &fdFile)); + FindClose(hFind); + } + +} + int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow) { LPTSTR app_dir, config_dir, exe, library_dir, too_long; @@ -147,6 +188,8 @@ int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine _sntprintf_s(exe, BUFSIZE, _TRUNCATE, _T("%sCalibre\\calibre.exe"), app_dir); _sntprintf_s(library_dir, BUFSIZE, _TRUNCATE, _T("%sCalibre Library"), app_dir); + find_calibre_library(library_dir); + if ( _tcscnlen(library_dir, BUFSIZE) <= 74 ) { launch_calibre(exe, config_dir, library_dir); } else {