Changeset View
Changeset View
Standalone View
Standalone View
krunner/main.cpp
Show All 36 Lines | |||||
37 | 37 | | |||
38 | #include <kworkspace.h> | 38 | #include <kworkspace.h> | ||
39 | 39 | | |||
40 | #include "view.h" | 40 | #include "view.h" | ||
41 | 41 | | |||
42 | int main(int argc, char **argv) | 42 | int main(int argc, char **argv) | ||
43 | { | 43 | { | ||
44 | qunsetenv("QT_DEVICE_PIXEL_RATIO"); | 44 | qunsetenv("QT_DEVICE_PIXEL_RATIO"); | ||
45 | QCommandLineParser parser; | ||||
45 | QCoreApplication::setAttribute(Qt::AA_DisableHighDpiScaling); | 46 | QCoreApplication::setAttribute(Qt::AA_DisableHighDpiScaling); | ||
46 | 47 | | |||
47 | const bool qpaVariable = qEnvironmentVariableIsSet("QT_QPA_PLATFORM"); | 48 | const bool qpaVariable = qEnvironmentVariableIsSet("QT_QPA_PLATFORM"); | ||
48 | KWorkSpace::detectPlatform(argc, argv); | 49 | KWorkSpace::detectPlatform(argc, argv); | ||
49 | QQuickWindow::setDefaultAlphaBuffer(true); | 50 | QQuickWindow::setDefaultAlphaBuffer(true); | ||
50 | QApplication app(argc, argv); | 51 | QApplication app(argc, argv); | ||
51 | if (!qpaVariable) { | 52 | if (!qpaVariable) { | ||
52 | // don't leak the env variable to processes we start | 53 | // don't leak the env variable to processes we start | ||
Show All 10 Lines | 63 | KAboutData aboutData(QStringLiteral("krunner"), | |||
63 | i18n("krunner"), | 64 | i18n("krunner"), | ||
64 | QStringLiteral(PROJECT_VERSION), | 65 | QStringLiteral(PROJECT_VERSION), | ||
65 | i18n("Run Command interface"), | 66 | i18n("Run Command interface"), | ||
66 | KAboutLicense::GPL); | 67 | KAboutLicense::GPL); | ||
67 | 68 | | |||
68 | KAboutData::setApplicationData(aboutData); | 69 | KAboutData::setApplicationData(aboutData); | ||
69 | app.setQuitOnLastWindowClosed(false); | 70 | app.setQuitOnLastWindowClosed(false); | ||
70 | 71 | | |||
71 | { | 72 | QCommandLineOption clipboardOption({QStringLiteral("c"), QStringLiteral("clipboard")}, | ||
broulik: Use initializer list `{QStringLiteral("c"), QStringLiteral("clipboard")}` | |||||
72 | QCommandLineParser parser; | 73 | i18n("Use the clipboard contents as query for KRunner")); | ||
74 | QCommandLineOption daemonOption({QStringLiteral("d"), QStringLiteral("daemon")}, | ||||
75 | i18n("Start KRunner in the background, don't show it.")); | ||||
73 | QCommandLineOption replaceOption({QStringLiteral("replace")}, i18n("Replace an existing instance")); | 76 | QCommandLineOption replaceOption({QStringLiteral("replace")}, i18n("Replace an existing instance")); | ||
77 | | ||||
78 | parser.addOption(clipboardOption); | ||||
broulik: Why no `i18n()` here?
Also, `QStringLiteral("query")` | |||||
79 | parser.addOption(daemonOption); | ||||
74 | parser.addOption(replaceOption); | 80 | parser.addOption(replaceOption); | ||
81 | parser.addPositionalArgument(QStringLiteral("query"), i18n("The query to run, only used if -c is not provided")); | ||||
82 | | ||||
75 | aboutData.setupCommandLine(&parser); | 83 | aboutData.setupCommandLine(&parser); | ||
76 | 84 | | |||
77 | parser.process(app); | 85 | parser.process(app); | ||
78 | aboutData.processCommandLine(&parser); | 86 | aboutData.processCommandLine(&parser); | ||
87 | | ||||
79 | if (parser.isSet(replaceOption)) { | 88 | if (parser.isSet(replaceOption)) { | ||
80 | auto message = QDBusMessage::createMethodCall(QStringLiteral("org.kde.krunner"), | 89 | auto message = QDBusMessage::createMethodCall(QStringLiteral("org.kde.krunner"), | ||
81 | QStringLiteral("/MainApplication"), | 90 | QStringLiteral("/MainApplication"), | ||
82 | QStringLiteral("org.qtproject.Qt.QCoreApplication"), | 91 | QStringLiteral("org.qtproject.Qt.QCoreApplication"), | ||
83 | QStringLiteral("quit")); | 92 | QStringLiteral("quit")); | ||
84 | QDBusConnection::sessionBus().call(message); //deliberately block until it's done, so we register the name after the app quits | 93 | QDBusConnection::sessionBus().call(message); //deliberately block until it's done, so we register the name after the app quits | ||
85 | } | 94 | } | ||
86 | } | | |||
87 | 95 | | |||
88 | if (!KAuthorized::authorize(QStringLiteral("run_command"))) { | 96 | if (!KAuthorized::authorize(QStringLiteral("run_command"))) { | ||
89 | return -1; | 97 | return -1; | ||
90 | } | 98 | } | ||
91 | 99 | | |||
92 | KDBusService service(KDBusService::Unique); | 100 | KDBusService service(KDBusService::Unique); | ||
93 | 101 | | |||
94 | QGuiApplication::setFallbackSessionManagementEnabled(false); | 102 | QGuiApplication::setFallbackSessionManagementEnabled(false); | ||
95 | 103 | | |||
96 | auto disableSessionManagement = [](QSessionManager &sm) { | 104 | auto disableSessionManagement = [](QSessionManager &sm) { | ||
97 | sm.setRestartHint(QSessionManager::RestartNever); | 105 | sm.setRestartHint(QSessionManager::RestartNever); | ||
98 | }; | 106 | }; | ||
99 | QObject::connect(&app, &QGuiApplication::commitDataRequest, disableSessionManagement); | 107 | QObject::connect(&app, &QGuiApplication::commitDataRequest, disableSessionManagement); | ||
100 | QObject::connect(&app, &QGuiApplication::saveStateRequest, disableSessionManagement); | 108 | QObject::connect(&app, &QGuiApplication::saveStateRequest, disableSessionManagement); | ||
101 | 109 | | |||
102 | View view; | 110 | View view; | ||
111 | | ||||
apol: You can do `const QString query = parser.positionalArguments().value(0);` | |||||
112 | auto updateVisibility = [&]() { | ||||
113 | const QString query = parser.positionalArguments().value(0); | ||||
114 | | ||||
115 | if (parser.isSet(daemonOption)) { | ||||
apol: ^ `value(0)`. | |||||
103 | view.setVisible(false); | 116 | view.setVisible(false); | ||
117 | } else if (parser.isSet(clipboardOption)) { | ||||
broulik: This stuffin the lambda is somewhat duplicated, isn't it? | |||||
118 | view.displayWithClipboardContents(); | ||||
119 | } else if (!query.isEmpty()) { | ||||
120 | view.query(query); | ||||
121 | } else { | ||||
122 | view.display(); | ||||
123 | } | ||||
124 | }; | ||||
125 | | ||||
126 | updateVisibility(); | ||||
104 | 127 | | |||
105 | QObject::connect(&service, &KDBusService::activateRequested, &view, &View::display); | 128 | QObject::connect(&service, &KDBusService::activateRequested, &view, [&](const QStringList &arguments, const QString &workingDirectory) { | ||
129 | Q_UNUSED(workingDirectory) | ||||
130 | parser.parse(arguments); | ||||
131 | updateVisibility(); | ||||
132 | }); | ||||
106 | 133 | | |||
107 | return app.exec(); | 134 | return app.exec(); | ||
108 | } | 135 | } |
Use initializer list {QStringLiteral("c"), QStringLiteral("clipboard")}