@@ -69,7 +69,8 @@ MainWindow::MainWindow(QWidget *parent, const char *config_file)
6969 }
7070 });
7171
72- load_config (config_file);
72+ QString cfgfilepath = find_config_file (config_file);
73+ load_config (cfgfilepath);
7374
7475 timer = std::make_unique<QTimer>(this );
7576 connect (&*timer, &QTimer::timeout, this , &MainWindow::statusUpdate);
@@ -441,6 +442,40 @@ QString MainWindow::replaceFilename(QString fullfile) const {
441442 return fullfile;
442443}
443444
445+ /* *
446+ * Find a config file to load. This is (in descending order or preference):
447+ * - a file supplied on the command line
448+ * - [executablename].cfg in one the the following folders:
449+ * - the current working directory
450+ * - the default config folder, e.g. '~/Library/Preferences' on OS X
451+ * - the executable folder
452+ * @param filename Optional file name supplied e.g. as command line parameter
453+ * @return Path to a found config file
454+ */
455+ QString MainWindow::find_config_file (const char *filename) {
456+ if (filename) {
457+ QString qfilename (filename);
458+ if (QFileInfo::exists (qfilename))
459+ QMessageBox (QMessageBox::Warning, " Config file not found" ,
460+ QStringLiteral (" The file '%1' doesn't exist" ).arg (qfilename), QMessageBox::Ok,
461+ this );
462+ else
463+ return qfilename;
464+ }
465+ QFileInfo exeInfo (QCoreApplication::applicationFilePath ());
466+ QString defaultCfgFilename (exeInfo.completeBaseName () + " .cfg" );
467+ QStringList cfgpaths;
468+ cfgpaths << QDir::currentPath ()
469+ << QStandardPaths::standardLocations (QStandardPaths::ConfigLocation) << exeInfo.path ();
470+ for (auto path : cfgpaths) {
471+ QString cfgfilepath = path + QDir::separator () + defaultCfgFilename;
472+ if (QFileInfo::exists (cfgfilepath)) return cfgfilepath;
473+ }
474+ QMessageBox (QMessageBox::Warning, " No config file not found" ,
475+ QStringLiteral (" No default config file could be found" ), QMessageBox::Ok, this );
476+ return " " ;
477+ }
478+
444479void MainWindow::printReplacedFilename () {
445480 ui->locationLabel ->setText (replaceFilename (ui->lineEdit_template ->text ()));
446481}
0 commit comments