2005-08-27 [colin] 1.9.13cvs63
[claws.git] / src / ldaplocate.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2003 Match Grun
4  *
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.
9  *
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.
14  *
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.
18  */
19
20 /*
21  * Functions to perform searches for LDAP entries.
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #  include "config.h"
26 #endif
27
28 #ifdef USE_LDAP
29
30 #include <glib.h>
31 #include "addrquery.h"
32 #include "ldapserver.h"
33 #include "ldapquery.h"
34
35 /**
36  * Setup the search that will be performed and registered with the query
37  * manager.
38  *
39  * \param  server        LDAP server object.
40  * \param  searchTerm    Search term to locate.
41  * \param  callBackEntry Function to call when each attribute is returned.
42  * \param  callBackEnd   Function to call when search is complete.
43  * \return Query ID allocated to query that will be executed.
44  */
45 gint ldaplocate_search_setup(
46         LdapServer *server, const gchar *searchTerm, void *callBackEntry,
47         void *callBackEnd )
48 {
49         QueryRequest *req;
50         LdapQuery *qry;
51         gint queryID;
52         gchar *name;
53
54         /* Name the query */
55         name = g_strdup_printf( "Locate '%s'", searchTerm );
56
57         /* Set up a generic address query */
58         req = qrymgr_add_request( searchTerm, callBackEnd, callBackEntry );
59         qryreq_set_search_type( req, ADDRSEARCH_LOCATE );
60         queryID = req->queryID;
61
62         /* Construct a query */
63         qry = ldapqry_create();
64         ldapqry_set_query_id( qry, queryID );
65         ldapqry_set_name( qry, name );
66         ldapqry_set_search_value( qry, searchTerm );
67         ldapqry_set_search_type( qry, ADDRSEARCH_LOCATE );
68         ldapqry_set_callback_end( qry, callBackEnd );
69         ldapqry_set_callback_entry( qry, callBackEntry );
70
71         /* Setup server */
72         ldapsvr_add_query( server, qry );
73
74         /* Set up query request */
75         qryreq_add_query( req, ADDRQUERY_OBJECT(qry) );
76
77         g_free( name );
78
79         return queryID;
80 }
81
82 /**
83  * Perform the previously registered search.
84  * \param  queryID    ID of search query to be executed.
85  * \return <i>TRUE</i> if search started successfully, or <i>FALSE</i> if
86  *         failed.
87  */
88 gboolean ldaplocate_search_start( const gint queryID ) {
89         gboolean retVal;
90         LdapServer *server;
91         LdapQuery *qry;
92         QueryRequest *req;
93         AddrQueryObject *aqo;
94
95         retVal = FALSE;
96         req = qrymgr_find_request( queryID );
97         if( req == NULL ) {
98                 return retVal;
99         }
100
101         /* Note: there should only be one query in the list */
102         aqo = req->queryList->data;
103         if( aqo->queryType == ADDRQUERY_LDAP ) {
104                 qry = ( LdapQuery * ) aqo;
105                 server = qry->server;
106
107                 /* Retire any aged queries */
108                 ldapsvr_retire_query( server );
109
110                 /* Start the search */
111                 retVal = TRUE;
112                 ldapsvr_execute_query( server, qry );
113         }
114         return retVal;
115 }
116
117 /**
118  * Notify search to stop execution.
119  * \param queryID Query to terminate.
120  */
121 void ldaplocate_search_stop( const gint queryID ) {
122         QueryRequest *req;
123         AddrQueryObject *aqo;
124         LdapQuery *qry;
125         gint queryType;
126
127         req = qrymgr_find_request( queryID );
128         if( req == NULL ) {
129                 return;
130         }
131
132         aqo = req->queryList->data;
133         if( aqo->queryType == ADDRQUERY_LDAP ) {
134                 /* Notify query to stop */
135                 qry = ( LdapQuery * ) aqo;
136                 ldapqry_set_stop_flag( qry, TRUE );
137         }
138         req->queryList->data = NULL;
139
140         /* Delete query */
141         qrymgr_delete_request( queryID );
142 }
143
144 #endif  /* USE_LDAP */
145
146 /*
147  * End of Source.
148  */
149
150