comparison src/main.cpp @ 129:f6685c2eb75d

Store SQLite database and log file in application data directory instead of the executable directory itself.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 23 Aug 2017 14:11:27 +0300
parents d77779789e76
children 10c6bd84eb32
comparison
equal deleted inserted replaced
128:d77779789e76 129:f6685c2eb75d
10 #include <QMessageBox> 10 #include <QMessageBox>
11 #include <QSettings> 11 #include <QSettings>
12 #include <QPrintDialog> 12 #include <QPrintDialog>
13 #include <QPrintPreviewDialog> 13 #include <QPrintPreviewDialog>
14 #include <QProgressDialog> 14 #include <QProgressDialog>
15 #include <QStandardPaths>
15 #include "main.h" 16 #include "main.h"
16 #include "ui_mainwindow.h" 17 #include "ui_mainwindow.h"
17 #include "ui_editperson.h" 18 #include "ui_editperson.h"
18 #include "ui_aboutwindow.h" 19 #include "ui_aboutwindow.h"
19 20
20 21
21 // Global UI scale factor 22 double setScale; // Global UI scale factor
22 double setScale; 23 QString appDataPath; // Application data path/directory
23 24
24 25
25 // 26 //
26 // Convert QString to a double value, replacing comma 27 // Convert QString to a double value, replacing comma
27 // 28 //
75 // 76 //
76 // Error logging 77 // Error logging
77 // 78 //
78 void slLog(QString msg) 79 void slLog(QString msg)
79 { 80 {
80 QString filename = qApp->applicationDirPath() + QDir::separator() + APP_LOG_FILE; 81 QString filename = appDataPath + QDir::separator() + APP_LOG_FILE;
81 QFile fh(filename); 82 QFile fh(filename);
82 if (fh.open(QIODevice::WriteOnly | QIODevice::Append)) 83 if (fh.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text))
83 { 84 {
84 QTextStream out(&fh); 85 QTextStream out(&fh);
85 out << 86 out <<
86 slDateTimeToLocal(QDateTime::currentDateTimeUtc()).toString(QStringLiteral("yyyy-MM-dd hh:mm:ss")) 87 slDateTimeToLocal(QDateTime::currentDateTimeUtc()).toString(QStringLiteral("yyyy-MM-dd hh:mm:ss"))
87 << " : " << msg << "\n"; 88 << " : " << msg << "\n";
223 int main(int argc, char *argv[]) 224 int main(int argc, char *argv[])
224 { 225 {
225 QApplication sapp(argc, argv); 226 QApplication sapp(argc, argv);
226 227
227 // 228 //
229 // Create logfile and data directory
230 //
231 appDataPath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
232 QDir path(appDataPath);
233 if (!path.exists(appDataPath))
234 path.mkpath(appDataPath);
235
236 //
228 // Initialize / open SQL database connection 237 // Initialize / open SQL database connection
229 // 238 //
230 QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); 239 QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
231 db.setDatabaseName(qApp->applicationDirPath() + 240 db.setDatabaseName(appDataPath + QDir::separator() + APP_SQLITE_FILE);
232 QDir::separator() + APP_SQLITE_FILE);
233 241
234 if (!db.open()) 242 if (!db.open())
235 { 243 {
236 slErrorMsg( 244 slErrorMsg(
237 QObject::tr("Tietokantaa ei voitu avata"), 245 QObject::tr("Tietokantaa ei voitu avata"),