* src/carray.h
[claws.git] / src / addrquery.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2003 Match Grun
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 /*
21  * Functions to define an address query (a request).
22  */
23
24 #include <stdio.h>
25 #include <string.h>
26 #include <glib.h>
27 #include <pthread.h>
28
29 #include "mgutils.h"
30 #include "addrquery.h"
31
32 /**
33  * Query list for tracking current queries.
34  */
35 static GList *_queryList_ = NULL;
36
37 /**
38  * Mutex to protect list from multiple threads.
39  */
40 static pthread_mutex_t _queryListMutex_ = PTHREAD_MUTEX_INITIALIZER;
41
42 /**
43  * Create new address query.
44  * \return Initialized address query object.
45  */
46 AddrQuery *addrqry_create( void ) {
47         AddrQuery *qry;
48
49         qry = g_new0( AddrQuery, 1 );
50         qry->queryType = ADDRQUERY_NONE;
51         qry->queryID = 0;
52         qry->idleID = 0;
53         qry->searchTerm = NULL;
54         qry->callBack = NULL;
55         qry->target = NULL;
56         qry->serverObject = NULL;
57         qry->queryObject = NULL;
58         return qry;
59 }
60
61 /**
62  * Clear the query.
63  * \param qry Address query object.
64  */
65 void addrqry_clear( AddrQuery *qry ) {
66         g_return_if_fail( qry != NULL );
67         g_free( qry->searchTerm );
68         qry->queryType = ADDRQUERY_NONE;
69         qry->queryID = 0;
70         qry->idleID = 0;
71         qry->searchTerm = NULL;
72         qry->callBack = NULL;
73         qry->target = NULL;
74         qry->serverObject = NULL;
75         qry->queryObject = NULL;
76 }
77
78 /**
79  * Free query.
80  * \param qry Address query object.
81  */
82 void addrqry_free( AddrQuery *qry ) {
83         g_return_if_fail( qry != NULL );
84         addrqry_clear( qry );
85         g_free( qry );
86 }
87
88 /**
89  * Specify query type.
90  * \param qry   Address query object.
91  * \param value Type.
92  */
93 void addrqry_set_query_type( AddrQuery *qry, const AddrQueryType value ) {
94         g_return_if_fail( qry != NULL );
95         qry->queryType = value;
96 }
97
98 /**
99  * Specify idle ID.
100  * \param qry   Address query object.
101  * \param value Idle ID.
102  */
103 void addrqry_set_idle_id( AddrQuery *qry, const guint value ) {
104         g_return_if_fail( qry != NULL );
105         qry->idleID = value;
106 }
107
108 /**
109  * Specify search term to be used.
110  * \param qry   Address query object.
111  * \param value Search term.
112  */
113 void addrqry_set_search_term( AddrQuery* qry, const gchar *value ) {
114         qry->searchTerm = mgu_replace_string( qry->searchTerm, value );
115         g_return_if_fail( qry != NULL );
116         g_strstrip( qry->searchTerm );
117 }
118
119 /**
120  * Specify server object to be used.
121  * \param qry   Address query object.
122  * \param value Server object that performs the search.
123  */
124 void addrqry_set_server( AddrQuery* qry, const gpointer value ) {
125         g_return_if_fail( qry != NULL );
126         qry->serverObject = value;
127 }
128
129 /**
130  * Specify query object to be used.
131  * \param qry   Address query object.
132  * \param value Query object that performs the search.
133  */
134 void addrqry_set_query( AddrQuery* qry, const gpointer value ) {
135         g_return_if_fail( qry != NULL );
136         qry->queryObject = value;
137 }
138
139 /**
140  * Display object to specified stream.
141  * \param qry    Address query object.
142  * \param stream Output stream.
143  */
144 void addrqry_print( const AddrQuery *qry, FILE *stream ) {
145         g_return_if_fail( qry != NULL );
146
147         fprintf( stream, "AddressQuery:\n" );
148         fprintf( stream, "     queryID: %d\n",   qry->queryID );
149         fprintf( stream, "      idleID: %d\n",   qry->idleID );
150         fprintf( stream, "  searchTerm: '%s'\n", qry->searchTerm );
151 }
152
153 /**
154  * Add query to list.
155  * \param queryID    ID of query being executed.
156  * \param searchTerm Search term. A private copy will be made.
157  * \param callBack   Callback function.
158  * \param target     Target object to receive data.
159  */
160 AddrQuery *qrymgr_add_query(
161         const gint queryID, const gchar *searchTerm, void *callBack,
162         gpointer target )
163 {
164         AddrQuery *qry;
165
166         qry = g_new0( AddrQuery, 1 );
167         qry->queryType = ADDRQUERY_NONE;
168         qry->queryID = queryID;
169         qry->idleID = 0;
170         qry->searchTerm = g_strdup( searchTerm );
171         qry->callBack = callBack;
172         qry->target = NULL;
173         qry->timeStart = time( NULL );
174         qry->serverObject = NULL;
175         qry->queryObject = NULL;
176
177         /* Insert in head of list */
178         pthread_mutex_lock( & _queryListMutex_ );
179         _queryList_ = g_list_prepend( _queryList_, qry );
180         pthread_mutex_unlock( & _queryListMutex_ );
181
182         return qry;
183 }
184
185 /**
186  * Find query in list.
187  * \param  queryID ID of query to find.
188  * \return Query object, or <i>NULL</i> if not found.
189  */
190 AddrQuery *qrymgr_find_query( const gint queryID ) {
191         AddrQuery *qry;
192         AddrQuery *q;
193         GList *node;
194
195         pthread_mutex_lock( & _queryListMutex_ );
196         qry = NULL;
197         node = _queryList_;
198         while( node ) {
199                 q = node->data;
200                 if( q->queryID == queryID ) {
201                         qry = q;
202                         break;
203                 }
204                 node = g_list_next( node );
205         }
206         pthread_mutex_unlock( & _queryListMutex_ );
207
208         return qry;
209 }
210
211 /**
212  * Delete specified query.
213  * \param  queryID ID of query to retire.
214  */
215 void qrymgr_delete_query( const gint queryID ) {
216         AddrQuery *qry;
217         GList *node, *nf;
218
219         pthread_mutex_lock( & _queryListMutex_ );
220
221         /* Find node */
222         nf = NULL;
223         node = _queryList_;
224         while( node ) {
225                 qry = node->data;
226                 if( qry->queryID == queryID ) {
227                         nf = node;
228                         addrqry_free( qry );
229                         break;
230                 }
231                 node = g_list_next( node );
232         }
233
234         /* Free link element and associated query */
235         if( nf ) {
236                 _queryList_ = g_list_remove_link( _queryList_, nf );
237                 g_list_free_1( nf );
238         }
239
240         pthread_mutex_unlock( & _queryListMutex_ );
241 }
242
243 /**
244  * Initialize query manager.
245  */
246 void qrymgr_initialize( void ) {
247         _queryList_ = NULL;
248 }
249
250 /**
251  * Free all queries.
252  */
253 static void qrymgr_free_all_query( void ) {
254         AddrQuery *qry;
255         GList *node;
256
257         pthread_mutex_lock( & _queryListMutex_ );
258         node = _queryList_;
259         while( node ) {
260                 qry = node->data;
261                 addrqry_free( qry );
262                 node->data = NULL;
263                 node = g_list_next( node );
264         }
265         g_list_free( _queryList_ );
266         _queryList_ = NULL;
267         pthread_mutex_unlock( & _queryListMutex_ );
268 }
269
270 /**
271  * Teardown query manager.
272  */
273 void qrymgr_teardown( void ) {
274         qrymgr_free_all_query();
275 }
276
277 /**
278  * Display all queries to specified stream.
279  * \param stream Output stream.
280  */
281 void qrymgr_print( FILE *stream ) {
282         AddrQuery *qry;
283         GList *node;
284
285         pthread_mutex_lock( & _queryListMutex_ );
286         fprintf( stream, "=== Query Manager ===\n" );
287         node = _queryList_;
288         while( node ) {
289                 qry = node->data;
290                 addrqry_print( qry, stream );
291                 fprintf( stream, "---\n" );
292                 node = g_list_next( node );
293         }
294         pthread_mutex_unlock( & _queryListMutex_ );
295 }
296
297 /*
298 * End of Source.
299 */
300
301