[SCM] ktp-contact-runner packaging branch, master, updated. debian/15.12.1-2-244-g38a1f58

Maximiliano Curia maxy at moszumanska.debian.org
Sat May 28 17:50:30 UTC 2016


Gitweb-URL: http://git.debian.org/?p=pkg-kde/applications/ktp-contact-runner.git;a=commitdiff;h=54e79d4

The following commit has been merged in the master branch:
commit 54e79d4de8e43b6cd4930fd57e1f158b92bb7908
Author: Dan Vrátil <dan at progdan.cz>
Date:   Thu Aug 2 21:16:06 2012 +0200

    Add syntax for changing global presence
---
 src/contactrunner.cpp | 430 +++++++++++++++++++++++++++++++++-----------------
 src/contactrunner.h   |  14 +-
 2 files changed, 296 insertions(+), 148 deletions(-)

diff --git a/src/contactrunner.cpp b/src/contactrunner.cpp
index 84da2fb..3dc15da 100644
--- a/src/contactrunner.cpp
+++ b/src/contactrunner.cpp
@@ -20,7 +20,6 @@
 #include "contactrunner.h"
 
 #include <KDebug>
-#include <KIcon>
 #include <KFileDialog>
 #include <KMimeType>
 #include <KToolInvocation>
@@ -43,46 +42,66 @@
 #include <KTp/Models/accounts-filter-model.h>
 #include <KTp/Models/contact-model-item.h>
 #include <KTp/presence.h>
+#include <KTp/global-presence.h>
 
-#include <QAction>
-
-struct ContactInfo {
+struct MatchInfo {
     Tp::AccountPtr account;
     Tp::ContactPtr contact;
+    KTp::Presence presence;
 };
 
 Q_DECLARE_METATYPE(QModelIndex);
-Q_DECLARE_METATYPE(ContactInfo);
+Q_DECLARE_METATYPE(MatchInfo);
 
 ContactRunner::ContactRunner(QObject *parent, const QVariantList &args):
     Plasma::AbstractRunner(parent, args),
-    m_accountsModel(0)
+    m_accountsModel(new AccountsModel(this)),
+    m_globalPresence(new KTp::GlobalPresence(this))
 {
     Q_UNUSED(args);
 
-    setObjectName("IM Contacts Runner");
-
-    m_loggerDisabled = KStandardDirs::findExe("ktp-log-viewer").isEmpty();
+    setObjectName(QLatin1String("IM Contacts Runner"));
 
-    addSyntax(Plasma::RunnerSyntax(":q:", i18n("Finds all IM contacts matching :q:.")));
-    addSyntax(Plasma::RunnerSyntax("chat :q:", i18n("Finds all contacts matching :q: that are capable of text chats (default behavior)")));
-    addSyntax(Plasma::RunnerSyntax("audiocall :q:", i18n("Finds all contacts matching :q: that are capable of audio call and uses audio calls as default action.")));
-    addSyntax(Plasma::RunnerSyntax("videocall :q:", i18n("Finds all contacts matching :q: that are capable of video call and uses video calls as default action.")));
-    addSyntax(Plasma::RunnerSyntax("sendfile :q:", i18n("Finds all contacts matching :q: that are capable of receiving files and sends file as default action.")));
-    addSyntax(Plasma::RunnerSyntax("sharedesktop :q:", i18n("Finds all contacts matching :q: that are capable of sharing desktop and sets desktop sharing as default action.")));
+    m_loggerDisabled = KStandardDirs::findExe(QLatin1String("ktp-log-viewer")).isEmpty();
 
-    if (!m_loggerDisabled)
-        addSyntax(Plasma::RunnerSyntax("log :q:", i18n("Open the log viewer for :q:")));
+    addSyntax(Plasma::RunnerSyntax(QLatin1String(":q:"), i18n("Finds all IM contacts matching :q:.")));
+    addSyntax(Plasma::RunnerSyntax(QLatin1String("chat :q:"), i18n("Finds all contacts matching :q: that are capable of text chats (default behavior)")));
+    addSyntax(Plasma::RunnerSyntax(QLatin1String("audiocall :q:"), i18n("Finds all contacts matching :q: that are capable of audio call and uses audio calls as default action.")));
+    addSyntax(Plasma::RunnerSyntax(QLatin1String("videocall :q:"), i18n("Finds all contacts matching :q: that are capable of video call and uses video calls as default action.")));
+    addSyntax(Plasma::RunnerSyntax(QLatin1String("sendfile :q:"), i18n("Finds all contacts matching :q: that are capable of receiving files and sends file as default action.")));
+    addSyntax(Plasma::RunnerSyntax(QLatin1String("sharedesktop :q:"), i18n("Finds all contacts matching :q: that are capable of sharing desktop and sets desktop sharing as default action.")));
 
+    if (!m_loggerDisabled) {
+        addSyntax(Plasma::RunnerSyntax(QLatin1String("log :q:"), i18n("Open the log viewer for :q:")));
+    }
 
-    addAction("start-text-chat", QIcon::fromTheme("text-x-generic"), i18n("Start Chat"));
-    addAction("start-audio-call", QIcon::fromTheme("audio-headset"), i18n("Start Audio Call"));
-    addAction("start-video-call", QIcon::fromTheme("camera-web"), i18n("Start Video Call"));
-    addAction("start-file-transfer", QIcon::fromTheme("mail-attachment"), i18n("Send file(s)"));
-    addAction("start-desktop-sharing", QIcon::fromTheme("krfb"), i18n("Share My Desktop"));
-
-    if (!m_loggerDisabled)
-        addAction("show-log-viewer", QIcon::fromTheme("view-pim-journal"), i18n("Open the log viewer"));
+    QString imKeyword = i18nc("A keyword to change IM status", "im") + QLatin1String(" :q:");
+    QString statusKeyword = i18n("A keyword to change IM status", "status") + QLatin1String(" :q:");
+    Plasma::RunnerSyntax presenceSyntax(imKeyword, i18n("Change IM status"));
+    presenceSyntax.addExampleQuery(statusKeyword);
+    presenceSyntax.setSearchTermDescription(i18nc("Search term description", "status"));
+    addSyntax(presenceSyntax);
+
+    Plasma::RunnerSyntax presenceMsgSyntax(imKeyword + ' ' + i18nc("Description of a search term, please keep the brackets", "<status message>"),
+                                           i18n("Change IM status and set status message."));
+    presenceMsgSyntax.addExampleQuery(statusKeyword + ' ' + i18nc("Description of a search term, please keep the brackets", "<status message>"));
+    presenceMsgSyntax.setSearchTermDescription(i18nc("Search term description", "status"));
+    addSyntax(presenceMsgSyntax);
+
+    addSyntax(Plasma::RunnerSyntax(i18nc("A command to connect all IM accounts", "connect"),
+                                   i18n("Connect all IM accounts")));
+    addSyntax(Plasma::RunnerSyntax(i18nc("A command to disconnect all IM accounts", "disconnect"),
+                                   i18n("Disconnect al IM accounts")));;
+
+    addAction(QLatin1String("start-text-chat"), QIcon::fromTheme(QLatin1String("text-x-generic")), i18n("Start Chat"));
+    addAction(QLatin1String("start-audio-call"), QIcon::fromTheme(QLatin1String("audio-headset")), i18n("Start Audio Call"));
+    addAction(QLatin1String("start-video-call"), QIcon::fromTheme(QLatin1String("camera-web")), i18n("Start Video Call"));
+    addAction(QLatin1String("start-file-transfer"), QIcon::fromTheme(QLatin1String("mail-attachment")), i18n("Send file(s)"));
+    addAction(QLatin1String("start-desktop-sharing"), QIcon::fromTheme(QLatin1String("krfb")), i18n("Share My Desktop"));
+
+    if (!m_loggerDisabled) {
+        addAction(QLatin1String("show-log-viewer"), QIcon::fromTheme(QLatin1String("view-pim-journal")), i18n("Open the log viewer"));
+    }
 
     Tp::registerTypes();
     Tp::AccountFactoryPtr  accountFactory = Tp::AccountFactory::create(
@@ -111,7 +130,7 @@ ContactRunner::ContactRunner(QObject *parent, const QVariantList &args):
 
 ContactRunner::~ContactRunner()
 {
-    delete m_accountsModel;
+
 }
 
 void ContactRunner::accountManagerReady(Tp::PendingOperation *operation)
@@ -123,15 +142,15 @@ void ContactRunner::accountManagerReady(Tp::PendingOperation *operation)
 
     kDebug() << "Accounts manager is ready!";
 
-    m_accountsModel = new AccountsModel(this);
     m_accountsModel->setAccountManager(m_accountManager);
+    m_globalPresence->setAccountManager(m_accountManager);
 }
 
 QList< QAction* > ContactRunner::actionsForMatch(const Plasma::QueryMatch &match)
 {
     QList< QAction* > actions;
 
-    ContactInfo data = match.data().value< ContactInfo >();
+    MatchInfo data = match.data().value< MatchInfo >();
     if (!data.contact) {
         return actions;
     }
@@ -139,26 +158,27 @@ QList< QAction* > ContactRunner::actionsForMatch(const Plasma::QueryMatch &match
     Tp::ContactCapabilities capabilities = data.contact->capabilities();
 
     if (capabilities.textChats()) {
-        actions.append(action("start-text-chat"));
+        actions.append(action(QLatin1String("start-text-chat")));
 
-        if (!m_loggerDisabled)
-            actions.append(action("show-log-viewer"));
+        if (!m_loggerDisabled) {
+            actions.append(action(QLatin1String("show-log-viewer")));
+        }
     }
 
     if (capabilities.audioCalls()) {
-        actions.append(action("start-audio-call"));
+        actions.append(action(QLatin1String("start-audio-call")));
     }
 
     if (capabilities.videoCallsWithAudio()) {
-        actions.append(action("start-video-call"));
+        actions.append(action(QLatin1String("start-video-call")));
     }
 
     if (capabilities.fileTransfers()) {
-        actions.append(action("start-file-transfer"));
+        actions.append(action(QLatin1String("start-file-transfer")));
     }
 
-    if (capabilities.streamTubes("rfb")) {
-        actions.append(action("start-desktop-sharing"));
+    if (capabilities.streamTubes(QLatin1String("rfb"))) {
+        actions.append(action(QLatin1String("start-desktop-sharing")));
     }
 
     return actions;
@@ -174,39 +194,170 @@ void ContactRunner::match(Plasma::RunnerContext &context)
         return;
     }
 
-    if (!m_accountsModel || !m_accountManager->isReady()) {
+    if (!m_accountManager->isReady()) {
+        return;
+    }
+
+    /* First check for "im" or "status" keywords */
+    matchPresence(context);
+
+    /* But always try to match contacts too (in case somebody had a contact
+     * names "im..." */
+    matchContacts(context);
+}
+
+
+void ContactRunner::run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &match)
+{
+    Q_UNUSED(context)
+
+    MatchInfo data = match.data().value< MatchInfo >();
+    if (data.presence.isValid()) {
+        data.presence.setStatus(data.presence.type(), data.presence.status(), data.presence.statusMessage());
+        m_globalPresence->setPresence(data.presence);
+
+        return;
+    }
+
+    if (!data.account || !data.contact) {
+        kWarning() << "Running invalid contact info";
         return;
     }
 
+    /* Open chat/call/whaterver with contact */
+    Tp::AccountPtr account = data.account;
+    Tp::ContactPtr contact = data.contact;
+
+    Tp::ChannelRequestHints hints;
+    hints.setHint(QLatin1String("org.freedesktop.Telepathy.ChannelRequest"),
+                  QLatin1String("DelegateToPreferredHandler"),
+                  QVariant(true));
+
+    if (match.selectedAction() == action(QLatin1String("start-text-chat"))) {
+
+        account->ensureTextChat(contact,
+                                QDateTime::currentDateTime(),
+                                QLatin1String("org.freedesktop.Telepathy.Client.KTp.TextUi"),
+                                hints);
+
+    } else if (match.selectedAction() == action(QLatin1String("start-audio-call"))) {
+
+        account->ensureAudioCall(contact,
+                QLatin1String("audio"),
+                QDateTime::currentDateTime(),
+                QLatin1String("org.freedesktop.Telepathy.Client.KTp.CallUi"));
+
+    } else if (match.selectedAction() == action("start-video-call")) {
+
+        account->ensureAudioVideoCall(contact,
+                QLatin1String("audio"), QLatin1String("video"),
+                QDateTime::currentDateTime(),
+                QLatin1String("org.freedesktop.Telepathy.Client.KTp.CallUi"));
+
+    } else if (match.selectedAction() == action("start-file-transfer")) {
+
+        QStringList filenames = KFileDialog::getOpenFileNames(
+                                    KUrl("kfiledialog:///FileTransferLastDirectory"),
+                                    QString(),
+                                    0,
+                                    i18n("Choose files to send to %1", contact->alias()));
+
+        if (filenames.isEmpty()) { // User hit cancel button
+            return;
+        }
+
+        foreach (const QString &filename, filenames) {
+            Tp::FileTransferChannelCreationProperties properties(
+                filename, KMimeType::findByFileContent(filename)->name());
+
+            account->createFileTransfer(contact,
+                                        properties,
+                                        QDateTime::currentDateTime(),
+                                        QLatin1String("org.freedesktop.Telepathy.Client.KTp.FileTransfer"));
+        }
+
+    } else if (match.selectedAction() == action("start-desktop-sharing")) {
+
+        account->createStreamTube(contact,
+                                  QLatin1String("rfb"),
+                                  QDateTime::currentDateTime(),
+                                  QLatin1String("org.freedesktop.Telepathy.Client.krfb_rfb_handler"));
+
+    } else if (match.selectedAction() == action(QLatin1String("show-log-viewer"))) {
+
+        KToolInvocation::kdeinitExec(QLatin1String("ktp-log-viewer"),
+                                     QStringList() << account->uniqueIdentifier() << contact->id());
+
+    }
+}
+
+bool ContactRunner::hasCapability(const QModelIndex &contact, AccountsFilterModel::CapabilityFilterFlag capability) const
+{
+    if (capability == AccountsFilterModel::DoNotFilterByCapability) {
+        return true;
+    }
+
+    if ((capability == AccountsFilterModel::FilterByTextChatCapability) &&
+        contact.data(AccountsModel::TextChatCapabilityRole).toBool()) {
+        return true;
+    }
+
+    if ((capability == AccountsFilterModel::FilterByAudioCallCapability) &&
+        contact.data(AccountsModel::AudioCallCapabilityRole).toBool()) {
+        return true;
+    }
+
+    if ((capability == AccountsFilterModel::FilterByVideoCallCapability) &&
+        contact.data(AccountsModel::VideoCallCapabilityRole).toBool()) {
+        return true;
+    }
+
+    if ((capability == AccountsFilterModel::FilterByFileTransferCapability) &&
+        contact.data(AccountsModel::FileTransferCapabilityRole).toBool()) {
+        return true;
+    }
+
+    if ((capability == AccountsFilterModel::FilterByDesktopSharingCapability) &&
+        contact.data(AccountsModel::DesktopSharingCapabilityRole).toBool()) {
+        return true;
+    }
+
+    return false;
+}
+
+void ContactRunner::matchContacts(Plasma::RunnerContext &context)
+{
+    QString term = context.query();
+
     QAction *defaultAction;
     QString contactQuery;
     AccountsFilterModel::CapabilityFilterFlag filterFlag;
     if (term.startsWith(QLatin1String("chat "), Qt::CaseInsensitive)) {
-        defaultAction = action("start-text-chat");
+        defaultAction = action(QLatin1String("start-text-chat"));
         filterFlag = AccountsFilterModel::FilterByTextChatCapability;
-        contactQuery = term.mid(5).trimmed();
+        contactQuery = term.mid(QString("chat").length()).trimmed();
     } else if (term.startsWith(QLatin1String("audiocall "), Qt::CaseInsensitive)) {
-        defaultAction = action("start-audio-call");
+        defaultAction = action(QLatin1String("start-audio-call"));
         filterFlag = AccountsFilterModel::FilterByAudioCallCapability;
-        contactQuery = term.mid(10).trimmed();
+        contactQuery = term.mid(QString("audiocall").length()).trimmed();
     } else if (term.startsWith(QLatin1String("videocall "), Qt::CaseInsensitive)) {
-        defaultAction = action("start-video-call");
+        defaultAction = action(QLatin1String("start-video-call"));
         filterFlag = AccountsFilterModel::FilterByVideoCallCapability;
-        contactQuery = term.mid(10).trimmed();
+        contactQuery = term.mid(QString("videocall").length()).trimmed();
     } else if (term.startsWith(QLatin1String("sendfile "), Qt::CaseInsensitive)) {
-        defaultAction = action("start-file-transfer");
+        defaultAction = action(QLatin1String("start-file-transfer"));
         filterFlag = AccountsFilterModel::FilterByFileTransferCapability;
-        contactQuery = term.mid(9).trimmed();
+        contactQuery = term.mid(QString("sendfile").length()).trimmed();
     } else if (term.startsWith(QLatin1String("sharedesktop "), Qt::CaseInsensitive)) {
-        defaultAction = action("start-desktop-sharing");
+        defaultAction = action(QLatin1String("start-desktop-sharing"));
         filterFlag = AccountsFilterModel::FilterByDesktopSharingCapability;
-        contactQuery = term.mid(13).trimmed();
+        contactQuery = term.mid(QString("sharedesktop").length()).trimmed();
     } else if (term.startsWith(QLatin1String("log "), Qt::CaseInsensitive)) {
-        defaultAction = action("show-log-viewer");
+        defaultAction = action(QLatin1String("show-log-viewer"));
         filterFlag = AccountsFilterModel::DoNotFilterByCapability;
-        contactQuery = term.mid(4).trimmed();
+        contactQuery = term.mid(QString("log").length()).trimmed();
     } else {
-        defaultAction = action("start-text-chat");
+        defaultAction = action(QLatin1String("start-text-chat"));
         filterFlag = AccountsFilterModel::DoNotFilterByCapability;
         contactQuery = term;
     }
@@ -241,40 +392,33 @@ void ContactRunner::match(Plasma::RunnerContext &context)
 
             /* Store AccountsModelItem and ContactsModelItem as the data of match so that it can
              * be retrieved quickly later */
-            ContactInfo data;
+            MatchInfo data;
             data.account = accountIndex.data(AccountsModel::ItemRole).value< AccountsModelItem* >()->account();
             data.contact = contactIndex.data(AccountsModel::ItemRole).value< ContactModelItem* >()->contact();
             match.setData(qVariantFromValue(data));
 
-            match.setText(name.append(" (%1)").arg(accountIndex.data(AccountsModel::DisplayNameRole).toString()));
+            match.setText(name + QLatin1String(" (") +  accountIndex.data(AccountsModel::DisplayNameRole).toString() + ')');
             match.setType(Plasma::QueryMatch::ExactMatch);
 
-            QString iconName;
             KTp::Presence presence = contactIndex.data(AccountsModel::PresenceRole).value< KTp::Presence >();
             switch (presence.type()) {
             case Tp::ConnectionPresenceTypeAvailable:
-                iconName = "im-user";
                 relevance *= 10;
                 break;
             case Tp::ConnectionPresenceTypeBusy:
-                iconName = "im-user-busy";
                 relevance *= 8;
                 break;
             case Tp::ConnectionPresenceTypeAway:
             case Tp::ConnectionPresenceTypeExtendedAway:
-                iconName = "im-user-away";
                 relevance *= 6;
                 break;
             case Tp::ConnectionPresenceTypeHidden:
-                iconName = "im-invisible-user";
                 relevance *= 4;
                 break;
             case Tp::ConnectionPresenceTypeOffline:
-                iconName = "im-user-offline";
                 relevance *= 1;
                 break;
             default:
-                iconName = "im-user-offline";
                 relevance *= 1;
                 break;
             }
@@ -283,11 +427,11 @@ void ContactRunner::match(Plasma::RunnerContext &context)
             if (!iconFile.isEmpty() && QFile::exists(iconFile)) {
                 match.setIcon(QIcon(iconFile));
             } else {
-                match.setIcon(QIcon::fromTheme(iconName));
+                match.setIcon(presence.icon());
             }
 
             if (!presence.statusMessage().isEmpty()) {
-                match.setSubtext(presence.displayString() + " | " + presence.statusMessage());
+                match.setSubtext(presence.displayString() + QLatin1String(" | ") + presence.statusMessage());
             } else {
                 match.setSubtext(presence.displayString());
             }
@@ -300,113 +444,105 @@ void ContactRunner::match(Plasma::RunnerContext &context)
     }
 }
 
-void ContactRunner::run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &match)
+void ContactRunner::matchPresence(Plasma::RunnerContext &context)
 {
-    Q_UNUSED(context)
+    const QString imKeyword = i18nc("A keyword to change IM status", "im");
+    const QString statusKeyword = i18nc("A keyword to change IM status", "status");
+    const QString connectCommand = i18nc("A command to connect all IM accounts", "connect");
+    const QString disconnectCommand = i18nc("A command to disconnect all IM accounts", "disconnect");
 
-    ContactInfo data = match.data().value< ContactInfo >();
-    if (!data.account || !data.contact) {
-        kWarning() << "Running invalid contact info";
+    QString term = context.query().trimmed();
+
+    if (!term.startsWith(imKeyword) && !term.startsWith(statusKeyword) &&
+        (term != connectCommand) && (term != disconnectCommand)) {
         return;
     }
 
-    Tp::AccountPtr account = data.account;
-    Tp::ContactPtr contact = data.contact;
-
-    Tp::ChannelRequestHints hints;
-    hints.setHint("org.freedesktop.Telepathy.ChannelRequest", "DelegateToPreferredHandler", QVariant(true));
-
-    if (match.selectedAction() == action("start-text-chat")) {
-
-        account->ensureTextChat(contact,
-                                QDateTime::currentDateTime(),
-                                "org.freedesktop.Telepathy.Client.KTp.TextUi",
-                                hints);
-
-    } else if (match.selectedAction() == action("start-audio-call")) {
-
-        account->ensureAudioCall(contact,
-                QLatin1String("audio"),
-                QDateTime::currentDateTime(),
-                QLatin1String("org.freedesktop.Telepathy.Client.KTp.CallUi"));
-
-    } else if (match.selectedAction() == action("start-video-call")) {
-
-        account->ensureAudioVideoCall(contact,
-                QLatin1String("audio"), QLatin1String("video"),
-                QDateTime::currentDateTime(),
-                QLatin1String("org.freedesktop.Telepathy.Client.KTp.CallUi"));
-
-    } else if (match.selectedAction() == action("start-file-transfer")) {
-
-        QStringList filenames = KFileDialog::getOpenFileNames(
-                                    KUrl("kfiledialog:///FileTransferLastDirectory"),
-                                    QString(),
-                                    0,
-                                    i18n("Choose files to send to %1", contact->alias()));
-
-        if (filenames.isEmpty()) { // User hit cancel button
-            return;
-        }
-
-        foreach (const QString &filename, filenames) {
-            Tp::FileTransferChannelCreationProperties properties(
-                filename, KMimeType::findByFileContent(filename)->name());
-
-            account->createFileTransfer(contact,
-                                        properties,
-                                        QDateTime::currentDateTime(),
-                                        "org.freedesktop.Telepathy.Client.KTp.FileTransfer");
+    /* Display all available presences? */
+    bool all = ((term == imKeyword) || (term == statusKeyword));
+    QString presenceString;
+    QString statusMessage;
+
+    if (!all) {
+        /* Get string after the keyword */
+        QString cmd = term.mid(term.indexOf(' ')).trimmed();
+
+        int pos = cmd.indexOf(' ');
+        if (pos > 0) {
+            presenceString = cmd.mid(0, pos);
+            statusMessage = cmd.mid(pos + 1);
+        } else {
+            presenceString = cmd;
         }
+    }
 
-    } else if (match.selectedAction() == action("start-desktop-sharing")) {
-
-        account->createStreamTube(contact,
-                                  QLatin1String("rfb"),
-                                  QDateTime::currentDateTime(),
-                                  "org.freedesktop.Telepathy.Client.krfb_rfb_handler");
-
-    } else if (match.selectedAction() == action("show-log-viewer")) {
-
-        KToolInvocation::kdeinitExec(QLatin1String("ktp-log-viewer"),
-                                     QStringList() << account->uniqueIdentifier() << contact->id());
-
+    if (all || i18nc("IM presence", "online").contains(presenceString, Qt::CaseInsensitive) ||  (term == connectCommand)) {
+        addPresenceMatch(context, Tp::ConnectionPresenceTypeAvailable, statusMessage);
     }
-}
 
-bool ContactRunner::hasCapability(const QModelIndex &contact, AccountsFilterModel::CapabilityFilterFlag capability) const
-{
-    if (capability == AccountsFilterModel::DoNotFilterByCapability) {
-        return true;
+    if (all || i18nc("IM presence", "away").contains(presenceString, Qt::CaseInsensitive)) {
+        addPresenceMatch(context, Tp::ConnectionPresenceTypeAway, statusMessage);
     }
 
-    if ((capability == AccountsFilterModel::FilterByTextChatCapability) &&
-        contact.data(AccountsModel::TextChatCapabilityRole).toBool()) {
-        return true;
+    if (all || i18nc("IM presence","busy").contains(presenceString, Qt::CaseInsensitive)) {
+        addPresenceMatch(context, Tp::ConnectionPresenceTypeBusy, statusMessage);
     }
 
-    if ((capability == AccountsFilterModel::FilterByAudioCallCapability) &&
-        contact.data(AccountsModel::AudioCallCapabilityRole).toBool()) {
-        return true;
+    if (all || i18nc("IM presence","hidden").contains(presenceString, Qt::CaseInsensitive)) {
+        addPresenceMatch(context, Tp::ConnectionPresenceTypeHidden, statusMessage);
     }
 
-    if ((capability == AccountsFilterModel::FilterByVideoCallCapability) &&
-        contact.data(AccountsModel::VideoCallCapabilityRole).toBool()) {
-        return true;
+    if (all || i18nc("IM presence","offline").contains(presenceString, Qt::CaseInsensitive) || (term == disconnectCommand)) {
+        addPresenceMatch(context, Tp::ConnectionPresenceTypeOffline, statusMessage);
     }
+}
 
-    if ((capability == AccountsFilterModel::FilterByFileTransferCapability) &&
-        contact.data(AccountsModel::FileTransferCapabilityRole).toBool()) {
-        return true;
+void ContactRunner::addPresenceMatch(Plasma::RunnerContext &context, Tp::ConnectionPresenceType presence,
+                                     const QString &statusMessage)
+{
+    Plasma::QueryMatch match(this);
+    match.setType(Plasma::QueryMatch::ExactMatch);
+
+    MatchInfo data;
+
+    switch (presence) {
+        case Tp::ConnectionPresenceTypeAvailable:
+            data.presence = KTp::Presence(Tp::Presence::available());
+            match.setIcon(data.presence.icon());
+            match.setText(i18nc("Description of runner action", "Set IM status to online"));
+            break;
+        case Tp::ConnectionPresenceTypeAway:
+            data.presence = KTp::Presence(Tp::Presence::away());
+            match.setIcon(data.presence.icon());
+            match.setText(i18nc("Description of runner action", "Set IM status to away"));
+            break;
+        case Tp::ConnectionPresenceTypeBusy:
+            data.presence = KTp::Presence(Tp::Presence::busy());
+            match.setIcon(data.presence.icon());
+            match.setText(i18nc("Description of runner action", "Set IM status to busy"));
+            break;
+        case Tp::ConnectionPresenceTypeHidden:
+            data.presence = KTp::Presence(Tp::Presence::hidden());
+            match.setIcon(data.presence.icon());
+            match.setText(i18nc("Description of runner action", "Set IM status to hidden"));
+            break;
+        case Tp::ConnectionPresenceTypeOffline:
+            data.presence = KTp::Presence(Tp::Presence::offline());
+            match.setIcon(data.presence.icon());
+            match.setText(i18nc("Description of runner action", "Set IM status to offline"));
+            break;
+        default:
+            return;
     }
 
-    if ((capability == AccountsFilterModel::FilterByDesktopSharingCapability) &&
-        contact.data(AccountsModel::DesktopSharingCapabilityRole).toBool()) {
-        return true;
+    if (!statusMessage.isEmpty()) {
+        match.setSubtext(i18n("Status message: %1", statusMessage));
+        data.presence.setStatusMessage(statusMessage);
     }
 
-    return false;
-}
+    match.setData(qVariantFromValue(data));
 
+    context.addMatch(context.query(), match);
+}
 
 #include "contactrunner.moc"
diff --git a/src/contactrunner.h b/src/contactrunner.h
index 54e151a..55e7e9d 100644
--- a/src/contactrunner.h
+++ b/src/contactrunner.h
@@ -31,12 +31,17 @@
 
 #include <TelepathyQt/AccountManager>
 
-class QAction;
+namespace KTp
+{
+    class GlobalPresence;
+}
 
 namespace Tp {
     class PendingOperation;
 }
 
+class QAction;
+
 class ContactRunner : public Plasma::AbstractRunner
 {
     Q_OBJECT
@@ -57,7 +62,14 @@ class ContactRunner : public Plasma::AbstractRunner
   private:
     bool hasCapability(const QModelIndex &contact, AccountsFilterModel::CapabilityFilterFlag capability) const;
 
+    void matchPresence(Plasma::RunnerContext &context);
+    void matchContacts(Plasma::RunnerContext &context);
+
+    void addPresenceMatch(Plasma::RunnerContext &context, Tp::ConnectionPresenceType presence,
+                          const QString &statusMessage);
+
     AccountsModel *m_accountsModel;
+    KTp::GlobalPresence *m_globalPresence;
     Tp::AccountManagerPtr m_accountManager;
 
     bool m_loggerDisabled;

-- 
ktp-contact-runner packaging



More information about the pkg-kde-commits mailing list