Rabbit Remote Control 0.1.0-bate5
Loading...
Searching...
No Matches
main.cpp
1// Author: Kang Lin <kl222@126.com>
2
16#include <QLoggingCategory>
17#include <QApplication>
18#include <QSettings>
19#include <QDebug>
20#include <QtGlobal>
21#include <QSharedPointer>
22#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
23 #include <QRegularExpression>
24 #include <QRandomGenerator>
25#endif
26#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
27 #include <QRandomGenerator>
28#endif
29#if defined(Q_OS_ANDROID) && (QT_VERSION >= QT_VERSION_CHECK(5, 7, 0)) && (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
30#include <QtAndroid>
31#endif
32
33#include "RabbitCommonTools.h"
34
35#ifdef HAVE_UPDATE
36#include "FrmUpdater.h"
37#endif
38#ifdef BUILD_QUIWidget
39 #include "QUIWidget/QUIWidget.h"
40#endif
41
42#include "StatsAppUsage.h"
43#include "mainwindow.h"
44
45static Q_LOGGING_CATEGORY(log, "App.Main")
46
47int main(int argc, char *argv[])
48{
49 int nRet = 0;
50
51 QCoreApplication::setAttribute(Qt::AA_ShareOpenGLContexts);
52
53 /* 移到 snapcraft.yaml 和 io.github.KangLin.RabbitRemoteControl.yml
54 // 修复 qtwebengine 沙箱权限问题
55 if(!qEnvironmentVariable("SNAP").isEmpty()) {
56 qputenv("QTWEBENGINE_DISABLE_SANDBOX", "1");
57 }//*/
58
59 //qputenv("QT_MEDIA_BACKEND", "ffmpeg");
60
61#if (defined(Q_OS_LINUX) && !defined(Q_OS_ANDROID) \
62 && (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)))
63 /* 修复使用 Qt6 时,最大化时,工具栏位置错误。
64 现在很多 linux 用 wayland 或 wayland-egl 作为桌面显示,这样会出现一个问题,
65 由于没有坐标系统,导致无边框窗体无法拖动和定位(即 QWidge::move 失效)。
66 (Qt6 开始强制默认优先用 wayland ,Qt5 默认有 xcb 则优先用 xcb),
67 所以需要在 main 函数最前面加一行 `qputenv("QT_QPA_PLATFORM", "xcb")`;
68 */
69 QString szPlatform = qEnvironmentVariable("QT_QPA_PLATFORM");
70 if(szPlatform.isEmpty()
71 || -1 != szPlatform.indexOf(QRegularExpression("wayland.*")))
72 qputenv("QT_QPA_PLATFORM", "xcb");
73 qInfo(log) << "QT_QPA_PLATFORM:" << szPlatform << "\nCurrent Qt Platform is:"
74 << qEnvironmentVariable("QT_QPA_PLATFORM")
75 << "; This can be modified with the environment variables QT_QPA_PLATFORM:\n"
76 << "export QT_QPA_PLATFORM=xcb\n Optional: xcb; vnc";
77#endif
78#if (QT_VERSION > QT_VERSION_CHECK(5,6,0)) && (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
79 QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
80#endif
81#if defined(Q_OS_ANDROID) && (QT_VERSION >= QT_VERSION_CHECK(5, 7, 0)) && (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
82 QtAndroid::hideSplashScreen();
83#endif
84
85//#if defined (_DEBUG) || !defined(BUILD_SHARED_LIBS)
86// Q_INIT_RESOURCE(translations_RabbitRemoteControlApp);
87//#endif
88
89 QApplication::setApplicationVersion(RabbitRemoteControl_VERSION);
90 QApplication::setApplicationName("RabbitRemoteControl");
91#if QT_VERSION >= QT_VERSION_CHECK(5, 7, 0)
92 QApplication::setDesktopFileName(QLatin1String("io.github.KangLin.RabbitRemoteControl.desktop"));
93#endif
94
95 QApplication app(argc, argv);
96
97 RabbitCommon::CTools::Instance()->Init();
98
99 qInfo(log) << app.applicationName() + " " + app.applicationVersion()
100 + " " + QObject::tr("Start") + " ......"
101 << "\n" << app.arguments();
102
103 QSharedPointer<QTranslator> tApp =
104 RabbitCommon::CTools::Instance()->InstallTranslator("RabbitRemoteControlApp");
105
106 app.setApplicationDisplayName(QObject::tr("Rabbit Remote Control"));
107 app.setOrganizationName(QObject::tr("Kang Lin Studio"));
108
109#ifdef HAVE_UPDATE
110 QSharedPointer<CFrmUpdater> pUpdate;
111 // Check update version
112 if(qEnvironmentVariable("SNAP").isEmpty()
113 && qEnvironmentVariable("FLATPAK_ID").isEmpty()) {
114 pUpdate = QSharedPointer<CFrmUpdater>(new CFrmUpdater());
115 if(pUpdate) {
116 QIcon icon = QIcon::fromTheme("app");
117 if(!icon.isNull())
118 {
119 auto sizeList = icon.availableSizes();
120 if(!sizeList.isEmpty()){
121 QPixmap p = icon.pixmap(*sizeList.begin());
122 pUpdate->SetTitle(p.toImage());
123 }
124 }
125 if(app.arguments().length() > 1) {
126 try{
127 pUpdate->GenerateUpdateJson();
128 pUpdate->GenerateUpdateXml();
129 } catch(...) {
130 qCritical(log) << "Generate update fail";
131 }
132 qInfo(log) << app.applicationName() + " " + app.applicationVersion()
133 + " " + QObject::tr("Generate update json file End");
134 return 0;
135 }
136 } else {
137 qCritical(log) << "new CFrmUpdater() fail";
138 }
139 }
140#endif
141
142 CStatsAppUsage* pStats = nullptr;
143 int randomInt = 0;
144 int nMin = 10;
145 int nMax = 50;
146#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
147 randomInt = QRandomGenerator::global()->bounded(nMin, nMax);
148#else
149 qsrand(static_cast<uint>(QTime::currentTime().msec())); // 使用当前时间的毫秒数来设置种子
150 randomInt = nMin + qrand() % (nMax - nMin + 1); // 生成一个0到99之间的随机数
151#endif
152 QTimer::singleShot(randomInt, [&](){
153 pStats = new CStatsAppUsage("v" + QApplication::applicationVersion());
154 app.processEvents();
155 });
156
157 MainWindow* w = new MainWindow();
158
159 try {
160 //w->setWindowIcon(QIcon::themeName("app"));
161 //w->setWindowTitle(app.applicationDisplayName());
162
163 w->show();
164 app.processEvents();
165 // For time-consuming operations
166 w->slotInitial();
167 nRet = app.exec();
168 } catch (std::exception &e) {
169 qCritical(log) << "exception:" << e.what();
170 } catch(...) {
171 qCritical(log) << "exception:";
172 }
173
174 delete w;
175 if(pStats) {
176 pStats->Stop();
177 delete pStats;
178 }
179
180 RabbitCommon::CTools::Instance()->Clean();
181 if(tApp)
182 RabbitCommon::CTools::Instance()->RemoveTranslator(tApp);
183//#if defined (_DEBUG) || !defined(BUILD_SHARED_LIBS)
184// Q_CLEANUP_RESOURCE(translations_RabbitRemoteControlApp);
185//#endif
186
187 qInfo(log) << app.applicationName() + " " + app.applicationVersion() + " " + "End";
188 return nRet;
189}
The MainWindow class.
Definition mainwindow.h:35
void slotInitial()
For time-consuming operations.