LDAP/UI code separation. Fix mem leak.
authorMatch Grun <match@dimensional.com>
Sun, 29 Dec 2002 05:13:00 +0000 (05:13 +0000)
committerMatch Grun <match@dimensional.com>
Sun, 29 Dec 2002 05:13:00 +0000 (05:13 +0000)
configure.in
src/addressbook.c
src/syldap.c

index 46bc11fd480d20f4e52733bee2200082349f5829..8814ce3e433ccf182ea05f7bf92a8bf56a4ac9af 100644 (file)
@@ -11,7 +11,7 @@ MINOR_VERSION=8
 MICRO_VERSION=8
 INTERFACE_AGE=0
 BINARY_AGE=0
-EXTRA_VERSION=claws21
+EXTRA_VERSION=claws22
 VERSION=$MAJOR_VERSION.$MINOR_VERSION.$MICRO_VERSION$EXTRA_VERSION
 
 dnl set $target
index d95540ba73505adfe7abc6c6a07896590bd482b3..970174284db2ddc8a5a47fdbbe9e7ef5235ff968 100644 (file)
@@ -3207,7 +3207,7 @@ static void addressbook_ldap_show_message( SyldapServer *svr ) {
        addressbook_status_show( addressbook_msgbuf );
 }
 
-static void ldapsearch_callback( SyldapServer *sls ) {
+static void addressbook_ldap_show_results( SyldapServer *sls ) {
        GtkCTree *ctree = GTK_CTREE(addrbook.ctree);
        AddressObject *obj;
        AdapterDSource *ads = NULL;
@@ -3241,6 +3241,52 @@ static void ldapsearch_callback( SyldapServer *sls ) {
                }
        }
 }
+
+/*
+ * LDAP idle function. This function is called during UI idle time while
+ * an LDAP search is in progress.
+ * Enter: data Reference to LDAP server object.
+ */
+static void addressbook_ldap_idle( gpointer data ) {
+       SyldapServer *server;
+
+       server = ( SyldapServer * ) data;       
+       if( ! server->busyFlag ) {
+               /* Server has completed search - remove from idle list */
+               gtk_idle_remove( server->idleId );
+
+               /* Process callback and free up the thread */
+               addressbook_ldap_show_results( server );
+               g_free( server->thread );
+               server->thread = NULL;
+       }
+}
+
+/*
+ * Perform lookup for LDAP search.
+ * Enter: ads     Adapter for data source.
+ *        sLookup Lookup string.
+ */
+static void addressbook_ldap_lookup( AdapterDSource *ads, gchar *sLookup ) {
+       AddressDataSource *ds = NULL;
+       AddressInterface *iface = NULL;
+       SyldapServer *server;
+
+       ds = ads->dataSource;
+       if( ds == NULL ) return;
+       iface = ds->interface;
+       if( ! iface->haveLibrary ) return;
+       server = ds->rawDataSource;
+       if( server ) {
+               syldap_cancel_read( server );
+               if( *sLookup == '\0' || strlen( sLookup ) < 1 ) return;
+               syldap_set_search_value( server, sLookup );
+               server->idleId = gtk_idle_add(
+                       ( GtkFunction ) addressbook_ldap_idle, server );
+               syldap_read_data_th( server );
+               addressbook_ldap_show_message( server );
+       }
+}
 #endif
 
 /*
@@ -3250,10 +3296,6 @@ static void addressbook_lup_clicked( GtkButton *button, gpointer data ) {
        GtkCTree *ctree = GTK_CTREE(addrbook.ctree);
        AddressObject *obj;
        AdapterDSource *ads = NULL;
-#ifdef USE_LDAP
-       AddressDataSource *ds = NULL;
-       AddressInterface *iface = NULL;
-#endif /* USE_LDAP */
        gchar *sLookup;
 
        if( ! addrbook.treeSelected ) return;
@@ -3269,21 +3311,7 @@ static void addressbook_lup_clicked( GtkButton *button, gpointer data ) {
                ads = ADAPTER_DSOURCE(obj);
 #ifdef USE_LDAP
                if( ads->subType == ADDR_LDAP ) {
-                       SyldapServer *server;
-
-                       ds = ads->dataSource;
-                       if( ds == NULL ) return;
-                       iface = ds->interface;
-                       if( ! iface->haveLibrary ) return;
-                       server = ds->rawDataSource;
-                       if( server ) {
-                               syldap_cancel_read( server );
-                               if( *sLookup == '\0' || strlen( sLookup ) < 1 ) return;
-                               syldap_set_search_value( server, sLookup );
-                               syldap_set_callback( server, ldapsearch_callback );
-                               syldap_read_data_th( server );
-                               addressbook_ldap_show_message( server );
-                       }
+                       addressbook_ldap_lookup( ads, sLookup );
                }
 #endif /* USE_LDAP */
        }
index bf7711ae1c920c1546d706f851db2ae76eb84f03..21afde7686583d5a745cfc468a3a1762eb54a3e4 100644 (file)
@@ -28,8 +28,6 @@
 #ifdef USE_LDAP
 
 #include <glib.h>
-#include <gdk/gdk.h>
-#include <gtk/gtkmain.h>
 #include <sys/time.h>
 #include <string.h>
 #include <ldap.h>
@@ -608,10 +606,10 @@ gint syldap_search( SyldapServer *ldapServer ) {
                        if( strcasecmp( attribute, SYLDAP_ATTR_DN ) == 0 ) {
                                listDN = syldap_add_single_value( ld, e, attribute );
                        }
-               }
 
-               /* Free memory used to store attribute */
-               ldap_memfree( attribute );
+                       /* Free memory used to store attribute */
+                       ldap_memfree( attribute );
+               }
 
                /* Format and add items to cache */
                syldap_build_items_fl( ldapServer, listAddress, listFirst, listLast );
@@ -641,20 +639,6 @@ gint syldap_search( SyldapServer *ldapServer ) {
        return ldapServer->retVal;
 }
 
-/* syldap_display_search_results() - updates the ui. this function is called from the
- * main thread (the thread running the GTK event loop). */
-static gint syldap_display_search_results(SyldapServer *ldapServer)
-{
-       /* NOTE: when this function is called the accompanying thread should
-        * already be terminated. */
-       gtk_idle_remove(ldapServer->idleId);
-       ldapServer->callBack(ldapServer);
-       /* FIXME:  match should know whether to free this SyldapServer stuff. */
-       g_free(ldapServer->thread);
-       ldapServer->thread = NULL;
-       return TRUE;
-}
-
 /* ============================================================================================ */
 /*
 * Read data into list. Main entry point
@@ -675,18 +659,7 @@ gint syldap_read_data( SyldapServer *ldapServer ) {
                ldapServer->addressCache->dataRead = TRUE;
                ldapServer->addressCache->accessFlag = FALSE;
        }
-
-       /* Callback */
        ldapServer->busyFlag = FALSE;
-       if( ldapServer->callBack ) {
-               /* make the ui thread update the search results */
-               /* TODO: really necessary to call gdk_threads_XXX()??? gtk_idle_add()
-                * should do this - could someone check the GTK sources please? */
-               gdk_threads_enter();
-               ldapServer->idleId = gtk_idle_add((GtkFunction)syldap_display_search_results,
-                               ldapServer);
-               gdk_threads_leave();
-       }
 
        return ldapServer->retVal;
 }
@@ -699,12 +672,11 @@ gint syldap_read_data( SyldapServer *ldapServer ) {
 void syldap_cancel_read( SyldapServer *ldapServer ) {
        g_return_if_fail( ldapServer != NULL );
 
-       /* DELETEME: this is called from inside UI thread so it's OK, Christoph! */
        if( ldapServer->thread ) {
                /* printf( "thread cancelled\n" ); */
                pthread_cancel( *ldapServer->thread );
+               g_free(ldapServer->thread);
        }
-       g_free(ldapServer->thread);
        ldapServer->thread = NULL;
        ldapServer->busyFlag = FALSE;
 }
@@ -722,11 +694,13 @@ gint syldap_read_data_th( SyldapServer *ldapServer ) {
        ldapServer->busyFlag = FALSE;
        syldap_check_search( ldapServer );
        if( ldapServer->retVal == MGU_SUCCESS ) {
-               /* debug_print("Staring LDAP read thread\n"); */
+               /* debug_print("Starting LDAP read thread\n"); */
 
                ldapServer->busyFlag = TRUE;
                ldapServer->thread = g_new0(pthread_t, 1);
-               pthread_create( ldapServer->thread, NULL, (void *) syldap_read_data, (void *) ldapServer );
+               pthread_create(
+                       ldapServer->thread, NULL, (void *) syldap_read_data,
+                       (void *) ldapServer );
        }
        return ldapServer->retVal;
 }
@@ -828,8 +802,8 @@ GList *syldap_read_basedn_s( const gchar *host, const gint port, const gchar *bi
                                        }
                                        ldap_value_free( vals );
                                }
+                               ldap_memfree( attribute );
                        }
-                       ldap_memfree( attribute );
                        if( ber != NULL ) {
                                ber_free( ber, 0 );
                        }
@@ -870,8 +844,8 @@ GList *syldap_read_basedn_s( const gchar *host, const gint port, const gchar *bi
                                                }
                                                ldap_value_free( vals );
                                        }
+                                       ldap_memfree( attribute );
                                }
-                               ldap_memfree( attribute );
                                if( ber != NULL ) {
                                        ber_free( ber, 0 );
                                }
@@ -957,8 +931,8 @@ GList *syldap_read_basedn( SyldapServer *ldapServer ) {
                                        }
                                        ldap_value_free( vals );
                                }
+                               ldap_memfree( attribute );
                        }
-                       ldap_memfree( attribute );
                        if( ber != NULL ) {
                                ber_free( ber, 0 );
                        }
@@ -1001,8 +975,8 @@ GList *syldap_read_basedn( SyldapServer *ldapServer ) {
                                                }
                                                ldap_value_free( vals );
                                        }
+                                       ldap_memfree( attribute );
                                }
-                               ldap_memfree( attribute );
                                if( ber != NULL ) {
                                        ber_free( ber, 0 );
                                }