OSDN Git Service

LinGui: integrate with eddyg's subtitle list changes
[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                 g_string_append_printf(str,
410                         "<b>Audio:</b><small> %s, Encoder: %s, Mixdown: %s, SampleRate: %s, Bitrate: %s</small>",
411                          track, acodec, mix, samplerate, bitrate);
412                 if (ii < count-1)
413                         g_string_append_printf(str, "\n");
414                 g_free(track);
415                 g_free(bitrate);
416                 g_free(samplerate);
417         }
418         info = g_string_free(str, FALSE);
419         gtk_tree_store_append(store, &citer, &iter);
420         gtk_tree_store_set(store, &citer, 1, info, -1);
421         g_free(info);
422         g_free(fps);
423         g_free(vcodec_abbr);
424         g_free(vol_name);
425         g_free(dest);
426         g_free(preset);
427 }
428
429 void
430 audio_list_refresh(signal_user_data_t *ud)
431 {
432         GtkTreeView *treeview;
433         GtkTreeIter iter;
434         GtkListStore *store;
435         gboolean done;
436         gint row = 0;
437         const GValue *audio_list;
438
439         g_debug("ghb_audio_list_refresh ()");
440         treeview = GTK_TREE_VIEW(GHB_WIDGET(ud->builder, "audio_list"));
441         store = GTK_LIST_STORE(gtk_tree_view_get_model(treeview));
442         if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter))
443         {
444                 do
445                 {
446                         const gchar *track, *codec, *br, *sr, *mix;
447                         gchar *drc, *s_track, *s_codec, *s_br, *s_sr, *s_mix;
448                         gdouble s_drc;
449                         GValue *asettings;
450
451                         audio_list = ghb_settings_get_value(ud->settings, "audio_list");
452                         if (row >= ghb_array_len(audio_list))
453                                 return;
454                         asettings = ghb_array_get_nth(audio_list, row);
455
456                         track = ghb_settings_combo_option(asettings, "AudioTrack");
457                         codec = ghb_settings_combo_option(asettings, "AudioEncoder");
458                         br = ghb_settings_combo_option(asettings, "AudioBitrate");
459                         sr = ghb_settings_combo_option(asettings, "AudioSamplerate");
460                         mix = ghb_settings_combo_option(asettings, "AudioMixdown");
461
462                         s_track = ghb_settings_get_string(asettings, "AudioTrack");
463                         s_codec = ghb_settings_get_string(asettings, "AudioEncoder");
464                         s_br = ghb_settings_get_string(asettings, "AudioBitrate");
465                         s_sr = ghb_settings_get_string(asettings, "AudioSamplerate");
466                         s_mix = ghb_settings_get_string(asettings, "AudioMixdown");
467                         s_drc = ghb_settings_get_double(asettings, "AudioTrackDRCSlider");
468                         if (s_drc < 0.1)
469                                 drc = g_strdup("Off");
470                         else
471                                 drc = g_strdup_printf("%.1f", s_drc);
472
473                         gtk_list_store_set(GTK_LIST_STORE(store), &iter, 
474                                 // These are displayed in list
475                                 0, track,
476                                 1, codec,
477                                 2, br,
478                                 3, sr,
479                                 4, mix,
480                                 5, drc,
481                                 // These are used to set combo values when an item is selected
482                                 6, s_track,
483                                 7, s_codec,
484                                 8, s_br,
485                                 9, s_sr,
486                                 10, s_mix,
487                                 11, s_drc,
488                                 -1);
489                         g_free(drc);
490                         g_free(s_track);
491                         g_free(s_codec);
492                         g_free(s_br);
493                         g_free(s_sr);
494                         g_free(s_mix);
495                         done = !gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &iter);
496                         row++;
497                 } while (!done);
498         }
499 }
500
501 static gboolean
502 validate_settings(signal_user_data_t *ud)
503 {
504         // Check to see if the dest file exists or is
505         // already in the queue
506         gchar *message, *dest;
507         gint count, ii;
508         gint titleindex;
509
510         titleindex = ghb_settings_combo_int(ud->settings, "title");
511         if (titleindex < 0) return FALSE;
512         dest = ghb_settings_get_string(ud->settings, "destination");
513         count = ghb_array_len(ud->queue);
514         for (ii = 0; ii < count; ii++)
515         {
516                 GValue *js;
517                 gchar *filename;
518
519                 js = ghb_array_get_nth(ud->queue, ii);
520                 filename = ghb_settings_get_string(js, "destination");
521                 if (strcmp(dest, filename) == 0)
522                 {
523                         message = g_strdup_printf(
524                                                 "Destination: %s\n\n"
525                                                 "Another queued job has specified the same destination.\n"
526                                                 "Do you want to overwrite?",
527                                                 dest);
528                         if (!ghb_message_dialog(GTK_MESSAGE_QUESTION, message, "Cancel", "Overwrite"))
529                         {
530                                 g_free(filename);
531                                 g_free(dest);
532                                 g_free(message);
533                                 return FALSE;
534                         }
535                         g_free(message);
536                         break;
537                 }
538                 g_free(filename);
539         }
540         gchar *destdir = g_path_get_dirname(dest);
541         if (!g_file_test(destdir, G_FILE_TEST_IS_DIR))
542         {
543                 message = g_strdup_printf(
544                                         "Destination: %s\n\n"
545                                         "This is not a valid directory.",
546                                         destdir);
547                 ghb_message_dialog(GTK_MESSAGE_ERROR, message, "Cancel", NULL);
548                 g_free(dest);
549                 g_free(message);
550                 g_free(destdir);
551                 return FALSE;
552         }
553         if (g_access(destdir, R_OK|W_OK) != 0)
554         {
555                 message = g_strdup_printf(
556                                         "Destination: %s\n\n"
557                                         "Can not read or write the directory.",
558                                         destdir);
559                 ghb_message_dialog(GTK_MESSAGE_ERROR, message, "Cancel", NULL);
560                 g_free(dest);
561                 g_free(message);
562                 g_free(destdir);
563                 return FALSE;
564         }
565         GFile *gfile;
566         GFileInfo *info;
567         guint64 size;
568         gchar *resolved = ghb_resolve_symlink(destdir);
569
570         gfile = g_file_new_for_path(resolved);
571         info = g_file_query_filesystem_info(gfile, 
572                                                 G_FILE_ATTRIBUTE_FILESYSTEM_FREE, NULL, NULL);
573         if (info != NULL)
574         {
575                 if (g_file_info_has_attribute(info, G_FILE_ATTRIBUTE_FILESYSTEM_FREE))
576                 {
577                         size = g_file_info_get_attribute_uint64(info, 
578                                                                         G_FILE_ATTRIBUTE_FILESYSTEM_FREE);
579                         
580                         gint64 fsize = (guint64)10 * 1024 * 1024 * 1024;
581                         if (size < fsize)
582                         {
583                                 message = g_strdup_printf(
584                                                         "Destination filesystem is almost full: %uM free\n\n"
585                                                         "Encode may be incomplete if you proceed.\n",
586                                                         (guint)(size / (1024L*1024L)));
587                                 if (!ghb_message_dialog(GTK_MESSAGE_QUESTION, message, "Cancel", "Proceed"))
588                                 {
589                                         g_free(dest);
590                                         g_free(message);
591                                         return FALSE;
592                                 }
593                                 g_free(message);
594                         }
595                 }
596                 g_object_unref(info);
597         }
598         g_object_unref(gfile);
599         g_free(resolved);
600         g_free(destdir);
601         if (g_file_test(dest, G_FILE_TEST_EXISTS))
602         {
603                 message = g_strdup_printf(
604                                         "Destination: %s\n\n"
605                                         "File already exists.\n"
606                                         "Do you want to overwrite?",
607                                         dest);
608                 if (!ghb_message_dialog(GTK_MESSAGE_QUESTION, message, "Cancel", "Overwrite"))
609                 {
610                         g_free(dest);
611                         g_free(message);
612                         return FALSE;
613                 }
614                 g_free(message);
615                 g_unlink(dest);
616         }
617         g_free(dest);
618         // Validate video quality is in a reasonable range
619         if (!ghb_validate_vquality(ud->settings))
620         {
621                 return FALSE;
622         }
623         // Validate audio settings
624         if (!ghb_validate_audio(ud))
625         {
626                 return FALSE;
627         }
628         // Validate video settings
629         if (!ghb_validate_video(ud))
630         {
631                 return FALSE;
632         }
633         // Validate filter settings
634         if (!ghb_validate_filters(ud))
635         {
636                 return FALSE;
637         }
638         audio_list_refresh(ud);
639         return TRUE;
640 }
641
642 static gboolean
643 queue_add(signal_user_data_t *ud)
644 {
645         // Add settings to the queue
646         GValue *settings;
647         gint titleindex;
648         gint titlenum;
649         gint sub;
650         
651         g_debug("queue_add ()");
652         if (!validate_settings(ud))
653         {
654                 return FALSE;
655         }
656
657         GtkStatusIcon *si;
658
659         si = GTK_STATUS_ICON(GHB_OBJECT(ud->builder, "hb_status"));
660         gtk_status_icon_set_from_icon_name(si, "hb-status");
661
662         if (ud->queue == NULL)
663                 ud->queue = ghb_array_value_new(32);
664         // Make a copy of current settings to be used for the new job
665         settings = ghb_value_dup(ud->settings);
666         sub = ghb_settings_combo_int(settings, "Subtitles");
667         ghb_settings_set_int(settings, "subtitle_index", sub);
668         
669         ghb_settings_set_int(settings, "job_status", GHB_QUEUE_PENDING);
670         ghb_settings_set_int(settings, "job_unique_id", 0);
671         titleindex = ghb_settings_combo_int(settings, "title");
672         titlenum = ghb_get_title_number(titleindex);
673         ghb_settings_set_int(settings, "titlenum", titlenum);
674         ghb_array_append(ud->queue, settings);
675         add_to_queue_list(ud, settings, NULL);
676         ghb_save_queue(ud->queue);
677
678         return TRUE;
679 }
680
681 G_MODULE_EXPORT void
682 queue_add_clicked_cb(GtkWidget *widget, signal_user_data_t *ud)
683 {
684         g_debug("queue_add_clicked_cb ()");
685         queue_add(ud);
686 }
687
688 G_MODULE_EXPORT void
689 queue_remove_clicked_cb(GtkWidget *widget, gchar *path, signal_user_data_t *ud)
690 {
691         GtkTreeView *treeview;
692         GtkTreePath *treepath;
693         GtkTreeModel *store;
694         GtkTreeIter iter;
695         gint row;
696         gint *indices;
697         gint unique_id;
698         GValue *settings;
699         gint status;
700
701         g_debug("queue_remove_clicked_cb ()");
702         treeview = GTK_TREE_VIEW(GHB_WIDGET(ud->builder, "queue_list"));
703         store = gtk_tree_view_get_model(treeview);
704         treepath = gtk_tree_path_new_from_string (path);
705         if (gtk_tree_path_get_depth(treepath) > 1) return;
706         if (gtk_tree_model_get_iter(store, &iter, treepath))
707         {
708                 // Find the entry in the queue
709                 indices = gtk_tree_path_get_indices (treepath);
710                 row = indices[0];
711                 // Can only free the treepath After getting what I need from
712                 // indices since this points into treepath somewhere.
713                 gtk_tree_path_free (treepath);
714                 if (row < 0) return;
715                 if (row >= ghb_array_len(ud->queue))
716                         return;
717                 settings = ghb_array_get_nth(ud->queue, row);
718                 status = ghb_settings_get_int(settings, "job_status");
719                 if (status == GHB_QUEUE_RUNNING)
720                 {
721                         // Ask if wants to stop encode.
722                         if (!ghb_cancel_encode(NULL))
723                         {
724                                 return;
725                         }
726                         unique_id = ghb_settings_get_int(settings, "job_unique_id");
727                         ghb_remove_job(unique_id);
728                 }
729                 // Remove the selected item
730                 gtk_tree_store_remove(GTK_TREE_STORE(store), &iter);
731                 // Remove the corresponding item from the queue list
732                 GValue *old = ghb_array_get_nth(ud->queue, row);
733                 ghb_value_free(old);
734                 ghb_array_remove(ud->queue, row);
735                 ghb_save_queue(ud->queue);
736         }
737         else
738         {       
739                 gtk_tree_path_free (treepath);
740         }
741 }
742
743 static gint
744 find_last_finished(GValue *queue)
745 {
746         GValue *js;
747         gint ii, count;
748         gint status;
749         
750         g_debug("find_last_finished");
751         count = ghb_array_len(queue);
752         for (ii = 0; ii < count; ii++)
753         {
754                 js = ghb_array_get_nth(queue, ii);
755                 status = ghb_settings_get_int(js, "job_status");
756                 if (status != GHB_QUEUE_DONE && status != GHB_QUEUE_RUNNING)
757                 {
758                         return ii-1;
759                 }
760         }
761         return -1;
762 }
763
764 // This little bit is needed to prevent the default drag motion
765 // handler from expanding rows if you hover over them while
766 // dragging.
767 // Also controls where valid drop locations are
768 G_MODULE_EXPORT gboolean
769 queue_drag_motion_cb(
770         GtkTreeView *tv,
771         GdkDragContext *ctx,
772         gint x,
773         gint y,
774         guint time,
775         signal_user_data_t *ud)
776 {
777         GtkTreePath *path = NULL;
778         GtkTreeViewDropPosition pos;
779         gint *indices, row, status, finished;
780         GValue *js;
781         GtkTreeIter iter;
782         GtkTreeView *srctv;
783         GtkTreeModel *model;
784         GtkTreeSelection *select;
785         GtkWidget *widget;
786
787         widget = gtk_drag_get_source_widget(ctx);
788         if (widget == NULL || widget != GTK_WIDGET(tv))
789                 return TRUE;
790
791         // This bit checks to see if the source is allowed to be
792         // moved.  Only pending and canceled items may be moved.
793         srctv = GTK_TREE_VIEW(gtk_drag_get_source_widget(ctx));
794         select = gtk_tree_view_get_selection (srctv);
795         gtk_tree_selection_get_selected (select, &model, &iter);
796         path = gtk_tree_model_get_path (model, &iter);
797         indices = gtk_tree_path_get_indices(path);
798         row = indices[0];
799         gtk_tree_path_free(path);
800         js = ghb_array_get_nth(ud->queue, row);
801         status = ghb_settings_get_int(js, "job_status");
802         if (status != GHB_QUEUE_PENDING && status != GHB_QUEUE_CANCELED)
803         {
804                 gdk_drag_status(ctx, 0, time);
805                 return TRUE;
806         }
807
808         // The reset checks that the destination is a valid position
809         // in the list.  Can not move above any finished or running items
810         gtk_tree_view_get_dest_row_at_pos (tv, x, y, &path, &pos);
811         if (path == NULL)
812         {
813                 gdk_drag_status(ctx, GDK_ACTION_MOVE, time);
814                 return TRUE;
815         }
816         // Don't allow *drop into*
817         if (pos == GTK_TREE_VIEW_DROP_INTO_OR_BEFORE)
818                 pos = GTK_TREE_VIEW_DROP_BEFORE;
819         if (pos == GTK_TREE_VIEW_DROP_INTO_OR_AFTER)
820                 pos = GTK_TREE_VIEW_DROP_AFTER;
821         // Don't allow droping int child items
822         if (gtk_tree_path_get_depth(path) > 1)
823         {
824                 gtk_tree_path_up(path);
825                 pos = GTK_TREE_VIEW_DROP_AFTER;
826         }
827         indices = gtk_tree_path_get_indices(path);
828         row = indices[0];
829         js = ghb_array_get_nth(ud->queue, row);
830
831         finished = find_last_finished(ud->queue);
832         if (row < finished)
833         {
834                 gtk_tree_path_free(path);
835                 gdk_drag_status(ctx, 0, time);
836                 return TRUE;
837         }
838         if (pos != GTK_TREE_VIEW_DROP_AFTER && 
839                 row == finished)
840         {
841                 gtk_tree_path_free(path);
842                 gdk_drag_status(ctx, 0, time);
843                 return TRUE;
844         }
845         gtk_tree_view_set_drag_dest_row(tv, path, pos);
846         gtk_tree_path_free(path);
847         gdk_drag_status(ctx, GDK_ACTION_MOVE, time);
848         return TRUE;
849 }
850
851 G_MODULE_EXPORT void 
852 queue_drag_cb(
853         GtkTreeView *dstwidget, 
854         GdkDragContext *dc, 
855         gint x, gint y, 
856         GtkSelectionData *selection_data, 
857         guint info, guint t, 
858         signal_user_data_t *ud)
859 {
860         GtkTreePath *path = NULL;
861         //GtkTreeModel *model;
862         GtkTreeViewDropPosition pos;
863         GtkTreeIter dstiter, srciter;
864         gint *indices, row;
865         GValue *js;
866         
867         GtkTreeModel *dstmodel = gtk_tree_view_get_model(dstwidget);
868                         
869         g_debug("queue_drag_cb ()");
870         // This doesn't work here for some reason...
871         // gtk_tree_view_get_drag_dest_row(dstwidget, &path, &pos);
872         gtk_tree_view_get_dest_row_at_pos (dstwidget, x, y, &path, &pos);
873         // This little hack is needed because attempting to drop after
874         // the last item gives us no path or pos.
875         if (path == NULL)
876         {
877                 gint n_children;
878
879                 n_children = gtk_tree_model_iter_n_children(dstmodel, NULL);
880                 if (n_children)
881                 {
882                         pos = GTK_TREE_VIEW_DROP_AFTER;
883                         path = gtk_tree_path_new_from_indices(n_children-1, -1);
884                 }
885                 else
886                 {
887                         pos = GTK_TREE_VIEW_DROP_BEFORE;
888                         path = gtk_tree_path_new_from_indices(0, -1);
889                 }
890         }
891         if (path)
892         {
893                 if (gtk_tree_path_get_depth(path) > 1)
894                         gtk_tree_path_up(path);
895                 if (gtk_tree_model_get_iter (dstmodel, &dstiter, path))
896                 {
897                         GtkTreeIter iter;
898                         GtkTreeView *srcwidget;
899                         GtkTreeModel *srcmodel;
900                         GtkTreeSelection *select;
901                         GtkTreePath *srcpath = NULL;
902                         GtkTreePath *dstpath = NULL;
903
904                         srcwidget = GTK_TREE_VIEW(gtk_drag_get_source_widget(dc));
905                         //srcmodel = gtk_tree_view_get_model(srcwidget);
906                         select = gtk_tree_view_get_selection (srcwidget);
907                         gtk_tree_selection_get_selected (select, &srcmodel, &srciter);
908
909                         srcpath = gtk_tree_model_get_path (srcmodel, &srciter);
910                         indices = gtk_tree_path_get_indices(srcpath);
911                         row = indices[0];
912                         gtk_tree_path_free(srcpath);
913                         js = ghb_array_get_nth(ud->queue, row);
914
915                         switch (pos)
916                         {
917                                 case GTK_TREE_VIEW_DROP_BEFORE:
918                                 case GTK_TREE_VIEW_DROP_INTO_OR_BEFORE:
919                                         gtk_tree_store_insert_before (GTK_TREE_STORE (dstmodel), 
920                                                                                                         &iter, NULL, &dstiter);
921                                         break;
922
923                                 case GTK_TREE_VIEW_DROP_AFTER:
924                                 case GTK_TREE_VIEW_DROP_INTO_OR_AFTER:
925                                         gtk_tree_store_insert_after (GTK_TREE_STORE (dstmodel), 
926                                                                                                         &iter, NULL, &dstiter);
927                                         break;
928
929                                 default:
930                                         break;
931                         }
932                         // Reset job to pending
933                         ghb_settings_set_int(js, "job_status", GHB_QUEUE_PENDING);
934                         add_to_queue_list(ud, js, &iter);
935
936                         dstpath = gtk_tree_model_get_path (dstmodel, &iter);
937                         indices = gtk_tree_path_get_indices(dstpath);
938                         row = indices[0];
939                         gtk_tree_path_free(dstpath);
940                         ghb_array_insert(ud->queue, row, js);
941
942                         srcpath = gtk_tree_model_get_path (srcmodel, &srciter);
943                         indices = gtk_tree_path_get_indices(srcpath);
944                         row = indices[0];
945                         gtk_tree_path_free(srcpath);
946                         ghb_array_remove(ud->queue, row);
947                         gtk_tree_store_remove (GTK_TREE_STORE (srcmodel), &srciter);
948                         ghb_save_queue(ud->queue);
949                 }
950                 gtk_tree_path_free(path);
951         }
952 }
953
954 void
955 ghb_queue_buttons_grey(signal_user_data_t *ud, gboolean working)
956 {
957         GtkWidget *widget;
958         GtkAction *action;
959         gint queue_count;
960         gint titleindex;
961         gboolean title_ok;
962
963         queue_count = ghb_array_len(ud->queue);
964         titleindex = ghb_settings_combo_int(ud->settings, "title");
965         title_ok = (titleindex >= 0);
966
967         widget = GHB_WIDGET (ud->builder, "queue_start1");
968         gtk_widget_set_sensitive (widget, !working && (title_ok || queue_count));
969         widget = GHB_WIDGET (ud->builder, "queue_start2");
970         gtk_widget_set_sensitive (widget, !working && (title_ok || queue_count));
971         action = GHB_ACTION (ud->builder, "queue_start_menu");
972         gtk_action_set_sensitive (action, !working && (title_ok || queue_count));
973         widget = GHB_WIDGET (ud->builder, "queue_pause1");
974         gtk_widget_set_sensitive (widget, working);
975         widget = GHB_WIDGET (ud->builder, "queue_pause2");
976         gtk_widget_set_sensitive (widget, working);
977         action = GHB_ACTION (ud->builder, "queue_pause_menu");
978         gtk_action_set_sensitive (action, working);
979         widget = GHB_WIDGET (ud->builder, "queue_stop");
980         gtk_widget_set_sensitive (widget, working);
981         action = GHB_ACTION (ud->builder, "queue_stop_menu");
982         gtk_action_set_sensitive (action, working);
983 }
984
985 G_MODULE_EXPORT void
986 queue_list_size_allocate_cb(GtkWidget *widget, GtkAllocation *allocation, GtkCellRenderer *cell)
987 {
988         GtkTreeViewColumn *column;
989         gint width;
990         
991         column = gtk_tree_view_get_column (GTK_TREE_VIEW(widget), 0);
992         width = gtk_tree_view_column_get_width(column);
993         g_debug("col width %d alloc width %d", width, allocation->width);
994         // Set new wrap-width.  Shave a little off to accomidate the icons
995         // that share this column.
996         if (width >= 564) // Don't allow below a certain size
997                 g_object_set(cell, "wrap-width", width-70, NULL);
998 }
999
1000 G_MODULE_EXPORT void
1001 queue_start_clicked_cb(GtkWidget *xwidget, signal_user_data_t *ud)
1002 {
1003         GValue *js;
1004         gboolean running = FALSE;
1005         gint count, ii;
1006         gint status;
1007         gint state;
1008
1009         count = ghb_array_len(ud->queue);
1010         for (ii = 0; ii < count; ii++)
1011         {
1012                 js = ghb_array_get_nth(ud->queue, ii);
1013                 status = ghb_settings_get_int(js, "job_status");
1014                 if ((status == GHB_QUEUE_RUNNING) || 
1015                         (status == GHB_QUEUE_PENDING))
1016                 {
1017                         running = TRUE;
1018                         break;
1019                 }
1020         }
1021         if (!running)
1022         {
1023                 // The queue has no running or pending jobs.
1024                 // Add current settings to the queue, then run.
1025                 if (!queue_add(ud))
1026                         return;
1027         }
1028         state = ghb_get_queue_state();
1029         if (state == GHB_STATE_IDLE)
1030         {
1031                 // Add the first pending queue item and start
1032                 ud->current_job = ghb_start_next_job(ud, TRUE);
1033         }
1034 }
1035
1036 G_MODULE_EXPORT void
1037 queue_stop_clicked_cb(GtkWidget *xwidget, signal_user_data_t *ud)
1038 {
1039         ud->cancel_encode = TRUE;
1040         ghb_cancel_encode(NULL);
1041 }
1042
1043 G_MODULE_EXPORT void
1044 queue_pause_clicked_cb(GtkWidget *xwidget, signal_user_data_t *ud)
1045 {
1046         ghb_pause_queue();
1047 }
1048
1049 gboolean
1050 ghb_reload_queue(signal_user_data_t *ud)
1051 {
1052         GValue *queue;
1053         gint unfinished = 0;
1054         gint count, ii;
1055         gint status;
1056         GValue *settings;
1057         gchar *message;
1058
1059         g_debug("ghb_reload_queue");
1060
1061         queue = ghb_load_queue();
1062         // Look for unfinished entries
1063         count = ghb_array_len(queue);
1064         for (ii = 0; ii < count; ii++)
1065         {
1066                 settings = ghb_array_get_nth(queue, ii);
1067                 status = ghb_settings_get_int(settings, "job_status");
1068                 if (status != GHB_QUEUE_DONE && status != GHB_QUEUE_CANCELED)
1069                 {
1070                         unfinished++;
1071                 }
1072         }
1073         if (unfinished)
1074         {
1075                 message = g_strdup_printf(
1076                                         "You have %d unfinished job%s in a saved queue.\n\n"
1077                                         "Would you like to reload %s?",
1078                                         unfinished, 
1079                                         (unfinished > 1) ? "s" : "",
1080                                         (unfinished > 1) ? "them" : "it");
1081                 if (ghb_message_dialog(GTK_MESSAGE_QUESTION, message, "No", "Yes"))
1082                 {
1083                         GtkWidget *widget = GHB_WIDGET (ud->builder, "queue_window");
1084                         gtk_widget_show (widget);
1085
1086                         ud->queue = queue;
1087                         // First get rid of any old items we don't want
1088                         for (ii = count-1; ii >= 0; ii--)
1089                         {
1090                                 settings = ghb_array_get_nth(queue, ii);
1091                                 status = ghb_settings_get_int(settings, "job_status");
1092                                 if (status == GHB_QUEUE_DONE || status == GHB_QUEUE_CANCELED)
1093                                 {
1094                                         GValue *old = ghb_array_get_nth(queue, ii);
1095                                         ghb_value_free(old);
1096                                         ghb_array_remove(queue, ii);
1097                                 }
1098                         }
1099                         count = ghb_array_len(queue);
1100                         for (ii = 0; ii < count; ii++)
1101                         {
1102                                 settings = ghb_array_get_nth(queue, ii);
1103                                 ghb_settings_set_int(settings, "job_unique_id", 0);
1104                                 ghb_settings_set_int(settings, "job_status", GHB_QUEUE_PENDING);
1105                                 add_to_queue_list(ud, settings, NULL);
1106                         }
1107                         ghb_queue_buttons_grey(ud, FALSE);
1108                 }
1109                 else
1110                 {
1111                         ghb_value_free(queue);
1112                         ghb_remove_queue_file();
1113                 }
1114                 g_free(message);
1115         }
1116         return FALSE;
1117 }
1118
1119 G_MODULE_EXPORT gboolean 
1120 queue_key_press_cb(
1121         GtkWidget *widget, 
1122         GdkEventKey *event,
1123         signal_user_data_t *ud)
1124 {
1125         GtkTreeView *treeview;
1126         GtkTreeSelection *selection;
1127         GtkTreeModel *store;
1128         GtkTreeIter iter;
1129         gint row;
1130         gint *indices;
1131         gint unique_id;
1132         GValue *settings;
1133         gint status;
1134
1135         g_debug("queue_key_press_cb ()");
1136         if (event->keyval != GDK_Delete)
1137                 return FALSE;
1138         treeview = GTK_TREE_VIEW(GHB_WIDGET(ud->builder, "queue_list"));
1139         store = gtk_tree_view_get_model(treeview);
1140
1141         selection = gtk_tree_view_get_selection (treeview);
1142         if (gtk_tree_selection_get_selected(selection, &store, &iter))
1143         {
1144                 GtkTreePath *treepath;
1145
1146                 treepath = gtk_tree_model_get_path (store, &iter);
1147                 // Find the entry in the queue
1148                 indices = gtk_tree_path_get_indices (treepath);
1149                 row = indices[0];
1150                 // Can only free the treepath After getting what I need from
1151                 // indices since this points into treepath somewhere.
1152                 gtk_tree_path_free (treepath);
1153                 if (row < 0) return FALSE;
1154                 if (row >= ghb_array_len(ud->queue))
1155                         return FALSE;
1156                 settings = ghb_array_get_nth(ud->queue, row);
1157                 status = ghb_settings_get_int(settings, "job_status");
1158                 if (status == GHB_QUEUE_RUNNING)
1159                 {
1160                         // Ask if wants to stop encode.
1161                         if (!ghb_cancel_encode(NULL))
1162                         {
1163                                 return TRUE;
1164                         }
1165                         unique_id = ghb_settings_get_int(settings, "job_unique_id");
1166                         ghb_remove_job(unique_id);
1167                 }
1168                 // Remove the selected item
1169                 gtk_tree_store_remove(GTK_TREE_STORE(store), &iter);
1170                 // Remove the corresponding item from the queue list
1171                 GValue *old = ghb_array_get_nth(ud->queue, row);
1172                 ghb_value_free(old);
1173                 ghb_array_remove(ud->queue, row);
1174                 ghb_save_queue(ud->queue);
1175                 return TRUE;
1176         }
1177         return FALSE;
1178 }
1179
1180 GValue *ghb_queue_edit_settings = NULL;
1181
1182 G_MODULE_EXPORT void
1183 queue_edit_clicked_cb(GtkWidget *xwidget, signal_user_data_t *ud)
1184 {
1185         GtkTreeView *treeview;
1186         GtkTreeSelection *selection;
1187         GtkTreeModel *store;
1188         GtkTreeIter iter;
1189         gint row;
1190         gint *indices;
1191         gint status;
1192
1193         g_debug("queue_key_press_cb ()");
1194         treeview = GTK_TREE_VIEW(GHB_WIDGET(ud->builder, "queue_list"));
1195         store = gtk_tree_view_get_model(treeview);
1196
1197         selection = gtk_tree_view_get_selection (treeview);
1198         if (gtk_tree_selection_get_selected(selection, &store, &iter))
1199         {
1200                 GtkTreePath *treepath;
1201
1202                 treepath = gtk_tree_model_get_path (store, &iter);
1203                 // Find the entry in the queue
1204                 indices = gtk_tree_path_get_indices (treepath);
1205                 row = indices[0];
1206                 // Can only free the treepath After getting what I need from
1207                 // indices since this points into treepath somewhere.
1208                 gtk_tree_path_free (treepath);
1209                 if (row < 0) return;
1210                 if (row >= ghb_array_len(ud->queue))
1211                         return;
1212                 ghb_queue_edit_settings = ghb_array_get_nth(ud->queue, row);
1213                 status = ghb_settings_get_int(ghb_queue_edit_settings, "job_status");
1214                 if (status == GHB_QUEUE_PENDING)
1215                 {
1216                         // Remove the selected item
1217                         gtk_tree_store_remove(GTK_TREE_STORE(store), &iter);
1218                         // Remove the corresponding item from the queue list
1219                         ghb_array_remove(ud->queue, row);
1220                 }
1221                 gchar *source;
1222                 source = ghb_settings_get_string(ghb_queue_edit_settings, "source");
1223                 ghb_do_scan(ud, source, 0, FALSE);
1224                 g_free(source);
1225         }
1226 }
1227