• Skip to content
  • Skip to link menu
KDE 4.2 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     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()) { // descend
00065             //kDebug () << "descending into" << bookmark.text();
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             //kDebug() << "Found bookmark: " << bookmark.text() << " (" << bookmark.url().prettyUrl() << ")";
00095             // getting the favicon is too slow and can easily lead to starving the thread pool out
00096             /*
00097             QIcon icon = getFavicon(bookmark.url());
00098             if (icon.isNull()) {
00099                 match->setIcon(m_icon);
00100             }
00101             else {
00102                 match->setIcon(icon);
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             //kDebug() << "ascending from" << bookmark.text() << "to" << bookmarkGroup.text();
00119             bookmark = bookmarkGroup.next(bookmark);
00120         }
00121     }
00122 
00123     context.addMatches(term, matches);
00124 }
00125 
00126 KIcon BookmarksRunner::getFavicon(const KUrl &url)
00127 {
00128     // query the favicons module
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     // locate the favicon
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     //kDebug() << "BookmarksRunner::run opening: " << url.url();
00152     KToolInvocation::invokeBrowser(url.url());
00153 }
00154 
00155 #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
  •   libsolidcontrol
  •   libtaskmanager
  • Plasma
  •   Animators
  •   Applets
  •   Engines
  • Solid Modules
Generated for API Reference by doxygen 1.5.7
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