]> git.zarvox.org Git - imoo.git/commitdiff
ribbon: emit signal when IMs are sent successfully
authorDrew Fisher <drew.m.fisher@gmail.com>
Sun, 18 May 2014 02:04:42 +0000 (19:04 -0700)
committerDrew Fisher <drew.m.fisher@gmail.com>
Sun, 18 May 2014 05:26:14 +0000 (22:26 -0700)
ribbon/ribbonconversations.cpp
ribbon/ribbonconversations.h

index 8f2dd5a0c23b4f21996999f45c58e9fb08cc3fc3..15cd11b727537075863e33d1dedebf3d47fcaf4e 100644 (file)
@@ -25,6 +25,22 @@ static void static_buddy_typing_stopped(PurpleAccount* account, const char* name
        c->buddy_typing(account, name, RibbonConversations::STOPPED);
 }
 
+static void static_sent_im_msg(PurpleAccount* account, const char* receiver,
+                               const char* message, void* data)
+{
+       RibbonConversations* c = (RibbonConversations*)data;
+       c->sent_im_msg(account, receiver, message);
+}
+
+static QString canonicalized(QString buddy)
+{
+       // Strips everything right of the slash to remove a Jabber resource ID.
+       int slash_pos = buddy.indexOf("/");
+       if (slash_pos != -1)
+               return buddy.left(slash_pos);
+       return buddy;
+}
+
 RibbonConversations::RibbonConversations(RibbonManager* parent) : QObject((QObject*)parent)
 {
        _manager = parent;
@@ -44,6 +60,8 @@ void RibbonConversations::init()
                        PURPLE_CALLBACK(static_buddy_typing), this);
        purple_signal_connect(conversations_handle, "buddy-typing-stopped", &handle,
                        PURPLE_CALLBACK(static_buddy_typing_stopped), this);
+       purple_signal_connect(conversations_handle, "sent-im-msg", &handle,
+                       PURPLE_CALLBACK(static_sent_im_msg), this);
 }
 
 void RibbonConversations::received_im_msg(PurpleAccount *account,
@@ -54,7 +72,7 @@ void RibbonConversations::received_im_msg(PurpleAccount *account,
                conv = purple_conversation_new(PURPLE_CONV_TYPE_IM, account, sender);
        }
        QVariantMap m = RibbonAccounts::context(account);
-       m["buddy"] = QString::fromUtf8(sender);
+       m["buddy"] = canonicalized(QString::fromUtf8(sender));
        m["timestamp"] = QDateTime::currentDateTimeUtc();
        m["message"] = QString::fromUtf8(message);
        emit event(QString::fromUtf8("conversations"),
@@ -66,7 +84,7 @@ void RibbonConversations::received_im_msg(PurpleAccount *account,
 void RibbonConversations::buddy_typing(PurpleAccount *account, const char* buddy, TypingState state)
 {
        QVariantMap m = RibbonAccounts::context(account);
-       m["buddy"] = QString::fromUtf8(buddy);
+       m["buddy"] = canonicalized(QString::fromUtf8(buddy));
        switch (state) {
                case STOPPED:      m["state"] = QString::fromUtf8("stopped"); break;
                case TYPING:       m["state"] = QString::fromUtf8("typing"); break;
@@ -86,15 +104,27 @@ void RibbonConversations::send_im(QString protocol, QString account, QString bud
                return;
        }
        PurpleConversation* p_conv = purple_find_conversation_with_account(PURPLE_CONV_TYPE_IM,
-               buddy.toUtf8(), p_acct);
+               canonicalized(buddy).toUtf8(), p_acct);
        if (!p_conv) {
                // Lazily create conversation objects if needed
-               p_conv = purple_conversation_new(PURPLE_CONV_TYPE_IM, p_acct, buddy.toUtf8());
+               p_conv = purple_conversation_new(PURPLE_CONV_TYPE_IM, p_acct, canonicalized(buddy).toUtf8());
        }
        PurpleConvIm* p_conv_im = purple_conversation_get_im_data(p_conv);
        purple_conv_im_send(p_conv_im, message.toUtf8());
 }
 
+void RibbonConversations::sent_im_msg(PurpleAccount* account, const char* buddy,
+                                  const char* message)
+{
+       QVariantMap m = RibbonAccounts::context(account);
+       m["buddy"] = canonicalized(QString::fromUtf8(buddy));
+       m["message"] = QString::fromUtf8(message);
+       qDebug() << "successfully sent message to" << m["buddy"];
+       emit event(QString::fromUtf8("conversations"),
+                  QString::fromUtf8("sent_im"),
+                  m);
+}
+
 void RibbonConversations::handleExternalEvent(QString method, QVariantMap context)
 {
        if (method == "send_im") {
index 99e50d817cf332135803145c3ec394c6d381ac05..efba2bcd8256b597a35436b8f647881b40fbc100 100644 (file)
@@ -22,6 +22,7 @@ public:
                             PurpleConversation *conv, PurpleMessageFlags flags);
        void buddy_typing(PurpleAccount *account, const char* buddy, TypingState state);
        void handleExternalEvent(QString method, QVariantMap context);
+       void sent_im_msg(PurpleAccount* account, const char* buddy, const char* message);
 signals:
        void event(QString source, QString signal, QVariantMap context);
 private: