Debian Codegraph @

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

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
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
31 const char *fe_xmpp_presence_show[] = {
32 "Unavailable",
33 "Error",
34 "Extended Away",
35 "Do Not Disturb",
36 "Away",
37 "Available",
38 "Free for Chat",
39 "Online",
40 NULL
41 };
42
43 const int fe_xmpp_presence_show_format[] = {
44 XMPPTXT_PRESENCE_UNAVAILABLE,
45 XMPPTXT_PRESENCE_ERROR,
46 XMPPTXT_PRESENCE_XA,
47 XMPPTXT_PRESENCE_DND,
48 XMPPTXT_PRESENCE_AWAY,
49 XMPPTXT_PRESENCE_AVAILABLE,
50 XMPPTXT_PRESENCE_CHAT,
51 XMPPTXT_PRESENCE_ONLINE,
52 0,
53 };
54
55 static char *
56 get_window_name(XMPP_SERVER_REC *server)
57 {
58 g_return_val_if_fail(IS_XMPP_SERVER(server), NULL);
59 return g_strconcat("(", (server->connrec->chatnet == NULL ||
60 *server->connrec->chatnet == '\0') ? server->jid :
61 server->connrec->chatnet, ")", (void *)NULL);
62 }
63
64 char *
65 fe_xmpp_status_get_window_name(XMPP_SERVER_REC *server)
66 {
67 WINDOW_REC *window;
68 char *name;
69
70 g_return_val_if_fail(IS_XMPP_SERVER(server), NULL);
71 if ((name = get_window_name(server)) == NULL)
72 return NULL;
73 window = window_find_name(name);
74 g_free(name);
75 return (window != NULL) ? window->name : NULL;
76 }
77
78 WINDOW_REC *
79 fe_xmpp_status_get_window(XMPP_SERVER_REC *server)
80 {
81 WINDOW_REC *window;
82 char *name;
83
84 g_return_val_if_fail(IS_XMPP_SERVER(server), NULL);
85 name = get_window_name(server);
86 if ((window = window_find_name(name)) == NULL) {
87 window = window_create(NULL, TRUE);
88 window_set_name(window, name);
89 window_change_server(window, server);
90 }
91 g_free(name);
92 return window;
93 }
94
95 static void
96 sig_presence_changed(XMPP_SERVER_REC *server, const char *full_jid,
97 int show, const char *status)
98 {
99 XMPP_ROSTER_USER_REC *user;
100 WINDOW_REC *window;
101 const char *msg;
102 char *name;
103
104 g_return_if_fail(IS_XMPP_SERVER(server));
105 g_return_if_fail(full_jid != NULL);
106 g_return_if_fail(0 <= show && show < XMPP_PRESENCE_SHOW_LEN);
107 window = fe_xmpp_status_get_window(server);
108 msg = fe_xmpp_presence_show[show];
109 user = rosters_find_user(server->roster, full_jid, NULL, NULL);
110 name = user != NULL && user->name != NULL ?
111 format_get_text(MODULE_NAME, NULL, server, NULL,
112 XMPPTXT_FORMAT_NAME, user->name, full_jid) :
113 format_get_text(MODULE_NAME, NULL, server, NULL,
114 XMPPTXT_FORMAT_JID, full_jid);
115 if (status != NULL)
116 printformat_module_window(MODULE_NAME, window, MSGLEVEL_CRAP | MSGLEVEL_MODES,
117 XMPPTXT_PRESENCE_CHANGE_REASON, name, msg, status);
118 else
119 printformat_module_window(MODULE_NAME, window, MSGLEVEL_CRAP | MSGLEVEL_MODES,
120 XMPPTXT_PRESENCE_CHANGE, name, msg);
121 g_free(name);
122 }
123
124 static void
125 sig_setup_changed(void)
126 {
127 signal_remove("xmpp presence changed", sig_presence_changed);
128 if (settings_get_bool("xmpp_status_window"))
129 signal_add("xmpp presence changed", sig_presence_changed);
130 }
131
132 static void
133 sig_server_connecting(XMPP_SERVER_REC *server)
134 {
135 if (!IS_XMPP_SERVER(server))
136 return;
137 if (settings_get_bool("xmpp_status_window"))
138 fe_xmpp_status_get_window(server);
139 }
140
141 void
142 fe_xmpp_status_init(void)
143 {
144 signal_add("server connecting", (SIGNAL_FUNC)sig_server_connecting);
145 signal_add("setup changed", (SIGNAL_FUNC)sig_setup_changed);
146
147 settings_add_bool("xmpp_lookandfeel", "xmpp_status_window", FALSE);
148
149 if (settings_get_bool("xmpp_status_window"))
150 signal_add("xmpp presence changed", sig_presence_changed);
151 }
152
153 void
154 fe_xmpp_status_deinit(void)
155 {
156 signal_remove("server connecting", sig_server_connecting);
157 signal_remove("setup changed", sig_setup_changed);
158 signal_remove("xmpp presence changed", sig_presence_changed);
159 }