diff --git a/sources/main.cpp b/sources/main.cpp index b0f32a237..efa0b46b4 100644 --- a/sources/main.cpp +++ b/sources/main.cpp @@ -91,10 +91,6 @@ int main(int argc, char **argv) // export environment-variable "QT_HASH_SEED" with value "0" to // disable radomisation for hashes in order to obtain "clean" XML-diffs: qputenv("QT_HASH_SEED", "0"); - //Some setup, notably to use with QSetting. - QCoreApplication::setOrganizationName("QElectroTech"); - QCoreApplication::setOrganizationDomain("qelectrotech.org"); - QCoreApplication::setApplicationName("QElectroTech"); // Refuse invalid data when building QDom documents instead of // serializing malformed XML (CVE-2026-15037). This is the default @@ -173,6 +169,65 @@ QGuiApplication::setHighDpiScaleFactorRoundingPolicy(QetSettings::hdpiScaleFacto return 0; } +/******************************************************************************/ +#ifdef Q_OS_WINDOWS + // install-version of QET normally resides in "C:\Program Files\QElectroTech" + // "normal" users are usually not allowed to create files or directories + // there, so if we are not allowed to write there, it is most likely that + // QET is installed. ReadyToUse-versions should reside in other directory + // like C:\Tools\QElectroTech, where it is allowed to create sub-dirs. + // Check if up-dir of QET-binary is writable for us: + const QFileInfo fi(QDir::cleanPath(QCoreApplication::applicationDirPath() % "/../")); + const bool pathIsWritable(fi.isDir() && fi.isWritable()); + bool useRegistry(false); + // set name of path for configuration + QString confPath = QDir::cleanPath(QCoreApplication::applicationDirPath() % "/../conf/"); + // check registry, if QET is already using it + QSettings regCheck("HKEY_CURRENT_USER\\Software\\" % QCoreApplication::organizationName(), + QSettings::NativeFormat); + if (regCheck.childGroups().contains(QCoreApplication::applicationName())) { + // registry-key already exists? --> use registry! + useRegistry = true; + } else if (QDir(confPath).exists() && (pathIsWritable == true)) { + // config-dir already exists? --> use it! + useRegistry = false; + } else if (pathIsWritable == true) { + // only ask user what to do, if we are allowed to create conf-dir + QMessageBox::StandardButton configReply; + configReply = QMessageBox::question(NULL, + QObject::tr("QElectroTech-Config", "title of QMessageBox: Choose location of configuration"), + QObject::tr("Save the configuration to registry?\n\nYes -> use Windows-Registry (default)\nNo -> use INI-file in conf-dir"), + QMessageBox::Yes | QMessageBox::No, + QMessageBox::Yes); + if (configReply == QMessageBox::Yes) { + // Config in Registry: + useRegistry = true; + } else { + // Config in User-Dir: + useRegistry = false; + } + } else { + // fallback + useRegistry = true; + } +#endif + //Some setup, notably to use with QSetting ... with every system! + QCoreApplication::setOrganizationName("QElectroTech"); + QCoreApplication::setOrganizationDomain("qelectrotech.org"); + QCoreApplication::setApplicationName("QElectroTech"); +#ifdef Q_OS_WINDOWS + // set chosen way of saving config for win: + if (useRegistry == true) { + QSettings::setDefaultFormat(QSettings::NativeFormat); + qInfo() << "using Windows-Registry to save configuration"; + } else { + QSettings::setPath(QSettings::IniFormat, QSettings::UserScope, confPath); + QSettings::setDefaultFormat(QSettings::IniFormat); + qInfo() << "using directory" << confPath << "to save configuration"; + } +#endif +/******************************************************************************/ + QETApp qetapp; QETApp::instance()->installEventFilter(&qetapp); #ifdef Q_OS_MACOS