| 1 | /* |
| 2 | * Copyright (C) 2021 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 "CommandLineCaptureHandler.h" |
| 21 | |
| 22 | CommandLineCaptureHandler::CommandLineCaptureHandler( |
| 23 | const QSharedPointer<IImageGrabber> &imageGrabber, |
| 24 | const QSharedPointer<IUploadHandler> &uploadHandler, |
| 25 | const QSharedPointer<IImageSaver> &imageSaver, |
| 26 | const QSharedPointer<ISavePathProvider> &savePathProvider) : |
| 27 | mImageGrabber(imageGrabber), |
| 28 | mUploadHandler(uploadHandler), |
| 29 | mImageSaver(imageSaver), |
| 30 | mSavePathProvider(savePathProvider), |
| 31 | mIsWithSave(false), |
| 32 | mIsWithUpload(false) |
| 33 | { |
| 34 | connect(mImageGrabber.data(), &IImageGrabber::finished, this, &CommandLineCaptureHandler::processCapture); |
| 35 | connect(mImageGrabber.data(), &IImageGrabber::canceled, this, &CommandLineCaptureHandler::canceled); |
| 36 | |
| 37 | connect(mUploadHandler.data(), &IUploader::finished, this, &CommandLineCaptureHandler::uploadFinished); |
| 38 | } |
| 39 | |
| 40 | void CommandLineCaptureHandler::captureAndProcessScreenshot(const CommandLineCaptureParameter ¶meter) |
| 41 | { |
| 42 | mIsWithSave = parameter.isWithSave; |
| 43 | mIsWithUpload = parameter.isWithUpload; |
| 44 | mSavePath = parameter.savePath; |
| 45 | mImageGrabber->grabImage(parameter.captureMode, parameter.isWithCursor, parameter.delay); |
| 46 | } |
| 47 | |
| 48 | void CommandLineCaptureHandler::processCapture(const CaptureDto &capture) |
| 49 | { |
| 50 | mCurrentCapture = capture; |
| 51 | |
| 52 | if (mIsWithSave) { |
| 53 | saveCapture(mCurrentCapture); |
| 54 | } |
| 55 | |
| 56 | if (mIsWithUpload) { |
| 57 | mUploadHandler->upload(capture.screenshot.toImage()); |
| 58 | } else { |
| 59 | finished(mCurrentCapture); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | void CommandLineCaptureHandler::saveCapture(const CaptureDto &capture) |
| 64 | { |
| 65 | auto savePath = mSavePath.isEmpty() ? mSavePathProvider->savePath() : mSavePath; |
| 66 | auto isSaveSuccessful = mImageSaver->save(capture.screenshot.toImage(), savePath); |
| 67 | |
| 68 | if (isSaveSuccessful) { |
| 69 | qInfo("Capture saved to %s", qPrintable(savePath)); |
| 70 | } else { |
| 71 | qWarning("Failed to save capture to %s", qPrintable(savePath)); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | QList<CaptureModes> CommandLineCaptureHandler::supportedCaptureModes() const |
| 76 | { |
| 77 | return mImageGrabber->supportedCaptureModes(); |
| 78 | } |
| 79 | |
| 80 | void CommandLineCaptureHandler::uploadFinished(const UploadResult &result) |
| 81 | { |
| 82 | if (result.isError()) { |
| 83 | auto enumTranslator = EnumTranslator::instance(); |
| 84 | qWarning("Upload failed: %s", qPrintable(enumTranslator->toString(result.status))); |
| 85 | } else { |
| 86 | qInfo("Upload finished"); |
| 87 | } |
| 88 | |
| 89 | if (result.hasContent()) { |
| 90 | qInfo("Upload result: %s", qPrintable(result.content)); |
| 91 | } |
| 92 | |
| 93 | finished(mCurrentCapture); |
| 94 | } |