OSDN Git Service

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