2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2003 Match Grun
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 2 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, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
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;
48 * Create new address query.
49 * \return Initialized address query object.
51 QueryRequest *reqreq_create( void ) {
54 req = g_new0( QueryRequest, 1 );
56 req->searchType = ADDRSEARCH_NONE;
57 req->searchTerm = NULL;
58 req->callBackEnd = NULL;
59 req->callBackEntry = NULL;
60 req->queryList = NULL;
66 * \param req Request query object.
68 void qryreq_clear( QueryRequest *req ) {
71 g_return_if_fail( req != NULL );
72 g_free( req->searchTerm );
74 req->searchType = ADDRSEARCH_NONE;
75 req->searchTerm = NULL;
76 req->callBackEnd = NULL;
77 req->callBackEntry = NULL;
80 node = req->queryList;
83 node = g_list_next( node );
85 g_list_free( req->queryList );
86 req->queryList = NULL;
91 * \param req Request query object.
93 void qryreq_free( QueryRequest *req ) {
94 g_return_if_fail( req != NULL );
100 * Specify search type.
101 * \param req Request query object.
104 void qryreq_set_search_type( QueryRequest *req, const AddrSearchType value ) {
105 g_return_if_fail( req != NULL );
106 req->searchType = value;
110 * Specify search term to be used.
111 * \param req Request query object.
112 * \param value Search term.
114 void qryreq_set_search_term( QueryRequest *req, const gchar *value ) {
115 req->searchTerm = mgu_replace_string( req->searchTerm, value );
116 g_return_if_fail( req != NULL );
117 g_strstrip( req->searchTerm );
121 * Add address query object to request.
122 * \param req Request query object.
123 * \param aqo Address query object that performs the search.
125 void qryreq_add_query( QueryRequest *req, AddrQueryObject *aqo ) {
126 g_return_if_fail( req != NULL );
127 g_return_if_fail( aqo != NULL );
128 req->queryList = g_list_append( req->queryList, aqo );
132 * Display object to specified stream.
133 * \param req Request query object.
134 * \param stream Output stream.
136 void qryreq_print( const QueryRequest *req, FILE *stream ) {
138 g_return_if_fail( req != NULL );
140 fprintf( stream, "QueryRequest:\n" );
141 fprintf( stream, " queryID: %d\n", req->queryID );
142 fprintf( stream, " searchType: %d\n", req->searchType );
143 fprintf( stream, " searchTerm: '%s'\n", req->searchTerm );
144 node = req->queryList;
146 AddrQueryObject *aqo = node->data;
147 fprintf( stream, " --- type: %d\n", aqo->queryType );
148 node = g_list_next( node );
155 * \param searchTerm Search term. A private copy will be made.
156 * \param callBackEnd Callback function that will be called when query
158 * \param callBackEntry Callback function that will be called after each
159 * address entry has been read.
160 * \return Initialize query request object.
162 QueryRequest *qrymgr_add_request(
163 const gchar *searchTerm, void *callBackEnd, void *callBackEntry )
167 req = g_new0( QueryRequest, 1 );
168 req->searchTerm = g_strdup( searchTerm );
169 req->callBackEnd = callBackEnd;
170 req->callBackEntry = callBackEntry;
171 req->timeStart = time( NULL );
172 req->queryList = NULL;
174 /* Insert in head of list */
175 pthread_mutex_lock( & _requestListMutex_ );
176 req->queryID = ++_currentQueryID_;
177 _requestList_ = g_list_prepend( _requestList_, req );
178 pthread_mutex_unlock( & _requestListMutex_ );
184 * Find query in list.
185 * \param queryID ID of query to find.
186 * \return Query object, or <i>NULL</i> if not found.
188 QueryRequest *qrymgr_find_request( const gint queryID ) {
193 pthread_mutex_lock( & _requestListMutex_ );
195 node = _requestList_;
198 if( q->queryID == queryID ) {
202 node = g_list_next( node );
204 pthread_mutex_unlock( & _requestListMutex_ );
210 * Delete specified query.
211 * \param queryID ID of query to retire.
213 void qrymgr_delete_request( const gint queryID ) {
217 pthread_mutex_lock( & _requestListMutex_ );
221 node = _requestList_;
224 if( req->queryID == queryID ) {
229 node = g_list_next( node );
232 /* Free link element and associated query */
234 _requestList_ = g_list_remove_link( _requestList_, nf );
238 pthread_mutex_unlock( & _requestListMutex_ );
242 * Initialize query manager.
244 void qrymgr_initialize( void ) {
245 _requestList_ = NULL;
251 static void qrymgr_free_all_request( void ) {
255 pthread_mutex_lock( & _requestListMutex_ );
256 node = _requestList_;
261 node = g_list_next( node );
263 g_list_free( _requestList_ );
264 _requestList_ = NULL;
265 pthread_mutex_unlock( & _requestListMutex_ );
269 * Teardown query manager.
271 void qrymgr_teardown( void ) {
272 qrymgr_free_all_request();
276 * Display all queries to specified stream.
277 * \param stream Output stream.
279 void qrymgr_print( FILE *stream ) {
283 pthread_mutex_lock( & _requestListMutex_ );
284 fprintf( stream, "=== Query Manager ===\n" );
285 node = _requestList_;
288 qryreq_print( req, stream );
289 fprintf( stream, "---\n" );
290 node = g_list_next( node );
292 pthread_mutex_unlock( & _requestListMutex_ );