Update list of wide characters
[centerim5.git] / src / Connections.cpp
blob19f0f4a074197da2dc38d4d4f85cde1119ea28f9
1 // Copyright (C) 2007 Mark Pustjens <pustjens@dds.nl>
2 // Copyright (C) 2010-2015 Petr Pavlu <setup@dagobah.cz>
3 //
4 // This file is part of CenterIM.
5 //
6 // CenterIM is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
11 // CenterIM is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with CenterIM. If not, see <http://www.gnu.org/licenses/>.
19 #include "Connections.h"
21 #include "Log.h"
23 #include "gettext.h"
24 #include <cstring>
26 #define RECONNECTION_DELAY_MIN 60000
27 #define RECONNECTION_DELAY_MAX 120000
29 Connections *Connections::my_instance_ = nullptr;
31 Connections *Connections::instance()
33 return my_instance_;
36 Connections::Connections()
38 // Set the purple connection callbacks.
39 std::memset(
40 &centerim_connection_ui_ops, 0, sizeof(centerim_connection_ui_ops));
41 centerim_connection_ui_ops.connect_progress = connect_progress_;
42 centerim_connection_ui_ops.connected = connected_;
43 centerim_connection_ui_ops.disconnected = disconnected_;
44 centerim_connection_ui_ops.notice = notice_;
45 // Deprecated in favour of report_disconnect_reason().
46 // centerim_connection_ui_ops.report_disconnect = report_disconnect_;
47 centerim_connection_ui_ops.network_connected = network_connected_;
48 centerim_connection_ui_ops.network_disconnected = network_disconnected_;
49 centerim_connection_ui_ops.report_disconnect_reason =
50 report_disconnect_reason_;
51 purple_connections_set_ui_ops(&centerim_connection_ui_ops);
54 Connections::~Connections()
56 purple_connections_set_ui_ops(nullptr);
59 void Connections::init()
61 g_assert(my_instance_ == nullptr);
63 my_instance_ = new Connections;
66 void Connections::finalize()
68 g_assert(my_instance_ != nullptr);
70 delete my_instance_;
71 my_instance_ = nullptr;
74 void Connections::reconnectAccount(PurpleAccount *account)
76 g_return_if_fail(account != nullptr);
78 if (!purple_account_is_disconnected(account) ||
79 !purple_status_is_online(purple_account_get_active_status(account)))
80 return;
82 purple_account_connect(account);
85 void Connections::connect_progress(PurpleConnection *gc, const char *text,
86 std::size_t /*step*/, std::size_t /*step_count*/)
88 PurpleAccount *account = purple_connection_get_account(gc);
89 LOG->message(_("+ [%s] %s: %s"), purple_account_get_protocol_name(account),
90 purple_account_get_username(account), text);
93 void Connections::connected(PurpleConnection *gc)
95 PurpleAccount *account = purple_connection_get_account(gc);
96 LOG->message(_("+ [%s] %s: Connected"),
97 purple_account_get_protocol_name(account),
98 purple_account_get_username(account));
101 void Connections::disconnected(PurpleConnection *gc)
103 PurpleAccount *account = purple_connection_get_account(gc);
104 LOG->message(_("+ [%s] %s: Disconnected"),
105 purple_account_get_protocol_name(account),
106 purple_account_get_username(account));
109 void Connections::notice(PurpleConnection *gc, const char *text)
111 PurpleAccount *account = purple_connection_get_account(gc);
112 LOG->message(_("+ [%s] %s: %s"), purple_account_get_protocol_name(account),
113 purple_account_get_username(account), text);
116 void Connections::network_connected()
118 LOG->message(_("+ Network connected"));
120 GList *list, *l;
121 l = list = purple_accounts_get_all_active();
122 while (l != nullptr) {
123 PurpleAccount *account = reinterpret_cast<PurpleAccount *>(l->data);
124 if (purple_account_is_disconnected(account))
125 reconnectAccount(account);
127 l = l->next;
129 g_list_free(list);
132 void Connections::network_disconnected()
134 LOG->message(_("+ Network disconnected"));
136 GList *list, *l;
137 l = list = purple_accounts_get_all_active();
138 while (l != nullptr) {
139 PurpleAccount *a = reinterpret_cast<PurpleAccount *>(l->data);
140 if (!purple_account_is_disconnected(a))
141 purple_account_disconnect(a);
143 l = l->next;
145 g_list_free(list);
148 void Connections::report_disconnect_reason(
149 PurpleConnection *gc, PurpleConnectionError reason, const char *text)
151 PurpleAccount *account = purple_connection_get_account(gc);
152 const char *protocol = purple_account_get_protocol_name(account);
153 const char *username = purple_account_get_username(account);
155 LOG->message(_("+ [%s] %s: %s"), protocol, username, text);
157 if (!purple_connection_error_is_fatal(reason)) {
158 unsigned delay =
159 g_random_int_range(RECONNECTION_DELAY_MIN, RECONNECTION_DELAY_MAX);
160 CENTERIM->timeoutOnceConnect(
161 sigc::bind(sigc::mem_fun(this, &Connections::reconnectAccount), account),
162 delay);
163 LOG->message(ngettext("+ [%s] %s: Auto-reconnection in %d second",
164 "+ [%s] %s: Auto-reconnection in %d seconds", delay / 1000),
165 protocol, username, delay / 1000);
167 else {
168 purple_account_set_enabled(account, PACKAGE_NAME, FALSE);
169 LOG->message(_("+ [%s] %s: Account disabled"), protocol, username);
173 // vim: set tabstop=2 shiftwidth=2 textwidth=80 expandtab: