diff src/bluetooth/bluetooth.c @ 2728:229647ca4942

added some UI stuff
author paula@crocodil
date Sun, 29 Jun 2008 00:37:01 +0300
parents d891ba4be5a5
children 1a27c497e526
line wrap: on
line diff
--- a/src/bluetooth/bluetooth.c	Mon Jun 23 12:15:36 2008 +0300
+++ b/src/bluetooth/bluetooth.c	Sun Jun 29 00:37:01 2008 +0300
@@ -5,12 +5,14 @@
 #include "bluetooth.h"
 #include "marshal.h"
 #include "gui.h"
-
+#include "scan_gui.h"
 #define DEBUG 1
 static gboolean plugin_active = FALSE,exiting=FALSE;
 GList * current_device = NULL;
 DBusGConnection * bus = NULL;
-gint discover_finish =0;
+DBusGProxy * obj = NULL;
+gint config = 0;
+gint devices_no = 0;
 GStaticMutex mutex = G_STATIC_MUTEX_INIT;
 
 void bluetooth_init ( void );
@@ -23,7 +25,7 @@
 static void print_results(void);
 static void discovery_completed(DBusGProxy *object, gpointer user_data);
 void discover_devices(void);
-
+void disconnect_dbus_signals(void);
 
 
 GeneralPlugin bluetooth_gp =
@@ -36,6 +38,7 @@
 };
 GeneralPlugin *bluetooth_gplist[] = { &bluetooth_gp, NULL };
 DECLARE_PLUGIN(bluetooth_gp, NULL, NULL, NULL, NULL, NULL, bluetooth_gplist, NULL, NULL)
+
 void bluetooth_init ( void )
 {
     audio_devices = NULL;
@@ -45,33 +48,60 @@
 void bluetooth_cleanup ( void )
 {
     printf("bluetooth: exit\n");
-    if(discover_finish == 2) {
+        if (config ==1 ){
+            destroy_scan_window();
+            config =0;
+        }
+        if(discover_finish == 2) {
         dbus_g_connection_flush (bus);
         dbus_g_connection_unref(bus);
     }
+    disconnect_dbus_signals();
 
 }
-/*void bt_cfg( void )
-  {
 
-  }
-  */
 void bt_about( void )
 {
+    printf("about call\n");
+    show_scan();
+}
+
+void bt_cfg(void)
+{   config =1;
+    if(discover_finish == 2){
+        if (devices_no == 0){
+            printf("no devs!\n");
+            show_scan();
+            show_no_devices();
+        }else 
+            results_ui();
+    }
+    else show_scan();    
+}
+
+void disconnect_dbus_signals(){
+
+    dbus_g_proxy_disconnect_signal(obj, "RemoteDeviceFound", G_CALLBACK(remote_device_found), bus);
+    dbus_g_proxy_disconnect_signal(obj, "DiscoveryStarted", G_CALLBACK(discovery_started), bus);
+    dbus_g_proxy_disconnect_signal(obj, "DiscoveryCompleted", G_CALLBACK(discovery_completed), bus);
+    dbus_g_proxy_disconnect_signal(obj, "RemoteNameUpdated", G_CALLBACK(remote_name_updated), NULL);
 
 }
 
 
 void refresh_call(void){
-   if(discover_finish == 0 ||discover_finish== 2)
-       discover_devices();
-       else 
-           printf("Scanning please wait!\n");
     printf("refresh function called\n");
+    disconnect_dbus_signals();
+    if(discover_finish == 0 ||discover_finish== 2){
+        discover_finish = 0;
+        discover_devices();
+    }
+    else 
+        printf("Scanning please wait!\n");
 }
 
 void connect_call(void){
-  printf("connect function \n");
+    printf("connect function \n");
 }
 
 
@@ -82,7 +112,6 @@
     current_device = audio_devices;
     if((class & 0x200404)==0x200404)
     {
-
         while(current_device != NULL)
         {
             if(g_str_equal(address,((DeviceData*)(current_device->data))->address)) 
@@ -98,8 +127,7 @@
             dev->class = class;
             dev->address = g_strdup(address);
             dev->name = NULL;
-            audio_devices=g_list_prepend(audio_devices, dev);     
-
+            audio_devices=g_list_prepend(audio_devices, dev);
         }
     }
     g_static_mutex_unlock(&mutex);
@@ -130,45 +158,54 @@
 {
     int i=0;
     g_print("Final Scan results:\n");
-    g_print("Number of audio devices: %d \n",g_list_length(audio_devices));
-    current_device=audio_devices;
-    while(current_device != NULL)
-    {
-        g_print("Device %d: Name: %s, Class: 0x%x, Address: %s\n",++i,
-                ((DeviceData*)(current_device->data))-> name,
-                ((DeviceData*)(current_device->data))-> class,
-                ((DeviceData*)(current_device->data))-> address);
+    devices_no = g_list_length(audio_devices);
+    g_print("Number of audio devices: %d \n",devices_no);
+    if(devices_no==0 ) {
+        if(config ==1) show_no_devices();        
+    } else {
+        current_device=audio_devices;
+        while(current_device != NULL)
+        {
+            g_print("Device %d: Name: %s, Class: 0x%x, Address: %s\n",++i,
+                    ((DeviceData*)(current_device->data))-> name,
+                    ((DeviceData*)(current_device->data))-> class,
+                    ((DeviceData*)(current_device->data))-> address);
+            current_device=g_list_next(current_device);
+        }
+        destroy_scan_window();
+      if(config==1) {
+          destroy_scan_window();
+          results_ui();
+      }
+        //   refresh_tree();
+    }
+}
 
-        current_device=g_list_next(current_device);
-    }
 
-    refresh_tree();
-}
 
 static void discovery_completed(DBusGProxy *object, gpointer user_data)
 {
     g_print("Signal: DiscoveryCompleted()\n");
+    discover_finish =2;
     print_results();
-    discover_finish =2;
 }
 
 
+
 void discover_devices(void){
     GError *error = NULL;
-    DBusGProxy * obj = NULL;
-    g_type_init();
+    //  g_type_init();
     g_log_set_always_fatal (G_LOG_LEVEL_WARNING);
-
     bus = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
     if (error != NULL)
     {
         g_printerr("Connecting to system bus failed: %s\n", error->message);
         g_error_free(error);
-        exit(EXIT_FAILURE);
     }
     obj = dbus_g_proxy_new_for_name(bus, "org.bluez", "/org/bluez/hci0", "org.bluez.Adapter");
+    printf("bluetooth plugin - start discovery \n");
+    dbus_g_object_register_marshaller(marshal_VOID__STRING_UINT_INT, G_TYPE_NONE, G_TYPE_STRING, G_TYPE_UINT, G_TYPE_INT, G_TYPE_INVALID);
 
-    dbus_g_object_register_marshaller(marshal_VOID__STRING_UINT_INT, G_TYPE_NONE, G_TYPE_STRING, G_TYPE_UINT, G_TYPE_INT, G_TYPE_INVALID);
     dbus_g_proxy_add_signal(obj, "RemoteDeviceFound", G_TYPE_STRING, G_TYPE_UINT, G_TYPE_INT, G_TYPE_INVALID);
     dbus_g_proxy_connect_signal(obj, "RemoteDeviceFound", G_CALLBACK(remote_device_found), bus, NULL);
 
@@ -179,6 +216,7 @@
     dbus_g_proxy_connect_signal(obj, "DiscoveryCompleted", G_CALLBACK(discovery_completed), bus, NULL);
 
     dbus_g_object_register_marshaller(marshal_VOID__STRING_STRING, G_TYPE_NONE, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INVALID);
+
     dbus_g_proxy_add_signal(obj, "RemoteNameUpdated", G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INVALID);
     dbus_g_proxy_connect_signal(obj, "RemoteNameUpdated", G_CALLBACK(remote_name_updated), NULL, NULL);
 
@@ -187,8 +225,6 @@
     {
         g_printerr("Failed to discover devices: %s\n", error->message);
         g_error_free(error);
-        exit(EXIT_FAILURE);
     }
-      dbus_g_connection_flush (bus);
-      dbus_g_connection_unref(bus);
-}    
+
+}