OSDN Git Service

LinGui: fix some audio list display problems
[handbrake-jp/handbrake-jp-git.git] / gtk / src / queuehandler.c
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3  * callbacks.c
4  * Copyright (C) John Stebbins 2008 <stebbins@stebbins>
5  * 
6  * callbacks.c is free software.
7  * 
8  * You may redistribute it and/or modify it under the terms of the
9  * GNU General Public License, as published by the Free Software
10  * Foundation; either version 2 of the License, or (at your option)
11  * any later version.
12  */
13
14 #include <gtk/gtk.h>
15 #include <gdk/gdkkeysyms.h>
16 #include <glib/gstdio.h>
17 #include <gio/gio.h>
18 #include "hb.h"
19 #include "settings.h"
20 #include "hb-backend.h"
21 #include "values.h"
22 #include "callbacks.h"
23 #include "presets.h"
24 #include "ghb-dvd.h"
25
26 G_MODULE_EXPORT void
27 queue_list_selection_changed_cb(GtkTreeSelection *selection, signal_user_data_t *ud)
28 {
29         GtkTreeModel *store;
30         GtkTreeIter iter, piter;
31         
32         g_debug("queue_list_selection_changed_cb ()");
33         // A queue entry is made up of a parent and multiple
34         // children that are visible when expanded.  When and entry
35         // is selected, I want the parent to be selected.
36         // This is purely cosmetic.
37         if (gtk_tree_selection_get_selected(selection, &store, &iter))
38         {
39                 GtkWidget *widget = GHB_WIDGET (ud->builder, "queue_edit");
40                 gtk_widget_set_sensitive (widget, TRUE);
41                 if (gtk_tree_model_iter_parent (store, &piter, &iter))
42                 {
43                         GtkTreePath *path;
44                         GtkTreeView *treeview;
45                         
46                         gtk_tree_selection_select_iter (selection, &piter);
47                         path = gtk_tree_model_get_path (store, &piter);
48                         treeview = gtk_tree_selection_get_tree_view (selection);
49                         // Make the parent visible in scroll window if it is not.
50                         gtk_tree_view_scroll_to_cell (treeview, path, NULL, FALSE, 0, 0);
51                         gtk_tree_path_free(path);
52                 }
53         }
54         else
55         {
56                 GtkWidget *widget = GHB_WIDGET (ud->builder, "queue_edit");
57                 gtk_widget_set_sensitive (widget, FALSE);
58         }
59 }
60
61 static void
62 add_to_queue_list(signal_user_data_t *ud, GValue *settings, GtkTreeIter *piter)
63 {
64         GtkTreeView *treeview;
65         GtkTreeIter iter;
66         GtkTreeStore *store;
67         gchar *info;
68         gint status;
69         GtkTreeIter citer;
70         gchar *dest, *preset, *vol_name, *basename;
71         const gchar *vcodec, *container;
72         gchar *fps, *vcodec_abbr;
73         gint title, start_point, end_point, width, height;
74         gint source_width, source_height;
75         gboolean pass2 = FALSE, keep_aspect, vqtype, turbo;
76         gint pic_par;
77         gboolean tweaks;
78         gchar *escape;
79         
80         g_debug("update_queue_list ()");
81         if (settings == NULL) return;
82         treeview = GTK_TREE_VIEW(GHB_WIDGET(ud->builder, "queue_list"));
83         store = GTK_TREE_STORE(gtk_tree_view_get_model(treeview));
84                 
85         tweaks = ghb_settings_get_boolean(settings, "allow_tweaks");
86         title = ghb_settings_get_int(settings, "titlenum");
87         start_point = ghb_settings_get_int(settings, "start_point");
88         end_point = ghb_settings_get_int(settings, "end_point");
89         vol_name = ghb_settings_get_string(settings, "volume_label");
90         dest = ghb_settings_get_string(settings, "destination");
91         basename = g_path_get_basename(dest);
92         escape = g_markup_escape_text(basename, -1);
93
94         vqtype = ghb_settings_get_boolean(settings, "vquality_type_constant");
95         if (!vqtype)
96                 pass2 = ghb_settings_get_boolean(settings, "VideoTwoPass");
97         const gchar *points = "Chapters";
98         if (ghb_settings_combo_int(settings, "PtoPType") == 0)
99                 points = "Chapters";
100         else if (ghb_settings_combo_int(settings, "PtoPType") == 1)
101                 points = "Seconds";
102         else if (ghb_settings_combo_int(settings, "PtoPType") == 2)
103                 points = "Frames";
104         info = g_strdup_printf 
105         (
106                 "<big><b>%s</b></big> "
107                 "<small>(Title %d, %s %d through %d, %d Video %s)"
108                 " --> %s</small>",
109                  vol_name, title, points, start_point, end_point, 
110                  pass2 ? 2:1, pass2 ? "Passes":"Pass", escape
111         );
112         g_free(basename);
113         g_free(escape);
114
115         if (piter)
116                 iter = *piter;
117         else
118                 gtk_tree_store_append(store, &iter, NULL);
119
120         gtk_tree_store_set(store, &iter, 1, info, 2, "hb-queue-delete", -1);
121         g_free(info);
122         status = ghb_settings_get_int(settings, "job_status");
123         switch (status)
124         {
125                 case GHB_QUEUE_PENDING:
126                         gtk_tree_store_set(store, &iter, 0, "hb-queue-job", -1);
127                         break;
128                 case GHB_QUEUE_CANCELED:
129                         gtk_tree_store_set(store, &iter, 0, "hb-canceled", -1);
130                         break;
131                 case GHB_QUEUE_RUNNING:
132                         gtk_tree_store_set(store, &iter, 0, "hb-working0", -1);
133                         break;
134                 case GHB_QUEUE_DONE:
135                         gtk_tree_store_set(store, &iter, 0, "hb-complete", -1);
136                         break;
137                 default:
138                         gtk_tree_store_set(store, &iter, 0, "hb-queue-job", -1);
139                         break;
140         }
141
142         GString *str = g_string_new("");
143         gboolean markers;
144         gboolean preset_modified;
145         gint mux;
146         const GValue *path;
147
148         container = ghb_settings_combo_option(settings, "FileFormat");
149         mux = ghb_settings_combo_int(settings, "FileFormat");
150         preset_modified = ghb_settings_get_boolean(settings, "preset_modified");
151         path = ghb_settings_get_value(settings, "preset");
152         preset = ghb_preset_path_string(path);
153         markers = ghb_settings_get_boolean(settings, "ChapterMarkers");
154
155         if (preset_modified)
156                 g_string_append_printf(str, 
157                         "<b>Modified Preset Based On:</b> <small>%s</small>\n", 
158                         preset);
159         else
160                 g_string_append_printf(str, 
161                         "<b>Preset:</b> <small>%s</small>\n", 
162                         preset);
163
164         if (markers)
165         {
166                 g_string_append_printf(str, 
167                         "<b>Format:</b> <small>%s Container, Chapter Markers</small>\n", 
168                         container);
169         }
170         else
171         {
172                 g_string_append_printf(str, 
173                         "<b>Format:</b> <small>%s Container</small>\n", container);
174         }
175         if (mux == HB_MUX_MP4)
176         {
177                 gboolean ipod, http, large;
178
179                 ipod = ghb_settings_get_boolean(settings, "Mp4iPodCompatible");
180                 http = ghb_settings_get_boolean(settings, "Mp4HttpOptimize");
181                 large = ghb_settings_get_boolean(settings, "Mp4LargeFile");
182                 if (http || ipod || large)
183                 {
184                         g_string_append_printf(str, "<b>MP4 Options:</b><small>");
185                         if (ipod)
186                                 g_string_append_printf(str, " - iPod 5G Support");
187                         if (http)
188                                 g_string_append_printf(str, " - Web Optimized");
189                         if (large)
190                                 g_string_append_printf(str, " - Large File Size (>4GB)");
191                         g_string_append_printf(str, "</small>\n");
192                 }
193         }
194         escape = g_markup_escape_text(dest, -1);
195         g_string_append_printf(str, 
196                 "<b>Destination:</b> <small>%s</small>\n", escape);
197
198         width = ghb_settings_get_int(settings, "scale_width");
199         height = ghb_settings_get_int(settings, "scale_height");
200         pic_par = ghb_settings_combo_int(settings, "PicturePAR");
201         keep_aspect = ghb_settings_get_boolean(settings, "PictureKeepRatio");
202
203         gchar *aspect_desc;
204         switch (pic_par)
205         {
206         case 0:
207         {
208                 if (keep_aspect)
209                 {
210                         aspect_desc = "(Aspect Preserved)";
211                 }
212                 else
213                 {
214                         aspect_desc = "(Aspect Lost)";
215                 }
216         } break;
217
218         case 1:
219         {
220                 aspect_desc = "(Strict Anamorphic)";
221         } break;
222
223         case 2:
224         {
225                 aspect_desc = "(Loose Anamorphic)";
226         } break;
227
228         case 3:
229         {
230                 aspect_desc = "(Custom Anamorphic)";
231         } break;
232
233         default:
234         {
235                 aspect_desc = "(Unknown)";
236         } break;
237         }
238         vqtype = ghb_settings_get_boolean(settings, "vquality_type_constant");
239
240         gchar *vq_desc = "Error";
241         gchar *vq_units = "";
242         gchar *vqstr;
243         gdouble vqvalue;
244         if (!vqtype)
245         {
246                 vqtype = ghb_settings_get_boolean(settings, "vquality_type_target");
247                 if (!vqtype)
248                 {
249                         // Has to be bitrate
250                         vqvalue = ghb_settings_get_int(settings, "VideoAvgBitrate");
251                         vq_desc = "Bitrate:";
252                         vq_units = "kbps";
253                 }
254                 else
255                 {
256                         // Target file size
257                         vqvalue = ghb_settings_get_int(settings, "VideoTargetSize");
258                         vq_desc = "Target Size:";
259                         vq_units = "MB";
260                 }
261                 vqstr = g_strdup_printf("%d", (gint)vqvalue);
262         }
263         else
264         {
265                 // Constant quality
266                 vqvalue = ghb_settings_get_double(settings, "VideoQualitySlider");
267                 vq_desc = "Constant Quality:";
268                 vqstr = g_strdup_printf("%d", (gint)vqvalue);
269                 vq_units = "(RF)";
270         }
271         fps = ghb_settings_get_string(settings, "VideoFramerate");
272         if (strcmp("source", fps) == 0)
273         {
274                 g_free(fps);
275                 if (ghb_settings_combo_int(settings, "PictureDetelecine"))
276                         fps = g_strdup("Same As Source (vfr detelecine)");
277                 else
278                         fps = g_strdup("Same As Source (variable)");
279         }
280         else
281         {
282                 gchar *tmp;
283                 tmp = g_strdup_printf("%s (constant frame rate)", fps);
284                 g_free(fps);
285                 fps = tmp;
286         }
287         vcodec = ghb_settings_combo_option(settings, "VideoEncoder");
288         vcodec_abbr = ghb_settings_get_string(settings, "VideoEncoder");
289         source_width = ghb_settings_get_int(settings, "source_width");
290         source_height = ghb_settings_get_int(settings, "source_height");
291         g_string_append_printf(str,
292                 "<b>Picture:</b> Source: <small>%d x %d, Output %d x %d %s</small>\n",
293                  source_width, source_height, width, height, aspect_desc);
294
295         gint decomb, detel;
296         gboolean decomb_deint;
297         gboolean filters = FALSE;
298
299         decomb_deint = ghb_settings_get_boolean(settings, "PictureDecombDeinterlace");
300         decomb = ghb_settings_combo_int(settings, "PictureDecomb");
301         g_string_append_printf(str, "<b>Filters:</b><small>");
302         detel = ghb_settings_combo_int(settings, "PictureDetelecine");
303         if (detel)
304         {
305                 g_string_append_printf(str, " - Detelecine");
306                 if (detel == 1)
307                 {
308                         gchar *cust;
309                         cust = ghb_settings_get_string(settings, "PictureDetelecineCustom");
310                         g_string_append_printf(str, ": %s", cust);
311                         g_free(cust);
312                 }
313                 filters = TRUE;
314         }
315         if (decomb_deint && decomb)
316         {
317                 g_string_append_printf(str, " - Decomb");
318                 if (decomb == 1)
319                 {
320                         gchar *cust;
321                         cust = ghb_settings_get_string(settings, "PictureDecombCustom");
322                         g_string_append_printf(str, ": %s", cust);
323                         g_free(cust);
324                 }
325                 filters = TRUE;
326         }
327         else if (!decomb_deint)
328         {
329                 gint deint = ghb_settings_combo_int(settings, "PictureDeinterlace");
330                 if (deint)
331                 {
332                         if (deint == 1)
333                         {
334                                 gchar *cust = ghb_settings_get_string(settings,
335                                                                                                 "PictureDeinterlaceCustom");
336                                 g_string_append_printf(str, " - Deinterlace: %s", cust);
337                                 g_free(cust);
338                         }
339                         else
340                         {
341                                 const gchar *opt = ghb_settings_combo_option(settings,
342                                                                                                         "PictureDeinterlace");
343                                 g_string_append_printf(str, " - Deinterlace: %s", opt);
344                         }
345                         filters = TRUE;
346                 }
347         }
348         gint denoise = ghb_settings_combo_int(settings, "PictureDenoise");
349         if (denoise)
350         {
351                 if (denoise == 1)
352                 {
353                         gchar *cust = ghb_settings_get_string(settings,
354                                                                                                         "PictureDenoiseCustom");
355                         g_string_append_printf(str, " - Denoise: %s", cust);
356                         g_free(cust);
357                 }
358                 else
359                 {
360                         const gchar *opt = ghb_settings_combo_option(settings,
361                                                                                                         "PictureDenoise");
362                         g_string_append_printf(str, " - Denoise: %s", opt);
363                 }
364                 filters = TRUE;
365         }
366         gint deblock = ghb_settings_get_int(settings, "PictureDeblock");
367         if (deblock >= 5)
368         {
369                 g_string_append_printf(str, " - Deblock (%d)", deblock);
370                 filters = TRUE;
371         }
372         if (ghb_settings_get_boolean(settings, "VideoGrayScale"))
373         {
374                 g_string_append_printf(str, " - Grayscale");
375                 filters = TRUE;
376         }
377         if (!filters)
378                 g_string_append_printf(str, " None");
379         g_string_append_printf(str, "</small>\n");
380
381         g_string_append_printf(str,
382                 "<b>Video:</b> <small>%s, Framerate: %s, %s %s%s</small>\n",
383                  vcodec, fps, vq_desc, vqstr, vq_units);
384
385         turbo = ghb_settings_get_boolean(settings, "VideoTurboTwoPass");
386         if (turbo)
387         {
388                 g_string_append_printf(str, "<b>Turbo:</b> <small>On</small>\n");
389         }
390         if (strcmp(vcodec_abbr, "x264") == 0)
391         {
392                 gchar *x264opts = ghb_build_x264opts_string(settings);
393                 g_string_append_printf(str, 
394                         "<b>x264 Options:</b> <small>%s</small>\n", x264opts);
395                 g_free(x264opts);
396         }
397         // Add the audios
398         gint count, ii;
399         const GValue *audio_list;
400
401         audio_list = ghb_settings_get_value(settings, "audio_list");
402         count = ghb_array_len(audio_list);
403         for (ii = 0; ii < count; ii++)
404         {
405                 gchar *bitrate, *samplerate, *track;
406                 const gchar *acodec, *mix;
407                 GValue *asettings;
408                 gdouble sr;
409
410                 asettings = ghb_array_get_nth(audio_list, ii);
411
412                 acodec = ghb_settings_combo_option(asettings, "AudioEncoderActual");
413                 bitrate = ghb_settings_get_string(asettings, "AudioBitrate");
414                 sr = ghb_settings_get_double(asettings, "AudioSamplerate");
415                 samplerate = ghb_settings_get_string(asettings, "AudioSamplerate");
416                 if ((int)sr == 0)
417                 {
418                         samplerate = g_strdup("Same As Source");
419                 }
420                 else
421                 {
422                         samplerate = g_strdup_printf("%.4g", sr);
423                 }
424                 track = ghb_settings_get_string(asettings, "AudioTrackDescription");
425                 mix = ghb_settings_combo_option(asettings, "AudioMixdown");
426                 if (count == 1)
427                         g_string_append_printf(str, "<b>Audio:</b>");
428                 else if (ii == 0)
429                         g_string_append_printf(str, "<b>Audio:</b>\n");
430                 if (count != 1)
431                         g_string_append_printf(str, "\t");
432
433                 g_string_append_printf(str,
434                         "<small> %s, Encoder: %s, Mixdown: %s, SampleRate: %s, Bitrate: %s</small>\n",
435                          track, acodec, mix, samplerate, bitrate);
436                 g_free(track);
437                 g_free(bitrate);
438                 g_free(samplerate);
439         }
440
441         // Add the audios
442         const GValue *sub_list;
443
444         sub_list = ghb_settings_get_value(settings, "subtitle_list");
445         count = ghb_array_len(sub_list);
446         for (ii = 0; ii < count; ii++)
447         {
448                 GValue *settings;
449                 gchar *track;
450                 gboolean force, burn, def;
451                 gint source;
452
453                 settings = ghb_array_get_nth(sub_list, ii);
454                 track = ghb_settings_get_string(settings, "SubtitleTrackDescription");
455                 source = ghb_settings_get_int(settings, "SubtitleSource");
456                 force = ghb_settings_get_boolean(settings, "SubtitleForced");
457                 burn = ghb_settings_get_boolean(settings, "SubtitleBurned");
458                 def = ghb_settings_get_boolean(settings, "SubtitleDefaultTrack");
459                 if (count == 1)
460                         g_string_append_printf(str, "<b>Subtitle:</b>");
461                 else if (ii == 0)
462                         g_string_append_printf(str, "<b>Subtitles:</b>\n");
463                 if (count != 1)
464                         g_string_append_printf(str, "\t");
465
466                 if (source != SRTSUB)
467                 {
468                         g_string_append_printf(str,
469                                 "<small> %s%s%s%s</small>",
470                                 track, 
471                                 force ? " (Force)":"",
472                                 burn  ? " (Burn)":"",
473                                 def   ? " (Default)":""
474                         );
475                 }
476                 else
477                 {
478                         gint offset;
479                         gchar *filename, *basename, *code;
480
481                         offset = ghb_settings_get_int(settings, "SrtOffset");
482                         filename = ghb_settings_get_string(settings, "SrtFile");
483                         basename = g_path_get_basename(filename);
484                         code = ghb_settings_get_string(settings, "SrtCodeset");
485                         g_string_append_printf(str,
486                                 "<small> %s (%s), %s, Offset (ms) %d%s</small>",
487                                 track, code, basename, offset,
488                                 def   ? " (Default)":""
489                         );
490                         g_free(filename);
491                         g_free(basename);
492                         g_free(code);
493                 }
494                 if (ii < count-1)
495                         g_string_append_printf(str, "\n");
496                 g_free(track);
497         }
498
499         info = g_string_free(str, FALSE);
500         gtk_tree_store_append(store, &citer, &iter);
501         gtk_tree_store_set(store, &citer, 1, info, -1);
502         g_free(info);
503         g_free(fps);
504         g_free(vcodec_abbr);
505         g_free(vol_name);
506         g_free(dest);
507         g_free(preset);
508 }
509
510 void
511 audio_list_refresh(signal_user_data_t *ud)
512 {
513         GtkTreeView *treeview;
514         GtkTreeIter iter;
515         GtkListStore *store;
516         gboolean done;
517         gint row = 0;
518         const GValue *audio_list;
519
520         g_debug("ghb_audio_list_refresh ()");
521         treeview = GTK_TREE_VIEW(GHB_WIDGET(ud->builder, "audio_list"));
522         store = GTK_LIST_STORE(gtk_tree_view_get_model(treeview));
523         if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter))
524         {
525                 do
526                 {
527                         const gchar *track, *codec, *br, *sr, *mix;
528                         gchar *s_drc;
529                         gint itrack;
530                         gdouble drc;
531                         GValue *asettings;
532
533                         audio_list = ghb_settings_get_value(ud->settings, "audio_list");
534                         if (row >= ghb_array_len(audio_list))
535                                 return;
536                         asettings = ghb_array_get_nth(audio_list, row);
537
538                         track = ghb_settings_combo_option(asettings, "AudioTrack");
539                         itrack = ghb_settings_combo_int(asettings, "AudioTrack");
540                         codec = ghb_settings_combo_option(asettings, "AudioEncoderActual");
541                         br = ghb_settings_combo_option(asettings, "AudioBitrate");
542                         sr = ghb_settings_combo_option(asettings, "AudioSamplerate");
543                         mix = ghb_settings_combo_option(asettings, "AudioMixdown");
544
545                         drc = ghb_settings_get_double(asettings, "AudioTrackDRCSlider");
546                         if (drc < 1.0)
547                                 s_drc = g_strdup("Off");
548                         else
549                                 s_drc = g_strdup_printf("%.1f", drc);
550
551                         gtk_list_store_set(GTK_LIST_STORE(store), &iter, 
552                                 // These are displayed in list
553                                 0, track,
554                                 1, codec,
555                                 2, br,
556                                 3, sr,
557                                 4, mix,
558                                 5, s_drc,
559                                 -1);
560                         g_free(s_drc);
561                         done = !gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &iter);
562                         row++;
563                 } while (!done);
564         }
565 }
566
567 static gboolean
568 validate_settings(signal_user_data_t *ud)
569 {
570         // Check to see if the dest file exists or is
571         // already in the queue
572         gchar *message, *dest;
573         gint count, ii;
574         gint titleindex;
575
576         titleindex = ghb_settings_combo_int(ud->settings, "title");
577         if (titleindex < 0) return FALSE;
578         dest = ghb_settings_get_string(ud->settings, "destination");
579         count = ghb_array_len(ud->queue);
580         for (ii = 0; ii < count; ii++)
581         {
582                 GValue *js;
583                 gchar *filename;
584
585                 js = ghb_array_get_nth(ud->queue, ii);
586                 filename = ghb_settings_get_string(js, "destination");
587                 if (strcmp(dest, filename) == 0)
588                 {
589                         message = g_strdup_printf(
590                                                 "Destination: %s\n\n"
591                                                 "Another queued job has specified the same destination.\n"
592                                                 "Do you want to overwrite?",
593                                                 dest);
594                         if (!ghb_message_dialog(GTK_MESSAGE_QUESTION, message, "Cancel", "Overwrite"))
595                         {
596                                 g_free(filename);
597                                 g_free(dest);
598                                 g_free(message);
599                                 return FALSE;
600                         }
601                         g_free(message);
602                         break;
603                 }
604                 g_free(filename);
605         }
606         gchar *destdir = g_path_get_dirname(dest);
607         if (!g_file_test(destdir, G_FILE_TEST_IS_DIR))
608         {
609                 message = g_strdup_printf(
610                                         "Destination: %s\n\n"
611                                         "This is not a valid directory.",
612                                         destdir);
613                 ghb_message_dialog(GTK_MESSAGE_ERROR, message, "Cancel", NULL);
614                 g_free(dest);
615                 g_free(message);
616                 g_free(destdir);
617                 return FALSE;
618         }
619 #if !defined(_WIN32)
620         // This doesn't work properly on windows
621         if (g_access(destdir, R_OK|W_OK) != 0)
622         {
623                 message = g_strdup_printf(
624                                         "Destination: %s\n\n"
625                                         "Can not read or write the directory.",
626                                         destdir);
627                 ghb_message_dialog(GTK_MESSAGE_ERROR, message, "Cancel", NULL);
628                 g_free(dest);
629                 g_free(message);
630                 g_free(destdir);
631                 return FALSE;
632         }
633 #endif
634         GFile *gfile;
635         GFileInfo *info;
636         guint64 size;
637         gchar *resolved = ghb_resolve_symlink(destdir);
638
639         gfile = g_file_new_for_path(resolved);
640         info = g_file_query_filesystem_info(gfile, 
641                                                 G_FILE_ATTRIBUTE_FILESYSTEM_FREE, NULL, NULL);
642         if (info != NULL)
643         {
644                 if (g_file_info_has_attribute(info, G_FILE_ATTRIBUTE_FILESYSTEM_FREE))
645                 {
646                         size = g_file_info_get_attribute_uint64(info, 
647                                                                         G_FILE_ATTRIBUTE_FILESYSTEM_FREE);
648                         
649                         gint64 fsize = (guint64)10 * 1024 * 1024 * 1024;
650                         if (size < fsize)
651                         {
652                                 message = g_strdup_printf(
653                                                         "Destination filesystem is almost full: %uM free\n\n"
654                                                         "Encode may be incomplete if you proceed.\n",
655                                                         (guint)(size / (1024L*1024L)));
656                                 if (!ghb_message_dialog(GTK_MESSAGE_QUESTION, message, "Cancel", "Proceed"))
657                                 {
658                                         g_free(dest);
659                                         g_free(message);
660                                         return FALSE;
661                                 }
662                                 g_free(message);
663                         }
664                 }
665                 g_object_unref(info);
666         }
667         g_object_unref(gfile);
668         g_free(resolved);
669         g_free(destdir);
670         if (g_file_test(dest, G_FILE_TEST_EXISTS))
671         {
672                 message = g_strdup_printf(
673                                         "Destination: %s\n\n"
674                                         "File already exists.\n"
675                                         "Do you want to overwrite?",
676                                         dest);
677                 if (!ghb_message_dialog(GTK_MESSAGE_QUESTION, message, "Cancel", "Overwrite"))
678                 {
679                         g_free(dest);
680                         g_free(message);
681                         return FALSE;
682                 }
683                 g_free(message);
684                 g_unlink(dest);
685         }
686         g_free(dest);
687         // Validate video quality is in a reasonable range
688         if (!ghb_validate_vquality(ud->settings))
689         {
690                 return FALSE;
691         }
692         // Validate audio settings
693         if (!ghb_validate_audio(ud))
694         {
695                 return FALSE;
696         }
697         // Validate audio settings
698         if (!ghb_validate_subtitles(ud))
699         {
700                 return FALSE;
701         }
702         // Validate video settings
703         if (!ghb_validate_video(ud))
704         {
705                 return FALSE;
706         }
707         // Validate filter settings
708         if (!ghb_validate_filters(ud))
709         {
710                 return FALSE;
711         }
712         audio_list_refresh(ud);
713         return TRUE;
714 }
715
716 static gboolean
717 queue_add(signal_user_data_t *ud)
718 {
719         // Add settings to the queue
720         GValue *settings;
721         gint titleindex;
722         gint titlenum;
723         
724         g_debug("queue_add ()");
725         if (!validate_settings(ud))
726         {
727                 return FALSE;
728         }
729
730         if (ud->queue == NULL)
731                 ud->queue = ghb_array_value_new(32);
732         // Make a copy of current settings to be used for the new job
733         settings = ghb_value_dup(ud->settings);
734         ghb_settings_set_int(settings, "job_status", GHB_QUEUE_PENDING);
735         ghb_settings_set_int(settings, "job_unique_id", 0);
736         titleindex = ghb_settings_combo_int(settings, "title");
737         titlenum = ghb_get_title_number(titleindex);
738         ghb_settings_set_int(settings, "titlenum", titlenum);
739         ghb_array_append(ud->queue, settings);
740         add_to_queue_list(ud, settings, NULL);
741         ghb_save_queue(ud->queue);
742         ghb_update_pending(ud);
743
744         return TRUE;
745 }
746
747 G_MODULE_EXPORT void
748 queue_add_clicked_cb(GtkWidget *widget, signal_user_data_t *ud)
749 {
750         g_debug("queue_add_clicked_cb ()");
751         queue_add(ud);
752 }
753
754 G_MODULE_EXPORT void
755 queue_remove_clicked_cb(GtkWidget *widget, gchar *path, signal_user_data_t *ud)
756 {
757         GtkTreeView *treeview;
758         GtkTreePath *treepath;
759         GtkTreeModel *store;
760         GtkTreeIter iter;
761         gint row;
762         gint *indices;
763         gint unique_id;
764         GValue *settings;
765         gint status;
766
767         g_debug("queue_remove_clicked_cb ()");
768         treeview = GTK_TREE_VIEW(GHB_WIDGET(ud->builder, "queue_list"));
769         store = gtk_tree_view_get_model(treeview);
770         treepath = gtk_tree_path_new_from_string (path);
771         if (gtk_tree_path_get_depth(treepath) > 1) return;
772         if (gtk_tree_model_get_iter(store, &iter, treepath))
773         {
774                 // Find the entry in the queue
775                 indices = gtk_tree_path_get_indices (treepath);
776                 row = indices[0];
777                 // Can only free the treepath After getting what I need from
778                 // indices since this points into treepath somewhere.
779                 gtk_tree_path_free (treepath);
780                 if (row < 0) return;
781                 if (row >= ghb_array_len(ud->queue))
782                         return;
783                 settings = ghb_array_get_nth(ud->queue, row);
784                 status = ghb_settings_get_int(settings, "job_status");
785                 if (status == GHB_QUEUE_RUNNING)
786                 {
787                         // Ask if wants to stop encode.
788                         if (!ghb_cancel_encode2(ud, NULL))
789                         {
790                                 return;
791                         }
792                         unique_id = ghb_settings_get_int(settings, "job_unique_id");
793                         ghb_remove_job(unique_id);
794                 }
795                 // Remove the selected item
796                 gtk_tree_store_remove(GTK_TREE_STORE(store), &iter);
797                 // Remove the corresponding item from the queue list
798                 GValue *old = ghb_array_get_nth(ud->queue, row);
799                 ghb_value_free(old);
800                 ghb_array_remove(ud->queue, row);
801                 ghb_save_queue(ud->queue);
802         }
803         else
804         {       
805                 gtk_tree_path_free (treepath);
806         }
807         ghb_update_pending(ud);
808 }
809
810 static gint
811 find_last_finished(GValue *queue)
812 {
813         GValue *js;
814         gint ii, count;
815         gint status;
816         
817         g_debug("find_last_finished");
818         count = ghb_array_len(queue);
819         for (ii = 0; ii < count; ii++)
820         {
821                 js = ghb_array_get_nth(queue, ii);
822                 status = ghb_settings_get_int(js, "job_status");
823                 if (status != GHB_QUEUE_DONE && status != GHB_QUEUE_RUNNING)
824                 {
825                         return ii-1;
826                 }
827         }
828         return -1;
829 }
830
831 // This little bit is needed to prevent the default drag motion
832 // handler from expanding rows if you hover over them while
833 // dragging.
834 // Also controls where valid drop locations are
835 G_MODULE_EXPORT gboolean
836 queue_drag_motion_cb(
837         GtkTreeView *tv,
838         GdkDragContext *ctx,
839         gint x,
840         gint y,
841         guint time,
842         signal_user_data_t *ud)
843 {
844         GtkTreePath *path = NULL;
845         GtkTreeViewDropPosition pos;
846         gint *indices, row, status, finished;
847         GValue *js;
848         GtkTreeIter iter;
849         GtkTreeView *srctv;
850         GtkTreeModel *model;
851         GtkTreeSelection *select;
852         GtkWidget *widget;
853
854         widget = gtk_drag_get_source_widget(ctx);
855         if (widget == NULL || widget != GTK_WIDGET(tv))
856                 return TRUE;
857
858         // This bit checks to see if the source is allowed to be
859         // moved.  Only pending and canceled items may be moved.
860         srctv = GTK_TREE_VIEW(gtk_drag_get_source_widget(ctx));
861         select = gtk_tree_view_get_selection (srctv);
862         gtk_tree_selection_get_selected (select, &model, &iter);
863         path = gtk_tree_model_get_path (model, &iter);
864         indices = gtk_tree_path_get_indices(path);
865         row = indices[0];
866         gtk_tree_path_free(path);
867         js = ghb_array_get_nth(ud->queue, row);
868         status = ghb_settings_get_int(js, "job_status");
869         if (status != GHB_QUEUE_PENDING && status != GHB_QUEUE_CANCELED)
870         {
871                 gdk_drag_status(ctx, 0, time);
872                 return TRUE;
873         }
874
875         // The reset checks that the destination is a valid position
876         // in the list.  Can not move above any finished or running items
877         gtk_tree_view_get_dest_row_at_pos (tv, x, y, &path, &pos);
878         if (path == NULL)
879         {
880                 gdk_drag_status(ctx, GDK_ACTION_MOVE, time);
881                 return TRUE;
882         }
883         // Don't allow *drop into*
884         if (pos == GTK_TREE_VIEW_DROP_INTO_OR_BEFORE)
885                 pos = GTK_TREE_VIEW_DROP_BEFORE;
886         if (pos == GTK_TREE_VIEW_DROP_INTO_OR_AFTER)
887                 pos = GTK_TREE_VIEW_DROP_AFTER;
888         // Don't allow droping int child items
889         if (gtk_tree_path_get_depth(path) > 1)
890         {
891                 gtk_tree_path_up(path);
892                 pos = GTK_TREE_VIEW_DROP_AFTER;
893         }
894         indices = gtk_tree_path_get_indices(path);
895         row = indices[0];
896         js = ghb_array_get_nth(ud->queue, row);
897
898         finished = find_last_finished(ud->queue);
899         if (row < finished)
900         {
901                 gtk_tree_path_free(path);
902                 gdk_drag_status(ctx, 0, time);
903                 return TRUE;
904         }
905         if (pos != GTK_TREE_VIEW_DROP_AFTER && 
906                 row == finished)
907         {
908                 gtk_tree_path_free(path);
909                 gdk_drag_status(ctx, 0, time);
910                 return TRUE;
911         }
912         gtk_tree_view_set_drag_dest_row(tv, path, pos);
913         gtk_tree_path_free(path);
914         gdk_drag_status(ctx, GDK_ACTION_MOVE, time);
915         return TRUE;
916 }
917
918 G_MODULE_EXPORT void 
919 queue_drag_cb(
920         GtkTreeView *dstwidget, 
921         GdkDragContext *dc, 
922         gint x, gint y, 
923         GtkSelectionData *selection_data, 
924         guint info, guint t, 
925         signal_user_data_t *ud)
926 {
927         GtkTreePath *path = NULL;
928         //GtkTreeModel *model;
929         GtkTreeViewDropPosition pos;
930         GtkTreeIter dstiter, srciter;
931         gint *indices, row;
932         GValue *js;
933         
934         GtkTreeModel *dstmodel = gtk_tree_view_get_model(dstwidget);
935                         
936         g_debug("queue_drag_cb ()");
937         // This doesn't work here for some reason...
938         // gtk_tree_view_get_drag_dest_row(dstwidget, &path, &pos);
939         gtk_tree_view_get_dest_row_at_pos (dstwidget, x, y, &path, &pos);
940         // This little hack is needed because attempting to drop after
941         // the last item gives us no path or pos.
942         if (path == NULL)
943         {
944                 gint n_children;
945
946                 n_children = gtk_tree_model_iter_n_children(dstmodel, NULL);
947                 if (n_children)
948                 {
949                         pos = GTK_TREE_VIEW_DROP_AFTER;
950                         path = gtk_tree_path_new_from_indices(n_children-1, -1);
951                 }
952                 else
953                 {
954                         pos = GTK_TREE_VIEW_DROP_BEFORE;
955                         path = gtk_tree_path_new_from_indices(0, -1);
956                 }
957         }
958         if (path)
959         {
960                 if (gtk_tree_path_get_depth(path) > 1)
961                         gtk_tree_path_up(path);
962                 if (gtk_tree_model_get_iter (dstmodel, &dstiter, path))
963                 {
964                         GtkTreeIter iter;
965                         GtkTreeView *srcwidget;
966                         GtkTreeModel *srcmodel;
967                         GtkTreeSelection *select;
968                         GtkTreePath *srcpath = NULL;
969                         GtkTreePath *dstpath = NULL;
970
971                         srcwidget = GTK_TREE_VIEW(gtk_drag_get_source_widget(dc));
972                         //srcmodel = gtk_tree_view_get_model(srcwidget);
973                         select = gtk_tree_view_get_selection (srcwidget);
974                         gtk_tree_selection_get_selected (select, &srcmodel, &srciter);
975
976                         srcpath = gtk_tree_model_get_path (srcmodel, &srciter);
977                         indices = gtk_tree_path_get_indices(srcpath);
978                         row = indices[0];
979                         gtk_tree_path_free(srcpath);
980                         js = ghb_array_get_nth(ud->queue, row);
981
982                         switch (pos)
983                         {
984                                 case GTK_TREE_VIEW_DROP_BEFORE:
985                                 case GTK_TREE_VIEW_DROP_INTO_OR_BEFORE:
986                                         gtk_tree_store_insert_before (GTK_TREE_STORE (dstmodel), 
987                                                                                                         &iter, NULL, &dstiter);
988                                         break;
989
990                                 case GTK_TREE_VIEW_DROP_AFTER:
991                                 case GTK_TREE_VIEW_DROP_INTO_OR_AFTER:
992                                         gtk_tree_store_insert_after (GTK_TREE_STORE (dstmodel), 
993                                                                                                         &iter, NULL, &dstiter);
994                                         break;
995
996                                 default:
997                                         break;
998                         }
999                         // Reset job to pending
1000                         ghb_settings_set_int(js, "job_status", GHB_QUEUE_PENDING);
1001                         add_to_queue_list(ud, js, &iter);
1002
1003                         dstpath = gtk_tree_model_get_path (dstmodel, &iter);
1004                         indices = gtk_tree_path_get_indices(dstpath);
1005                         row = indices[0];
1006                         gtk_tree_path_free(dstpath);
1007                         ghb_array_insert(ud->queue, row, js);
1008
1009                         srcpath = gtk_tree_model_get_path (srcmodel, &srciter);
1010                         indices = gtk_tree_path_get_indices(srcpath);
1011                         row = indices[0];
1012                         gtk_tree_path_free(srcpath);
1013                         ghb_array_remove(ud->queue, row);
1014                         gtk_tree_store_remove (GTK_TREE_STORE (srcmodel), &srciter);
1015                         ghb_save_queue(ud->queue);
1016                 }
1017                 gtk_tree_path_free(path);
1018         }
1019 }
1020
1021 void
1022 ghb_queue_buttons_grey(signal_user_data_t *ud)
1023 {
1024         GtkWidget *widget;
1025         GtkAction *action;
1026         gint queue_count;
1027         gint titleindex;
1028         gint queue_state, scan_state;
1029         gboolean show_start, show_stop, paused;
1030
1031         queue_count = ghb_array_len(ud->queue);
1032         titleindex = ghb_settings_combo_int(ud->settings, "title");
1033
1034         queue_state = ghb_get_queue_state();
1035         scan_state = ghb_get_scan_state();
1036
1037         show_stop = queue_state & 
1038                                 (GHB_STATE_WORKING | GHB_STATE_SEARCHING | 
1039                                  GHB_STATE_SCANNING | GHB_STATE_MUXING);
1040         show_start = !(scan_state & GHB_STATE_SCANNING) && 
1041                                         (titleindex >= 0 || queue_count > 0);
1042
1043
1044         paused = queue_state & GHB_STATE_PAUSED;
1045
1046         widget = GHB_WIDGET (ud->builder, "queue_start1");
1047         if (show_stop)
1048         {
1049                 gtk_widget_set_sensitive (widget, TRUE);
1050                 gtk_tool_button_set_icon_name(GTK_TOOL_BUTTON(widget), "hb-stop");
1051                 gtk_tool_button_set_label(GTK_TOOL_BUTTON(widget), "Stop");
1052                 gtk_tool_item_set_tooltip_text(GTK_TOOL_ITEM(widget), "Stop Encoding");
1053         }
1054         else
1055         {
1056                 gtk_widget_set_sensitive (widget, show_start);
1057                 gtk_tool_button_set_icon_name(GTK_TOOL_BUTTON(widget), "hb-play");
1058                 gtk_tool_button_set_label(GTK_TOOL_BUTTON(widget), "Start");
1059                 gtk_tool_item_set_tooltip_text(GTK_TOOL_ITEM(widget), "Start Encoding");
1060         }
1061         widget = GHB_WIDGET (ud->builder, "queue_start2");
1062         if (show_stop)
1063         {
1064                 gtk_widget_set_sensitive (widget, TRUE);
1065                 gtk_tool_button_set_icon_name(GTK_TOOL_BUTTON(widget), "hb-stop");
1066                 gtk_tool_button_set_label(GTK_TOOL_BUTTON(widget), "Stop");
1067                 gtk_tool_item_set_tooltip_text(GTK_TOOL_ITEM(widget), "Stop Encoding");
1068         }
1069         else
1070         {
1071                 gtk_widget_set_sensitive (widget, show_start);
1072                 gtk_tool_button_set_icon_name(GTK_TOOL_BUTTON(widget), "hb-play");
1073                 gtk_tool_button_set_label(GTK_TOOL_BUTTON(widget), "Start");
1074                 gtk_tool_item_set_tooltip_text(GTK_TOOL_ITEM(widget), "Start Encoding");
1075         }
1076         widget = GHB_WIDGET (ud->builder, "queue_pause1");
1077         if (paused)
1078         {
1079                 gtk_widget_set_sensitive (widget, show_stop);
1080                 gtk_tool_button_set_icon_name(GTK_TOOL_BUTTON(widget), "hb-play");
1081                 gtk_tool_button_set_label(GTK_TOOL_BUTTON(widget), "Resume");
1082                 gtk_tool_item_set_tooltip_text(GTK_TOOL_ITEM(widget), "Resume Encoding");
1083         }
1084         else
1085         {
1086                 gtk_widget_set_sensitive (widget, show_stop);
1087                 gtk_tool_button_set_icon_name(GTK_TOOL_BUTTON(widget), "hb-pause");
1088                 gtk_tool_button_set_label(GTK_TOOL_BUTTON(widget), "Pause");
1089                 gtk_tool_item_set_tooltip_text(GTK_TOOL_ITEM(widget), "Pause Encoding");
1090         }
1091         widget = GHB_WIDGET (ud->builder, "queue_pause2");
1092         if (paused)
1093         {
1094                 gtk_widget_set_sensitive (widget, show_stop);
1095                 gtk_tool_button_set_icon_name(GTK_TOOL_BUTTON(widget), "hb-play");
1096                 gtk_tool_button_set_label(GTK_TOOL_BUTTON(widget), "Resume");
1097                 gtk_tool_item_set_tooltip_text(GTK_TOOL_ITEM(widget), "Resume Encoding");
1098         }
1099         else
1100         {
1101                 gtk_widget_set_sensitive (widget, show_stop);
1102                 gtk_tool_button_set_icon_name(GTK_TOOL_BUTTON(widget), "hb-pause");
1103                 gtk_tool_button_set_label(GTK_TOOL_BUTTON(widget), "Pause");
1104                 gtk_tool_item_set_tooltip_text(GTK_TOOL_ITEM(widget), "Pause Encoding");
1105         }
1106
1107         action = GHB_ACTION (ud->builder, "queue_start_menu");
1108         if (show_stop)
1109         {
1110                 gtk_action_set_sensitive (action, TRUE);
1111 #if GTK_CHECK_VERSION(2, 16, 0)
1112                 gtk_action_set_icon_name(action, "hb-stop");
1113                 gtk_action_set_label(action, "S_top Queue");
1114                 gtk_action_set_tooltip(action, "Stop Encoding");
1115 #else
1116                 g_object_set_property(G_OBJECT(action), "icon-name", 
1117                                                                                         ghb_string_value("hb-stop"));
1118                 g_object_set_property(G_OBJECT(action), "label",
1119                                                                                         ghb_string_value("S_top Queue"));
1120                 g_object_set_property(G_OBJECT(action), "tooltip",
1121                                                                                         ghb_string_value("Stop Encoding"));
1122 #endif
1123         }
1124         else
1125         {
1126                 gtk_action_set_sensitive (action, show_start);
1127 #if GTK_CHECK_VERSION(2, 16, 0)
1128                 gtk_action_set_icon_name(action, "hb-play");
1129                 gtk_action_set_label(action, "_Start Queue");
1130                 gtk_action_set_tooltip(action, "Start Encoding");
1131 #else
1132                 g_object_set_property(G_OBJECT(action), "icon-name", 
1133                                                                                         ghb_string_value("hb-play"));
1134                 g_object_set_property(G_OBJECT(action), "label",
1135                                                                                         ghb_string_value("_Start Queue"));
1136                 g_object_set_property(G_OBJECT(action), "tooltip",
1137                                                                                         ghb_string_value("Start Encoding"));
1138 #endif
1139         }
1140         action = GHB_ACTION (ud->builder, "queue_pause_menu");
1141         if (paused)
1142         {
1143                 gtk_action_set_sensitive (action, show_start);
1144 #if GTK_CHECK_VERSION(2, 16, 0)
1145                 gtk_action_set_icon_name(action, "hb-play");
1146                 gtk_action_set_label(action, "_Resume Queue");
1147                 gtk_action_set_tooltip(action, "Resume Encoding");
1148 #else
1149                 g_object_set_property(G_OBJECT(action), "icon-name", 
1150                                                                                 ghb_string_value("hb-play"));
1151                 g_object_set_property(G_OBJECT(action), "label",
1152                                                                                 ghb_string_value("_Resume Queue"));
1153                 g_object_set_property(G_OBJECT(action), "tooltip",
1154                                                                                 ghb_string_value("Resume Encoding"));
1155 #endif
1156         }
1157         else
1158         {
1159                 gtk_action_set_sensitive (action, show_stop);
1160 #if GTK_CHECK_VERSION(2, 16, 0)
1161                 gtk_action_set_icon_name(action, "hb-pause");
1162                 gtk_action_set_label(action, "_Pause Queue");
1163                 gtk_action_set_tooltip(action, "Pause Encoding");
1164 #else
1165                 g_object_set_property(G_OBJECT(action), "icon-name", 
1166                                                                                 ghb_string_value("hb-pause"));
1167                 g_object_set_property(G_OBJECT(action), "label",
1168                                                                                 ghb_string_value("_Pause Queue"));
1169                 g_object_set_property(G_OBJECT(action), "tooltip",
1170                                                                                 ghb_string_value("Pause Encoding"));
1171 #endif
1172         }
1173 }
1174
1175 G_MODULE_EXPORT void
1176 queue_list_size_allocate_cb(GtkWidget *widget, GtkAllocation *allocation, GtkCellRenderer *cell)
1177 {
1178         GtkTreeViewColumn *column;
1179         gint width;
1180         
1181         column = gtk_tree_view_get_column (GTK_TREE_VIEW(widget), 0);
1182         width = gtk_tree_view_column_get_width(column);
1183         g_debug("col width %d alloc width %d", width, allocation->width);
1184         // Set new wrap-width.  Shave a little off to accomidate the icons
1185         // that share this column.
1186         if (width >= 564) // Don't allow below a certain size
1187                 g_object_set(cell, "wrap-width", width-70, NULL);
1188 }
1189
1190 G_MODULE_EXPORT void
1191 queue_start_clicked_cb(GtkWidget *xwidget, signal_user_data_t *ud)
1192 {
1193         GValue *js;
1194         gboolean running = FALSE;
1195         gint count, ii;
1196         gint status;
1197         gint state;
1198
1199         state = ghb_get_queue_state();
1200         if (state & (GHB_STATE_WORKING | GHB_STATE_SEARCHING | 
1201                                  GHB_STATE_SCANNING | GHB_STATE_MUXING))
1202         {
1203                 ghb_cancel_encode(ud, "You are currently encoding.  "
1204                                                                 "What would you like to do?");
1205                 return;
1206         }
1207
1208         count = ghb_array_len(ud->queue);
1209         for (ii = 0; ii < count; ii++)
1210         {
1211                 js = ghb_array_get_nth(ud->queue, ii);
1212                 status = ghb_settings_get_int(js, "job_status");
1213                 if ((status == GHB_QUEUE_RUNNING) || 
1214                         (status == GHB_QUEUE_PENDING))
1215                 {
1216                         running = TRUE;
1217                         break;
1218                 }
1219         }
1220         if (!running)
1221         {
1222                 // The queue has no running or pending jobs.
1223                 // Add current settings to the queue, then run.
1224                 if (!queue_add(ud))
1225                         return;
1226         }
1227         if (state == GHB_STATE_IDLE)
1228         {
1229                 // Add the first pending queue item and start
1230                 ud->current_job = ghb_start_next_job(ud, TRUE);
1231         }
1232 }
1233
1234 G_MODULE_EXPORT void
1235 queue_pause_clicked_cb(GtkWidget *xwidget, signal_user_data_t *ud)
1236 {
1237         ghb_pause_queue();
1238 }
1239
1240 gboolean
1241 ghb_reload_queue(signal_user_data_t *ud)
1242 {
1243         GValue *queue;
1244         gint unfinished = 0;
1245         gint count, ii;
1246         gint pid;
1247         gint status;
1248         GValue *settings;
1249         gchar *message;
1250
1251         g_debug("ghb_reload_queue");
1252
1253 find_pid:
1254         pid = ghb_find_pid_file();
1255         if (pid < 0)
1256                 return FALSE;
1257
1258         queue = ghb_load_old_queue(pid);
1259         ghb_remove_old_queue_file(pid);
1260         // Look for unfinished entries
1261         count = ghb_array_len(queue);
1262         for (ii = 0; ii < count; ii++)
1263         {
1264                 settings = ghb_array_get_nth(queue, ii);
1265                 status = ghb_settings_get_int(settings, "job_status");
1266                 if (status != GHB_QUEUE_DONE && status != GHB_QUEUE_CANCELED)
1267                 {
1268                         unfinished++;
1269                 }
1270         }
1271         if (!unfinished)
1272                 goto find_pid;
1273
1274         if (unfinished)
1275         {
1276                 message = g_strdup_printf(
1277                                         "You have %d unfinished job%s in a saved queue.\n\n"
1278                                         "Would you like to reload %s?",
1279                                         unfinished, 
1280                                         (unfinished > 1) ? "s" : "",
1281                                         (unfinished > 1) ? "them" : "it");
1282                 if (ghb_message_dialog(GTK_MESSAGE_QUESTION, message, "No", "Yes"))
1283                 {
1284                         GtkWidget *widget = GHB_WIDGET (ud->builder, "queue_window");
1285                         gtk_widget_show (widget);
1286                         widget = GHB_WIDGET (ud->builder, "show_queue");
1287                         gtk_toggle_tool_button_set_active(GTK_TOGGLE_TOOL_BUTTON(widget), TRUE);
1288
1289                         ud->queue = queue;
1290                         // First get rid of any old items we don't want
1291                         for (ii = count-1; ii >= 0; ii--)
1292                         {
1293                                 settings = ghb_array_get_nth(queue, ii);
1294                                 status = ghb_settings_get_int(settings, "job_status");
1295                                 if (status == GHB_QUEUE_DONE || status == GHB_QUEUE_CANCELED)
1296                                 {
1297                                         GValue *old = ghb_array_get_nth(queue, ii);
1298                                         ghb_value_free(old);
1299                                         ghb_array_remove(queue, ii);
1300                                 }
1301                         }
1302                         count = ghb_array_len(queue);
1303                         for (ii = 0; ii < count; ii++)
1304                         {
1305                                 settings = ghb_array_get_nth(queue, ii);
1306                                 ghb_settings_set_int(settings, "job_unique_id", 0);
1307                                 ghb_settings_set_int(settings, "job_status", GHB_QUEUE_PENDING);
1308                                 add_to_queue_list(ud, settings, NULL);
1309                         }
1310                         ghb_queue_buttons_grey(ud);
1311                         ghb_save_queue(ud->queue);
1312                 }
1313                 else
1314                 {
1315                         ghb_value_free(queue);
1316                 }
1317                 g_free(message);
1318         }
1319         return FALSE;
1320 }
1321
1322 G_MODULE_EXPORT gboolean 
1323 queue_key_press_cb(
1324         GtkWidget *widget, 
1325         GdkEventKey *event,
1326         signal_user_data_t *ud)
1327 {
1328         GtkTreeView *treeview;
1329         GtkTreeSelection *selection;
1330         GtkTreeModel *store;
1331         GtkTreeIter iter;
1332         gint row;
1333         gint *indices;
1334         gint unique_id;
1335         GValue *settings;
1336         gint status;
1337
1338         g_debug("queue_key_press_cb ()");
1339         if (event->keyval != GDK_Delete)
1340                 return FALSE;
1341         treeview = GTK_TREE_VIEW(GHB_WIDGET(ud->builder, "queue_list"));
1342         store = gtk_tree_view_get_model(treeview);
1343
1344         selection = gtk_tree_view_get_selection (treeview);
1345         if (gtk_tree_selection_get_selected(selection, &store, &iter))
1346         {
1347                 GtkTreePath *treepath;
1348
1349                 treepath = gtk_tree_model_get_path (store, &iter);
1350                 // Find the entry in the queue
1351                 indices = gtk_tree_path_get_indices (treepath);
1352                 row = indices[0];
1353                 // Can only free the treepath After getting what I need from
1354                 // indices since this points into treepath somewhere.
1355                 gtk_tree_path_free (treepath);
1356                 if (row < 0) return FALSE;
1357                 if (row >= ghb_array_len(ud->queue))
1358                         return FALSE;
1359                 settings = ghb_array_get_nth(ud->queue, row);
1360                 status = ghb_settings_get_int(settings, "job_status");
1361                 if (status == GHB_QUEUE_RUNNING)
1362                 {
1363                         // Ask if wants to stop encode.
1364                         if (!ghb_cancel_encode2(ud, NULL))
1365                         {
1366                                 return TRUE;
1367                         }
1368                         unique_id = ghb_settings_get_int(settings, "job_unique_id");
1369                         ghb_remove_job(unique_id);
1370                 }
1371                 // Remove the selected item
1372                 gtk_tree_store_remove(GTK_TREE_STORE(store), &iter);
1373                 // Remove the corresponding item from the queue list
1374                 GValue *old = ghb_array_get_nth(ud->queue, row);
1375                 ghb_value_free(old);
1376                 ghb_array_remove(ud->queue, row);
1377                 ghb_save_queue(ud->queue);
1378                 return TRUE;
1379         }
1380         return FALSE;
1381 }
1382
1383 GValue *ghb_queue_edit_settings = NULL;
1384
1385 G_MODULE_EXPORT void
1386 queue_edit_clicked_cb(GtkWidget *xwidget, signal_user_data_t *ud)
1387 {
1388         GtkTreeView *treeview;
1389         GtkTreeSelection *selection;
1390         GtkTreeModel *store;
1391         GtkTreeIter iter;
1392         gint row;
1393         gint *indices;
1394         gint status;
1395
1396         g_debug("queue_key_press_cb ()");
1397         treeview = GTK_TREE_VIEW(GHB_WIDGET(ud->builder, "queue_list"));
1398         store = gtk_tree_view_get_model(treeview);
1399
1400         selection = gtk_tree_view_get_selection (treeview);
1401         if (gtk_tree_selection_get_selected(selection, &store, &iter))
1402         {
1403                 GtkTreePath *treepath;
1404
1405                 treepath = gtk_tree_model_get_path (store, &iter);
1406                 // Find the entry in the queue
1407                 indices = gtk_tree_path_get_indices (treepath);
1408                 row = indices[0];
1409                 // Can only free the treepath After getting what I need from
1410                 // indices since this points into treepath somewhere.
1411                 gtk_tree_path_free (treepath);
1412                 if (row < 0) return;
1413                 if (row >= ghb_array_len(ud->queue))
1414                         return;
1415                 ghb_queue_edit_settings = ghb_array_get_nth(ud->queue, row);
1416                 status = ghb_settings_get_int(ghb_queue_edit_settings, "job_status");
1417                 if (status == GHB_QUEUE_PENDING)
1418                 {
1419                         // Remove the selected item
1420                         gtk_tree_store_remove(GTK_TREE_STORE(store), &iter);
1421                         // Remove the corresponding item from the queue list
1422                         ghb_array_remove(ud->queue, row);
1423                 }
1424                 else
1425                 {
1426                         ghb_queue_edit_settings = ghb_value_dup(ghb_queue_edit_settings);
1427                 }
1428                 gchar *source;
1429                 source = ghb_settings_get_string(ghb_queue_edit_settings, "source");
1430                 ghb_do_scan(ud, source, 0, FALSE);
1431                 g_free(source);
1432         }
1433 }
1434