Skip to content

Commit f163f46

Browse files
committed
Find config files in more locations
1 parent ce6cfb2 commit f163f46

4 files changed

Lines changed: 39 additions & 4 deletions

File tree

File renamed without changes.

main.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@
44
int main(int argc, char *argv[]) {
55

66
// determine the startup config file...
7-
const char *config_file = "default_config.cfg";
7+
const char *config_file = nullptr;
88
for (int k = 1; k < argc; k++)
99
if (std::string(argv[k]) == "-c" || std::string(argv[k]) == "--config")
1010
config_file = argv[k + 1];
1111

1212
QApplication a(argc, argv);
1313
MainWindow w(nullptr, config_file);
1414
w.show();
15-
1615
return a.exec();
1716
}

mainwindow.cpp

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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+
444479
void MainWindow::printReplacedFilename() {
445480
ui->locationLabel->setText(replaceFilename(ui->lineEdit_template->text()));
446481
}

mainwindow.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ private slots:
4747
private:
4848
QSet<QString> getCheckedStreams() const;
4949
QString replaceFilename(QString fullfile) const;
50-
// function for loading config file
50+
// function for loading / saving the config file
51+
QString find_config_file(const char *filename);
5152
void load_config(QString filename);
5253
void save_config(QString filename);
5354

0 commit comments

Comments
 (0)