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 Q_UNUSED(args)
00041 setObjectName("Bookmarks");
00042 m_icon = KIcon("bookmarks");
00043 m_bookmarkManager = KBookmarkManager::userBookmarksManager();
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 KBookmarkGroup bookmarkGroup = m_bookmarkManager->root();
00058
00059 QList<Plasma::QueryMatch> matches;
00060 QStack<KBookmarkGroup> groups;
00061
00062 KBookmark bookmark = bookmarkGroup.first();
00063 while (!bookmark.isNull()) {
00064 if (bookmark.isGroup()) {
00065
00066 groups.push(bookmarkGroup);
00067 bookmarkGroup = bookmark.toGroup();
00068 bookmark = bookmarkGroup.first();
00069
00070 while (bookmark.isNull() && !groups.isEmpty()) {
00071 bookmark = bookmarkGroup;
00072 bookmarkGroup = groups.pop();
00073 bookmark = bookmarkGroup.next(bookmark);
00074 }
00075
00076 continue;
00077 }
00078
00079 Plasma::QueryMatch::Type type = Plasma::QueryMatch::NoMatch;
00080 qreal relevance = 0;
00081
00082 if (bookmark.text().toLower() == term.toLower()) {
00083 type = Plasma::QueryMatch::ExactMatch;
00084 relevance = 1.0;
00085 } else if (bookmark.text().contains(term, Qt::CaseInsensitive)) {
00086 type = Plasma::QueryMatch::PossibleMatch;
00087 relevance = 0.4;
00088 } else if (bookmark.url().prettyUrl().contains(term, Qt::CaseInsensitive)) {
00089 type = Plasma::QueryMatch::PossibleMatch;
00090 relevance = 0.2;
00091 }
00092
00093 if (type != Plasma::QueryMatch::NoMatch) {
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105 Plasma::QueryMatch match(this);
00106 match.setType(type);
00107 match.setRelevance(relevance);
00108 match.setIcon(m_icon);
00109 match.setText(bookmark.text());
00110 match.setData(bookmark.url().url());
00111 matches << match;
00112 }
00113
00114 bookmark = bookmarkGroup.next(bookmark);
00115 while (bookmark.isNull() && !groups.isEmpty()) {
00116 bookmark = bookmarkGroup;
00117 bookmarkGroup = groups.pop();
00118
00119 bookmark = bookmarkGroup.next(bookmark);
00120 }
00121 }
00122
00123 context.addMatches(term, matches);
00124 }
00125
00126 KIcon BookmarksRunner::getFavicon(const KUrl &url)
00127 {
00128
00129 QDBusInterface favicon("org.kde.kded", "/modules/favicons", "org.kde.FavIcon");
00130 QDBusReply<QString> reply = favicon.call("iconForUrl", url.url());
00131
00132 if (!reply.isValid()) {
00133 return KIcon();
00134 }
00135
00136
00137 QString iconFile = KGlobal::dirs()->findResource("cache",reply.value()+".png");
00138 if(iconFile.isNull()) {
00139 return KIcon();
00140 }
00141
00142 KIcon icon = KIcon(iconFile);
00143
00144 return icon;
00145 }
00146
00147 void BookmarksRunner::run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &action)
00148 {
00149 Q_UNUSED(context);
00150 KUrl url = (KUrl)action.data().toString();
00151
00152 KToolInvocation::invokeBrowser(url.url());
00153 }
00154
00155 #include "bookmarksrunner.moc"