OSDN Git Service

LinGui: don't disable subme 10 when psy-rd is 0
[handbrake-jp/handbrake-jp-git.git] / gtk / src / callbacks.c
index bccc030..0d2aa76 100644 (file)
@@ -284,7 +284,7 @@ on_quit1_activate(GtkMenuItem *quit, signal_user_data_t *ud)
        g_debug("on_quit1_activate ()");
        if (state & GHB_STATE_WORKING)
        {
-               if (ghb_cancel_encode("Closing HandBrake will terminate encoding.\n"))
+               if (ghb_cancel_encode2(ud, "Closing HandBrake will terminate encoding.\n"))
                {
                        ghb_hb_cleanup(FALSE);
                        prune_logs(ud);
@@ -393,7 +393,7 @@ 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))
+       if (GetVolumeInformation(drive, vname, 50, NULL, NULL, NULL, fsname, 50))
        {
                result = g_strdup_printf("%s", vname);
        }
@@ -599,21 +599,17 @@ set_destination(signal_user_data_t *ud)
 static gchar*
 get_file_label(const gchar *filename)
 {
-       static gchar *containers[] = 
-               {".vob", ".mpg", ".m2ts", ".mkv", ".mp4", ".m4v", ".avi", ".ogm", NULL};
-       gchar *base;
-       gint ii;
+       gchar *base, *pos, *end;
 
        base = g_path_get_basename(filename);
-       for (ii = 0; containers[ii] != NULL; ii++)
+       pos = strrchr(base, '.');
+       if (pos != NULL)
        {
-               if (g_str_has_suffix(base, containers[ii]))
-               {
-                       gchar *pos;
-                       pos = strrchr(base, '.');
+               // If the last '.' is within 4 chars of end of name, assume
+               // there is an extension we want to strip.
+               end = &base[strlen(base) - 1];
+               if (end - pos <= 4)
                        *pos = 0;
-                       break;
-               }
        }
        return base;
 }
@@ -646,7 +642,7 @@ resolve_drive_name(gchar *filename)
 }
 
 static gboolean
-update_source_label(signal_user_data_t *ud, const gchar *source)
+update_source_label(signal_user_data_t *ud, const gchar *source, gboolean update_dest)
 {
        gchar *label = NULL;
        gint len;
@@ -654,8 +650,8 @@ update_source_label(signal_user_data_t *ud, const gchar *source)
        gchar *start;
        gchar *filename = g_strdup(source);
        
+       g_debug("update_source_label()");
        len = strlen(filename);
-       if (filename[len-1] == G_DIR_SEPARATOR) filename[len-1] = 0;
        if (g_file_test(filename, G_FILE_TEST_IS_DIR))
        {
                // Skip dos drive letters
@@ -670,6 +666,7 @@ update_source_label(signal_user_data_t *ud, const gchar *source)
                }
                else
                {
+                       if (filename[len-1] == G_DIR_SEPARATOR) filename[len-1] = 0;
                        if (start != NULL)
                                start++;
                        else
@@ -727,7 +724,8 @@ update_source_label(signal_user_data_t *ud, const gchar *source)
                gtk_label_set_text (GTK_LABEL(widget), label);
                ghb_settings_set_string(ud->settings, "volume_label", label);
                g_free(label);
-               set_destination(ud);
+               if (update_dest)
+                       set_destination(ud);
        }
        else
        {
@@ -871,6 +869,30 @@ show_scan_progress(signal_user_data_t *ud)
        gtk_label_set_text( label, "Scanning ..." );
 }
 
+static void
+start_scan(
+       signal_user_data_t *ud, 
+       const gchar *path, 
+       gint titlenum, 
+       gint preview_count)
+{
+       GtkWidget *widget;
+       GtkAction *action;
+       ghb_status_t status;
+
+       ghb_get_status(&status);
+       if (status.scan.state != GHB_STATE_IDLE)
+               return;
+
+       widget = GHB_WIDGET(ud->builder, "sourcetoolbutton");
+       gtk_widget_set_sensitive(widget, FALSE);
+       action = GHB_ACTION(ud->builder, "source_action");
+       gtk_action_set_sensitive(action, FALSE);
+       action = GHB_ACTION(ud->builder, "source_single_action");
+       gtk_action_set_sensitive(action, FALSE);
+       ghb_backend_scan(path, titlenum, preview_count);
+}
+
 void
 ghb_do_scan(
        signal_user_data_t *ud, 
@@ -878,6 +900,7 @@ ghb_do_scan(
        gint titlenum, 
        gboolean force)
 {
+       g_debug("ghb_do_scan()");
        if (!force && last_scan_file != NULL &&
                strcmp(last_scan_file, filename) == 0)
        {
@@ -904,7 +927,7 @@ ghb_do_scan(
        {
                last_scan_file = g_strdup(filename);
                ghb_settings_set_string(ud->settings, "source", filename);
-               if (update_source_label(ud, filename))
+               if (update_source_label(ud, filename, TRUE))
                {
                        gchar *path;
                        gint preview_count;
@@ -914,7 +937,7 @@ ghb_do_scan(
                        prune_logs(ud);
 
                        preview_count = ghb_settings_get_int(ud->settings, "preview_count");
-                       ghb_backend_scan(path, titlenum, preview_count);
+                       start_scan(ud, path, titlenum, preview_count);
                        g_free(path);
                }
                else
@@ -1215,7 +1238,7 @@ window_delete_event_cb(GtkWidget *widget, GdkEvent *event, signal_user_data_t *u
        g_debug("window_delete_event_cb ()");
        if (state & GHB_STATE_WORKING)
        {
-               if (ghb_cancel_encode("Closing HandBrake will terminate encoding.\n"))
+               if (ghb_cancel_encode2(ud, "Closing HandBrake will terminate encoding.\n"))
                {
                        ghb_hb_cleanup(FALSE);
                        prune_logs(ud);
@@ -1902,8 +1925,48 @@ ghb_error_dialog(GtkMessageType type, const gchar *message, const gchar *cancel)
        gtk_widget_destroy (dialog);
 }
 
+void
+ghb_cancel_encode(signal_user_data_t *ud, const gchar *extra_msg)
+{
+       GtkWidget *dialog;
+       GtkResponseType response;
+       
+       if (extra_msg == NULL) extra_msg = "";
+       // Toss up a warning dialog
+       dialog = gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL,
+                               GTK_MESSAGE_WARNING, GTK_BUTTONS_NONE,
+                               "%sYour movie will be lost if you don't continue encoding.",
+                               extra_msg);
+       gtk_dialog_add_buttons( GTK_DIALOG(dialog), 
+                                                  "Cancel Current and Stop", 1,
+                                                  "Cancel Current, Start Next", 2,
+                                                  "Finish Current, then Stop", 3,
+                                                  "Continue Encoding", 4,
+                                                  NULL);
+       response = gtk_dialog_run(GTK_DIALOG(dialog));
+       gtk_widget_destroy (dialog);
+       switch (response)
+       {
+               case 1:
+                       ghb_stop_queue();
+                       ud->cancel_encode = GHB_CANCEL_ALL;
+                       break;
+               case 2:
+                       ghb_stop_queue();
+                       ud->cancel_encode = GHB_CANCEL_CURRENT;
+                       break;
+               case 3:
+                       ud->cancel_encode = GHB_CANCEL_FINISH;
+                       break;
+               case 4:
+               default:
+                       ud->cancel_encode = GHB_CANCEL_NONE;
+                       break;
+       }
+}
+
 gboolean
-ghb_cancel_encode(const gchar *extra_msg)
+ghb_cancel_encode2(signal_user_data_t *ud, const gchar *extra_msg)
 {
        GtkWidget *dialog;
        GtkResponseType response;
@@ -1915,13 +1978,22 @@ ghb_cancel_encode(const gchar *extra_msg)
                                "%sYour movie will be lost if you don't continue encoding.",
                                extra_msg);
        gtk_dialog_add_buttons( GTK_DIALOG(dialog), 
-                                                  "Continue Encoding", GTK_RESPONSE_NO,
-                                                  "Stop Encoding", GTK_RESPONSE_YES, NULL);
+                                                  "Cancel Current and Stop", 1,
+                                                  "Continue Encoding", 4,
+                                                  NULL);
        response = gtk_dialog_run(GTK_DIALOG(dialog));
        gtk_widget_destroy (dialog);
-       if (response == GTK_RESPONSE_NO) return FALSE;
-       ghb_stop_queue();
-       return TRUE;
+       switch (response)
+       {
+               case 1:
+                       ghb_stop_queue();
+                       ud->cancel_encode = GHB_CANCEL_ALL;
+                       return TRUE;
+               case 4:
+               default:
+                       break;
+       }
+       return FALSE;
 }
 
 static void
@@ -2077,7 +2149,7 @@ ghb_update_pending(signal_user_data_t *ud)
 
        label = GTK_LABEL(GHB_WIDGET(ud->builder, "pending_status"));
        pending = queue_pending_count(ud->queue);
-       str = g_strdup_printf("%d encode(s) pending in the queue", pending);
+       str = g_strdup_printf("%d encode(s) pending", pending);
        gtk_label_set_text(label, str);
        g_free(str);
 }
@@ -2172,6 +2244,9 @@ find_queue_job(GValue *queue, gint unique_id, GValue **job)
        
        *job = NULL;
        g_debug("find_queue_job");
+       if (unique_id == 0)  // Invalid Id
+               return -1;
+
        count = ghb_array_len(queue);
        for (ii = 0; ii < count; ii++)
        {
@@ -2319,8 +2394,18 @@ ghb_backend_events(signal_user_data_t *ud)
                GtkProgressBar *scan_prog;
                GtkLabel *label;
 
+               GtkWidget *widget;
+               GtkAction *action;
+
+               widget = GHB_WIDGET(ud->builder, "sourcetoolbutton");
+               gtk_widget_set_sensitive(widget, TRUE);
+               action = GHB_ACTION(ud->builder, "source_action");
+               gtk_action_set_sensitive(action, TRUE);
+               action = GHB_ACTION(ud->builder, "source_single_action");
+               gtk_action_set_sensitive(action, TRUE);
+
                source = ghb_settings_get_string(ud->settings, "source");
-               update_source_label(ud, source);
+               update_source_label(ud, source, FALSE);
 
                scan_prog = GTK_PROGRESS_BAR(GHB_WIDGET (ud->builder, "scan_prog"));
                gtk_progress_bar_set_fraction (scan_prog, 1.0);
@@ -2418,7 +2503,8 @@ ghb_backend_events(signal_user_data_t *ud)
                index = find_queue_job(ud->queue, status.queue.unique_id, &js);
                treeview = GTK_TREE_VIEW(GHB_WIDGET(ud->builder, "queue_list"));
                store = GTK_TREE_STORE(gtk_tree_view_get_model(treeview));
-               if (ud->cancel_encode)
+               if (ud->cancel_encode == GHB_CANCEL_ALL || 
+                       ud->cancel_encode == GHB_CANCEL_CURRENT)
                        status.queue.error = GHB_ERROR_CANCELED;
                switch( status.queue.error )
                {
@@ -2469,7 +2555,8 @@ ghb_backend_events(signal_user_data_t *ud)
                if (ud->job_activity_log)
                        g_io_channel_unref(ud->job_activity_log);
                ud->job_activity_log = NULL;
-               if (!ud->cancel_encode)
+               if (ud->cancel_encode != GHB_CANCEL_ALL &&
+                       ud->cancel_encode != GHB_CANCEL_FINISH)
                {
                        ud->current_job = ghb_start_next_job(ud, FALSE);
                }
@@ -2482,7 +2569,7 @@ ghb_backend_events(signal_user_data_t *ud)
                if (js)
                        ghb_settings_set_int(js, "job_status", qstatus);
                ghb_save_queue(ud->queue);
-               ud->cancel_encode = FALSE;
+               ud->cancel_encode = GHB_CANCEL_NONE;
 #if !GTK_CHECK_VERSION(2, 16, 0)
                GtkStatusIcon *si;
 
@@ -2617,7 +2704,7 @@ G_MODULE_EXPORT gboolean
 ghb_log_cb(GIOChannel *source, GIOCondition cond, gpointer data)
 {
        gchar *text = NULL;
-       gsize length;
+       gsize length, outlength;
        GtkTextView *textview;
        GtkTextBuffer *buffer;
        GtkTextIter iter;
@@ -2628,7 +2715,11 @@ ghb_log_cb(GIOChannel *source, GIOCondition cond, gpointer data)
        signal_user_data_t *ud = (signal_user_data_t*)data;
 
        status = g_io_channel_read_line (source, &text, &length, NULL, &gerror);
-       if (text != NULL && length > 0 && text[length-1] != 0)
+       // Trim nils from end of text, they cause g_io_channel_write_chars to
+       // fail with an assertion that aborts
+       while (length > 0 && text[length-1] == 0)
+               length--;
+       if (text != NULL && length > 0)
        {
                GdkWindow *window;
                gint width, height;
@@ -2672,7 +2763,7 @@ ghb_log_cb(GIOChannel *source, GIOCondition cond, gpointer data)
                text[length-1] = '\r';
 #endif
                g_io_channel_write_chars (ud->activity_log, text, 
-                                                               length, &length, NULL);
+                                                               length, &outlength, NULL);
 #if defined(_WIN32)
                g_io_channel_write_chars (ud->activity_log, "\n", 
                                                                one, &one, NULL);
@@ -2681,15 +2772,17 @@ ghb_log_cb(GIOChannel *source, GIOCondition cond, gpointer data)
                if (ud->job_activity_log)
                {
                        g_io_channel_write_chars (ud->job_activity_log, text, 
-                                                                       length, &length, NULL);
+                                                                       length, &outlength, NULL);
 #if defined(_WIN32)
                        g_io_channel_write_chars (ud->activity_log, "\n", 
-                                                                       one, &one, NULL);
+                                                                       one, &outlength, NULL);
 #endif
                        g_io_channel_flush(ud->job_activity_log, NULL);
                }
-               g_free(text);
        }
+       if (text != NULL)
+               g_free(text);
+
        if (status != G_IO_STATUS_NORMAL)
        {
                // This should never happen, but if it does I would get into an
@@ -3351,14 +3444,15 @@ handle_media_change(const gchar *device, gboolean insert, signal_user_data_t *ud
                if (ins_count == 2)
                {
                        g_thread_create((GThreadFunc)ghb_cache_volnames, ud, FALSE, NULL);
-                       if (ud->current_dvd_device != NULL &&
+                       if (ghb_settings_get_boolean(ud->settings, "AutoScan") &&
+                               ud->current_dvd_device != NULL &&
                                strcmp(device, ud->current_dvd_device) == 0)
                        {
                                show_scan_progress(ud);
-                               update_source_label(ud, device);
+                               update_source_label(ud, device, TRUE);
                                gint preview_count;
                                preview_count = ghb_settings_get_int(ud->settings, "preview_count");
-                               ghb_backend_scan(device, 0, preview_count);
+                               start_scan(ud, device, 0, preview_count);
                        }
                }
        }
@@ -3374,7 +3468,8 @@ handle_media_change(const gchar *device, gboolean insert, signal_user_data_t *ud
                        {
                                ghb_hb_cleanup(TRUE);
                                prune_logs(ud);
-                               ghb_backend_scan("/dev/null", 0, 1);
+                               ghb_settings_set_string(ud->settings, "source", "/dev/null");
+                               start_scan(ud, "/dev/null", 0, 1);
                        }
                }
        }
@@ -3454,17 +3549,21 @@ drive_changed_cb(GVolumeMonitor *gvm, GDrive *gd, signal_user_data_t *ud)
        }
        if (g_drive_has_media(gd))
        {
-               show_scan_progress(ud);
-               update_source_label(ud, device);
-               gint preview_count;
-               preview_count = ghb_settings_get_int(ud->settings, "preview_count");
-               ghb_backend_scan(device, 0, preview_count);
+               if (ghb_settings_get_boolean(ud->settings, "AutoScan"))
+               {
+                       show_scan_progress(ud);
+                       update_source_label(ud, device, TRUE);
+                       gint preview_count;
+                       preview_count = ghb_settings_get_int(ud->settings, "preview_count");
+                       start_scan(ud, device, 0, preview_count);
+               }
        }
        else
        {
                ghb_hb_cleanup(TRUE);
                prune_logs(ud);
-               ghb_backend_scan("/dev/null", 0, 1);
+               ghb_settings_set_string(ud->settings, "source", "/dev/null");
+               start_scan(ud, "/dev/null", 0, 1);
        }
 }
 #endif