imported.
[runtime.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         if (query->server_tag != NULL) {
40                 query->server = server_find_tag(query->server_tag);
41                 if (query->server != NULL) {
42                         query->server->queries =
43                                 g_slist_append(query->server->queries, query);
44                 }
45         }
46
47         signal_emit("query created", 2, query, GINT_TO_POINTER(automatic));
48 }
49
50 void query_destroy(QUERY_REC *query)
51 {
52         g_return_if_fail(IS_QUERY(query));
53
54         if (query->destroying) return;
55         query->destroying = TRUE;
56
57         queries = g_slist_remove(queries, query);
58         if (query->server != NULL) {
59                 query->server->queries =
60                         g_slist_remove(query->server->queries, query);
61         }
62         signal_emit("query destroyed", 1, query);
63
64         MODULE_DATA_DEINIT(query);
65         g_free_not_null(query->hilight_color);
66         g_free_not_null(query->server_tag);
67         g_free_not_null(query->address);
68         g_free(query->name);
69         g_free(query);
70 }
71
72 static QUERY_REC *query_find_server(SERVER_REC *server, const char *nick)
73 {
74         GSList *tmp;
75
76         g_return_val_if_fail(IS_SERVER(server), NULL);
77
78         if (server->query_find_func != NULL) {
79                 /* use the server specific query find function */
80                 return server->query_find_func(server, nick);
81         }
82
83         for (tmp = server->queries; tmp != NULL; tmp = tmp->next) {
84                 QUERY_REC *rec = tmp->data;
85
86                 if (g_strcasecmp(rec->name, nick) == 0)
87                         return rec;
88         }
89
90         return NULL;
91 }
92
93 QUERY_REC *query_find(SERVER_REC *server, const char *nick)
94 {
95         GSList *tmp;
96
97         g_return_val_if_fail(server == NULL || IS_SERVER(server), NULL);
98         g_return_val_if_fail(nick != NULL, NULL);
99
100         if (server != NULL)
101                 return query_find_server(server, nick);
102
103         for (tmp = queries; tmp != NULL; tmp = tmp->next) {
104                 QUERY_REC *rec = tmp->data;
105
106                 if (g_strcasecmp(rec->name, nick) == 0)
107                         return rec;
108         }
109
110         return NULL;
111 }
112
113 void query_change_nick(QUERY_REC *query, const char *nick)
114 {
115         char *oldnick;
116
117         g_return_if_fail(IS_QUERY(query));
118
119         oldnick = query->name;
120         query->name = g_strdup(nick);
121         signal_emit("query nick changed", 2, query, oldnick);
122         g_free(oldnick);
123 }
124
125 void query_change_address(QUERY_REC *query, const char *address)
126 {
127         g_return_if_fail(IS_QUERY(query));
128
129         g_free_not_null(query->address);
130         query->address = g_strdup(address);
131         signal_emit("query address changed", 1, query);
132 }
133
134 void query_change_server(QUERY_REC *query, SERVER_REC *server)
135 {
136         g_return_if_fail(IS_QUERY(query));
137
138         if (query->server != NULL) {
139                 query->server->queries =
140                         g_slist_remove(query->server->queries, query);
141         }
142         if (server != NULL)
143                 server->queries = g_slist_append(server->queries, query);
144
145         query->server = server;
146         signal_emit("query server changed", 1, query);
147 }
148
149 void queries_init(void)
150 {
151 }
152
153 void queries_deinit(void)
154 {
155         module_uniq_destroy("QUERY");
156 }