Repository navigation
Expand file tree
/
Copy pathunsubscribeplugininterface.cpp
More file actions
206 lines (188 loc) · 6.17 KB
/
Copy pathunsubscribeplugininterface.cpp
File metadata and controls
206 lines (188 loc) · 6.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include "unsubscribe_debug.h"
#include "unsubscribeplugininterface.h"
#include <QMessageBox>
#include <KActionCollection>
#include <KLocalizedString>
#include <MessageCore/MailingList>
#include <PimCommon/NetworkManager>
#include <KIO/OpenUrlJob>
#include <KIO/JobUiDelegate>
#include <KIO/JobUiDelegateFactory>
using namespace MessageViewer;
// General yes/no confirm dialog
static bool
confirmDialog(const QString &text, const QString &question, bool safe)
{
QMessageBox msgBox;
msgBox.setText(text);
msgBox.setInformativeText(question);
msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
msgBox.setIcon(safe ? QMessageBox::Question : QMessageBox::Warning);
msgBox.setDefaultButton(safe ? QMessageBox::Yes : QMessageBox::No);
return msgBox.exec() == QMessageBox::Yes;
}
UnsubscribePluginInterface::UnsubscribePluginInterface(QWidget *parent, KActionCollection *ac)
: ViewerPluginInterface(parent),
mParent(parent)
{
if (ac)
{
// Create the action...
auto action = new QAction(this);
action->setIcon(QIcon::fromTheme(QStringLiteral("news-unsubscribe")));
action->setIconText(i18n("Unsubscribe"));
action->setWhatsThis(i18n("Allows you to unsubscribe from a mailing list, if the sender supports One-Click Unsubscribe"));
// ... and add it to the application's collection
ac->addAction(QStringLiteral("oneclick_unsubscribe"), action);
connect(action, &QAction::triggered, this, &UnsubscribePluginInterface::slotActivatePlugin);
// also add it to our list, for actions()
mActions.append(action);
}
connect(&mUnsub, &UnsubscribeManager::oneClickResult, this, &UnsubscribePluginInterface::getOneClickResult);
}
UnsubscribePluginInterface::~UnsubscribePluginInterface() = default;
QList<QAction *> UnsubscribePluginInterface::actions() const
{
return mActions;
}
void UnsubscribePluginInterface::closePlugin()
{
// Reset the UnsubscribeManager's state.
// XXX: Currently, this doesn't cancel any running One-Click Unsubscribe
// calls!
mUnsub.reset();
}
/// @brief Called when the action is clicked or otherwise activated
void UnsubscribePluginInterface::execute()
{
qCDebug(UnsubscribePlugin) << "-click!-";
// short-circuit if we're offline
if (!PimCommon::NetworkManager::self()->isOnline())
{
QMessageBox::critical(mParent,
i18n("Network not available"),
i18n("Please go back online to unsubscribe from this list."));
return;
}
if (mUnsub.hasMessage())
{
switch (mUnsub.unsubscribeStatus())
{
case UnsubscribeManager::None:
{
// Should not happen
QMessageBox::warning(mParent,
i18n("Can't Unsubscribe"),
i18n("This email doesn't advertise a way to unsubscribe."));
break;
}
break;
case UnsubscribeManager::NoOneClick:
{
// Load the unsubscribe URL normally
QUrl url = mUnsub.getUrl();
if (!url.isEmpty())
{
auto job = new KIO::OpenUrlJob(url);
job->setUiDelegate(KIO::createDefaultJobUiDelegate(KJobUiDelegate::AutoHandlingEnabled, mParent));
job->start();
}
break;
}
break;
case UnsubscribeManager::InvalidOneClick:
{
if (confirmDialog(
i18n("The digital signature of this email couldn't be validated."),
i18n("Do you still want to unsubscribe?"),
false))
{
mUnsub.doOneClick();
}
break;
}
case UnsubscribeManager::ValidOneClick:
{
if (confirmDialog(
i18n("This mailing list supports One-Click Unsubscribe."),
i18n("Do you want to unsubscribe?"),
true))
{
mUnsub.doOneClick();
}
break;
}
}
}
}
void UnsubscribePluginInterface::setMessageItem(const Akonadi::Item &item)
{
mUnsub.setMessageItem(item);
}
/// @brief Triggered when the selected item changes.
/// @param item The new item.
void UnsubscribePluginInterface::updateAction(const Akonadi::Item &item)
{
mUnsub.setMessageItem(item);
auto status = mUnsub.unsubscribeStatus();
QAction *action = (mActions.isEmpty()) ? nullptr : mActions.first();
QString caption;
switch (mUnsub.unsubscribeStatus())
{
case UnsubscribeManager::NoOneClick:
{
QString scheme = mUnsub.getUrl().scheme();
if (scheme.startsWith("http"))
{
caption = i18nc("unsubscribe via the web", "Web");
}
else if (scheme.startsWith("mailto"))
{
caption = i18nc("unsubscribe via email", "Email");
}
else
{
// Unknown, just set to
caption = scheme;
}
}
break;
case UnsubscribeManager::ValidOneClick:
case UnsubscribeManager::InvalidOneClick:
caption = i18nc("using RFC 8058 unsubscribe", "One-Click");
break;
case UnsubscribeManager::None:
default:
// Do nothing
break;
}
// Assuming we have an action, set the action up
if (action)
{
action->setDisabled(status == UnsubscribeManager::None);
if (caption.isEmpty())
{
action->setIconText(i18n("Unsubscribe"));
}
else
{
action->setIconText(i18nc("unsubscribe via %1", "Unsubscribe (%1)", caption));
}
}
}
void UnsubscribePluginInterface::getOneClickResult(bool isSuccess, const QString &resultString)
{
if (isSuccess)
{
QMessageBox::information(mParent,
i18n("Request Complete"),
i18n("The unsubscribe request was successfully sent."));
}
else
{
QMessageBox::critical(mParent,
i18n("Unsubscribe Error"),
resultString);
}
}
#include "moc_unsubscribeplugininterface.cpp"