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 	"Offline",
33 	"error",
34 	"Not Available",
35 	"Busy",
36 	"Away",
37 	"Available",
38 	"Free for Chat"
39 };
40 
41 static char *
get_window_name(XMPP_SERVER_REC * server)42 get_window_name(XMPP_SERVER_REC *server)
43 {
44 	g_return_val_if_fail(IS_XMPP_SERVER(server), NULL);
45 	return g_strconcat("(", (server->connrec->chatnet == NULL ||
46 	    *server->connrec->chatnet == '\0') ? server->jid :
47 	    server->connrec->chatnet, ")", (void *)NULL);
48 }
49 
50 char *
fe_xmpp_status_get_window_name(XMPP_SERVER_REC * server)51 fe_xmpp_status_get_window_name(XMPP_SERVER_REC *server)
52 {
53 	WINDOW_REC *window;
54 	char *name;
55 
56 	g_return_val_if_fail(IS_XMPP_SERVER(server), NULL);
57 	if ((name = get_window_name(server)) == NULL)
58 		return NULL;
59 	window = window_find_name(name);
60 	g_free(name);
61 	return (window != NULL) ? window->name : NULL;
62 }
63 
64 WINDOW_REC *
fe_xmpp_status_get_window(XMPP_SERVER_REC * server)65 fe_xmpp_status_get_window(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 	name = get_window_name(server);
72 	if ((window = window_find_name(name)) == NULL) {
73 		window = window_create(NULL, TRUE);
74 		window_set_name(window, name);
75 		window_change_server(window, server);
76 	}
77 	g_free(name);
78 	return window;
79 }
80 
81 static void
sig_presence_changed(XMPP_SERVER_REC * server,const char * full_jid,int show,const char * status)82 sig_presence_changed(XMPP_SERVER_REC *server, const char *full_jid,
83    int show, const char *status)
84 {
85 	XMPP_ROSTER_USER_REC *user;
86 	WINDOW_REC *window;
87 	const char *msg;
88 	char *name;
89 
90 	g_return_if_fail(IS_XMPP_SERVER(server));
91 	g_return_if_fail(full_jid != NULL);
92 	g_return_if_fail(0 <= show && show < XMPP_PRESENCE_SHOW_LEN);
93 	window = fe_xmpp_status_get_window(server);
94 	msg = fe_xmpp_presence_show[show];
95 	user = rosters_find_user(server->roster, full_jid, NULL, NULL);
96 	name = user != NULL && user->name != NULL ?
97 	    format_get_text(MODULE_NAME, NULL, server, NULL,
98 		XMPPTXT_FORMAT_NAME, user->name, full_jid) :
99 	    format_get_text(MODULE_NAME, NULL, server, NULL,
100 		XMPPTXT_FORMAT_JID, full_jid);
101 	if (status != NULL)
102 		printformat_module_window(MODULE_NAME, window, MSGLEVEL_CRAP,
103 		    XMPPTXT_PRESENCE_CHANGE_REASON, name, msg, status);
104 	else
105 		printformat_module_window(MODULE_NAME, window, MSGLEVEL_CRAP,
106 		    XMPPTXT_PRESENCE_CHANGE, name, msg);
107 	g_free(name);
108 }
109 
110 static void
sig_setup_changed(void)111 sig_setup_changed(void)
112 {
113 	signal_remove("xmpp presence changed", sig_presence_changed);
114 	if (settings_get_bool("xmpp_status_window"))
115 		signal_add("xmpp presence changed", sig_presence_changed);
116 }
117 
118 static void
sig_server_connecting(XMPP_SERVER_REC * server)119 sig_server_connecting(XMPP_SERVER_REC *server)
120 {
121 	if (!IS_XMPP_SERVER(server))
122 		return;
123 	if (settings_get_bool("xmpp_status_window"))
124 		fe_xmpp_status_get_window(server);
125 }
126 
127 void
fe_xmpp_status_init(void)128 fe_xmpp_status_init(void)
129 {
130 	signal_add("server connecting", (SIGNAL_FUNC)sig_server_connecting);
131 	signal_add("setup changed", (SIGNAL_FUNC)sig_setup_changed);
132 
133 	settings_add_bool("xmpp_lookandfeel", "xmpp_status_window", FALSE);
134 
135 	if (settings_get_bool("xmpp_status_window"))
136 		signal_add("xmpp presence changed", sig_presence_changed);
137 }
138 
139 void
fe_xmpp_status_deinit(void)140 fe_xmpp_status_deinit(void)
141 {
142 	signal_remove("server connecting", sig_server_connecting);
143 	signal_remove("setup changed", sig_setup_changed);
144 	signal_remove("xmpp presence changed", sig_presence_changed);
145 }
146