-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
255 lines (221 loc) · 8.5 KB
/
Copy pathmain.cpp
File metadata and controls
255 lines (221 loc) · 8.5 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#include "autostartmanager.h"
#include "helper.h"
#include "tooltipmanager.h"
#include "src/panchangacalculator.h"
#include "src/waylandhelper.h"
#include <QApplication>
#include <QCommandLineParser>
#include <QDir>
#include <QFile>
#include <QIcon>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QQuickWindow>
#include <QStandardPaths>
#include <QStyleHints>
#include <QTextStream>
#include <QtCore>
#include <QLibrary>
void ensureDesktopFile(const QString &desktopFileName,
const QString &startupWMClass) {
QString dirPath =
QStandardPaths::writableLocation(QStandardPaths::ApplicationsLocation);
QDir().mkpath(dirPath);
// Hack to show calendar icon for calendar window
QString desktopFilePath = dirPath + "/" + desktopFileName;
QFile desktopFile(desktopFilePath);
if (desktopFile.exists()) {
if (desktopFile.open(QIODevice::ReadWrite | QIODevice::Text)) {
QString content = desktopFile.readAll();
desktopFile.resize(0); // clear contents
QTextStream out(&desktopFile);
QString updatedContent;
for (const QString &line : content.split('\n')) {
if (line.startsWith("Exec=")) {
updatedContent +=
"Exec=\"" + QCoreApplication::applicationFilePath() + "\"\n";
} else {
updatedContent += line + "\n";
}
}
out << updatedContent.trimmed();
desktopFile.close();
} else {
qWarning() << "Couldn't update desktop file:" << desktopFilePath;
}
} else {
if (desktopFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
QTextStream out(&desktopFile);
out << "[Desktop Entry]\n";
out << "Categories=Utility;Calendar;\n";
out << "Comment=Nepali Calendar Application\n";
out << "Icon=calendar\n";
out << "Exec=\"" << QCoreApplication::applicationFilePath() << "\"\n";
out << "Name=Nepdate Calendar\n";
out << "StartupNotify=true\n";
out << "StartupWMClass=" << startupWMClass << "\n";
out << "Terminal=false\n";
out << "Type=Application\n";
out << "NoDisplay=true\n";
desktopFile.close();
} else {
qWarning() << "Couldn't create desktop file:" << desktopFilePath;
}
}
}
QString readStringFromResource(const QString &resourcePath) {
QFile file(resourcePath);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
qDebug() << "Could not open resource file:" << resourcePath;
return QString();
}
QTextStream in(&file);
return in.readLine().trimmed();
}
bool checkX11Compositor() {
QLibrary x11Lib("X11");
if (!x11Lib.load()) {
x11Lib.setFileName("libX11.so.6");
if (!x11Lib.load()) {
x11Lib.setFileName("libX11.so");
if (!x11Lib.load()) {
return false;
}
}
}
typedef void* (*XOpenDisplay_fn)(const char*);
typedef int (*DefaultScreen_fn)(void*);
typedef unsigned long (*XInternAtom_fn)(void*, const char*, int);
typedef unsigned long (*XGetSelectionOwner_fn)(void*, unsigned long);
typedef int (*XCloseDisplay_fn)(void*);
auto xOpenDisplay = (XOpenDisplay_fn)x11Lib.resolve("XOpenDisplay");
auto defaultScreen = (DefaultScreen_fn)x11Lib.resolve("DefaultScreen");
auto xInternAtom = (XInternAtom_fn)x11Lib.resolve("XInternAtom");
auto xGetSelectionOwner = (XGetSelectionOwner_fn)x11Lib.resolve("XGetSelectionOwner");
auto xCloseDisplay = (XCloseDisplay_fn)x11Lib.resolve("XCloseDisplay");
if (!xOpenDisplay || !defaultScreen || !xInternAtom || !xGetSelectionOwner || !xCloseDisplay) {
return false;
}
void* display = xOpenDisplay(nullptr);
if (!display) {
return false;
}
int screen = defaultScreen(display);
QString atomName = QString("_NET_WM_CM_S%1").arg(screen);
unsigned long managerAtom = xInternAtom(display, atomName.toUtf8().constData(), 1); // True means only if exists
bool hasCompositor = false;
if (managerAtom != 0) { // None in X11 is 0
unsigned long owner = xGetSelectionOwner(display, managerAtom);
if (owner != 0) {
hasCompositor = true;
}
}
xCloseDisplay(display);
return hasCompositor;
}
bool isCompositorInProc() {
QDir procDir("/proc");
QStringList filters;
filters << "[0-9]*";
procDir.setNameFilters(filters);
procDir.setFilter(QDir::Dirs);
for (const QString &pidStr : procDir.entryList()) {
QFile commFile(QString("/proc/%1/comm").arg(pidStr));
if (commFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
QString name = commFile.readAll().trimmed();
commFile.close();
if (name == "kwin_x11" || name == "kwin" || name == "picom" ||
name == "compton" || name == "mutter" || name == "gnome-shell" ||
name == "muffin" || name == "cinnamon" || name == "xfwm4" ||
name == "compiz" || name == "deepin-wm" || name == "deepin-kwin" ||
name == "dde-kwin") {
return true;
}
}
}
return false;
}
bool checkCompositorSupportsTransparency() {
QString platform = QGuiApplication::platformName();
if (platform == "wayland" || platform == "wayland-egl") {
return true;
}
if (platform == "cocoa" || platform == "windows") {
return true;
}
if (platform == "xcb" || platform == "dxcb") {
return checkX11Compositor() || isCompositorInProc();
}
return true; // Default fallback
}
int main(int argc, char *argv[]) {
QCoreApplication::setOrganizationName("Nepdate");
QCoreApplication::setOrganizationDomain("com.nepdate.calendar");
QCoreApplication::setApplicationName("NepaliCalendar");
QQuickWindow::setDefaultAlphaBuffer(true);
QApplication app(argc, argv);
app.setWindowIcon(QIcon(":/resources/flag.png"));
app.setDesktopFileName("nepdate-calendar");
// Ensure .desktop file is created or Exec= line updated
ensureDesktopFile("nepdate-calendar.desktop", "NepaliCalendar");
AutostartManager autostartManager;
TooltipManager tooltipManager;
// Command-line parsing must happen before QML engine load so --help works quickly
QCommandLineParser parser;
parser.setApplicationDescription("Nepali Calendar Application");
parser.addHelpOption();
parser.addVersionOption(); // --version and -v
parser.process(app);
QQmlApplicationEngine engine;
// C++ Platform Detection
// Get the platform name (e.g., "xcb" or "wayland").
QString platform = QGuiApplication::platformName();
engine.rootContext()->setContextProperty("platformName", platform);
bool compositorSupportsTransparency = checkCompositorSupportsTransparency();
engine.rootContext()->setContextProperty("compositorSupportsTransparency", compositorSupportsTransparency);
// Pass Version and Build Info to QML
QString baseVersion = readStringFromResource(":/resources/version.conf");
QString buildInfo = readStringFromResource(":/resources/build_info.conf");
engine.rootContext()->setContextProperty("appVersion", baseVersion);
engine.rootContext()->setContextProperty("appBuildInfo", buildInfo);
// Register the Printer class as a QML singleton
qmlRegisterSingletonType<Printer>(
"com.calendar.printer", 1, 0, "Printer",
[](QQmlEngine *engine, QJSEngine *scriptEngine) -> QObject * {
Q_UNUSED(engine)
Q_UNUSED(scriptEngine)
return new Printer();
});
engine.rootContext()->setContextProperty("autostartManager",
&autostartManager);
engine.rootContext()->setContextProperty("tooltipManager", &tooltipManager);
PanchangaCalculator panchangaCalculator;
engine.rootContext()->setContextProperty("PanchangaNative", &panchangaCalculator);
WaylandHelper waylandHelper;
engine.rootContext()->setContextProperty("WaylandHelper", &waylandHelper);
const QUrl url(QStringLiteral("qrc:/qml/widget.qml"));
QObject::connect(
&engine, &QQmlApplicationEngine::objectCreated, &app,
[url](QObject *obj, const QUrl &objUrl) {
if (!obj && url == objUrl)
QCoreApplication::exit(-1);
},
Qt::QueuedConnection);
engine.load(url);
// Set translucent background on the widget window for platforms (like Deepin's
// dxcb) that require the widget attribute to be set from C++.
for (QObject *obj : engine.rootObjects()) {
QQuickWindow *window = qobject_cast<QQuickWindow *>(obj);
if (window) {
window->setColor(Qt::transparent);
}
}
// Tooltip style. dark light mode detection removed for compatibility with
// smaller Qt6 versions.
app.setStyleSheet("QToolTip {"
" color: #000000;"
" background-color: #ffffff;"
"}");
QCoreApplication::setApplicationVersion(baseVersion);
return app.exec();
}