• Skip to content
  • Skip to link menu
KDE 4.1 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

Plasma

bookmarksrunner.cpp

Go to the documentation of this file.
00001 /*
00002  *   Copyright 2007 Glenn Ergeerts <glenn.ergeerts@telenet.be>
00003  *
00004  *   This program is free software; you can redistribute it and/or modify
00005  *   it under the terms of the GNU Library General Public License as
00006  *   published by the Free Software Foundation; either version 2, or
00007  *   (at your option) any later version.
00008  *
00009  *   This program is distributed in the hope that it will be useful,
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *   GNU General Public License for more details
00013  *
00014  *   You should have received a copy of the GNU Library General Public
00015  *   License along with this program; if not, write to the
00016  *   Free Software Foundation, Inc.,
00017  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
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()) { // descend
00066             //kDebug () << "descending into" << bookmark.text();
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             //kDebug() << "Found bookmark: " << bookmark.text() << " (" << bookmark.url().prettyUrl() << ")";
00096             // getting the favicon is too slow and can easily lead to starving the thread pool out
00097             /*
00098             QIcon icon = getFavicon(bookmark.url());
00099             if (icon.isNull()) {
00100                 match->setIcon(m_icon);
00101             }
00102             else {
00103                 match->setIcon(icon);
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             //kDebug() << "ascending from" << bookmark.text() << "to" << bookmarkGroup.text();
00121             bookmark = bookmarkGroup.next(bookmark);
00122         }
00123     }
00124 
00125     context.addMatches(term, matches);
00126 }
00127 
00128 KIcon BookmarksRunner::getFavicon(const KUrl &url)
00129 {
00130     // query the favicons module
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     // locate the favicon
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     //kDebug() << "BookmarksRunner::run opening: " << url.url();
00154     KToolInvocation::invokeBrowser(url.url());
00155 }
00156 
00157 #include "bookmarksrunner.moc"

Plasma

Skip menu "Plasma"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members

API Reference

Skip menu "API Reference"
  • KWin
  •   KWin Libraries
  • Libraries
  •   libkworkspace
  •   libplasma
  •   libsolidcontrol
  •   libtaskmanager
  • Plasma
  •   Animators
  •   Applets
  •   Engines
  • Solid Modules
Generated for API Reference by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal