| 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 "SingleInstanceServerBootstrapper.h" |
| 21 | |
| 22 | SingleInstanceServerBootstrapper::SingleInstanceServerBootstrapper(DependencyInjector *dependencyInjector) : |
| 23 | StandAloneBootstrapper(dependencyInjector), |
| 24 | mIpcServer(new IpcServer), |
| 25 | mUsernameProvider(dependencyInjector->get<IUsernameProvider>()) |
| 26 | { |
| 27 | } |
| 28 | |
| 29 | SingleInstanceServerBootstrapper::~SingleInstanceServerBootstrapper() |
| 30 | { |
| 31 | delete mIpcServer; |
| 32 | } |
| 33 | |
| 34 | int SingleInstanceServerBootstrapper::start(const QApplication &app) |
| 35 | { |
| 36 | startServer(); |
| 37 | return StandAloneBootstrapper::start(app); |
| 38 | } |
| 39 | |
| 40 | void SingleInstanceServerBootstrapper::startServer() const |
| 41 | { |
| 42 | mIpcServer->listen(SingleInstance::ServerName.arg(mUsernameProvider->getUsername())); |
| 43 | connect(mIpcServer, &IpcServer::received, this, &SingleInstanceServerBootstrapper::processData); |
| 44 | } |
| 45 | |
| 46 | void SingleInstanceServerBootstrapper::processData(const QByteArray &data) |
| 47 | { |
| 48 | mLogger->log(QLatin1String("Single instance server received data from client")); |
| 49 | auto parameter = mParameterTranslator.translate(data); |
| 50 | switch (parameter.startupMode) { |
| 51 | case SingleInstanceStartupModes::Start: |
| 52 | mLogger->log(QLatin1String("Start triggered.")); |
| 53 | show(); |
| 54 | break; |
| 55 | case SingleInstanceStartupModes::Edit: |
| 56 | if(parameter.isImageAsByteArraySet()) { |
| 57 | mLogger->log(QLatin1String("Edit triggered with image received as byte array")); |
| 58 | QPixmap pixmap; |
| 59 | pixmap.loadFromData(parameter.imageAsByteArray); |
| 60 | mMainWindow->processCapture(CaptureDto(pixmap)); |
| 61 | } else { |
| 62 | mLogger->log(QLatin1String("Edit triggered with image received as image path")); |
| 63 | processImage(parameter.imagePath); |
| 64 | } |
| 65 | break; |
| 66 | case SingleInstanceStartupModes::Capture: |
| 67 | mLogger->log(QLatin1String("Capture triggered")); |
| 68 | capture(parameter); |
| 69 | break; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | void SingleInstanceServerBootstrapper::capture(const SingleInstanceParameter ¶meter) const |
| 74 | { |
| 75 | mLogger->log(QLatin1String("Single instance server was request to take screenshot")); |
| 76 | mMainWindow->captureScreenshot(parameter.captureMode, parameter.captureCursor, parameter.delay); |
| 77 | } |
| 78 | |
| 79 | void SingleInstanceServerBootstrapper::show() const |
| 80 | { |
| 81 | mLogger->log(QLatin1String("Single instance server was request to show main window")); |
| 82 | mMainWindow->show(); |
| 83 | } |
| 84 | |
| 85 | void SingleInstanceServerBootstrapper::processImage(const QString &imagePath) |
| 86 | { |
| 87 | mLogger->log(QString("Single instance server was request to open image %1.").arg(imagePath)); |
| 88 | QPixmap pixmap(imagePath); |
| 89 | auto captureDto = CaptureFromFileDto(pixmap, imagePath); |
| 90 | mMainWindow->processCapture(captureDto); |
| 91 | } |