OSDN Git Service

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