OSDN Git Service

LinGui: drop dependency on hal, use udev instead
[handbrake-jp/handbrake-jp-git.git] / gtk / src / presets.c
index b419414..66f69a7 100644 (file)
  * any later version.
  * 
  */
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
 #include <glib.h>
 #include <glib-object.h>
 #include <glib/gstdio.h>
@@ -517,6 +521,37 @@ presets_clear_default(GValue *presets)
        }
 }
 
+static void
+presets_customize(GValue *presets)
+{
+       gint count, ii;
+
+       count = ghb_array_len(presets);
+       for (ii = 0; ii < count; ii++)
+       {
+               GValue *dict;
+               gboolean folder;
+               gint ptype;
+
+               dict = ghb_array_get_nth(presets, ii);
+
+               ptype = ghb_value_int(preset_dict_get_value(dict, "Type"));
+               if (ptype != PRESETS_CUSTOM)
+               {
+                       ghb_dict_insert(dict, g_strdup("Type"), 
+                                               ghb_int64_value_new(PRESETS_CUSTOM));
+               }
+               folder = ghb_value_boolean(preset_dict_get_value(dict, "Folder"));
+               if (folder)
+               {
+                       GValue *nested;
+
+                       nested = ghb_dict_lookup(dict, "ChildrenArray");
+                       presets_customize(nested);
+               }
+       }
+}
+
 static gint*
 presets_find_default2(GValue *presets, gint *len)
 {
@@ -871,6 +906,19 @@ preset_to_ui(signal_user_data_t *ud, GValue *dict)
        init_settings_from_dict(ud->settings, hidden, dict);
        init_ui_from_dict(ud, internal, dict);
        init_ui_from_dict(ud, hidden, dict);
+
+       if (dict != NULL)
+       {
+               GValue *val;
+               gboolean dd;
+
+               val = ghb_dict_lookup(dict, "PictureDecombDeinterlace");
+               if (val != NULL)
+               {
+                       dd = ghb_value_boolean(val);
+                       ghb_ui_update(ud, "PictureDeinterlaceDecomb", ghb_boolean_value(!dd));
+               }
+       }
 }
 
 void
@@ -1081,6 +1129,135 @@ load_plist(const gchar *name)
        return plist;
 }
 
+gboolean
+ghb_lock_file(const gchar *name)
+{
+#if !defined(_WIN32)
+       gchar *config, *path;
+       int fd, lock = 0;
+
+       config = ghb_get_user_config_dir(NULL);
+       path = g_strdup_printf ("%s/%s", config, name);
+       fd = open(path, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR);
+       if (fd >= 0)
+               lock = lockf(fd, F_TLOCK, 0);
+       if (lock)
+               close(fd);
+       g_free(config);
+       g_free(path);
+       return !lock;
+#else
+       return 1;
+#endif
+}
+
+void
+ghb_write_pid_file()
+{
+#if !defined(_WIN32)
+       gchar *config, *path;
+       pid_t pid;
+       FILE *fp;
+       int fd, lock;
+
+       pid = getpid();
+
+       config = ghb_get_user_config_dir(NULL);
+       path = g_strdup_printf ("%s/ghb.pid.%d", config, pid);
+
+       fp = g_fopen(path, "w");
+       fprintf(fp, "%d\n", pid);
+       fclose(fp);
+
+       fd = open(path, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR);
+       lock = lockf(fd, F_TLOCK, 0);
+
+       g_free(config);
+       g_free(path);
+#endif
+}
+
+void
+ghb_unlink_pid_file(int pid)
+{
+       gchar *config, *path;
+
+       config = ghb_get_user_config_dir(NULL);
+       path = g_strdup_printf ("%s/ghb.pid.%d", config, pid);
+
+       if (g_file_test(path, G_FILE_TEST_IS_REGULAR))
+       {
+               g_unlink(path);
+       }
+
+       g_free(config);
+       g_free(path);
+}
+
+int
+ghb_find_pid_file()
+{
+       const gchar *file;
+       gchar *config;
+
+       config = ghb_get_user_config_dir(NULL);
+
+       if (g_file_test(config, G_FILE_TEST_IS_DIR))
+       {
+               GDir *gdir = g_dir_open(config, 0, NULL);
+               file = g_dir_read_name(gdir);
+               while (file)
+               {
+                       if (strncmp(file, "ghb.pid.", 8) == 0)
+                       {
+                               gchar *path;
+                               pid_t my_pid;
+                               int pid;
+
+                               sscanf(file, "ghb.pid.%d", &pid);
+                               my_pid = getpid();
+                               if (my_pid == pid)
+                               {
+                                       file = g_dir_read_name(gdir);
+                                       continue;
+                               }
+                               path = g_strdup_printf("%s/%s", config, file);
+
+#if !defined(_WIN32)
+                               int fd, lock = 1;
+
+                               fd = open(path, O_RDWR);
+                               if (fd >= 0)
+                               {
+                                       lock = lockf(fd, F_TLOCK, 0);
+                               }
+                               if (lock == 0)
+                               {
+                                       close(fd);
+                                       g_dir_close(gdir);
+                                       g_unlink(path);
+                                       g_free(path);
+                                       g_free(config);
+                                       return pid;
+                               }
+                               g_free(path);
+                               close(fd);
+#else
+                               g_dir_close(gdir);
+                               g_unlink(path);
+                               g_free(path);
+                               g_free(config);
+                               return pid;
+#endif
+                       }
+                       file = g_dir_read_name(gdir);
+               }
+               g_dir_close(gdir);
+       }
+       g_free(config);
+       return -1;
+}
+
 static void
 remove_plist(const gchar *name)
 {
@@ -1171,7 +1348,7 @@ ghb_prefs_to_ui(signal_user_data_t *ud)
                ghb_ui_update(ud, "hbfd", ghb_int64_value(0));
        }
        gval = ghb_settings_get_value(ud->settings, "default_source");
-       ghb_settings_set_value (ud->settings, "source", gval);
+       ghb_settings_set_value (ud->settings, "scan_source", gval);
 
        str = ghb_settings_get_string(ud->settings, "destination_dir");
        ghb_ui_update(ud, "dest_dir", ghb_string_value(str));
@@ -1385,13 +1562,25 @@ ghb_prefs_load(signal_user_data_t *ud)
                {
                        ghb_dict_insert(dict, g_strdup(key), ghb_value_dup(gval));
                }
-               const gchar *dir = g_get_user_special_dir (G_USER_DIRECTORY_VIDEOS);
+
+               const gchar *dir = g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP);
+               if (dir == NULL)
+               {
+                       dir = ".";
+               }
+               ghb_dict_insert(dict, 
+                       g_strdup("ExportDirectory"), ghb_value_dup(ghb_string_value(dir)));
+
+               dir = g_get_user_special_dir (G_USER_DIRECTORY_VIDEOS);
                if (dir == NULL)
                {
                        dir = ".";
                }
                ghb_dict_insert(dict, 
                        g_strdup("destination_dir"), ghb_value_dup(ghb_string_value(dir)));
+
+               ghb_dict_insert(dict, 
+                       g_strdup("SrtDir"), ghb_value_dup(ghb_string_value(dir)));
 #if defined(_WIN32)
                gchar *source;
 
@@ -1522,6 +1711,7 @@ ghb_presets_list_init(
                                                        2, def ? 2 : 0,
                                                        3, color, 
                                                        4, description,
+                                                       5, type == PRESETS_BUILTIN ? 0 : 1,
                                                        -1);
                if (def && piter)
                {
@@ -1565,7 +1755,8 @@ static void
 presets_list_update_item(
        signal_user_data_t *ud, 
        gint *indices,
-       gint len)
+       gint len,
+       gboolean recurse)
 {
        GtkTreeView *treeview;
        GtkTreeStore *store;
@@ -1599,8 +1790,9 @@ presets_list_update_item(
                                                2, def ? 2 : 0,
                                                3, color,
                                                4, description,
+                                               5, type == PRESETS_BUILTIN ? 0 : 1,
                                                -1);
-       if (folder)
+       if (recurse && folder)
        {
                ghb_presets_list_init(ud, indices, len);
        }
@@ -1663,6 +1855,7 @@ presets_list_insert(
                                                2, def ? 2 : 0,
                                                3, color,
                                                4, description,
+                                               5, type == PRESETS_BUILTIN ? 0 : 1,
                                                -1);
        if (folder)
        {
@@ -1720,19 +1913,61 @@ remove_std_presets(signal_user_data_t *ud)
 void
 ghb_save_queue(GValue *queue)
 {
-       store_plist(queue, "queue");
+       pid_t pid;
+       char *path;
+
+       pid = getpid();
+       path = g_strdup_printf ("queue.%d", pid);
+       store_plist(queue, path);
+       g_free(path);
 }
 
 GValue*
 ghb_load_queue()
 {
-       return load_plist("queue");
+       GValue *queue;
+       pid_t pid;
+       char *path;
+
+       pid = getpid();
+       path = g_strdup_printf ("queue.%d", pid);
+       queue = load_plist(path);
+       g_free(path);
+       return queue;
+}
+
+GValue*
+ghb_load_old_queue(int pid)
+{
+       GValue *queue;
+       char *path;
+
+       path = g_strdup_printf ("queue.%d", pid);
+       queue = load_plist(path);
+       g_free(path);
+       return queue;
+}
+
+void
+ghb_remove_old_queue_file(int pid)
+{
+       char *path;
+
+       path = g_strdup_printf ("queue.%d", pid);
+       remove_plist(path);
+       g_free(path);
 }
 
 void
 ghb_remove_queue_file()
 {
-       remove_plist("queue");
+       pid_t pid;
+       char *path;
+
+       pid = getpid();
+       path = g_strdup_printf ("queue.%d", pid);
+       remove_plist(path);
+       g_free(path);
 }
 
 typedef struct
@@ -1763,7 +1998,7 @@ static value_map_t acodec_xlat[] =
 value_map_t container_xlat[] =
 {
        {"MP4 file", "mp4"},
-       {"M4V file", "m4v"},
+       {"M4V file", "mp4"},
        {"MKV file", "mkv"},
        {"AVI file", "mkv"},
        {"OGM file", "mkv"},
@@ -1808,7 +2043,7 @@ value_map_t mix_xlat[] =
 
 value_map_t deint_xlat[] =
 {
-       {"0", "none"},
+       {"0", "off"},
        {"1", "custom"},
        {"2", "fast"},
        {"3", "slow"},
@@ -1818,7 +2053,7 @@ value_map_t deint_xlat[] =
 
 value_map_t denoise_xlat[] =
 {
-       {"0", "none"},
+       {"0", "off"},
        {"1", "custom"},
        {"2", "weak"},
        {"3", "medium"},
@@ -1828,7 +2063,7 @@ value_map_t denoise_xlat[] =
 
 value_map_t detel_xlat[] =
 {
-       {"0", "none"},
+       {"0", "off"},
        {"1", "custom"},
        {"2", "default"},
        {NULL, NULL}
@@ -1836,7 +2071,7 @@ value_map_t detel_xlat[] =
 
 value_map_t decomb_xlat[] =
 {
-       {"0", "none"},
+       {"0", "off"},
        {"1", "custom"},
        {"2", "default"},
        {NULL, NULL}
@@ -1858,7 +2093,14 @@ export_lang_xlat2(GValue *lin_val)
        {
                if (strcmp(str, ghb_language_table[ii].iso639_2) == 0)
                {
-                       gval = ghb_string_value_new(ghb_language_table[ii].eng_name);
+                       const gchar *lang;
+
+                       if (ghb_language_table[ii].native_name[0] != 0)
+                               lang = ghb_language_table[ii].native_name;
+                       else
+                               lang = ghb_language_table[ii].eng_name;
+
+                       gval = ghb_string_value_new(lang);
                        g_free(str);
                        return gval;
                }
@@ -1904,7 +2146,8 @@ import_lang_xlat2(GValue *mac_val)
        str = ghb_value_string(mac_val);
        for (ii = 0; ghb_language_table[ii].eng_name; ii++)
        {
-               if (strcmp(str, ghb_language_table[ii].eng_name) == 0)
+               if ((strcmp(str, ghb_language_table[ii].eng_name) == 0) ||
+                       (strcmp(str, ghb_language_table[ii].native_name) == 0))
                {
                        gval = ghb_string_value_new(ghb_language_table[ii].iso639_2);
                        g_free(str);
@@ -2434,7 +2677,7 @@ import_xlat_preset(GValue *dict)
        const GValue *gval;
 
        vquality = ghb_value_double(preset_dict_get_value(dict, "VideoQualitySlider"));
-       if (vquality < 1.0)
+       if (vquality > 0.0 && vquality < 1.0)
        {
                gint vcodec;
 
@@ -2911,7 +3154,7 @@ settings_save(signal_user_data_t *ud, const GValue *path)
                        ghb_dict_insert(dict, g_strdup("Default"), 
                                                        ghb_boolean_value_new(FALSE));
                }
-               presets_list_update_item(ud, indices, len);
+               presets_list_update_item(ud, indices, len, FALSE);
        }
        else
        {
@@ -2957,7 +3200,9 @@ folder_save(signal_user_data_t *ud, const GValue *path)
                ghb_dict_insert(dict, g_strdup("PresetDescription"), 
                        ghb_value_dup(preset_dict_get_value(
                                ud->settings, "PresetDescription")));
+               presets_list_update_item(ud, indices, len, FALSE);
                g_free(indices);
+               store_presets();
                return;
        }
        else
@@ -3064,7 +3309,7 @@ update_subtitle_presets(signal_user_data_t *ud)
        g_debug("update_subtitle_presets");
        const GValue *subtitle_list, *subtitle;
        GValue *slist, *dict;
-       gint count, ii;
+       gint count, ii, source;
 
        subtitle_list = ghb_settings_get_value(ud->settings, "subtitle_list");
        slist = ghb_array_value_new(8);
@@ -3072,51 +3317,233 @@ update_subtitle_presets(signal_user_data_t *ud)
        for (ii = 0; ii < count; ii++)
        {
                subtitle = ghb_array_get_nth(subtitle_list, ii);
-               dict = ghb_value_dup(subtitle);
-               ghb_array_append(slist, dict);
+               source = ghb_settings_get_int(subtitle, "SubtitleSource");
+               if (source != SRTSUB)
+               {
+                       dict = ghb_value_dup(subtitle);
+                       ghb_array_append(slist, dict);
+               }
        }
-       ghb_settings_set_value(ud->settings, "SubtitleList", slist);
+       ghb_settings_take_value(ud->settings, "SubtitleList", slist);
 }
 
-void
-enforce_preset_type(signal_user_data_t *ud, const GValue *path)
+G_MODULE_EXPORT void
+presets_menu_clicked_cb(GtkWidget *xwidget, signal_user_data_t *ud)
 {
-       gint *indices, len;
-       GtkWidget *normal, *folder;
-       gboolean fold;
+       GtkMenu *menu;
 
-       normal = GHB_WIDGET(ud->builder, "preset_type_normal");
-       folder = GHB_WIDGET(ud->builder, "preset_type_folder");
-       indices = ghb_preset_indices_from_path(presetsPlist, path, &len);
-       if (indices)
+       menu = GTK_MENU(GHB_WIDGET(ud->builder, "presets_menu"));
+       gtk_menu_popup(menu, NULL, NULL, NULL, NULL, 1, 
+                                       gtk_get_current_event_time());
+}
+
+G_MODULE_EXPORT void
+preset_import_clicked_cb(GtkWidget *xwidget, signal_user_data_t *ud)
+{
+       GtkWidget *dialog;
+       GtkResponseType response;
+       gchar *exportDir;
+       gchar *filename;
+       GtkFileFilter *filter;
+
+       g_debug("preset_import_clicked_cb ()");
+
+       dialog = gtk_file_chooser_dialog_new("Import Preset", NULL,
+                               GTK_FILE_CHOOSER_ACTION_OPEN,
+                               GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
+                               GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
+                               NULL);
+
+       filter = gtk_file_filter_new();
+       gtk_file_filter_set_name(filter, "All (*)");
+       gtk_file_filter_add_pattern(filter, "*");
+       gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter);
+
+       filter = gtk_file_filter_new();
+       gtk_file_filter_set_name(filter, "Presets (*.plist)");
+       gtk_file_filter_add_pattern(filter, "*.plist");
+       gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter);
+       gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(dialog), filter);
+
+       exportDir = ghb_settings_get_string(ud->settings, "ExportDirectory");
+       if (exportDir == NULL || exportDir[0] == '\0')
        {
-               fold = ghb_presets_get_folder(presetsPlist, indices, len);
-               if (fold)
-                       gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(folder), 
-                                                                       TRUE);
-               else
-                       gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(normal), 
-                                                                       TRUE);
-               gtk_widget_set_sensitive(folder,  fold);
-               gtk_widget_set_sensitive(normal,  !fold);
-               g_free(indices);
+               exportDir = g_strdup(".");
        }
-       else
+       gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), exportDir);
+       g_free(exportDir);
+
+       response = gtk_dialog_run(GTK_DIALOG(dialog));
+       gtk_widget_hide(dialog);
+       if (response == GTK_RESPONSE_ACCEPT)
        {
-               gtk_widget_set_sensitive(folder, TRUE);
-               gtk_widget_set_sensitive(normal, TRUE);
+               GValue *dict, *array;
+               gchar  *dir;
+               gint count, ii;
+
+               filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
+
+               // import the preset
+               if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR))
+               {
+                       gtk_widget_destroy(dialog);
+                       g_free(filename);
+                       return;
+               }
+               array = ghb_plist_parse_file(filename);
+
+               import_xlat_presets(array);
+               presets_clear_default(array);
+               presets_customize(array);
+
+               count = ghb_array_len(array);
+               for (ii = 0; ii < count; ii++)
+               {
+                       GValue *path, *name;
+                       gint *indices, len;
+                       gint index = 1;
+
+                       dict = ghb_array_get_nth(array, ii);
+                       path = ghb_array_value_new(1);
+                       name = ghb_value_dup(ghb_dict_lookup(dict, "PresetName"));
+                       ghb_array_append(path, name);
+                       indices = ghb_preset_indices_from_path(presetsPlist, path, &len);
+                       // Modify the preset name till we make it unique
+                       while (indices != NULL)
+                       {
+                               gchar *str = ghb_value_string(name);
+
+                               ghb_value_free(path);
+                               g_free(indices);
+
+                               str = g_strdup_printf("%s %d", str, index);
+                               path = ghb_array_value_new(1);
+                               name = ghb_string_value_new(str);
+                               ghb_array_append(path, name);
+                               g_free(str);
+
+                               index++;
+                               indices = ghb_preset_indices_from_path(presetsPlist, path, &len);
+                       }
+                       ghb_dict_insert(dict, g_strdup("PresetName"), ghb_value_dup(name));
+                       indices = presets_find_pos(path, PRESETS_CUSTOM, &len);
+                       ghb_presets_insert(presetsPlist, ghb_value_dup(dict), indices, len);
+                       presets_list_insert(ud, indices, len);
+                       ghb_value_free(path);
+               }
+               ghb_value_free(array);
+
+               exportDir = ghb_settings_get_string(ud->settings, "ExportDirectory");
+               dir = g_path_get_dirname(filename);
+               if (strcmp(dir, exportDir) != 0)
+               {
+                       ghb_settings_set_string(ud->settings, "ExportDirectory", dir);
+                       ghb_pref_save(ud->settings, "ExportDirectory");
+               }
+               g_free(filename);
+               g_free(exportDir);
+               g_free(dir);
+               store_presets();
        }
+       gtk_widget_destroy(dialog);
 }
 
 G_MODULE_EXPORT void
-presets_save_clicked_cb(GtkWidget *xwidget, signal_user_data_t *ud)
+preset_export_clicked_cb(GtkWidget *xwidget, signal_user_data_t *ud)
+{
+       GtkWidget *dialog;
+       GtkResponseType response;
+       GValue *preset;
+       const gchar *name = "";
+       gint count, *indices, len;
+       gchar *exportDir;
+       gchar *filename;
+
+       g_debug("preset_export_clicked_cb ()");
+       preset = ghb_settings_get_value (ud->settings, "preset_selection");
+       if (preset == NULL)
+               return;
+
+       count = ghb_array_len(preset);
+       if (count <= 0)
+               return;
+
+       name = g_value_get_string(ghb_array_get_nth(preset, count-1));
+
+       dialog = gtk_file_chooser_dialog_new("Export Preset", NULL,
+                               GTK_FILE_CHOOSER_ACTION_SAVE,
+                               GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
+                               GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
+                               NULL);
+
+       exportDir = ghb_settings_get_string(ud->settings, "ExportDirectory");
+       if (exportDir == NULL || exportDir[0] == '\0')
+       {
+               exportDir = g_strdup(".");
+       }
+       filename = g_strdup_printf("%s.plist", name);
+       gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), exportDir);
+       gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), filename);
+       g_free(filename);
+       g_free(exportDir);
+
+       indices = ghb_preset_indices_from_path(presetsPlist, preset, &len);
+       if (indices == NULL)
+               return;
+
+       response = gtk_dialog_run(GTK_DIALOG(dialog));
+       gtk_widget_hide(dialog);
+       if (response == GTK_RESPONSE_ACCEPT)
+       {
+               GValue *export, *dict, *array;
+               FILE *file;
+               gchar  *dir;
+
+               filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
+
+               // export the preset
+               dict = presets_get_dict(presetsPlist, indices, len);
+
+               export = ghb_value_dup(dict);
+               array = ghb_array_value_new(1);
+               ghb_array_append(array, export);
+               presets_clear_default(array);
+               presets_customize(array);
+               export_xlat_presets(array);
+
+               file = g_fopen(filename, "w");
+               if (file != NULL)
+               {
+                       ghb_plist_write(file, array);
+                       fclose(file);
+               }
+               ghb_value_free(array);
+
+               exportDir = ghb_settings_get_string(ud->settings, "ExportDirectory");
+               dir = g_path_get_dirname(filename);
+               if (strcmp(dir, exportDir) != 0)
+               {
+                       ghb_settings_set_string(ud->settings, "ExportDirectory", dir);
+                       ghb_pref_save(ud->settings, "ExportDirectory");
+               }
+               g_free(exportDir);
+               g_free(dir);
+               g_free(filename);
+       }
+       gtk_widget_destroy(dialog);
+       g_free(indices);
+}
+
+G_MODULE_EXPORT void
+presets_new_folder_clicked_cb(GtkWidget *xwidget, signal_user_data_t *ud)
 {
        GtkWidget *dialog;
        GtkEntry *entry;
        GtkTextView *desc;
        GtkResponseType response;
-       GValue *preset;
+       GValue *preset, *dict;
        const gchar *name = "";
+       const gchar *description = "";
        gint count, *indices, len;
 
        g_debug("presets_save_clicked_cb ()");
@@ -3127,12 +3554,20 @@ presets_save_clicked_cb(GtkWidget *xwidget, signal_user_data_t *ud)
                name = g_value_get_string(ghb_array_get_nth(preset, count-1));
        else
                count = 1;
-       // Clear the description
+
+       indices = ghb_preset_indices_from_path(presetsPlist, preset, &len);
+       dict = presets_get_dict(presetsPlist, indices, len);
+       if (dict != NULL)
+       {
+               description = g_value_get_string(
+                                                       ghb_dict_lookup(dict, "PresetDescription"));
+               ghb_ui_update(ud, "PresetDescription", ghb_string_value(description));
+       }
+
        desc = GTK_TEXT_VIEW(GHB_WIDGET(ud->builder, "PresetDescription"));
        dialog = GHB_WIDGET(ud->builder, "preset_save_dialog");
        entry = GTK_ENTRY(GHB_WIDGET(ud->builder, "PresetName"));
        gtk_entry_set_text(entry, name);
-       enforce_preset_type(ud, preset);
        response = gtk_dialog_run(GTK_DIALOG(dialog));
        gtk_widget_hide(dialog);
        if (response == GTK_RESPONSE_OK)
@@ -3141,13 +3576,61 @@ presets_save_clicked_cb(GtkWidget *xwidget, signal_user_data_t *ud)
                const gchar *name = gtk_entry_get_text(entry);
                GValue *dest;
 
-               if (ghb_settings_get_boolean(ud->settings, "preset_type_folder"))
+               if (count > MAX_NESTED_PRESET-1)
+                       count = MAX_NESTED_PRESET-1;
+
+               dest = ghb_array_value_new(MAX_NESTED_PRESET);
+               if (indices != NULL)
                {
-                       if (count > MAX_NESTED_PRESET-1)
+                       gint ptype;
+
+                       ptype = ghb_presets_get_type(presetsPlist, indices, len);
+                       if (ptype == PRESETS_CUSTOM)
                        {
-                               count = MAX_NESTED_PRESET-1;
+                               ghb_array_copy(dest, preset, count-1);
                        }
                }
+               ghb_array_append(dest, ghb_string_value_new(name));
+               ghb_widget_to_setting(ud->settings, GTK_WIDGET(desc));
+               folder_save(ud, dest);
+               ghb_value_free(dest);
+       }
+       if (indices != NULL)
+               g_free(indices);
+}
+
+G_MODULE_EXPORT void
+presets_save_clicked_cb(GtkWidget *xwidget, signal_user_data_t *ud)
+{
+       GtkWidget *dialog;
+       GtkEntry *entry;
+       GtkTextView *desc;
+       GtkResponseType response;
+       GValue *preset;
+       const gchar *name = "";
+       gint count, *indices, len;
+
+       g_debug("presets_save_clicked_cb ()");
+       preset = ghb_settings_get_value (ud->settings, "preset_selection");
+
+       count = ghb_array_len(preset);
+       if (count > 0)
+               name = g_value_get_string(ghb_array_get_nth(preset, count-1));
+       else
+               count = 1;
+
+       desc = GTK_TEXT_VIEW(GHB_WIDGET(ud->builder, "PresetDescription"));
+       dialog = GHB_WIDGET(ud->builder, "preset_save_dialog");
+       entry = GTK_ENTRY(GHB_WIDGET(ud->builder, "PresetName"));
+       gtk_entry_set_text(entry, name);
+       response = gtk_dialog_run(GTK_DIALOG(dialog));
+       gtk_widget_hide(dialog);
+       if (response == GTK_RESPONSE_OK)
+       {
+               // save the preset
+               const gchar *name = gtk_entry_get_text(entry);
+               GValue *dest;
+
                dest = ghb_array_value_new(MAX_NESTED_PRESET);
                indices = ghb_preset_indices_from_path(presetsPlist, preset, &len);
                if (indices)
@@ -3159,21 +3642,16 @@ presets_save_clicked_cb(GtkWidget *xwidget, signal_user_data_t *ud)
                        {
                                ghb_array_copy(dest, preset, count-1);
                        }
+                       g_free(indices);
                }
                ghb_array_append(dest, ghb_string_value_new(name));
 
                ghb_widget_to_setting(ud->settings, GTK_WIDGET(desc));
-               if (ghb_settings_get_boolean(ud->settings, "preset_type_folder"))
-               {
-                       folder_save(ud, dest);
-               }
-               else
-               {
-                       // Construct the audio settings presets from the current audio list
-                       update_audio_presets(ud);
-                       update_subtitle_presets(ud);
-                       settings_save(ud, dest);
-               }
+
+               // Construct the audio settings presets from the current audio list
+               update_audio_presets(ud);
+               update_subtitle_presets(ud);
+               settings_save(ud, dest);
                ghb_value_free(dest);
        }
 }
@@ -3185,22 +3663,6 @@ preset_type_changed_cb(GtkWidget *widget, signal_user_data_t *ud)
 }
 
 G_MODULE_EXPORT void
-preset_name_changed_cb(GtkWidget *entry, signal_user_data_t *ud)
-{
-       gchar *name;
-       GValue *preset, *dest;
-       gint count;
-
-       preset = ghb_settings_get_value (ud->settings, "preset_selection");
-       name = ghb_widget_string(entry);
-       dest = ghb_value_dup(preset);
-       count = ghb_array_len(dest);
-       ghb_array_replace(dest, count-1, ghb_string_value_new(name));
-       enforce_preset_type(ud, dest);
-       ghb_value_free(dest);
-}
-
-G_MODULE_EXPORT void
 presets_restore_clicked_cb(GtkWidget *xwidget, signal_user_data_t *ud)
 {
        GValue *preset;
@@ -3519,7 +3981,7 @@ presets_drag_cb(
                        dstpath = gtk_tree_model_get_path (dstmodel, &iter);
                        dst_indices = gtk_tree_path_get_indices(dstpath);
                        dst_len = gtk_tree_path_get_depth(dstpath);
-                       presets_list_update_item(ud, dst_indices, dst_len);
+                       presets_list_update_item(ud, dst_indices, dst_len, TRUE);
                        gtk_tree_path_free(dstpath);
 
                        store_presets();
@@ -3624,6 +4086,74 @@ preset_update_title_deps(signal_user_data_t *ud, ghb_title_info_t *tinfo)
        }
 }
 
+void
+ghb_refresh_preset(signal_user_data_t *ud)
+{
+       ghb_title_info_t tinfo;
+       GValue *preset;
+       gint *indices, len;
+
+       g_debug("ghb_refresh_preset ()");
+       preset = ghb_settings_get_value(ud->settings, "preset_selection");
+       indices = ghb_preset_indices_from_path(presetsPlist, preset, &len);
+       if (indices)
+       {
+               gboolean folder;
+
+               folder = ghb_presets_get_folder(presetsPlist, indices, len);
+               if (!folder)
+               {
+                       ud->dont_clear_presets = TRUE;
+                       // Temporarily set the video_quality range to (0,100)
+                       // This is needed so the video_quality value does not get
+                       // truncated when set.  The range will be readjusted below
+                       GtkWidget *qp = GHB_WIDGET(ud->builder, "VideoQualitySlider");
+                       gtk_range_set_range (GTK_RANGE(qp), 0, 100);
+                       gtk_scale_set_digits(GTK_SCALE(qp), 3);
+                       // Clear the audio list prior to changing the preset.  Existing 
+                       // audio can cause the container extension to be automatically 
+                       // changed when it shouldn't be
+                       ghb_clear_audio_list(ud);
+                       ghb_set_preset_from_indices(ud, indices, len);
+                       gint titleindex;
+                       titleindex = ghb_settings_combo_int(ud->settings, "title");
+                       ghb_set_pref_audio(titleindex, ud);
+                       ghb_set_pref_subtitle(titleindex, ud);
+                       ghb_settings_set_boolean(ud->settings, "preset_modified", FALSE);
+                       if (ghb_get_title_info (&tinfo, titleindex))
+                       {
+                               preset_update_title_deps(ud, &tinfo);
+                       }
+                       ghb_set_scale (ud, GHB_PIC_KEEP_PAR);
+                       ud->dont_clear_presets = FALSE;
+
+                       gdouble vqmin, vqmax, step, page;
+                       gint digits;
+                       gboolean inverted;
+
+                       ghb_vquality_range(ud, &vqmin, &vqmax, &step, 
+                                                               &page, &digits, &inverted);
+                       gtk_range_set_range (GTK_RANGE(qp), vqmin, vqmax);
+                       gtk_range_set_increments (GTK_RANGE(qp), step, page);
+                       gtk_scale_set_digits(GTK_SCALE(qp), digits);
+                       gtk_range_set_inverted (GTK_RANGE(qp), inverted);
+
+                       gchar *text;
+                       gint crop[4];
+                       GtkWidget *crop_widget;
+                       crop[0] = ghb_settings_get_int(ud->settings, "PictureTopCrop");
+                       crop[1] = ghb_settings_get_int(ud->settings, "PictureBottomCrop");
+                       crop[2] = ghb_settings_get_int(ud->settings, "PictureLeftCrop");
+                       crop[3] = ghb_settings_get_int(ud->settings, "PictureRightCrop");
+                       crop_widget = GHB_WIDGET (ud->builder, "crop_values");
+                       text = g_strdup_printf("%d:%d:%d:%d", 
+                                                                       crop[0], crop[1], crop[2], crop[3]);
+                       gtk_label_set_text (GTK_LABEL(crop_widget), text);
+                       g_free(text);
+               }
+       }
+}
+
 G_MODULE_EXPORT void
 presets_list_selection_changed_cb(GtkTreeSelection *selection, signal_user_data_t *ud)
 {
@@ -3663,7 +4193,6 @@ presets_list_selection_changed_cb(GtkTreeSelection *selection, signal_user_data_
                        // changed when it shouldn't be
                        ghb_clear_audio_list(ud);
                        ghb_set_preset_from_indices(ud, indices, len);
-                       gtk_tree_path_free(treepath);
                        gint titleindex;
                        titleindex = ghb_settings_combo_int(ud->settings, "title");
                        ghb_set_pref_audio(titleindex, ud);
@@ -3700,6 +4229,7 @@ presets_list_selection_changed_cb(GtkTreeSelection *selection, signal_user_data_
                        gtk_label_set_text (GTK_LABEL(crop_widget), text);
                        g_free(text);
                }
+               gtk_tree_path_free(treepath);
                gtk_widget_set_sensitive(widget, TRUE);
        }
        else
@@ -3707,6 +4237,21 @@ presets_list_selection_changed_cb(GtkTreeSelection *selection, signal_user_data_
                g_debug("No selection???  Perhaps unselected.");
                gtk_widget_set_sensitive(widget, FALSE);
        }
+       if (ghb_settings_combo_int(ud->settings, "PtoPType") == 0)
+       {
+               gint start, end;
+               start = ghb_settings_get_int(ud->settings, "start_point");
+               end = ghb_settings_get_int(ud->settings, "end_point");
+               widget = GHB_WIDGET (ud->builder, "ChapterMarkers");
+               gtk_widget_set_sensitive(widget, TRUE);
+               if (start == end)
+               {
+                       ud->dont_clear_presets = TRUE;
+                       ghb_ui_update(ud, "ChapterMarkers", ghb_boolean_value(FALSE));
+                       ud->dont_clear_presets = FALSE;
+                       gtk_widget_set_sensitive(widget, FALSE);
+               }
+       }
 }
 
 void
@@ -3764,3 +4309,50 @@ presets_default_clicked_cb(GtkWidget *xwidget, signal_user_data_t *ud)
        }
 }
 
+G_MODULE_EXPORT void
+preset_edited_cb(
+       GtkCellRendererText *cell, 
+       gchar *path, 
+       gchar *text, 
+       signal_user_data_t *ud)
+{
+       GtkTreePath *treepath;
+       GtkTreeStore *store;
+       GtkTreeView *treeview;
+       GtkTreeIter iter;
+       gint *indices, len, count;
+       GValue *dict;
+       GValue *preset, *dest;
+       
+       g_debug("preset_edited_cb ()");
+       g_debug("path (%s)", path);
+       g_debug("text (%s)", text);
+
+       preset = ghb_settings_get_value (ud->settings, "preset_selection");
+       dest = ghb_array_value_new(MAX_NESTED_PRESET);
+       count = ghb_array_len(preset);
+       ghb_array_copy(dest, preset, count-1);
+       ghb_array_append(dest, ghb_string_value_new(text));
+       indices = ghb_preset_indices_from_path(presetsPlist, dest, &len);
+       ghb_value_free(dest);
+       if (indices != NULL)
+       {
+               // Already exists
+               g_free(indices);
+               return;
+       }
+
+       treeview = GTK_TREE_VIEW(GHB_WIDGET(ud->builder, "presets_list"));
+       store = GTK_TREE_STORE(gtk_tree_view_get_model(treeview));
+       treepath = gtk_tree_path_new_from_string (path);
+       indices = gtk_tree_path_get_indices(treepath);
+       len = gtk_tree_path_get_depth(treepath);
+       gtk_tree_model_get_iter(GTK_TREE_MODEL(store), &iter, treepath);
+       gtk_tree_store_set(store, &iter, 0, text, -1);
+
+       dict = presets_get_dict(presetsPlist, indices, len);
+       ghb_dict_insert(dict, g_strdup("PresetName"), ghb_string_value_new(text));
+       store_presets();
+       gtk_tree_path_free (treepath);
+}
+