Changeset View
Changeset View
Standalone View
Standalone View
runners/mediawiki/mediawikirunner.cpp
Show All 12 Lines | |||||
13 | * | 13 | * | ||
14 | * You should have received a copy of the GNU Library General Public | 14 | * You should have received a copy of the GNU Library General Public | ||
15 | * License along with this program; if not, write to the | 15 | * License along with this program; if not, write to the | ||
16 | * Free Software Foundation, Inc., | 16 | * Free Software Foundation, Inc., | ||
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
18 | */ | 18 | */ | ||
19 | 19 | | |||
20 | #include "mediawikirunner.h" | 20 | #include "mediawikirunner.h" | ||
21 | | ||||
21 | #include "mediawiki.h" | 22 | #include "mediawiki.h" | ||
22 | 23 | | |||
24 | // KF | ||||
25 | #include <KPluginInfo> | ||||
26 | #include <KServiceTypeTrader> | ||||
27 | #include <KLocalizedString> | ||||
28 | // Qt | ||||
23 | #include <QMutex> | 29 | #include <QMutex> | ||
24 | #include <QtCore/QEventLoop> | 30 | #include <QEventLoop> | ||
25 | #include <QWaitCondition> | 31 | #include <QWaitCondition> | ||
32 | #include <QDesktopServices> | ||||
33 | #include <QDebug> | ||||
26 | 34 | | |||
27 | #include <KDebug> | | |||
28 | #include <KIcon> | | |||
29 | #include <KPluginInfo> | | |||
30 | #include <KServiceTypeTrader> | | |||
31 | #include <KToolInvocation> | | |||
32 | #include <solid/networking.h> | | |||
33 | 35 | | |||
34 | MediaWikiRunner::MediaWikiRunner(QObject *parent, const QVariantList& args) | 36 | MediaWikiRunner::MediaWikiRunner(QObject *parent, const QVariantList &args) | ||
35 | : Plasma::AbstractRunner(parent, args) | 37 | : Plasma::AbstractRunner(parent, args) | ||
36 | { | 38 | { | ||
37 | Q_UNUSED(args); | 39 | setObjectName(QStringLiteral("MediaWikiRunner")); | ||
38 | setObjectName("MediaWikiRunner"); | | |||
39 | 40 | | |||
40 | const QString constraint = QString("[X-KDE-PluginInfo-Name] == '%1'").arg(id()); | 41 | const QString constraint = QStringLiteral("[X-KDE-PluginInfo-Name] == '%1'").arg(id()); | ||
41 | const KService::List offers = KServiceTypeTrader::self()->query("Plasma/Runner", constraint); | 42 | const KService::List offers = KServiceTypeTrader::self()->query(QStringLiteral("Plasma/Runner"), constraint); | ||
42 | 43 | | |||
43 | foreach (const KPluginInfo &info, KPluginInfo::fromServices(offers)) { | 44 | foreach (const KPluginInfo &info, KPluginInfo::fromServices(offers)) { | ||
44 | 45 | | |||
45 | QStringList _urls = info.property("X-Plasma-Args").toStringList(); | 46 | const auto _urls = info.property(QStringLiteral("X-Plasma-Args")).toStringList(); | ||
46 | QString _u; | 47 | qDebug() << info.name() << _urls << _urls.count(); | ||
47 | kDebug() << info.name() << info.property("X-Plasma-Args").toStringList() << _urls.count(); | | |||
48 | if (_urls.count()) { | 48 | if (_urls.count()) { | ||
49 | m_apiUrl = _urls[0]; | 49 | m_apiUrl = QUrl(_urls[0]); | ||
50 | } else { | 50 | } else { | ||
51 | kWarning() << "No X-Plasma-Args found in .desktop file"; | 51 | qWarning() << "No X-Plasma-Args found in .desktop file"; | ||
52 | } | 52 | } | ||
53 | m_name = info.name(); | 53 | m_name = info.name(); | ||
54 | m_comment = info.comment(); | 54 | m_comment = info.comment(); | ||
55 | m_icon = KIcon(info.icon()); | 55 | m_iconName = info.icon(); | ||
56 | } | 56 | } | ||
57 | 57 | | |||
58 | 58 | | |||
59 | addSyntax(Plasma::RunnerSyntax("wiki :q:", i18n("Searches %1 for :q:.", m_name))); | 59 | addSyntax(Plasma::RunnerSyntax(QStringLiteral("wiki :q:"), i18n("Searches %1 for :q:.", m_name))); | ||
60 | 60 | | |||
61 | setSpeed( SlowSpeed ); | 61 | setSpeed( SlowSpeed ); | ||
62 | } | 62 | } | ||
63 | 63 | | |||
64 | MediaWikiRunner::~MediaWikiRunner() | 64 | MediaWikiRunner::~MediaWikiRunner() | ||
65 | { | 65 | { | ||
66 | } | 66 | } | ||
67 | 67 | | |||
68 | 68 | | |||
69 | void MediaWikiRunner::match(Plasma::RunnerContext &context) | 69 | void MediaWikiRunner::match(Plasma::RunnerContext &context) | ||
70 | { | 70 | { | ||
71 | // Check for networkconnection | 71 | // Check for networkconnection | ||
72 | if(Solid::Networking::status() == Solid::Networking::Unconnected) { | 72 | if (!m_networkConfigurationManager.isOnline() || | ||
73 | !m_apiUrl.isValid()) { | ||||
73 | return; | 74 | return; | ||
74 | } | 75 | } | ||
75 | 76 | | |||
76 | QString term = context.query(); | 77 | QString term = context.query(); | ||
78 | | ||||
77 | if (!context.singleRunnerQueryMode()) { | 79 | if (!context.singleRunnerQueryMode()) { | ||
78 | if (!term.startsWith("wiki ")) { | 80 | if (!term.startsWith(QLatin1String("wiki "))) { | ||
79 | return; | 81 | return; | ||
80 | } else { | | |||
81 | term = term.remove("wiki "); | | |||
82 | } | 82 | } | ||
83 | | ||||
84 | term.remove(0, 5); | ||||
83 | } | 85 | } | ||
84 | if (!m_apiUrl.isValid() || term.length() < 3) { | 86 | | ||
85 | //kDebug() << "yours is too short" << term; | 87 | if (term.length() < 3) { | ||
88 | //qDebug() << "yours is too short" << term; | ||||
86 | return; | 89 | return; | ||
87 | } | 90 | } | ||
88 | 91 | | |||
89 | QEventLoop loop; | 92 | QEventLoop loop; | ||
90 | // Wait a second, we don't want to query on every keypress | 93 | // Wait a second, we don't want to query on every keypress | ||
91 | QMutex mutex; | 94 | QMutex mutex; | ||
92 | QWaitCondition waiter; | 95 | QWaitCondition waiter; | ||
93 | mutex.lock(); | 96 | mutex.lock(); | ||
94 | waiter.wait(&mutex, 1000); | 97 | waiter.wait(&mutex, 1000); | ||
95 | mutex.unlock(); | 98 | mutex.unlock(); | ||
96 | 99 | | |||
97 | if (!context.isValid()) { | 100 | if (!context.isValid()) { | ||
98 | return; | 101 | return; | ||
99 | } | 102 | } | ||
100 | 103 | | |||
101 | MediaWiki mediawiki; | 104 | MediaWiki mediawiki; | ||
102 | if (context.singleRunnerQueryMode()) { | 105 | if (context.singleRunnerQueryMode()) { | ||
103 | mediawiki.setMaxItems(10); | 106 | mediawiki.setMaxItems(10); | ||
104 | } else { | 107 | } else { | ||
105 | mediawiki.setMaxItems(3); | 108 | mediawiki.setMaxItems(3); | ||
106 | } | 109 | } | ||
107 | mediawiki.setApiUrl( m_apiUrl ); | 110 | mediawiki.setApiUrl( m_apiUrl ); | ||
108 | connect( &mediawiki, SIGNAL(finished(bool)), &loop, SLOT(quit()) ); | 111 | connect(&mediawiki, &MediaWiki::finished, &loop, &QEventLoop::quit); | ||
109 | 112 | | |||
110 | mediawiki.search(term); | 113 | mediawiki.search(term); | ||
111 | kDebug() << "Wikisearch:" << m_name << term; | 114 | qDebug() << "Wikisearch:" << m_name << term; | ||
112 | 115 | | |||
113 | loop.exec(); | 116 | loop.exec(); | ||
114 | 117 | | |||
115 | if (!context.isValid()) { | 118 | if (!context.isValid()) { | ||
116 | return; | 119 | return; | ||
117 | } | 120 | } | ||
118 | qreal relevance = 0.5; | 121 | qreal relevance = 0.5; | ||
119 | qreal stepRelevance = 0.1; | 122 | qreal stepRelevance = 0.1; | ||
120 | 123 | | |||
121 | foreach(const MediaWiki::Result& res, mediawiki.results()) { | 124 | foreach(const MediaWiki::Result& res, mediawiki.results()) { | ||
122 | kDebug() << "Match:" << res.url << res.title; | 125 | qDebug() << "Match:" << res.url << res.title; | ||
123 | Plasma::QueryMatch match(this); | 126 | Plasma::QueryMatch match(this); | ||
124 | match.setType(Plasma::QueryMatch::PossibleMatch); | 127 | match.setType(Plasma::QueryMatch::PossibleMatch); | ||
125 | match.setIcon(m_icon); | 128 | match.setIconName(m_iconName); | ||
126 | match.setText(QString("%1: %2").arg(m_name, res.title)); | 129 | match.setText(QStringLiteral("%1: %2").arg(m_name, res.title)); | ||
127 | match.setData(res.url); | 130 | match.setData(res.url); | ||
128 | match.setRelevance(relevance); | 131 | match.setRelevance(relevance); | ||
129 | relevance +=stepRelevance; | 132 | relevance +=stepRelevance; | ||
130 | stepRelevance *=0.5; | 133 | stepRelevance *=0.5; | ||
131 | context.addMatch(res.title, match); | 134 | context.addMatch(match); | ||
132 | } | 135 | } | ||
133 | } | 136 | } | ||
134 | 137 | | |||
135 | void MediaWikiRunner::run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &match) | 138 | void MediaWikiRunner::run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &match) | ||
136 | { | 139 | { | ||
137 | Q_UNUSED(context) | 140 | Q_UNUSED(context) | ||
141 | | ||||
138 | const QString wikiurl = match.data().toUrl().toString(); | 142 | const QString wikiurl = match.data().toUrl().toString(); | ||
139 | kDebug() << "Open MediaWiki page " << wikiurl; | 143 | qDebug() << "Open MediaWiki page " << wikiurl; | ||
140 | 144 | | |||
141 | if (!wikiurl.isEmpty()) { | 145 | if (!wikiurl.isEmpty()) { | ||
142 | KToolInvocation::invokeBrowser(wikiurl, ""); | 146 | QDesktopServices::openUrl(QUrl(wikiurl)); | ||
143 | } | 147 | } | ||
144 | } | 148 | } | ||
145 | 149 | | |||
150 | | ||||
151 | K_EXPORT_PLASMA_RUNNER(mediawiki, MediaWikiRunner) | ||||
152 | | ||||
153 | #include "mediawikirunner.moc" |