OSDN Git Service

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