cc16ad1e60877141531fb466e46ad9340e73a4d1
[crypto.git] / apps / irssi / src / core / queries.c
1 /*
2  queries.c : irssi
3
4     Copyright (C) 1999-2000 Timo Sirainen
5
6     This program 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.
10
11     This program 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.
15
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 #include "module.h"
22 #include "signals.h"
23 #include "misc.h"
24
25 #include "servers.h"
26 #include "queries.h"
27
28 GSList *queries;
29
30 void query_init(QUERY_REC *query, int automatic)
31 {
32         g_return_if_fail(query != NULL);
33         g_return_if_fail(query->name != NULL);
34
35         queries = g_slist_append(queries, query);
36
37         MODULE_DATA_INIT(query);
38         query->type = module_get_uniq_id_str("WINDOW ITEM TYPE", "QUERY");
39         query->destroy = (void (*) (WI_ITEM_REC *)) query_destroy;
40         if (query->server_tag != NULL) {
41                 query->server = server_find_tag(query->server_tag);
42                 if (query->server != NULL) {
43                         query->server->queries =
44                                 g_slist_append(query->server->queries, query);
45                 }
46         }
47
48         signal_emit("query created", 2, query, GINT_TO_POINTER(automatic));
49 }
50
51 void query_destroy(QUERY_REC *query)
52 {
53         g_return_if_fail(IS_QUERY(query));
54
55         if (query->destroying) return;
56         query->destroying = TRUE;
57
58         queries = g_slist_remove(queries, query);
59         if (query->server != NULL) {
60                 query->server->queries =
61                         g_slist_remove(query->server->queries, query);
62         }
63         signal_emit("query destroyed", 1, query);
64
65         MODULE_DATA_DEINIT(query);
66         g_free_not_null(query->hilight_color);
67         g_free_not_null(query->server_tag);
68         g_free_not_null(query->address);
69         g_free(query->name);
70
71         query->type = 0;
72         g_free(query);
73 }
74
75 static QUERY_REC *query_find_server(SERVER_REC *server, const char *nick)
76 {
77         GSList *tmp;
78
79         g_return_val_if_fail(IS_SERVER(server), NULL);
80
81         if (server->query_find_func != NULL) {
82                 /* use the server specific query find function */
83                 return server->query_find_func(server, nick);
84         }
85
86         for (tmp = server->queries; tmp != NULL; tmp = tmp->next) {
87                 QUERY_REC *rec = tmp->data;
88
89                 if (g_strcasecmp(rec->name, nick) == 0)
90                         return rec;
91         }
92
93         return NULL;
94 }
95
96 QUERY_REC *query_find(SERVER_REC *server, const char *nick)
97 {
98         GSList *tmp;
99
100         g_return_val_if_fail(server == NULL || IS_SERVER(server), NULL);
101         g_return_val_if_fail(nick != NULL, NULL);
102
103         if (server != NULL)
104                 return query_find_server(server, nick);
105
106         for (tmp = queries; tmp != NULL; tmp = tmp->next) {
107                 QUERY_REC *rec = tmp->data;
108
109                 if (g_strcasecmp(rec->name, nick) == 0)
110                         return rec;
111         }
112
113         return NULL;
114 }
115
116 void query_change_nick(QUERY_REC *query, const char *nick)
117 {
118         char *oldnick;
119
120         g_return_if_fail(IS_QUERY(query));
121
122         oldnick = query->name;
123         query->name = g_strdup(nick);
124         signal_emit("query nick changed", 2, query, oldnick);
125         g_free(oldnick);
126 }
127
128 void query_change_address(QUERY_REC *query, const char *address)
129 {
130         g_return_if_fail(IS_QUERY(query));
131
132         g_free_not_null(query->address);
133         query->address = g_strdup(address);
134         signal_emit("query address changed", 1, query);
135 }
136
137 void query_change_server(QUERY_REC *query, SERVER_REC *server)
138 {
139         g_return_if_fail(IS_QUERY(query));
140
141         if (query->server != NULL) {
142                 query->server->queries =
143                         g_slist_remove(query->server->queries, query);
144         }
145         if (server != NULL)
146                 server->queries = g_slist_append(server->queries, query);
147
148         query->server = server;
149         signal_emit("query server changed", 1, query);
150 }
151
152 void queries_init(void)
153 {
154 }
155
156 void queries_deinit(void)
157 {
158         module_uniq_destroy("QUERY");
159 }