]> git.zarvox.org Git - imoo.git/blob - ribbon/ribbonconnections.cpp
Fix misnamed parameter in log line
[imoo.git] / ribbon / ribbonconnections.cpp
1 #include "ribbonconnections.h"
2 #include "ribbonmanager.h"
3 #include "ribbonaccounts.h"
4
5 #include <QDebug>
6
7 static void connect_progress(PurpleConnection *gc, const char* text, size_t step, size_t step_count)
8 {
9         PurpleAccount* account = purple_connection_get_account(gc);
10         RibbonManager* r = static_cast<RibbonManager*>(account->ui_data);
11         qDebug() << QString("Connection progress (%1:%2):").arg(account->protocol_id, account->username).toUtf8().constData() << text << step << "of" << step_count;
12         // TODO: add enough unique information from account to ctx to handle the message
13         QVariantMap ctx = RibbonAccounts::context(account);
14         ctx["step"] = QVariant((qulonglong)step);
15         ctx["step_count"] = QVariant((qulonglong)step_count);
16         ctx["text"] = QVariant(QString::fromUtf8(text));
17         r->event(QString("connection"), QString("connect_progress"), ctx);
18 }
19
20 static void connection_notice(PurpleConnection *gc, const char* text)
21 {
22         qDebug() << "Connection notice:" << text;
23         PurpleAccount* account = purple_connection_get_account(gc);
24         RibbonManager* r = static_cast<RibbonManager*>(account->ui_data);
25         QVariantMap ctx = RibbonAccounts::context(account);
26         ctx["message"] = QVariant(QString::fromUtf8(text));
27         r->event(QString("connection"), QString("connect_notice"), ctx);
28 }
29
30 static void connection_disconnected(PurpleConnection *gc, const char* text)
31 {
32         qDebug() << "Connection disconnected:" << text;
33         PurpleAccount* account = purple_connection_get_account(gc);
34         RibbonManager* r = static_cast<RibbonManager*>(account->ui_data);
35         QVariantMap ctx = RibbonAccounts::context(account);
36         ctx["message"] = QVariant(QString::fromUtf8(text));
37         r->event(QString("connection"), QString("connection_disconnected"), ctx);
38 }
39
40 static void network_connected(void)
41 {
42         qWarning() << "Network connection established";
43 }
44
45 static void network_disconnected(void)
46 {
47         qWarning() << "This machine has been disconnected from the internet";
48 }
49
50 static void report_disconnect_reason(PurpleConnection *gc, PurpleConnectionError reason, const char *text)
51 {
52         PurpleAccount *account = purple_connection_get_account(gc);
53         //qDebug() << QString("Connection disconnected: \'%1\' (%2)\n  >Error: %3\n  >Reason: %4\n").arg(purple_account_get_username(account), purple_account_get_protocol_id(account), qint32(reason), text);
54         RibbonManager* r = static_cast<RibbonManager*>(account->ui_data);
55         QVariantMap ctx = RibbonAccounts::context(account);
56         ctx["reason_code"] = QVariant(qint32(reason));
57         ctx["reason_text"] = QVariant(QString::fromUtf8(text));
58         r->event(QString("connection"), QString("disconnect_reason"), ctx);
59 }
60
61 static PurpleConnectionUiOps connection_uiops =
62 {
63         connect_progress,          /* connect_progress         */
64         NULL,                      /* connected                */
65         NULL,                      /* disconnected             */
66         connection_notice,         /* notice                   */
67         connection_disconnected,   /* report_disconnect        */
68         network_connected,         /* network_connected        */
69         network_disconnected,      /* network_disconnected     */
70         report_disconnect_reason,  /* report_disconnect_reason */
71         NULL,
72         NULL,
73         NULL
74 };
75
76 // TODO: note that the void* data can be set at purple_signal_connect time, and
77 // set it to the class instance to allow for convenient upcalls
78 // Then, actually do upcalls and emit the appropriate signals.
79 static void signed_on(PurpleConnection *gc, void* data)
80 {
81         Q_UNUSED(data);
82         PurpleAccount *account = purple_connection_get_account(gc);
83         printf("Account connected: \"%s\" (%s)\n", purple_account_get_username(account), purple_account_get_protocol_id(account));
84 }
85
86 RibbonConnections::RibbonConnections(RibbonManager* parent) : QObject((QObject*)parent)
87 {
88         _manager = parent;
89 }
90
91 RibbonConnections::~RibbonConnections()
92 {
93 }
94
95 void RibbonConnections::init(void)
96 {
97         purple_connections_set_ui_ops(&connection_uiops);
98         static int handle;
99         void *connections_handle = purple_connections_get_handle();
100
101         purple_signal_connect(connections_handle, "signed-on", &handle,
102                         PURPLE_CALLBACK(signed_on), this);
103 }