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