2 * Sylpheed -- 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).
30 #include "addrquery.h"
34 * Query list for tracking current queries.
36 static GList
*_requestList_
= NULL
;
39 * Mutex to protect list from multiple threads.
41 static pthread_mutex_t _requestListMutex_
= PTHREAD_MUTEX_INITIALIZER
;
44 * Current query ID. This is incremented for each query request created.
46 static gint _currentQueryID_
= 0;
50 * \param req Request query object.
52 static void qryreq_clear( QueryRequest
*req
) {
55 cm_return_if_fail( req
!= NULL
);
56 g_free( req
->searchTerm
);
58 req
->searchType
= ADDRSEARCH_NONE
;
59 req
->searchTerm
= NULL
;
60 req
->callBackEnd
= NULL
;
61 req
->callBackEntry
= NULL
;
64 node
= req
->queryList
;
67 node
= g_list_next( node
);
69 g_list_free( req
->queryList
);
70 req
->queryList
= NULL
;
75 * \param req Request query object.
77 static void qryreq_free( QueryRequest
*req
) {
78 cm_return_if_fail( req
!= NULL
);
84 * Specify search type.
85 * \param req Request query object.
88 void qryreq_set_search_type( QueryRequest
*req
, const AddrSearchType value
) {
89 cm_return_if_fail( req
!= NULL
);
90 req
->searchType
= value
;
94 * Add address query object to request.
95 * \param req Request query object.
96 * \param aqo Address query object that performs the search.
98 void qryreq_add_query( QueryRequest
*req
, AddrQueryObject
*aqo
) {
99 cm_return_if_fail( req
!= NULL
);
100 cm_return_if_fail( aqo
!= NULL
);
101 req
->queryList
= g_list_append( req
->queryList
, aqo
);
107 * \param searchTerm Search term. A private copy will be made.
108 * \param callBackEnd Callback function that will be called when query
110 * \param callBackEntry Callback function that will be called after each
111 * address entry has been read.
112 * \return Initialize query request object.
114 QueryRequest
*qrymgr_add_request(
115 const gchar
*searchTerm
, void *callBackEnd
, void *callBackEntry
)
119 req
= g_new0( QueryRequest
, 1 );
120 req
->searchTerm
= g_strdup( searchTerm
);
121 req
->callBackEnd
= callBackEnd
;
122 req
->callBackEntry
= callBackEntry
;
123 req
->timeStart
= time( NULL
);
124 req
->queryList
= NULL
;
126 /* Insert in head of list */
127 pthread_mutex_lock( & _requestListMutex_
);
128 req
->queryID
= ++_currentQueryID_
;
129 _requestList_
= g_list_prepend( _requestList_
, req
);
130 pthread_mutex_unlock( & _requestListMutex_
);
136 * Find query in list.
137 * \param queryID ID of query to find.
138 * \return Query object, or <i>NULL</i> if not found.
140 QueryRequest
*qrymgr_find_request( const gint queryID
) {
145 pthread_mutex_lock( & _requestListMutex_
);
147 node
= _requestList_
;
150 if( q
->queryID
== queryID
) {
154 node
= g_list_next( node
);
156 pthread_mutex_unlock( & _requestListMutex_
);
162 * Delete specified query.
163 * \param queryID ID of query to retire.
165 void qrymgr_delete_request( const gint queryID
) {
169 pthread_mutex_lock( & _requestListMutex_
);
173 node
= _requestList_
;
176 if( req
->queryID
== queryID
) {
181 node
= g_list_next( node
);
184 /* Free link element and associated query */
186 _requestList_
= g_list_remove_link( _requestList_
, nf
);
190 pthread_mutex_unlock( & _requestListMutex_
);
194 * Initialize query manager.
196 void qrymgr_initialize( void ) {
197 _requestList_
= NULL
;
203 static void qrymgr_free_all_request( void ) {
207 pthread_mutex_lock( & _requestListMutex_
);
208 node
= _requestList_
;
213 node
= g_list_next( node
);
215 g_list_free( _requestList_
);
216 _requestList_
= NULL
;
217 pthread_mutex_unlock( & _requestListMutex_
);
221 * Teardown query manager.
223 void qrymgr_teardown( void ) {
224 qrymgr_free_all_request();