KIO

usernotificationhandler.cpp
1/*
2 This file is part of the KDE libraries
3 SPDX-FileCopyrightText: 2012 Dawit Alemayehu <adawit@kde.org>
4
5 SPDX-License-Identifier: LGPL-2.0-only
6*/
7
8#include "askuseractioninterface.h"
9#include "usernotificationhandler_p.h"
10
11#include "job_p.h"
12#include "kiocoredebug.h"
13#include "worker_p.h"
14#include "workerbase.h"
15
16#include <QTimer>
17
18using namespace KIO;
19
20QString UserNotificationHandler::Request::key() const
21{
22 QString key;
23 if (worker) {
24 key = worker->protocol();
25 key += worker->host();
26 key += worker->port();
27 key += QLatin1Char('-');
28 key += QChar(type);
29 }
30 return key;
31}
32
33UserNotificationHandler::UserNotificationHandler(QObject *parent)
34 : QObject(parent)
35{
36}
37
38UserNotificationHandler::~UserNotificationHandler()
39{
40 qDeleteAll(m_pendingRequests);
41}
42
43void UserNotificationHandler::requestMessageBox(WorkerInterface *iface, int type, const QHash<MessageBoxDataType, QVariant> &data)
44{
45 Request *r = new Request;
46 r->type = type;
47 r->worker = qobject_cast<KIO::Worker *>(iface);
48 r->data = data;
49
50 m_pendingRequests.append(r);
51 if (m_pendingRequests.count() == 1) {
52 QTimer::singleShot(0, this, &UserNotificationHandler::processRequest);
53 }
54}
55
56void UserNotificationHandler::processRequest()
57{
58 if (m_pendingRequests.isEmpty()) {
59 return;
60 }
61
62 int result = -1;
63 Request *r = m_pendingRequests.first();
64
65 if (r->worker) {
66 const QString key = r->key();
67
68 if (m_cachedResults.contains(key)) {
69 result = *(m_cachedResults[key]);
70 } else {
71 KIO::SimpleJob *job = r->worker->job();
73
74 if (askUserIface) {
75 connect(askUserIface, &AskUserActionInterface::messageBoxResult, this, &UserNotificationHandler::slotProcessRequest, Qt::UniqueConnection);
76
78 switch (r->type) {
83 case WorkerBase::WarningContinueCancel:
84 case WorkerBase::WarningContinueCancelDetailed:
85 return AskUserActionInterface::WarningContinueCancel;
88 case WorkerBase::Information:
89 return AskUserActionInterface::Information;
90 default:
91 Q_UNREACHABLE();
93 }
94 }();
95
96 askUserIface->requestUserMessageBox(type,
97 r->data.value(MSG_TEXT).toString(),
98 r->data.value(MSG_TITLE).toString(),
99 r->data.value(MSG_PRIMARYACTION_TEXT).toString(),
100 r->data.value(MSG_SECONDARYACTION_TEXT).toString(),
101 r->data.value(MSG_PRIMARYACTION_ICON).toString(),
102 r->data.value(MSG_SECONDARYACTION_ICON).toString(),
103 r->data.value(MSG_DONT_ASK_AGAIN).toString(),
104 r->data.value(MSG_DETAILS).toString());
105 return;
106 }
107 }
108 } else {
109 qCWarning(KIO_CORE) << "Cannot prompt user because the requesting KIO worker died!" << r->worker;
110 }
111
112 slotProcessRequest(result);
113}
114
115void UserNotificationHandler::slotProcessRequest(int result)
116{
117 Request *request = m_pendingRequests.takeFirst();
118 m_cachedResults.insert(request->key(), new int(result));
119
120 request->worker->sendMessageBoxAnswer(result);
121 delete request;
122
123 if (m_pendingRequests.isEmpty()) {
124 m_cachedResults.clear();
125 } else {
126 processRequest();
127 }
128}
129
130void UserNotificationHandler::sslError(WorkerInterface *iface, const QVariantMap &sslErrorData)
131{
132 KIO::SimpleJob *job = qobject_cast<KIO::Worker *>(iface)->job();
134
135 if (askUserIface) {
136 askUserIface->askIgnoreSslErrors(sslErrorData, nullptr);
137
138 connect(askUserIface, &AskUserActionInterface::askIgnoreSslErrorsResult, this, [iface](int result) {
139 iface->sendSslErrorAnswer(result);
140 });
141 }
142}
143#include "moc_usernotificationhandler_p.cpp"
The AskUserActionInterface class allows a KIO::Job to prompt the user for a decision when e....
void messageBoxResult(int result)
Implementations of this interface must emit this signal when the dialog invoked by requestUserMessage...
virtual void requestUserMessageBox(MessageDialogType type, const QString &text, const QString &title, const QString &primaryActionText, const QString &secondatyActionText, const QString &primaryActionIconName={}, const QString &secondatyActionIconName={}, const QString &dontAskAgainName={}, const QString &details={}, QWidget *parent=nullptr)=0
This function allows for the delegation of user prompts from the KIO worker.
A simple job (one url and one command).
A namespace for KIO globals.
T delegateExtension(KJob *job)
Returns the child of the job's uiDelegate() that implements the given extension, or nullptr if none w...
VehicleSection::Type type(QStringView coachNumber, QStringView coachClassification)
UniqueConnection
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:04:58 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.