QtPass 2.0.0
Developer documentation
Loading...
Searching...
No Matches
main.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2014 Anne Jan Brouwer
2// SPDX-License-Identifier: GPL-3.0-or-later
8
9#include "mainwindow.h"
10#include "qtpasssettings.h"
11#include "sshauthsock.h"
12#include "util.h"
13#if SINGLE_APP
14#include "singleapplication.h"
15#endif
16
17#include <QApplication>
18#include <QDir>
19#include <QTranslator>
20#include <QtWidgets>
21
29static auto joinRemainingArgs(const QStringList &args, int start) -> QString {
30 Q_ASSERT(start >= 0 && start <= args.size());
31 return args.mid(start).join(" ");
32}
33
43static auto appendWithSpaceIfSuffixNotEmpty(QString &target,
44 const QString &suffix) -> void {
45 if (!suffix.isEmpty()) {
46 if (!target.isEmpty())
47 target += " ";
48 target += suffix;
49 }
50}
51
59auto main(int argc, char *argv[]) -> int {
60 // Before anything starts git or gpg: pass's umask for them, and no
61 // inherited variable pointing git at another repository.
63 QString text;
64#if SINGLE_APP
65 QString name = qgetenv("USER");
66 if (name.isEmpty())
67 name = qgetenv("USERNAME");
68 SingleApplication app(argc, argv,
69 SingleApplication::privateKey(name + "QtPass"));
70#else
71 QApplication app(argc, argv);
72#endif
73
74 const QStringList args = app.arguments();
75 bool consumeNextArg = false;
76 for (int i = 1; i < args.count(); ++i) {
77 const auto &arg = args.at(i);
78
79 if (arg == "--") {
80 consumeNextArg = false;
82 break;
83 }
84
85 if (consumeNextArg) {
86 consumeNextArg = false;
87 continue;
88 }
89
90 if (arg.startsWith('-')) {
91 // We only collect positional arguments into `text`.
92 // For options in the form `--option value`, skip the separate value
93 // token. Options in the form `--option=value` are fully contained in
94 // `arg`.
95 const bool optionTakesSeparateValue =
96 !arg.contains('=') && i + 1 < args.count() && arg.startsWith("--") &&
97 !arg.startsWith("---") && !args[i + 1].startsWith('-');
98 if (optionTakesSeparateValue)
99 consumeNextArg = true;
100 continue;
101 }
102
103 if (!text.isEmpty())
104 text += " ";
105 text += arg;
106 }
107
108 if ((text.indexOf("-psn_") == 0) || (text.indexOf("-session") == 0)) {
109 text.clear();
110 }
111
112#if SINGLE_APP
113 // Hand the arguments to the running instance. When that fails (the peer
114 // vanished between the probe and now) start normally instead of exiting
115 // silently, which would leave the user with a launcher click that did
116 // nothing.
117 if (app.isRunning() && app.sendMessage(text)) {
118 return 0;
119 }
120#endif
121
122 Q_INIT_RESOURCE(resources);
123 Q_INIT_RESOURCE(qmake_qmake_qm_files); // qmake names the file
124
125 QCoreApplication::setOrganizationName("IJHack");
126 QCoreApplication::setOrganizationDomain("ijhack.org");
127 QCoreApplication::setApplicationName("QtPass");
128 QCoreApplication::setApplicationVersion(VERSION);
129
130 // Probe / set SSH_AUTH_SOCK before any subprocess runs (issue #543).
131 // GUI launchers don't inherit shell-set env vars, so users with
132 // gpg-agent's SSH support get failed git push/pull until this fires.
133 SshAuthSock::initialise(QtPassSettings::load().sshAuthSockOverride);
134
135 // Setup and load translator for localization.
136 //
137 // Use the QLocale-aware load() overload so Qt walks the user's preferred
138 // language list (e.g. ar_MA -> ar, da_DK -> da) automatically. The previous
139 // single-filename form returned false on the first miss and left the app
140 // untranslated whenever the system locale carried a country suffix that
141 // didn't match any .qm file verbatim.
142 QTranslator translator;
143 if (translator.load(QLocale::system(), QStringLiteral("localization"),
144 QStringLiteral("_"), QStringLiteral(":/localization"),
145 QStringLiteral(".qm"))) {
146#if SINGLE_APP
147 SingleApplication::installTranslator(&translator);
148 SingleApplication::setLayoutDirection(
149 QObject::tr("LTR") == "RTL" ? Qt::RightToLeft : Qt::LeftToRight);
150#else
151 QApplication::installTranslator(&translator);
152 QApplication::setLayoutDirection(
153 QObject::tr("LTR") == "RTL" ? Qt::RightToLeft : Qt::LeftToRight);
154#endif
155 }
156
157 // Both must be set before the window exists: Wayland compositors read the
158 // desktop file name when the surface is created and use it to find the
159 // icon, and a window created before setWindowIcon() keeps the default one.
160 // Inside a Flatpak the desktop file is renamed to the app id.
161 QGuiApplication::setDesktopFileName(
162 qEnvironmentVariable("FLATPAK_ID", QStringLiteral("qtpass")));
163 QApplication::setWindowIcon(QIcon(":artwork/icon.png"));
164
165 MainWindow w(text);
166
167 // A cancelled first-run wizard (or otherwise unusable configuration) makes
168 // QtPass::init() report failure from the MainWindow constructor. Quitting
169 // there is a no-op before exec() runs, so bail here before the window is
170 // ever shown.
171 if (!w.initSucceeded()) {
172 return 0;
173 }
174
175 w.activateWindow();
176
177#if SINGLE_APP
178 QObject::connect(&app, &SingleApplication::messageAvailable, &w,
180#endif
181
182 // Placement is MainWindow::restoreWindow()'s job: the saved geometry when
183 // there is one, the cursor's screen otherwise. Re-centring here threw the
184 // saved position away on every start.
185 w.show();
186
187#if SINGLE_APP
188 return SingleApplication::exec();
189#else
190 return QApplication::exec();
191#endif
192}
Main application window orchestrating UI, user interactions, and external process handlers.
Definition mainwindow.h:59
void messageAvailable(const QString &message)
Handle an incoming inter-process message (single-instance mode).
auto initSucceeded() const -> bool
Return whether startup configuration succeeded.
Definition mainwindow.h:133
static auto load() -> AppSettings
Load all flat settings into an AppSettings value object.
QApplication subclass enforcing single-instance behavior via IPC. Detects whether another instance wi...
auto isRunning() -> bool
Query whether another instance is already running.
static auto privateKey(const QString &base) -> QString
The key to construct with: a socket path in a folder only this user can write to (see privateKeyIn())...
void messageAvailable(const QString &message)
messageAvailable notification from commandline
auto sendMessage(const QString &message) -> bool
Send a message to the running instance.
static void initialise(const QString &override={})
Ensure SSH_AUTH_SOCK is set for child processes.
static void applyPassProcessDefaults()
Set up the process the way pass sets up its own, before anything runs git or gpg or writes into the s...
Definition util.cpp:126
auto main(int argc, char *argv[]) -> int
Application entry point: parses arguments and launches the main window.
Definition main.cpp:59
static auto joinRemainingArgs(const QStringList &args, int start) -> QString
Joins all arguments from index start onward into a space-separated string.
Definition main.cpp:29
static auto appendWithSpaceIfSuffixNotEmpty(QString &target, const QString &suffix) -> void
Appends a suffix to a target string, inserting a separating space if the target is not empty.
Definition main.cpp:43
MainWindow: the main application window (tree, toolbar, search, status bar, process output dock) and ...
QtPassSettings, the QSettings singleton behind every saved setting, the profiles and the Pass backend...
SingleApplication: a QApplication that keeps one QtPass instance per user via a lock file and forward...
SshAuthSock: finding an SSH agent socket for child processes and judging a configured override.
Util: static helpers for the process environment, binary discovery, store paths, URL safety and the l...