diff options
Diffstat (limited to 'ui/profile.c')
-rw-r--r-- | ui/profile.c | 167 |
1 files changed, 127 insertions, 40 deletions
diff --git a/ui/profile.c b/ui/profile.c index 59687fac..04025fda 100644 --- a/ui/profile.c +++ b/ui/profile.c @@ -16,6 +16,9 @@ #include <glib.h> +#include <epan/prefs.h> +#include <epan/prefs-int.h> + #include <wsutil/filesystem.h> #include "profile.h" @@ -26,8 +29,8 @@ #include <wsutil/file_util.h> #include <wsutil/ws_assert.h> -static GList *current_profiles = NULL; -static GList *edited_profiles = NULL; +static GList *current_profiles; +static GList *edited_profiles; #define PROF_OPERATION_NEW 1 #define PROF_OPERATION_EDIT 2 @@ -40,9 +43,12 @@ GList * edited_profile_list(void) { return g_list_first(edited_profiles); } +static void load_profile_settings(profile_def *profile); +static void save_profile_settings(profile_def *profile); + static GList * add_profile_entry(GList *fl, const char *profilename, const char *reference, int status, - gboolean is_global, gboolean from_global, gboolean is_import) + bool is_global, bool from_global, bool is_import) { profile_def *profile; @@ -65,19 +71,20 @@ remove_profile_entry(GList *fl, GList *fl_entry) profile = (profile_def *) fl_entry->data; g_free(profile->name); g_free(profile->reference); + g_free(profile->auto_switch_filter); g_free(profile); list = g_list_remove_link(fl, fl_entry); g_list_free_1(fl_entry); return list; } -const gchar * -get_profile_parent (const gchar *profilename) +const char * +get_profile_parent (const char *profilename) { GList *fl_entry = g_list_first(edited_profiles); - guint no_edited = g_list_length(edited_profiles); + unsigned no_edited = g_list_length(edited_profiles); profile_def *profile; - guint i; + unsigned i; if (fl_entry) { /* We have edited profiles, find parent */ @@ -103,13 +110,13 @@ get_profile_parent (const gchar *profilename) return profilename; } -gchar *apply_profile_changes(void) +char *apply_profile_changes(void) { char *pf_dir_path, *pf_dir_path2, *pf_filename; GList *fl1, *fl2; profile_def *profile1, *profile2; - gboolean found; - gchar *err_msg; + bool found; + char *err_msg; /* First validate all profile names */ fl1 = edited_profile_list(); @@ -117,7 +124,7 @@ gchar *apply_profile_changes(void) profile1 = (profile_def *) fl1->data; g_strstrip(profile1->name); if ((err_msg = profile_name_is_valid(profile1->name)) != NULL) { - gchar *message = ws_strdup_printf("%s\nProfiles unchanged.", err_msg); + char *message = ws_strdup_printf("%s\nProfiles unchanged.", err_msg); g_free(err_msg); return message; } @@ -186,7 +193,7 @@ gchar *apply_profile_changes(void) profile1->status = PROF_STAT_EXISTS; g_free (profile1->reference); profile1->reference = g_strdup(profile1->name); - profile1->is_import = FALSE; + profile1->is_import = false; } } else if (profile1->status == PROF_STAT_CHANGED) { if (strcmp(profile1->reference, profile1->name)!=0) { @@ -209,7 +216,7 @@ gchar *apply_profile_changes(void) /* Last remove deleted */ fl1 = current_profile_list(); while (fl1) { - found = FALSE; + found = false; profile1 = (profile_def *) fl1->data; fl2 = edited_profile_list(); while (fl2) { @@ -217,12 +224,12 @@ gchar *apply_profile_changes(void) if (!profile2->is_global) { if (strcmp(profile1->name, profile2->name)==0) { /* Profile exists in both lists */ - found = TRUE; + found = true; } else if (strcmp(profile1->name, profile2->reference)==0) { /* Profile has been renamed, update reference to the new name */ g_free (profile2->reference); profile2->reference = g_strdup(profile2->name); - found = TRUE; + found = true; } } fl2 = g_list_next(fl2); @@ -240,13 +247,24 @@ gchar *apply_profile_changes(void) fl1 = g_list_next(fl1); } + /* Save our profile settings */ + for (fl1 = edited_profile_list() ; fl1 ; fl1 = fl1->next) { + profile1 = (profile_def *) fl1->data; + if (profile1->is_global) { + continue; + } + if (profile1->prefs_changed) { + save_profile_settings(profile1); + } + } + copy_profile_list(); return NULL; } GList * add_to_profile_list(const char *name, const char *expression, int status, - gboolean is_global, gboolean from_global, gboolean is_imported) + bool is_global, bool from_global, bool is_imported) { edited_profiles = add_profile_entry(edited_profiles, name, expression, status, is_global, from_global, is_imported); @@ -261,7 +279,7 @@ remove_from_profile_list(GList *fl_entry) } void -empty_profile_list(gboolean edit_list) +empty_profile_list(bool edit_list) { GList **flpp; @@ -297,7 +315,7 @@ copy_profile_list(void) flp_src = edited_profiles; /* throw away the "old" destination list - a NULL list is ok here */ - empty_profile_list(FALSE); + empty_profile_list(false); /* copy the list entries */ while(flp_src) { @@ -305,7 +323,12 @@ copy_profile_list(void) current_profiles = add_profile_entry(current_profiles, profile->name, profile->reference, profile->status, - profile->is_global, profile->from_global, FALSE); + profile->is_global, profile->from_global, false); + if (profile->auto_switch_filter) { + profile_def *new_profile = (profile_def *) g_list_last(current_profiles)->data; + new_profile->auto_switch_filter = g_strdup(profile->auto_switch_filter); + } + flp_src = g_list_next(flp_src); } } @@ -315,16 +338,17 @@ init_profile_list(void) { WS_DIR *dir; /* scanned directory */ WS_DIRENT *file; /* current file */ - const gchar *name; + const char *name; GList *local_profiles = NULL; GList *global_profiles = NULL; - GList *iter; - gchar *profiles_dir, *filename; + GList *iter, *item; + char *profiles_dir, *filename; - empty_profile_list(TRUE); + empty_profile_list(true); /* Default entry */ - add_to_profile_list(DEFAULT_PROFILE, DEFAULT_PROFILE, PROF_STAT_DEFAULT, FALSE, FALSE, FALSE); + item = add_to_profile_list(DEFAULT_PROFILE, DEFAULT_PROFILE, PROF_STAT_DEFAULT, false, false, false); + load_profile_settings((profile_def *)item->data); /* Local (user) profiles */ profiles_dir = get_profiles_dir(); @@ -344,8 +368,9 @@ init_profile_list(void) local_profiles = g_list_sort(local_profiles, (GCompareFunc)g_ascii_strcasecmp); for (iter = g_list_first(local_profiles); iter; iter = g_list_next(iter)) { - name = (gchar *)iter->data; - add_to_profile_list(name, name, PROF_STAT_EXISTS, FALSE, FALSE, FALSE); + name = (char *)iter->data; + item = add_to_profile_list(name, name, PROF_STAT_EXISTS, false, false, false); + load_profile_settings((profile_def *)item->data); } g_list_free_full(local_profiles, g_free); @@ -367,8 +392,8 @@ init_profile_list(void) global_profiles = g_list_sort(global_profiles, (GCompareFunc)g_ascii_strcasecmp); for (iter = g_list_first(global_profiles); iter; iter = g_list_next(iter)) { - name = (gchar *)iter->data; - add_to_profile_list(name, name, PROF_STAT_EXISTS, TRUE, TRUE, FALSE); + name = (char *)iter->data; + add_to_profile_list(name, name, PROF_STAT_EXISTS, true, true, false); } g_list_free_full(global_profiles, g_free); @@ -376,26 +401,26 @@ init_profile_list(void) copy_profile_list (); } -gchar * -profile_name_is_valid(const gchar *name) +char * +profile_name_is_valid(const char *name) { - gchar *reason = NULL; - gchar *message; + char *reason = NULL; + char *message; #ifdef _WIN32 char *invalid_dir_char = "\\/:*?\"<>|"; - gboolean invalid = FALSE; + bool invalid = false; int i; for (i = 0; i < 9; i++) { if (strchr(name, invalid_dir_char[i])) { /* Invalid character in directory */ - invalid = TRUE; + invalid = true; } } if (name[0] == '.' || name[strlen(name)-1] == '.') { /* Profile name cannot start or end with period */ - invalid = TRUE; + invalid = true; } if (invalid) { reason = ws_strdup_printf("start or end with period (.), or contain any of the following characters:\n" @@ -417,11 +442,11 @@ profile_name_is_valid(const gchar *name) return NULL; } -gboolean delete_current_profile(void) { - const gchar *name = get_profile_name(); +bool delete_current_profile(void) { + const char *name = get_profile_name(); char *pf_dir_path; - if (profile_exists(name, FALSE) && strcmp (name, DEFAULT_PROFILE) != 0) { + if (profile_exists(name, false) && strcmp (name, DEFAULT_PROFILE) != 0) { if (delete_persconffile_profile(name, &pf_dir_path) == -1) { simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "Can't delete profile directory\n\"%s\":\n%s.", @@ -429,8 +454,70 @@ gboolean delete_current_profile(void) { g_free(pf_dir_path); } else { - return TRUE; + return true; } } - return FALSE; + return false; +} + +// Use a settings file in case we ever want to include an author, description, +// URL, etc. +#define PROFILE_SETTINGS_FILENAME "profile_settings" +#define AUTO_SWITCH_FILTER_KEY "auto_switch_filter" + +static char *get_profile_settings_path(const char *profile_name) { + char *profile_settings_path; + char *profile_dir = get_profile_dir(profile_name, false); + profile_settings_path = g_build_filename(profile_dir, PROFILE_SETTINGS_FILENAME, NULL); + g_free(profile_dir); + + return profile_settings_path; +} + +/* Set */ +static prefs_set_pref_e +set_profile_setting(char *key, const char *value, void *profile_ptr, bool return_range_errors _U_) +{ + profile_def *profile = (profile_def *) profile_ptr; + if (strcmp(key, AUTO_SWITCH_FILTER_KEY) == 0) { + g_free(profile->auto_switch_filter); + profile->auto_switch_filter = g_strdup(value); + } + + return PREFS_SET_OK; +} + +static void load_profile_settings(profile_def *profile) +{ + char *profile_settings_path = get_profile_settings_path(profile->name); + FILE *fp; + + if ((fp = ws_fopen(profile_settings_path, "r")) != NULL) { + read_prefs_file(profile_settings_path, fp, set_profile_setting, profile); + fclose(fp); + } + g_free(profile_settings_path); +} + +void save_profile_settings(profile_def *profile) +{ + char *profile_settings_path = get_profile_settings_path(profile->name); + FILE *fp; + + if ((fp = ws_fopen(profile_settings_path, "w")) == NULL) { + simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, + "Can't open recent file\n\"%s\": %s.", profile_settings_path, + g_strerror(errno)); + g_free(profile_settings_path); + return; + } + g_free(profile_settings_path); + + fprintf(fp, "# \"%s\" profile settings file for %s " VERSION ". Edit with care.\n", + profile->name, get_configuration_namespace()); + + fprintf(fp, "\n# Automatically switch to this profile if this display filter matches.\n"); + fprintf(fp, AUTO_SWITCH_FILTER_KEY ": %s\n", profile->auto_switch_filter); + + fclose(fp); } |