QtPass 1.7.0
Multi-platform GUI for pass, the standard unix password manager.
Loading...
Searching...
No Matches
main.cpp
1// SPDX-FileCopyrightText: 2014 Anne Jan Brouwer
2// SPDX-License-Identifier: GPL-3.0-or-later
3
4#include "mainwindow.h"
5#if SINGLE_APP
6#include "singleapplication.h"
7#endif
8
9#include <QApplication>
10#include <QDir>
11#include <QTranslator>
12#include <QtWidgets>
13
40
48static auto joinRemainingArgs(const QStringList &args, int start) -> QString {
49 Q_ASSERT(start >= 0 && start <= args.size());
50 return args.mid(start).join(" ");
51}
52
62static auto appendWithSpaceIfSuffixNotEmpty(QString &target,
63 const QString &suffix) -> void {
64 if (!suffix.isEmpty()) {
65 if (!target.isEmpty())
66 target += " ";
67 target += suffix;
68 }
69}
70
78auto main(int argc, char *argv[]) -> int {
79#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
80 QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
81 QApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
82#endif
83
84 QString text;
85#if SINGLE_APP
86 QString name = qgetenv("USER");
87 if (name.isEmpty())
88 name = qgetenv("USERNAME");
89 SingleApplication app(argc, argv, name + "QtPass");
90#else
91 QApplication app(argc, argv);
92#endif
93
94 const QStringList args = app.arguments();
95 bool consumeNextArg = false;
96 for (int i = 1; i < args.count(); ++i) {
97 const auto &arg = args.at(i);
98
99 if (arg == "--") {
100 consumeNextArg = false;
101 appendWithSpaceIfSuffixNotEmpty(text, joinRemainingArgs(args, i + 1));
102 break;
103 }
104
105 if (consumeNextArg) {
106 consumeNextArg = false;
107 continue;
108 }
109
110 if (arg.startsWith('-')) {
111 // We only collect positional arguments into `text`.
112 // For options in the form `--option value`, skip the separate value
113 // token. Options in the form `--option=value` are fully contained in
114 // `arg`.
115 const bool optionTakesSeparateValue =
116 !arg.contains('=') && i + 1 < args.count() && arg.startsWith("--") &&
117 !arg.startsWith("---") && !args[i + 1].startsWith('-');
118 if (optionTakesSeparateValue)
119 consumeNextArg = true;
120 continue;
121 }
122
123 if (!text.isEmpty())
124 text += " ";
125 text += arg;
126 }
127
128 if ((text.indexOf("-psn_") == 0) || (text.indexOf("-session") == 0)) {
129 text.clear();
130 }
131
132#if SINGLE_APP
133 if (app.isRunning()) {
134 app.sendMessage(text);
135 return 0;
136 }
137#endif
138
139 Q_INIT_RESOURCE(resources);
140 Q_INIT_RESOURCE(qmake_qmake_qm_files); // qmake names the file
141
142 QCoreApplication::setOrganizationName("IJHack");
143 QCoreApplication::setOrganizationDomain("ijhack.org");
144 QCoreApplication::setApplicationName("QtPass");
145 QCoreApplication::setApplicationVersion(VERSION);
146
147 // Setup and load translator for localization
148 QTranslator translator;
149 QString locale = QLocale::system().name();
150 if (translator.load(
151 QString(":localization/localization_%1.qm").arg(locale))) {
152#if SINGLE_APP
153 SingleApplication::installTranslator(&translator);
154 SingleApplication::setLayoutDirection(
155 QObject::tr("LTR") == "RTL" ? Qt::RightToLeft : Qt::LeftToRight);
156#else
157 QApplication::installTranslator(&translator);
158 QApplication::setLayoutDirection(
159 QObject::tr("LTR") == "RTL" ? Qt::RightToLeft : Qt::LeftToRight);
160#endif
161 }
162
163 MainWindow w(text);
164
165 w.activateWindow();
166
167 QApplication::setWindowIcon(QIcon(":artwork/icon.png"));
168
169#if SINGLE_APP
170 QObject::connect(&app, &SingleApplication::messageAvailable, &w,
172#endif
173
174 QGuiApplication::setDesktopFileName("qtpass");
175
176 // Center the MainWindow on the screen the mouse pointer is currently on
177#if QT_VERSION < QT_VERSION_CHECK(5, 12, 0)
178 static int cursorScreen =
179 app.desktop()->screenNumber(app.desktop()->cursor().pos());
180 QPoint cursorScreenCenter =
181 app.desktop()->screenGeometry(cursorScreen).center();
182 QRect windowFrameGeo = w.frameGeometry();
183 windowFrameGeo.moveCenter(cursorScreenCenter);
184 w.move(windowFrameGeo.topLeft());
185#else
186 QScreen *screen = QGuiApplication::screenAt(QCursor::pos());
187 if (!screen)
188 screen = QGuiApplication::primaryScreen();
189 if (screen) {
190 const QPoint cursorScreenCenter = screen->geometry().center();
191 QRect windowFrameGeo = w.frameGeometry();
192 windowFrameGeo.moveCenter(cursorScreenCenter);
193 w.move(windowFrameGeo.topLeft());
194 }
195#endif
196
197 w.show();
198
199#if SINGLE_APP
200 return SingleApplication::exec();
201#else
202 return QApplication::exec();
203#endif
204}
Main application window orchestrating UI, user interactions, and external process handlers.
Definition mainwindow.h:45
void messageAvailable(const QString &message)
Handle an incoming inter-process message (single-instance mode).