OSDN Git Service

LinGui: missed changing usage of the shutdown function
[handbrake-jp/handbrake-jp-git.git] / gtk / src / callbacks.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 #ifdef HAVE_CONFIG_H
15 #  include <config.h>
16 #endif
17
18 #include <string.h>
19 #include <fcntl.h>
20 #include <sys/stat.h>
21 #include <time.h>
22
23 #if !defined(_WIN32)
24 #include <poll.h>
25 #include <libhal-storage.h>
26 #include <dbus/dbus-glib.h>
27 #include <dbus/dbus-glib-lowlevel.h>
28
29 #include <netinet/in.h>
30 #include <netdb.h>
31 #if defined(_OLD_WEBKIT)
32 #include <webkit.h>
33 #else
34 #include <webkit/webkit.h>
35 #endif
36 #include <libnotify/notify.h>
37 #include <gdk/gdkx.h>
38 #else
39 #define WINVER 0x0500
40 #include <winsock2.h>
41 #include <dbt.h>
42 #endif
43
44 #include <gtk/gtk.h>
45 #include <gdk/gdkkeysyms.h>
46 #include <glib/gstdio.h>
47 #include <gio/gio.h>
48
49 #include "hb.h"
50 #include "callbacks.h"
51 #include "queuehandler.h"
52 #include "audiohandler.h"
53 #include "subtitlehandler.h"
54 #include "resources.h"
55 #include "settings.h"
56 #include "presets.h"
57 #include "preview.h"
58 #include "values.h"
59 #include "plist.h"
60 #include "appcast.h"
61 #include "hb-backend.h"
62 #include "ghb-dvd.h"
63 #include "ghbcellrenderertext.h"
64
65 static void update_chapter_list(signal_user_data_t *ud);
66 static GList* dvd_device_list();
67 static void prune_logs(signal_user_data_t *ud);
68 void ghb_notify_done(signal_user_data_t *ud);
69 gpointer ghb_check_update(signal_user_data_t *ud);
70 static gboolean ghb_can_shutdown_gsm();
71 static void ghb_shutdown_gsm();
72 static gboolean ghb_can_suspend_gpm();
73 static void ghb_suspend_gpm();
74 static gboolean appcast_busy = FALSE;
75
76 // This is a dependency map used for greying widgets
77 // that are dependent on the state of another widget.
78 // The enable_value comes from the values that are
79 // obtained from ghb_widget_value().  For combo boxes
80 // you will have to look further to combo box options
81 // maps in hb-backend.c
82
83 GValue *dep_map;
84 GValue *rev_map;
85
86 void
87 ghb_init_dep_map()
88 {
89         dep_map = ghb_resource_get("widget-deps");
90         rev_map = ghb_resource_get("widget-reverse-deps");
91 }
92
93 static gboolean
94 dep_check(signal_user_data_t *ud, const gchar *name, gboolean *out_hide)
95 {
96         GtkWidget *widget;
97         GObject *dep_object;
98         gint ii;
99         gint count;
100         gboolean result = TRUE;
101         GValue *array, *data;
102         gchar *widget_name;
103         
104         g_debug("dep_check () %s", name);
105
106         if (rev_map == NULL) return TRUE;
107         array = ghb_dict_lookup(rev_map, name);
108         count = ghb_array_len(array);
109         *out_hide = FALSE;
110         for (ii = 0; ii < count; ii++)
111         {
112                 data = ghb_array_get_nth(array, ii);
113                 widget_name = ghb_value_string(ghb_array_get_nth(data, 0));
114                 widget = GHB_WIDGET(ud->builder, widget_name);
115                 dep_object = gtk_builder_get_object(ud->builder, name);
116                 g_free(widget_name);
117                 if (!GTK_WIDGET_SENSITIVE(widget))
118                         continue;
119                 if (dep_object == NULL)
120                 {
121                         g_message("Failed to find widget");
122                 }
123                 else
124                 {
125                         gchar *value;
126                         gint jj = 0;
127                         gchar **values;
128                         gboolean sensitive = FALSE;
129                         gboolean die, hide;
130
131                         die = ghb_value_boolean(ghb_array_get_nth(data, 2));
132                         hide = ghb_value_boolean(ghb_array_get_nth(data, 3));
133                         value = ghb_value_string(ghb_array_get_nth(data, 1));
134                         values = g_strsplit(value, "|", 10);
135                         g_free(value);
136
137                         if (widget)
138                                 value = ghb_widget_string(widget);
139                         else
140                                 value = ghb_settings_get_string(ud->settings, name);
141                         while (values && values[jj])
142                         {
143                                 if (values[jj][0] == '>')
144                                 {
145                                         gdouble dbl = g_strtod (&values[jj][1], NULL);
146                                         gdouble dvalue = ghb_widget_double(widget);
147                                         if (dvalue > dbl)
148                                         {
149                                                 sensitive = TRUE;
150                                                 break;
151                                         }
152                                 }
153                                 else if (values[jj][0] == '<')
154                                 {
155                                         gdouble dbl = g_strtod (&values[jj][1], NULL);
156                                         gdouble dvalue = ghb_widget_double(widget);
157                                         if (dvalue < dbl)
158                                         {
159                                                 sensitive = TRUE;
160                                                 break;
161                                         }
162                                 }
163                                 if (strcmp(values[jj], value) == 0)
164                                 {
165                                         sensitive = TRUE;
166                                         break;
167                                 }
168                                 jj++;
169                         }
170                         sensitive = die ^ sensitive;
171                         if (!sensitive)
172                         {
173                                 result = FALSE;
174                                 *out_hide |= hide;
175                         }
176                         g_strfreev (values);
177                         g_free(value);
178                 }
179         }
180         return result;
181 }
182
183 void
184 ghb_check_dependency(signal_user_data_t *ud, GtkWidget *widget)
185 {
186         GObject *dep_object;
187         const gchar *name;
188         GValue *array, *data;
189         gint count, ii;
190         gchar *dep_name;
191         GType type;
192
193         type = GTK_WIDGET_TYPE(widget);
194         if (type == GTK_TYPE_COMBO_BOX || type == GTK_TYPE_COMBO_BOX_ENTRY)
195                 if (gtk_combo_box_get_active(GTK_COMBO_BOX(widget)) < 0) return;
196
197         name = gtk_widget_get_name(widget);
198         g_debug("ghb_check_dependency () %s", name);
199
200         if (dep_map == NULL) return;
201         array = ghb_dict_lookup(dep_map, name);
202         count = ghb_array_len(array);
203         for (ii = 0; ii < count; ii++)
204         {
205                 gboolean sensitive;
206                 gboolean hide;
207
208                 data = ghb_array_get_nth(array, ii);
209                 dep_name = ghb_value_string(data);
210                 dep_object = gtk_builder_get_object(ud->builder, dep_name);
211                 if (dep_object == NULL)
212                 {
213                         g_message("Failed to find dependent widget %s", dep_name);
214                         g_free(dep_name);
215                         continue;
216                 }
217                 sensitive = dep_check(ud, dep_name, &hide);
218                 g_free(dep_name);
219                 if (GTK_IS_ACTION(dep_object))
220                 {
221                         gtk_action_set_sensitive(GTK_ACTION(dep_object), sensitive);
222                         gtk_action_set_visible(GTK_ACTION(dep_object), sensitive || !hide);
223                 }
224                 else
225                 {
226                         gtk_widget_set_sensitive(GTK_WIDGET(dep_object), sensitive);
227                         if (!sensitive && hide)
228                         {
229                                 gtk_widget_hide(GTK_WIDGET(dep_object));
230                         }
231                         else
232                         {
233                                 gtk_widget_show_now(GTK_WIDGET(dep_object));
234                         }
235                 }
236         }
237 }
238
239 void
240 ghb_check_all_depencencies(signal_user_data_t *ud)
241 {
242         GHashTableIter iter;
243         gchar *dep_name;
244         GValue *value;
245         GObject *dep_object;
246
247         g_debug("ghb_check_all_depencencies ()");
248         if (rev_map == NULL) return;
249         ghb_dict_iter_init(&iter, rev_map);
250         // middle (void*) cast prevents gcc warning "defreferencing type-punned
251         // pointer will break strict-aliasing rules"
252         while (g_hash_table_iter_next(
253                         &iter, (gpointer*)(void*)&dep_name, (gpointer*)(void*)&value))
254         {
255                 gboolean sensitive;
256                 gboolean hide;
257
258                 dep_object = gtk_builder_get_object (ud->builder, dep_name);
259                 if (dep_object == NULL)
260                 {
261                         g_message("Failed to find dependent widget %s", dep_name);
262                         continue;
263                 }
264                 sensitive = dep_check(ud, dep_name, &hide);
265                 if (GTK_IS_ACTION(dep_object))
266                 {
267                         gtk_action_set_sensitive(GTK_ACTION(dep_object), sensitive);
268                         gtk_action_set_visible(GTK_ACTION(dep_object), sensitive || !hide);
269                 }
270                 else
271                 {
272                         gtk_widget_set_sensitive(GTK_WIDGET(dep_object), sensitive);
273                         if (!sensitive && hide)
274                         {
275                                 gtk_widget_hide(GTK_WIDGET(dep_object));
276                         }
277                         else
278                         {
279                                 gtk_widget_show_now(GTK_WIDGET(dep_object));
280                         }
281                 }
282         }
283 }
284
285 G_MODULE_EXPORT void
286 on_quit1_activate(GtkMenuItem *quit, signal_user_data_t *ud)
287 {
288         gint state = ghb_get_queue_state();
289         g_debug("on_quit1_activate ()");
290         if (state & GHB_STATE_WORKING)
291         {
292                 if (ghb_cancel_encode2(ud, "Closing HandBrake will terminate encoding.\n"))
293                 {
294                         ghb_hb_cleanup(FALSE);
295                         prune_logs(ud);
296                         gtk_main_quit();
297                         return;
298                 }
299                 return;
300         }
301         ghb_hb_cleanup(FALSE);
302         prune_logs(ud);
303         gtk_main_quit();
304 }
305
306 gboolean
307 uppers_and_unders(gchar *str)
308 {
309         if (str == NULL) return FALSE;
310         str = g_strchomp(g_strchug(str));
311         while (*str)
312         {
313                 if (*str == ' ')
314                 {
315                         return FALSE;
316                 }
317                 if (*str >= 'a' && *str <= 'z')
318                 {
319                         return FALSE;
320                 }
321                 str++;
322         }
323         return TRUE;
324 }
325
326 enum
327 {
328         CAMEL_FIRST_UPPER,
329         CAMEL_OTHER
330 };
331
332 void
333 camel_convert(gchar *str)
334 {
335         gint state = CAMEL_OTHER;
336         
337         if (str == NULL) return;
338         while (*str)
339         {
340                 if (*str == '_') *str = ' ';
341                 switch (state)
342                 {
343                         case CAMEL_OTHER:
344                         {
345                                 if (*str >= 'A' && *str <= 'Z')
346                                         state = CAMEL_FIRST_UPPER;
347                                 else
348                                         state = CAMEL_OTHER;
349                                 
350                         } break;
351                         case CAMEL_FIRST_UPPER:
352                         {
353                                 if (*str >= 'A' && *str <= 'Z')
354                                         *str = *str - 'A' + 'a';
355                                 else
356                                         state = CAMEL_OTHER;
357                         } break;
358                 }
359                 str++;
360         }
361 }
362
363 #if defined(_WIN32)
364 static gchar*
365 get_dvd_device_name(gchar *device)
366 {
367         return g_strdup(device);
368 }
369 #else
370 static gchar*
371 get_dvd_device_name(GDrive *gd)
372 {
373         return g_drive_get_identifier(gd, G_VOLUME_IDENTIFIER_KIND_UNIX_DEVICE);
374 }
375 #endif
376
377 static GHashTable *volname_hash = NULL;
378 static GMutex     *volname_mutex = NULL;
379
380 static void
381 free_volname_key(gpointer data)
382 {
383         if (data != NULL)
384                 g_free(data);
385 }
386
387 static void
388 free_volname_value(gpointer data)
389 {
390         if (data != NULL)
391                 g_free(data);
392 }
393
394 #if defined(_WIN32)
395 static gchar*
396 get_direct_dvd_volume_name(const gchar *drive)
397 {
398         gchar *result = NULL;
399         gchar vname[51], fsname[51];
400
401         if (GetVolumeInformation(drive, vname, 50, NULL, NULL, NULL, fsname, 50))
402         {
403                 result = g_strdup_printf("%s", vname);
404         }
405         return result;
406 }
407 #else
408 static gchar*
409 get_direct_dvd_volume_name(const gchar *drive)
410 {
411         gchar *result;
412
413         result = ghb_dvd_volname (drive);
414         return result;
415 }
416 #endif
417
418 static gchar*
419 get_dvd_volume_name(gpointer gd)
420 {
421         gchar *label = NULL;
422         gchar *result;
423         gchar *drive;
424
425         drive = get_dvd_device_name(gd);
426         g_mutex_lock(volname_mutex);
427         label = g_strdup(g_hash_table_lookup(volname_hash, drive));
428         g_mutex_unlock(volname_mutex);
429         if (label != NULL)
430         {
431                 if (uppers_and_unders(label))
432                 {
433                         camel_convert(label);
434                 }
435 #if defined(_WIN32)
436                 result = g_strdup_printf("%s (%s)", label, drive);
437 #else
438                 result = g_strdup_printf("%s - %s", drive, label);
439 #endif
440                 g_free(label);
441         }
442         else
443         {
444                 result = g_strdup_printf("%s", drive);
445         }
446         g_free(drive);
447         return result;
448 }
449
450 void
451 ghb_volname_cache_init(void)
452 {
453         volname_mutex = g_mutex_new();
454         volname_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
455                                                                                 free_volname_key, free_volname_value);
456 }
457
458 static void
459 free_drive(gpointer drive)
460 {
461 #if defined(_WIN32)
462                 g_free(drive);
463 #else
464                 g_object_unref(drive);
465 #endif
466 }
467
468 gpointer
469 ghb_cache_volnames(signal_user_data_t *ud)
470 {
471         GList *link, *drives;
472
473         g_debug("ghb_cache_volnames()");
474         link = drives = dvd_device_list();
475         if (drives == NULL)
476                 return NULL;
477
478         g_mutex_lock(volname_mutex);
479         g_hash_table_remove_all(volname_hash);
480         while (link != NULL)
481         {
482                 gchar *name, *drive;
483
484 #if !defined(_WIN32)
485                 if (!g_drive_has_media (link->data))
486                 {
487                         g_object_unref(link->data);
488                         link = link->next;
489                         continue;
490                 }
491 #endif
492                 drive = get_dvd_device_name(link->data);
493                 name = get_direct_dvd_volume_name(drive);
494
495                 if (drive != NULL && name != NULL)
496                 {
497                         g_hash_table_insert(volname_hash, drive, name);
498                 }
499                 else
500                 {
501                         if (drive != NULL)
502                                 g_free(drive);
503                         if (name != NULL)
504                                 g_free(name);
505                 }
506         
507                 free_drive(link->data);
508                 link = link->next;
509         }
510         g_mutex_unlock(volname_mutex);
511
512         g_list_free(drives);
513
514         g_idle_add((GSourceFunc)ghb_file_menu_add_dvd, ud);
515
516         return NULL;
517 }
518
519 static const gchar*
520 get_extension(signal_user_data_t *ud)
521 {
522         int container;
523         const gchar *extension = "error";
524         GValue *audio_list;
525
526         container = ghb_settings_combo_int(ud->settings, "FileFormat");
527         if (container == HB_MUX_MP4)
528         {
529                 extension = "mp4";
530                 audio_list = ghb_settings_get_value(ud->settings, "audio_list");
531                 if (ghb_ac3_in_audio_list (audio_list))
532                 {
533                         extension = "m4v";
534                 }
535                 else if (ghb_settings_get_boolean(ud->settings, "ChapterMarkers"))
536                 {
537                         extension = "m4v";
538                 }
539                 else if (ghb_settings_get_boolean(ud->settings, "UseM4v"))
540                 {
541                         extension = "m4v";
542                 }
543         }
544         else if (container == HB_MUX_MKV)
545         {
546                 extension = "mkv";
547         }
548         return extension;
549 }
550
551 static void
552 set_destination(signal_user_data_t *ud)
553 {
554         g_debug("set_destination");
555         if (ghb_settings_get_boolean(ud->settings, "use_source_name"))
556         {
557                 GString *str = g_string_new("");
558                 gchar *vol_name, *filename;
559                 const gchar *extension;
560                 gchar *new_name;
561                 gint title;
562                 
563                 filename = ghb_settings_get_string(ud->settings, "dest_file");
564                 extension = get_extension(ud);
565                 vol_name = ghb_settings_get_string(ud->settings, "volume_label");
566                 g_string_append_printf(str, "%s", vol_name);
567                 title = ghb_settings_combo_int(ud->settings, "title");
568                 if (title >= 0)
569                 {
570                         if (ghb_settings_get_boolean(
571                                         ud->settings, "title_no_in_destination"))
572                         {
573
574                                 title = ghb_settings_combo_int(ud->settings, "title");
575                                 g_string_append_printf(str, " - %d", title+1);
576                         }
577                         if (ghb_settings_get_boolean(
578                                         ud->settings, "chapters_in_destination"))
579                         {
580                                 gint start, end;
581
582                                 if (!ghb_settings_get_boolean(
583                                                 ud->settings, "title_no_in_destination"))
584                                 {
585                                         g_string_append_printf(str, " -");
586                                 }
587                                 start = ghb_settings_get_int(ud->settings, "start_chapter");
588                                 end = ghb_settings_get_int(ud->settings, "end_chapter");
589                                 if (start == end)
590                                         g_string_append_printf(str, " Ch %d", start);
591                                 else
592                                         g_string_append_printf(str, " Ch %d-%d", start, end);
593                         }
594                 }
595                 g_string_append_printf(str, ".%s", extension);
596                 new_name = g_string_free(str, FALSE);
597                 ghb_ui_update(ud, "dest_file", ghb_string_value(new_name));
598                 g_free(filename);
599                 g_free(vol_name);
600                 g_free(new_name);
601         }
602 }
603
604 static gchar*
605 get_file_label(const gchar *filename)
606 {
607         gchar *base, *pos, *end;
608
609         base = g_path_get_basename(filename);
610         pos = strrchr(base, '.');
611         if (pos != NULL)
612         {
613                 // If the last '.' is within 4 chars of end of name, assume
614                 // there is an extension we want to strip.
615                 end = &base[strlen(base) - 1];
616                 if (end - pos <= 4)
617                         *pos = 0;
618         }
619         return base;
620 }
621
622 static gchar*
623 resolve_drive_name(gchar *filename)
624 {
625 #if defined(_WIN32)
626         if (filename[1] == ':')
627         {
628                 gchar drive[4];
629                 gchar *name;
630                 gint dtype;
631
632                 g_strlcpy(drive, filename, 4);
633                 dtype = GetDriveType(drive);
634                 if (dtype == DRIVE_CDROM)
635                 {
636                         gchar vname[51], fsname[51];
637                         GetVolumeInformation(drive, vname, 50, NULL, 
638                                                                 NULL, NULL, fsname, 50);
639                         name = g_strdup(vname);
640                         return name;
641                 }
642         }
643         return NULL;
644 #else
645         return NULL;
646 #endif
647 }
648
649 static gboolean
650 update_source_label(signal_user_data_t *ud, const gchar *source, gboolean update_dest)
651 {
652         gchar *label = NULL;
653         gint len;
654         gchar **path;
655         gchar *start;
656         gchar *filename = g_strdup(source);
657         
658         g_debug("update_source_label()");
659         len = strlen(filename);
660         if (g_file_test(filename, G_FILE_TEST_IS_DIR))
661         {
662                 // Skip dos drive letters
663                 start = strchr(filename, ':');
664                 label = resolve_drive_name(filename);
665                 if (label != NULL)
666                 {
667                         if (uppers_and_unders(label))
668                         {
669                                 camel_convert(label);
670                         }
671                 }
672                 else
673                 {
674                         if (filename[len-1] == G_DIR_SEPARATOR) filename[len-1] = 0;
675                         if (start != NULL)
676                                 start++;
677                         else
678                                 start = filename;
679                         
680                         path = g_strsplit(start, G_DIR_SEPARATOR_S, -1);
681                         len = g_strv_length (path);
682                         if ((len > 1) && (strcmp("VIDEO_TS", path[len-1]) == 0))
683                         {
684                                 label = g_strdup(path[len-2]);
685                                 if (uppers_and_unders(label))
686                                 {
687                                         camel_convert(label);
688                                 }
689                         }
690                         else if (len > 0)
691                         {
692                                 if (path[len-1][0] != 0)
693                                 {
694                                         label = g_strdup(path[len-1]);
695                                         if (uppers_and_unders(label))
696                                         {
697                                                 camel_convert(label);
698                                         }
699                                 }
700                                 else
701                                         label = g_strdup("new_video");
702                         }
703                         else
704                                 label = g_strdup("new_video");
705                         g_strfreev (path);
706                 }
707         }
708         else
709         {
710                 // Is regular file or block dev.
711                 // Check to see if it is a dvd image
712                 label = ghb_dvd_volname (filename);
713                 if (label == NULL)
714                 {
715                         label = get_file_label(filename);
716                 }
717                 else
718                 {
719                         if (uppers_and_unders(label))
720                         {
721                                 camel_convert(label);
722                         }
723                 }
724         }
725         g_free(filename);
726         GtkWidget *widget = GHB_WIDGET (ud->builder, "source_title");
727         if (label != NULL)
728         {
729                 gtk_label_set_text (GTK_LABEL(widget), label);
730                 ghb_settings_set_string(ud->settings, "volume_label", label);
731                 g_free(label);
732                 if (update_dest)
733                         set_destination(ud);
734         }
735         else
736         {
737                 label = "No Title Found";
738                 gtk_label_set_text (GTK_LABEL(widget), label);
739                 ghb_settings_set_string(ud->settings, "volume_label", label);
740                 return FALSE;
741         }
742         return TRUE;
743 }
744
745 G_MODULE_EXPORT void
746 chooser_file_selected_cb(GtkFileChooser *dialog, signal_user_data_t *ud)
747 {
748         gchar *name = gtk_file_chooser_get_filename (dialog);
749         GtkTreeModel *store;
750         GtkTreeIter iter;
751         const gchar *device;
752         gboolean foundit = FALSE;
753         GtkComboBox *combo;
754         
755         if (name == NULL) return;
756         combo = GTK_COMBO_BOX(GHB_WIDGET(ud->builder, "source_device"));
757         store = gtk_combo_box_get_model(combo);
758         if (gtk_tree_model_get_iter_first(store, &iter))
759         {
760                 do
761                 {
762                         gtk_tree_model_get(store, &iter, 0, &device, -1);
763                         if (strcmp(name, device) == 0)
764                         {
765                                 foundit = TRUE;
766                                 break;
767                         }
768                 } while (gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &iter));
769         }
770         if (foundit)
771                 gtk_combo_box_set_active_iter (combo, &iter);
772         else
773                 gtk_combo_box_set_active (combo, 0);
774
775         g_free(name);
776 }
777
778 G_MODULE_EXPORT void
779 dvd_device_changed_cb(GtkComboBox *combo, signal_user_data_t *ud)
780 {
781         GtkWidget *dialog;
782         gint ii;
783
784         ii = gtk_combo_box_get_active (combo);
785         if (ii > 0)
786         {
787                 const gchar *device;
788                 gchar *name;
789
790                 dialog = GHB_WIDGET(ud->builder, "source_dialog");
791                 device = gtk_combo_box_get_active_text (combo);
792                 name = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER(dialog));
793                 if (name == NULL || strcmp(name, device) != 0)
794                         gtk_file_chooser_select_filename (GTK_FILE_CHOOSER(dialog), device);
795                 if (name != NULL)
796                         g_free(name);
797         }
798 }
799
800 G_MODULE_EXPORT void
801 source_type_changed_cb(GtkToggleButton *toggle, signal_user_data_t *ud)
802 {
803         gchar *folder;
804         GtkFileChooser *chooser;
805         GtkWidget *dvd_device_combo;
806         
807         g_debug("source_type_changed_cb ()");
808         chooser = GTK_FILE_CHOOSER(GHB_WIDGET(ud->builder, "source_dialog"));
809         dvd_device_combo = GHB_WIDGET(ud->builder, "source_device");
810         folder = gtk_file_chooser_get_current_folder (chooser);
811         if (gtk_toggle_button_get_active (toggle))
812         {
813                 gtk_file_chooser_set_action (chooser, 
814                                                                         GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER);
815                 gtk_widget_set_sensitive (dvd_device_combo, FALSE);
816                 gtk_combo_box_set_active (GTK_COMBO_BOX(dvd_device_combo), 0);
817         }
818         else
819         {
820                 gtk_file_chooser_set_action (chooser, GTK_FILE_CHOOSER_ACTION_OPEN);
821                 gtk_widget_set_sensitive (dvd_device_combo, TRUE);
822         }
823         if (folder != NULL)
824         {
825                 gtk_file_chooser_set_current_folder(chooser, folder);
826                 g_free(folder);
827         }
828 }
829
830 static void
831 source_dialog_extra_widgets(
832         signal_user_data_t *ud,
833         GtkWidget *dialog, 
834         gboolean checkbutton_active)
835 {
836         GtkToggleButton *checkbutton;
837         GtkComboBox *combo;
838         GList *drives, *link;
839         
840         checkbutton = GTK_TOGGLE_BUTTON(
841                 GHB_WIDGET(ud->builder, "source_folder_flag"));
842         gtk_toggle_button_set_active(checkbutton, checkbutton_active);
843         combo = GTK_COMBO_BOX(GHB_WIDGET(ud->builder, "source_device"));
844         gtk_list_store_clear(GTK_LIST_STORE(
845                                                 gtk_combo_box_get_model(combo)));
846
847         link = drives = dvd_device_list();
848         gtk_combo_box_append_text (combo, "Not Selected");
849         while (link != NULL)
850         {
851                 gchar *name = get_dvd_device_name(link->data);
852                 gtk_combo_box_append_text(combo, name);
853                 g_free(name);
854                 free_drive(link->data);
855                 link = link->next;
856         }
857         g_list_free(drives);
858 }
859
860 extern GValue *ghb_queue_edit_settings;
861 static gchar *last_scan_file = NULL;
862
863 static void 
864 show_scan_progress(signal_user_data_t *ud)
865 {
866         GtkProgressBar *progress;
867         GtkLabel *label;
868
869         progress = GTK_PROGRESS_BAR(GHB_WIDGET(ud->builder, "scan_prog"));
870         gtk_progress_bar_set_fraction (progress, 0);
871         gtk_widget_show(GTK_WIDGET(progress));
872
873         label = GTK_LABEL(GHB_WIDGET(ud->builder, "source_title"));
874         gtk_label_set_text( label, "Scanning ..." );
875 }
876
877 static void
878 start_scan(
879         signal_user_data_t *ud, 
880         const gchar *path, 
881         gint titlenum, 
882         gint preview_count)
883 {
884         GtkWidget *widget;
885         GtkAction *action;
886         ghb_status_t status;
887
888         ghb_get_status(&status);
889         if (status.scan.state != GHB_STATE_IDLE)
890                 return;
891
892         widget = GHB_WIDGET(ud->builder, "sourcetoolbutton");
893         gtk_widget_set_sensitive(widget, FALSE);
894         action = GHB_ACTION(ud->builder, "source_action");
895         gtk_action_set_sensitive(action, FALSE);
896         action = GHB_ACTION(ud->builder, "source_single_action");
897         gtk_action_set_sensitive(action, FALSE);
898         ghb_backend_scan(path, titlenum, preview_count);
899 }
900
901 void
902 ghb_do_scan(
903         signal_user_data_t *ud, 
904         const gchar *filename, 
905         gint titlenum, 
906         gboolean force)
907 {
908         g_debug("ghb_do_scan()");
909         if (!force && last_scan_file != NULL &&
910                 strcmp(last_scan_file, filename) == 0)
911         {
912                 if (ghb_queue_edit_settings)
913                 {
914                         gint jstatus;
915
916                         jstatus = ghb_settings_get_int(ghb_queue_edit_settings, "job_status");
917                         ghb_settings_to_ui(ud, ghb_queue_edit_settings);
918                         ghb_set_audio(ud, ghb_queue_edit_settings);
919                         ghb_reset_subtitles(ud, ghb_queue_edit_settings);
920                         if (jstatus == GHB_QUEUE_PENDING)
921                         {
922                                 ghb_value_free(ghb_queue_edit_settings);
923                         }
924                         ghb_queue_edit_settings = NULL;
925                 }
926                 return;
927         }
928         if (last_scan_file != NULL)
929                 g_free(last_scan_file);
930         last_scan_file = NULL;
931         if (filename != NULL)
932         {
933                 last_scan_file = g_strdup(filename);
934                 ghb_settings_set_string(ud->settings, "source", filename);
935                 if (update_source_label(ud, filename, TRUE))
936                 {
937                         gchar *path;
938                         gint preview_count;
939
940                         show_scan_progress(ud);
941                         path = ghb_settings_get_string( ud->settings, "source");
942                         prune_logs(ud);
943
944                         preview_count = ghb_settings_get_int(ud->settings, "preview_count");
945                         start_scan(ud, path, titlenum, preview_count);
946                         g_free(path);
947                 }
948                 else
949                 {
950                         // TODO: error dialog
951                 }
952         }
953 }
954
955 static gboolean 
956 update_source_name(gpointer data)
957 {
958         signal_user_data_t *ud = (signal_user_data_t*)data;
959         GtkWidget *dialog;
960         gchar *sourcename;
961
962         sourcename = ghb_settings_get_string(ud->settings, "source");
963         dialog = GHB_WIDGET(ud->builder, "source_dialog");
964         gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dialog), sourcename);
965         g_free(sourcename);
966         return FALSE;
967 }
968
969 static void
970 do_source_dialog(GtkButton *button, gboolean single, signal_user_data_t *ud)
971 {
972         GtkWidget *dialog;
973         gchar *sourcename;
974         gint    response;
975         GtkFileChooserAction action;
976         gboolean checkbutton_active;
977
978         g_debug("source_browse_clicked_cb ()");
979         sourcename = ghb_settings_get_string(ud->settings, "source");
980         checkbutton_active = FALSE;
981         if (g_file_test(sourcename, G_FILE_TEST_IS_DIR))
982         {
983                 action = GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER;
984                 checkbutton_active = TRUE;
985         }
986         else
987         {
988                 action = GTK_FILE_CHOOSER_ACTION_OPEN;
989         }
990         GtkWidget *widget;
991         widget = GHB_WIDGET(ud->builder, "single_title_box");
992         if (single)
993                 gtk_widget_show(widget);
994         else
995                 gtk_widget_hide(widget);
996         dialog = GHB_WIDGET(ud->builder, "source_dialog");
997         source_dialog_extra_widgets(ud, dialog, checkbutton_active);
998         gtk_file_chooser_set_action(GTK_FILE_CHOOSER(dialog), action);
999         // Updating the filename in the file chooser dialog doesn't seem
1000         // to work unless the dialog is running for some reason.
1001         // So handle it in an "idle" event.
1002         //gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dialog), sourcename);
1003         g_idle_add((GSourceFunc)update_source_name, ud);
1004         response = gtk_dialog_run(GTK_DIALOG (dialog));
1005         gtk_widget_hide(dialog);
1006         if (response == GTK_RESPONSE_ACCEPT)
1007         {
1008                 gchar *filename;
1009
1010                 filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
1011                 if (filename != NULL)
1012                 {
1013                         gint titlenum;
1014
1015                         if (single)
1016                                 titlenum = ghb_settings_get_int(ud->settings, "single_title");
1017                         else
1018                                 titlenum = 0;
1019                         ghb_do_scan(ud, filename, titlenum, TRUE);
1020                         if (strcmp(sourcename, filename) != 0)
1021                         {
1022                                 ghb_settings_set_string (ud->settings, 
1023                                                                                 "default_source", filename);
1024                                 ghb_pref_save (ud->settings, "default_source");
1025                                 ghb_dvd_set_current (filename, ud);
1026                         }
1027                         g_free(filename);
1028                 }
1029         }
1030         g_free(sourcename);
1031 }
1032
1033 G_MODULE_EXPORT void
1034 source_button_clicked_cb(GtkButton *button, signal_user_data_t *ud)
1035 {
1036         do_source_dialog(button, FALSE, ud);
1037 }
1038
1039 G_MODULE_EXPORT void
1040 single_title_source_cb(GtkButton *button, signal_user_data_t *ud)
1041 {
1042         do_source_dialog(button, TRUE, ud);
1043 }
1044
1045 G_MODULE_EXPORT void
1046 dvd_source_activate_cb(GtkAction *action, signal_user_data_t *ud)
1047 {
1048         const gchar *filename;
1049         gchar *sourcename;
1050
1051         sourcename = ghb_settings_get_string(ud->settings, "source");
1052         filename = gtk_action_get_name(action);
1053         ghb_do_scan(ud, filename, 0, TRUE);
1054         if (strcmp(sourcename, filename) != 0)
1055         {
1056                 ghb_settings_set_string (ud->settings, "default_source", filename);
1057                 ghb_pref_save (ud->settings, "default_source");
1058                 ghb_dvd_set_current (filename, ud);
1059         }
1060         g_free(sourcename);
1061 }
1062
1063 void
1064 ghb_update_destination_extension(signal_user_data_t *ud)
1065 {
1066         static gchar *containers[] = {".mkv", ".mp4", ".m4v", NULL};
1067         gchar *filename;
1068         const gchar *extension;
1069         gint ii;
1070         GtkEntry *entry;
1071         static gboolean busy = FALSE;
1072
1073         g_debug("ghb_update_destination_extension ()");
1074         // Since this function modifies the thing that triggers it's
1075         // invocation, check to see if busy to prevent accidental infinite
1076         // recursion.
1077         if (busy)
1078                 return;
1079         busy = TRUE;
1080         extension = get_extension(ud);
1081         entry = GTK_ENTRY(GHB_WIDGET(ud->builder, "dest_file"));
1082         filename = g_strdup(gtk_entry_get_text(entry));
1083         for (ii = 0; containers[ii] != NULL; ii++)
1084         {
1085                 if (g_str_has_suffix(filename, containers[ii]))
1086                 {
1087                         gchar *pos;
1088                         gchar *new_name;
1089                         
1090                         pos = g_strrstr( filename, "." );
1091                         if (pos == NULL)
1092                         {
1093                                 // No period? shouldn't happen
1094                                 break;
1095                         }
1096                         *pos = 0;
1097                         if (strcmp(extension, &pos[1]) == 0)
1098                         {
1099                                 // Extension is already correct
1100                                 break;
1101                         }
1102                         new_name = g_strjoin(".", filename, extension, NULL); 
1103                         ghb_ui_update(ud, "dest_file", ghb_string_value(new_name));
1104                         g_free(new_name);
1105                         break;
1106                 }
1107         }
1108         g_free(filename);
1109         busy = FALSE;
1110 }
1111
1112 static void
1113 destination_select_title(GtkEntry *entry)
1114 {
1115         const gchar *dest;
1116         gint start, end;
1117
1118         dest = gtk_entry_get_text(entry);
1119         for (end = strlen(dest)-1; end > 0; end--)
1120         {
1121                 if (dest[end] == '.')
1122                 {
1123                         break;
1124                 }
1125         }
1126         for (start = end; start >= 0; start--)
1127         {
1128                 if (dest[start] == G_DIR_SEPARATOR)
1129                 {
1130                         start++;
1131                         break;
1132                 }
1133         }
1134         if (start < 0) start = 0;
1135         if (start < end)
1136         {
1137                 gtk_editable_select_region(GTK_EDITABLE(entry), start, end);
1138         }
1139 }
1140
1141 G_MODULE_EXPORT gboolean
1142 destination_grab_cb(
1143         GtkEntry *entry, 
1144         signal_user_data_t *ud)
1145 {
1146         destination_select_title(entry);
1147         return FALSE;
1148 }
1149
1150 static gboolean update_default_destination = FALSE;
1151
1152 G_MODULE_EXPORT void
1153 dest_dir_set_cb(GtkFileChooserButton *dest_chooser, signal_user_data_t *ud)
1154 {
1155         gchar *dest_file, *dest_dir, *dest;
1156         
1157         g_debug("dest_dir_set_cb ()");
1158         ghb_widget_to_setting(ud->settings, (GtkWidget*)dest_chooser);
1159         dest_file = ghb_settings_get_string(ud->settings, "dest_file");
1160         dest_dir = ghb_settings_get_string(ud->settings, "dest_dir");
1161         dest = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s", dest_dir, dest_file);
1162         ghb_settings_set_string(ud->settings, "destination", dest);
1163         g_free(dest_file);
1164         g_free(dest_dir);
1165         g_free(dest);
1166         update_default_destination = TRUE;
1167 }
1168
1169 G_MODULE_EXPORT void
1170 dest_file_changed_cb(GtkEntry *entry, signal_user_data_t *ud)
1171 {
1172         gchar *dest_file, *dest_dir, *dest;
1173         
1174         g_debug("dest_file_changed_cb ()");
1175         ghb_update_destination_extension(ud);
1176         ghb_widget_to_setting(ud->settings, (GtkWidget*)entry);
1177         // This signal goes off with ever keystroke, so I'm putting this
1178         // update on the timer.
1179         dest_file = ghb_settings_get_string(ud->settings, "dest_file");
1180         dest_dir = ghb_settings_get_string(ud->settings, "dest_dir");
1181         dest = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s", dest_dir, dest_file);
1182         ghb_settings_set_string(ud->settings, "destination", dest);
1183         g_free(dest_file);
1184         g_free(dest_dir);
1185         g_free(dest);
1186         update_default_destination = TRUE;
1187 }
1188
1189 G_MODULE_EXPORT void
1190 destination_browse_clicked_cb(GtkButton *button, signal_user_data_t *ud)
1191 {
1192         GtkWidget *dialog;
1193         GtkEntry *entry;
1194         gchar *destname;
1195         gchar *basename;
1196
1197         g_debug("destination_browse_clicked_cb ()");
1198         destname = ghb_settings_get_string(ud->settings, "destination");
1199         dialog = gtk_file_chooser_dialog_new ("Choose Destination",
1200                                           NULL,
1201                                           GTK_FILE_CHOOSER_ACTION_SAVE,
1202                                           GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1203                                           GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
1204                                           NULL);
1205         gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dialog), destname);
1206         basename = g_path_get_basename(destname);
1207         g_free(destname);
1208         gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), basename);
1209         g_free(basename);
1210         if (gtk_dialog_run(GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
1211         {
1212                 char *filename, *dirname;
1213                 GtkFileChooser *dest_chooser;
1214                 
1215                 filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
1216                 basename = g_path_get_basename(filename);
1217                 dirname = g_path_get_dirname(filename);
1218                 entry = (GtkEntry*)GHB_WIDGET(ud->builder, "dest_file");
1219                 gtk_entry_set_text(entry, basename);
1220                 dest_chooser = GTK_FILE_CHOOSER(GHB_WIDGET(ud->builder, "dest_dir"));
1221                 gtk_file_chooser_set_filename(dest_chooser, dirname);
1222                 g_free (dirname);
1223                 g_free (basename);
1224                 g_free (filename);
1225         }
1226         gtk_widget_destroy(dialog);
1227 }
1228
1229 G_MODULE_EXPORT gboolean
1230 window_destroy_event_cb(GtkWidget *widget, GdkEvent *event, signal_user_data_t *ud)
1231 {
1232         g_debug("window_destroy_event_cb ()");
1233         ghb_hb_cleanup(FALSE);
1234         prune_logs(ud);
1235         gtk_main_quit();
1236         return FALSE;
1237 }
1238
1239 G_MODULE_EXPORT gboolean
1240 window_delete_event_cb(GtkWidget *widget, GdkEvent *event, signal_user_data_t *ud)
1241 {
1242         gint state = ghb_get_queue_state();
1243         g_debug("window_delete_event_cb ()");
1244         if (state & GHB_STATE_WORKING)
1245         {
1246                 if (ghb_cancel_encode2(ud, "Closing HandBrake will terminate encoding.\n"))
1247                 {
1248                         ghb_hb_cleanup(FALSE);
1249                         prune_logs(ud);
1250                         gtk_main_quit();
1251                         return FALSE;
1252                 }
1253                 return TRUE;
1254         }
1255         ghb_hb_cleanup(FALSE);
1256         prune_logs(ud);
1257         gtk_main_quit();
1258         return FALSE;
1259 }
1260
1261 static void
1262 update_acodec_combo(signal_user_data_t *ud)
1263 {
1264         ghb_grey_combo_options (ud->builder);
1265 }
1266
1267 G_MODULE_EXPORT void
1268 container_changed_cb(GtkWidget *widget, signal_user_data_t *ud)
1269 {
1270         g_debug("container_changed_cb ()");
1271         ghb_widget_to_setting(ud->settings, widget);
1272         ghb_check_dependency(ud, widget);
1273         update_acodec_combo(ud);
1274         ghb_update_destination_extension(ud);
1275         ghb_clear_presets_selection(ud);
1276         ghb_live_reset(ud);
1277         ghb_subtitle_prune(ud);
1278 }
1279
1280 static gchar*
1281 get_aspect_string(gint aspect_n, gint aspect_d)
1282 {
1283         gchar *aspect;
1284
1285         if (aspect_d < 10)
1286         {
1287                 aspect = g_strdup_printf("%d:%d", aspect_n, aspect_d);
1288         }
1289         else
1290         {
1291                 gdouble aspect_nf = (gdouble)aspect_n / aspect_d;
1292                 aspect = g_strdup_printf("%.2f:1", aspect_nf);
1293         }
1294         return aspect;
1295 }
1296
1297 static gchar*
1298 get_rate_string(gint rate_base, gint rate)
1299 {
1300         gdouble rate_f = (gdouble)rate / rate_base;
1301         gchar *rate_s;
1302
1303         rate_s = g_strdup_printf("%.6g", rate_f);
1304         return rate_s;
1305 }
1306 static void
1307 show_title_info(signal_user_data_t *ud, ghb_title_info_t *tinfo)
1308 {
1309         GtkWidget *widget;
1310         gchar *text;
1311
1312         ud->dont_clear_presets = TRUE;
1313         widget = GHB_WIDGET (ud->builder, "title_duration");
1314         if (tinfo->duration != 0)
1315         {
1316                 text = g_strdup_printf ("%02d:%02d:%02d", tinfo->hours, 
1317                                 tinfo->minutes, tinfo->seconds);
1318         }
1319         else
1320         {
1321                 text = g_strdup_printf ("Unknown");
1322         }
1323         gtk_label_set_text (GTK_LABEL(widget), text);
1324         g_free(text);
1325         widget = GHB_WIDGET (ud->builder, "source_dimensions");
1326         text = g_strdup_printf ("%d x %d", tinfo->width, tinfo->height);
1327         gtk_label_set_text (GTK_LABEL(widget), text);
1328         ghb_settings_set_int(ud->settings, "source_width", tinfo->width);
1329         ghb_settings_set_int(ud->settings, "source_height", tinfo->height);
1330         g_free(text);
1331         widget = GHB_WIDGET (ud->builder, "source_aspect");
1332         text = get_aspect_string(tinfo->aspect_n, tinfo->aspect_d);
1333         gtk_label_set_text (GTK_LABEL(widget), text);
1334         g_free(text);
1335
1336         widget = GHB_WIDGET (ud->builder, "source_frame_rate");
1337         text = (gchar*)get_rate_string(tinfo->rate_base, tinfo->rate);
1338         gtk_label_set_text (GTK_LABEL(widget), text);
1339         g_free(text);
1340
1341         ghb_ui_update(ud, "scale_width", 
1342                 ghb_int64_value(tinfo->width - tinfo->crop[2] - tinfo->crop[3]));
1343         // If anamorphic or keep_aspect, the hight will be automatically calculated
1344         gboolean keep_aspect;
1345         gint pic_par;
1346         keep_aspect = ghb_settings_get_boolean(ud->settings, "PictureKeepRatio");
1347         pic_par = ghb_settings_combo_int(ud->settings, "PicturePAR");
1348         if (!(keep_aspect || pic_par) || pic_par == 3)
1349         {
1350                 ghb_ui_update(ud, "scale_height", 
1351                         ghb_int64_value(tinfo->height - tinfo->crop[0] - tinfo->crop[1]));
1352         }
1353
1354         // Set the limits of cropping.  hb_set_anamorphic_size crashes if
1355         // you pass it a cropped width or height == 0.
1356         gint bound;
1357         bound = tinfo->height / 2 - 2;
1358         widget = GHB_WIDGET (ud->builder, "PictureTopCrop");
1359         gtk_spin_button_set_range (GTK_SPIN_BUTTON(widget), 0, bound);
1360         widget = GHB_WIDGET (ud->builder, "PictureBottomCrop");
1361         gtk_spin_button_set_range (GTK_SPIN_BUTTON(widget), 0, bound);
1362         bound = tinfo->width / 2 - 2;
1363         widget = GHB_WIDGET (ud->builder, "PictureLeftCrop");
1364         gtk_spin_button_set_range (GTK_SPIN_BUTTON(widget), 0, bound);
1365         widget = GHB_WIDGET (ud->builder, "PictureRightCrop");
1366         gtk_spin_button_set_range (GTK_SPIN_BUTTON(widget), 0, bound);
1367         if (ghb_settings_get_boolean(ud->settings, "PictureAutoCrop"))
1368         {
1369                 ghb_ui_update(ud, "PictureTopCrop", ghb_int64_value(tinfo->crop[0]));
1370                 ghb_ui_update(ud, "PictureBottomCrop", ghb_int64_value(tinfo->crop[1]));
1371                 ghb_ui_update(ud, "PictureLeftCrop", ghb_int64_value(tinfo->crop[2]));
1372                 ghb_ui_update(ud, "PictureRightCrop", ghb_int64_value(tinfo->crop[3]));
1373         }
1374         ghb_set_scale (ud, GHB_PIC_KEEP_PAR);
1375         gint width, height, crop[4];
1376         crop[0] = ghb_settings_get_int(ud->settings, "PictureTopCrop");
1377         crop[1] = ghb_settings_get_int(ud->settings, "PictureBottomCrop");
1378         crop[2] = ghb_settings_get_int(ud->settings, "PictureLeftCrop");
1379         crop[3] = ghb_settings_get_int(ud->settings, "PictureRightCrop");
1380         width = tinfo->width - crop[2] - crop[3];
1381         height = tinfo->height - crop[0] - crop[1];
1382         widget = GHB_WIDGET (ud->builder, "crop_dimensions");
1383         text = g_strdup_printf ("%d x %d", width, height);
1384         gtk_label_set_text (GTK_LABEL(widget), text);
1385         g_free(text);
1386
1387         g_debug("setting max end chapter %d", tinfo->num_chapters);
1388         widget = GHB_WIDGET (ud->builder, "end_chapter");
1389         gtk_spin_button_set_range (GTK_SPIN_BUTTON(widget), 1, tinfo->num_chapters);
1390         gtk_spin_button_set_value (GTK_SPIN_BUTTON(widget), tinfo->num_chapters);
1391         widget = GHB_WIDGET (ud->builder, "start_chapter");
1392         gtk_spin_button_set_value (GTK_SPIN_BUTTON(widget), 1);
1393         gtk_spin_button_set_range (GTK_SPIN_BUTTON(widget), 1, tinfo->num_chapters);
1394
1395         widget = GHB_WIDGET (ud->builder, "angle");
1396         gtk_spin_button_set_value (GTK_SPIN_BUTTON(widget), 1);
1397         gtk_spin_button_set_range (GTK_SPIN_BUTTON(widget), 1, tinfo->angle_count);
1398         ud->dont_clear_presets = FALSE;
1399 }
1400
1401 static gboolean update_preview = FALSE;
1402
1403 G_MODULE_EXPORT void
1404 title_changed_cb(GtkWidget *widget, signal_user_data_t *ud)
1405 {
1406         ghb_title_info_t tinfo;
1407         gint titleindex;
1408         
1409         g_debug("title_changed_cb ()");
1410         ghb_widget_to_setting(ud->settings, widget);
1411         ghb_check_dependency(ud, widget);
1412
1413         titleindex = ghb_settings_combo_int(ud->settings, "title");
1414         ghb_update_ui_combo_box (ud, "AudioTrack", titleindex, FALSE);
1415         ghb_update_ui_combo_box (ud, "SubtitleTrack", titleindex, FALSE);
1416
1417         if (ghb_get_title_info (&tinfo, titleindex))
1418         {
1419                 show_title_info(ud, &tinfo);
1420         }
1421         update_chapter_list (ud);
1422         ghb_adjust_audio_rate_combos(ud);
1423         ghb_set_pref_audio(titleindex, ud);
1424         ghb_set_pref_subtitle(titleindex, ud);
1425         if (ghb_settings_get_boolean(ud->settings, "vquality_type_target"))
1426         {
1427                 gint bitrate = ghb_calculate_target_bitrate (ud->settings, titleindex);
1428                 ghb_ui_update(ud, "VideoAvgBitrate", ghb_int64_value(bitrate));
1429         }
1430
1431         // Unfortunately, there is no way to query how many frames were
1432         // actually generated during the scan.
1433         // If I knew how many were generated, I would adjust the spin
1434         // control range here.
1435         // I do know how many were asked for.
1436         gint preview_count;
1437         preview_count = ghb_settings_get_int(ud->settings, "preview_count");
1438         widget = GHB_WIDGET(ud->builder, "preview_frame");
1439         gtk_range_set_range (GTK_RANGE(widget), 1, preview_count);
1440         ghb_ui_update(ud, "preview_frame", ghb_int64_value(2));
1441
1442         ghb_set_preview_image (ud);
1443         if (ghb_settings_get_boolean(ud->settings, "title_no_in_destination"))
1444         {
1445                 set_destination(ud);
1446         }
1447 }
1448
1449 G_MODULE_EXPORT void
1450 setting_widget_changed_cb(GtkWidget *widget, signal_user_data_t *ud)
1451 {
1452         ghb_widget_to_setting(ud->settings, widget);
1453         ghb_check_dependency(ud, widget);
1454         ghb_clear_presets_selection(ud);
1455         ghb_live_reset(ud);
1456 }
1457
1458 G_MODULE_EXPORT void
1459 chapter_markers_changed_cb(GtkWidget *widget, signal_user_data_t *ud)
1460 {
1461         ghb_widget_to_setting(ud->settings, widget);
1462         ghb_check_dependency(ud, widget);
1463         ghb_clear_presets_selection(ud);
1464         ghb_live_reset(ud);
1465         ghb_update_destination_extension(ud);
1466 }
1467
1468 G_MODULE_EXPORT void
1469 vquality_changed_cb(GtkWidget *widget, signal_user_data_t *ud)
1470 {
1471         ghb_widget_to_setting(ud->settings, widget);
1472         ghb_check_dependency(ud, widget);
1473         ghb_clear_presets_selection(ud);
1474         ghb_live_reset(ud);
1475
1476         gint vcodec = ghb_settings_combo_int(ud->settings, "VideoEncoder");
1477         gdouble step;
1478         if (vcodec == HB_VCODEC_X264)
1479         {
1480                 step = ghb_settings_combo_double(ud->settings, 
1481                                                                                         "VideoQualityGranularity");
1482         }
1483         else
1484         {
1485                 step = 1;
1486         }
1487         gdouble val = gtk_range_get_value(GTK_RANGE(widget));
1488         val = ((int)((val + step / 2) / step)) * step;
1489         gtk_range_set_value(GTK_RANGE(widget), val);
1490 }
1491
1492 G_MODULE_EXPORT void
1493 http_opt_changed_cb(GtkWidget *widget, signal_user_data_t *ud)
1494 {
1495         ghb_widget_to_setting(ud->settings, widget);
1496         ghb_check_dependency(ud, widget);
1497         ghb_clear_presets_selection(ud);
1498         ghb_live_reset(ud);
1499         // AC3 is not allowed when Web optimized
1500         ghb_grey_combo_options (ud->builder);
1501 }
1502
1503 G_MODULE_EXPORT void
1504 vcodec_changed_cb(GtkWidget *widget, signal_user_data_t *ud)
1505 {
1506         gdouble vqmin, vqmax, step, page;
1507         gboolean inverted;
1508         gint digits;
1509
1510         ghb_widget_to_setting(ud->settings, widget);
1511         ghb_check_dependency(ud, widget);
1512         ghb_clear_presets_selection(ud);
1513         ghb_live_reset(ud);
1514         ghb_vquality_range(ud, &vqmin, &vqmax, &step, &page, &digits, &inverted);
1515         GtkWidget *qp = GHB_WIDGET(ud->builder, "VideoQualitySlider");
1516         gtk_range_set_range (GTK_RANGE(qp), vqmin, vqmax);
1517         gtk_range_set_increments (GTK_RANGE(qp), step, page);
1518         gtk_scale_set_digits(GTK_SCALE(qp), digits);
1519         gtk_range_set_inverted (GTK_RANGE(qp), inverted);
1520 }
1521
1522 G_MODULE_EXPORT void
1523 target_size_changed_cb(GtkWidget *widget, signal_user_data_t *ud)
1524 {
1525         const gchar *name = gtk_widget_get_name(widget);
1526         g_debug("target_size_changed_cb () %s", name);
1527         ghb_widget_to_setting(ud->settings, widget);
1528         ghb_check_dependency(ud, widget);
1529         ghb_clear_presets_selection(ud);
1530         ghb_live_reset(ud);
1531         if (ghb_settings_get_boolean(ud->settings, "vquality_type_target"))
1532         {
1533                 gint titleindex;
1534                 titleindex = ghb_settings_combo_int(ud->settings, "title");
1535                 gint bitrate = ghb_calculate_target_bitrate (ud->settings, titleindex);
1536                 ghb_ui_update(ud, "VideoAvgBitrate", ghb_int64_value(bitrate));
1537         }
1538 }
1539
1540 G_MODULE_EXPORT void
1541 start_chapter_changed_cb(GtkWidget *widget, signal_user_data_t *ud)
1542 {
1543         gint start, end;
1544         const gchar *name = gtk_widget_get_name(widget);
1545
1546         g_debug("start_chapter_changed_cb () %s", name);
1547         ghb_widget_to_setting(ud->settings, widget);
1548         start = ghb_settings_get_int(ud->settings, "start_chapter");
1549         end = ghb_settings_get_int(ud->settings, "end_chapter");
1550         if (start > end)
1551                 ghb_ui_update(ud, "end_chapter", ghb_int_value(start));
1552         ghb_check_dependency(ud, widget);
1553         if (ghb_settings_get_boolean(ud->settings, "chapters_in_destination"))
1554         {
1555                 set_destination(ud);
1556         }
1557         widget = GHB_WIDGET (ud->builder, "chapters_tab");
1558         // End may have been changed above, get it again
1559         end = ghb_settings_get_int(ud->settings, "end_chapter");
1560         if (start == end)
1561         {
1562                 gtk_widget_hide(widget);
1563         }
1564         else
1565         {
1566                 gtk_widget_show(widget);
1567         }
1568 }
1569
1570 G_MODULE_EXPORT void
1571 end_chapter_changed_cb(GtkWidget *widget, signal_user_data_t *ud)
1572 {
1573         gint start, end;
1574         const gchar *name = gtk_widget_get_name(widget);
1575
1576         g_debug("end_chapter_changed_cb () %s", name);
1577         ghb_widget_to_setting(ud->settings, widget);
1578         start = ghb_settings_get_int(ud->settings, "start_chapter");
1579         end = ghb_settings_get_int(ud->settings, "end_chapter");
1580         if (start > end)
1581                 ghb_ui_update(ud, "start_chapter", ghb_int_value(end));
1582         ghb_check_dependency(ud, widget);
1583         if (ghb_settings_get_boolean(ud->settings, "chapters_in_destination"))
1584         {
1585                 set_destination(ud);
1586         }
1587         widget = GHB_WIDGET (ud->builder, "chapters_tab");
1588         // Start may have been changed above, get it again
1589         start = ghb_settings_get_int(ud->settings, "start_chapter");
1590         if (start == end)
1591         {
1592                 gtk_widget_hide(widget);
1593         }
1594         else
1595         {
1596                 gtk_widget_show(widget);
1597         }
1598 }
1599
1600 G_MODULE_EXPORT void
1601 scale_width_changed_cb(GtkWidget *widget, signal_user_data_t *ud)
1602 {
1603         g_debug("scale_width_changed_cb ()");
1604         ghb_widget_to_setting(ud->settings, widget);
1605         ghb_check_dependency(ud, widget);
1606         ghb_clear_presets_selection(ud);
1607         if (GTK_WIDGET_SENSITIVE(widget))
1608                 ghb_set_scale (ud, GHB_PIC_KEEP_WIDTH);
1609         update_preview = TRUE;
1610         gchar *text;
1611         gint width = ghb_settings_get_int(ud->settings, "scale_width");
1612         gint height = ghb_settings_get_int(ud->settings, "scale_height");
1613         widget = GHB_WIDGET (ud->builder, "scale_dimensions");
1614         text = g_strdup_printf ("%d x %d", width, height);
1615         gtk_label_set_text (GTK_LABEL(widget), text);
1616         g_free(text);
1617         ghb_live_reset(ud);
1618 }
1619
1620 G_MODULE_EXPORT void
1621 scale_height_changed_cb(GtkWidget *widget, signal_user_data_t *ud)
1622 {
1623         g_debug("scale_height_changed_cb ()");
1624         ghb_widget_to_setting(ud->settings, widget);
1625         ghb_check_dependency(ud, widget);
1626         ghb_clear_presets_selection(ud);
1627         if (GTK_WIDGET_SENSITIVE(widget))
1628                 ghb_set_scale (ud, GHB_PIC_KEEP_HEIGHT);
1629         update_preview = TRUE;
1630         gchar *text;
1631         gint width = ghb_settings_get_int(ud->settings, "scale_width");
1632         gint height = ghb_settings_get_int(ud->settings, "scale_height");
1633         widget = GHB_WIDGET (ud->builder, "scale_dimensions");
1634         text = g_strdup_printf ("%d x %d", width, height);
1635         gtk_label_set_text (GTK_LABEL(widget), text);
1636         g_free(text);
1637         ghb_live_reset(ud);
1638 }
1639
1640 G_MODULE_EXPORT void
1641 crop_changed_cb(GtkWidget *widget, signal_user_data_t *ud)
1642 {
1643         gint titleindex, crop[4];
1644         ghb_title_info_t tinfo;
1645         
1646         g_debug("crop_changed_cb ()");
1647         ghb_widget_to_setting(ud->settings, widget);
1648         ghb_check_dependency(ud, widget);
1649         ghb_clear_presets_selection(ud);
1650         if (GTK_WIDGET_SENSITIVE(widget))
1651                 ghb_set_scale (ud, 0);
1652
1653         crop[0] = ghb_settings_get_int(ud->settings, "PictureTopCrop");
1654         crop[1] = ghb_settings_get_int(ud->settings, "PictureBottomCrop");
1655         crop[2] = ghb_settings_get_int(ud->settings, "PictureLeftCrop");
1656         crop[3] = ghb_settings_get_int(ud->settings, "PictureRightCrop");
1657         titleindex = ghb_settings_combo_int(ud->settings, "title");
1658         if (ghb_get_title_info (&tinfo, titleindex))
1659         {
1660                 gint width, height;
1661                 gchar *text;
1662                 
1663                 width = tinfo.width - crop[2] - crop[3];
1664                 height = tinfo.height - crop[0] - crop[1];
1665                 widget = GHB_WIDGET (ud->builder, "crop_dimensions");
1666                 text = g_strdup_printf ("%d x %d", width, height);
1667                 gtk_label_set_text (GTK_LABEL(widget), text);
1668                 widget = GHB_WIDGET (ud->builder, "crop_dimensions2");
1669                 gtk_label_set_text (GTK_LABEL(widget), text);
1670                 g_free(text);
1671         }
1672         gchar *text;
1673         widget = GHB_WIDGET (ud->builder, "crop_values");
1674         text = g_strdup_printf ("%d:%d:%d:%d", crop[0], crop[1], crop[2], crop[3]);
1675         gtk_label_set_text (GTK_LABEL(widget), text);
1676         g_free(text);
1677         update_preview = TRUE;
1678         ghb_live_reset(ud);
1679 }
1680
1681 G_MODULE_EXPORT void
1682 display_width_changed_cb(GtkWidget *widget, signal_user_data_t *ud)
1683 {
1684         g_debug("display_width_changed_cb ()");
1685         ghb_widget_to_setting(ud->settings, widget);
1686         ghb_check_dependency(ud, widget);
1687         ghb_clear_presets_selection(ud);
1688         ghb_live_reset(ud);
1689         if (GTK_WIDGET_SENSITIVE(widget))
1690                 ghb_set_scale (ud, GHB_PIC_KEEP_DISPLAY_WIDTH);
1691
1692         update_preview = TRUE;
1693 }
1694
1695 G_MODULE_EXPORT void
1696 display_height_changed_cb(GtkWidget *widget, signal_user_data_t *ud)
1697 {
1698         g_debug("display_height_changed_cb ()");
1699         ghb_widget_to_setting(ud->settings, widget);
1700         ghb_check_dependency(ud, widget);
1701         ghb_clear_presets_selection(ud);
1702         ghb_live_reset(ud);
1703         if (GTK_WIDGET_SENSITIVE(widget))
1704                 ghb_set_scale (ud, GHB_PIC_KEEP_DISPLAY_HEIGHT);
1705
1706         update_preview = TRUE;
1707 }
1708
1709 G_MODULE_EXPORT void
1710 par_changed_cb(GtkWidget *widget, signal_user_data_t *ud)
1711 {
1712         g_debug("par_changed_cb ()");
1713         ghb_widget_to_setting(ud->settings, widget);
1714         ghb_check_dependency(ud, widget);
1715         ghb_clear_presets_selection(ud);
1716         ghb_live_reset(ud);
1717         if (GTK_WIDGET_SENSITIVE(widget))
1718                 ghb_set_scale (ud, GHB_PIC_KEEP_PAR);
1719
1720         update_preview = TRUE;
1721 }
1722
1723 G_MODULE_EXPORT void
1724 scale_changed_cb(GtkWidget *widget, signal_user_data_t *ud)
1725 {
1726         g_debug("scale_changed_cb ()");
1727         ghb_widget_to_setting(ud->settings, widget);
1728         ghb_check_dependency(ud, widget);
1729         ghb_clear_presets_selection(ud);
1730         ghb_live_reset(ud);
1731         if (GTK_WIDGET_SENSITIVE(widget))
1732                 ghb_set_scale (ud, 0);
1733         update_preview = TRUE;
1734         
1735         gchar *text;
1736         
1737         text = ghb_settings_get_boolean(ud->settings, "PictureAutoCrop") ? "On" : "Off";
1738         widget = GHB_WIDGET (ud->builder, "crop_auto");
1739         gtk_label_set_text (GTK_LABEL(widget), text);
1740         text = ghb_settings_get_boolean(ud->settings, "autoscale") ? "On" : "Off";
1741         widget = GHB_WIDGET (ud->builder, "scale_auto");
1742         gtk_label_set_text (GTK_LABEL(widget), text);
1743         switch (ghb_settings_combo_int(ud->settings, "PicturePAR"))
1744         {
1745                 case 0:
1746                         text = "Off";
1747                         break;
1748                 case 1:
1749                         text = "Strict";
1750                         break;
1751                 case 2:
1752                         text = "Loose";
1753                         break;
1754                 case 3:
1755                         text = "Custom";
1756                         break;
1757                 default:
1758                         text = "Unknown";
1759                         break;
1760         }
1761         widget = GHB_WIDGET (ud->builder, "scale_anamorphic");
1762         gtk_label_set_text (GTK_LABEL(widget), text);
1763 }
1764
1765 G_MODULE_EXPORT void
1766 show_crop_changed_cb(GtkWidget *widget, signal_user_data_t *ud)
1767 {
1768         g_debug("show_crop_changed_cb ()");
1769         ghb_widget_to_setting(ud->settings, widget);
1770         ghb_check_dependency(ud, widget);
1771         ghb_live_reset(ud);
1772         if (GTK_WIDGET_SENSITIVE(widget))
1773                 ghb_set_scale (ud, 0);
1774         update_preview = TRUE;
1775 }
1776
1777 G_MODULE_EXPORT void
1778 generic_entry_changed_cb(GtkEntry *entry, signal_user_data_t *ud)
1779 {
1780         // Normally (due to user input) I only want to process the entry
1781         // when editing is done and the focus-out signal is sent.
1782         // But... there's always a but.
1783         // If the entry is changed by software, the focus-out signal is not sent.
1784         // The changed signal is sent ... so here we are.
1785         // I don't want to process upon every keystroke, so I prevent processing
1786         // while the widget has focus.
1787         g_debug("generic_entry_changed_cb ()");
1788         if (!GTK_WIDGET_HAS_FOCUS((GtkWidget*)entry))
1789         {
1790                 ghb_widget_to_setting(ud->settings, (GtkWidget*)entry);
1791         }
1792 }
1793
1794 G_MODULE_EXPORT void
1795 prefs_dialog_cb(GtkWidget *xwidget, signal_user_data_t *ud)
1796 {
1797         GtkWidget *dialog;
1798         GtkResponseType response;
1799
1800         g_debug("prefs_dialog_cb ()");
1801         dialog = GHB_WIDGET(ud->builder, "prefs_dialog");
1802         response = gtk_dialog_run(GTK_DIALOG(dialog));
1803         gtk_widget_hide(dialog);
1804 }
1805
1806 typedef struct
1807 {
1808         GtkMessageDialog *dlg;
1809         const gchar *msg;
1810         const gchar *action;
1811         gint timeout;
1812 } countdown_t;
1813
1814 static gboolean
1815 shutdown_cb(countdown_t *cd)
1816 {
1817         gchar *str;
1818
1819         cd->timeout--;
1820         if (cd->timeout == 0)
1821         {
1822                 ghb_shutdown_gsm();
1823                 gtk_main_quit();
1824                 return FALSE;
1825         }
1826         str = g_strdup_printf("%s\n\n%s in %d seconds ...", 
1827                                                         cd->msg, cd->action, cd->timeout);
1828         gtk_message_dialog_set_markup(cd->dlg, str);
1829         g_free(str);
1830         return TRUE;
1831 }
1832
1833 static gboolean
1834 suspend_cb(countdown_t *cd)
1835 {
1836         gchar *str;
1837
1838         cd->timeout--;
1839         if (cd->timeout == 0)
1840         {
1841                 gtk_widget_destroy (GTK_WIDGET(cd->dlg));
1842                 ghb_suspend_gpm();
1843                 return FALSE;
1844         }
1845         str = g_strdup_printf("%s\n\n%s in %d seconds ...", 
1846                                                         cd->msg, cd->action, cd->timeout);
1847         gtk_message_dialog_set_markup(cd->dlg, str);
1848         g_free(str);
1849         return TRUE;
1850 }
1851
1852 void
1853 ghb_countdown_dialog(
1854         GtkMessageType type, 
1855         const gchar *message, 
1856         const gchar *action, 
1857         const gchar *cancel, 
1858         GSourceFunc action_func,
1859         gint timeout)
1860 {
1861         GtkWidget *dialog;
1862         GtkResponseType response;
1863         guint timeout_id;
1864         countdown_t cd;
1865                         
1866         cd.msg = message;
1867         cd.action = action;
1868         cd.timeout = timeout;
1869
1870         // Toss up a warning dialog
1871         dialog = gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL,
1872                                                         type, GTK_BUTTONS_NONE,
1873                                                         "%s\n\n%s in %d seconds ...", 
1874                                                         message, action, timeout);
1875         gtk_dialog_add_buttons( GTK_DIALOG(dialog), 
1876                                                    cancel, GTK_RESPONSE_CANCEL,
1877                                                    NULL);
1878
1879         cd.dlg = GTK_MESSAGE_DIALOG(dialog);
1880         timeout_id = g_timeout_add(1000, action_func, &cd);
1881         response = gtk_dialog_run(GTK_DIALOG(dialog));
1882         gtk_widget_destroy (dialog);
1883         if (response == GTK_RESPONSE_CANCEL)
1884         {
1885                 GMainContext *mc;
1886                 GSource *source;
1887
1888                 mc = g_main_context_default();
1889                 source = g_main_context_find_source_by_id(mc, timeout_id);
1890                 if (source != NULL)
1891                         g_source_destroy(source);
1892         }
1893 }
1894
1895 gboolean
1896 ghb_message_dialog(GtkMessageType type, const gchar *message, const gchar *no, const gchar *yes)
1897 {
1898         GtkWidget *dialog;
1899         GtkResponseType response;
1900                         
1901         // Toss up a warning dialog
1902         dialog = gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL,
1903                                                         type, GTK_BUTTONS_NONE,
1904                                                         "%s", message);
1905         gtk_dialog_add_buttons( GTK_DIALOG(dialog), 
1906                                                    no, GTK_RESPONSE_NO,
1907                                                    yes, GTK_RESPONSE_YES, NULL);
1908         response = gtk_dialog_run(GTK_DIALOG(dialog));
1909         gtk_widget_destroy (dialog);
1910         if (response == GTK_RESPONSE_NO)
1911         {
1912                 return FALSE;
1913         }
1914         return TRUE;
1915 }
1916
1917 void
1918 ghb_error_dialog(GtkMessageType type, const gchar *message, const gchar *cancel)
1919 {
1920         GtkWidget *dialog;
1921         GtkResponseType response;
1922                         
1923         // Toss up a warning dialog
1924         dialog = gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL,
1925                                                         type, GTK_BUTTONS_NONE,
1926                                                         "%s", message);
1927         gtk_dialog_add_buttons( GTK_DIALOG(dialog), 
1928                                                    cancel, GTK_RESPONSE_CANCEL, NULL);
1929         response = gtk_dialog_run(GTK_DIALOG(dialog));
1930         gtk_widget_destroy (dialog);
1931 }
1932
1933 void
1934 ghb_cancel_encode(signal_user_data_t *ud, const gchar *extra_msg)
1935 {
1936         GtkWidget *dialog;
1937         GtkResponseType response;
1938         
1939         if (extra_msg == NULL) extra_msg = "";
1940         // Toss up a warning dialog
1941         dialog = gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL,
1942                                 GTK_MESSAGE_WARNING, GTK_BUTTONS_NONE,
1943                                 "%sYour movie will be lost if you don't continue encoding.",
1944                                 extra_msg);
1945         gtk_dialog_add_buttons( GTK_DIALOG(dialog), 
1946                                                    "Cancel Current and Stop", 1,
1947                                                    "Cancel Current, Start Next", 2,
1948                                                    "Finish Current, then Stop", 3,
1949                                                    "Continue Encoding", 4,
1950                                                    NULL);
1951         response = gtk_dialog_run(GTK_DIALOG(dialog));
1952         gtk_widget_destroy (dialog);
1953         switch (response)
1954         {
1955                 case 1:
1956                         ghb_stop_queue();
1957                         ud->cancel_encode = GHB_CANCEL_ALL;
1958                         break;
1959                 case 2:
1960                         ghb_stop_queue();
1961                         ud->cancel_encode = GHB_CANCEL_CURRENT;
1962                         break;
1963                 case 3:
1964                         ud->cancel_encode = GHB_CANCEL_FINISH;
1965                         break;
1966                 case 4:
1967                 default:
1968                         ud->cancel_encode = GHB_CANCEL_NONE;
1969                         break;
1970         }
1971 }
1972
1973 gboolean
1974 ghb_cancel_encode2(signal_user_data_t *ud, const gchar *extra_msg)
1975 {
1976         GtkWidget *dialog;
1977         GtkResponseType response;
1978         
1979         if (extra_msg == NULL) extra_msg = "";
1980         // Toss up a warning dialog
1981         dialog = gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL,
1982                                 GTK_MESSAGE_WARNING, GTK_BUTTONS_NONE,
1983                                 "%sYour movie will be lost if you don't continue encoding.",
1984                                 extra_msg);
1985         gtk_dialog_add_buttons( GTK_DIALOG(dialog), 
1986                                                    "Cancel Current and Stop", 1,
1987                                                    "Continue Encoding", 4,
1988                                                    NULL);
1989         response = gtk_dialog_run(GTK_DIALOG(dialog));
1990         gtk_widget_destroy (dialog);
1991         switch (response)
1992         {
1993                 case 1:
1994                         ghb_stop_queue();
1995                         ud->cancel_encode = GHB_CANCEL_ALL;
1996                         return TRUE;
1997                 case 4:
1998                 default:
1999                         break;
2000         }
2001         return FALSE;
2002 }
2003
2004 static void
2005 submit_job(GValue *settings)
2006 {
2007         static gint unique_id = 1;
2008         gchar *type, *modified, *preset;
2009         const GValue *path;
2010         gboolean preset_modified;
2011
2012         g_debug("submit_job");
2013         if (settings == NULL) return;
2014         preset_modified = ghb_settings_get_boolean(settings, "preset_modified");
2015         path = ghb_settings_get_value(settings, "preset");
2016         preset = ghb_preset_path_string(path);
2017         type = ghb_preset_is_custom() ? "Custom " : "";
2018         modified = preset_modified ? "Modified " : "";
2019         ghb_log("%s%sPreset: %s", modified, type, preset);
2020         g_free(preset);
2021
2022         ghb_settings_set_int(settings, "job_unique_id", unique_id);
2023         ghb_settings_set_int(settings, "job_status", GHB_QUEUE_RUNNING);
2024         ghb_add_job (settings, unique_id);
2025         ghb_start_queue();
2026         unique_id++;
2027 }
2028
2029 static void
2030 prune_logs(signal_user_data_t *ud)
2031 {
2032         gchar *dest_dir;
2033         gint days;
2034
2035         // Only prune logs stored in the default config dir location
2036         days = ghb_settings_combo_int(ud->settings, "LogLongevity");
2037         if (days > 365)
2038                 return;
2039
2040         dest_dir = ghb_get_user_config_dir("EncodeLogs");
2041         if (g_file_test(dest_dir, G_FILE_TEST_IS_DIR))
2042         {
2043                 const gchar *file;
2044                 gint duration = days * 24 * 60 * 60;
2045                 
2046                 GDir *gdir = g_dir_open(dest_dir, 0, NULL);
2047                 time_t now;
2048
2049                 now = time(NULL);
2050                 file = g_dir_read_name(gdir);
2051                 while (file)
2052                 {
2053                         gchar *path;
2054                         struct stat stbuf;
2055
2056                         path = g_strdup_printf("%s/%s", dest_dir, file);
2057                         g_stat(path, &stbuf);
2058                         if (now - stbuf.st_mtime > duration)
2059                         {
2060                                 g_unlink(path);
2061                         }
2062                         g_free(path);
2063                         file = g_dir_read_name(gdir);
2064                 }
2065                 g_dir_close(gdir);
2066         }
2067         g_free(dest_dir);
2068         ghb_preview_cleanup(ud);
2069 }
2070
2071 static void
2072 queue_scan(signal_user_data_t *ud, GValue *js)
2073 {
2074         gchar *path;
2075         gint titlenum;
2076         time_t  _now;
2077         struct tm *now;
2078         gchar *log_path, *pos, *destname, *basename, *dest_dir;
2079
2080         _now = time(NULL);
2081         now = localtime(&_now);
2082         destname = ghb_settings_get_string(js, "destination");
2083         basename = g_path_get_basename(destname);
2084         if (ghb_settings_get_boolean(ud->settings, "EncodeLogLocation"))
2085         {
2086                 dest_dir = g_path_get_dirname (destname);
2087         }
2088         else
2089         {
2090                 dest_dir = ghb_get_user_config_dir("EncodeLogs");
2091         }
2092         g_free(destname);
2093         pos = g_strrstr( basename, "." );
2094         if (pos != NULL)
2095         {
2096                 *pos = 0;
2097         }
2098         log_path = g_strdup_printf("%s/%s %d-%02d-%02d %02d-%02d-%02d.log",
2099                 dest_dir,
2100                 basename,
2101                 now->tm_year + 1900, now->tm_mon + 1, now->tm_mday,
2102                 now->tm_hour, now->tm_min, now->tm_sec);
2103         g_free(basename);
2104         g_free(dest_dir);
2105         if (ud->job_activity_log)
2106                 g_io_channel_unref(ud->job_activity_log);
2107         ud->job_activity_log = g_io_channel_new_file (log_path, "w", NULL);
2108         if (ud->job_activity_log)
2109         {
2110                 gchar *ver_str;
2111
2112                 ver_str = g_strdup_printf("Handbrake Version: %s (%d)\n", 
2113                                                                         hb_get_version(NULL), hb_get_build(NULL));
2114                 g_io_channel_write_chars (ud->job_activity_log, ver_str, 
2115                                                                         -1, NULL, NULL);
2116                 g_free(ver_str);
2117         }
2118         g_free(log_path);
2119
2120         path = ghb_settings_get_string( js, "source");
2121         titlenum = ghb_settings_get_int(js, "titlenum");
2122         ghb_backend_queue_scan(path, titlenum);
2123         g_free(path);
2124 }
2125
2126 static gint
2127 queue_pending_count(GValue *queue)
2128 {
2129         gint nn, ii, count;
2130         GValue *js;
2131         gint status;
2132
2133         nn = 0;
2134         count = ghb_array_len(queue);
2135         for (ii = 0; ii < count; ii++)
2136         {
2137
2138                 js = ghb_array_get_nth(queue, ii);
2139                 status = ghb_settings_get_int(js, "job_status");
2140                 if (status == GHB_QUEUE_PENDING)
2141                 {
2142                         nn++;
2143                 }
2144         }
2145         return nn;
2146 }
2147
2148 void
2149 ghb_update_pending(signal_user_data_t *ud)
2150 {
2151         GtkLabel *label;
2152         gint pending;
2153         gchar *str;
2154
2155         label = GTK_LABEL(GHB_WIDGET(ud->builder, "pending_status"));
2156         pending = queue_pending_count(ud->queue);
2157         str = g_strdup_printf("%d encode(s) pending", pending);
2158         gtk_label_set_text(label, str);
2159         g_free(str);
2160 }
2161
2162 GValue* 
2163 ghb_start_next_job(signal_user_data_t *ud, gboolean find_first)
2164 {
2165         static gint current = 0;
2166         gint count, ii, jj;
2167         GValue *js;
2168         gint status;
2169         GtkWidget *prog;
2170
2171         g_debug("start_next_job");
2172         prog = GHB_WIDGET(ud->builder, "progressbar");
2173         gtk_widget_show(prog);
2174
2175         count = ghb_array_len(ud->queue);
2176         if (find_first)
2177         {       // Start the first pending item in the queue
2178                 current = 0;
2179                 for (ii = 0; ii < count; ii++)
2180                 {
2181
2182                         js = ghb_array_get_nth(ud->queue, ii);
2183                         status = ghb_settings_get_int(js, "job_status");
2184                         if (status == GHB_QUEUE_PENDING)
2185                         {
2186                                 current = ii;
2187                                 ghb_inhibit_gsm(ud);
2188                                 queue_scan(ud, js);
2189                                 ghb_update_pending(ud);
2190                                 return js;
2191                         }
2192                 }
2193                 // Nothing pending
2194                 ghb_uninhibit_gsm();
2195                 ghb_notify_done(ud);
2196                 return NULL;
2197         }
2198         // Find the next pending item after the current running item
2199         for (ii = 0; ii < count-1; ii++)
2200         {
2201                 js = ghb_array_get_nth(ud->queue, ii);
2202                 status = ghb_settings_get_int(js, "job_status");
2203                 if (status == GHB_QUEUE_RUNNING)
2204                 {
2205                         for (jj = ii+1; jj < count; jj++)
2206                         {
2207                                 js = ghb_array_get_nth(ud->queue, jj);
2208                                 status = ghb_settings_get_int(js, "job_status");
2209                                 if (status == GHB_QUEUE_PENDING)
2210                                 {
2211                                         current = jj;
2212                                         ghb_inhibit_gsm(ud);
2213                                         queue_scan(ud, js);
2214                                         ghb_update_pending(ud);
2215                                         return js;
2216                                 }
2217                         }
2218                 }
2219         }
2220         // No running item found? Maybe it was deleted
2221         // Look for a pending item starting from the last index we started
2222         for (ii = current; ii < count; ii++)
2223         {
2224                 js = ghb_array_get_nth(ud->queue, ii);
2225                 status = ghb_settings_get_int(js, "job_status");
2226                 if (status == GHB_QUEUE_PENDING)
2227                 {
2228                         current = ii;
2229                         ghb_inhibit_gsm(ud);
2230                         queue_scan(ud, js);
2231                         ghb_update_pending(ud);
2232                         return js;
2233                 }
2234         }
2235         // Nothing found
2236         ghb_uninhibit_gsm();
2237         ghb_notify_done(ud);
2238         ghb_update_pending(ud);
2239         gtk_widget_hide(prog);
2240         return NULL;
2241 }
2242
2243 static gint
2244 find_queue_job(GValue *queue, gint unique_id, GValue **job)
2245 {
2246         GValue *js;
2247         gint ii, count;
2248         gint job_unique_id;
2249         
2250         *job = NULL;
2251         g_debug("find_queue_job");
2252         if (unique_id == 0)  // Invalid Id
2253                 return -1;
2254
2255         count = ghb_array_len(queue);
2256         for (ii = 0; ii < count; ii++)
2257         {
2258                 js = ghb_array_get_nth(queue, ii);
2259                 job_unique_id = ghb_settings_get_int(js, "job_unique_id");
2260                 if (job_unique_id == unique_id)
2261                 {
2262                         *job = js;
2263                         return ii;
2264                 }
2265         }
2266         return -1;
2267 }
2268
2269 gchar*
2270 working_status_string(signal_user_data_t *ud, ghb_instance_status_t *status)
2271 {
2272         gchar *task_str, *job_str, *status_str;
2273         gint qcount;
2274         gint index;
2275         GValue *js;
2276         gboolean subtitle_scan = FALSE;
2277
2278         qcount = ghb_array_len(ud->queue);
2279         index = find_queue_job(ud->queue, status->unique_id, &js);
2280         if (js != NULL)
2281         {
2282                 subtitle_scan = ghb_settings_get_boolean(js, "subtitle_scan");
2283         }
2284         if (qcount > 1)
2285         {
2286                 job_str = g_strdup_printf("job %d of %d, ", index+1, qcount);
2287         }
2288         else
2289         {
2290                 job_str = g_strdup("");
2291         }
2292         if (status->job_count > 1)
2293         {
2294                 if (status->job_cur == 1 && subtitle_scan)
2295                 {
2296                         task_str = g_strdup_printf("pass %d (subtitle scan) of %d, ", 
2297                                 status->job_cur, status->job_count);
2298                 }
2299                 else
2300                 {
2301                         task_str = g_strdup_printf("pass %d of %d, ", 
2302                                 status->job_cur, status->job_count);
2303                 }
2304         }
2305         else
2306         {
2307                 task_str = g_strdup("");
2308         }
2309         if(status->seconds > -1)
2310         {
2311                 status_str= g_strdup_printf(
2312                         "Encoding: %s%s%.2f %%"
2313                         " (%.2f fps, avg %.2f fps, ETA %02dh%02dm%02ds)",
2314                         job_str, task_str,
2315                         100.0 * status->progress,
2316                         status->rate_cur, status->rate_avg, status->hours, 
2317                         status->minutes, status->seconds );
2318         }
2319         else
2320         {
2321                 status_str= g_strdup_printf(
2322                         "Encoding: %s%s%.2f %%",
2323                         job_str, task_str,
2324                         100.0 * status->progress );
2325         }
2326         g_free(task_str);
2327         g_free(job_str);
2328         return status_str;
2329 }
2330
2331 static void
2332 ghb_backend_events(signal_user_data_t *ud)
2333 {
2334         ghb_status_t status;
2335         gchar *status_str;
2336         GtkProgressBar *progress;
2337         GtkLabel       *work_status;
2338         gint titleindex;
2339         GValue *js;
2340         gint index;
2341         GtkTreeView *treeview;
2342         GtkTreeStore *store;
2343         GtkTreeIter iter;
2344         static gint prev_scan_state = 0;
2345         static gint prev_queue_state = 0;
2346         
2347         ghb_track_status();
2348         ghb_get_status(&status);
2349         if (prev_scan_state != status.scan.state ||
2350                 prev_queue_state != status.queue.state)
2351         {
2352                 ghb_queue_buttons_grey(ud);
2353                 prev_scan_state = status.scan.state;
2354                 prev_queue_state = status.queue.state;
2355         }
2356         progress = GTK_PROGRESS_BAR(GHB_WIDGET (ud->builder, "progressbar"));
2357         work_status = GTK_LABEL(GHB_WIDGET (ud->builder, "work_status"));
2358         if (status.scan.state == GHB_STATE_IDLE && 
2359                 status.queue.state == GHB_STATE_IDLE)
2360         {
2361                 static gboolean prev_dvdnav;
2362                 gboolean dvdnav = ghb_settings_get_boolean(ud->settings, "use_dvdnav");
2363                 if (dvdnav != prev_dvdnav)
2364                 {
2365                         hb_dvd_set_dvdnav(dvdnav);
2366                         prev_dvdnav = dvdnav;
2367                 }
2368         }
2369         // First handle the status of title scans
2370         // Then handle the status of the queue
2371         if (status.scan.state & GHB_STATE_SCANNING)
2372         {
2373                 GtkProgressBar *scan_prog;
2374                 GtkLabel *label;
2375
2376                 scan_prog = GTK_PROGRESS_BAR(GHB_WIDGET (ud->builder, "scan_prog"));
2377                 label = GTK_LABEL(GHB_WIDGET (ud->builder, "source_title"));
2378
2379                 if (status.scan.title_cur == 0)
2380                 {
2381                         status_str = g_strdup ("Scanning...");
2382                 }
2383                 else
2384                 {
2385                         status_str = g_strdup_printf ("Scanning title %d of %d...", 
2386                                                           status.scan.title_cur, status.scan.title_count );
2387                 }
2388                 gtk_label_set_text (label, status_str);
2389                 g_free(status_str);
2390                 if (status.scan.title_count > 0)
2391                 {
2392                         gtk_progress_bar_set_fraction (scan_prog, 
2393                                 (gdouble)status.scan.title_cur / status.scan.title_count);
2394                 }
2395         }
2396         else if (status.scan.state & GHB_STATE_SCANDONE)
2397         {
2398                 gchar *source;
2399                 GtkProgressBar *scan_prog;
2400                 GtkLabel *label;
2401
2402                 GtkWidget *widget;
2403                 GtkAction *action;
2404
2405                 widget = GHB_WIDGET(ud->builder, "sourcetoolbutton");
2406                 gtk_widget_set_sensitive(widget, TRUE);
2407                 action = GHB_ACTION(ud->builder, "source_action");
2408                 gtk_action_set_sensitive(action, TRUE);
2409                 action = GHB_ACTION(ud->builder, "source_single_action");
2410                 gtk_action_set_sensitive(action, TRUE);
2411
2412                 source = ghb_settings_get_string(ud->settings, "source");
2413                 update_source_label(ud, source, FALSE);
2414
2415                 scan_prog = GTK_PROGRESS_BAR(GHB_WIDGET (ud->builder, "scan_prog"));
2416                 gtk_progress_bar_set_fraction (scan_prog, 1.0);
2417                 gtk_widget_hide(GTK_WIDGET(scan_prog));
2418
2419                 ghb_title_info_t tinfo;
2420                         
2421                 ghb_update_ui_combo_box(ud, "title", 0, FALSE);
2422                 titleindex = ghb_longest_title();
2423                 ghb_ui_update(ud, "title", ghb_int64_value(titleindex));
2424
2425                 label = GTK_LABEL(GHB_WIDGET (ud->builder, "source_title"));
2426                 // Are there really any titles.
2427                 if (!ghb_get_title_info(&tinfo, titleindex))
2428                 {
2429                         gtk_label_set_text(label, "None");
2430                 }
2431                 ghb_clear_scan_state(GHB_STATE_SCANDONE);
2432                 if (ghb_queue_edit_settings)
2433                 {
2434                         gint jstatus;
2435
2436                         jstatus = ghb_settings_get_int(ghb_queue_edit_settings, "job_status");
2437                         ghb_settings_to_ui(ud, ghb_queue_edit_settings);
2438                         ghb_set_audio(ud, ghb_queue_edit_settings);
2439                         ghb_reset_subtitles(ud, ghb_queue_edit_settings);
2440                         if (jstatus == GHB_QUEUE_PENDING)
2441                         {
2442                                 ghb_value_free(ghb_queue_edit_settings);
2443                         }
2444                         ghb_queue_edit_settings = NULL;
2445                 }
2446         }
2447
2448         if (status.queue.state & GHB_STATE_SCANNING)
2449         {
2450                 // This needs to be in scanning and working since scanning
2451                 // happens fast enough that it can be missed
2452                 gtk_label_set_text (work_status, "Scanning ...");
2453                 gtk_progress_bar_set_fraction (progress, 0);
2454         }
2455         else if (status.queue.state & GHB_STATE_SCANDONE)
2456         {
2457                 ghb_clear_queue_state(GHB_STATE_SCANDONE);
2458                 usleep(2000000);
2459                 submit_job(ud->current_job);
2460                 ghb_update_pending(ud);
2461         }
2462         else if (status.queue.state & GHB_STATE_PAUSED)
2463         {
2464                 gtk_label_set_text (work_status, "Paused");
2465         }
2466         else if (status.queue.state & GHB_STATE_WORKING)
2467         {
2468                 static gint working = 0;
2469
2470                 // This needs to be in scanning and working since scanning
2471                 // happens fast enough that it can be missed
2472                 index = find_queue_job(ud->queue, status.queue.unique_id, &js);
2473                 if (status.queue.unique_id != 0 && index >= 0)
2474                 {
2475                         gchar working_icon[] = "hb-working0";
2476                         working_icon[10] = '0' + working;
2477                         working = (working+1) % 6;
2478                         treeview = GTK_TREE_VIEW(GHB_WIDGET(ud->builder, "queue_list"));
2479                         store = GTK_TREE_STORE(gtk_tree_view_get_model(treeview));
2480                         gchar *path = g_strdup_printf ("%d", index);
2481                         if (gtk_tree_model_get_iter_from_string(
2482                                         GTK_TREE_MODEL(store), &iter, path))
2483                         {
2484                                 gtk_tree_store_set(store, &iter, 0, working_icon, -1);
2485                         }
2486                         g_free(path);
2487                 }
2488                 GtkLabel *label;
2489                 gchar *status_str;
2490
2491                 status_str = working_status_string(ud, &status.queue);
2492                 label = GTK_LABEL(GHB_WIDGET(ud->builder, "queue_status"));
2493                 gtk_label_set_text (label, status_str);
2494 #if !GTK_CHECK_VERSION(2, 16, 0)
2495                 GtkStatusIcon *si;
2496
2497                 si = GTK_STATUS_ICON(GHB_OBJECT(ud->builder, "hb_status"));
2498                 gtk_status_icon_set_tooltip(si, status_str);
2499 #endif
2500                 gtk_label_set_text (work_status, status_str);
2501                 gtk_progress_bar_set_fraction (progress, status.queue.progress);
2502                 g_free(status_str);
2503         }
2504         else if (status.queue.state & GHB_STATE_WORKDONE)
2505         {
2506                 gint qstatus;
2507
2508                 index = find_queue_job(ud->queue, status.queue.unique_id, &js);
2509                 treeview = GTK_TREE_VIEW(GHB_WIDGET(ud->builder, "queue_list"));
2510                 store = GTK_TREE_STORE(gtk_tree_view_get_model(treeview));
2511                 if (ud->cancel_encode == GHB_CANCEL_ALL || 
2512                         ud->cancel_encode == GHB_CANCEL_CURRENT)
2513                         status.queue.error = GHB_ERROR_CANCELED;
2514                 switch( status.queue.error )
2515                 {
2516                         case GHB_ERROR_NONE:
2517                                 gtk_label_set_text (work_status, "Rip Done!");
2518                                 qstatus = GHB_QUEUE_DONE;
2519                                 if (js != NULL)
2520                                 {
2521                                         gchar *path = g_strdup_printf ("%d", index);
2522                                         if (gtk_tree_model_get_iter_from_string(
2523                                                         GTK_TREE_MODEL(store), &iter, path))
2524                                         {
2525                                                 gtk_tree_store_set(store, &iter, 0, "hb-complete", -1);
2526                                         }
2527                                         g_free(path);
2528                                 }
2529                                 break;
2530                         case GHB_ERROR_CANCELED:
2531                                 gtk_label_set_text (work_status, "Rip Canceled.");
2532                                 qstatus = GHB_QUEUE_CANCELED;
2533                                 if (js != NULL)
2534                                 {
2535                                         gchar *path = g_strdup_printf ("%d", index);
2536                                         if (gtk_tree_model_get_iter_from_string(
2537                                                         GTK_TREE_MODEL(store), &iter, path))
2538                                         {
2539                                                 gtk_tree_store_set(store, &iter, 0, "hb-canceled", -1);
2540                                         }
2541                                         g_free(path);
2542                                 }
2543                                 break;
2544                         default:
2545                                 gtk_label_set_text (work_status, "Rip Failed.");
2546                                 qstatus = GHB_QUEUE_CANCELED;
2547                                 if (js != NULL)
2548                                 {
2549                                         gchar *path = g_strdup_printf ("%d", index);
2550                                         if (gtk_tree_model_get_iter_from_string(
2551                                                         GTK_TREE_MODEL(store), &iter, path))
2552                                         {
2553                                                 gtk_tree_store_set(store, &iter, 0, "hb-canceled", -1);
2554                                         }
2555                                         g_free(path);
2556                                 }
2557                 }
2558                 gtk_progress_bar_set_fraction (progress, 1.0);
2559                 ghb_clear_queue_state(GHB_STATE_WORKDONE);
2560                 if (ud->job_activity_log)
2561                         g_io_channel_unref(ud->job_activity_log);
2562                 ud->job_activity_log = NULL;
2563                 if (ud->cancel_encode != GHB_CANCEL_ALL &&
2564                         ud->cancel_encode != GHB_CANCEL_FINISH)
2565                 {
2566                         ud->current_job = ghb_start_next_job(ud, FALSE);
2567                 }
2568                 else
2569                 {
2570                         ghb_uninhibit_gsm();
2571                         ud->current_job = NULL;
2572                         gtk_widget_hide(GTK_WIDGET(progress));
2573                 }
2574                 if (js)
2575                         ghb_settings_set_int(js, "job_status", qstatus);
2576                 ghb_save_queue(ud->queue);
2577                 ud->cancel_encode = GHB_CANCEL_NONE;
2578 #if !GTK_CHECK_VERSION(2, 16, 0)
2579                 GtkStatusIcon *si;
2580
2581                 si = GTK_STATUS_ICON(GHB_OBJECT(ud->builder, "hb_status"));
2582                 gtk_status_icon_set_tooltip(si, "HandBrake");
2583 #endif
2584         }
2585         else if (status.queue.state & GHB_STATE_MUXING)
2586         {
2587                 gtk_label_set_text (work_status, "Muxing: This may take a while...");
2588         }
2589
2590         if (status.scan.state & GHB_STATE_WORKING)
2591         {
2592                 GtkProgressBar *live_progress;
2593                 live_progress = GTK_PROGRESS_BAR(
2594                         GHB_WIDGET(ud->builder, "live_encode_progress"));
2595                 status_str = working_status_string(ud, &status.scan);
2596                 gtk_progress_bar_set_text (live_progress, status_str);
2597                 gtk_progress_bar_set_fraction (live_progress, status.scan.progress);
2598                 g_free(status_str);
2599         }
2600         if (status.scan.state & GHB_STATE_WORKDONE)
2601         {
2602                 switch( status.scan.error )
2603                 {
2604                         case GHB_ERROR_NONE:
2605                         {
2606                                 ghb_live_encode_done(ud, TRUE);
2607                         } break;
2608                         default:
2609                         {
2610                                 ghb_live_encode_done(ud, FALSE);
2611                         } break;
2612                 }
2613                 ghb_clear_scan_state(GHB_STATE_WORKDONE);
2614         }
2615 }
2616
2617 #if GTK_CHECK_VERSION(2, 16, 0)
2618 G_MODULE_EXPORT gboolean
2619 status_icon_query_tooltip_cb(
2620         GtkStatusIcon *si,
2621         gint           x,
2622         gint           y,
2623         gboolean       kbd_mode,
2624         GtkTooltip    *tt,
2625         signal_user_data_t *ud)
2626 {
2627         ghb_status_t status;
2628         gchar *status_str;
2629
2630         ghb_get_status(&status);
2631         if (status.queue.state & GHB_STATE_WORKING)
2632                 status_str = working_status_string(ud, &status.queue);
2633         else if (status.queue.state & GHB_STATE_WORKDONE)
2634                 status_str = g_strdup("Encode Complete");
2635         else
2636                 status_str = g_strdup("HandBrake");
2637
2638         gtk_tooltip_set_text(tt, status_str);
2639         gtk_tooltip_set_icon_from_icon_name(tt, "hb-icon", GTK_ICON_SIZE_BUTTON);
2640         g_free(status_str);
2641         return TRUE;
2642 }
2643 #endif
2644
2645 G_MODULE_EXPORT gboolean
2646 ghb_timer_cb(gpointer data)
2647 {
2648         signal_user_data_t *ud = (signal_user_data_t*)data;
2649
2650         ghb_live_preview_progress(ud);
2651         ghb_backend_events(ud);
2652         if (update_default_destination)
2653         {
2654                 gchar *dest, *dest_dir, *def_dest;
2655                 dest = ghb_settings_get_string(ud->settings, "destination");
2656                 dest_dir = g_path_get_dirname (dest);
2657                 def_dest = ghb_settings_get_string(ud->settings, "destination_dir");
2658                 if (strcmp(dest_dir, def_dest) != 0)
2659                 {
2660                         ghb_settings_set_string (ud->settings, "destination_dir", dest_dir);
2661                         ghb_pref_save (ud->settings, "destination_dir");
2662                 }
2663                 g_free(dest);
2664                 g_free(dest_dir);
2665                 g_free(def_dest);
2666                 update_default_destination = FALSE;
2667         }
2668         if (update_preview)
2669         {
2670                 g_debug("Updating preview\n");
2671                 ghb_set_preview_image (ud);
2672                 update_preview = FALSE;
2673         }
2674
2675         if (!appcast_busy)
2676         {
2677                 gchar *updates;
2678                 updates = ghb_settings_get_string(ud->settings, "check_updates");
2679                 gint64 duration = 0;
2680                 if (strcmp(updates, "daily") == 0)
2681                         duration = 60 * 60 * 24;
2682                 else if (strcmp(updates, "weekly") == 0)
2683                         duration = 60 * 60 * 24 * 7;
2684                 else if (strcmp(updates, "monthly") == 0)
2685                         duration = 60 * 60 * 24 * 7;
2686
2687                 g_free(updates);
2688                 if (duration != 0)
2689                 {
2690                         gint64 last;
2691                         time_t tt;
2692
2693                         last = ghb_settings_get_int64(ud->settings, "last_update_check");
2694                         time(&tt);
2695                         if (last + duration < tt)
2696                         {
2697                                 ghb_settings_set_int64(ud->settings, 
2698                                                                                 "last_update_check", tt);
2699                                 ghb_pref_save(ud->settings, "last_update_check");
2700                                 g_thread_create((GThreadFunc)ghb_check_update, ud, 
2701                                                                 FALSE, NULL);
2702                         }
2703                 }
2704         }
2705         return TRUE;
2706 }
2707
2708 G_MODULE_EXPORT gboolean
2709 ghb_log_cb(GIOChannel *source, GIOCondition cond, gpointer data)
2710 {
2711         gchar *text = NULL;
2712         gsize length, outlength;
2713         GtkTextView *textview;
2714         GtkTextBuffer *buffer;
2715         GtkTextIter iter;
2716         GtkTextMark *mark;
2717         GError *gerror = NULL;
2718         GIOStatus status;
2719         
2720         signal_user_data_t *ud = (signal_user_data_t*)data;
2721
2722         status = g_io_channel_read_line (source, &text, &length, NULL, &gerror);
2723         // Trim nils from end of text, they cause g_io_channel_write_chars to
2724         // fail with an assertion that aborts
2725         while (length > 0 && text[length-1] == 0)
2726                 length--;
2727         if (text != NULL && length > 0)
2728         {
2729                 GdkWindow *window;
2730                 gint width, height;
2731                 gint x, y;
2732                 gboolean bottom = FALSE;
2733
2734                 textview = GTK_TEXT_VIEW(GHB_WIDGET (ud->builder, "activity_view"));
2735                 buffer = gtk_text_view_get_buffer (textview);
2736                 // I would like to auto-scroll the window when the scrollbar
2737                 // is at the bottom, 
2738                 // must determine whether the insert point is at
2739                 // the bottom of the window 
2740                 window = gtk_text_view_get_window(textview, GTK_TEXT_WINDOW_TEXT);
2741                 if (window != NULL)
2742                 {
2743                         gdk_drawable_get_size(GDK_DRAWABLE(window), &width, &height);
2744                         gtk_text_view_window_to_buffer_coords(textview, 
2745                                 GTK_TEXT_WINDOW_TEXT, width, height, &x, &y);
2746                         gtk_text_view_get_iter_at_location(textview, &iter, x, y);
2747                         if (gtk_text_iter_is_end(&iter))
2748                         {
2749                                 bottom = TRUE;
2750                         }
2751                 }
2752                 else
2753                 {
2754                         // If the window isn't available, assume bottom
2755                         bottom = TRUE;
2756                 }
2757                 gtk_text_buffer_get_end_iter(buffer, &iter);
2758                 gtk_text_buffer_insert(buffer, &iter, text, -1);
2759                 if (bottom)
2760                 {
2761                         gtk_text_buffer_get_end_iter(buffer, &iter);
2762                         mark = gtk_text_buffer_create_mark(buffer, NULL, &iter, FALSE);
2763                         gtk_text_view_scroll_mark_onscreen(textview, mark);
2764                         gtk_text_buffer_delete_mark(buffer, mark);
2765                 }
2766 #if defined(_WIN32)
2767                 gsize one = 1;
2768                 text[length-1] = '\r';
2769 #endif
2770                 g_io_channel_write_chars (ud->activity_log, text, 
2771                                                                 length, &outlength, NULL);
2772 #if defined(_WIN32)
2773                 g_io_channel_write_chars (ud->activity_log, "\n", 
2774                                                                 one, &one, NULL);
2775 #endif
2776                 g_io_channel_flush(ud->activity_log, NULL);
2777                 if (ud->job_activity_log)
2778                 {
2779                         g_io_channel_write_chars (ud->job_activity_log, text, 
2780                                                                         length, &outlength, NULL);
2781 #if defined(_WIN32)
2782                         g_io_channel_write_chars (ud->activity_log, "\n", 
2783                                                                         one, &outlength, NULL);
2784 #endif
2785                         g_io_channel_flush(ud->job_activity_log, NULL);
2786                 }
2787         }
2788         if (text != NULL)
2789                 g_free(text);
2790
2791         if (status != G_IO_STATUS_NORMAL)
2792         {
2793                 // This should never happen, but if it does I would get into an
2794                 // infinite loop.  Returning false removes this callback.
2795                 g_warning("Error while reading activity from pipe");
2796                 if (gerror != NULL)
2797                 {
2798                         g_warning("%s", gerror->message);
2799                         g_error_free (gerror);
2800                 }
2801                 return FALSE;
2802         }
2803         if (gerror != NULL)
2804                 g_error_free (gerror);
2805         return TRUE;
2806 }
2807
2808 static void
2809 set_visible(GtkWidget *widget, gboolean visible)
2810 {
2811         if (visible)
2812         {
2813                 gtk_widget_show_now(widget);
2814         }
2815         else
2816         {
2817                 gtk_widget_hide(widget);
2818         }
2819 }
2820
2821 G_MODULE_EXPORT void
2822 show_activity_clicked_cb(GtkWidget *xwidget, signal_user_data_t *ud)
2823 {
2824         GtkWidget *widget = GHB_WIDGET (ud->builder, "activity_window");
2825         set_visible(widget, gtk_toggle_tool_button_get_active(
2826                                                 GTK_TOGGLE_TOOL_BUTTON(xwidget)));
2827 }
2828
2829 G_MODULE_EXPORT void
2830 show_activity_menu_clicked_cb(GtkWidget *xwidget, signal_user_data_t *ud)
2831 {
2832         GtkWidget *widget = GHB_WIDGET (ud->builder, "activity_window");
2833         set_visible(widget, TRUE);
2834         widget = GHB_WIDGET (ud->builder, "show_activity");
2835         gtk_toggle_tool_button_set_active(GTK_TOGGLE_TOOL_BUTTON(widget), TRUE);
2836 }
2837
2838 G_MODULE_EXPORT gboolean
2839 activity_window_delete_cb(GtkWidget *xwidget, GdkEvent *event, signal_user_data_t *ud)
2840 {
2841         set_visible(xwidget, FALSE);
2842         GtkWidget *widget = GHB_WIDGET (ud->builder, "show_activity");
2843         gtk_toggle_tool_button_set_active(GTK_TOGGLE_TOOL_BUTTON(widget), FALSE);
2844         return TRUE;
2845 }
2846
2847 void
2848 ghb_log(gchar *log, ...)
2849 {
2850         va_list args;
2851         time_t _now;
2852     struct tm *now;
2853         gchar fmt[362];
2854
2855         _now = time(NULL);
2856         now = localtime( &_now );
2857         snprintf(fmt, 362, "[%02d:%02d:%02d] gtkgui: %s\n", 
2858                         now->tm_hour, now->tm_min, now->tm_sec, log);
2859         va_start(args, log);
2860         vfprintf(stderr, fmt, args);
2861         va_end(args);
2862 }
2863
2864 static void
2865 browse_url(const gchar *url)
2866 {
2867         gboolean result;
2868         char *argv[] = 
2869                 {"xdg-open",NULL,NULL,NULL};
2870         argv[1] = (gchar*)url;
2871         result = g_spawn_async(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL,
2872                                 NULL, NULL, NULL);
2873         if (result) return;
2874
2875         argv[0] = "gnome-open";
2876         result = g_spawn_async(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL,
2877                                 NULL, NULL, NULL);
2878         if (result) return;
2879
2880         argv[0] = "kfmclient";
2881         argv[1] = "exec";
2882         argv[2] = "http://trac.handbrake.fr/wiki/HandBrakeGuide";
2883         result = g_spawn_async(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL,
2884                                 NULL, NULL, NULL);
2885         if (result) return;
2886
2887         argv[0] = "firefox";
2888         argv[1] = "http://trac.handbrake.fr/wiki/HandBrakeGuide";
2889         argv[2] = NULL;
2890         result = g_spawn_async(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL,
2891                                 NULL, NULL, NULL);
2892 }
2893
2894 void
2895 about_web_hook(GtkAboutDialog *about, const gchar *link, gpointer data)
2896 {
2897         browse_url(link);
2898 }
2899
2900 G_MODULE_EXPORT void
2901 about_activate_cb(GtkWidget *xwidget, signal_user_data_t *ud)
2902 {
2903         GtkWidget *widget = GHB_WIDGET (ud->builder, "hb_about");
2904         gchar *ver;
2905
2906         ver = g_strdup_printf("%s (%s)", HB_PROJECT_VERSION, HB_PROJECT_BUILD_ARCH);
2907         gtk_about_dialog_set_url_hook(about_web_hook, NULL, NULL);
2908         gtk_about_dialog_set_version(GTK_ABOUT_DIALOG(widget), ver);
2909         g_free(ver);
2910         gtk_about_dialog_set_website(GTK_ABOUT_DIALOG(widget), 
2911                                                                 HB_PROJECT_URL_WEBSITE);
2912         gtk_about_dialog_set_website_label(GTK_ABOUT_DIALOG(widget), 
2913                                                                                 HB_PROJECT_URL_WEBSITE);
2914         gtk_widget_show (widget);
2915 }
2916
2917 G_MODULE_EXPORT void
2918 guide_activate_cb(GtkWidget *xwidget, signal_user_data_t *ud)
2919 {
2920         browse_url("http://trac.handbrake.fr/wiki/HandBrakeGuide");
2921 }
2922
2923 G_MODULE_EXPORT void
2924 hb_about_response_cb(GtkWidget *widget, gint response, signal_user_data_t *ud)
2925 {
2926         gtk_widget_hide (widget);
2927 }
2928
2929 G_MODULE_EXPORT void
2930 show_queue_clicked_cb(GtkWidget *xwidget, signal_user_data_t *ud)
2931 {
2932         GtkWidget *widget = GHB_WIDGET (ud->builder, "queue_window");
2933         set_visible(widget, gtk_toggle_tool_button_get_active(
2934                                                 GTK_TOGGLE_TOOL_BUTTON(xwidget)));
2935 }
2936
2937 G_MODULE_EXPORT void
2938 show_queue_menu_clicked_cb(GtkWidget *xwidget, signal_user_data_t *ud)
2939 {
2940         GtkWidget *widget = GHB_WIDGET (ud->builder, "queue_window");
2941         set_visible(widget, TRUE);
2942         widget = GHB_WIDGET (ud->builder, "show_queue");
2943         gtk_toggle_tool_button_set_active(GTK_TOGGLE_TOOL_BUTTON(widget), TRUE);
2944 }
2945
2946 G_MODULE_EXPORT gboolean
2947 queue_window_delete_cb(GtkWidget *xwidget, GdkEvent *event, signal_user_data_t *ud)
2948 {
2949         set_visible(xwidget, FALSE);
2950         GtkWidget *widget = GHB_WIDGET (ud->builder, "show_queue");
2951         gtk_toggle_tool_button_set_active(GTK_TOGGLE_TOOL_BUTTON(widget), FALSE);
2952         return TRUE;
2953 }
2954
2955 G_MODULE_EXPORT void
2956 show_presets_toggled_cb(GtkWidget *action, signal_user_data_t *ud)
2957 {
2958         GtkWidget *widget;
2959         GtkWindow *hb_window;
2960         
2961         g_debug("show_presets_clicked_cb ()");
2962         widget = GHB_WIDGET (ud->builder, "presets_frame");
2963         ghb_widget_to_setting(ud->settings, action);
2964         if (ghb_settings_get_boolean(ud->settings, "show_presets"))
2965         {
2966                 gtk_widget_show_now(widget);
2967         }
2968         else
2969         {
2970                 gtk_widget_hide(widget);
2971                 hb_window = GTK_WINDOW(GHB_WIDGET (ud->builder, "hb_window"));
2972                 gtk_window_resize(hb_window, 16, 16);
2973         }
2974         ghb_pref_save(ud->settings, "show_presets");
2975 }
2976
2977 static void
2978 update_chapter_list(signal_user_data_t *ud)
2979 {
2980         GtkTreeView *treeview;
2981         GtkTreeIter iter;
2982         GtkListStore *store;
2983         gboolean done;
2984         GValue *chapters;
2985         gint titleindex, ii;
2986         gint count;
2987         
2988         g_debug("update_chapter_list ()");
2989         titleindex = ghb_settings_combo_int(ud->settings, "title");
2990         chapters = ghb_get_chapters(titleindex);
2991         count = ghb_array_len(chapters);
2992         if (chapters)
2993                 ghb_settings_set_value(ud->settings, "chapter_list", chapters);
2994         
2995         treeview = GTK_TREE_VIEW(GHB_WIDGET(ud->builder, "chapters_list"));
2996         store = GTK_LIST_STORE(gtk_tree_view_get_model(treeview));
2997         ii = 0;
2998         if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter))
2999         {
3000                 do
3001                 {
3002
3003                         if (ii < count)
3004                         {
3005                                 gchar *chapter, *duration;
3006                                 gint hh, mm, ss;
3007
3008                                 // Update row with settings data
3009                                 g_debug("Updating row");
3010                                 chapter = ghb_value_string(ghb_array_get_nth(chapters, ii));
3011                                 ghb_get_chapter_duration(titleindex, ii, &hh, &mm, &ss);
3012                                 duration = g_strdup_printf("%02d:%02d:%02d", hh, mm, ss);
3013                                 gtk_list_store_set(store, &iter, 
3014                                         0, ii+1,
3015                                         1, duration,
3016                                         2, chapter,
3017                                         3, TRUE,
3018                                         -1);
3019                                 g_free(chapter);
3020                                 g_free(duration);
3021                                 ii++;
3022                                 done = !gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &iter);
3023                         }
3024                         else
3025                         {
3026                                 // No more settings data, remove row
3027                                 g_debug("Removing row");
3028                                 done = !gtk_list_store_remove(store, &iter);
3029                         }
3030                 } while (!done);
3031         }
3032         while (ii < count)
3033         {
3034                 gchar *chapter, *duration;
3035                 gint hh, mm, ss;
3036
3037                 // Additional settings, add row
3038                 g_debug("Adding row");
3039                 chapter = ghb_value_string(ghb_array_get_nth(chapters, ii));
3040                 ghb_get_chapter_duration(titleindex, ii, &hh, &mm, &ss);
3041                 duration = g_strdup_printf("%02d:%02d:%02d", hh, mm, ss);
3042                 gtk_list_store_append(store, &iter);
3043                 gtk_list_store_set(store, &iter, 
3044                         0, ii+1,
3045                         1, duration,
3046                         2, chapter,
3047                         3, TRUE,
3048                         -1);
3049                 g_free(chapter);
3050                 g_free(duration);
3051                 ii++;
3052         }
3053 }
3054
3055 static gint chapter_edit_key = 0;
3056
3057 G_MODULE_EXPORT gboolean
3058 chapter_keypress_cb(
3059         GhbCellRendererText *cell,
3060         GdkEventKey *event,
3061         signal_user_data_t *ud)
3062 {
3063         chapter_edit_key = event->keyval;
3064         return FALSE;
3065 }
3066
3067 G_MODULE_EXPORT void
3068 chapter_edited_cb(
3069         GhbCellRendererText *cell, 
3070         gchar *path, 
3071         gchar *text, 
3072         signal_user_data_t *ud)
3073 {
3074         GtkTreePath *treepath;
3075         GtkListStore *store;
3076         GtkTreeView *treeview;
3077         GtkTreeIter iter;
3078         gint index;
3079         gint *pi;
3080         gint row;
3081         
3082         g_debug("chapter_edited_cb ()");
3083         g_debug("path (%s)", path);
3084         g_debug("text (%s)", text);
3085         treeview = GTK_TREE_VIEW(GHB_WIDGET(ud->builder, "chapters_list"));
3086         store = GTK_LIST_STORE(gtk_tree_view_get_model(treeview));
3087         treepath = gtk_tree_path_new_from_string (path);
3088         pi = gtk_tree_path_get_indices(treepath);
3089         row = pi[0];
3090         gtk_tree_model_get_iter(GTK_TREE_MODEL(store), &iter, treepath);
3091         gtk_list_store_set(store, &iter, 
3092                 2, text,
3093                 3, TRUE,
3094                 -1);
3095         gtk_tree_model_get(GTK_TREE_MODEL(store), &iter, 0, &index, -1);
3096
3097         const GValue *chapters;
3098         GValue *chapter;
3099
3100         chapters = ghb_settings_get_value(ud->settings, "chapter_list");
3101         chapter = ghb_array_get_nth(chapters, index-1);
3102         g_value_set_string(chapter, text);
3103         if ((chapter_edit_key == GDK_Return || chapter_edit_key == GDK_Down) &&
3104                 gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &iter))
3105         {
3106                 GtkTreeViewColumn *column;
3107
3108                 gtk_tree_path_next(treepath);
3109                 // When a cell has been edited, I want to advance to the
3110                 // next cell and start editing it automaitcally.
3111                 // Unfortunately, we may not be in a state here where
3112                 // editing is allowed.  This happens when the user selects
3113                 // a new cell with the mouse instead of just hitting enter.
3114                 // Some kind of Gtk quirk.  widget_editable==NULL assertion.
3115                 // Editing is enabled again once the selection event has been
3116                 // processed.  So I'm queueing up a callback to be called
3117                 // when things go idle.  There, I will advance to the next
3118                 // cell and initiate editing.
3119                 //
3120                 // Now, you might be asking why I don't catch the keypress
3121                 // event and determine what action to take based on that.
3122                 // The Gtk developers in their infinite wisdom have made the 
3123                 // actual GtkEdit widget being used a private member of
3124                 // GtkCellRendererText, so it can not be accessed to hang a
3125                 // signal handler off of.  And they also do not propagate the
3126                 // keypress signals in any other way.  So that information is lost.
3127                 //g_idle_add((GSourceFunc)next_cell, ud);
3128                 //
3129                 // Keeping the above comment for posterity.
3130                 // I got industrious and made my own CellTextRendererText that
3131                 // passes on the key-press-event. So now I have much better
3132                 // control of this.
3133                 column = gtk_tree_view_get_column(treeview, 2);
3134                 gtk_tree_view_set_cursor(treeview, treepath, column, TRUE);
3135         }
3136         else if (chapter_edit_key == GDK_Up && row > 0)
3137         {
3138                 GtkTreeViewColumn *column;
3139                 gtk_tree_path_prev(treepath);
3140                 column = gtk_tree_view_get_column(treeview, 2);
3141                 gtk_tree_view_set_cursor(treeview, treepath, column, TRUE);
3142         }
3143         gtk_tree_path_free (treepath);
3144 }
3145
3146 void
3147 debug_log_handler(const gchar *domain, GLogLevelFlags flags, const gchar *msg, gpointer data)
3148 {
3149         signal_user_data_t *ud = (signal_user_data_t*)data;
3150         
3151         if (ud->debug)
3152         {
3153                 printf("%s: %s\n", domain, msg);
3154         }
3155 }
3156
3157 void
3158 warn_log_handler(const gchar *domain, GLogLevelFlags flags, const gchar *msg, gpointer data)
3159 {
3160         printf("%s: %s\n", domain, msg);
3161 }
3162
3163 void
3164 ghb_hbfd(signal_user_data_t *ud, gboolean hbfd)
3165 {
3166         GtkWidget *widget;
3167         g_debug("ghb_hbfd");
3168         widget = GHB_WIDGET(ud->builder, "queue_pause1");
3169         set_visible(widget, !hbfd);
3170         widget = GHB_WIDGET(ud->builder, "queue_add");
3171         set_visible(widget, !hbfd);
3172         widget = GHB_WIDGET(ud->builder, "show_queue");
3173         set_visible(widget, !hbfd);
3174         widget = GHB_WIDGET(ud->builder, "show_activity");
3175         set_visible(widget, !hbfd);
3176
3177         widget = GHB_WIDGET(ud->builder, "chapter_box");
3178         set_visible(widget, !hbfd);
3179         widget = GHB_WIDGET(ud->builder, "container_box");
3180         set_visible(widget, !hbfd);
3181         widget = GHB_WIDGET(ud->builder, "settings_box");
3182         set_visible(widget, !hbfd);
3183         widget = GHB_WIDGET(ud->builder, "presets_save");
3184         set_visible(widget, !hbfd);
3185         widget = GHB_WIDGET(ud->builder, "presets_remove");
3186         set_visible(widget, !hbfd);
3187         widget = GHB_WIDGET (ud->builder, "hb_window");
3188         gtk_window_resize(GTK_WINDOW(widget), 16, 16);
3189
3190 }
3191
3192 G_MODULE_EXPORT void
3193 hbfd_toggled_cb(GtkWidget *widget, signal_user_data_t *ud)
3194 {
3195         g_debug("hbfd_toggled_cb");
3196         ghb_widget_to_setting (ud->settings, widget);
3197         gboolean hbfd = ghb_settings_get_boolean(ud->settings, "hbfd");
3198         ghb_hbfd(ud, hbfd);
3199         ghb_pref_save(ud->settings, "hbfd");
3200 }
3201
3202 G_MODULE_EXPORT void
3203 pref_changed_cb(GtkWidget *widget, signal_user_data_t *ud)
3204 {
3205         g_debug("pref_changed_cb");
3206         ghb_widget_to_setting (ud->settings, widget);
3207         ghb_check_dependency(ud, widget);
3208         const gchar *name = gtk_widget_get_name(widget);
3209         ghb_pref_save(ud->settings, name);
3210 }
3211
3212 G_MODULE_EXPORT void
3213 use_m4v_changed_cb(GtkWidget *widget, signal_user_data_t *ud)
3214 {
3215         g_debug("use_m4v_changed_cb");
3216         ghb_widget_to_setting (ud->settings, widget);
3217         ghb_check_dependency(ud, widget);
3218         const gchar *name = gtk_widget_get_name(widget);
3219         ghb_pref_save(ud->settings, name);
3220         ghb_update_destination_extension(ud);
3221 }
3222
3223 G_MODULE_EXPORT void
3224 show_status_cb(GtkWidget *widget, signal_user_data_t *ud)
3225 {
3226         g_debug("show_status_cb");
3227         ghb_widget_to_setting (ud->settings, widget);
3228         ghb_check_dependency(ud, widget);
3229         const gchar *name = gtk_widget_get_name(widget);
3230         ghb_pref_save(ud->settings, name);
3231
3232         GtkStatusIcon *si;
3233
3234         si = GTK_STATUS_ICON(GHB_OBJECT (ud->builder, "hb_status"));
3235         gtk_status_icon_set_visible(si,
3236                         ghb_settings_get_boolean(ud->settings, "show_status"));
3237 }
3238
3239 G_MODULE_EXPORT void
3240 vqual_granularity_changed_cb(GtkWidget *widget, signal_user_data_t *ud)
3241 {
3242         g_debug("vqual_granularity_changed_cb");
3243         ghb_widget_to_setting (ud->settings, widget);
3244         ghb_check_dependency(ud, widget);
3245
3246         const gchar *name = gtk_widget_get_name(widget);
3247         ghb_pref_save(ud->settings, name);
3248
3249         gdouble vqmin, vqmax, step, page;
3250         gboolean inverted;
3251         gint digits;
3252
3253         ghb_vquality_range(ud, &vqmin, &vqmax, &step, &page, &digits, &inverted);
3254         GtkWidget *qp = GHB_WIDGET(ud->builder, "VideoQualitySlider");
3255         gtk_range_set_increments (GTK_RANGE(qp), step, page);
3256 }
3257
3258 G_MODULE_EXPORT void
3259 tweaks_changed_cb(GtkWidget *widget, signal_user_data_t *ud)
3260 {
3261         g_debug("tweaks_changed_cb");
3262         ghb_widget_to_setting (ud->settings, widget);
3263         const gchar *name = gtk_widget_get_name(widget);
3264         ghb_pref_save(ud->settings, name);
3265 }
3266
3267 G_MODULE_EXPORT void
3268 hbfd_feature_changed_cb(GtkWidget *widget, signal_user_data_t *ud)
3269 {
3270         g_debug("hbfd_feature_changed_cb");
3271         ghb_widget_to_setting (ud->settings, widget);
3272         const gchar *name = gtk_widget_get_name(widget);
3273         ghb_pref_save(ud->settings, name);
3274
3275         gboolean hbfd = ghb_settings_get_boolean(ud->settings, "hbfd_feature");
3276         GtkAction *action;
3277         if (hbfd)
3278         {
3279                 const GValue *val;
3280                 val = ghb_settings_get_value(ud->settings, "hbfd");
3281                 ghb_ui_update(ud, "hbfd", val);
3282         }
3283         action = GHB_ACTION (ud->builder, "hbfd");
3284         gtk_action_set_visible(action, hbfd);
3285 }
3286
3287 gboolean
3288 ghb_file_menu_add_dvd(signal_user_data_t *ud)
3289 {
3290         GList *link, *drives;
3291         static GtkActionGroup *agroup = NULL;
3292         static gint merge_id;
3293
3294         g_debug("ghb_file_menu_add_dvd()");
3295         link = drives = dvd_device_list();
3296         if (drives != NULL)
3297         {
3298                 GtkUIManager *ui = GTK_UI_MANAGER(
3299                         gtk_builder_get_object(ud->builder, "uimanager1"));
3300
3301                 if (agroup == NULL)
3302                 {
3303                         agroup = gtk_action_group_new("dvdgroup");
3304                         gtk_ui_manager_insert_action_group(ui, agroup, 0);
3305                 }
3306                 else
3307                         gtk_ui_manager_remove_ui(ui, merge_id);
3308
3309                 merge_id = gtk_ui_manager_new_merge_id(ui);
3310                 // Add separator
3311                 gtk_ui_manager_add_ui(ui, merge_id, 
3312                         "ui/menubar1/menuitem1/quit1", "dvdsep", NULL,
3313                         GTK_UI_MANAGER_SEPARATOR, TRUE);
3314
3315                 while (link != NULL)
3316                 {
3317                         GtkAction *action;
3318                         gchar *drive = get_dvd_device_name(link->data);
3319                         gchar *name = get_dvd_volume_name(link->data);
3320                 
3321                         action = gtk_action_group_get_action(agroup, drive);
3322                         if (action != NULL)
3323                         {
3324                                 gtk_action_group_remove_action(agroup, action);
3325                                 g_object_unref(G_OBJECT(action));
3326                         }
3327                         // Create action for this drive
3328                         action = gtk_action_new(drive, name,
3329                                 "Scan this DVD source", "gtk-cdrom");
3330                         // Add action to action group
3331                         gtk_action_group_add_action_with_accel(agroup, action, NULL);
3332                         // Add to ui manager
3333                         gtk_ui_manager_add_ui(ui, merge_id, 
3334                                 "ui/menubar1/menuitem1/dvdsep", drive, drive,
3335                                 GTK_UI_MANAGER_AUTO, TRUE);
3336                         // Connect signal to action (menu item)
3337                         g_signal_connect(action, "activate", 
3338                                 (GCallback)dvd_source_activate_cb, ud);
3339                         g_free(name);
3340                         g_free(drive);
3341                         free_drive(link->data);
3342                         link = link->next;
3343                 }
3344                 g_list_free(drives);
3345         }
3346         return FALSE;
3347 }
3348
3349 gboolean ghb_is_cd(GDrive *gd);
3350
3351 static GList*
3352 dvd_device_list()
3353 {
3354         GList *dvd_devices = NULL;
3355
3356 #if defined(_WIN32)
3357         gint ii, drives;
3358         gchar drive[5];
3359
3360         strcpy(drive, "A:" G_DIR_SEPARATOR_S);
3361         drives = GetLogicalDrives();
3362         for (ii = 0; ii < 26; ii++)
3363         {
3364                 if (drives & 0x01)
3365                 {
3366                         guint dtype;
3367
3368                         drive[0] = 'A' + ii;
3369                         dtype = GetDriveType(drive);
3370                         if (dtype == DRIVE_CDROM)
3371                         {
3372                                 dvd_devices = g_list_append(dvd_devices, 
3373                                                 (gpointer)g_strdup(drive));
3374                         }
3375                 }
3376                 drives >>= 1;
3377         }
3378 #else
3379         GVolumeMonitor *gvm;
3380         GList *drives, *link;
3381         
3382         gvm = g_volume_monitor_get ();
3383         drives = g_volume_monitor_get_connected_drives (gvm);
3384         link = drives;
3385         while (link != NULL)
3386         {
3387                 GDrive *gd;
3388                 
3389                 gd = (GDrive*)link->data;
3390                 if (ghb_is_cd(gd))
3391                 {
3392                         dvd_devices = g_list_append(dvd_devices, gd);
3393                 }
3394                 else
3395                         g_object_unref (gd);
3396                 link = link->next;
3397         }
3398         g_list_free(drives);
3399 #endif
3400
3401         return dvd_devices;
3402 }
3403
3404 #if !defined(_WIN32)
3405 static LibHalContext *hal_ctx;
3406 #endif
3407
3408 gboolean
3409 ghb_is_cd(GDrive *gd)
3410 {
3411 #if !defined(_WIN32)
3412         gchar *device;
3413         LibHalDrive *halDrive;
3414         LibHalDriveType dtype;
3415
3416         device = g_drive_get_identifier(gd, G_VOLUME_IDENTIFIER_KIND_UNIX_DEVICE);
3417         if (device == NULL)
3418                 return FALSE;
3419         halDrive = libhal_drive_from_device_file (hal_ctx, device);
3420         g_free(device);
3421         if (halDrive == NULL)
3422                 return FALSE;
3423         dtype = libhal_drive_get_type(halDrive);
3424         libhal_drive_free(halDrive);
3425         return (dtype == LIBHAL_DRIVE_TYPE_CDROM);
3426 #else
3427         return FALSE;
3428 #endif
3429 }
3430
3431 #if defined(_WIN32)
3432 static void
3433 handle_media_change(const gchar *device, gboolean insert, signal_user_data_t *ud)
3434 {
3435         guint dtype;
3436         static gint ins_count = 0;
3437         static gint rem_count = 0;
3438
3439         // The media change event in windows bounces around a bit
3440         // so I debounce it here
3441         // DVD insertion detected.  Scan it.
3442         dtype = GetDriveType(device);
3443         if (dtype != DRIVE_CDROM)
3444                 return;
3445         if (insert)
3446         {
3447                 rem_count = 0;
3448                 ins_count++;
3449                 if (ins_count == 2)
3450                 {
3451                         g_thread_create((GThreadFunc)ghb_cache_volnames, ud, FALSE, NULL);
3452                         if (ghb_settings_get_boolean(ud->settings, "AutoScan") &&
3453                                 ud->current_dvd_device != NULL &&
3454                                 strcmp(device, ud->current_dvd_device) == 0)
3455                         {
3456                                 show_scan_progress(ud);
3457                                 update_source_label(ud, device, TRUE);
3458                                 gint preview_count;
3459                                 preview_count = ghb_settings_get_int(ud->settings, "preview_count");
3460                                 start_scan(ud, device, 0, preview_count);
3461                         }
3462                 }
3463         }
3464         else
3465         {
3466                 ins_count = 0;
3467                 rem_count++;
3468                 if (rem_count == 2)
3469                 {
3470                         g_thread_create((GThreadFunc)ghb_cache_volnames, ud, FALSE, NULL);
3471                         if (ud->current_dvd_device != NULL &&
3472                                 strcmp(device, ud->current_dvd_device) == 0)
3473                         {
3474                                 ghb_hb_cleanup(TRUE);
3475                                 prune_logs(ud);
3476                                 ghb_settings_set_string(ud->settings, "source", "/dev/null");
3477                                 start_scan(ud, "/dev/null", 0, 1);
3478                         }
3479                 }
3480         }
3481 }
3482
3483 static gchar
3484 FindDriveFromMask(ULONG unitmask)
3485 {
3486         gchar cc;
3487         for (cc = 0; cc < 26; cc++)
3488         {
3489                 if (unitmask & 0x01)
3490                         return 'A' + cc;
3491                 unitmask >>= 1;
3492         }
3493         return 0;
3494 }
3495
3496 void
3497 wm_drive_changed(MSG *msg, signal_user_data_t *ud)
3498 {
3499         PDEV_BROADCAST_HDR bch = (PDEV_BROADCAST_HDR)msg->lParam;
3500         gchar drive[4];
3501
3502         g_strlcpy(drive, "A:" G_DIR_SEPARATOR_S, 4);
3503         switch (msg->wParam)
3504         {
3505                 case DBT_DEVICEARRIVAL:
3506                 {
3507                         if (bch->dbch_devicetype == DBT_DEVTYP_VOLUME)
3508                         {
3509                                 PDEV_BROADCAST_VOLUME bcv = (PDEV_BROADCAST_VOLUME)bch;
3510
3511                                 if (bcv->dbcv_flags & DBTF_MEDIA)
3512                                 {
3513                                         drive[0] = FindDriveFromMask(bcv->dbcv_unitmask);
3514                                         handle_media_change(drive, TRUE, ud);
3515                                 }
3516                         }
3517                 } break;
3518
3519                 case DBT_DEVICEREMOVECOMPLETE:
3520                 {
3521                         if (bch->dbch_devicetype == DBT_DEVTYP_VOLUME)
3522                         {
3523                                 PDEV_BROADCAST_VOLUME bcv = (PDEV_BROADCAST_VOLUME)bch;
3524
3525                                 if (bcv->dbcv_flags & DBTF_MEDIA)
3526                                 {
3527                                         drive[0] = FindDriveFromMask(bcv->dbcv_unitmask);
3528                                         handle_media_change(drive, FALSE, ud);
3529                                 }
3530                         }
3531                 } break;
3532                 default: ;
3533         }
3534 }
3535
3536 #else
3537
3538 G_MODULE_EXPORT void
3539 drive_changed_cb(GVolumeMonitor *gvm, GDrive *gd, signal_user_data_t *ud)
3540 {
3541         gchar *device;
3542         gint state;
3543
3544         g_debug("drive_changed_cb()");
3545         g_thread_create((GThreadFunc)ghb_cache_volnames, ud, FALSE, NULL);
3546
3547         state = ghb_get_scan_state();
3548         device = g_drive_get_identifier(gd, G_VOLUME_IDENTIFIER_KIND_UNIX_DEVICE);
3549         if (ud->current_dvd_device == NULL ||
3550                 strcmp(device, ud->current_dvd_device) != 0 ||
3551                 state != GHB_STATE_IDLE )
3552         {
3553                 return;
3554         }
3555         if (g_drive_has_media(gd))
3556         {
3557                 if (ghb_settings_get_boolean(ud->settings, "AutoScan"))
3558                 {
3559                         show_scan_progress(ud);
3560                         update_source_label(ud, device, TRUE);
3561                         gint preview_count;
3562                         preview_count = ghb_settings_get_int(ud->settings, "preview_count");
3563                         start_scan(ud, device, 0, preview_count);
3564                 }
3565         }
3566         else
3567         {
3568                 ghb_hb_cleanup(TRUE);
3569                 prune_logs(ud);
3570                 ghb_settings_set_string(ud->settings, "source", "/dev/null");
3571                 start_scan(ud, "/dev/null", 0, 1);
3572         }
3573 }
3574 #endif
3575
3576 #if !defined(_WIN32)
3577 static void
3578 dbus_init (void)
3579 {
3580         dbus_g_thread_init();
3581 }
3582
3583 #define GPM_DBUS_PM_SERVICE                     "org.freedesktop.PowerManagement"
3584 #define GPM_DBUS_PM_PATH                        "/org/freedesktop/PowerManagement"
3585 #define GPM_DBUS_PM_INTERFACE           "org.freedesktop.PowerManagement"
3586 #define GPM_DBUS_INHIBIT_PATH           "/org/freedesktop/PowerManagement/Inhibit"
3587 #define GPM_DBUS_INHIBIT_INTERFACE      "org.freedesktop.PowerManagement.Inhibit" 
3588 static gboolean gpm_inhibited = FALSE;
3589 static guint gpm_cookie = -1;
3590 #endif
3591
3592 static gboolean
3593 ghb_can_suspend_gpm()
3594 {
3595         gboolean can_suspend = FALSE;
3596 #if !defined(_WIN32)
3597         DBusGConnection *conn;
3598         DBusGProxy      *proxy;
3599         GError *error = NULL;
3600         gboolean res;
3601         
3602
3603         g_debug("ghb_can_suspend_gpm()");
3604         conn = dbus_g_bus_get(DBUS_BUS_SESSION, &error);
3605         if (error != NULL)
3606         {
3607                 g_warning("DBUS cannot connect: %s", error->message);
3608                 g_error_free(error);
3609                 return FALSE;
3610         }
3611         proxy = dbus_g_proxy_new_for_name(conn, GPM_DBUS_PM_SERVICE,
3612                                                         GPM_DBUS_PM_PATH, GPM_DBUS_PM_INTERFACE);
3613         if (proxy == NULL)
3614         {
3615                 g_warning("Could not get DBUS proxy: %s", GPM_DBUS_PM_SERVICE);
3616                 dbus_g_connection_unref(conn);
3617                 return FALSE;
3618         }
3619         res = dbus_g_proxy_call(proxy, "CanSuspend", &error,
3620                                                         G_TYPE_INVALID,
3621                                                         G_TYPE_BOOLEAN, &can_suspend,
3622                                                         G_TYPE_INVALID);
3623         if (!res)
3624         {
3625                 if (error != NULL)
3626                 {
3627                         g_warning("CanSuspend failed: %s", error->message);
3628                         g_error_free(error);
3629                 }
3630                 else
3631                         g_warning("CanSuspend failed");
3632                 // Try to shutdown anyway
3633                 can_suspend = TRUE;
3634         }
3635         g_object_unref(G_OBJECT(proxy));
3636         dbus_g_connection_unref(conn);
3637 #endif
3638         return can_suspend;
3639 }
3640
3641 static void
3642 ghb_suspend_gpm()
3643 {
3644 #if !defined(_WIN32)
3645         DBusGConnection *conn;
3646         DBusGProxy      *proxy;
3647         GError *error = NULL;
3648         gboolean res;
3649         
3650
3651         g_debug("ghb_suspend_gpm()");
3652         conn = dbus_g_bus_get(DBUS_BUS_SESSION, &error);
3653         if (error != NULL)
3654         {
3655                 g_warning("DBUS cannot connect: %s", error->message);
3656                 g_error_free(error);
3657                 return;
3658         }
3659         proxy = dbus_g_proxy_new_for_name(conn, GPM_DBUS_PM_SERVICE,
3660                                                         GPM_DBUS_PM_PATH, GPM_DBUS_PM_INTERFACE);
3661         if (proxy == NULL)
3662         {
3663                 g_warning("Could not get DBUS proxy: %s", GPM_DBUS_PM_SERVICE);
3664                 dbus_g_connection_unref(conn);
3665                 return;
3666         }
3667         res = dbus_g_proxy_call(proxy, "Suspend", &error,
3668                                                         G_TYPE_INVALID,
3669                                                         G_TYPE_INVALID);
3670         if (!res)
3671         {
3672                 if (error != NULL)
3673                 {
3674                         g_warning("Suspend failed: %s", error->message);
3675                         g_error_free(error);
3676                 }
3677                 else
3678                         g_warning("Suspend failed");
3679         }
3680         g_object_unref(G_OBJECT(proxy));
3681         dbus_g_connection_unref(conn);
3682 #endif
3683 }
3684
3685 static gboolean
3686 ghb_can_shutdown_gpm()
3687 {
3688         gboolean can_shutdown = FALSE;
3689 #if !defined(_WIN32)
3690         DBusGConnection *conn;
3691         DBusGProxy      *proxy;
3692         GError *error = NULL;
3693         gboolean res;
3694         
3695
3696         g_debug("ghb_can_shutdown_gpm()");
3697         conn = dbus_g_bus_get(DBUS_BUS_SESSION, &error);
3698         if (error != NULL)
3699         {
3700                 g_warning("DBUS cannot connect: %s", error->message);
3701                 g_error_free(error);
3702                 return FALSE;
3703         }
3704         proxy = dbus_g_proxy_new_for_name(conn, GPM_DBUS_PM_SERVICE,
3705                                                         GPM_DBUS_PM_PATH, GPM_DBUS_PM_INTERFACE);
3706         if (proxy == NULL)
3707         {
3708                 g_warning("Could not get DBUS proxy: %s", GPM_DBUS_PM_SERVICE);
3709                 dbus_g_connection_unref(conn);
3710                 return FALSE;
3711         }
3712         res = dbus_g_proxy_call(proxy, "CanShutdown", &error,
3713                                                         G_TYPE_INVALID,
3714                                                         G_TYPE_BOOLEAN, &can_shutdown,
3715                                                         G_TYPE_INVALID);
3716         if (!res)
3717         {
3718                 if (error != NULL)
3719                 {
3720                         g_warning("CanShutdown failed: %s", error->message);
3721                         g_error_free(error);
3722                 }
3723                 else
3724                         g_warning("CanShutdown failed");
3725                 // Try to shutdown anyway
3726                 can_shutdown = TRUE;
3727         }
3728         g_object_unref(G_OBJECT(proxy));
3729         dbus_g_connection_unref(conn);
3730 #endif
3731         return can_shutdown;
3732 }
3733
3734 static void
3735 ghb_shutdown_gpm()
3736 {
3737 #if !defined(_WIN32)
3738         DBusGConnection *conn;
3739         DBusGProxy      *proxy;
3740         GError *error = NULL;
3741         gboolean res;
3742         
3743
3744         g_debug("ghb_shutdown_gpm()");
3745         conn = dbus_g_bus_get(DBUS_BUS_SESSION, &error);
3746         if (error != NULL)
3747         {
3748                 g_warning("DBUS cannot connect: %s", error->message);
3749                 g_error_free(error);
3750                 return;
3751         }
3752         proxy = dbus_g_proxy_new_for_name(conn, GPM_DBUS_PM_SERVICE,
3753                                                         GPM_DBUS_PM_PATH, GPM_DBUS_PM_INTERFACE);
3754         if (proxy == NULL)
3755         {
3756                 g_warning("Could not get DBUS proxy: %s", GPM_DBUS_PM_SERVICE);
3757                 dbus_g_connection_unref(conn);
3758                 return;
3759         }
3760         res = dbus_g_proxy_call(proxy, "Shutdown", &error,
3761                                                         G_TYPE_INVALID,
3762                                                         G_TYPE_INVALID);
3763         if (!res)
3764         {
3765                 if (error != NULL)
3766                 {
3767                         g_warning("Shutdown failed: %s", error->message);
3768                         g_error_free(error);
3769                 }
3770                 else
3771                         g_warning("Shutdown failed");
3772         }
3773         g_object_unref(G_OBJECT(proxy));
3774         dbus_g_connection_unref(conn);
3775 #endif
3776 }
3777
3778 void
3779 ghb_inhibit_gpm()
3780 {
3781 #if !defined(_WIN32)
3782         DBusGConnection *conn;
3783         DBusGProxy      *proxy;
3784         GError *error = NULL;
3785         gboolean res;
3786         
3787
3788         if (gpm_inhibited)
3789         {
3790                 // Already inhibited
3791                 return;
3792         }
3793         g_debug("ghb_inhibit_gpm()");
3794         conn = dbus_g_bus_get(DBUS_BUS_SESSION, &error);
3795         if (error != NULL)
3796         {
3797                 g_warning("DBUS cannot connect: %s", error->message);
3798                 g_error_free(error);
3799                 return;
3800         }
3801         proxy = dbus_g_proxy_new_for_name(conn, GPM_DBUS_PM_SERVICE,
3802                                                         GPM_DBUS_INHIBIT_PATH, GPM_DBUS_INHIBIT_INTERFACE);
3803         if (proxy == NULL)
3804         {
3805                 g_warning("Could not get DBUS proxy: %s", GPM_DBUS_PM_SERVICE);
3806                 dbus_g_connection_unref(conn);
3807                 return;
3808         }
3809         res = dbus_g_proxy_call(proxy, "Inhibit", &error,
3810                                                         G_TYPE_STRING, "ghb",
3811                                                         G_TYPE_STRING, "Encoding",
3812                                                         G_TYPE_INVALID,
3813                                                         G_TYPE_UINT, &gpm_cookie,
3814                                                         G_TYPE_INVALID);
3815         gpm_inhibited = TRUE;
3816         if (!res)
3817         {
3818                 if (error != NULL)
3819                 {
3820                         g_warning("Inhibit failed: %s", error->message);
3821                         g_error_free(error);
3822                         gpm_cookie = -1;
3823                 }
3824                 else
3825                         g_warning("Inhibit failed");
3826                 gpm_cookie = -1;
3827                 gpm_inhibited = FALSE;
3828         }
3829         g_object_unref(G_OBJECT(proxy));
3830         dbus_g_connection_unref(conn);
3831 #endif
3832 }
3833
3834 void
3835 ghb_uninhibit_gpm()
3836 {
3837 #if !defined(_WIN32)
3838         DBusGConnection *conn;
3839         DBusGProxy      *proxy;
3840         GError *error = NULL;
3841         gboolean res;
3842         
3843         g_debug("ghb_uninhibit_gpm() gpm_cookie %u", gpm_cookie);
3844
3845         if (!gpm_inhibited)
3846         {
3847                 // Not inhibited
3848                 return;
3849         }
3850         conn = dbus_g_bus_get(DBUS_BUS_SESSION, &error);
3851         if (error != NULL)
3852         {
3853                 g_warning("DBUS cannot connect: %s", error->message);
3854                 g_error_free(error);
3855                 return;
3856         }
3857         proxy = dbus_g_proxy_new_for_name(conn, GPM_DBUS_PM_SERVICE,
3858                                                         GPM_DBUS_INHIBIT_PATH, GPM_DBUS_INHIBIT_INTERFACE);
3859         if (proxy == NULL)
3860         {
3861                 g_warning("Could not get DBUS proxy: %s", GPM_DBUS_PM_SERVICE);
3862                 dbus_g_connection_unref(conn);
3863                 return;
3864         }
3865         res = dbus_g_proxy_call(proxy, "UnInhibit", &error,
3866                                                         G_TYPE_UINT, gpm_cookie,
3867                                                         G_TYPE_INVALID,
3868                                                         G_TYPE_INVALID);
3869         if (!res)
3870         {
3871                 if (error != NULL)
3872                 {
3873                         g_warning("UnInhibit failed: %s", error->message);
3874                         g_error_free(error);
3875                 }
3876                 else
3877                         g_warning("UnInhibit failed");
3878         }
3879         gpm_inhibited = FALSE;
3880         dbus_g_connection_unref(conn);
3881         g_object_unref(G_OBJECT(proxy));
3882 #endif
3883 }
3884
3885 #if !defined(_WIN32)
3886
3887 // For inhibit and shutdown
3888 #define GPM_DBUS_SM_SERVICE                     "org.gnome.SessionManager"
3889 #define GPM_DBUS_SM_PATH                        "/org/gnome/SessionManager"
3890 #define GPM_DBUS_SM_INTERFACE           "org.gnome.SessionManager"
3891
3892 #endif
3893
3894 static gboolean
3895 ghb_can_shutdown_gsm()
3896 {
3897         gboolean can_shutdown = FALSE;
3898 #if !defined(_WIN32)
3899         DBusGConnection *conn;
3900         DBusGProxy      *proxy;
3901         GError *error = NULL;
3902         gboolean res;
3903         
3904
3905         g_debug("ghb_can_shutdown_gpm()");
3906         conn = dbus_g_bus_get(DBUS_BUS_SESSION, &error);
3907         if (error != NULL)
3908         {
3909                 g_warning("DBUS cannot connect: %s", error->message);
3910                 g_error_free(error);
3911                 return FALSE;
3912         }
3913         proxy = dbus_g_proxy_new_for_name(conn, GPM_DBUS_SM_SERVICE,
3914                                                         GPM_DBUS_SM_PATH, GPM_DBUS_SM_INTERFACE);
3915         if (proxy == NULL)
3916         {
3917                 g_warning("Could not get DBUS proxy: %s", GPM_DBUS_SM_SERVICE);
3918                 dbus_g_connection_unref(conn);
3919                 return FALSE;
3920         }
3921         res = dbus_g_proxy_call(proxy, "CanShutdown", &error,
3922                                                         G_TYPE_INVALID,
3923                                                         G_TYPE_BOOLEAN, &can_shutdown,
3924                                                         G_TYPE_INVALID);
3925         g_object_unref(G_OBJECT(proxy));
3926         dbus_g_connection_unref(conn);
3927         if (!res)
3928         {
3929                 if (error != NULL)
3930                 {
3931                         g_error_free(error);
3932                 }
3933                 // Try to shutdown anyway
3934                 can_shutdown = TRUE;
3935                 // Try the gpm version
3936                 return ghb_can_shutdown_gpm();
3937         }
3938 #endif
3939         return can_shutdown;
3940 }
3941
3942 static void
3943 ghb_shutdown_gsm()
3944 {
3945 #if !defined(_WIN32)
3946         DBusGConnection *conn;
3947         DBusGProxy      *proxy;
3948         GError *error = NULL;
3949         gboolean res;
3950         
3951
3952         g_debug("ghb_shutdown_gpm()");
3953         conn = dbus_g_bus_get(DBUS_BUS_SESSION, &error);
3954         if (error != NULL)
3955         {
3956                 g_warning("DBUS cannot connect: %s", error->message);
3957                 g_error_free(error);
3958                 return;
3959         }
3960         proxy = dbus_g_proxy_new_for_name(conn, GPM_DBUS_SM_SERVICE,
3961                                                         GPM_DBUS_SM_PATH, GPM_DBUS_SM_INTERFACE);
3962         if (proxy == NULL)
3963         {
3964                 g_warning("Could not get DBUS proxy: %s", GPM_DBUS_SM_SERVICE);
3965                 dbus_g_connection_unref(conn);
3966                 return;
3967         }
3968         res = dbus_g_proxy_call(proxy, "Shutdown", &error,
3969                                                         G_TYPE_INVALID,
3970                                                         G_TYPE_INVALID);
3971         g_object_unref(G_OBJECT(proxy));
3972         dbus_g_connection_unref(conn);
3973         if (!res)
3974         {
3975                 if (error != NULL)
3976                 {
3977                         g_error_free(error);
3978                 }
3979                 // Try the gpm version
3980                 ghb_shutdown_gpm();
3981         }
3982 #endif
3983 }
3984
3985 void
3986 ghb_inhibit_gsm(signal_user_data_t *ud)
3987 {
3988 #if !defined(_WIN32)
3989         DBusGConnection *conn;
3990         DBusGProxy      *proxy;
3991         GError *error = NULL;
3992         gboolean res;
3993         guint xid;
3994         GtkWidget *widget;
3995         
3996
3997         if (gpm_inhibited)
3998         {
3999                 // Already inhibited
4000                 return;
4001         }
4002         g_debug("ghb_inhibit_gsm()");
4003         conn = dbus_g_bus_get(DBUS_BUS_SESSION, &error);
4004         if (error != NULL)
4005         {
4006                 g_warning("DBUS cannot connect: %s", error->message);
4007                 g_error_free(error);
4008                 return;
4009         }
4010         proxy = dbus_g_proxy_new_for_name(conn, GPM_DBUS_SM_SERVICE,
4011                                                         GPM_DBUS_SM_PATH, GPM_DBUS_SM_INTERFACE);
4012         if (proxy == NULL)
4013         {
4014                 g_warning("Could not get DBUS proxy: %s", GPM_DBUS_SM_SERVICE);
4015                 dbus_g_connection_unref(conn);
4016                 return;
4017         }
4018         widget = GHB_WIDGET(ud->builder, "hb_window");
4019         xid = GDK_DRAWABLE_XID(widget->window);
4020         res = dbus_g_proxy_call(proxy, "Inhibit", &error,
4021                                                         G_TYPE_STRING, "ghb",
4022                                                         G_TYPE_UINT, xid,
4023                                                         G_TYPE_STRING, "Encoding",
4024                                                         G_TYPE_UINT, 1 | 4,
4025                                                         G_TYPE_INVALID,
4026                                                         G_TYPE_UINT, &gpm_cookie,
4027                                                         G_TYPE_INVALID);
4028         gpm_inhibited = TRUE;
4029         g_object_unref(G_OBJECT(proxy));
4030         dbus_g_connection_unref(conn);
4031         if (!res)
4032         {
4033                 if (error != NULL)
4034                 {
4035                         g_error_free(error);
4036                         gpm_cookie = -1;
4037                 }
4038                 gpm_cookie = -1;
4039                 gpm_inhibited = FALSE;
4040                 // Try the gpm version
4041                 ghb_inhibit_gpm();
4042         }
4043 #endif
4044 }
4045
4046 void
4047 ghb_uninhibit_gsm()
4048 {
4049 #if !defined(_WIN32)
4050         DBusGConnection *conn;
4051         DBusGProxy      *proxy;
4052         GError *error = NULL;
4053         gboolean res;
4054         
4055         g_debug("ghb_uninhibit_gsm() gpm_cookie %u", gpm_cookie);
4056
4057         if (!gpm_inhibited)
4058         {
4059                 // Not inhibited
4060                 return;
4061         }
4062         conn = dbus_g_bus_get(DBUS_BUS_SESSION, &error);
4063         if (error != NULL)
4064         {
4065                 g_warning("DBUS cannot connect: %s", error->message);
4066                 g_error_free(error);
4067                 return;
4068         }
4069         proxy = dbus_g_proxy_new_for_name(conn, GPM_DBUS_SM_SERVICE,
4070                                                         GPM_DBUS_SM_PATH, GPM_DBUS_SM_INTERFACE);
4071         if (proxy == NULL)
4072         {
4073                 g_warning("Could not get DBUS proxy: %s", GPM_DBUS_SM_SERVICE);
4074                 dbus_g_connection_unref(conn);
4075                 return;
4076         }
4077         res = dbus_g_proxy_call(proxy, "Uninhibit", &error,
4078                                                         G_TYPE_UINT, gpm_cookie,
4079                                                         G_TYPE_INVALID,
4080                                                         G_TYPE_INVALID);
4081         dbus_g_connection_unref(conn);
4082         g_object_unref(G_OBJECT(proxy));
4083         if (!res)
4084         {
4085                 if (error != NULL)
4086                 {
4087                         g_error_free(error);
4088                 }
4089                 ghb_uninhibit_gpm();
4090         }
4091         gpm_inhibited = FALSE;
4092 #endif
4093 }
4094
4095 void
4096 ghb_hal_init()
4097 {
4098 #if !defined(_WIN32)
4099         DBusGConnection *gconn;
4100         DBusConnection *conn;
4101         GError *gerror = NULL;
4102         DBusError error;
4103         char **devices;
4104         int nr;
4105
4106         dbus_init ();
4107
4108         if (!(hal_ctx = libhal_ctx_new ())) {
4109                 g_warning ("failed to create a HAL context!");
4110                 return;
4111         }
4112
4113         gconn = dbus_g_bus_get(DBUS_BUS_SYSTEM, &gerror);
4114         if (gerror != NULL)
4115         {
4116                 g_warning("DBUS cannot connect: %s", gerror->message);
4117                 g_error_free(gerror);
4118                 return;
4119         }
4120         conn = dbus_g_connection_get_connection(gconn);
4121         libhal_ctx_set_dbus_connection (hal_ctx, conn);
4122         dbus_error_init (&error);
4123         if (!libhal_ctx_init (hal_ctx, &error)) {
4124                 g_warning ("libhal_ctx_init failed: %s", error.message ? error.message : "unknown");
4125                 dbus_error_free (&error);
4126                 libhal_ctx_free (hal_ctx);
4127                 dbus_g_connection_unref(gconn);
4128                 return;
4129         }
4130
4131         /*
4132          * Do something to ping the HAL daemon - the above functions will
4133          * succeed even if hald is not running, so long as DBUS is.  But we
4134          * want to exit silently if hald is not running, to behave on
4135          * pre-2.6 systems.
4136          */
4137         if (!(devices = libhal_get_all_devices (hal_ctx, &nr, &error))) {
4138                 g_warning ("seems that HAL is not running: %s", error.message ? error.message : "unknown");
4139                 dbus_error_free (&error);
4140
4141                 libhal_ctx_shutdown (hal_ctx, NULL);
4142                 libhal_ctx_free (hal_ctx);
4143                 dbus_g_connection_unref(gconn);
4144                 return;
4145         }
4146
4147         libhal_free_string_array (devices);
4148         dbus_g_connection_unref(gconn);
4149 #endif
4150 }
4151
4152 G_MODULE_EXPORT gboolean 
4153 tweak_setting_cb(
4154         GtkWidget *widget, 
4155         GdkEventButton *event, 
4156         signal_user_data_t *ud)
4157 {
4158         const gchar *name;
4159         gchar *tweak_name;
4160         gboolean ret = FALSE;
4161         gboolean allow_tweaks;
4162
4163         g_debug("press %d %d", event->type, event->button);
4164         allow_tweaks = ghb_settings_get_boolean(ud->settings, "allow_tweaks");
4165         if (allow_tweaks && event->type == GDK_BUTTON_PRESS && event->button == 3)
4166         { // Its a right mouse click
4167                 GtkWidget *dialog;
4168                 GtkEntry *entry;
4169                 GtkResponseType response;
4170                 gchar *tweak = NULL;
4171
4172                 name = gtk_widget_get_name(widget);
4173                 if (g_str_has_prefix(name, "tweak_"))
4174                 {
4175                         tweak_name = g_strdup(name);
4176                 }
4177                 else
4178                 {
4179                         tweak_name = g_strdup_printf("tweak_%s", name);
4180                 }
4181
4182                 tweak = ghb_settings_get_string (ud->settings, tweak_name);
4183                 dialog = GHB_WIDGET(ud->builder, "tweak_dialog");
4184                 gtk_window_set_title(GTK_WINDOW(dialog), tweak_name);
4185                 entry = GTK_ENTRY(GHB_WIDGET(ud->builder, "tweak_setting"));
4186                 if (tweak)
4187                 {
4188                         gtk_entry_set_text(entry, tweak);
4189                         g_free(tweak);
4190                 }
4191                 response = gtk_dialog_run(GTK_DIALOG(dialog));
4192                 gtk_widget_hide(dialog);
4193                 if (response == GTK_RESPONSE_OK)
4194                 {
4195                         tweak = (gchar*)gtk_entry_get_text(entry);
4196                         if (ghb_validate_filter_string(tweak, -1))
4197                                 ghb_settings_set_string(ud->settings, tweak_name, tweak);
4198                         else
4199                         {
4200                                 gchar *message;
4201                                 message = g_strdup_printf(
4202                                                         "Invalid Settings:\n%s",
4203                                                         tweak);
4204                                 ghb_message_dialog(GTK_MESSAGE_ERROR, message, "Cancel", NULL);
4205                                 g_free(message);
4206                         }
4207                 }
4208                 g_free(tweak_name);
4209                 ret = TRUE;
4210         }
4211         return ret;
4212 }
4213
4214 G_MODULE_EXPORT gboolean 
4215 easter_egg_cb(
4216         GtkWidget *widget, 
4217         GdkEventButton *event, 
4218         signal_user_data_t *ud)
4219 {
4220         g_debug("press %d %d", event->type, event->button);
4221         if (event->type == GDK_3BUTTON_PRESS && event->button == 1)
4222         { // Its a tripple left mouse button click
4223                 GtkWidget *widget;
4224                 widget = GHB_WIDGET(ud->builder, "allow_tweaks");
4225                 gtk_widget_show(widget);
4226                 widget = GHB_WIDGET(ud->builder, "hbfd_feature");
4227                 gtk_widget_show(widget);
4228         }
4229         else if (event->type == GDK_BUTTON_PRESS && event->button == 1)
4230         {
4231                 GtkWidget *widget;
4232                 widget = GHB_WIDGET(ud->builder, "allow_tweaks");
4233                 gtk_widget_hide(widget);
4234                 widget = GHB_WIDGET(ud->builder, "hbfd_feature");
4235                 gtk_widget_hide(widget);
4236         }
4237         return FALSE;
4238 }
4239
4240 G_MODULE_EXPORT gchar*
4241 format_deblock_cb(GtkScale *scale, gdouble val, signal_user_data_t *ud)
4242 {
4243         if (val < 5.0)
4244         {
4245                 return g_strdup_printf("Off");
4246         }
4247         else
4248         {
4249                 return g_strdup_printf("%d", (gint)val);
4250         }
4251 }
4252
4253 G_MODULE_EXPORT gchar*
4254 format_drc_cb(GtkScale *scale, gdouble val, signal_user_data_t *ud)
4255 {
4256         if (val <= 0.0)
4257         {
4258                 return g_strdup_printf("Off");
4259         }
4260         else
4261         {
4262                 return g_strdup_printf("%.1f", val);
4263         }
4264 }
4265
4266 G_MODULE_EXPORT gchar*
4267 format_vquality_cb(GtkScale *scale, gdouble val, signal_user_data_t *ud)
4268 {
4269         gdouble percent;
4270
4271         gint vcodec = ghb_settings_combo_int(ud->settings, "VideoEncoder");
4272         switch (vcodec)
4273         {
4274                 case HB_VCODEC_X264:
4275                 {
4276                         gboolean crf;
4277                         crf = ghb_settings_get_boolean(ud->settings, "constant_rate_factor");
4278                         percent = 100. * (51 - val) / 51.;
4279                         if (crf)
4280                                 return g_strdup_printf("RF: %.4g (%.0f%%)", val, percent);
4281                         else
4282                                 return g_strdup_printf("QP: %.4g (%.0f%%)", val, percent);
4283                 } break;
4284
4285                 case HB_VCODEC_FFMPEG:
4286                 {
4287                         percent = 100. * (30 - (val - 1)) / 30.;
4288                         return g_strdup_printf("QP: %d (%.0f%%)", (int)val, percent);
4289                 } break;
4290
4291                 case HB_VCODEC_THEORA:
4292                 {
4293                         percent = 100. * val / 63.;
4294                         return g_strdup_printf("QP: %d (%.0f%%)", (int)val, percent);
4295                 } break;
4296
4297                 default:
4298                 {
4299                         percent = 0;
4300                 } break;
4301         }
4302         return g_strdup_printf("QP: %.1f / %.1f%%", val, percent);
4303 }
4304
4305 static void
4306 process_appcast(signal_user_data_t *ud)
4307 {
4308         gchar *description = NULL, *build = NULL, *version = NULL, *msg;
4309 #if !defined(_WIN32)
4310         GtkWidget *window;
4311         static GtkWidget *html = NULL;
4312 #endif
4313         GtkWidget *dialog, *label;
4314         gint    response, ibuild = 0, skip;
4315
4316         if (ud->appcast == NULL || ud->appcast_len < 15 || 
4317                 strncmp(&(ud->appcast[9]), "200 OK", 6))
4318         {
4319                 goto done;
4320         }
4321         ghb_appcast_parse(ud->appcast, &description, &build, &version);
4322         if (build)
4323                 ibuild = g_strtod(build, NULL);
4324         skip = ghb_settings_get_int(ud->settings, "update_skip_version");
4325         if (description == NULL || build == NULL || version == NULL 
4326                 || ibuild <= hb_get_build(NULL) || skip == ibuild)
4327         {
4328                 goto done;
4329         }
4330         msg = g_strdup_printf("HandBrake %s/%s is now available (you have %s/%d).",
4331                         version, build, hb_get_version(NULL), hb_get_build(NULL));
4332         label = GHB_WIDGET(ud->builder, "update_message");
4333         gtk_label_set_text(GTK_LABEL(label), msg);
4334
4335 #if !defined(_WIN32)
4336         if (html == NULL)
4337         {
4338                 html = webkit_web_view_new();
4339                 window = GHB_WIDGET(ud->builder, "update_scroll");
4340                 gtk_container_add(GTK_CONTAINER(window), html);
4341                 // Show it
4342                 gtk_widget_set_size_request(html, 420, 240);
4343                 gtk_widget_show(html);
4344         }
4345         webkit_web_view_open(WEBKIT_WEB_VIEW(html), description);
4346 #endif
4347         dialog = GHB_WIDGET(ud->builder, "update_dialog");
4348         response = gtk_dialog_run(GTK_DIALOG(dialog));
4349         gtk_widget_hide(dialog);
4350         if (response == GTK_RESPONSE_OK)
4351         {
4352                 // Skip
4353                 ghb_settings_set_int(ud->settings, "update_skip_version", ibuild);
4354                 ghb_pref_save(ud->settings, "update_skip_version");
4355         }
4356         g_free(msg);
4357
4358 done:
4359         if (description) g_free(description);
4360         if (build) g_free(build);
4361         if (version) g_free(version);
4362         g_free(ud->appcast);
4363         ud->appcast_len = 0;
4364         ud->appcast = NULL;
4365         appcast_busy = FALSE;
4366 }
4367
4368 void
4369 ghb_net_close(GIOChannel *ioc)
4370 {
4371         gint fd;
4372
4373         g_debug("ghb_net_close");
4374         if (ioc == NULL) return;
4375         fd = g_io_channel_unix_get_fd(ioc);
4376         close(fd);
4377         g_io_channel_unref(ioc);
4378 }
4379
4380 G_MODULE_EXPORT gboolean
4381 ghb_net_recv_cb(GIOChannel *ioc, GIOCondition cond, gpointer data)
4382 {
4383         gchar buf[2048];
4384         gsize len;
4385         GError *gerror = NULL;
4386         GIOStatus status;
4387         
4388         g_debug("ghb_net_recv_cb");
4389         signal_user_data_t *ud = (signal_user_data_t*)data;
4390
4391         status = g_io_channel_read_chars (ioc, buf, 2048, &len, &gerror);
4392         if ((status == G_IO_STATUS_NORMAL || status == G_IO_STATUS_EOF) &&
4393                 len > 0)
4394         {
4395                 gint new_len = ud->appcast_len + len;
4396                 ud->appcast = g_realloc(ud->appcast, new_len + 1);
4397                 memcpy(&(ud->appcast[ud->appcast_len]), buf, len);
4398                 ud->appcast_len = new_len;
4399         }
4400         if (status == G_IO_STATUS_EOF)
4401         {
4402                 ud->appcast[ud->appcast_len] = 0;
4403                 ghb_net_close(ioc);
4404                 process_appcast(ud);
4405                 return FALSE;
4406         }
4407         return TRUE;
4408 }
4409
4410 GIOChannel*
4411 ghb_net_open(signal_user_data_t *ud, gchar *address, gint port)
4412 {
4413         GIOChannel *ioc;
4414         gint fd;
4415
4416         struct sockaddr_in   sock;
4417         struct hostent     * host;
4418
4419         g_debug("ghb_net_open");
4420         if( !( host = gethostbyname( address ) ) )
4421         {
4422                 g_warning( "gethostbyname failed (%s)", address );
4423                 appcast_busy = FALSE;
4424                 return NULL;
4425         }
4426
4427         memset( &sock, 0, sizeof( struct sockaddr_in ) );
4428         sock.sin_family = host->h_addrtype;
4429         sock.sin_port   = htons( port );
4430         memcpy( &sock.sin_addr, host->h_addr, host->h_length );
4431
4432         fd = socket(host->h_addrtype, SOCK_STREAM, 0);
4433         if( fd < 0 )
4434         {
4435                 g_debug( "socket failed" );
4436                 appcast_busy = FALSE;
4437                 return NULL;
4438         }
4439
4440         if(connect(fd, (struct sockaddr*)&sock, sizeof(struct sockaddr_in )) < 0 )
4441         {
4442                 g_debug( "connect failed" );
4443                 appcast_busy = FALSE;
4444                 return NULL;
4445         }
4446         ioc = g_io_channel_unix_new(fd);
4447         g_io_channel_set_encoding (ioc, NULL, NULL);
4448         g_io_channel_set_flags(ioc, G_IO_FLAG_NONBLOCK, NULL);
4449         g_io_add_watch (ioc, G_IO_IN, ghb_net_recv_cb, (gpointer)ud );
4450
4451         return ioc;
4452 }
4453
4454 gpointer
4455 ghb_check_update(signal_user_data_t *ud)
4456 {
4457         gchar *query;
4458         gsize len;
4459         GIOChannel *ioc;
4460         GError *gerror = NULL;
4461         GRegex *regex;
4462         GMatchInfo *mi;
4463         gchar *host, *appcast;
4464
4465         g_debug("ghb_check_update");
4466         appcast_busy = TRUE;
4467         regex = g_regex_new("^http://(.+)/(.+)$", 0, 0, NULL);
4468         if (!g_regex_match(regex, HB_PROJECT_URL_APPCAST, 0, &mi))
4469         {
4470                 return NULL;
4471         }
4472
4473         host = g_match_info_fetch(mi, 1);
4474         appcast = g_match_info_fetch(mi, 2);
4475
4476         if (host == NULL || appcast == NULL)
4477                 return NULL;
4478
4479         query = g_strdup_printf( "GET /%s HTTP/1.0\r\nHost: %s\r\n\r\n",
4480                                                         appcast, host);
4481
4482         ioc = ghb_net_open(ud, host, 80);
4483         if (ioc == NULL)
4484                 return NULL;
4485
4486         g_io_channel_write_chars(ioc, query, strlen(query), &len, &gerror);
4487         g_io_channel_flush(ioc, &gerror);
4488         g_free(query);
4489         g_free(host);
4490         g_free(appcast);
4491         g_match_info_free(mi);
4492         g_regex_unref(regex);
4493         return NULL;
4494 }
4495
4496 G_MODULE_EXPORT gboolean
4497 hb_visibility_event_cb(
4498         GtkWidget *widget, 
4499         GdkEventVisibility *vs, 
4500         signal_user_data_t *ud)
4501 {
4502         ud->hb_visibility = vs->state;
4503         return FALSE;
4504 }
4505
4506 G_MODULE_EXPORT void
4507 status_activate_cb(GtkStatusIcon *si, signal_user_data_t *ud)
4508 {
4509         GtkWindow *window;
4510         GdkWindowState state;
4511
4512         window = GTK_WINDOW(GHB_WIDGET(ud->builder, "hb_window"));
4513         state = gdk_window_get_state(GTK_WIDGET(window)->window);
4514         if ((state & GDK_WINDOW_STATE_ICONIFIED) ||
4515                 (ud->hb_visibility != GDK_VISIBILITY_UNOBSCURED))
4516         {
4517                 gtk_window_present(window);
4518                 gtk_window_set_skip_taskbar_hint(window, FALSE);
4519         }
4520         else
4521         {
4522                 gtk_window_set_skip_taskbar_hint(window, TRUE);
4523                 gtk_window_iconify(window);
4524         }
4525 }
4526
4527 #if !defined(_WIN32)
4528 G_MODULE_EXPORT void
4529 notify_closed_cb(NotifyNotification *notification, signal_user_data_t *ud)
4530 {
4531         g_object_unref(G_OBJECT(notification));
4532 }
4533 #endif
4534
4535 void
4536 ghb_notify_done(signal_user_data_t *ud)
4537 {
4538         GtkStatusIcon *si;
4539
4540         if (ghb_settings_combo_int(ud->settings, "WhenComplete") == 0)
4541                 return;
4542
4543         si = GTK_STATUS_ICON(GHB_OBJECT(ud->builder, "hb_status"));
4544
4545 #if !defined(_WIN32)
4546         NotifyNotification *notification;
4547         notification = notify_notification_new(
4548                 "Encode Complete",
4549                 "Put down that cocktail, Your HandBrake queue is done!",
4550                 "hb-icon",
4551                 NULL);
4552         notify_notification_attach_to_status_icon(notification, si);
4553         g_signal_connect(notification, "closed", (GCallback)notify_closed_cb, ud);
4554         notify_notification_show(notification, NULL);
4555 #endif
4556
4557         if (ghb_settings_combo_int(ud->settings, "WhenComplete") == 3)
4558         {
4559                 if (ghb_can_shutdown_gsm())
4560                 {
4561                         ghb_countdown_dialog(GTK_MESSAGE_WARNING, 
4562                                 "Your encode is complete.",
4563                                 "Shutting down the computer", 
4564                                 "Cancel", (GSourceFunc)shutdown_cb, 60);
4565                 }
4566         }
4567         if (ghb_settings_combo_int(ud->settings, "WhenComplete") == 2)
4568         {
4569                 if (ghb_can_suspend_gpm())
4570                 {
4571                         ghb_countdown_dialog(GTK_MESSAGE_WARNING, 
4572                                 "Your encode is complete.",
4573                                 "Putting computer to sleep", 
4574                                 "Cancel", (GSourceFunc)suspend_cb, 60);
4575                 }
4576         }
4577 }