-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVSLapp.cpp
131 lines (107 loc) · 3.81 KB
/
VSLapp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include "VSLapp.h"
#include <QLockFile>
#include <QDir>
#include <QApplication>
#include <QSettings>
#include <QDateTime>
#include <QMessageBox>
#include <QMainWindow>
#ifdef __unix
#include <unistd.h>
#include <sys/types.h>
#endif
void VSLapp::applicationSetup(const char *organizationName)
{
// set up application name
QFileInfo applicationFile(QApplication::applicationFilePath());
// These allow us to simply construct a "QSettings" object without arguments
qApp->setOrganizationDomain("mil.army.arl");
qApp->setApplicationName(applicationFile.baseName());
qApp->setOrganizationName(organizationName);
qApp->setApplicationVersion(__DATE__ __TIME__);
// Look up the last directory where the user opened files.
// set it if it hasn't been set.
QSettings settings;
if (!settings.allKeys().contains("app/currentDirectory"))
settings.setValue("app/currentDirectory", applicationFile.absoluteDir().absolutePath());
// log this launch of the program to track usage.
logApplicationLaunch(applicationFile); // comes after settingsRead so we can set a preference
}
void VSLapp::logApplicationLaunch(QFileInfo appFile)
{
QSettings settings;
settings.value("logUsage", QVariant(true));
qApp->applicationDirPath();
// obtain the string we will log to the file
QString message =
QDateTime::currentDateTime().toString("yyyyMMddhhmmss");
#ifdef __unix
message.append(getuid());
#endif
message.append("\n");
QString nameOfLogFile = QString("%1%2").arg(appFile.absoluteFilePath()).arg(".log");
QString nameOfLockFile = QString("%1%2").arg(nameOfLogFile).arg(".lck");
QLockFile lockFile(nameOfLockFile);
if (lockFile.tryLock(1000)) {
// got the lock write the data
QFile usageLogFile(nameOfLogFile);
if (usageLogFile.open(QIODevice::Append)) {
usageLogFile.write(message.toStdString().c_str());
usageLogFile.close();
}
lockFile.unlock();
}
}
QString VSLapp::getApplicationDir()
{
QDir applicationDir(qApp->applicationDirPath());
#if defined(Q_OS_WIN)
if (applicationDir.dirName().toLower() == "debug" ||
applicationDir.dirName().toLower() == "release"
# ifdef CMAKE_INTDIR
|| applicationDir.dirName() == CMAKE_INTDIR
# endif
)
applicationDir.cdUp();
#elif defined(Q_OS_MAC)
if (applicationDir.dirName() == "MacOS") {
applicationDir.cdUp();
applicationDir.cdUp();
applicationDir.cdUp();
}
#endif
return applicationDir.absolutePath();
}
void VSLapp::showAboutDialog(QWidget *parent)
{
QMessageBox msgBox(QMessageBox::Information,
QString("About %1").arg(qApp->applicationName()),
QString("This is <b>%1</b> from <br><b>%2</b>")
.arg(qApp->applicationName())
.arg(qApp->organizationName()),
QMessageBox::Ok,
parent,
Qt::Dialog);
msgBox.setInformativeText(QString("Built %1").arg(__DATE__ " " __TIME__));
msgBox.setDetailedText(QString("Binary:%1").arg(VSLapp::getApplicationDir()));
msgBox.setDefaultButton(QMessageBox::Ok);
msgBox.exec();
}
#define MainWindowGeometry "MainWindow/geometry"
#define MainWindowDoRestore "MainWindow/restore"
#define UI_VERSION 1
void VSLapp::mainWindowSetup(QMainWindow *mw)
{
mw->setWindowTitle(qApp->applicationName());
// set the geometry
QSettings settings;
if (settings.allKeys().contains(MainWindowGeometry)) {
mw->setGeometry(settings.value(MainWindowGeometry).toRect());
}
}
void VSLapp::mainWindowSave(QMainWindow *mw)
{
// stash things that we will want on startup.
QSettings settings;
settings.setValue(MainWindowGeometry, mw->geometry());
}