+#if defined(_WIN32)
+static gchar*
+get_dvd_device_name(gchar *device)
+{
+ return g_strdup(device);
+}
+#else
+static gchar*
+get_dvd_device_name(GDrive *gd)
+{
+ return g_drive_get_identifier(gd, G_VOLUME_IDENTIFIER_KIND_UNIX_DEVICE);
+}
+#endif
+
+static GHashTable *volname_hash = NULL;
+static GMutex *volname_mutex = 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_direct_dvd_volume_name(const gchar *drive)
+{
+ gchar *result = NULL;
+ gchar vname[51], fsname[51];
+
+ if (GetVolumeInformation(drive, vname, 50, NULL, NULL, NULL, fsname, 51))
+ {
+ 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(gpointer gd)
+{
+ gchar *label = NULL;
+ gchar *result;
+ gchar *drive;
+
+ drive = get_dvd_device_name(gd);
+ g_mutex_lock(volname_mutex);
+ label = g_strdup(g_hash_table_lookup(volname_hash, drive));
+ g_mutex_unlock(volname_mutex);
+ 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
+ {
+ result = g_strdup_printf("%s", drive);
+ }
+ g_free(drive);
+ return result;
+}
+
+void
+ghb_volname_cache_init(void)
+{
+ volname_mutex = g_mutex_new();
+ volname_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
+ free_volname_key, free_volname_value);
+}
+
+static void
+free_drive(gpointer drive)
+{
+#if defined(_WIN32)
+ g_free(drive);
+#else
+ g_object_unref(drive);
+#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;
+
+ g_mutex_lock(volname_mutex);
+ g_hash_table_remove_all(volname_hash);
+ while (link != NULL)
+ {
+ gchar *name, *drive;
+
+#if !defined(_WIN32)
+ if (!g_drive_has_media (link->data))
+ {
+ g_object_unref(link->data);
+ link = link->next;
+ continue;
+ }
+#endif
+ drive = get_dvd_device_name(link->data);
+ 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);
+ }
+
+ free_drive(link->data);
+ link = link->next;
+ }
+ g_mutex_unlock(volname_mutex);
+
+ 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)
+{
+ g_debug("set_destination");
+ if (ghb_settings_get_boolean(ud->settings, "use_source_name"))
+ {
+ GString *str = g_string_new("");
+ gchar *vol_name, *filename, *extension;
+ gchar *new_name;
+ gint title;
+
+ filename = ghb_settings_get_string(ud->settings, "dest_file");
+ extension = ghb_settings_get_string(ud->settings, "FileFormat");
+ vol_name = ghb_settings_get_string(ud->settings, "volume_label");
+ g_string_append_printf(str, "%s", vol_name);
+ title = ghb_settings_combo_int(ud->settings, "title");
+ if (title >= 0)
+ {
+ if (ghb_settings_get_boolean(
+ ud->settings, "title_no_in_destination"))
+ {
+
+ title = ghb_settings_combo_int(ud->settings, "title");
+ g_string_append_printf(str, " - %d", title+1);
+ }
+ if (ghb_settings_get_boolean(
+ ud->settings, "chapters_in_destination"))
+ {
+ gint start, end;
+
+ if (!ghb_settings_get_boolean(
+ ud->settings, "title_no_in_destination"))
+ {
+ g_string_append_printf(str, " -");
+ }
+ start = ghb_settings_get_int(ud->settings, "start_chapter");
+ end = ghb_settings_get_int(ud->settings, "end_chapter");
+ if (start == end)
+ g_string_append_printf(str, " Ch %d", start);
+ else
+ g_string_append_printf(str, " Ch %d-%d", start, end);
+ }
+ }
+ g_string_append_printf(str, ".%s", extension);
+ new_name = g_string_free(str, FALSE);
+ ghb_ui_update(ud, "dest_file", ghb_string_value(new_name));
+ g_free(filename);
+ g_free(extension);
+ g_free(vol_name);
+ g_free(new_name);
+ }
+}
+