Pimcommon

shareserviceurlmanager.cpp
1/*
2 SPDX-FileCopyrightText: 2015-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "shareserviceurlmanager.h"
8
9#include "pimcommon_debug.h"
10#include <KActionMenu>
11#include <KLocalizedString>
12#include <QDesktopServices>
13#include <QMenu>
14#include <QUrlQuery>
15
16using namespace PimCommon;
17
18class PimCommon::ShareServiceUrlManagerPrivate
19{
20public:
21 explicit ShareServiceUrlManagerPrivate(ShareServiceUrlManager *qq)
22 : q(qq)
23 {
24 }
25
26 [[nodiscard]] QIcon typeToIcon(ShareServiceUrlManager::ServiceType type) const;
27 [[nodiscard]] QString typeToI18n(ShareServiceUrlManager::ServiceType type) const;
28 void initializeMenu();
29 KActionMenu *mMenu = nullptr;
31};
32
33void ShareServiceUrlManagerPrivate::initializeMenu()
34{
35 mMenu = new KActionMenu(QIcon::fromTheme(QStringLiteral("document-share")), i18n("Share On..."), q);
36 for (int i = 0; i < ShareServiceUrlManager::ServiceEndType; ++i) {
37 const auto type = static_cast<ShareServiceUrlManager::ServiceType>(i);
38 auto action = new QAction(i18nc("@action:inmenu Share On...", "%1", typeToI18n(type)), q);
39 action->setData(QVariant::fromValue(type));
40 const QIcon icon = typeToIcon(type);
41 if (!icon.isNull()) {
42 action->setIcon(icon);
43 }
44 mMenu->addAction(action);
45 }
46 q->connect(mMenu->menu(), &QMenu::triggered, q, &ShareServiceUrlManager::slotSelectServiceUrl);
47}
48
49QIcon ShareServiceUrlManagerPrivate::typeToIcon(ShareServiceUrlManager::ServiceType type) const
50{
51 QIcon icon;
52 switch (type) {
53 case ShareServiceUrlManager::Fbook:
54 icon = QIcon::fromTheme(QStringLiteral("im-facebook"));
55 break;
56 case ShareServiceUrlManager::Twitter:
57 icon = QIcon::fromTheme(QStringLiteral("im-twitter"));
58 break;
59 case ShareServiceUrlManager::MailTo:
60 icon = QIcon::fromTheme(QStringLiteral("kmail"));
61 break;
62 case ShareServiceUrlManager::LinkedIn:
63 break;
64 case ShareServiceUrlManager::Evernote:
65 break;
66 case ShareServiceUrlManager::Pocket:
67 break;
68 case ShareServiceUrlManager::LiveJournal:
69 break;
70 case ShareServiceUrlManager::ServiceEndType:
71 qCWarning(PIMCOMMON_LOG) << "icon for unknown type requested. It's a bug.";
72 break;
73 }
74
75 return icon;
76}
77
78QString ShareServiceUrlManagerPrivate::typeToI18n(ShareServiceUrlManager::ServiceType type) const
79{
80 QString str;
81 switch (type) {
82 case ShareServiceUrlManager::Fbook:
83 str = i18n("Facebook");
84 break;
85 case ShareServiceUrlManager::Twitter:
86 str = i18n("Twitter");
87 break;
88 case ShareServiceUrlManager::MailTo:
89 str = i18n("Mail");
90 break;
91 case ShareServiceUrlManager::LinkedIn:
92 str = i18n("LinkedIn");
93 break;
94 case ShareServiceUrlManager::Evernote:
95 str = i18n("Evernote");
96 break;
97 case ShareServiceUrlManager::Pocket:
98 str = i18n("Pocket");
99 break;
100 case ShareServiceUrlManager::LiveJournal:
101 str = i18n("LiveJournal");
102 break;
103 case ShareServiceUrlManager::ServiceEndType:
104 qCWarning(PIMCOMMON_LOG) << "text for unknown type requested. It's a bug.";
105 break;
106 }
107 return str;
108}
109
110ShareServiceUrlManager::ShareServiceUrlManager(QObject *parent)
111 : QObject(parent)
112 , d(new PimCommon::ShareServiceUrlManagerPrivate(this))
113{
114 d->initializeMenu();
115}
116
117ShareServiceUrlManager::~ShareServiceUrlManager() = default;
118
119KActionMenu *ShareServiceUrlManager::menu() const
120{
121 return d->mMenu;
122}
123
124void ShareServiceUrlManager::slotSelectServiceUrl(QAction *act)
125{
126 if (act) {
127 const auto type = act->data().value<PimCommon::ShareServiceUrlManager::ServiceType>();
128 Q_EMIT serviceUrlSelected(type);
129 }
130}
131
132QUrl ShareServiceUrlManager::generateServiceUrl(const QString &link, const QString &title, ServiceType type)
133{
134 QUrl url;
135 if (link.isEmpty()) {
136 return url;
137 }
138 switch (type) {
139 case Fbook: {
140 url.setUrl(QStringLiteral("https://www.facebook.com/sharer.php"));
141 QUrlQuery urlQuery;
142 urlQuery.addQueryItem(QStringLiteral("u"), link);
143 urlQuery.addQueryItem(QStringLiteral("t"), title);
144 url.setQuery(urlQuery);
145 break;
146 }
147 case Twitter: {
148 url.setUrl(QStringLiteral("https://twitter.com/share"));
149 QUrlQuery urlQuery;
150 urlQuery.addQueryItem(QStringLiteral("url"), link);
151 urlQuery.addQueryItem(QStringLiteral("text"), title);
152 url.setQuery(urlQuery);
153 break;
154 }
155 case MailTo: {
156 url.setUrl(QStringLiteral("mailto:"));
157 QUrlQuery urlQuery;
158 urlQuery.addQueryItem(QStringLiteral("subject"), title);
159 urlQuery.addQueryItem(QStringLiteral("body"), link);
160 url.setQuery(urlQuery);
161 break;
162 }
163 case LinkedIn: {
164 url.setUrl(QStringLiteral("http://www.linkedin.com/shareArticle"));
165 QUrlQuery urlQuery;
166 urlQuery.addQueryItem(QStringLiteral("mini"), QStringLiteral("true"));
167 urlQuery.addQueryItem(QStringLiteral("url"), link);
168 urlQuery.addQueryItem(QStringLiteral("title"), title);
169 url.setQuery(urlQuery);
170 break;
171 }
172 case Evernote: {
173 url.setUrl(QStringLiteral("https://www.evernote.com/clip.action"));
174 QUrlQuery urlQuery;
175 urlQuery.addQueryItem(QStringLiteral("url"), link);
176 urlQuery.addQueryItem(QStringLiteral("title"), title);
177 url.setQuery(urlQuery);
178 break;
179 }
180 case Pocket: {
181 url.setUrl(QStringLiteral("https://getpocket.com/save"));
182 QUrlQuery urlQuery;
183 urlQuery.addQueryItem(QStringLiteral("url"), link);
184 urlQuery.addQueryItem(QStringLiteral("title"), title);
185 url.setQuery(urlQuery);
186 break;
187 }
188 case LiveJournal: {
189 url.setUrl(QStringLiteral("http://www.livejournal.com/update.bml"));
190 QUrlQuery urlQuery;
191 urlQuery.addQueryItem(QStringLiteral("event"), link);
192 urlQuery.addQueryItem(QStringLiteral("subject"), title);
193 url.setQuery(urlQuery);
194 break;
195 }
196 case ServiceEndType:
197 break;
198 }
199 return url;
200}
201
202void ShareServiceUrlManager::openUrl(const QUrl &url)
203{
204 if (url.isValid()) {
206 } else {
207 qCDebug(PIMCOMMON_LOG) << "url is invalid.";
208 }
209}
210
211#include "moc_shareserviceurlmanager.cpp"
void addAction(QAction *action)
The ShareServiceUrlManager class.
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
KIOCORE_EXPORT CopyJob * link(const QList< QUrl > &src, const QUrl &destDir, JobFlags flags=DefaultFlags)
VehicleSection::Type type(QStringView coachNumber, QStringView coachClassification)
folderdialogacltab.h
QVariant data() const const
QMenu * menu() const const
bool openUrl(const QUrl &url)
QIcon fromTheme(const QString &name)
bool isNull() const const
void triggered(QAction *action)
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool isValid() const const
void setQuery(const QString &query, ParsingMode mode)
void setUrl(const QString &url, ParsingMode parsingMode)
void addQueryItem(const QString &key, const QString &value)
QVariant fromValue(T &&value)
T value() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Oct 11 2024 12:08:32 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.