-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
279 lines (242 loc) · 11.8 KB
/
main.cpp
File metadata and controls
279 lines (242 loc) · 11.8 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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#include "core/ExampleNotes.h"
#include "core/NoteManager.h"
#include "core/Settings.h"
#include "integration/DBusService.h"
#include "integration/DesktopHelper.h"
#include "integration/PortalShortcuts.h"
#include "storage/Crypto.h"
#include "ui/FirstRunDialog.h"
#include "ui/MainWindow.h"
#include "ui/TrayIcon.h"
#include <QApplication>
#include <QCommandLineParser>
#include <QCoreApplication>
#include <QCryptographicHash>
#include <QDir>
#include <QFile>
#include <QLocalServer>
#include <QLocalSocket>
#include <QLoggingCategory>
#include <QMessageBox>
#include <QStyleFactory>
#include <QSystemTrayIcon>
#include <QTextStream>
#include <QUrl>
#include <QUrlQuery>
#include <cstdlib> // for unsetenv
static const QString SOCKET_NAME = "linnote-single-instance";
int main(int argc, char *argv[]) {
// ═══════════════════════════════════════════════════════════════════════════
// Cross-Desktop Compatibility: Pre-Application Initialization
// These must be called BEFORE QApplication is created
// ═══════════════════════════════════════════════════════════════════════════
// Suppress Wayland protocol noise (zwp_text_input warnings, etc.)
QLoggingCategory::setFilterRules(QStringLiteral("qt.qpa.wayland*=false"));
// Prevent GNOME startup notification cursor hang (30s loading cursor)
// This must be done before QApplication creation
unsetenv("DESKTOP_STARTUP_ID");
QApplication app(argc, argv);
app.setApplicationName("linnote");
app.setApplicationVersion("1.0");
app.setOrganizationName("LinNote");
app.setOrganizationDomain("linnote.app");
// ═══════════════════════════════════════════════════════════════════════════
// Cross-Desktop Compatibility: Desktop File Name for GNOME Dock
// This ensures the app icon and name display correctly in GNOME's dock
// ═══════════════════════════════════════════════════════════════════════════
app.setDesktopFileName("linnote");
// ═══════════════════════════════════════════════════════════════════════════
// Cross-Desktop Compatibility: Style Hardening for GTK-based Desktops
// Prevents theme collision artifacts (popup ghosting, dropdown bugs)
// ═══════════════════════════════════════════════════════════════════════════
if (LinNote::isGtkBasedDesktop()) {
app.setStyle(QStyleFactory::create(QStringLiteral("Fusion")));
qDebug() << "LinNote: Using Fusion style for"
<< LinNote::desktopEnvironmentName() << "compatibility";
}
// === Single Instance Check ===
// Try to connect to existing instance
QLocalSocket socket;
socket.connectToServer(SOCKET_NAME);
if (socket.waitForConnected(500)) {
// Another instance is running - send activate signal
socket.write("activate");
socket.waitForBytesWritten();
socket.disconnectFromServer();
// Show notification and exit
QMessageBox::information(nullptr, "LinNote",
"LinNote is already running!\n\n"
"The existing window has been brought to front.");
return 0;
}
// Create server for this instance
QLocalServer *server = new QLocalServer(&app);
// Remove stale socket file if exists
QLocalServer::removeServer(SOCKET_NAME);
server->listen(SOCKET_NAME);
// Don't quit when last window is closed (tray icon keeps running)
app.setQuitOnLastWindowClosed(false);
// Set application icon
app.setWindowIcon(QIcon(":/app/icons/app/linnote-128.png"));
// Command line parser
QCommandLineParser parser;
parser.setApplicationDescription("A quick scratchpad for Linux");
parser.addHelpOption();
parser.addVersionOption();
// URL Scheme support
QCommandLineOption newNoteOption(QStringList() << "n" << "new",
"Create new note with text", "text");
QCommandLineOption searchOption(QStringList() << "s" << "search",
"Search notes", "query");
parser.addOption(newNoteOption);
parser.addOption(searchOption);
parser.addPositionalArgument("url", "linnote:// URL to open");
parser.process(app);
// First-run wizard
if (Settings::instance()->isFirstRun()) {
FirstRunDialog firstRunDialog;
if (firstRunDialog.exec() == QDialog::Accepted) {
// Apply preferences
Settings::instance()->setThemeName(firstRunDialog.selectedTheme());
Settings::instance()->setDarkMode(firstRunDialog.darkModeEnabled());
Settings::instance()->setBaseCurrency(firstRunDialog.selectedCurrency());
Settings::instance()->setAutoPasteEnabled(
firstRunDialog.autoPasteEnabled());
// Apply encryption settings
Settings::instance()->setEncryptionEnabled(
firstRunDialog.encryptionEnabled());
if (firstRunDialog.encryptionEnabled()) {
// Hash the password and store using Crypto::hashPassword
QString password = firstRunDialog.password();
QString hash = Crypto::hashPassword(password);
Settings::instance()->setMasterPasswordHash(hash);
Settings::instance()->setRecoveryKey(firstRunDialog.recoveryKey());
}
// Apply global hotkey (this triggers KDE shortcut registration)
Settings::instance()->setGlobalHotkey(firstRunDialog.selectedHotkey());
// Apply autostart setting
if (firstRunDialog.startOnBootEnabled()) {
QString autostartDir = QDir::homePath() + "/.config/autostart";
QString autostartPath = autostartDir + "/linnote.desktop";
QDir().mkpath(autostartDir);
QFile file(autostartPath);
if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
QTextStream out(&file);
out << "[Desktop Entry]\n";
out << "Type=Application\n";
out << "Name=LinNote\n";
out << "Comment=Linux Scratchpad\n";
out << "Exec=" << QCoreApplication::applicationFilePath() << "\n";
out << "Icon=linnote\n";
out << "Terminal=false\n";
out << "Categories=Utility;TextEditor;\n";
out << "X-GNOME-Autostart-enabled=true\n";
out << "StartupNotify=false\n";
file.close();
}
}
Settings::instance()->setFirstRunComplete();
} else {
// User closed dialog - still mark first run complete with no encryption
Settings::instance()->setFirstRunComplete();
}
}
// Create main window
MainWindow mainWindow;
// Connect single instance server to activate window
QObject::connect(server, &QLocalServer::newConnection,
[&mainWindow, server]() {
QLocalSocket *client = server->nextPendingConnection();
if (client) {
client->waitForReadyRead(500);
// Activate window
mainWindow.showAndFocus();
client->disconnectFromServer();
client->deleteLater();
}
});
// Check if this is a fresh setup - offer example notes only once
if (!Settings::instance()->examplesShown() &&
mainWindow.noteManager()->noteCount() <= 1) {
QMessageBox::StandardButton reply = QMessageBox::question(
&mainWindow, "Example Notes",
"Would you like to see example notes?\n\n"
"These examples will show you how each feature works.\n\n"
"Essential Shortcuts:\n"
"• Ctrl+N New page\n"
"• Ctrl+D Delete page\n"
"• Ctrl+Tab Next page\n"
"• Ctrl+Shift+Tab Previous page\n"
"• Ctrl+F Search\n"
"• Esc Hide window",
QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
Settings::instance()->setExamplesShown(true); // Mark as shown
if (reply == QMessageBox::Yes) {
ExampleNotes::createExamples(mainWindow.noteManager());
}
}
// ═══════════════════════════════════════════════════════════════════════════
// Cross-Desktop Compatibility: System Tray Availability Guard
// Vanilla GNOME has no system tray - requires extension or fallback to
// DockOnly
// ═══════════════════════════════════════════════════════════════════════════
TrayIcon trayIcon(&mainWindow);
Settings::DisplayMode displayMode = Settings::instance()->displayMode();
// Check if system tray is actually available
if (!QSystemTrayIcon::isSystemTrayAvailable()) {
qWarning() << "LinNote: System tray not available on"
<< LinNote::desktopEnvironmentName();
if (displayMode == Settings::TrayOnly) {
// TrayOnly mode is not possible - use DockOnly for this session
qWarning() << "LinNote: TrayOnly mode not possible, using DockOnly";
displayMode = Settings::DockOnly; // Session override, don't save
// Show one-time informative dialog (non-blocking)
QMessageBox::information(
&mainWindow, QObject::tr("System Tray Unavailable"),
QObject::tr(
"Your desktop environment doesn't have a system tray.\n\n"
"LinNote will appear in the dock/taskbar instead.\n\n"
"Tip: On GNOME, you can install the 'AppIndicator Support' "
"extension to enable system tray icons."));
}
}
// Show tray icon if available and mode requires it
if (displayMode != Settings::DockOnly &&
QSystemTrayIcon::isSystemTrayAvailable()) {
trayIcon.show();
}
// Quit behavior based on effective display mode
// If tray is shown, don't quit when last window closed
app.setQuitOnLastWindowClosed(displayMode == Settings::DockOnly);
// Register DBus service for external control
// This allows triggering via: dbus-send --session --type=method_call \
// --dest=org.linnote.LinNote /LinNote org.linnote.LinNote.Toggle
DBusService dbusService(&mainWindow);
dbusService.registerService();
// Setup global shortcuts via portal (may not work on all systems)
PortalShortcuts portalShortcuts;
QObject::connect(&portalShortcuts, &PortalShortcuts::toggleRequested,
&mainWindow, &MainWindow::toggleVisibility);
portalShortcuts.registerShortcuts();
// Handle CLI arguments
QString newNoteText = parser.value(newNoteOption);
QString searchQuery = parser.value(searchOption);
QStringList positionalArgs = parser.positionalArguments();
// Check for linnote:// URL in positional args
if (!positionalArgs.isEmpty()) {
QString urlArg = positionalArgs.first();
if (urlArg.startsWith("linnote://")) {
dbusService.OpenUrl(urlArg);
}
} else if (!newNoteText.isEmpty()) {
mainWindow.showAndFocus();
mainWindow.createNewNoteWithText(newNoteText);
} else if (!searchQuery.isEmpty()) {
mainWindow.showAndFocus();
mainWindow.searchNotes(searchQuery);
} else {
// Show window on first launch
mainWindow.showAndFocus();
}
return app.exec();
}