Plasma
bookmarksrunner.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "bookmarksrunner.h"
00021
00022 #include <QAction>
00023 #include <QDBusInterface>
00024 #include <QDBusReply>
00025 #include <QLabel>
00026 #include <QList>
00027 #include <QStack>
00028 #include <QWidget>
00029
00030 #include <KIcon>
00031 #include <KBookmarkManager>
00032 #include <KToolInvocation>
00033 #include <KUrl>
00034 #include <KStandardDirs>
00035
00036
00037 BookmarksRunner::BookmarksRunner( QObject* parent, const QVariantList &args )
00038 : Plasma::AbstractRunner(parent, args)
00039 {
00040 KGlobal::locale()->insertCatalog("krunner_bookmarksrunner");
00041 Q_UNUSED(args)
00042 setObjectName(i18n("Bookmarks"));
00043 m_icon = KIcon("bookmarks");
00044 }
00045
00046 BookmarksRunner::~BookmarksRunner()
00047 {
00048 }
00049
00050 void BookmarksRunner::match(Plasma::RunnerContext &context)
00051 {
00052 const QString term = context.query();
00053 if (term.length() < 3) {
00054 return;
00055 }
00056
00057 KBookmarkManager *bookmarkManager = KBookmarkManager::userBookmarksManager();
00058 KBookmarkGroup bookmarkGroup = bookmarkManager->root();
00059
00060 QList<Plasma::QueryMatch> matches;
00061 QStack<KBookmarkGroup> groups;
00062
00063 KBookmark bookmark = bookmarkGroup.first();
00064 while (!bookmark.isNull()) {
00065 if (bookmark.isGroup()) {
00066
00067 groups.push(bookmarkGroup);
00068 bookmarkGroup = bookmark.toGroup();
00069 bookmark = bookmarkGroup.first();
00070
00071 while (bookmark.isNull() && !groups.isEmpty()) {
00072 bookmark = bookmarkGroup;
00073 bookmarkGroup = groups.pop();
00074 bookmark = bookmarkGroup.next(bookmark);
00075 }
00076
00077 continue;
00078 }
00079
00080 Plasma::QueryMatch::Type type = Plasma::QueryMatch::NoMatch;
00081 qreal relevance = 0;
00082
00083 if (bookmark.text().toLower() == term.toLower()) {
00084 type = Plasma::QueryMatch::ExactMatch;
00085 relevance = 1.0;
00086 } else if (bookmark.text().contains(term, Qt::CaseInsensitive)) {
00087 type = Plasma::QueryMatch::PossibleMatch;
00088 relevance = 0.7;
00089 } else if (bookmark.url().prettyUrl().contains(term, Qt::CaseInsensitive)) {
00090 type = Plasma::QueryMatch::PossibleMatch;
00091 relevance = 0.6;
00092 }
00093
00094 if (type != Plasma::QueryMatch::NoMatch) {
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107 Plasma::QueryMatch match(this);
00108 match.setType(type);
00109 match.setRelevance(relevance);
00110 match.setIcon(m_icon);
00111 match.setText(bookmark.text());
00112 match.setData(bookmark.url().url());
00113 matches << match;
00114 }
00115
00116 bookmark = bookmarkGroup.next(bookmark);
00117 while (bookmark.isNull() && !groups.isEmpty()) {
00118 bookmark = bookmarkGroup;
00119 bookmarkGroup = groups.pop();
00120
00121 bookmark = bookmarkGroup.next(bookmark);
00122 }
00123 }
00124
00125 context.addMatches(term, matches);
00126 }
00127
00128 KIcon BookmarksRunner::getFavicon(const KUrl &url)
00129 {
00130
00131 QDBusInterface favicon("org.kde.kded", "/modules/favicons", "org.kde.FavIcon");
00132 QDBusReply<QString> reply = favicon.call("iconForUrl", url.url());
00133
00134 if (!reply.isValid()) {
00135 return KIcon();
00136 }
00137
00138
00139 QString iconFile = KGlobal::dirs()->findResource("cache",reply.value()+".png");
00140 if(iconFile.isNull()) {
00141 return KIcon();
00142 }
00143
00144 KIcon icon = KIcon(iconFile);
00145
00146 return icon;
00147 }
00148
00149 void BookmarksRunner::run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &action)
00150 {
00151 Q_UNUSED(context);
00152 KUrl url = (KUrl)action.data().toString();
00153
00154 KToolInvocation::invokeBrowser(url.url());
00155 }
00156
00157 #include "bookmarksrunner.moc"