[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    KDE/kdebase/workspace/plasma/generic/runners/bookmarks
From:       Jan Gerrit Marker <jangerrit () weiler-marker ! com>
Date:       2009-11-06 21:05:18
Message-ID: 1257541518.735080.16876.nullmailer () svn ! kde ! org
[Download RAW message or body]

SVN commit 1045833 by jangmarker:

Add Firefox bookmark support to the bookmark runner. It is done in a way, which \
allows to easily add other browsers

 M  +2 -1      CMakeLists.txt  
 M  +159 -15   bookmarksrunner.cpp  
 M  +39 -2     bookmarksrunner.h  


--- trunk/KDE/kdebase/workspace/plasma/generic/runners/bookmarks/CMakeLists.txt \
#1045832:1045833 @@ -6,7 +6,8 @@
 )
 
 kde4_add_plugin(krunner_bookmarksrunner ${krunner_bookmarksrunner_SRCS})
-target_link_libraries(krunner_bookmarksrunner ${KDE4_KIO_LIBS} \
${QT_QTSCRIPT_LIBRARY} ${KDE4_PLASMA_LIBS}) \
+target_link_libraries(krunner_bookmarksrunner ${KDE4_KIO_LIBS} \
${QT_QTSCRIPT_LIBRARY} ${KDE4_PLASMA_LIBS} +                      \
${QT_QTSQL_LIBRARY})  
 install(TARGETS krunner_bookmarksrunner DESTINATION ${PLUGIN_INSTALL_DIR} )
 
--- trunk/KDE/kdebase/workspace/plasma/generic/runners/bookmarks/bookmarksrunner.cpp \
#1045832:1045833 @@ -23,12 +23,16 @@
 #include <QDBusReply>
 #include <QList>
 #include <QStack>
+#include <QSqlQuery>
+#include <QDir>
 
 #include <KIcon>
 #include <KBookmarkManager>
 #include <KToolInvocation>
 #include <KUrl>
 #include <KStandardDirs>
+#include <KDebug>
+#include <KIO/Job>
 
 BookmarksRunner::BookmarksRunner( QObject* parent, const QVariantList &args )
     : Plasma::AbstractRunner(parent, args)
@@ -37,21 +41,118 @@
     setObjectName("Bookmarks");
     m_icon = KIcon("bookmarks");
     m_bookmarkManager = KBookmarkManager::userBookmarksManager();
+    m_browser = whichBrowser();
+    m_dbCacheFile = KStandardDirs::locateLocal("cache", "") + \
                "bookmarkrunnerfirefoxdbfile.sqlite";
     addSyntax(Plasma::RunnerSyntax(":q:", i18n("Finds web browser bookmarks matching \
                :q:.")));
-    addSyntax(Plasma::RunnerSyntax(i18nc("list of all konqueror bookmarks", \
"bookmarks"), i18n("List all bookmarks of the browser"))); +    \
addSyntax(Plasma::RunnerSyntax(i18nc("list of all web browser bookmarks", \
"bookmarks"), +                                   i18n("List all web browser \
bookmarks"))); +
+    connect(this, SIGNAL(prepare()), this, SLOT(prep()));
+    connect(this, SIGNAL(teardown()), this, SLOT(down()));
+
+    reloadConfiguration();
 }
 
 BookmarksRunner::~BookmarksRunner()
 {
 }
 
+void BookmarksRunner::reloadConfiguration()
+{
+    if (QSqlDatabase::isDriverAvailable("QSQLITE")) {
+        KConfigGroup grp = config();
+        /* This allows the user to specify a profile database */
+        m_dbFile = grp.readEntry<QString>("dbfile", "");
+        if (m_dbFile.isEmpty()) {//Try to get the right database file, the default \
profile is used +            KConfig firefoxProfile(QDir::homePath() + \
"/.mozilla/firefox/profiles.ini", +                                   \
KConfig::SimpleConfig); +            QString profilePath(QDir::homePath() + \
"/.mozilla/firefox/"); +            for (int i = 0; true; i++) {
+                QString groupName = QString("Profile%1").arg(i);
+                if (firefoxProfile.hasGroup(groupName)) {
+                    KConfigGroup fGrp = firefoxProfile.group(groupName);
+                    if (fGrp.readEntry<int>("Default", 0)) {
+                        profilePath = profilePath + fGrp.readEntry("Path", \
QString()); +                        break;
+                    }
+                }
+            }
+            m_dbFile = profilePath + "/places.sqlite";
+            grp.writeEntry("dbfile", m_dbFile);
+        }
+        m_db = QSqlDatabase::addDatabase("QSQLITE");
+        m_db.setHostName("localhost");
+    } else {
+        kDebug() << "SQLITE driver isn't available";
+        m_db = QSqlDatabase();
+    }
+}
+
+void BookmarksRunner::prep()
+{
+    m_browser = whichBrowser();
+    if (m_browser == Firefox) {
+        if (m_db.isValid()) {
+            kDebug() << "Cache file was removed: " << QFile(m_dbCacheFile).remove();
+            kDebug() << "Database was copyed: " << \
QFile(m_dbFile).copy(m_dbCacheFile); +            \
m_db.setDatabaseName(m_dbCacheFile); +            m_dbOK = m_db.open();
+            kDebug() << "Database was opened: " << m_dbOK;
+        }
+    }
+}
+
+void BookmarksRunner::down()
+{
+    if (m_db.isOpen()) {
+        m_db.close();
+        m_dbOK = false;
+        QFile db_CacheFile(m_dbCacheFile);
+        if (db_CacheFile.exists()) {
+            kDebug() << "Cache file was removed: " << db_CacheFile.remove();
+        }
+    }
+}
+
 void BookmarksRunner::match(Plasma::RunnerContext &context)
 {
     const QString term = context.query();
     if (term.length() < 3) {
         return;
     }
+    bool allBookmarks = term.compare(i18nc("list of all konqueror bookmarks", \
"bookmarks"), +                                     Qt::CaseInsensitive) == 0;
+    if (m_browser == Konqueror) {
+        matchKonquerorBookmarks(context, allBookmarks, term);
+    } else if (m_browser == Firefox) {
+        matchFirefoxBookmarks(context, allBookmarks, term);
+    }
+}
 
+KIcon BookmarksRunner::favicon(const KUrl &url)
+{
+    // query the favicons module
+    QDBusInterface favicon("org.kde.kded", "/modules/favicons", "org.kde.FavIcon");
+    QDBusReply<QString> reply = favicon.call("iconForUrl", url.url());
+
+    if (!reply.isValid()) {
+        return KIcon();
+    }
+
+    // locate the favicon
+    const QString iconFile = \
KGlobal::dirs()->findResource("cache",reply.value()+".png"); +    \
if(iconFile.isNull()) { +        return KIcon();
+    }
+
+    KIcon icon = KIcon(iconFile);
+
+    return icon;
+}
+
+void BookmarksRunner::matchKonquerorBookmarks(Plasma::RunnerContext& context, bool \
allBookmarks, +                                              const QString& term)
+{
     KBookmarkGroup bookmarkGroup = m_bookmarkManager->root();
 
     QList<Plasma::QueryMatch> matches;
@@ -96,7 +197,7 @@
         } else if (url.contains(term, Qt::CaseInsensitive)) {
             type = Plasma::QueryMatch::PossibleMatch;
             relevance = 0.2;
-        } else if (term.compare(i18nc("list of all konqueror bookmarks", \
"bookmarks"), Qt::CaseInsensitive) == 0) { +        } else if (allBookmarks) {
             type = Plasma::QueryMatch::PossibleMatch;
             relevance = 0.18;
         }
@@ -134,31 +235,74 @@
             bookmark = bookmarkGroup.next(bookmark);
         }
     }
-
     context.addMatches(term, matches);
 }
 
-KIcon BookmarksRunner::getFavicon(const KUrl &url)
+void BookmarksRunner::matchFirefoxBookmarks(Plasma::RunnerContext& context, bool \
allBookmarks, const QString& term)  {
-    // query the favicons module
-    QDBusInterface favicon("org.kde.kded", "/modules/favicons", "org.kde.FavIcon");
-    QDBusReply<QString> reply = favicon.call("iconForUrl", url.url());
+    if (!m_dbOK) {
+        return;
+    }
 
-    if (!reply.isValid()) {
-        return KIcon();
+    QList<int> fks;
+    QHash<int, QString> titles; //index (fk), title
+    QHash<int, QUrl> urls; //index, url (QUrl in order to go with QVariant)
+    QHash<int, KIcon> icons; //index, icon
+    QString tmpTerm = term;
+    QSqlQuery query;
+    if (allBookmarks) {
+        query = QSqlQuery("SELECT moz_bookmarks.fk, moz_bookmarks.title, \
moz_places.url," \ +                    "moz_places.favicon_id FROM moz_bookmarks, \
moz_places WHERE " \ +                    "moz_bookmarks.type = 1 AND \
moz_bookmarks.fk = moz_places.id"); +    } else {
+        const QString escapedTerm = tmpTerm.replace("'", "\\'");
+        query = QSqlQuery("SELECT moz_bookmarks.fk, moz_bookmarks.title, \
moz_places.url," \ +                        "moz_places.favicon_id FROM \
moz_bookmarks, moz_places WHERE " \ +                        "moz_bookmarks.type = 1 \
AND moz_bookmarks.fk = moz_places.id AND " \ +                        \
"(moz_bookmarks.title LIKE  '%" + escapedTerm + "%' or moz_places.url LIKE '%" +      \
+ escapedTerm + "%')");  }
+    while (query.next()) {
+        const QString title = query.value(1).toString();
+        const QUrl url = query.value(2).toString();
+        //const int favicon_id = query.value(3).toInt();
 
-    // locate the favicon
-    const QString iconFile = \
                KGlobal::dirs()->findResource("cache",reply.value()+".png");
-    if(iconFile.isNull()) {
-        return KIcon();
+        int fk = query.value(0).toInt();
+        fks << fk;
+        titles.insert(fk, title);
+        urls.insert(fk, url);
+        icons.insert(fk, m_icon); //could be changed to use favicon
     }
 
-    KIcon icon = KIcon(iconFile);
+    if (!context.isValid()) {
+        return;
+    }
 
-    return icon;
+    QList<Plasma::QueryMatch> matches;
+    foreach (const int& fk, fks) {
+        Plasma::QueryMatch match(this);
+        match.setIcon(icons[fk]);
+        match.setText(titles[fk]);
+        match.setData(urls[fk]);
+        matches << match;
+    }
+    context.addMatches(term, matches);
 }
 
+BookmarksRunner::Browser BookmarksRunner::whichBrowser()
+{
+    KConfigGroup config(KSharedConfig::openConfig("kdeglobals"), \
QLatin1String("General") ); +    QString exec = config.readPathEntry( \
QLatin1String("BrowserApplication"), QString("") ); +    kDebug() << exec;
+    if (exec.contains("firefox", Qt::CaseInsensitive)) {
+        return Firefox;
+    } else if (exec.contains("konqueror", Qt::CaseInsensitive)) {
+        return Konqueror;
+    } else {
+        return Default;
+    }
+}
+
 void BookmarksRunner::run(const Plasma::RunnerContext &context, const \
Plasma::QueryMatch &action)  {
     Q_UNUSED(context);
--- trunk/KDE/kdebase/workspace/plasma/generic/runners/bookmarks/bookmarksrunner.h \
#1045832:1045833 @@ -21,13 +21,14 @@
 #define BOOKMARKSRUNNER_H
 
 #include <KIcon>
-
+#include <QSqlDatabase>
 #include <Plasma/AbstractRunner>
 
 
 class KBookmark;
 class KBookmarkManager;
 
+/** This runner searchs for bookmarks in browsers like Konqueror and Firefox */
 class BookmarksRunner : public Plasma::AbstractRunner
 {
     Q_OBJECT
@@ -38,13 +39,49 @@
 
         void match(Plasma::RunnerContext &context);
         void run(const Plasma::RunnerContext &context, const Plasma::QueryMatch \
&action); +        void reloadConfiguration();
 
     private:
-        KIcon getFavicon(const KUrl &url);
+        /** Defines the browser to get the bookmarks from
+          * Add support for a browser by
+          * -adding the browser as part of this enum
+          * -adding an if-statement with the enum part
+          * -adding a method getBrowserBookmarks
+          * -adding a statement to whichBrowser()
+          * @see whichBrowser()
+          */
+        enum Browser { Konqueror, ///< the browser is Konqueror
+                       Firefox, ///< the browser is Firefox
+                       Default = Konqueror ///< Konqueror is default
+                     };
 
+        /** @returns the favicon for the url */
+        KIcon favicon(const KUrl &url);
+
+        /** Get the bookmarks from Firefox */
+        void matchFirefoxBookmarks(Plasma::RunnerContext& context, bool \
allBookmarks, +                                                      const QString& \
term); +        /** Get the bookmarks from Konqueror */
+        void matchKonquerorBookmarks(Plasma::RunnerContext& context, bool \
allBookmarks, +                                                        const QString& \
term); +
+        /** @returns the browser to get the bookmarks from
+          * @see Browser
+          */
+        Browser whichBrowser();
+
     private:
         KIcon m_icon;
+        bool m_dbOK;
+        Browser m_browser;
+        QString m_dbFile;
+        QString m_dbCacheFile;
+        QSqlDatabase m_db;
         KBookmarkManager *m_bookmarkManager;
+
+    private Q_SLOTS:
+        void prep();
+        void down();
 };
 
 K_EXPORT_PLASMA_RUNNER(bookmarksrunner, BookmarksRunner)


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic