OSDN Git Service

Add Bluray support
[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, "AudioEncoder");
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, icodec;
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, "AudioEncoder");
541                         icodec = ghb_settings_combo_int(asettings, "AudioEncoder");
542                         br = ghb_settings_combo_option(asettings, "AudioBitrate");
543                         sr = ghb_settings_combo_option(asettings, "AudioSamplerate");
544                         mix = ghb_settings_combo_option(asettings, "AudioMixdown");
545
546                         drc = ghb_settings_get_double(asettings, "AudioTrackDRCSlider");
547                         if (drc < 1.0)
548                                 s_drc = g_strdup("Off");
549                         else
550                                 s_drc = g_strdup_printf("%.1f", drc);
551
552                         if (icodec == HB_ACODEC_MASK)
553                                 codec = ghb_select_audio_codec_str(ud, itrack);
554
555                         gtk_list_store_set(GTK_LIST_STORE(store), &iter, 
556                                 // These are displayed in list
557                                 0, track,
558                                 1, codec,
559                                 2, br,
560                                 3, sr,
561                                 4, mix,
562                                 5, s_drc,
563                                 -1);
564                         g_free(s_drc);
565                         done = !gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &iter);
566                         row++;
567                 } while (!done);
568         }
569 }
570
571 static gboolean
572 validate_settings(signal_user_data_t *ud)
573 {
574         // Check to see if the dest file exists or is
575         // already in the queue
576         gchar *message, *dest;
577         gint count, ii;
578         gint titleindex;
579
580         titleindex = ghb_settings_combo_int(ud->settings, "title");
581         if (titleindex < 0) return FALSE;
582         dest = ghb_settings_get_string(ud->settings, "destination");
583         count = ghb_array_len(ud->queue);
584         for (ii = 0; ii < count; ii++)
585         {
586                 GValue *js;
587                 gchar *filename;
588
589                 js = ghb_array_get_nth(ud->queue, ii);
590                 filename = ghb_settings_get_string(js, "destination");
591                 if (strcmp(dest, filename) == 0)
592                 {
593                         message = g_strdup_printf(
594                                                 "Destination: %s\n\n"
595                                                 "Another queued job has specified the same destination.\n"
596                                                 "Do you want to overwrite?",
597                                                 dest);
598                         if (!ghb_message_dialog(GTK_MESSAGE_QUESTION, message, "Cancel", "Overwrite"))
599                         {
600                                 g_free(filename);
601                                 g_free(dest);
602                                 g_free(message);
603                                 return FALSE;
604                         }
605                         g_free(message);
606                         break;
607                 }
608                 g_free(filename);
609         }
610         gchar *destdir = g_path_get_dirname(dest);
611         if (!g_file_test(destdir, G_FILE_TEST_IS_DIR))
612         {
613                 message = g_strdup_printf(
614                                         "Destination: %s\n\n"
615                                         "This is not a valid directory.",
616                                         destdir);
617                 ghb_message_dialog(GTK_MESSAGE_ERROR, message, "Cancel", NULL);
618                 g_free(dest);
619                 g_free(message);
620                 g_free(destdir);
621                 return FALSE;
622         }
623 #if !defined(_WIN32)
624         // This doesn't work properly on windows
625         if (g_access(destdir, R_OK|W_OK) != 0)
626         {
627                 message = g_strdup_printf(
628                                         "Destination: %s\n\n"
629                                         "Can not read or write the directory.",
630                                         destdir);
631                 ghb_message_dialog(GTK_MESSAGE_ERROR, message, "Cancel", NULL);
632                 g_free(dest);
633                 g_free(message);
634                 g_free(destdir);
635                 return FALSE;
636         }
637 #endif
638         GFile *gfile;
639         GFileInfo *info;
640         guint64 size;
641         gchar *resolved = ghb_resolve_symlink(destdir);
642
643         gfile = g_file_new_for_path(resolved);
644         info = g_file_query_filesystem_info(gfile, 
645                                                 G_FILE_ATTRIBUTE_FILESYSTEM_FREE, NULL, NULL);
646         if (info != NULL)
647         {
648                 if (g_file_info_has_attribute(info, G_FILE_ATTRIBUTE_FILESYSTEM_FREE))
649                 {
650                         size = g_file_info_get_attribute_uint64(info, 
651                                                                         G_FILE_ATTRIBUTE_FILESYSTEM_FREE);
652                         
653                         gint64 fsize = (guint64)10 * 1024 * 1024 * 1024;
654                         if (size < fsize)
655                         {
656                                 message = g_strdup_printf(
657                                                         "Destination filesystem is almost full: %uM free\n\n"
658                                                         "Encode may be incomplete if you proceed.\n",
659                                                         (guint)(size / (1024L*1024L)));
660                                 if (!ghb_message_dialog(GTK_MESSAGE_QUESTION, message, "Cancel", "Proceed"))
661                                 {
662                                         g_free(dest);
663                                         g_free(message);
664                                         return FALSE;
665                                 }
666                                 g_free(message);
667                         }
668                 }
669                 g_object_unref(info);
670         }
671         g_object_unref(gfile);
672         g_free(resolved);
673         g_free(destdir);
674         if (g_file_test(dest, G_FILE_TEST_EXISTS))
675         {
676                 message = g_strdup_printf(
677                                         "Destination: %s\n\n"
678                                         "File already exists.\n"
679                                         "Do you want to overwrite?",
680                                         dest);
681                 if (!ghb_message_dialog(GTK_MESSAGE_QUESTION, message, "Cancel", "Overwrite"))
682                 {
683                         g_free(dest);
684                         g_free(message);
685                         return FALSE;
686                 }
687                 g_free(message);
688                 g_unlink(dest);
689         }
690         g_free(dest);
691         // Validate video quality is in a reasonable range
692         if (!ghb_validate_vquality(ud->settings))
693         {
694                 return FALSE;
695         }
696         // Validate audio settings
697         if (!ghb_validate_audio(ud))
698         {
699                 return FALSE;
700         }
701         // Validate audio settings
702         if (!ghb_validate_subtitles(ud))
703         {
704                 return FALSE;
705         }
706         // Validate video settings
707         if (!ghb_validate_video(ud))
708         {
709                 return FALSE;
710         }
711         // Validate filter settings
712         if (!ghb_validate_filters(ud))
713         {
714                 return FALSE;
715         }
716         audio_list_refresh(ud);
717         return TRUE;
718 }
719
720 static gboolean
721 queue_add(signal_user_data_t *ud)
722 {
723         // Add settings to the queue
724         GValue *settings;
725         gint titleindex;
726         gint titlenum;
727         
728         g_debug("queue_add ()");
729         if (!validate_settings(ud))
730         {
731                 return FALSE;
732         }
733
734         if (ud->queue == NULL)
735                 ud->queue = ghb_array_value_new(32);
736         // Make a copy of current settings to be used for the new job
737         settings = ghb_value_dup(ud->settings);
738         ghb_settings_set_int(settings, "job_status", GHB_QUEUE_PENDING);
739         ghb_settings_set_int(settings, "job_unique_id", 0);
740         titleindex = ghb_settings_combo_int(settings, "title");
741         titlenum = ghb_get_title_number(titleindex);
742         ghb_settings_set_int(settings, "titlenum", titlenum);
743         ghb_array_append(ud->queue, settings);
744         add_to_queue_list(ud, settings, NULL);
745         ghb_save_queue(ud->queue);
746         ghb_update_pending(ud);
747
748         return TRUE;
749 }
750
751 G_MODULE_EXPORT void
752 queue_add_clicked_cb(GtkWidget *widget, signal_user_data_t *ud)
753 {
754         g_debug("queue_add_clicked_cb ()");
755         queue_add(ud);
756 }
757
758 G_MODULE_EXPORT void
759 queue_remove_clicked_cb(GtkWidget *widget, gchar *path, signal_user_data_t *ud)
760 {
761         GtkTreeView *treeview;
762         GtkTreePath *treepath;
763         GtkTreeModel *store;
764         GtkTreeIter iter;
765         gint row;
766         gint *indices;
767         gint unique_id;
768         GValue *settings;
769         gint status;
770
771         g_debug("queue_remove_clicked_cb ()");
772         treeview = GTK_TREE_VIEW(GHB_WIDGET(ud->builder, "queue_list"));
773         store = gtk_tree_view_get_model(treeview);
774         treepath = gtk_tree_path_new_from_string (path);
775         if (gtk_tree_path_get_depth(treepath) > 1) return;
776         if (gtk_tree_model_get_iter(store, &iter, treepath))
777         {
778                 // Find the entry in the queue
779                 indices = gtk_tree_path_get_indices (treepath);
780                 row = indices[0];
781                 // Can only free the treepath After getting what I need from
782                 // indices since this points into treepath somewhere.
783                 gtk_tree_path_free (treepath);
784                 if (row < 0) return;
785                 if (row >= ghb_array_len(ud->queue))
786                         return;
787                 settings = ghb_array_get_nth(ud->queue, row);
788                 status = ghb_settings_get_int(settings, "job_status");
789                 if (status == GHB_QUEUE_RUNNING)
790                 {
791                         // Ask if wants to stop encode.
792                         if (!ghb_cancel_encode2(ud, NULL))
793                         {
794                                 return;
795                         }
796                         unique_id = ghb_settings_get_int(settings, "job_unique_id");
797                         ghb_remove_job(unique_id);
798                 }
799                 // Remove the selected item
800                 gtk_tree_store_remove(GTK_TREE_STORE(store), &iter);
801                 // Remove the corresponding item from the queue list
802                 GValue *old = ghb_array_get_nth(ud->queue, row);
803                 ghb_value_free(old);
804                 ghb_array_remove(ud->queue, row);
805                 ghb_save_queue(ud->queue);
806         }
807         else
808         {       
809                 gtk_tree_path_free (treepath);
810         }
811         ghb_update_pending(ud);
812 }
813
814 static gint
815 find_last_finished(GValue *queue)
816 {
817         GValue *js;
818         gint ii, count;
819         gint status;
820         
821         g_debug("find_last_finished");
822         count = ghb_array_len(queue);
823         for (ii = 0; ii < count; ii++)
824         {
825                 js = ghb_array_get_nth(queue, ii);
826                 status = ghb_settings_get_int(js, "job_status");
827                 if (status != GHB_QUEUE_DONE && status != GHB_QUEUE_RUNNING)
828                 {
829                         return ii-1;
830                 }
831         }
832         return -1;
833 }
834
835 // This little bit is needed to prevent the default drag motion
836 // handler from expanding rows if you hover over them while
837 // dragging.
838 // Also controls where valid drop locations are
839 G_MODULE_EXPORT gboolean
840 queue_drag_motion_cb(
841         GtkTreeView *tv,
842         GdkDragContext *ctx,
843         gint x,
844         gint y,
845         guint time,
846         signal_user_data_t *ud)
847 {
848         GtkTreePath *path = NULL;
849         GtkTreeViewDropPosition pos;
850         gint *indices, row, status, finished;
851         GValue *js;
852         GtkTreeIter iter;
853         GtkTreeView *srctv;
854         GtkTreeModel *model;
855         GtkTreeSelection *select;
856         GtkWidget *widget;
857
858         widget = gtk_drag_get_source_widget(ctx);
859         if (widget == NULL || widget != GTK_WIDGET(tv))
860                 return TRUE;
861
862         // This bit checks to see if the source is allowed to be
863         // moved.  Only pending and canceled items may be moved.
864         srctv = GTK_TREE_VIEW(gtk_drag_get_source_widget(ctx));
865         select = gtk_tree_view_get_selection (srctv);
866         gtk_tree_selection_get_selected (select, &model, &iter);
867         path = gtk_tree_model_get_path (model, &iter);
868         indices = gtk_tree_path_get_indices(path);
869         row = indices[0];
870         gtk_tree_path_free(path);
871         js = ghb_array_get_nth(ud->queue, row);
872         status = ghb_settings_get_int(js, "job_status");
873         if (status != GHB_QUEUE_PENDING && status != GHB_QUEUE_CANCELED)
874         {
875                 gdk_drag_status(ctx, 0, time);
876                 return TRUE;
877         }
878
879         // The reset checks that the destination is a valid position
880         // in the list.  Can not move above any finished or running items
881         gtk_tree_view_get_dest_row_at_pos (tv, x, y, &path, &pos);
882         if (path == NULL)
883         {
884                 gdk_drag_status(ctx, GDK_ACTION_MOVE, time);
885                 return TRUE;
886         }
887         // Don't allow *drop into*
888         if (pos == GTK_TREE_VIEW_DROP_INTO_OR_BEFORE)
889                 pos = GTK_TREE_VIEW_DROP_BEFORE;
890         if (pos == GTK_TREE_VIEW_DROP_INTO_OR_AFTER)
891                 pos = GTK_TREE_VIEW_DROP_AFTER;
892         // Don't allow droping int child items
893         if (gtk_tree_path_get_depth(path) > 1)
894         {
895                 gtk_tree_path_up(path);
896                 pos = GTK_TREE_VIEW_DROP_AFTER;
897         }
898         indices = gtk_tree_path_get_indices(path);
899         row = indices[0];
900         js = ghb_array_get_nth(ud->queue, row);
901
902         finished = find_last_finished(ud->queue);
903         if (row < finished)
904         {
905                 gtk_tree_path_free(path);
906                 gdk_drag_status(ctx, 0, time);
907                 return TRUE;
908         }
909         if (pos != GTK_TREE_VIEW_DROP_AFTER && 
910                 row == finished)
911         {
912                 gtk_tree_path_free(path);
913                 gdk_drag_status(ctx, 0, time);
914                 return TRUE;
915         }
916         gtk_tree_view_set_drag_dest_row(tv, path, pos);
917         gtk_tree_path_free(path);
918         gdk_drag_status(ctx, GDK_ACTION_MOVE, time);
919         return TRUE;
920 }
921
922 G_MODULE_EXPORT void 
923 queue_drag_cb(
924         GtkTreeView *dstwidget, 
925         GdkDragContext *dc, 
926         gint x, gint y, 
927         GtkSelectionData *selection_data, 
928         guint info, guint t, 
929         signal_user_data_t *ud)
930 {
931         GtkTreePath *path = NULL;
932         //GtkTreeModel *model;
933         GtkTreeViewDropPosition pos;
934         GtkTreeIter dstiter, srciter;
935         gint *indices, row;
936         GValue *js;
937         
938         GtkTreeModel *dstmodel = gtk_tree_view_get_model(dstwidget);
939                         
940         g_debug("queue_drag_cb ()");
941         // This doesn't work here for some reason...
942         // gtk_tree_view_get_drag_dest_row(dstwidget, &path, &pos);
943         gtk_tree_view_get_dest_row_at_pos (dstwidget, x, y, &path, &pos);
944         // This little hack is needed because attempting to drop after
945         // the last item gives us no path or pos.
946         if (path == NULL)
947         {
948                 gint n_children;
949
950                 n_children = gtk_tree_model_iter_n_children(dstmodel, NULL);
951                 if (n_children)
952                 {
953                         pos = GTK_TREE_VIEW_DROP_AFTER;
954                         path = gtk_tree_path_new_from_indices(n_children-1, -1);
955                 }
956                 else
957                 {
958                         pos = GTK_TREE_VIEW_DROP_BEFORE;
959                         path = gtk_tree_path_new_from_indices(0, -1);
960                 }
961         }
962         if (path)
963         {
964                 if (gtk_tree_path_get_depth(path) > 1)
965                         gtk_tree_path_up(path);
966                 if (gtk_tree_model_get_iter (dstmodel, &dstiter, path))
967                 {
968                         GtkTreeIter iter;
969                         GtkTreeView *srcwidget;
970                         GtkTreeModel *srcmodel;
971                         GtkTreeSelection *select;
972                         GtkTreePath *srcpath = NULL;
973                         GtkTreePath *dstpath = NULL;
974
975                         srcwidget = GTK_TREE_VIEW(gtk_drag_get_source_widget(dc));
976                         //srcmodel = gtk_tree_view_get_model(srcwidget);
977                         select = gtk_tree_view_get_selection (srcwidget);
978                         gtk_tree_selection_get_selected (select, &srcmodel, &srciter);
979
980                         srcpath = gtk_tree_model_get_path (srcmodel, &srciter);
981                         indices = gtk_tree_path_get_indices(srcpath);
982                         row = indices[0];
983                         gtk_tree_path_free(srcpath);
984                         js = ghb_array_get_nth(ud->queue, row);
985
986                         switch (pos)
987                         {
988                                 case GTK_TREE_VIEW_DROP_BEFORE:
989                                 case GTK_TREE_VIEW_DROP_INTO_OR_BEFORE:
990                                         gtk_tree_store_insert_before (GTK_TREE_STORE (dstmodel), 
991                                                                                                         &iter, NULL, &dstiter);
992                                         break;
993
994                                 case GTK_TREE_VIEW_DROP_AFTER:
995                                 case GTK_TREE_VIEW_DROP_INTO_OR_AFTER:
996                                         gtk_tree_store_insert_after (GTK_TREE_STORE (dstmodel), 
997                                                                                                         &iter, NULL, &dstiter);
998                                         break;
999
1000                                 default:
1001                                         break;
1002                         }
1003                         // Reset job to pending
1004                         ghb_settings_set_int(js, "job_status", GHB_QUEUE_PENDING);
1005                         add_to_queue_list(ud, js, &iter);
1006
1007                         dstpath = gtk_tree_model_get_path (dstmodel, &iter);
1008                         indices = gtk_tree_path_get_indices(dstpath);
1009                         row = indices[0];
1010                         gtk_tree_path_free(dstpath);
1011                         ghb_array_insert(ud->queue, row, js);
1012
1013                         srcpath = gtk_tree_model_get_path (srcmodel, &srciter);
1014                         indices = gtk_tree_path_get_indices(srcpath);
1015                         row = indices[0];
1016                         gtk_tree_path_free(srcpath);
1017                         ghb_array_remove(ud->queue, row);
1018                         gtk_tree_store_remove (GTK_TREE_STORE (srcmodel), &srciter);
1019                         ghb_save_queue(ud->queue);
1020                 }
1021                 gtk_tree_path_free(path);
1022         }
1023 }
1024
1025 void
1026 ghb_queue_buttons_grey(signal_user_data_t *ud)
1027 {
1028         GtkWidget *widget;
1029         GtkAction *action;
1030         gint queue_count;
1031         gint titleindex;
1032         gint queue_state, scan_state;
1033         gboolean show_start, show_stop, paused;
1034
1035         queue_count = ghb_array_len(ud->queue);
1036         titleindex = ghb_settings_combo_int(ud->settings, "title");
1037
1038         queue_state = ghb_get_queue_state();
1039         scan_state = ghb_get_scan_state();
1040
1041         show_stop = queue_state & 
1042                                 (GHB_STATE_WORKING | GHB_STATE_SEARCHING | 
1043                                  GHB_STATE_SCANNING | GHB_STATE_MUXING);
1044         show_start = !(scan_state & GHB_STATE_SCANNING) && 
1045                                         (titleindex >= 0 || queue_count > 0);
1046
1047
1048         paused = queue_state & GHB_STATE_PAUSED;
1049
1050         widget = GHB_WIDGET (ud->builder, "queue_start1");
1051         if (show_stop)
1052         {
1053                 gtk_widget_set_sensitive (widget, TRUE);
1054                 gtk_tool_button_set_icon_name(GTK_TOOL_BUTTON(widget), "hb-stop");
1055                 gtk_tool_button_set_label(GTK_TOOL_BUTTON(widget), "Stop");
1056                 gtk_tool_item_set_tooltip_text(GTK_TOOL_ITEM(widget), "Stop Encoding");
1057         }
1058         else
1059         {
1060                 gtk_widget_set_sensitive (widget, show_start);
1061                 gtk_tool_button_set_icon_name(GTK_TOOL_BUTTON(widget), "hb-play");
1062                 gtk_tool_button_set_label(GTK_TOOL_BUTTON(widget), "Start");
1063                 gtk_tool_item_set_tooltip_text(GTK_TOOL_ITEM(widget), "Start Encoding");
1064         }
1065         widget = GHB_WIDGET (ud->builder, "queue_start2");
1066         if (show_stop)
1067         {
1068                 gtk_widget_set_sensitive (widget, TRUE);
1069                 gtk_tool_button_set_icon_name(GTK_TOOL_BUTTON(widget), "hb-stop");
1070                 gtk_tool_button_set_label(GTK_TOOL_BUTTON(widget), "Stop");
1071                 gtk_tool_item_set_tooltip_text(GTK_TOOL_ITEM(widget), "Stop Encoding");
1072         }
1073         else
1074         {
1075                 gtk_widget_set_sensitive (widget, show_start);
1076                 gtk_tool_button_set_icon_name(GTK_TOOL_BUTTON(widget), "hb-play");
1077                 gtk_tool_button_set_label(GTK_TOOL_BUTTON(widget), "Start");
1078                 gtk_tool_item_set_tooltip_text(GTK_TOOL_ITEM(widget), "Start Encoding");
1079         }
1080         widget = GHB_WIDGET (ud->builder, "queue_pause1");
1081         if (paused)
1082         {
1083                 gtk_widget_set_sensitive (widget, show_stop);
1084                 gtk_tool_button_set_icon_name(GTK_TOOL_BUTTON(widget), "hb-play");
1085                 gtk_tool_button_set_label(GTK_TOOL_BUTTON(widget), "Resume");
1086                 gtk_tool_item_set_tooltip_text(GTK_TOOL_ITEM(widget), "Resume Encoding");
1087         }
1088         else
1089         {
1090                 gtk_widget_set_sensitive (widget, show_stop);
1091                 gtk_tool_button_set_icon_name(GTK_TOOL_BUTTON(widget), "hb-pause");
1092                 gtk_tool_button_set_label(GTK_TOOL_BUTTON(widget), "Pause");
1093                 gtk_tool_item_set_tooltip_text(GTK_TOOL_ITEM(widget), "Pause Encoding");
1094         }
1095         widget = GHB_WIDGET (ud->builder, "queue_pause2");
1096         if (paused)
1097         {
1098                 gtk_widget_set_sensitive (widget, show_stop);
1099                 gtk_tool_button_set_icon_name(GTK_TOOL_BUTTON(widget), "hb-play");
1100                 gtk_tool_button_set_label(GTK_TOOL_BUTTON(widget), "Resume");
1101                 gtk_tool_item_set_tooltip_text(GTK_TOOL_ITEM(widget), "Resume Encoding");
1102         }
1103         else
1104         {
1105                 gtk_widget_set_sensitive (widget, show_stop);
1106                 gtk_tool_button_set_icon_name(GTK_TOOL_BUTTON(widget), "hb-pause");
1107                 gtk_tool_button_set_label(GTK_TOOL_BUTTON(widget), "Pause");
1108                 gtk_tool_item_set_tooltip_text(GTK_TOOL_ITEM(widget), "Pause Encoding");
1109         }
1110
1111         action = GHB_ACTION (ud->builder, "queue_start_menu");
1112         if (show_stop)
1113         {
1114                 gtk_action_set_sensitive (action, TRUE);
1115 #if GTK_CHECK_VERSION(2, 16, 0)
1116                 gtk_action_set_icon_name(action, "hb-stop");
1117                 gtk_action_set_label(action, "S_top Queue");
1118                 gtk_action_set_tooltip(action, "Stop Encoding");
1119 #else
1120                 g_object_set_property(G_OBJECT(action), "icon-name", 
1121                                                                                         ghb_string_value("hb-stop"));
1122                 g_object_set_property(G_OBJECT(action), "label",
1123                                                                                         ghb_string_value("S_top Queue"));
1124                 g_object_set_property(G_OBJECT(action), "tooltip",
1125                                                                                         ghb_string_value("Stop Encoding"));
1126 #endif
1127         }
1128         else
1129         {
1130                 gtk_action_set_sensitive (action, show_start);
1131 #if GTK_CHECK_VERSION(2, 16, 0)
1132                 gtk_action_set_icon_name(action, "hb-play");
1133                 gtk_action_set_label(action, "_Start Queue");
1134                 gtk_action_set_tooltip(action, "Start Encoding");
1135 #else
1136                 g_object_set_property(G_OBJECT(action), "icon-name", 
1137                                                                                         ghb_string_value("hb-play"));
1138                 g_object_set_property(G_OBJECT(action), "label",
1139                                                                                         ghb_string_value("_Start Queue"));
1140                 g_object_set_property(G_OBJECT(action), "tooltip",
1141                                                                                         ghb_string_value("Start Encoding"));
1142 #endif
1143         }
1144         action = GHB_ACTION (ud->builder, "queue_pause_menu");
1145         if (paused)
1146         {
1147                 gtk_action_set_sensitive (action, show_start);
1148 #if GTK_CHECK_VERSION(2, 16, 0)
1149                 gtk_action_set_icon_name(action, "hb-play");
1150                 gtk_action_set_label(action, "_Resume Queue");
1151                 gtk_action_set_tooltip(action, "Resume Encoding");
1152 #else
1153                 g_object_set_property(G_OBJECT(action), "icon-name", 
1154                                                                                 ghb_string_value("hb-play"));
1155                 g_object_set_property(G_OBJECT(action), "label",
1156                                                                                 ghb_string_value("_Resume Queue"));
1157                 g_object_set_property(G_OBJECT(action), "tooltip",
1158                                                                                 ghb_string_value("Resume Encoding"));
1159 #endif
1160         }
1161         else
1162         {
1163                 gtk_action_set_sensitive (action, show_stop);
1164 #if GTK_CHECK_VERSION(2, 16, 0)
1165                 gtk_action_set_icon_name(action, "hb-pause");
1166                 gtk_action_set_label(action, "_Pause Queue");
1167                 gtk_action_set_tooltip(action, "Pause Encoding");
1168 #else
1169                 g_object_set_property(G_OBJECT(action), "icon-name", 
1170                                                                                 ghb_string_value("hb-pause"));
1171                 g_object_set_property(G_OBJECT(action), "label",
1172                                                                                 ghb_string_value("_Pause Queue"));
1173                 g_object_set_property(G_OBJECT(action), "tooltip",
1174                                                                                 ghb_string_value("Pause Encoding"));
1175 #endif
1176         }
1177 }
1178
1179 G_MODULE_EXPORT void
1180 queue_list_size_allocate_cb(GtkWidget *widget, GtkAllocation *allocation, GtkCellRenderer *cell)
1181 {
1182         GtkTreeViewColumn *column;
1183         gint width;
1184         
1185         column = gtk_tree_view_get_column (GTK_TREE_VIEW(widget), 0);
1186         width = gtk_tree_view_column_get_width(column);
1187         g_debug("col width %d alloc width %d", width, allocation->width);
1188         // Set new wrap-width.  Shave a little off to accomidate the icons
1189         // that share this column.
1190         if (width >= 564) // Don't allow below a certain size
1191                 g_object_set(cell, "wrap-width", width-70, NULL);
1192 }
1193
1194 G_MODULE_EXPORT void
1195 queue_start_clicked_cb(GtkWidget *xwidget, signal_user_data_t *ud)
1196 {
1197         GValue *js;
1198         gboolean running = FALSE;
1199         gint count, ii;
1200         gint status;
1201         gint state;
1202
1203         state = ghb_get_queue_state();
1204         if (state & (GHB_STATE_WORKING | GHB_STATE_SEARCHING | 
1205                                  GHB_STATE_SCANNING | GHB_STATE_MUXING))
1206         {
1207                 ghb_cancel_encode(ud, "You are currently encoding.  "
1208                                                                 "What would you like to do?");
1209                 return;
1210         }
1211
1212         count = ghb_array_len(ud->queue);
1213         for (ii = 0; ii < count; ii++)
1214         {
1215                 js = ghb_array_get_nth(ud->queue, ii);
1216                 status = ghb_settings_get_int(js, "job_status");
1217                 if ((status == GHB_QUEUE_RUNNING) || 
1218                         (status == GHB_QUEUE_PENDING))
1219                 {
1220                         running = TRUE;
1221                         break;
1222                 }
1223         }
1224         if (!running)
1225         {
1226                 // The queue has no running or pending jobs.
1227                 // Add current settings to the queue, then run.
1228                 if (!queue_add(ud))
1229                         return;
1230         }
1231         if (state == GHB_STATE_IDLE)
1232         {
1233                 // Add the first pending queue item and start
1234                 ud->current_job = ghb_start_next_job(ud, TRUE);
1235         }
1236 }
1237
1238 G_MODULE_EXPORT void
1239 queue_pause_clicked_cb(GtkWidget *xwidget, signal_user_data_t *ud)
1240 {
1241         ghb_pause_queue();
1242 }
1243
1244 gboolean
1245 ghb_reload_queue(signal_user_data_t *ud)
1246 {
1247         GValue *queue;
1248         gint unfinished = 0;
1249         gint count, ii;
1250         gint pid;
1251         gint status;
1252         GValue *settings;
1253         gchar *message;
1254
1255         g_debug("ghb_reload_queue");
1256
1257 find_pid:
1258         pid = ghb_find_pid_file();
1259         if (pid < 0)
1260                 return FALSE;
1261
1262         queue = ghb_load_old_queue(pid);
1263         ghb_remove_old_queue_file(pid);
1264         // Look for unfinished entries
1265         count = ghb_array_len(queue);
1266         for (ii = 0; ii < count; ii++)
1267         {
1268                 settings = ghb_array_get_nth(queue, ii);
1269                 status = ghb_settings_get_int(settings, "job_status");
1270                 if (status != GHB_QUEUE_DONE && status != GHB_QUEUE_CANCELED)
1271                 {
1272                         unfinished++;
1273                 }
1274         }
1275         if (!unfinished)
1276                 goto find_pid;
1277
1278         if (unfinished)
1279         {
1280                 message = g_strdup_printf(
1281                                         "You have %d unfinished job%s in a saved queue.\n\n"
1282                                         "Would you like to reload %s?",
1283                                         unfinished, 
1284                                         (unfinished > 1) ? "s" : "",
1285                                         (unfinished > 1) ? "them" : "it");
1286                 if (ghb_message_dialog(GTK_MESSAGE_QUESTION, message, "No", "Yes"))
1287                 {
1288                         GtkWidget *widget = GHB_WIDGET (ud->builder, "queue_window");
1289                         gtk_widget_show (widget);
1290                         widget = GHB_WIDGET (ud->builder, "show_queue");
1291                         gtk_toggle_tool_button_set_active(GTK_TOGGLE_TOOL_BUTTON(widget), TRUE);
1292
1293                         ud->queue = queue;
1294                         // First get rid of any old items we don't want
1295                         for (ii = count-1; ii >= 0; ii--)
1296                         {
1297                                 settings = ghb_array_get_nth(queue, ii);
1298                                 status = ghb_settings_get_int(settings, "job_status");
1299                                 if (status == GHB_QUEUE_DONE || status == GHB_QUEUE_CANCELED)
1300                                 {
1301                                         GValue *old = ghb_array_get_nth(queue, ii);
1302                                         ghb_value_free(old);
1303                                         ghb_array_remove(queue, ii);
1304                                 }
1305                         }
1306                         count = ghb_array_len(queue);
1307                         for (ii = 0; ii < count; ii++)
1308                         {
1309                                 settings = ghb_array_get_nth(queue, ii);
1310                                 ghb_settings_set_int(settings, "job_unique_id", 0);
1311                                 ghb_settings_set_int(settings, "job_status", GHB_QUEUE_PENDING);
1312                                 add_to_queue_list(ud, settings, NULL);
1313                         }
1314                         ghb_queue_buttons_grey(ud);
1315                         ghb_save_queue(ud->queue);
1316                 }
1317                 else
1318                 {
1319                         ghb_value_free(queue);
1320                 }
1321                 g_free(message);
1322         }
1323         return FALSE;
1324 }
1325
1326 G_MODULE_EXPORT gboolean 
1327 queue_key_press_cb(
1328         GtkWidget *widget, 
1329         GdkEventKey *event,
1330         signal_user_data_t *ud)
1331 {
1332         GtkTreeView *treeview;
1333         GtkTreeSelection *selection;
1334         GtkTreeModel *store;
1335         GtkTreeIter iter;
1336         gint row;
1337         gint *indices;
1338         gint unique_id;
1339         GValue *settings;
1340         gint status;
1341
1342         g_debug("queue_key_press_cb ()");
1343         if (event->keyval != GDK_Delete)
1344                 return FALSE;
1345         treeview = GTK_TREE_VIEW(GHB_WIDGET(ud->builder, "queue_list"));
1346         store = gtk_tree_view_get_model(treeview);
1347
1348         selection = gtk_tree_view_get_selection (treeview);
1349         if (gtk_tree_selection_get_selected(selection, &store, &iter))
1350         {
1351                 GtkTreePath *treepath;
1352
1353                 treepath = gtk_tree_model_get_path (store, &iter);
1354                 // Find the entry in the queue
1355                 indices = gtk_tree_path_get_indices (treepath);
1356                 row = indices[0];
1357                 // Can only free the treepath After getting what I need from
1358                 // indices since this points into treepath somewhere.
1359                 gtk_tree_path_free (treepath);
1360                 if (row < 0) return FALSE;
1361                 if (row >= ghb_array_len(ud->queue))
1362                         return FALSE;
1363                 settings = ghb_array_get_nth(ud->queue, row);
1364                 status = ghb_settings_get_int(settings, "job_status");
1365                 if (status == GHB_QUEUE_RUNNING)
1366                 {
1367                         // Ask if wants to stop encode.
1368                         if (!ghb_cancel_encode2(ud, NULL))
1369                         {
1370                                 return TRUE;
1371                         }
1372                         unique_id = ghb_settings_get_int(settings, "job_unique_id");
1373                         ghb_remove_job(unique_id);
1374                 }
1375                 // Remove the selected item
1376                 gtk_tree_store_remove(GTK_TREE_STORE(store), &iter);
1377                 // Remove the corresponding item from the queue list
1378                 GValue *old = ghb_array_get_nth(ud->queue, row);
1379                 ghb_value_free(old);
1380                 ghb_array_remove(ud->queue, row);
1381                 ghb_save_queue(ud->queue);
1382                 return TRUE;
1383         }
1384         return FALSE;
1385 }
1386
1387 GValue *ghb_queue_edit_settings = NULL;
1388
1389 G_MODULE_EXPORT void
1390 queue_edit_clicked_cb(GtkWidget *xwidget, signal_user_data_t *ud)
1391 {
1392         GtkTreeView *treeview;
1393         GtkTreeSelection *selection;
1394         GtkTreeModel *store;
1395         GtkTreeIter iter;
1396         gint row;
1397         gint *indices;
1398         gint status;
1399
1400         g_debug("queue_key_press_cb ()");
1401         treeview = GTK_TREE_VIEW(GHB_WIDGET(ud->builder, "queue_list"));
1402         store = gtk_tree_view_get_model(treeview);
1403
1404         selection = gtk_tree_view_get_selection (treeview);
1405         if (gtk_tree_selection_get_selected(selection, &store, &iter))
1406         {
1407                 GtkTreePath *treepath;
1408
1409                 treepath = gtk_tree_model_get_path (store, &iter);
1410                 // Find the entry in the queue
1411                 indices = gtk_tree_path_get_indices (treepath);
1412                 row = indices[0];
1413                 // Can only free the treepath After getting what I need from
1414                 // indices since this points into treepath somewhere.
1415                 gtk_tree_path_free (treepath);
1416                 if (row < 0) return;
1417                 if (row >= ghb_array_len(ud->queue))
1418                         return;
1419                 ghb_queue_edit_settings = ghb_array_get_nth(ud->queue, row);
1420                 status = ghb_settings_get_int(ghb_queue_edit_settings, "job_status");
1421                 if (status == GHB_QUEUE_PENDING)
1422                 {
1423                         // Remove the selected item
1424                         gtk_tree_store_remove(GTK_TREE_STORE(store), &iter);
1425                         // Remove the corresponding item from the queue list
1426                         ghb_array_remove(ud->queue, row);
1427                 }
1428                 else
1429                 {
1430                         ghb_queue_edit_settings = ghb_value_dup(ghb_queue_edit_settings);
1431                 }
1432                 gchar *source;
1433                 source = ghb_settings_get_string(ghb_queue_edit_settings, "source");
1434                 ghb_do_scan(ud, source, 0, FALSE);
1435                 g_free(source);
1436         }
1437 }
1438