| 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 "ImgurUploaderSettings.h" |
| 21 | |
| 22 | ImgurUploaderSettings::ImgurUploaderSettings(const QSharedPointer<IConfig> &config) : |
| 23 | mConfig(config), |
| 24 | mForceAnonymousCheckbox(new QCheckBox(this)), |
| 25 | mDirectLinkToImageCheckbox(new QCheckBox(this)), |
| 26 | mAlwaysCopyToClipboardCheckBox(new QCheckBox(this)), |
| 27 | mOpenLinkInBrowserCheckbox(new QCheckBox(this)), |
| 28 | mClientIdLineEdit(new QLineEdit(this)), |
| 29 | mClientSecretLineEdit(new QLineEdit(this)), |
| 30 | mPinLineEdit(new QLineEdit(this)), |
| 31 | mUsernameLineEdit(new QLineEdit(this)), |
| 32 | mBaseUrlLineEdit(new CustomLineEdit(this)), |
| 33 | mUploadTitleEdit(new CustomLineEdit(this)), |
| 34 | mUploadDescriptionEdit(new CustomLineEdit(this)), |
| 35 | mUsernameLabel(new QLabel(this)), |
| 36 | mBaseUrlLabel(new QLabel(this)), |
| 37 | mUploadTitleLabel(new QLabel(this)), |
| 38 | mUploadDescriptionLabel(new QLabel(this)), |
| 39 | mGetPinButton(new QPushButton(this)), |
| 40 | mGetTokenButton(new QPushButton(this)), |
| 41 | mClearTokenButton(new QPushButton(this)), |
| 42 | mHistoryButton(new QPushButton(this)), |
| 43 | mImgurWrapper(new ImgurWrapper(mConfig->imgurBaseUrl(), this)), |
| 44 | mLayout(new QGridLayout(this)) |
| 45 | { |
| 46 | Q_ASSERT(mConfig != nullptr); |
| 47 | |
| 48 | initGui(); |
| 49 | loadConfig(); |
| 50 | } |
| 51 | |
| 52 | void ImgurUploaderSettings::saveSettings() |
| 53 | { |
| 54 | mConfig->setImgurForceAnonymous(mForceAnonymousCheckbox->isChecked()); |
| 55 | mConfig->setImgurLinkDirectlyToImage(mDirectLinkToImageCheckbox->isChecked()); |
| 56 | mConfig->setImgurAlwaysCopyToClipboard(mAlwaysCopyToClipboardCheckBox->isChecked()); |
| 57 | mConfig->setImgurOpenLinkInBrowser(mOpenLinkInBrowserCheckbox->isChecked()); |
| 58 | mConfig->setImgurUploadTitle(mUploadTitleEdit->textOrPlaceholderText()); |
| 59 | mConfig->setImgurUploadDescription(mUploadDescriptionEdit->textOrPlaceholderText()); |
| 60 | mConfig->setImgurBaseUrl(mBaseUrlLineEdit->textOrPlaceholderText()); |
| 61 | } |
| 62 | |
| 63 | void ImgurUploaderSettings::initGui() |
| 64 | { |
| 65 | connect(mImgurWrapper, &ImgurWrapper::tokenUpdated, this, &ImgurUploaderSettings::imgurTokenUpdated); |
| 66 | connect(mImgurWrapper, &ImgurWrapper::error, this, &ImgurUploaderSettings::imgurTokenError); |
| 67 | |
| 68 | mForceAnonymousCheckbox->setText(tr("Force anonymous upload")); |
| 69 | mOpenLinkInBrowserCheckbox->setText(tr("After uploading open Imgur link in default browser")); |
| 70 | mDirectLinkToImageCheckbox->setText(tr("Link directly to image")); |
| 71 | mAlwaysCopyToClipboardCheckBox->setText(tr("Always copy Imgur link to clipboard")); |
| 72 | |
| 73 | mUploadTitleLabel->setText(tr("Upload title:")); |
| 74 | mUploadDescriptionLabel->setText(tr("Upload description:")); |
| 75 | |
| 76 | mBaseUrlLabel->setText(tr("Base Url:")); |
| 77 | mBaseUrlLabel->setToolTip(tr("Base url that will be used for communication with Imgur.\n" |
| 78 | "Changing requires restart.")); |
| 79 | |
| 80 | mClientIdLineEdit->setPlaceholderText(tr("Client ID")); |
| 81 | connect(mClientIdLineEdit, &QLineEdit::textChanged, this, &ImgurUploaderSettings::imgurClientEntered); |
| 82 | |
| 83 | mClientSecretLineEdit->setPlaceholderText(tr("Client Secret")); |
| 84 | connect(mClientSecretLineEdit, &QLineEdit::textChanged, this, &ImgurUploaderSettings::imgurClientEntered); |
| 85 | |
| 86 | mPinLineEdit->setPlaceholderText(tr("PIN")); |
| 87 | mPinLineEdit->setToolTip(tr("Enter imgur Pin which will be exchanged for a token.")); |
| 88 | connect(mPinLineEdit, &QLineEdit::textChanged, [this](const QString & text) { |
| 89 | mGetTokenButton->setEnabled(text.length() > 8); |
| 90 | }); |
| 91 | |
| 92 | mUploadTitleEdit->setPlaceholderText(DefaultValues::ImgurUploadTitle); |
| 93 | mUploadDescriptionEdit->setPlaceholderText(DefaultValues::ImgurUploadDescription); |
| 94 | |
| 95 | mBaseUrlLineEdit->setPlaceholderText(DefaultValues::ImgurBaseUrl); |
| 96 | mBaseUrlLineEdit->setToolTip(mBaseUrlLabel->toolTip()); |
| 97 | |
| 98 | mUsernameLabel->setText(tr("Username") + QLatin1String(":")); |
| 99 | |
| 100 | mUsernameLineEdit->setReadOnly(true); |
| 101 | |
| 102 | connect(mUsernameLineEdit, &QLineEdit::textChanged, this, &ImgurUploaderSettings::usernameChanged); |
| 103 | |
| 104 | mGetPinButton->setText(tr("Get PIN")); |
| 105 | connect(mGetPinButton, &QPushButton::clicked, this, &ImgurUploaderSettings::requestImgurPin); |
| 106 | mGetPinButton->setEnabled(false); |
| 107 | |
| 108 | mGetTokenButton->setText(tr("Get Token")); |
| 109 | connect(mGetTokenButton, &QPushButton::clicked, this, &ImgurUploaderSettings::getImgurToken); |
| 110 | mGetTokenButton->setEnabled(false); |
| 111 | |
| 112 | mClearTokenButton->setText(tr("Clear Token")); |
| 113 | connect(mClearTokenButton, &QPushButton::clicked, this, &ImgurUploaderSettings::clearImgurToken); |
| 114 | |
| 115 | mHistoryButton->setText(tr("Imgur History")); |
| 116 | connect(mHistoryButton, &QPushButton::clicked, this, &ImgurUploaderSettings::showImgurHistoryDialog); |
| 117 | |
| 118 | mLayout->setAlignment(Qt::AlignTop); |
| 119 | mLayout->setColumnMinimumWidth(0, 10); |
| 120 | mLayout->addWidget(mForceAnonymousCheckbox, 0, 0, 1, 3); |
| 121 | mLayout->addWidget(mOpenLinkInBrowserCheckbox, 1, 0, 1, 3); |
| 122 | mLayout->addWidget(mDirectLinkToImageCheckbox, 2, 0, 1, 3); |
| 123 | mLayout->addWidget(mAlwaysCopyToClipboardCheckBox, 3, 0, 1, 3); |
| 124 | mLayout->setRowMinimumHeight(4, 15); |
| 125 | mLayout->addWidget(mUploadTitleLabel, 5, 0, 1, 1); |
| 126 | mLayout->addWidget(mUploadTitleEdit, 5, 1, 1, 2); |
| 127 | mLayout->addWidget(mUploadDescriptionLabel, 6, 0, 1, 1); |
| 128 | mLayout->addWidget(mUploadDescriptionEdit, 6, 1, 1, 2); |
| 129 | mLayout->addWidget(mBaseUrlLabel, 7, 0, 1, 1); |
| 130 | mLayout->addWidget(mBaseUrlLineEdit, 7, 1, 1, 2); |
| 131 | mLayout->setRowMinimumHeight(8, 15); |
| 132 | mLayout->addWidget(mUsernameLabel, 9, 0, 1, 1); |
| 133 | mLayout->addWidget(mUsernameLineEdit, 9, 1, 1, 2); |
| 134 | mLayout->addWidget(mClearTokenButton, 9, 3, 1, 1); |
| 135 | mLayout->addWidget(mClientIdLineEdit, 10, 0, 1, 3); |
| 136 | mLayout->addWidget(mClientSecretLineEdit, 11, 0, 1, 3); |
| 137 | mLayout->addWidget(mGetPinButton, 11, 3, 1, 1); |
| 138 | mLayout->addWidget(mPinLineEdit, 12, 0, 1, 3); |
| 139 | mLayout->addWidget(mGetTokenButton, 12, 3, 1, 1); |
| 140 | mLayout->addWidget(mHistoryButton, 13, 3, 1, 1); |
| 141 | |
| 142 | setTitle(tr("Imgur Uploader")); |
| 143 | setLayout(mLayout); |
| 144 | } |
| 145 | |
| 146 | void ImgurUploaderSettings::loadConfig() |
| 147 | { |
| 148 | mForceAnonymousCheckbox->setChecked(mConfig->imgurForceAnonymous()); |
| 149 | mOpenLinkInBrowserCheckbox->setChecked(mConfig->imgurOpenLinkInBrowser()); |
| 150 | mDirectLinkToImageCheckbox->setChecked(mConfig->imgurLinkDirectlyToImage()); |
| 151 | mAlwaysCopyToClipboardCheckBox->setChecked(mConfig->imgurAlwaysCopyToClipboard()); |
| 152 | mUploadTitleEdit->setText(mConfig->imgurUploadTitle()); |
| 153 | mUploadDescriptionEdit->setText(mConfig->imgurUploadDescription()); |
| 154 | mUsernameLineEdit->setText(mConfig->imgurUsername()); |
| 155 | mBaseUrlLineEdit->setText(mConfig->imgurBaseUrl()); |
| 156 | |
| 157 | if(!mConfig->imgurClientId().isEmpty()) { |
| 158 | mClientIdLineEdit->setPlaceholderText(mConfig->imgurClientId()); |
| 159 | } |
| 160 | |
| 161 | usernameChanged(); |
| 162 | } |
| 163 | |
| 164 | /* |
| 165 | * Based on the entered client id and client secret we create a pin request and open it up in the |
| 166 | * default browser. |
| 167 | */ |
| 168 | void ImgurUploaderSettings::requestImgurPin() |
| 169 | { |
| 170 | // Save client ID and Secret to config file |
| 171 | mConfig->setImgurClientId(mClientIdLineEdit->text().toUtf8()); |
| 172 | mConfig->setImgurClientSecret(mClientSecretLineEdit->text().toUtf8()); |
| 173 | |
| 174 | // Open the pin request in the default browser |
| 175 | QDesktopServices::openUrl(mImgurWrapper->pinRequestUrl(mClientIdLineEdit->text())); |
| 176 | |
| 177 | // Cleanup line edits |
| 178 | mClientIdLineEdit->setPlaceholderText(mClientIdLineEdit->text()); |
| 179 | mClientIdLineEdit->clear(); |
| 180 | |
| 181 | mClientSecretLineEdit->clear(); |
| 182 | } |
| 183 | |
| 184 | /* |
| 185 | * Request a new token from imgur.com when clicked. |
| 186 | */ |
| 187 | void ImgurUploaderSettings::getImgurToken() |
| 188 | { |
| 189 | mImgurWrapper->getAccessToken(mPinLineEdit->text().toUtf8(), |
| 190 | mConfig->imgurClientId(), |
| 191 | mConfig->imgurClientSecret()); |
| 192 | mPinLineEdit->clear(); |
| 193 | qInfo("%s", qPrintable(tr("Waiting for imgur.com…"))); |
| 194 | } |
| 195 | |
| 196 | void ImgurUploaderSettings::clearImgurToken() |
| 197 | { |
| 198 | mConfig->setImgurAccessToken({}); |
| 199 | mConfig->setImgurRefreshToken({}); |
| 200 | mConfig->setImgurUsername({}); |
| 201 | |
| 202 | mUsernameLineEdit->setText({}); |
| 203 | } |
| 204 | |
| 205 | void ImgurUploaderSettings::imgurClientEntered(const QString&) |
| 206 | { |
| 207 | mGetPinButton->setEnabled(!mClientIdLineEdit->text().isEmpty() && !mClientSecretLineEdit->text().isEmpty()); |
| 208 | } |
| 209 | |
| 210 | /* |
| 211 | * We have received a new token from imgur.com, now we save it to config for |
| 212 | * later use and inform the user about it. |
| 213 | */ |
| 214 | void ImgurUploaderSettings::imgurTokenUpdated(const QString& accessToken, const QString& refreshToken, const QString& username) |
| 215 | { |
| 216 | mConfig->setImgurAccessToken(accessToken.toUtf8()); |
| 217 | mConfig->setImgurRefreshToken(refreshToken.toUtf8()); |
| 218 | mConfig->setImgurUsername(username); |
| 219 | |
| 220 | mUsernameLineEdit->setText(username); |
| 221 | qInfo("%s", qPrintable(tr("Imgur.com token successfully updated."))); |
| 222 | } |
| 223 | |
| 224 | /* |
| 225 | * Something went wrong while requesting a new token, we write the message to |
| 226 | * shell. |
| 227 | */ |
| 228 | void ImgurUploaderSettings::imgurTokenError(QNetworkReply::NetworkError networkError, const QString& message) |
| 229 | { |
| 230 | Q_UNUSED(networkError); |
| 231 | qCritical("SettingsDialog returned error: '%s'", qPrintable(message)); |
| 232 | qInfo("%s", qPrintable(tr("Imgur.com token update error."))); |
| 233 | } |
| 234 | |
| 235 | void ImgurUploaderSettings::showImgurHistoryDialog() |
| 236 | { |
| 237 | ImgurHistoryDialog dialog; |
| 238 | dialog.exec(); |
| 239 | } |
| 240 | |
| 241 | void ImgurUploaderSettings::usernameChanged() |
| 242 | { |
| 243 | mClearTokenButton->setEnabled(!mUsernameLineEdit->text().isEmpty()); |
| 244 | } |
| 245 |