Debian Codegraph @

src/bootstrapper/singleInstance/SingleInstanceClientBootstrapper.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2020 Damir Porobic <[email protected]>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20 #include "SingleInstanceClientBootstrapper.h"
21
22 SingleInstanceClientBootstrapper::SingleInstanceClientBootstrapper(DependencyInjector *dependencyInjector) :
23 StandAloneBootstrapper(dependencyInjector),
24 mIpcClient(new IpcClient),
25 mImageFromStdInputReader(dependencyInjector->get<IImageFromStdInputReader>())
26 {
27 auto usernameProvider = dependencyInjector->get<IUsernameProvider>();
28 mIpcClient->connectTo(SingleInstance::ServerName.arg(usernameProvider->getUsername()));
29 }
30
31 SingleInstanceClientBootstrapper::~SingleInstanceClientBootstrapper()
32 {
33 delete mIpcClient;
34 }
35
36 int SingleInstanceClientBootstrapper::start(const QApplication &app)
37 {
38 createCommandLineParser(app);
39
40 if (isVersionRequested()) {
41 return showVersion();
42 } else {
43 return notifyServer();
44 }
45 }
46
47 bool SingleInstanceClientBootstrapper::isImagePathValid(const QString &imagePath)
48 {
49 QPixmap pixmap(imagePath);
50 return !pixmap.isNull();
51 }
52
53 int SingleInstanceClientBootstrapper::notifyServer() const
54 {
55 SingleInstanceParameter parameter;
56 if (isStartedWithoutArguments()) {
57 mLogger->log(QLatin1String("Starting without arguments"));
58 parameter = SingleInstanceParameter();
59 } else if (isEditRequested()) {
60 auto imagePath = getImagePath();
61 if (PathHelper::isPipePath(imagePath)) {
62 mLogger->log(QLatin1String("Edit image from stdin"));
63 auto imageAsByteArray = mImageFromStdInputReader->read();
64 parameter = SingleInstanceParameter(imageAsByteArray.toBase64());
65 } else if (isImagePathValid(imagePath)) {
66 mLogger->log(QLatin1String("Edit image from file path"));
67 parameter = SingleInstanceParameter(imagePath);
68 } else {
69 qWarning("Unable to open image file %s.", qPrintable(imagePath));
70 return 1;
71 }
72 } else {
73 parameter = SingleInstanceParameter(getCaptureMode(), getSave(), getSavePath(), getCaptureCursor(), getDelay());
74 }
75 mIpcClient->send(mParameterTranslator.translate(parameter));
76
77 mLogger->log(QLatin1String("Notification sent to server, closing client.."));
78
79 return 0;
80 }
81