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"
23 #include <QDBusInterface>
31 #include <KBookmarkManager>
32 #include <KToolInvocation>
34 #include <KStandardDirs>
37 BookmarksRunner::BookmarksRunner( QObject
* parent
, const QVariantList
&args
)
38 : Plasma::AbstractRunner(parent
, 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) {
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
);
79 Plasma::QueryMatch::Type type
= Plasma::QueryMatch::NoMatch
;
82 if (bookmark
.text().toLower() == term
.toLower()) {
83 type
= Plasma::QueryMatch::ExactMatch
;
85 } else if (bookmark
.text().contains(term
, Qt::CaseInsensitive
)) {
86 type
= Plasma::QueryMatch::PossibleMatch
;
88 } else if (bookmark
.url().prettyUrl().contains(term
, Qt::CaseInsensitive
)) {
89 type
= Plasma::QueryMatch::PossibleMatch
;
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());
99 match->setIcon(m_icon);
102 match->setIcon(icon);
105 Plasma::QueryMatch
match(this);
107 match
.setRelevance(relevance
);
108 match
.setIcon(m_icon
);
109 match
.setText(bookmark
.text());
110 match
.setData(bookmark
.url().url());
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()) {
136 // locate the favicon
137 QString iconFile
= KGlobal::dirs()->findResource("cache",reply
.value()+".png");
138 if(iconFile
.isNull()) {
142 KIcon icon
= KIcon(iconFile
);
147 void BookmarksRunner::run(const Plasma::RunnerContext
&context
, const Plasma::QueryMatch
&action
)
150 KUrl url
= (KUrl
)action
.data().toString();
151 //kDebug() << "BookmarksRunner::run opening: " << url.url();
152 KToolInvocation::invokeBrowser(url
.url());
155 #include "bookmarksrunner.moc"