-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.cpp
154 lines (132 loc) · 4.46 KB
/
main.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include "yatewindow.h"
#include <QApplication>
#include <QFile>
#include <QTextStream>
#include <QFontDatabase>
#include <QDateTime>
#include <QDebug>
#include <QMessageBox>
#include <QFile>
#include <QTimer>
#include <QSettings>
#include <QRegularExpression>
#include "globals.h"
#ifdef QT_DEBUG
QFile loggingFile;
void logMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg);
QTextStream& qStdErr()
{
static QTextStream ts(stderr);
ts.setAutoDetectUnicode(true);
ts.setEncoding(QStringConverter::Utf16);
return ts;
}
QTextStream& qStdOut()
{
static QTextStream ts(stdout);
ts.setAutoDetectUnicode(true);
ts.setEncoding(QStringConverter::Utf16);
return ts;
}
#endif
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
#ifdef QT_DEBUG
qInstallMessageHandler(logMessageHandler);
qDebug() << "YATE Launched";
QStringList args;
for(int i = 0; i < argc; i++) {
args.push_back(argv[i]);
}
qDebug() << "Version: " << QApplication::applicationVersion();
qDebug() << "Arguments: " << args.join(", ");
#endif
QString path = QDir::toNativeSeparators(qApp->applicationFilePath());
qDebug() << "Registering Custom URI Handler";
QSettings set("HKEY_CURRENT_USER\\Software\\Classes", QSettings::NativeFormat);
set.beginGroup("yate");
set.setValue("Default", "URL:YATE Protocol");
set.setValue("DefaultIcon/Default", path);
set.setValue("URL Protocol", "");
set.setValue("shell/open/command/Default", QString("\"%1\"").arg(path) + " \"%1\"");
set.endGroup();
QString codeURI;
if (argc >= 5) {
QString action = argv[1];
if (action == "update") {
//pid = argv[2]
QString version = argv[3];
for(int i = 4; i < argc; i++) {
QString oldPath = argv[i];
if(!QFile::remove(oldPath)) {
QTimer::singleShot(5000, [=](){
QFile::remove(oldPath);
});
}
}
QMessageBox::information(0, "Updated Successfully", "Successfully updated YATE to version " + version + ". To learn about the latest features visit " + "<a href='" + SETTINGS_WEBSITE_HTTPS + "'>" + SETTINGS_WEBSITE_HTTPS + "</a>");
}
} else if (argc == 2 && QString(argv[1]).startsWith("yate://")) {
codeURI = QString(argv[1]);
if (codeURI.startsWith("yate://run") || codeURI == "yate://" || codeURI == "yate" || codeURI == "yate:///") {
codeURI = "";
} else {
const QRegularExpression rx("yate\\:\\/\\/(\\w+)\\/?\\:([a-z0-9]+)");
auto match = rx.match(codeURI);
if (!match.isValid() || !match.hasMatch()) {
QMessageBox::critical(0, "Invalid URI", "Provided URI format is invalid");
} else {
codeURI = match.captured(1) + ":" + match.captured(2);
}
}
qDebug() << "Starting with URI: " << codeURI;
}
QCoreApplication::setOrganizationName(SETTINGS_ORGANIZATION);
QCoreApplication::setApplicationName(SETTINGS_APPLICATION);
QFile file(":/MaterialDark.qss");
file.open(QFile::ReadOnly | QFile::Text);
QTextStream stream(&file);
a.setStyleSheet(stream.readAll());
Yate::YATEWindow w(codeURI);
w.show();
return a.exec();
}
#ifdef QT_DEBUG
void logMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
const char *file = context.file ? context.file : "";
const char *function = context.function ? context.function : "";
QString logType = "System";
bool isStdOut = false;
switch (type) {
case QtDebugMsg:
logType = "Debug";
isStdOut = true;
break;
case QtInfoMsg:
logType = "Info";
isStdOut = true;
break;
case QtWarningMsg:
logType = "Warning";
break;
case QtCriticalMsg:
logType = "Critical";
break;
case QtFatalMsg:
logType = "Fatal";
break;
}
QString txt = QString("[%1] %2: %3 (%4:%5, %6)").arg(QDateTime::currentDateTime().toString()).arg(logType).arg(msg).arg(file).arg(context.line).arg(function);
if (isStdOut) {
qStdOut() << txt << Qt::endl;
} else {
qStdErr() << txt << Qt::endl;
}
QFile outFile("yate.log");
outFile.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text);
QTextStream ts(&outFile);
ts << txt << Qt::endl;
}
#endif