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

List:       kde-commits
Subject:    KDE/kdebase/workspace/plasma/generic/runners/webshortcuts
From:       Dawit Alemayehu <adawit () kde ! org>
Date:       2010-09-07 17:56:46
Message-ID: 20100907180059.28AADAC769 () svn ! kde ! org
[Download RAW message or body]

SVN commit 1172660 by adawit:

- Make use of the newly enhanced API in KUriFilter.
- Optimize the call ::match by not attempting to filter the input on every keystorke.
  Instead only request a filter if the keyword is changed by the user.
- Use dbus to be notified of configuration changes insead sycoca and configuration
  file change monitoring.


 M  +55 -60    webshortcutrunner.cpp  
 M  +5 -7      webshortcutrunner.h  


--- trunk/KDE/kdebase/workspace/plasma/generic/runners/webshortcuts/webshortcutrunner.cpp \
#1172659:1172660 @@ -18,7 +18,6 @@
 
 #include "webshortcutrunner.h"
 
-
 #include <KDebug>
 #include <KLocale>
 #include <KMimeType>
@@ -29,9 +28,11 @@
 #include <KUrl>
 #include <KUriFilter>
 
+#include <QtDBus/QtDBus>
+
 WebshortcutRunner::WebshortcutRunner(QObject *parent, const QVariantList& args)
     : Plasma::AbstractRunner(parent, args),
-      m_match(this)
+      m_match(this), m_filterBeforeRun(false)
 {
     Q_UNUSED(args);
     setObjectName("Web Shortcut");
@@ -42,15 +43,12 @@
     m_match.setType(Plasma::QueryMatch::ExactMatch);
     m_match.setRelevance(0.9);
 
-    m_watch.addFile(KGlobal::dirs()->locateLocal("config", "kuriikwsfilterrc"));
-    connect(&m_watch, SIGNAL(dirty(QString)), this, SLOT(readFiltersConfig()));
-    connect(&m_watch, SIGNAL(created(QString)), this, SLOT(readFiltersConfig()));
-    connect(&m_watch, SIGNAL(deleted(QString)), this, SLOT(readFiltersConfig()));
+    // Listen for KUriFilter plugin config changes and update state...
+    QDBusConnection sessionDbus = QDBusConnection::sessionBus();
+    sessionDbus.connect(QString(), QString(), "org.kde.KUriFilterPlugin",
+                        "configure", this, SLOT(readFiltersConfig()));
 
-    connect(KSycoca::self(), SIGNAL(databaseChanged(QStringList)), this,
-            SLOT(sycocaChanged(QStringList)));
     connect(this, SIGNAL(teardown()), this, SLOT(resetState()));
-
     readFiltersConfig();
 }
 
@@ -64,38 +62,21 @@
     loadSyntaxes();
 }
 
-void WebshortcutRunner::sycocaChanged(const QStringList &changes)
-{
-    if (changes.contains("services")) {
-        loadSyntaxes();
-    }
-}
-
 void WebshortcutRunner::loadSyntaxes()
 {
-    KUriFilterData filterData;
-    QStringList filters;
-    filters << "kuriikwsfilter";
-    KUriFilter::self()->filterUri(filterData, filters);
+    KUriFilterData filterData (QLatin1String(":q"));
+    filterData.setSearchFilteringOptions(KUriFilterData::RetrieveAvailableSearchProvidersOnly);
 +    if (KUriFilter::self()->filterSearchUri(filterData, \
KUriFilter::NormalTextFilter))  m_delimiter = filterData.searchTermSeparator();
-    //kDebug() << "keyword delimiter is: " << m_delimiter << \
filterData.preferredSearchProviders();  
+    //kDebug() << "keyword delimiter:" << m_delimiter;
+    //kDebug() << "search providers:" << filterData.preferredSearchProviders();
+
     QList<Plasma::RunnerSyntax> syns;
-    foreach (const QString &provider, filterData.preferredSearchProviders()) {
+    Q_FOREACH (const QString &provider, filterData.preferredSearchProviders()) {
         //kDebug() << "checking out" << provider;
         Plasma::RunnerSyntax s(filterData.queryForPreferredSearchProvider(provider), \
                /*":q:",*/
                               i18n("Opens \"%1\" in a web browser with the query \
                :q:.", provider));
-        /*
-        // counter potential confusion by displaying one definite
-        // shorthand as an example.
-        if (keys.count() > 1) {
-            QStringListIterator it(keys);
-            it.next(); // skip the first key, we already have it!
-            while (it.hasNext()) {
-                s.addExampleQuery(it.next() + m_delimiter + ":q:");
-            }
-        }
-        */
         syns << s;
     }
 
@@ -104,67 +85,81 @@
 
 void WebshortcutRunner::resetState()
 {
+    kDebug();
     m_lastFailedKey.clear();
-    m_lastServiceName.clear();
+    m_lastProvider.clear();
+    m_lastKey.clear();
 }
 
 void WebshortcutRunner::match(Plasma::RunnerContext &context)
 {
     const QString term = context.query();
 
-    if (term.length() < 3 || !term.contains(m_delimiter)) {
+    if (term.length() < 3 || !term.contains(m_delimiter))
         return;
-    }
 
-    //kDebug() << "checking with" << term;
+    // kDebug() << "checking term" << term;
 
-    int delimIndex = term.indexOf(m_delimiter);
-    if (delimIndex == term.length() - 1) {
+    const int delimIndex = term.indexOf(m_delimiter);
+    if (delimIndex == term.length() - 1)
         return;
-    }
 
-    QString key = term.left(delimIndex);
+    const QString key = term.left(delimIndex);
 
-    if (key == m_lastFailedKey) {
-        // we already know it's going to suck ;)
+    if (key == m_lastFailedKey)
+        return;    // we already know it's going to suck ;)
+
+    if (!context.isValid()) {
+        kDebug() << "invalid context";
         return;
     }
 
-    KUriFilterData filterData(term);
-    QStringList filters;
-    filters << "kurisearchfilter";//"kuriikwsfilter";
-
-    if (!KUriFilter::self()->filterUri(filterData, filters)) {
-        m_lastFailedKey = key;
+    // Do a fake user feedback text update if the keyword has not changed.
+    // There is no point filtering the request on every key stroke.
+    // filtering
+    if (m_lastKey == key) {
+        m_filterBeforeRun = true;
+        m_match.setText(i18n("Search %1 for %2", m_lastProvider, \
term.mid(delimIndex))); +        context.addMatch(term, m_match);
         return;
     }
 
-    if (!context.isValid()) {
-        //kDebug() << "invalid context";
+    KUriFilterData filterData(term);
+    if (!KUriFilter::self()->filterSearchUri(filterData, \
KUriFilter::WebShortcutFilter)) { +        m_lastFailedKey = key;
         return;
     }
 
     m_lastFailedKey.clear();
+    m_lastKey = key;
+    m_lastProvider = filterData.searchProvider();
 
     m_match.setData(filterData.uri().url());
     m_match.setId("WebShortcut:" + key);
 
     m_match.setIcon(KIcon(filterData.iconName()));
-
-    QString actionText = i18n("Search %1 for %2", filterData.searchProvider(), \
                filterData.searchTerm());
-    //kDebug() << "url is" << url << "!!!!!!!!!!!!!!!!!!!!!!!";
-
-    m_match.setText(actionText);
+    m_match.setText(i18n("Search %1 for %2", m_lastProvider, \
filterData.searchTerm()));  context.addMatch(term, m_match);
 }
 
-void WebshortcutRunner::run(const Plasma::RunnerContext &, const Plasma::QueryMatch \
&match) +void WebshortcutRunner::run(const Plasma::RunnerContext &context, const \
Plasma::QueryMatch &match)  {
-    const QString location = match.data().toString();
+    QString location;
 
-    if (!location.isEmpty()) {
+    //kDebug() << "filter before run?" << m_filterBeforeRun;
+    if (m_filterBeforeRun) {
+        m_filterBeforeRun = false;
+        //kDebug() << "look up webshortcut:" << context.query();
+        KUriFilterData filterData (context.query());
+        if (KUriFilter::self()->filterSearchUri(filterData, \
KUriFilter::WebShortcutFilter)) +            location = filterData.uri().url();
+    } else {
+        location = match.data().toString();
+    }
+
+    //kDebug() << location;
+    if (!location.isEmpty())
         KToolInvocation::invokeBrowser(location);
     }
-}
 
 #include "webshortcutrunner.moc"
--- trunk/KDE/kdebase/workspace/plasma/generic/runners/webshortcuts/webshortcutrunner.h \
#1172659:1172660 @@ -19,8 +19,6 @@
 #ifndef WEBSHORTCUTRUNNER_H
 #define WEBSHORTCUTRUNNER_H
 
-
-#include <KDirWatch>
 #include <KIcon>
 
 #include <Plasma/AbstractRunner>
@@ -40,17 +38,17 @@
 
     private Q_SLOTS:
         void readFiltersConfig();
-        void sycocaChanged(const QStringList &changes);
         void resetState();
 
     private:
-        QString m_delimiter;
-        Plasma::QueryMatch m_match;
         KIcon m_icon;
+        Plasma::QueryMatch m_match;
+        bool m_filterBeforeRun;
+
+        QChar m_delimiter;
         QString m_lastFailedKey;
         QString m_lastKey;
-        QString m_lastServiceName;
-        KDirWatch m_watch;
+        QString m_lastProvider;
 };
 
 K_EXPORT_PLASMA_RUNNER(webshortcuts, WebshortcutRunner)


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

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