| 1 | /* |
| 2 | * Copyright (C) 2019 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 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 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 "CommandLine.h" |
| 21 | |
| 22 | CommandLine::CommandLine(const QCoreApplication &app, const QList<CaptureModes> &captureModes) |
| 23 | { |
| 24 | setApplicationDescription(translateText(QLatin1String("Ksnip Screenshot Tool"))); |
| 25 | addHelpOption(); |
| 26 | addVersionOptions(); |
| 27 | addImageGrabberOptions(captureModes); |
| 28 | addDefaultOptions(); |
| 29 | addPositionalArguments(); |
| 30 | process(app); |
| 31 | } |
| 32 | |
| 33 | CommandLine::~CommandLine() |
| 34 | { |
| 35 | delete mRectAreaOption; |
| 36 | delete mLastRectAreaOption; |
| 37 | delete mFullScreenOption; |
| 38 | delete mCurrentScreenOption; |
| 39 | delete mActiveWindowOption; |
| 40 | delete mWindowUnderCursorOption; |
| 41 | delete mPortalOption; |
| 42 | delete mDelayOption; |
| 43 | delete mCursorOption; |
| 44 | delete mEditOption; |
| 45 | delete mSaveOption; |
| 46 | delete mSaveToOption; |
| 47 | delete mVersionOption; |
| 48 | delete mUploadOption; |
| 49 | } |
| 50 | |
| 51 | void CommandLine::addImageGrabberOptions(const QList<CaptureModes> &captureModes) |
| 52 | { |
| 53 | if (captureModes.contains(CaptureModes::RectArea)) { |
| 54 | mRectAreaOption = addOption(QLatin1String("r"), QLatin1String("rectarea"), QLatin1String("Select a rectangular area from where to take a screenshot.")); |
| 55 | } |
| 56 | if (captureModes.contains(CaptureModes::LastRectArea)) { |
| 57 | mLastRectAreaOption = addOption(QLatin1String("l"), QLatin1String("lastrectarea"), QLatin1String("Take a screenshot using last selected rectangular area.")); |
| 58 | } |
| 59 | if (captureModes.contains(CaptureModes::FullScreen)) { |
| 60 | mFullScreenOption = addOption(QLatin1String("f"), QLatin1String("fullscreen"), QLatin1String("Capture the fullscreen including all monitors.")); |
| 61 | } |
| 62 | if (captureModes.contains(CaptureModes::CurrentScreen)) { |
| 63 | mCurrentScreenOption = addOption(QLatin1String("m"), QLatin1String("current"), QLatin1String("Capture the screen (monitor) where the mouse cursor is currently located.")); |
| 64 | } |
| 65 | if (captureModes.contains(CaptureModes::ActiveWindow)) { |
| 66 | mActiveWindowOption = addOption(QLatin1String("a"), QLatin1String("active"), QLatin1String("Capture the window that currently has input focus.")); |
| 67 | } |
| 68 | if (captureModes.contains(CaptureModes::WindowUnderCursor)) { |
| 69 | mWindowUnderCursorOption = addOption(QLatin1String("u"), QLatin1String("windowundercursor"), QLatin1String("Capture the window that is currently under the mouse cursor.")); |
| 70 | } |
| 71 | if (captureModes.contains(CaptureModes::Portal)) { |
| 72 | mWindowUnderCursorOption = addOption(QLatin1String("t"), QLatin1String("portal"), QLatin1String("Uses the screenshot Portal for taking screenshot.")); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | void CommandLine::addDefaultOptions() |
| 77 | { |
| 78 | mDelayOption = addParameterOption(QLatin1String("d"), QLatin1String("delay"), QLatin1String("Delay before taking the screenshot."), QLatin1String("seconds")); |
| 79 | mCursorOption = addOption(QLatin1String("c"), QLatin1String("cursor"), QLatin1String("Capture mouse cursor on screenshot.")); |
| 80 | mEditOption = addParameterOption(QLatin1String("e"), QLatin1String("edit"), QLatin1String("Edit existing image in ksnip."), QLatin1String("image")); |
| 81 | mSaveOption = addOption(QLatin1String("s"), QLatin1String("save"), QLatin1String("Save screenshot to default location without opening in editor.")); |
| 82 | mSaveToOption = addParameterOption(QLatin1String("p"),QLatin1String("saveto"),QLatin1String("Save screenshot to provided path without opening in editor."), QLatin1String("path")); |
| 83 | mUploadOption = addOption(QLatin1String("o"), QLatin1String("upload"), QLatin1String("Upload screenshot via default uploader without opening in editor.")); |
| 84 | } |
| 85 | |
| 86 | void CommandLine::addVersionOptions() |
| 87 | { |
| 88 | mVersionOption = addOption(QLatin1String("v"), QLatin1String("version"), QLatin1String("Displays version information.")); |
| 89 | } |
| 90 | |
| 91 | QString CommandLine::translateText(const QString &text) |
| 92 | { |
| 93 | return QCoreApplication::translate("main", text.toLatin1()); |
| 94 | } |
| 95 | |
| 96 | QCommandLineOption* CommandLine::addOption(const QString &shortName, const QString &longName, const QString &description) |
| 97 | { |
| 98 | auto newOption = new QCommandLineOption({shortName, longName}, translateText(description)); |
| 99 | QCommandLineParser::addOption(*newOption); |
| 100 | return newOption; |
| 101 | } |
| 102 | |
| 103 | QCommandLineOption* CommandLine::addParameterOption(const QString &shortName, const QString &longName, const QString &description, const QString ¶meter) |
| 104 | { |
| 105 | auto newOption = new QCommandLineOption({shortName, longName}, translateText(description), translateText(parameter), QString()); |
| 106 | QCommandLineParser::addOption(*newOption); |
| 107 | return newOption; |
| 108 | } |
| 109 | |
| 110 | bool CommandLine::isRectAreaSet() const |
| 111 | { |
| 112 | return mRectAreaOption != nullptr && isSet(*mRectAreaOption); |
| 113 | } |
| 114 | |
| 115 | bool CommandLine::isLastRectAreaSet() const |
| 116 | { |
| 117 | return mLastRectAreaOption != nullptr && isSet(*mLastRectAreaOption); |
| 118 | } |
| 119 | |
| 120 | bool CommandLine::isFullScreenSet() const |
| 121 | { |
| 122 | return mFullScreenOption != nullptr && isSet(*mFullScreenOption); |
| 123 | } |
| 124 | |
| 125 | bool CommandLine::isCurrentScreenSet() const |
| 126 | { |
| 127 | return mCurrentScreenOption != nullptr && isSet(*mCurrentScreenOption); |
| 128 | } |
| 129 | |
| 130 | bool CommandLine::isActiveWindowSet() const |
| 131 | { |
| 132 | return mActiveWindowOption != nullptr && isSet(*mActiveWindowOption); |
| 133 | } |
| 134 | |
| 135 | bool CommandLine::isWindowsUnderCursorSet() const |
| 136 | { |
| 137 | return mWindowUnderCursorOption != nullptr && isSet(*mWindowUnderCursorOption); |
| 138 | } |
| 139 | |
| 140 | bool CommandLine::isPortalSet() const |
| 141 | { |
| 142 | return mPortalOption != nullptr && isSet(*mPortalOption); |
| 143 | } |
| 144 | |
| 145 | bool CommandLine::isDelaySet() const |
| 146 | { |
| 147 | return mDelayOption != nullptr && isSet(*mDelayOption); |
| 148 | } |
| 149 | |
| 150 | bool CommandLine::isCursorSet() const |
| 151 | { |
| 152 | return mCursorOption != nullptr && isSet(*mCursorOption); |
| 153 | } |
| 154 | |
| 155 | bool CommandLine::isEditSet() const |
| 156 | { |
| 157 | return (mEditOption != nullptr && isSet(*mEditOption)) || positionalArguments().count() == 1; |
| 158 | } |
| 159 | |
| 160 | bool CommandLine::isSaveSet() const |
| 161 | { |
| 162 | return (mSaveOption != nullptr && isSet(*mSaveOption)) || (mSaveToOption != nullptr && isSet(*mSaveToOption)); |
| 163 | } |
| 164 | |
| 165 | bool CommandLine::isVersionSet() const |
| 166 | { |
| 167 | return mVersionOption != nullptr && isSet(*mVersionOption); |
| 168 | } |
| 169 | |
| 170 | int CommandLine::delay() const |
| 171 | { |
| 172 | auto valid = true; |
| 173 | auto delay = value(*mDelayOption).toInt(&valid); |
| 174 | return valid && delay >= 0 ? delay : -1; |
| 175 | } |
| 176 | |
| 177 | QString CommandLine::imagePath() const |
| 178 | { |
| 179 | return positionalArguments().count() == 1 ? positionalArguments().first() : value(*mEditOption); |
| 180 | } |
| 181 | |
| 182 | QString CommandLine::saveToPath() const |
| 183 | { |
| 184 | return value(*mSaveToOption); |
| 185 | } |
| 186 | |
| 187 | bool CommandLine::isCaptureModeSet() const |
| 188 | { |
| 189 | return isRectAreaSet() || isLastRectAreaSet() || isFullScreenSet() || isCurrentScreenSet() || isActiveWindowSet() || isWindowsUnderCursorSet(); |
| 190 | } |
| 191 | |
| 192 | bool CommandLine::isUploadSet() const |
| 193 | { |
| 194 | return mUploadOption != nullptr && isSet(*mUploadOption); |
| 195 | } |
| 196 | |
| 197 | CaptureModes CommandLine::captureMode() const |
| 198 | { |
| 199 | if (isFullScreenSet()) { |
| 200 | return CaptureModes::FullScreen; |
| 201 | } else if (isCurrentScreenSet()) { |
| 202 | return CaptureModes::CurrentScreen; |
| 203 | } else if (isActiveWindowSet()) { |
| 204 | return CaptureModes::ActiveWindow; |
| 205 | } else if (isWindowsUnderCursorSet()) { |
| 206 | return CaptureModes::WindowUnderCursor; |
| 207 | } else if (isLastRectAreaSet()) { |
| 208 | return CaptureModes::LastRectArea; |
| 209 | } else if (isPortalSet()) { |
| 210 | return CaptureModes::Portal; |
| 211 | } else { |
| 212 | return CaptureModes::RectArea; |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | void CommandLine::addPositionalArguments() |
| 217 | { |
| 218 | addPositionalArgument(QLatin1String("image"), QLatin1String("Edit existing image in ksnip"), QLatin1String("[image]")); |
| 219 | } |