add more spacing
[personal-kdebase.git] / workspace / plasma / runners / bookmarks / bookmarksrunner.cpp
blob0a0a2ab8e4032be6ce1511ecb08ebd1e4a1debf2
1 /*
2 * Copyright 2007 Glenn Ergeerts <glenn.ergeerts@telenet.be>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Library General Public License as
6 * published by the Free Software Foundation; either version 2, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details
14 * You should have received a copy of the GNU Library General Public
15 * License along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 #include "bookmarksrunner.h"
22 #include <QAction>
23 #include <QDBusInterface>
24 #include <QDBusReply>
25 #include <QLabel>
26 #include <QList>
27 #include <QStack>
28 #include <QWidget>
30 #include <KIcon>
31 #include <KBookmarkManager>
32 #include <KToolInvocation>
33 #include <KUrl>
34 #include <KStandardDirs>
37 BookmarksRunner::BookmarksRunner( QObject* parent, const QVariantList &args )
38 : Plasma::AbstractRunner(parent, args)
40 Q_UNUSED(args)
41 setObjectName("Bookmarks");
42 m_icon = KIcon("bookmarks");
43 m_bookmarkManager = KBookmarkManager::userBookmarksManager();
46 BookmarksRunner::~BookmarksRunner()
50 void BookmarksRunner::match(Plasma::RunnerContext &context)
52 const QString term = context.query();
53 if (term.length() < 3) {
54 return;
57 KBookmarkGroup bookmarkGroup = m_bookmarkManager->root();
59 QList<Plasma::QueryMatch> matches;
60 QStack<KBookmarkGroup> groups;
62 KBookmark bookmark = bookmarkGroup.first();
63 while (!bookmark.isNull()) {
64 if (bookmark.isGroup()) { // descend
65 //kDebug () << "descending into" << bookmark.text();
66 groups.push(bookmarkGroup);
67 bookmarkGroup = bookmark.toGroup();
68 bookmark = bookmarkGroup.first();
70 while (bookmark.isNull() && !groups.isEmpty()) {
71 bookmark = bookmarkGroup;
72 bookmarkGroup = groups.pop();
73 bookmark = bookmarkGroup.next(bookmark);
76 continue;
79 Plasma::QueryMatch::Type type = Plasma::QueryMatch::NoMatch;
80 qreal relevance = 0;
82 if (bookmark.text().toLower() == term.toLower()) {
83 type = Plasma::QueryMatch::ExactMatch;
84 relevance = 1.0;
85 } else if (bookmark.text().contains(term, Qt::CaseInsensitive)) {
86 type = Plasma::QueryMatch::PossibleMatch;
87 relevance = 0.4;
88 } else if (bookmark.url().prettyUrl().contains(term, Qt::CaseInsensitive)) {
89 type = Plasma::QueryMatch::PossibleMatch;
90 relevance = 0.2;
93 if (type != Plasma::QueryMatch::NoMatch) {
94 //kDebug() << "Found bookmark: " << bookmark.text() << " (" << bookmark.url().prettyUrl() << ")";
95 // getting the favicon is too slow and can easily lead to starving the thread pool out
97 QIcon icon = getFavicon(bookmark.url());
98 if (icon.isNull()) {
99 match->setIcon(m_icon);
101 else {
102 match->setIcon(icon);
105 Plasma::QueryMatch match(this);
106 match.setType(type);
107 match.setRelevance(relevance);
108 match.setIcon(m_icon);
109 match.setText(bookmark.text());
110 match.setData(bookmark.url().url());
111 matches << match;
114 bookmark = bookmarkGroup.next(bookmark);
115 while (bookmark.isNull() && !groups.isEmpty()) {
116 bookmark = bookmarkGroup;
117 bookmarkGroup = groups.pop();
118 //kDebug() << "ascending from" << bookmark.text() << "to" << bookmarkGroup.text();
119 bookmark = bookmarkGroup.next(bookmark);
123 context.addMatches(term, matches);
126 KIcon BookmarksRunner::getFavicon(const KUrl &url)
128 // query the favicons module
129 QDBusInterface favicon("org.kde.kded", "/modules/favicons", "org.kde.FavIcon");
130 QDBusReply<QString> reply = favicon.call("iconForUrl", url.url());
132 if (!reply.isValid()) {
133 return KIcon();
136 // locate the favicon
137 QString iconFile = KGlobal::dirs()->findResource("cache",reply.value()+".png");
138 if(iconFile.isNull()) {
139 return KIcon();
142 KIcon icon = KIcon(iconFile);
144 return icon;
147 void BookmarksRunner::run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &action)
149 Q_UNUSED(context);
150 KUrl url = (KUrl)action.data().toString();
151 //kDebug() << "BookmarksRunner::run opening: " << url.url();
152 KToolInvocation::invokeBrowser(url.url());
155 #include "bookmarksrunner.moc"