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