QtPass 1.6.0
Multi-platform GUI for pass, the standard unix password manager.
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
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
39
40static auto joinRemainingArgs(const QStringList &args, int start) -> QString {
41 Q_ASSERT(start >= 0 && start <= args.size());
42 return args.mid(start).join(" ");
43}
44
45static auto appendWithSpaceIfSuffixNotEmpty(QString &target,
46 const QString &suffix) -> void {
47 if (!suffix.isEmpty()) {
48 if (!target.isEmpty())
49 target += " ";
50 target += suffix;
51 }
52}
53
60auto main(int argc, char *argv[]) -> int {
61#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
62 QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
63 QApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
64#endif
65
66 QString text;
67#if SINGLE_APP
68 QString name = qgetenv("USER");
69 if (name.isEmpty())
70 name = qgetenv("USERNAME");
71 SingleApplication app(argc, argv, name + "QtPass");
72#else
73 QApplication app(argc, argv);
74#endif
75
76 const QStringList args = app.arguments();
77 bool consumeNextArg = false;
78 for (int i = 1; i < args.count(); ++i) {
79 const auto &arg = args.at(i);
80
81 if (arg == "--") {
82 consumeNextArg = false;
83 appendWithSpaceIfSuffixNotEmpty(text, joinRemainingArgs(args, i + 1));
84 break;
85 }
86
87 if (consumeNextArg) {
88 consumeNextArg = false;
89 continue;
90 }
91
92 if (arg.startsWith('-')) {
93 // We only collect positional arguments into `text`.
94 // For options in the form `--option value`, skip the separate value
95 // token. Options in the form `--option=value` are fully contained in
96 // `arg`.
97 const bool optionTakesSeparateValue =
98 !arg.contains('=') && i + 1 < args.count() && arg.startsWith("--") &&
99 !arg.startsWith("---") && !args[i + 1].startsWith('-');
100 if (optionTakesSeparateValue)
101 consumeNextArg = true;
102 continue;
103 }
104
105 if (!text.isEmpty())
106 text += " ";
107 text += arg;
108 }
109
110 if ((text.indexOf("-psn_") == 0) || (text.indexOf("-session") == 0)) {
111 text.clear();
112 }
113
114#if SINGLE_APP
115 if (app.isRunning()) {
116 app.sendMessage(text);
117 return 0;
118 }
119#endif
120
121 Q_INIT_RESOURCE(resources);
122 Q_INIT_RESOURCE(qmake_qmake_qm_files); // qmake names the file
123
124 QCoreApplication::setOrganizationName("IJHack");
125 QCoreApplication::setOrganizationDomain("ijhack.org");
126 QCoreApplication::setApplicationName("QtPass");
127 QCoreApplication::setApplicationVersion(VERSION);
128
129 // Setup and load translator for localization
130 QTranslator translator;
131 QString locale = QLocale::system().name();
132 if (translator.load(
133 QString(":localization/localization_%1.qm").arg(locale))) {
134#if SINGLE_APP
135 SingleApplication::installTranslator(&translator);
136 SingleApplication::setLayoutDirection(
137 QObject::tr("LTR") == "RTL" ? Qt::RightToLeft : Qt::LeftToRight);
138#else
139 QApplication::installTranslator(&translator);
140 QApplication::setLayoutDirection(
141 QObject::tr("LTR") == "RTL" ? Qt::RightToLeft : Qt::LeftToRight);
142#endif
143 }
144
145 MainWindow w(text);
146
147 w.activateWindow();
148
149 QApplication::setWindowIcon(QIcon(":artwork/icon.png"));
150
151#if SINGLE_APP
152 QObject::connect(&app, &SingleApplication::messageAvailable, &w,
154#endif
155
156 QGuiApplication::setDesktopFileName("qtpass");
157
158 // Center the MainWindow on the screen the mouse pointer is currently on
159#if QT_VERSION < QT_VERSION_CHECK(5, 12, 0)
160 static int cursorScreen =
161 app.desktop()->screenNumber(app.desktop()->cursor().pos());
162 QPoint cursorScreenCenter =
163 app.desktop()->screenGeometry(cursorScreen).center();
164 QRect windowFrameGeo = w.frameGeometry();
165 windowFrameGeo.moveCenter(cursorScreenCenter);
166 w.move(windowFrameGeo.topLeft());
167#else
168 QScreen *screen = QGuiApplication::screenAt(QCursor::pos());
169 if (!screen)
170 screen = QGuiApplication::primaryScreen();
171 if (screen) {
172 const QPoint cursorScreenCenter = screen->geometry().center();
173 QRect windowFrameGeo = w.frameGeometry();
174 windowFrameGeo.moveCenter(cursorScreenCenter);
175 w.move(windowFrameGeo.topLeft());
176 }
177#endif
178
179 w.show();
180
181#if SINGLE_APP
182 return SingleApplication::exec();
183#else
184 return QApplication::exec();
185#endif
186}
The MainWindow class does way too much, not only is it a switchboard, configuration handler and more,...
Definition mainwindow.h:51
void messageAvailable(const QString &message)
MainWindow::messageAvailable we have some text/message/search to do.
auto isRunning() -> bool
SingleApplication::isRunning is there already a QtPass instance running, to check wether to be server...
auto sendMessage(const QString &message) -> bool
SingleApplication::sendMessage send a message (from commandline) to an already running QtPass instanc...
auto main(int argc, char *argv[]) -> int
main
Definition main.cpp:60
#define SingleApplication
Definition mainwindow.h:17