Debian Codegraph @

src/fe-common/fe-xmpp-status.c

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /*
2 * Copyright (C) 2007 Colin DIDIER
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18 #include <string.h>
19
20 #include "module.h"
21 #include "levels.h"
22 #include "module-formats.h"
23 #include "printtext.h"
24 #include "settings.h"
25 #include "signals.h"
26 #include "window-items.h"
27
28 #include "xmpp-servers.h"
29 #include "rosters-tools.h"
•30 #include "tools.h"
31
32 const char *fe_xmpp_presence_show[] = {
33 "Unavailable",
34 "Error",
35 "Extended Away",
36 "Do Not Disturb",
37 "Away",
38 "Available",
39 "Free for Chat",
40 "Online",
41 NULL
42 };
43
44 const int fe_xmpp_presence_show_format[] = {
45 XMPPTXT_PRESENCE_UNAVAILABLE,
46 XMPPTXT_PRESENCE_ERROR,
47 XMPPTXT_PRESENCE_XA,
48 XMPPTXT_PRESENCE_DND,
49 XMPPTXT_PRESENCE_AWAY,
50 XMPPTXT_PRESENCE_AVAILABLE,
51 XMPPTXT_PRESENCE_CHAT,
52 XMPPTXT_PRESENCE_ONLINE,
53 0,
54 };
55
56 static char *
57 get_window_name(XMPP_SERVER_REC *server)
58 {
59 g_return_val_if_fail(IS_XMPP_SERVER(server), NULL);
60 return g_strconcat("(", (server->connrec->chatnet == NULL ||
61 *server->connrec->chatnet == '\0') ? server->jid :
62 server->connrec->chatnet, ")", (void *)NULL);
63 }
64
65 char *
66 fe_xmpp_status_get_window_name(XMPP_SERVER_REC *server)
67 {
68 WINDOW_REC *window;
69 char *name;
70
71 g_return_val_if_fail(IS_XMPP_SERVER(server), NULL);
72 if ((name = get_window_name(server)) == NULL)
73 return NULL;
74 window = window_find_name(name);
75 g_free(name);
76 return (window != NULL) ? window->name : NULL;
77 }
78
79 WINDOW_REC *
80 fe_xmpp_status_get_window(XMPP_SERVER_REC *server)
81 {
82 WINDOW_REC *window;
83 char *name;
84
85 g_return_val_if_fail(IS_XMPP_SERVER(server), NULL);
86 name = get_window_name(server);
87 if ((window = window_find_name(name)) == NULL) {
88 window = window_create(NULL, TRUE);
89 window_set_name(window, name);
90 window_change_server(window, server);
91 }
92 g_free(name);
93 return window;
94 }
95
96 static void
97 sig_presence_changed(XMPP_SERVER_REC *server, const char *full_jid,
98 int show, const char *status)
99 {
100 XMPP_ROSTER_USER_REC *user;
101 WINDOW_REC *window;
•102 const char *msg, *stripped_jid;
103 char *name;
104
•105 stripped_jid = (settings_get_bool("xmpp_strip_resource")) ?
106 xmpp_strip_resource(full_jid) : g_strdup(full_jid);
107
108 g_return_if_fail(IS_XMPP_SERVER(server));
•109 g_return_if_fail(stripped_jid != NULL);
110 g_return_if_fail(0 <= show && show < XMPP_PRESENCE_SHOW_LEN);
111 window = fe_xmpp_status_get_window(server);
112 msg = fe_xmpp_presence_show[show];
•113 user = rosters_find_user(server->roster, stripped_jid, NULL, NULL);
114 name = user != NULL && user->name != NULL ?
115 format_get_text(MODULE_NAME, NULL, server, NULL,
•116 XMPPTXT_FORMAT_NAME, user->name, stripped_jid) :
117 format_get_text(MODULE_NAME, NULL, server, NULL,
•118 XMPPTXT_FORMAT_JID, stripped_jid);
119 if (status != NULL)
120 printformat_module_window(MODULE_NAME, window, MSGLEVEL_CRAP | MSGLEVEL_MODES,
121 XMPPTXT_PRESENCE_CHANGE_REASON, name, msg, status);
122 else
123 printformat_module_window(MODULE_NAME, window, MSGLEVEL_CRAP | MSGLEVEL_MODES,
124 XMPPTXT_PRESENCE_CHANGE, name, msg);
125 g_free(name);
126 }
127
128 static void
129 sig_setup_changed(void)
130 {
131 signal_remove("xmpp presence changed", sig_presence_changed);
132 if (settings_get_bool("xmpp_status_window"))
133 signal_add("xmpp presence changed", sig_presence_changed);
134 }
135
136 static void
137 sig_server_connecting(XMPP_SERVER_REC *server)
138 {
139 if (!IS_XMPP_SERVER(server))
140 return;
141 if (settings_get_bool("xmpp_status_window"))
142 fe_xmpp_status_get_window(server);
143 }
144
145 void
146 fe_xmpp_status_init(void)
147 {
148 signal_add("server connecting", (SIGNAL_FUNC)sig_server_connecting);
149 signal_add("setup changed", (SIGNAL_FUNC)sig_setup_changed);
150
151 settings_add_bool("xmpp_lookandfeel", "xmpp_status_window", FALSE);
152
•153 settings_add_bool("xmpp_lookandfeel", "xmpp_strip_resource", FALSE);
154
155 if (settings_get_bool("xmpp_status_window"))
156 signal_add("xmpp presence changed", sig_presence_changed);
157 }
158
159 void
160 fe_xmpp_status_deinit(void)
161 {
162 signal_remove("server connecting", sig_server_connecting);
163 signal_remove("setup changed", sig_setup_changed);
164 signal_remove("xmpp presence changed", sig_presence_changed);
165 }