1 #include "ribbonconnections.h"
2 #include "ribbonmanager.h"
3 #include "ribbonaccounts.h"
7 static void connect_progress(PurpleConnection *gc, const char* text, size_t step, size_t step_count)
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);
20 static void connection_notice(PurpleConnection *gc, const char* text)
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);
30 static void connection_disconnected(PurpleConnection *gc, const char* text)
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);
40 static void network_connected(void)
42 qWarning() << "Network connection established";
45 static void network_disconnected(void)
47 qWarning() << "This machine has been disconnected from the internet";
50 static void report_disconnect_reason(PurpleConnection *gc, PurpleConnectionError reason, const char *text)
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);
61 static PurpleConnectionUiOps connection_uiops =
63 connect_progress, /* connect_progress */
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 */
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)
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));
86 RibbonConnections::RibbonConnections(RibbonManager* parent) : QObject((QObject*)parent)
91 RibbonConnections::~RibbonConnections()
95 void RibbonConnections::init(void)
97 purple_connections_set_ui_ops(&connection_uiops);
99 void *connections_handle = purple_connections_get_handle();
101 purple_signal_connect(connections_handle, "signed-on", &handle,
102 PURPLE_CALLBACK(signed_on), this);