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 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 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 * Specify search term to be used.
94 * \param req Request query object.
95 * \param value Search term.
97 void qryreq_set_search_term( QueryRequest *req, const gchar *value ) {
98 req->searchTerm = mgu_replace_string( req->searchTerm, value );
99 g_return_if_fail( req != NULL );
100 g_strstrip( req->searchTerm );
104 * Add address query object to request.
105 * \param req Request query object.
106 * \param aqo Address query object that performs the search.
108 void qryreq_add_query( QueryRequest *req, AddrQueryObject *aqo ) {
109 g_return_if_fail( req != NULL );
110 g_return_if_fail( aqo != NULL );
111 req->queryList = g_list_append( req->queryList, aqo );
115 * Display object to specified stream.
116 * \param req Request query object.
117 * \param stream Output stream.
119 void qryreq_print( const QueryRequest *req, FILE *stream ) {
121 g_return_if_fail( req != NULL );
123 fprintf( stream, "QueryRequest:\n" );
124 fprintf( stream, " queryID: %d\n", req->queryID );
125 fprintf( stream, " searchType: %d\n", req->searchType );
126 fprintf( stream, " searchTerm: '%s'\n", req->searchTerm );
127 node = req->queryList;
129 AddrQueryObject *aqo = node->data;
130 fprintf( stream, " --- type: %d\n", aqo->queryType );
131 node = g_list_next( node );
138 * \param searchTerm Search term. A private copy will be made.
139 * \param callBackEnd Callback function that will be called when query
141 * \param callBackEntry Callback function that will be called after each
142 * address entry has been read.
143 * \return Initialize query request object.
145 QueryRequest *qrymgr_add_request(
146 const gchar *searchTerm, void *callBackEnd, void *callBackEntry )
150 req = g_new0( QueryRequest, 1 );
151 req->searchTerm = g_strdup( searchTerm );
152 req->callBackEnd = callBackEnd;
153 req->callBackEntry = callBackEntry;
154 req->timeStart = time( NULL );
155 req->queryList = NULL;
157 /* Insert in head of list */
158 pthread_mutex_lock( & _requestListMutex_ );
159 req->queryID = ++_currentQueryID_;
160 _requestList_ = g_list_prepend( _requestList_, req );
161 pthread_mutex_unlock( & _requestListMutex_ );
167 * Find query in list.
168 * \param queryID ID of query to find.
169 * \return Query object, or <i>NULL</i> if not found.
171 QueryRequest *qrymgr_find_request( const gint queryID ) {
176 pthread_mutex_lock( & _requestListMutex_ );
178 node = _requestList_;
181 if( q->queryID == queryID ) {
185 node = g_list_next( node );
187 pthread_mutex_unlock( & _requestListMutex_ );
193 * Delete specified query.
194 * \param queryID ID of query to retire.
196 void qrymgr_delete_request( const gint queryID ) {
200 pthread_mutex_lock( & _requestListMutex_ );
204 node = _requestList_;
207 if( req->queryID == queryID ) {
212 node = g_list_next( node );
215 /* Free link element and associated query */
217 _requestList_ = g_list_remove_link( _requestList_, nf );
221 pthread_mutex_unlock( & _requestListMutex_ );
225 * Initialize query manager.
227 void qrymgr_initialize( void ) {
228 _requestList_ = NULL;
234 static void qrymgr_free_all_request( void ) {
238 pthread_mutex_lock( & _requestListMutex_ );
239 node = _requestList_;
244 node = g_list_next( node );
246 g_list_free( _requestList_ );
247 _requestList_ = NULL;
248 pthread_mutex_unlock( & _requestListMutex_ );
252 * Teardown query manager.
254 void qrymgr_teardown( void ) {
255 qrymgr_free_all_request();
259 * Display all queries to specified stream.
260 * \param stream Output stream.
262 void qrymgr_print( FILE *stream ) {
266 pthread_mutex_lock( & _requestListMutex_ );
267 fprintf( stream, "=== Query Manager ===\n" );
268 node = _requestList_;
271 qryreq_print( req, stream );
272 fprintf( stream, "---\n" );
273 node = g_list_next( node );
275 pthread_mutex_unlock( & _requestListMutex_ );