| 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 "StandAloneBootstrapper.h" |
| 21 | |
| 22 | StandAloneBootstrapper::StandAloneBootstrapper(DependencyInjector *dependencyInjector) : |
| 23 | mCommandLine(nullptr), |
| 24 | mMainWindow(nullptr), |
| 25 | mCommandLineCaptureHandler(nullptr), |
| 26 | mLogger(dependencyInjector->get<ILogger>()), |
| 27 | mImageFromStdInputReader(dependencyInjector->get<IImageFromStdInputReader>()), |
| 28 | mDependencyInjector(dependencyInjector) |
| 29 | { |
| 30 | |
| 31 | } |
| 32 | |
| 33 | StandAloneBootstrapper::~StandAloneBootstrapper() |
| 34 | { |
| 35 | delete mCommandLine; |
| 36 | } |
| 37 | |
| 38 | int StandAloneBootstrapper::start(const QApplication &app) |
| 39 | { |
| 40 | app.setQuitOnLastWindowClosed(false); |
| 41 | |
| 42 | createCommandLineParser(app); |
| 43 | |
| 44 | if (isVersionRequested()) { |
| 45 | return showVersion(); |
| 46 | } |
| 47 | |
| 48 | if (isStartedWithoutArguments()) { |
| 49 | return startKsnip(app); |
| 50 | } |
| 51 | |
| 52 | if (isEditRequested()) { |
| 53 | return startKsnipAndEditImage(app); |
| 54 | } |
| 55 | |
| 56 | if (isCommandLineCaptureRequested()) { |
| 57 | return takeCaptureAndProcess(app); |
| 58 | } |
| 59 | |
| 60 | return takeCaptureAndStartKsnip(app); |
| 61 | } |
| 62 | |
| 63 | int StandAloneBootstrapper::takeCaptureAndProcess(const QApplication &app) |
| 64 | { |
| 65 | connect(mCommandLineCaptureHandler.data(), &ICommandLineCaptureHandler::finished, this, &StandAloneBootstrapper::close); |
| 66 | connect(mCommandLineCaptureHandler.data(), &ICommandLineCaptureHandler::canceled, this, &StandAloneBootstrapper::close); |
| 67 | |
| 68 | CommandLineCaptureParameter parameter(getCaptureMode(), getDelay(), getCaptureCursor()); |
| 69 | parameter.isWithSave = getIsSave(); |
| 70 | parameter.isWithUpload = getIsUpload(); |
| 71 | parameter.savePath = getSavePath(); |
| 72 | |
| 73 | mCommandLineCaptureHandler->captureAndProcessScreenshot(parameter); |
| 74 | |
| 75 | return app.exec(); |
| 76 | } |
| 77 | |
| 78 | bool StandAloneBootstrapper::getIsUpload() const |
| 79 | { |
| 80 | return mCommandLine->isUploadSet(); |
| 81 | } |
| 82 | |
| 83 | bool StandAloneBootstrapper::getIsSave() const |
| 84 | { |
| 85 | return mCommandLine->isSaveSet(); |
| 86 | } |
| 87 | |
| 88 | bool StandAloneBootstrapper::isCommandLineCaptureRequested() const |
| 89 | { |
| 90 | return getIsSave() || getIsUpload(); |
| 91 | } |
| 92 | |
| 93 | bool StandAloneBootstrapper::isEditRequested() const |
| 94 | { |
| 95 | return mCommandLine->isEditSet(); |
| 96 | } |
| 97 | |
| 98 | bool StandAloneBootstrapper::isVersionRequested() const |
| 99 | { |
| 100 | return mCommandLine->isVersionSet(); |
| 101 | } |
| 102 | |
| 103 | bool StandAloneBootstrapper::isStartedWithoutArguments() |
| 104 | { |
| 105 | auto arguments = QCoreApplication::arguments(); |
| 106 | return arguments.count() <= 1; |
| 107 | } |
| 108 | |
| 109 | int StandAloneBootstrapper::takeCaptureAndStartKsnip(const QApplication &app) |
| 110 | { |
| 111 | loadTranslations(app); |
| 112 | |
| 113 | connect(mCommandLineCaptureHandler.data(), &ICommandLineCaptureHandler::finished, this, &StandAloneBootstrapper::openMainWindow); |
| 114 | connect(mCommandLineCaptureHandler.data(), &ICommandLineCaptureHandler::canceled, this, &StandAloneBootstrapper::close); |
| 115 | |
| 116 | CommandLineCaptureParameter parameter(getCaptureMode(), getDelay(), getCaptureCursor()); |
| 117 | |
| 118 | mCommandLineCaptureHandler->captureAndProcessScreenshot(parameter); |
| 119 | |
| 120 | return app.exec(); |
| 121 | } |
| 122 | |
| 123 | bool StandAloneBootstrapper::getCaptureCursor() const |
| 124 | { |
| 125 | return mCommandLine->isCursorSet(); |
| 126 | } |
| 127 | |
| 128 | int StandAloneBootstrapper::getDelay() const |
| 129 | { |
| 130 | auto delay = 0; |
| 131 | if (mCommandLine->isDelaySet()) { |
| 132 | delay = mCommandLine->delay(); |
| 133 | if (delay < 0) { |
| 134 | qWarning("Delay flag set without value, ignoring delay."); |
| 135 | delay = 0; |
| 136 | } |
| 137 | } |
| 138 | return delay * 1000; |
| 139 | } |
| 140 | |
| 141 | bool StandAloneBootstrapper::getSave() const |
| 142 | { |
| 143 | return mCommandLine->isSaveSet(); |
| 144 | } |
| 145 | |
| 146 | QString StandAloneBootstrapper::getSavePath() const |
| 147 | { |
| 148 | return mCommandLine->saveToPath(); |
| 149 | } |
| 150 | |
| 151 | CaptureModes StandAloneBootstrapper::getCaptureMode() const |
| 152 | { |
| 153 | if (mCommandLine->isCaptureModeSet()) { |
| 154 | return mCommandLine->captureMode(); |
| 155 | } else { |
| 156 | qWarning("No capture mode selected, using default."); |
| 157 | return CaptureModes::RectArea; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | int StandAloneBootstrapper::startKsnipAndEditImage(const QApplication &app) |
| 162 | { |
| 163 | auto pathToImage = getImagePath(); |
| 164 | auto pixmap = getPixmapFromCorrectSource(pathToImage); |
| 165 | |
| 166 | if (pixmap.isNull()) { |
| 167 | qWarning("Unable to open image file %s.", qPrintable(pathToImage)); |
| 168 | return 1; |
| 169 | } else { |
| 170 | loadTranslations(app); |
| 171 | if(PathHelper::isPipePath(pathToImage)) { |
| 172 | openMainWindow(CaptureDto(pixmap)); |
| 173 | } else { |
| 174 | openMainWindow(CaptureFromFileDto(pixmap, pathToImage)); |
| 175 | } |
| 176 | return app.exec(); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | QPixmap StandAloneBootstrapper::getPixmapFromCorrectSource(const QString &pathToImage) const |
| 181 | { |
| 182 | if (PathHelper::isPipePath(pathToImage)) { |
| 183 | qInfo("Reading image from stdin."); |
| 184 | return getPixmapFromStdin(); |
| 185 | } else { |
| 186 | return { pathToImage }; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | QPixmap StandAloneBootstrapper::getPixmapFromStdin() const |
| 191 | { |
| 192 | auto imageAsByteArray = mImageFromStdInputReader->read(); |
| 193 | QPixmap pixmap; |
| 194 | pixmap.loadFromData(imageAsByteArray); |
| 195 | return pixmap; |
| 196 | } |
| 197 | |
| 198 | QString StandAloneBootstrapper::getImagePath() const |
| 199 | { |
| 200 | return mCommandLine->imagePath(); |
| 201 | } |
| 202 | |
| 203 | int StandAloneBootstrapper::startKsnip(const QApplication &app) |
| 204 | { |
| 205 | loadTranslations(app); |
| 206 | createMainWindow(); |
| 207 | return app.exec(); |
| 208 | } |
| 209 | |
| 210 | int StandAloneBootstrapper::showVersion() |
| 211 | { |
| 212 | QTextStream stream(stdout); |
| 213 | stream << QLatin1String("Version: ") + qPrintable(KSNIP_VERSION) + QLatin1String("\n"); |
| 214 | stream << QLatin1String("Build: ") + qPrintable(KSNIP_BUILD_NUMBER) + QLatin1String("\n"); |
| 215 | return 0; |
| 216 | } |
| 217 | |
| 218 | void StandAloneBootstrapper::createMainWindow() |
| 219 | { |
| 220 | Q_ASSERT(mMainWindow == nullptr); |
| 221 | |
| 222 | DependencyInjectorBootstrapper::BootstrapGui(mDependencyInjector); |
| 223 | |
| 224 | mMainWindow = new MainWindow(mDependencyInjector); |
| 225 | } |
| 226 | |
| 227 | void StandAloneBootstrapper::createCommandLineParser(const QApplication &app) |
| 228 | { |
| 229 | Q_ASSERT(mCommandLine == nullptr); |
| 230 | Q_ASSERT(mCommandLineCaptureHandler == nullptr); |
| 231 | |
| 232 | DependencyInjectorBootstrapper::BootstrapCommandLine(mDependencyInjector); |
| 233 | |
| 234 | mCommandLineCaptureHandler = mDependencyInjector->get<ICommandLineCaptureHandler>(); |
| 235 | mCommandLine = new CommandLine (app, mCommandLineCaptureHandler->supportedCaptureModes()); |
| 236 | } |
| 237 | |
| 238 | void StandAloneBootstrapper::loadTranslations(const QApplication &app) |
| 239 | { |
| 240 | auto translationLoader = mDependencyInjector->get<ITranslationLoader>(); |
| 241 | translationLoader->load(app); |
| 242 | } |
| 243 | |
| 244 | void StandAloneBootstrapper::openMainWindow(const CaptureDto &captureDto) |
| 245 | { |
| 246 | createMainWindow(); |
| 247 | mMainWindow->processCapture(captureDto); |
| 248 | } |
| 249 | |
| 250 | void StandAloneBootstrapper::close() |
| 251 | { |
| 252 | QCoreApplication::quit(); |
| 253 | } |