diff --git a/lib/func.cpp b/lib/func.cpp index 880040cb..2cc86656 100644 --- a/lib/func.cpp +++ b/lib/func.cpp @@ -30,6 +30,7 @@ #endif #include #include +#include #include #include #include @@ -89,9 +90,9 @@ int portable_app() QString getPrefix() { #if defined(Q_OS_WIN32) - static char inst_dir[100] = ""; + static char inst_dir[512] = ""; char *p; - ULONG dwLength = 100; + ULONG dwLength = sizeof inst_dir; HKEY hKey; if (inst_dir[0] != '\0') { @@ -115,7 +116,7 @@ QString getPrefix() XCA_WARN("Registry Key: 'HKEY_LOCAL_MACHINE\\Software\\xca' not found"); return QString(inst_dir); } - if (RegQueryValueEx(hKey, "Install_Dir", NULL, NULL, + if (RegQueryValueEx(hKey, "Install_Dir64", NULL, NULL, (unsigned char *)inst_dir, &dwLength) != ERROR_SUCCESS) { XCA_WARN("Registry Key: 'HKEY_LOCAL_MACHINE->Software->xca->Install_Dir' not found"); @@ -138,38 +139,56 @@ QString getPrefix() } -QString getHomeDir() -{ - QString hd; #if defined(Q_OS_WIN32) +static QString specialFolder(int csidl) +{ LPITEMIDLIST pidl = NULL; TCHAR buf[255] = ""; - if (SUCCEEDED(SHGetSpecialFolderLocation(NULL, CSIDL_PERSONAL, &pidl))) { + + if (SUCCEEDED(SHGetSpecialFolderLocation(NULL, csidl, &pidl))) SHGetPathFromIDList(pidl, buf); - } - hd = buf; -#else - hd = QDir::homePath(); + + return QDir::toNativeSeparators(buf); +} #endif - return hd; + +QString getHomeDir() +{ +#if defined(Q_OS_WIN32) + return portable_app() ? getPrefix() : specialFolder(CSIDL_PERSONAL); +#else + return QDir::homePath(); +#endif +} + +/* For portable APP remove leading file name if it is + * the app directory. + */ +QString relativePath(QString path) +{ + QFileInfo fi_path(path); + QFileInfo fi_home(getHomeDir()); + + QString prefix = QDir::toNativeSeparators(fi_home.canonicalFilePath()); + path = QDir::toNativeSeparators(fi_path.canonicalFilePath()); + + if (portable_app()) { + if (path.startsWith(prefix)) + path = path.mid(prefix.length()+1); + } + return path; } QString getLibDir() { - QString hd; #if defined(Q_OS_WIN32) - LPITEMIDLIST pidl = NULL; - TCHAR buf[255] = ""; - if (SUCCEEDED(SHGetSpecialFolderLocation(NULL, CSIDL_SYSTEM, &pidl))) { - SHGetPathFromIDList(pidl, buf); - } - hd = buf; + return specialFolder(CSIDL_SYSTEM); #else QString ulib = "/usr/lib/"; QString lib = "/lib/"; QString multi; + QString hd = ulib; - hd = ulib; QFile f(ulib + "pkg-config.multiarch"); if (f.open(QIODevice::ReadOnly)) { QTextStream in(&f); @@ -192,8 +211,8 @@ QString getLibDir() break; } } -#endif return QDir::toNativeSeparators(hd); +#endif } QString getDocDir() @@ -213,14 +232,8 @@ QString getUserSettingsDir() { QString rv; #if defined(Q_OS_WIN32) - LPITEMIDLIST pidl = NULL; - TCHAR buf[255] = ""; - if (SUCCEEDED(SHGetSpecialFolderLocation(NULL, CSIDL_APPDATA, &pidl))) { - SHGetPathFromIDList(pidl, buf); - } - rv = buf; - rv += QDir::separator(); - rv += "xca"; + rv = portable_app() ? getPrefix() + "/settings" : + specialFolder(CSIDL_APPDATA) + "/xca"; #elif defined(Q_OS_MAC) #if QT_VERSION < 0x050000 rv = QDesktopServices::storageLocation(QDesktopServices::DataLocation); @@ -233,11 +246,9 @@ QString getUserSettingsDir() QCoreApplication::applicationName(); #endif #else - rv = QDir::homePath(); - rv += QDir::separator(); - rv += ".xca"; + rv = QDir::homePath() + "/.xca"; #endif - return rv; + return QDir::toNativeSeparators(rv); } // Qt's open and save dialogs result in some undesirable quirks. diff --git a/lib/func.h b/lib/func.h index 34e06c4c..cfeeaa30 100644 --- a/lib/func.h +++ b/lib/func.h @@ -29,6 +29,7 @@ QString getHomeDir(); QString getLibDir(); QString getDocDir(); QString getUserSettingsDir(); +QString relativePath(QString path); QString getFullFilename(const QString &filename, const QString &selectedFilter); const QStringList getLibExtensions(); QString hostId(); diff --git a/lib/main.cpp b/lib/main.cpp index 4a5aef80..daad4d37 100644 --- a/lib/main.cpp +++ b/lib/main.cpp @@ -142,9 +142,6 @@ void XCA_application::switchLanguage(QAction* a) QLocale lang = a->data().toLocale(); setupLanguage(lang); - if (portable_app()) - return; - QFile file(defaultlang()); if (lang == QLocale::system()) { diff --git a/lib/settings.cpp b/lib/settings.cpp index 8c472068..24e122df 100644 --- a/lib/settings.cpp +++ b/lib/settings.cpp @@ -30,7 +30,8 @@ settings::settings() defaul["mandatory_dn"] = ""; defaul["explicit_dn"] = "C,ST,L,O,OU,CN,emailAddress"; defaul["string_opt"] = "MASK:0x2002"; - defaul["workingdir"] = QDir::toNativeSeparators(QDir::currentPath()); + defaul["workingdir"] = QDir::toNativeSeparators(portable_app() ? + getHomeDir() : QDir::currentPath()); defaul["default_hash"] = hashBox::getDefault(); defaul["ical_expiry"] = "1W"; defaul["cert_expiry"] = "80%"; @@ -117,7 +118,7 @@ void settings::set(QString key, QString value) if (key == "workingdir") { if (!QDir(value).exists()) return; - value = QDir::toNativeSeparators(value); + value = relativePath(value); } if (hostspecific.contains(key)) key += QString(":%1").arg(hostId()); diff --git a/widgets/MW_database.cpp b/widgets/MW_database.cpp index 7a47d439..9c898d46 100644 --- a/widgets/MW_database.cpp +++ b/widgets/MW_database.cpp @@ -463,11 +463,7 @@ int MainWindow::open_default_db() void MainWindow::default_database() { - if (portable_app()) - return; - QFile file(defaultdb()); - QFileInfo fi(currentDB); if (currentDB.isEmpty()) { file.remove(); @@ -479,7 +475,7 @@ void MainWindow::default_database() if (OpenDb::isRemoteDB(currentDB)) ba = filename2bytearray(currentDB); else - ba = filename2bytearray(fi.canonicalFilePath()); + ba = filename2bytearray(relativePath(currentDB)); ba += '\n'; file.write(ba); /* write() failed? Harmless. Only inconvenient */ @@ -585,6 +581,9 @@ void MainWindow::update_history(QString fname) QFile file; int pos; + if (!OpenDb::isRemoteDB(fname)) + fname = relativePath(fname); + pos = history.indexOf(fname); if (pos == 0) return; /* no changes */ @@ -597,9 +596,6 @@ void MainWindow::update_history(QString fname) update_history_menu(); - if (portable_app()) - return; - file.setFileName(dbhistory()); if (!file.open(QIODevice::ReadWrite)) return; diff --git a/widgets/MW_menu.cpp b/widgets/MW_menu.cpp index 90eb52b7..18283f36 100644 --- a/widgets/MW_menu.cpp +++ b/widgets/MW_menu.cpp @@ -100,10 +100,8 @@ void MainWindow::init_menu() this, SLOT(openRemoteSqlDB())) ->setEnabled(OpenDb::hasRemoteDrivers()); file->addMenu(historyMenu); - if (!portable_app()) { - file->addAction(tr("Set as default DataBase"), this, + file->addAction(tr("Set as default DataBase"), this, SLOT(default_database())); - } acList += file->addAction(tr("Close DataBase"), this, SLOT(close_database()), QKeySequence::Close);