| 1 | /* |
| 2 | This file is part of the KDE libraries |
| 3 | SPDX-FileCopyrightText: 2020 Ahmad Samir <a.samirh78@gmail.com> |
| 4 | |
| 5 | SPDX-License-Identifier: LGPL-2.0-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL |
| 6 | */ |
| 7 | |
| 8 | #include "widgetsaskuseractionhandler.h" |
| 9 | |
| 10 | #include <KConfig> |
| 11 | #include <KConfigGroup> |
| 12 | #include <KGuiItem> |
| 13 | #include <KIO/WorkerBase> |
| 14 | #include <KJob> |
| 15 | #include <KJobWidgets> |
| 16 | #include <KLocalizedString> |
| 17 | #include <KMessageDialog> |
| 18 | #include <KSharedConfig> |
| 19 | #include <KSslInfoDialog> |
| 20 | #include <KStandardGuiItem> |
| 21 | #include <kio_widgets_debug.h> |
| 22 | |
| 23 | #include <QApplication> |
| 24 | #include <QDialogButtonBox> |
| 25 | #include <QPointer> |
| 26 | #include <QRegularExpression> |
| 27 | #include <QUrl> |
| 28 | |
| 29 | class KIO::WidgetsAskUserActionHandlerPrivate |
| 30 | { |
| 31 | public: |
| 32 | explicit WidgetsAskUserActionHandlerPrivate(WidgetsAskUserActionHandler *qq) |
| 33 | : q(qq) |
| 34 | { |
| 35 | } |
| 36 | |
| 37 | // Creates a KSslInfoDialog or falls back to a generic Information dialog |
| 38 | void sslMessageBox(const QString &text, const KIO::MetaData &metaData, QWidget *parent); |
| 39 | |
| 40 | bool gotPersistentUserReply(KIO::AskUserActionInterface::MessageDialogType type, const KConfigGroup &cg, const QString &dontAskAgainName); |
| 41 | void savePersistentUserReply(KIO::AskUserActionInterface::MessageDialogType type, KConfigGroup &cg, const QString &dontAskAgainName, int result); |
| 42 | |
| 43 | WidgetsAskUserActionHandler *const q; |
| 44 | QPointer<QWidget> m_parentWidget = nullptr; |
| 45 | |
| 46 | QWidget *getParentWidget(KJob *job); |
| 47 | QWidget *getParentWidget(QWidget *widget); |
| 48 | }; |
| 49 | |
| 50 | bool KIO::WidgetsAskUserActionHandlerPrivate::gotPersistentUserReply(KIO::AskUserActionInterface::MessageDialogType type, |
| 51 | const KConfigGroup &cg, |
| 52 | const QString &dontAskAgainName) |
| 53 | { |
| 54 | // storage values matching the logic of FrameworkIntegration's KMessageBoxDontAskAgainConfigStorage |
| 55 | switch (type) { |
| 56 | case KIO::AskUserActionInterface::QuestionTwoActions: |
| 57 | case KIO::AskUserActionInterface::QuestionTwoActionsCancel: |
| 58 | case KIO::AskUserActionInterface::WarningTwoActions: |
| 59 | case KIO::AskUserActionInterface::WarningTwoActionsCancel: { |
| 60 | // storage holds "true" if persistent reply is "Yes", "false" for persistent "No", |
| 61 | // otherwise no persistent reply is present |
| 62 | const QString value = cg.readEntry(key: dontAskAgainName, aDefault: QString()); |
| 63 | if ((value.compare(other: QLatin1String("yes" ), cs: Qt::CaseInsensitive) == 0) || (value.compare(other: QLatin1String("true" ), cs: Qt::CaseInsensitive) == 0)) { |
| 64 | Q_EMIT q->messageBoxResult(result: KIO::WorkerBase::PrimaryAction); |
| 65 | return true; |
| 66 | } |
| 67 | if ((value.compare(other: QLatin1String("no" ), cs: Qt::CaseInsensitive) == 0) || (value.compare(other: QLatin1String("false" ), cs: Qt::CaseInsensitive) == 0)) { |
| 68 | Q_EMIT q->messageBoxResult(result: KIO::WorkerBase::SecondaryAction); |
| 69 | return true; |
| 70 | } |
| 71 | break; |
| 72 | } |
| 73 | case KIO::AskUserActionInterface::WarningContinueCancel: { |
| 74 | // storage holds "false" if persistent reply is "Continue" |
| 75 | // otherwise no persistent reply is present |
| 76 | const bool value = cg.readEntry(key: dontAskAgainName, aDefault: true); |
| 77 | if (value == false) { |
| 78 | Q_EMIT q->messageBoxResult(result: KIO::WorkerBase::Continue); |
| 79 | return true; |
| 80 | } |
| 81 | break; |
| 82 | } |
| 83 | default: |
| 84 | break; |
| 85 | } |
| 86 | |
| 87 | return false; |
| 88 | } |
| 89 | |
| 90 | void KIO::WidgetsAskUserActionHandlerPrivate::savePersistentUserReply(KIO::AskUserActionInterface::MessageDialogType type, |
| 91 | KConfigGroup &cg, |
| 92 | const QString &dontAskAgainName, |
| 93 | int result) |
| 94 | { |
| 95 | // see gotPersistentUserReply for values stored and why |
| 96 | switch (type) { |
| 97 | case KIO::AskUserActionInterface::QuestionTwoActions: |
| 98 | case KIO::AskUserActionInterface::QuestionTwoActionsCancel: |
| 99 | case KIO::AskUserActionInterface::WarningTwoActions: |
| 100 | case KIO::AskUserActionInterface::WarningTwoActionsCancel: |
| 101 | cg.writeEntry(key: dontAskAgainName, value: result == KIO::WorkerBase::PrimaryAction); |
| 102 | cg.sync(); |
| 103 | break; |
| 104 | case KIO::AskUserActionInterface::WarningContinueCancel: |
| 105 | cg.writeEntry(key: dontAskAgainName, value: false); |
| 106 | cg.sync(); |
| 107 | break; |
| 108 | default: |
| 109 | break; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | QWidget *KIO::WidgetsAskUserActionHandlerPrivate::getParentWidget(KJob *job) |
| 114 | { |
| 115 | QWidget *parentWidget = nullptr; |
| 116 | |
| 117 | if (job) { |
| 118 | auto parentWindow = KJobWidgets::window(job); |
| 119 | if (parentWindow) { |
| 120 | parentWidget = parentWindow; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | return getParentWidget(widget: parentWidget); |
| 125 | } |
| 126 | |
| 127 | QWidget *KIO::WidgetsAskUserActionHandlerPrivate::getParentWidget(QWidget *widget) |
| 128 | { |
| 129 | QWidget *parentWidget = widget; |
| 130 | |
| 131 | if (!parentWidget) { |
| 132 | parentWidget = this->m_parentWidget; |
| 133 | } |
| 134 | |
| 135 | if (!parentWidget) { |
| 136 | parentWidget = qApp->activeWindow(); |
| 137 | } |
| 138 | |
| 139 | return parentWidget; |
| 140 | } |
| 141 | |
| 142 | KIO::WidgetsAskUserActionHandler::WidgetsAskUserActionHandler(QObject *parent) |
| 143 | : KIO::AskUserActionInterface(parent) |
| 144 | , d(new WidgetsAskUserActionHandlerPrivate(this)) |
| 145 | { |
| 146 | } |
| 147 | |
| 148 | KIO::WidgetsAskUserActionHandler::~WidgetsAskUserActionHandler() |
| 149 | { |
| 150 | } |
| 151 | |
| 152 | void KIO::WidgetsAskUserActionHandler::askUserRename(KJob *job, |
| 153 | const QString &title, |
| 154 | const QUrl &src, |
| 155 | const QUrl &dest, |
| 156 | KIO::RenameDialog_Options options, |
| 157 | KIO::filesize_t sizeSrc, |
| 158 | KIO::filesize_t sizeDest, |
| 159 | const QDateTime &ctimeSrc, |
| 160 | const QDateTime &ctimeDest, |
| 161 | const QDateTime &mtimeSrc, |
| 162 | const QDateTime &mtimeDest) |
| 163 | { |
| 164 | QMetaObject::invokeMethod(qGuiApp, function: [=, this] { |
| 165 | auto *dlg = new KIO::RenameDialog(d->getParentWidget(job), title, src, dest, options, sizeSrc, sizeDest, ctimeSrc, ctimeDest, mtimeSrc, mtimeDest); |
| 166 | |
| 167 | dlg->setAttribute(Qt::WA_DeleteOnClose); |
| 168 | dlg->setWindowModality(Qt::WindowModal); |
| 169 | |
| 170 | connect(sender: job, signal: &KJob::finished, context: dlg, slot: &QDialog::reject); |
| 171 | connect(sender: dlg, signal: &QDialog::finished, context: this, slot: [this, job, dlg](const int exitCode) { |
| 172 | KIO::RenameDialog_Result result = static_cast<RenameDialog_Result>(exitCode); |
| 173 | const QUrl newUrl = result == Result_AutoRename ? dlg->autoDestUrl() : dlg->newDestUrl(); |
| 174 | Q_EMIT askUserRenameResult(result, newUrl, parentJob: job); |
| 175 | }); |
| 176 | |
| 177 | dlg->show(); |
| 178 | }); |
| 179 | } |
| 180 | |
| 181 | void KIO::WidgetsAskUserActionHandler::askUserSkip(KJob *job, KIO::SkipDialog_Options options, const QString &errorText) |
| 182 | { |
| 183 | QMetaObject::invokeMethod(qGuiApp, function: [=, this] { |
| 184 | auto *dlg = new KIO::SkipDialog(d->getParentWidget(job), options, errorText); |
| 185 | dlg->setAttribute(Qt::WA_DeleteOnClose); |
| 186 | dlg->setWindowModality(Qt::WindowModal); |
| 187 | |
| 188 | connect(sender: job, signal: &KJob::finished, context: dlg, slot: &QDialog::reject); |
| 189 | connect(sender: dlg, signal: &QDialog::finished, context: this, slot: [this, job](const int exitCode) { |
| 190 | Q_EMIT askUserSkipResult(result: static_cast<KIO::SkipDialog_Result>(exitCode), parentJob: job); |
| 191 | }); |
| 192 | |
| 193 | dlg->show(); |
| 194 | }); |
| 195 | } |
| 196 | |
| 197 | struct ProcessAskDeleteResult { |
| 198 | QStringList prettyList; |
| 199 | KMessageDialog::Type dialogType = KMessageDialog::QuestionTwoActions; |
| 200 | KGuiItem acceptButton; |
| 201 | QString text; |
| 202 | QIcon icon; |
| 203 | QString title = i18n("Delete Permanently" ); |
| 204 | bool isSingleUrl = false; |
| 205 | }; |
| 206 | |
| 207 | using AskIface = KIO::AskUserActionInterface; |
| 208 | static ProcessAskDeleteResult processAskDelete(const QList<QUrl> &urls, AskIface::DeletionType deletionType) |
| 209 | { |
| 210 | ProcessAskDeleteResult res; |
| 211 | res.prettyList.reserve(asize: urls.size()); |
| 212 | std::transform(first: urls.cbegin(), last: urls.cend(), result: std::back_inserter(x&: res.prettyList), unary_op: [](const auto &url) { |
| 213 | if (url.scheme() == QLatin1String("trash" )) { |
| 214 | QString path = url.path(); |
| 215 | // HACK (#98983): remove "0-foo". Note that it works better than |
| 216 | // displaying KFileItem::name(), for files under a subdir. |
| 217 | static const QRegularExpression re(QStringLiteral("^/[0-9]+-" )); |
| 218 | path.remove(re); |
| 219 | return path; |
| 220 | } else { |
| 221 | return url.toDisplayString(QUrl::PreferLocalFile); |
| 222 | } |
| 223 | }); |
| 224 | |
| 225 | const int urlCount = res.prettyList.size(); |
| 226 | res.isSingleUrl = urlCount == 1; |
| 227 | |
| 228 | switch (deletionType) { |
| 229 | case AskIface::Delete: { |
| 230 | res.dialogType = KMessageDialog::QuestionTwoActions; // Using Question* so the Delete button is pre-selected. Bug 462845 |
| 231 | res.icon = QIcon::fromTheme(QStringLiteral("dialog-warning" )); |
| 232 | if (res.isSingleUrl) { |
| 233 | res.text = xi18nc("@info" , |
| 234 | "Do you really want to permanently delete this item?<nl/><nl/>" |
| 235 | "<filename>%1</filename><nl/><nl/>" |
| 236 | "<emphasis strong='true'>This action cannot be undone.</emphasis>" , |
| 237 | res.prettyList.at(0)); |
| 238 | } else { |
| 239 | res.text = xi18ncp("@info" , |
| 240 | "Do you really want to permanently delete this %1 item?<nl/><nl/>" |
| 241 | "<emphasis strong='true'>This action cannot be undone.</emphasis>" , |
| 242 | "Do you really want to permanently delete these %1 items?<nl/><nl/>" |
| 243 | "<emphasis strong='true'>This action cannot be undone.</emphasis>" , |
| 244 | urlCount); |
| 245 | } |
| 246 | res.acceptButton = KGuiItem(i18nc("@action:button" , "Delete Permanently" ), QStringLiteral("edit-delete" )); |
| 247 | break; |
| 248 | } |
| 249 | case AskIface::DeleteInsteadOfTrash: { |
| 250 | res.dialogType = KMessageDialog::WarningTwoActions; |
| 251 | if (res.isSingleUrl) { |
| 252 | res.text = xi18nc("@info" , |
| 253 | "The trash is too full to accept this file." |
| 254 | " Permanently delete it instead?<nl/><nl/>" |
| 255 | "<filename>%1</filename><nl/><nl/>" |
| 256 | "<emphasis strong='true'>This action cannot be undone.</emphasis>" , |
| 257 | res.prettyList.at(0)); |
| 258 | } else { |
| 259 | res.text = xi18ncp("@info" , |
| 260 | "The trash is too full to accept this %1 file." |
| 261 | " Permanently delete it instead?<nl/>" |
| 262 | "<emphasis strong='true'>This action cannot be undone.</emphasis>" , |
| 263 | "The trash is too full to accept these %1 files." |
| 264 | " Permanently delete them instead?<nl/><nl/>" |
| 265 | "<emphasis strong='true'>This action cannot be undone.</emphasis>" , |
| 266 | urlCount); |
| 267 | } |
| 268 | res.acceptButton = KGuiItem(i18nc("@action:button" , "Delete Permanently" ), QStringLiteral("edit-delete" )); |
| 269 | break; |
| 270 | } |
| 271 | case AskIface::EmptyTrash: { |
| 272 | res.dialogType = KMessageDialog::QuestionTwoActions; // Using Question* so the Delete button is pre-selected. |
| 273 | res.icon = QIcon::fromTheme(QStringLiteral("dialog-warning" )); |
| 274 | res.text = xi18nc("@info" , |
| 275 | "Do you want to permanently delete all items from the Trash?<nl/><nl/>" |
| 276 | "<emphasis strong='true'>This action cannot be undone.</emphasis>" ); |
| 277 | res.acceptButton = KGuiItem(i18nc("@action:button" , "Empty Trash" ), QStringLiteral("edit-delete" )); |
| 278 | break; |
| 279 | } |
| 280 | case AskIface::Trash: { |
| 281 | if (res.isSingleUrl) { |
| 282 | res.text = xi18nc("@info" , |
| 283 | "Do you really want to move this item to the Trash?<nl/>" |
| 284 | "<filename>%1</filename>" , |
| 285 | res.prettyList.at(0)); |
| 286 | } else { |
| 287 | res.text = |
| 288 | xi18ncp("@info" , "Do you really want to move this %1 item to the Trash?" , "Do you really want to move these %1 items to the Trash?" , urlCount); |
| 289 | } |
| 290 | res.title = i18n("Move to Trash" ); |
| 291 | res.acceptButton = KGuiItem(res.title, QStringLiteral("user-trash" )); |
| 292 | break; |
| 293 | } |
| 294 | default: |
| 295 | break; |
| 296 | } |
| 297 | return res; |
| 298 | } |
| 299 | |
| 300 | void KIO::WidgetsAskUserActionHandler::askUserDelete(const QList<QUrl> &urls, DeletionType deletionType, ConfirmationType confirmationType, QWidget *parent) |
| 301 | { |
| 302 | QString keyName; |
| 303 | bool ask = (confirmationType == ForceConfirmation); |
| 304 | if (!ask) { |
| 305 | // The default value for confirmations is true for delete and false |
| 306 | // for trash. If you change this, please also update: |
| 307 | // dolphin/src/settings/general/confirmationssettingspage.cpp |
| 308 | bool defaultValue = true; |
| 309 | |
| 310 | switch (deletionType) { |
| 311 | case DeleteInsteadOfTrash: |
| 312 | case Delete: |
| 313 | keyName = QStringLiteral("ConfirmDelete" ); |
| 314 | break; |
| 315 | case Trash: |
| 316 | keyName = QStringLiteral("ConfirmTrash" ); |
| 317 | defaultValue = false; |
| 318 | break; |
| 319 | case EmptyTrash: |
| 320 | keyName = QStringLiteral("ConfirmEmptyTrash" ); |
| 321 | break; |
| 322 | } |
| 323 | |
| 324 | KSharedConfigPtr kioConfig = KSharedConfig::openConfig(QStringLiteral("kiorc" ), mode: KConfig::NoGlobals); |
| 325 | ask = kioConfig->group(QStringLiteral("Confirmations" )).readEntry(key: keyName, aDefault: defaultValue); |
| 326 | } |
| 327 | |
| 328 | if (!ask) { |
| 329 | Q_EMIT askUserDeleteResult(allowDelete: true, urls, deletionType, parent); |
| 330 | return; |
| 331 | } |
| 332 | |
| 333 | QMetaObject::invokeMethod(qGuiApp, function: [=, this] { |
| 334 | const auto &[prettyList, dialogType, acceptButton, text, icon, title, singleUrl] = processAskDelete(urls, deletionType); |
| 335 | KMessageDialog *dlg = new KMessageDialog(dialogType, text, parent); |
| 336 | dlg->setAttribute(Qt::WA_DeleteOnClose); |
| 337 | dlg->setCaption(title); |
| 338 | dlg->setIcon(icon); |
| 339 | dlg->setButtons(primaryAction: acceptButton, secondaryAction: KStandardGuiItem::cancel()); |
| 340 | if (!singleUrl) { |
| 341 | dlg->setListWidgetItems(prettyList); |
| 342 | } |
| 343 | dlg->setDontAskAgainText(i18nc("@option:checkbox" , "Do not ask again" )); |
| 344 | // If we get here, !ask must be false |
| 345 | dlg->setDontAskAgainChecked(!ask); |
| 346 | |
| 347 | connect(sender: dlg, signal: &QDialog::finished, context: this, slot: [=, this](const int buttonCode) { |
| 348 | const bool isDelete = (buttonCode == KMessageDialog::PrimaryAction); |
| 349 | |
| 350 | Q_EMIT askUserDeleteResult(allowDelete: isDelete, urls, deletionType, parent); |
| 351 | |
| 352 | if (isDelete) { |
| 353 | KSharedConfigPtr kioConfig = KSharedConfig::openConfig(QStringLiteral("kiorc" ), mode: KConfig::NoGlobals); |
| 354 | KConfigGroup cg = kioConfig->group(QStringLiteral("Confirmations" )); |
| 355 | cg.writeEntry(key: keyName, value: !dlg->isDontAskAgainChecked()); |
| 356 | cg.sync(); |
| 357 | } |
| 358 | }); |
| 359 | |
| 360 | dlg->setWindowModality(Qt::WindowModal); |
| 361 | dlg->show(); |
| 362 | }); |
| 363 | } |
| 364 | |
| 365 | void KIO::WidgetsAskUserActionHandler::requestUserMessageBox(MessageDialogType type, |
| 366 | const QString &text, |
| 367 | const QString &title, |
| 368 | const QString &primaryActionText, |
| 369 | const QString &secondaryActionText, |
| 370 | const QString &primaryActionIconName, |
| 371 | const QString &secondaryActionIconName, |
| 372 | const QString &dontAskAgainName, |
| 373 | const QString &details, |
| 374 | QWidget *parent) |
| 375 | { |
| 376 | if (d->gotPersistentUserReply(type, |
| 377 | cg: KSharedConfig::openConfig(QStringLiteral("kioslaverc" ))->group(QStringLiteral("Notification Messages" )), |
| 378 | dontAskAgainName)) { |
| 379 | return; |
| 380 | } |
| 381 | |
| 382 | const KGuiItem primaryActionButton(primaryActionText, primaryActionIconName); |
| 383 | const KGuiItem secondaryActionButton(secondaryActionText, secondaryActionIconName); |
| 384 | |
| 385 | // It's "Do not ask again" every where except with Information |
| 386 | QString dontAskAgainText = i18nc("@option:check" , "Do not ask again" ); |
| 387 | |
| 388 | KMessageDialog::Type dlgType; |
| 389 | bool hasCancelButton = false; |
| 390 | |
| 391 | switch (type) { |
| 392 | case AskUserActionInterface::QuestionTwoActions: |
| 393 | dlgType = KMessageDialog::QuestionTwoActions; |
| 394 | break; |
| 395 | case AskUserActionInterface::QuestionTwoActionsCancel: |
| 396 | dlgType = KMessageDialog::QuestionTwoActionsCancel; |
| 397 | hasCancelButton = true; |
| 398 | break; |
| 399 | case AskUserActionInterface::WarningTwoActions: |
| 400 | dlgType = KMessageDialog::WarningTwoActions; |
| 401 | break; |
| 402 | case AskUserActionInterface::WarningTwoActionsCancel: |
| 403 | dlgType = KMessageDialog::WarningTwoActionsCancel; |
| 404 | hasCancelButton = true; |
| 405 | break; |
| 406 | case AskUserActionInterface::WarningContinueCancel: |
| 407 | dlgType = KMessageDialog::WarningContinueCancel; |
| 408 | hasCancelButton = true; |
| 409 | break; |
| 410 | case AskUserActionInterface::Information: |
| 411 | dlgType = KMessageDialog::Information; |
| 412 | dontAskAgainText = i18nc("@option:check" , "Do not show this message again" ); |
| 413 | break; |
| 414 | case AskUserActionInterface::Error: |
| 415 | dlgType = KMessageDialog::Error; |
| 416 | dontAskAgainText = QString{}; // No dontAskAgain checkbox |
| 417 | break; |
| 418 | default: |
| 419 | qCWarning(KIO_WIDGETS) << "Unknown message dialog type" << type; |
| 420 | return; |
| 421 | } |
| 422 | |
| 423 | QMetaObject::invokeMethod(qGuiApp, function: [=, this]() { |
| 424 | auto cancelButton = hasCancelButton ? KStandardGuiItem::cancel() : KGuiItem(); |
| 425 | auto *dialog = new KMessageDialog(dlgType, text, d->getParentWidget(widget: parent)); |
| 426 | |
| 427 | dialog->setAttribute(Qt::WA_DeleteOnClose); |
| 428 | dialog->setCaption(title); |
| 429 | dialog->setIcon(QIcon{}); |
| 430 | dialog->setButtons(primaryAction: primaryActionButton, secondaryAction: secondaryActionButton, cancelAction: cancelButton); |
| 431 | dialog->setDetails(details); |
| 432 | dialog->setDontAskAgainText(dontAskAgainText); |
| 433 | dialog->setDontAskAgainChecked(false); |
| 434 | dialog->setOpenExternalLinks(true); // Allow opening external links in the text labels |
| 435 | |
| 436 | connect(sender: dialog, signal: &QDialog::finished, context: this, slot: [=, this](const int result) { |
| 437 | KIO::WorkerBase::ButtonCode btnCode; |
| 438 | switch (result) { |
| 439 | case KMessageDialog::PrimaryAction: |
| 440 | if (dlgType == KMessageDialog::WarningContinueCancel) { |
| 441 | btnCode = KIO::WorkerBase::Continue; |
| 442 | } else { |
| 443 | btnCode = KIO::WorkerBase::PrimaryAction; |
| 444 | } |
| 445 | break; |
| 446 | case KMessageDialog::SecondaryAction: |
| 447 | btnCode = KIO::WorkerBase::SecondaryAction; |
| 448 | break; |
| 449 | case KMessageDialog::Cancel: |
| 450 | btnCode = KIO::WorkerBase::Cancel; |
| 451 | break; |
| 452 | case KMessageDialog::Ok: |
| 453 | btnCode = KIO::WorkerBase::Ok; |
| 454 | break; |
| 455 | default: |
| 456 | qCWarning(KIO_WIDGETS) << "Unknown message dialog result" << result; |
| 457 | return; |
| 458 | } |
| 459 | |
| 460 | Q_EMIT messageBoxResult(result: btnCode); |
| 461 | |
| 462 | if ((result != KMessageDialog::Cancel) && dialog->isDontAskAgainChecked()) { |
| 463 | KSharedConfigPtr reqMsgConfig = KSharedConfig::openConfig(QStringLiteral("kioslaverc" )); |
| 464 | KConfigGroup cg = reqMsgConfig->group(QStringLiteral("Notification Messages" )); |
| 465 | d->savePersistentUserReply(type, cg, dontAskAgainName, result); |
| 466 | } |
| 467 | }); |
| 468 | |
| 469 | dialog->show(); |
| 470 | }); |
| 471 | } |
| 472 | |
| 473 | void KIO::WidgetsAskUserActionHandler::setWindow(QWidget *window) |
| 474 | { |
| 475 | d->m_parentWidget = window; |
| 476 | } |
| 477 | |
| 478 | void KIO::WidgetsAskUserActionHandler::askIgnoreSslErrors(const QVariantMap &sslErrorData, QWidget *parent) |
| 479 | { |
| 480 | QWidget *parentWidget = d->getParentWidget(widget: parent); |
| 481 | |
| 482 | QString message = i18n("The server failed the authenticity check (%1).\n\n" , sslErrorData[QLatin1String("hostname" )].toString()); |
| 483 | |
| 484 | message += sslErrorData[QLatin1String("sslError" )].toString(); |
| 485 | |
| 486 | auto *dialog = new KMessageDialog(KMessageDialog::WarningTwoActionsCancel, message, parentWidget); |
| 487 | |
| 488 | dialog->setAttribute(Qt::WA_DeleteOnClose); |
| 489 | dialog->setCaption(i18n("Server Authentication" )); |
| 490 | dialog->setIcon(QIcon{}); |
| 491 | dialog->setButtons(primaryAction: KGuiItem{i18n("&Details" ), QStringLiteral("documentinfo" )}, secondaryAction: KStandardGuiItem::cont(), cancelAction: KStandardGuiItem::cancel()); |
| 492 | |
| 493 | connect(sender: dialog, signal: &KMessageDialog::finished, context: this, slot: [this, parentWidget, sslErrorData](int result) { |
| 494 | if (result == KMessageDialog::PrimaryAction) { |
| 495 | showSslDetails(sslErrorData, parentWidget); |
| 496 | } else if (result == KMessageDialog::SecondaryAction) { |
| 497 | // continue(); |
| 498 | Q_EMIT askIgnoreSslErrorsResult(result: 1); |
| 499 | } else if (result == KMessageDialog::Cancel) { |
| 500 | // cancel(); |
| 501 | Q_EMIT askIgnoreSslErrorsResult(result: 0); |
| 502 | } |
| 503 | }); |
| 504 | |
| 505 | dialog->show(); |
| 506 | } |
| 507 | |
| 508 | void KIO::WidgetsAskUserActionHandler::showSslDetails(const QVariantMap &sslErrorData, QWidget *parentWidget) |
| 509 | { |
| 510 | const QStringList sslList = sslErrorData[QLatin1String("peerCertChain" )].toStringList(); |
| 511 | |
| 512 | QList<QSslCertificate> certChain; |
| 513 | bool decodedOk = true; |
| 514 | for (const QString &str : sslList) { |
| 515 | certChain.append(t: QSslCertificate(str.toUtf8())); |
| 516 | if (certChain.last().isNull()) { |
| 517 | decodedOk = false; |
| 518 | break; |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | QMetaObject::invokeMethod(qGuiApp, function: [=, this] { |
| 523 | if (decodedOk) { // Use KSslInfoDialog |
| 524 | KSslInfoDialog *ksslDlg = new KSslInfoDialog(parentWidget); |
| 525 | ksslDlg->setSslInfo( |
| 526 | certificateChain: certChain, |
| 527 | ip: QString(), |
| 528 | host: sslErrorData[QLatin1String("hostname" )].toString(), |
| 529 | sslProtocol: sslErrorData[QLatin1String("protocol" )].toString(), |
| 530 | cipher: sslErrorData[QLatin1String("cipher" )].toString(), |
| 531 | usedBits: sslErrorData[QLatin1String("usedBits" )].toInt(), |
| 532 | bits: sslErrorData[QLatin1String("bits" )].toInt(), |
| 533 | validationErrors: KSslInfoDialog::certificateErrorsFromString(errorsString: sslErrorData[QLatin1String("certificateErrors" )].toStringList().join(sep: QLatin1Char('\n')))); |
| 534 | |
| 535 | // KSslInfoDialog deletes itself by setting Qt::WA_DeleteOnClose |
| 536 | |
| 537 | QObject::connect(sender: ksslDlg, signal: &QDialog::finished, context: this, slot: [this, sslErrorData, parentWidget]() { |
| 538 | // KSslInfoDialog only has one button, QDialogButtonBox::Close |
| 539 | askIgnoreSslErrors(sslErrorData, parent: parentWidget); |
| 540 | }); |
| 541 | |
| 542 | ksslDlg->show(); |
| 543 | return; |
| 544 | } |
| 545 | |
| 546 | // Fallback to a generic message box |
| 547 | auto *dialog = new KMessageDialog(KMessageDialog::Information, i18n("The peer SSL certificate chain appears to be corrupt." ), parentWidget); |
| 548 | |
| 549 | dialog->setAttribute(Qt::WA_DeleteOnClose); |
| 550 | dialog->setCaption(i18n("SSL" )); |
| 551 | dialog->setButtons(primaryAction: KStandardGuiItem::ok()); |
| 552 | |
| 553 | QObject::connect(sender: dialog, signal: &QDialog::finished, context: this, slot: [this](const int result) { |
| 554 | Q_EMIT askIgnoreSslErrorsResult(result: result == KMessageDialog::Ok ? 1 : 0); |
| 555 | }); |
| 556 | |
| 557 | dialog->show(); |
| 558 | }); |
| 559 | } |
| 560 | |
| 561 | #include "moc_widgetsaskuseractionhandler.cpp" |
| 562 | |