for release 4.2.0
[claws.git] / src / addrquery.c
blob921bc9fed14d2b4c57001c108bab174e6d9b5277
1 /*
2 * Claws Mail -- a GTK based, lightweight, and fast e-mail client
3 * Copyright (C) 2003-2012 Match Grun and the Claws Mail team
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 3 of the License, or
8 * (at your option) any later version.
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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 * Functions to define an address query (a request).
24 #include "config.h"
26 #include <stdio.h>
27 #include <string.h>
28 #include <glib.h>
29 #include <pthread.h>
31 #include "mgutils.h"
32 #include "addrquery.h"
33 #include "utils.h"
35 /**
36 * Query list for tracking current queries.
38 static GList *_requestList_ = NULL;
40 /**
41 * Mutex to protect list from multiple threads.
43 static pthread_mutex_t _requestListMutex_ = PTHREAD_MUTEX_INITIALIZER;
45 /**
46 * Current query ID. This is incremented for each query request created.
48 static gint _currentQueryID_ = 0;
50 /**
51 * Clear the query.
52 * \param req Request query object.
54 static void qryreq_clear( QueryRequest *req ) {
55 GList *node;
57 cm_return_if_fail( req != NULL );
58 g_free( req->searchTerm );
59 req->queryID = 0;
60 req->searchType = ADDRSEARCH_NONE;
61 req->searchTerm = NULL;
62 req->callBackEnd = NULL;
63 req->callBackEntry = NULL;
65 /* Empty the list */
66 node = req->queryList;
67 while( node ) {
68 node->data = NULL;
69 node = g_list_next( node );
71 g_list_free( req->queryList );
72 req->queryList = NULL;
75 /**
76 * Free query.
77 * \param req Request query object.
79 static void qryreq_free( QueryRequest *req ) {
80 cm_return_if_fail( req != NULL );
81 qryreq_clear( req );
82 g_free( req );
85 /**
86 * Specify search type.
87 * \param req Request query object.
88 * \param value Type.
90 void qryreq_set_search_type( QueryRequest *req, const AddrSearchType value ) {
91 cm_return_if_fail( req != NULL );
92 req->searchType = value;
95 /**
96 * Add address query object to request.
97 * \param req Request query object.
98 * \param aqo Address query object that performs the search.
100 void qryreq_add_query( QueryRequest *req, AddrQueryObject *aqo ) {
101 cm_return_if_fail( req != NULL );
102 cm_return_if_fail( aqo != NULL );
103 req->queryList = g_list_append( req->queryList, aqo );
107 * Add query to list.
109 * \param searchTerm Search term. A private copy will be made.
110 * \param callBackEnd Callback function that will be called when query
111 * terminates.
112 * \param callBackEntry Callback function that will be called after each
113 * address entry has been read.
114 * \return Initialize query request object.
116 QueryRequest *qrymgr_add_request(
117 const gchar *searchTerm, void *callBackEnd, void *callBackEntry )
119 QueryRequest *req;
121 req = g_new0( QueryRequest, 1 );
122 req->searchTerm = g_strdup( searchTerm );
123 req->callBackEnd = callBackEnd;
124 req->callBackEntry = callBackEntry;
125 req->timeStart = time( NULL );
126 req->queryList = NULL;
128 /* Insert in head of list */
129 pthread_mutex_lock( & _requestListMutex_ );
130 req->queryID = ++_currentQueryID_;
131 _requestList_ = g_list_prepend( _requestList_, req );
132 pthread_mutex_unlock( & _requestListMutex_ );
134 return req;
138 * Find query in list.
139 * \param queryID ID of query to find.
140 * \return Query object, or <i>NULL</i> if not found.
142 QueryRequest *qrymgr_find_request( const gint queryID ) {
143 QueryRequest *req;
144 QueryRequest *q;
145 GList *node;
147 pthread_mutex_lock( & _requestListMutex_ );
148 req = NULL;
149 node = _requestList_;
150 while( node ) {
151 q = node->data;
152 if( q->queryID == queryID ) {
153 req = q;
154 break;
156 node = g_list_next( node );
158 pthread_mutex_unlock( & _requestListMutex_ );
160 return req;
164 * Delete specified query.
165 * \param queryID ID of query to retire.
167 void qrymgr_delete_request( const gint queryID ) {
168 QueryRequest *req;
169 GList *node, *nf;
171 pthread_mutex_lock( & _requestListMutex_ );
173 /* Find node */
174 nf = NULL;
175 node = _requestList_;
176 while( node ) {
177 req = node->data;
178 if( req->queryID == queryID ) {
179 nf = node;
180 qryreq_free( req );
181 break;
183 node = g_list_next( node );
186 /* Free link element and associated query */
187 if( nf ) {
188 _requestList_ = g_list_remove_link( _requestList_, nf );
189 g_list_free_1( nf );
192 pthread_mutex_unlock( & _requestListMutex_ );
196 * Initialize query manager.
198 void qrymgr_initialize( void ) {
199 _requestList_ = NULL;
203 * Free all queries.
205 static void qrymgr_free_all_request( void ) {
206 QueryRequest *req;
207 GList *node;
209 pthread_mutex_lock( & _requestListMutex_ );
210 node = _requestList_;
211 while( node ) {
212 req = node->data;
213 qryreq_free( req );
214 node->data = NULL;
215 node = g_list_next( node );
217 g_list_free( _requestList_ );
218 _requestList_ = NULL;
219 pthread_mutex_unlock( & _requestListMutex_ );
223 * Teardown query manager.
225 void qrymgr_teardown( void ) {
226 qrymgr_free_all_request();
230 * End of Source.