2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2003-2007 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"
33 * Query list for tracking current queries.
35 static GList *_requestList_ = NULL;
38 * Mutex to protect list from multiple threads.
40 static pthread_mutex_t _requestListMutex_ = PTHREAD_MUTEX_INITIALIZER;
43 * Current query ID. This is incremented for each query request created.
45 static gint _currentQueryID_ = 0;
49 * \param req Request query object.
51 static void qryreq_clear( QueryRequest *req ) {
54 g_return_if_fail( req != NULL );
55 g_free( req->searchTerm );
57 req->searchType = ADDRSEARCH_NONE;
58 req->searchTerm = NULL;
59 req->callBackEnd = NULL;
60 req->callBackEntry = NULL;
63 node = req->queryList;
66 node = g_list_next( node );
68 g_list_free( req->queryList );
69 req->queryList = NULL;
74 * \param req Request query object.
76 static void qryreq_free( QueryRequest *req ) {
77 g_return_if_fail( req != NULL );
83 * Specify search type.
84 * \param req Request query object.
87 void qryreq_set_search_type( QueryRequest *req, const AddrSearchType value ) {
88 g_return_if_fail( req != NULL );
89 req->searchType = value;
93 * Add address query object to request.
94 * \param req Request query object.
95 * \param aqo Address query object that performs the search.
97 void qryreq_add_query( QueryRequest *req, AddrQueryObject *aqo ) {
98 g_return_if_fail( req != NULL );
99 g_return_if_fail( aqo != NULL );
100 req->queryList = g_list_append( req->queryList, aqo );
106 * \param searchTerm Search term. A private copy will be made.
107 * \param callBackEnd Callback function that will be called when query
109 * \param callBackEntry Callback function that will be called after each
110 * address entry has been read.
111 * \return Initialize query request object.
113 QueryRequest *qrymgr_add_request(
114 const gchar *searchTerm, void *callBackEnd, void *callBackEntry )
118 req = g_new0( QueryRequest, 1 );
119 req->searchTerm = g_strdup( searchTerm );
120 req->callBackEnd = callBackEnd;
121 req->callBackEntry = callBackEntry;
122 req->timeStart = time( NULL );
123 req->queryList = NULL;
125 /* Insert in head of list */
126 pthread_mutex_lock( & _requestListMutex_ );
127 req->queryID = ++_currentQueryID_;
128 _requestList_ = g_list_prepend( _requestList_, req );
129 pthread_mutex_unlock( & _requestListMutex_ );
135 * Find query in list.
136 * \param queryID ID of query to find.
137 * \return Query object, or <i>NULL</i> if not found.
139 QueryRequest *qrymgr_find_request( const gint queryID ) {
144 pthread_mutex_lock( & _requestListMutex_ );
146 node = _requestList_;
149 if( q->queryID == queryID ) {
153 node = g_list_next( node );
155 pthread_mutex_unlock( & _requestListMutex_ );
161 * Delete specified query.
162 * \param queryID ID of query to retire.
164 void qrymgr_delete_request( const gint queryID ) {
168 pthread_mutex_lock( & _requestListMutex_ );
172 node = _requestList_;
175 if( req->queryID == queryID ) {
180 node = g_list_next( node );
183 /* Free link element and associated query */
185 _requestList_ = g_list_remove_link( _requestList_, nf );
189 pthread_mutex_unlock( & _requestListMutex_ );
193 * Initialize query manager.
195 void qrymgr_initialize( void ) {
196 _requestList_ = NULL;
202 static void qrymgr_free_all_request( void ) {
206 pthread_mutex_lock( & _requestListMutex_ );
207 node = _requestList_;
212 node = g_list_next( node );
214 g_list_free( _requestList_ );
215 _requestList_ = NULL;
216 pthread_mutex_unlock( & _requestListMutex_ );
220 * Teardown query manager.
222 void qrymgr_teardown( void ) {
223 qrymgr_free_all_request();