OSDN Git Service

LinGui: Put DVD volume name scanning on it's own thread.
authorjstebbins <jstebbins@b64f7644-9d1e-0410-96f1-a4d463321fa5>
Fri, 19 Jun 2009 21:37:29 +0000 (21:37 +0000)
committerjstebbins <jstebbins@b64f7644-9d1e-0410-96f1-a4d463321fa5>
Fri, 19 Jun 2009 21:37:29 +0000 (21:37 +0000)
At startup, I scan all dvd devices for their volume names.  Usually this
information is cached by the filesystem so this is fast.  But every once
in a while this takes several seconds which delays the initial display
of the UI.  This activity is now on it's own thread to prevent the delay.

git-svn-id: svn://localhost/HandBrake/trunk@2578 b64f7644-9d1e-0410-96f1-a4d463321fa5

gtk/src/callbacks.c
gtk/src/callbacks.h
gtk/src/main.c
gtk/src/subtitlehandler.c

index 810051d..d472889 100644 (file)
@@ -365,41 +365,68 @@ get_dvd_device_name(GDrive *gd)
 }
 #endif
 
+static GHashTable *volname_hash = NULL;
+
+static void
+free_volname_key(gpointer data)
+{
+       if (data != NULL)
+               g_free(data);
+}
+
+static void
+free_volname_value(gpointer data)
+{
+       if (data != NULL)
+               g_free(data);
+}
+
 #if defined(_WIN32)
 static gchar*
-get_dvd_volume_name(const gchar *drive)
+get_direct_dvd_volume_name(const gchar *drive)
 {
-       gchar *result;
+       gchar *result = NULL;
        gchar vname[51], fsname[51];
+
        if (GetVolumeInformation(drive, vname, 50, NULL, NULL, NULL, fsname, 51))
        {
-               result = g_strdup_printf("%s (%s)", vname, drive);
-       }
-       else
-       {
-               result = g_strdup_printf("%s", drive);
+               result = g_strdup_printf("%s", vname);
        }
        return result;
 }
 #else
 static gchar*
+get_direct_dvd_volume_name(const gchar *drive)
+{
+       gchar *result;
+
+       result = ghb_dvd_volname (drive);
+       return result;
+}
+#endif
+static gchar*
 get_dvd_volume_name(GDrive *gd)
 {
-       gchar *label;
+       gchar *label = NULL;
        gchar *result;
        gchar *drive;
 
-       drive = g_drive_get_identifier(gd, G_VOLUME_IDENTIFIER_KIND_UNIX_DEVICE);
+       drive = get_dvd_device_name(gd);
        if (g_drive_has_media (gd))
        {
-               label = ghb_dvd_volname (drive);
+               if (volname_hash != NULL)
+                       label = g_strdup(g_hash_table_lookup(volname_hash, drive));
                if (label != NULL)
                {
                        if (uppers_and_unders(label))
                        {
                                camel_convert(label);
                        }
+#if defined(_WIN32)
+                       result = g_strdup_printf("%s (%s)", label, drive);
+#else
                        result = g_strdup_printf("%s - %s", drive, label);
+#endif
                        g_free(label);
                }
                else
@@ -414,8 +441,48 @@ get_dvd_volume_name(GDrive *gd)
        g_free(drive);
        return result;
 }
-#endif
 
+gpointer
+ghb_cache_volnames(signal_user_data_t *ud)
+{
+       GList *link, *drives;
+
+       g_debug("ghb_cache_volnames()");
+       link = drives = dvd_device_list();
+       if (drives == NULL)
+               return NULL;
+
+       if (volname_hash == NULL)
+       {
+               volname_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
+                                                                               free_volname_key, free_volname_value);
+       }
+       while (link != NULL)
+       {
+               gchar *drive = get_dvd_device_name(link->data);
+               gchar *name = get_direct_dvd_volume_name(drive);
+               if (drive != NULL && name != NULL)
+               {
+                       g_hash_table_insert(volname_hash, drive, name);
+               }
+               else
+               {
+                       if (drive != NULL)
+                               g_free(drive);
+                       if (name != NULL)
+                               g_free(name);
+               }
+       
+               g_object_unref(link->data);
+               link = link->next;
+       }
+
+       g_list_free(drives);
+
+       g_idle_add((GSourceFunc)ghb_file_menu_add_dvd, ud);
+
+       return NULL;
+}
 
 static void
 set_destination(signal_user_data_t *ud)
@@ -2887,6 +2954,7 @@ ghb_file_menu_add_dvd(signal_user_data_t *ud)
        static GtkActionGroup *agroup = NULL;
        static gint merge_id;
 
+       g_debug("ghb_file_menu_add_dvd()\n");
        link = drives = dvd_device_list();
        if (drives != NULL)
        {
@@ -3134,7 +3202,7 @@ drive_changed_cb(GVolumeMonitor *gvm, GDrive *gd, signal_user_data_t *ud)
        gint state;
 
        g_debug("drive_changed_cb()");
-       ghb_file_menu_add_dvd(ud);
+       g_thread_create((GThreadFunc)ghb_cache_volnames, ud, FALSE, NULL);
 
        state = ghb_get_scan_state();
        device = g_drive_get_identifier(gd, G_VOLUME_IDENTIFIER_KIND_UNIX_DEVICE);
index 5b2816c..787aa44 100644 (file)
@@ -58,6 +58,7 @@ void ghb_inhibit_gpm(void);
 #if defined(_WIN32)
 void wm_drive_changed(MSG *msg, signal_user_data_t *ud);
 #endif
+gpointer ghb_cache_volnames(signal_user_data_t *ud);
 
 #endif // _CALLBACKS_H_
 
index 4a68484..fc0a78d 100644 (file)
@@ -788,7 +788,7 @@ main (int argc, char *argv[])
        g_timeout_add (500, ghb_timer_cb, (gpointer)ud);
 
        // Add dvd devices to File menu
-       g_idle_add((GSourceFunc)ghb_file_menu_add_dvd, ud);
+       g_thread_create((GThreadFunc)ghb_cache_volnames, ud, FALSE, NULL);
 
        GtkStatusIcon *si;
        si = GTK_STATUS_ICON(GHB_OBJECT(ud->builder, "hb_status"));
index ea863b6..c5d44eb 100644 (file)
 
 static void add_to_subtitle_list(signal_user_data_t *ud, GValue *settings);
 
-void
+static void
 free_subtitle_index_list(gpointer data)
 {
        g_free(data);
 }
 
-void
+static void
 free_subtitle_key(gpointer data)
 {
        if (data != NULL)
@@ -188,7 +188,7 @@ ghb_set_pref_subtitle(gint titleindex, signal_user_data_t *ud)
        
        g_debug("ghb_set_pref_subtitle %d", titleindex);
        track_indices = g_hash_table_new_full(g_str_hash, g_str_equal, 
-                                                                                       NULL, free_subtitle_index_list);
+                                                                                       free_subtitle_key, free_subtitle_index_list);
 
        ghb_ui_update(ud, "SubtitleTrack", ghb_int_value(0));