-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
137 lines (120 loc) · 3.98 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
#include <QStandardPaths>
#include <QApplication>
#include <QObject>
#include <QScreen>
#include <fcntl.h>
#include <syslog.h>
#include <QDebug>
#include <QFile>
#include <QMutex>
#include <QDateTime>
#include <sys/inotify.h>
#include <QTranslator>
#include <QLocale>
#include <QStandardPaths>
#include <QLibraryInfo>
#include <QDir>
#include "popwindow.h"
#include "deviceMonitor.h"
#include <QFileSystemWatcher>
#include <QThread>
#include <QLabel>
#include <QMetaType>
void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
// 加锁
static QMutex mutex;
mutex.lock();
QByteArray localMsg = msg.toLocal8Bit();
QString strMsg("");
switch (type)
{
case QtDebugMsg:
default:
strMsg = QString("Debug:");
break;
case QtWarningMsg:
strMsg = QString("Warning:");
break;
case QtCriticalMsg:
strMsg = QString("Critical:");
break;
case QtFatalMsg:
strMsg = QString("Fatal:");
break;
}
// 设置输出信息格式
QString strDateTime = QDateTime::currentDateTime().toString("yyyy-MM-dd ddd");
QString strMessage = QString(QString() +
"Message:%1"
// +" File:%2\n"
// +" Line:%3\n"
// +" Function:%4\n"
// +" DateTime:%5"
)
.arg(localMsg.constData());
// .arg(context.file).arg(context.line).arg(context.function).arg(strDateTime);
// 输出信息至文件中(读写、追加形式)
QString dirPath = "/tmp/kylin-printer/log";
QDir dir;
QFile file;
if (dir.mkpath(dirPath)) {
dirPath = dirPath + "/" + strDateTime + ".txt" ;
file.setFileName(dirPath);
}
file.open(QIODevice::ReadWrite | QIODevice::Append);
QTextStream stream(&file);
stream << strMessage << "\r\n";
file.flush();
file.close();
// 解锁
mutex.unlock();
}
int main(int argc, char *argv[])
{
printf("Program start ...\n");
qRegisterMetaType<DeviceInformation>("DeviceInformation");
qRegisterMetaType<DeviceInformation>("DeviceInformation&");
qInstallMessageHandler(myMessageOutput);
//高清屏幕自适应,适配990高DPI
#if (QT_VERSION >= QT_VERSION_CHECK(5, 12, 0))
QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
#endif
#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
QApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::PassThrough);
#endif
QApplication app(argc, argv);
QStringList homePath = QStandardPaths::standardLocations(QStandardPaths::HomeLocation);
int fd = open(QString(homePath.at(0) + "/.config/kylin-printer%1.lock").arg(getenv("DISPLAY")).toUtf8().data(),
O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
if (fd < 0)
{
exit(1);
}
if (lockf(fd, F_TLOCK, 0))
{
syslog(LOG_ERR, "Can't lock single file, kylin-printer is already running!");
exit(0);
}
QString trans_path;
if (QDir("/usr/share/kylin-recorder/translations").exists()) {
trans_path = "/usr/share/kylin-recorder/translations";
}
else {
trans_path = qApp->applicationDirPath() + "/translations";
}
QTranslator qt_trans;
QString locale = QLocale::system().name();
QString qt_trans_path;
qt_trans_path = QLibraryInfo::location(QLibraryInfo::TranslationsPath);// /usr/share/qt5/translations
if (locale == "zh_CN") {
if(!qt_trans.load("qt_" + locale + ".qm", qt_trans_path))
qDebug() << "Load translation file:"<< "qt_" + locale + ".qm from" << qt_trans_path << "failed!";
else
app.installTranslator(&qt_trans);
}
PopWindow popWid;
//qDebug()<<DeviceMonitor::getAllPrinterConnected();
return app.exec();
}