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).
32 #include "addrquery.h"
36 * Query list for tracking current queries.
38 static GList
*_requestList_
= NULL
;
41 * Mutex to protect list from multiple threads.
43 static pthread_mutex_t _requestListMutex_
= PTHREAD_MUTEX_INITIALIZER
;
46 * Current query ID. This is incremented for each query request created.
48 static gint _currentQueryID_
= 0;
52 * \param req Request query object.
54 static void qryreq_clear( QueryRequest
*req
) {
57 cm_return_if_fail( req
!= NULL
);
58 g_free( req
->searchTerm
);
60 req
->searchType
= ADDRSEARCH_NONE
;
61 req
->searchTerm
= NULL
;
62 req
->callBackEnd
= NULL
;
63 req
->callBackEntry
= NULL
;
66 node
= req
->queryList
;
69 node
= g_list_next( node
);
71 g_list_free( req
->queryList
);
72 req
->queryList
= NULL
;
77 * \param req Request query object.
79 static void qryreq_free( QueryRequest
*req
) {
80 cm_return_if_fail( req
!= NULL
);
86 * Specify search type.
87 * \param req Request query object.
90 void qryreq_set_search_type( QueryRequest
*req
, const AddrSearchType value
) {
91 cm_return_if_fail( req
!= NULL
);
92 req
->searchType
= value
;
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
);
109 * \param searchTerm Search term. A private copy will be made.
110 * \param callBackEnd Callback function that will be called when query
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
)
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_
);
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
) {
147 pthread_mutex_lock( & _requestListMutex_
);
149 node
= _requestList_
;
152 if( q
->queryID
== queryID
) {
156 node
= g_list_next( node
);
158 pthread_mutex_unlock( & _requestListMutex_
);
164 * Delete specified query.
165 * \param queryID ID of query to retire.
167 void qrymgr_delete_request( const gint queryID
) {
171 pthread_mutex_lock( & _requestListMutex_
);
175 node
= _requestList_
;
178 if( req
->queryID
== queryID
) {
183 node
= g_list_next( node
);
186 /* Free link element and associated query */
188 _requestList_
= g_list_remove_link( _requestList_
, nf
);
192 pthread_mutex_unlock( & _requestListMutex_
);
196 * Initialize query manager.
198 void qrymgr_initialize( void ) {
199 _requestList_
= NULL
;
205 static void qrymgr_free_all_request( void ) {
209 pthread_mutex_lock( & _requestListMutex_
);
210 node
= _requestList_
;
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();