diff --git a/apps/plugins/viewer.c b/apps/plugins/viewer.c index a726a4b..02c0ae3 100755 --- a/apps/plugins/viewer.c +++ b/apps/plugins/viewer.c @@ -25,6 +25,36 @@ PLUGIN_HEADER +/* global settings file + * binary file, so dont use .cfg + * + * setting file format + * + * part byte count + * -------------------------------- + * 'TVGS' 4 + * version 1 + * word_mode 1 + * line_mode 1 + * view_mode 1 + * encoding 1 + * scrollbar_mode 1 + * need_scrollbar 1 + * page_mode 1 + * page_number_mode 1 + * title_mode 1 + * scroll_mode 1 + * autoscroll_speed 1 + * font name MAX_PATH + */ +#define GLOBAL_SETTINGS_FILE VIEWERS_DIR "/viewer.dat" + +/* temporary file */ +#define GLOBAL_SETTINGS_TMP_FILE VIEWERS_DIR "/viewer_file.tmp" + +#define GLOBAL_SETTINGS_HEADER "\x54\x56\x47\x53\x31" /* header="TVGS" version=1 */ +#define GLOBAL_SETTINGS_H_SIZE 5 + /* preferences and bookmarks at each file * binary file, so dont use .cfg * @@ -1831,6 +1861,46 @@ static bool viewer_write_bookmark_infos(int bfd) return true; } +static bool viewer_load_global_settings(void) +{ + unsigned buf[GLOBAL_SETTINGS_H_SIZE]; + int sfd = rb->open(GLOBAL_SETTINGS_FILE, O_RDONLY); + + if (sfd < 0) + return false; + + if ((rb->read(sfd, buf, GLOBAL_SETTINGS_H_SIZE) != GLOBAL_SETTINGS_H_SIZE) || + rb->memcmp(buf, GLOBAL_SETTINGS_HEADER, GLOBAL_SETTINGS_H_SIZE) || + !viewer_read_preferences(sfd)) + { + rb->close(sfd); + return false; + } + rb->close(sfd); + return true; +} + +static bool viewer_save_global_settings(void) +{ + int sfd = rb->open(GLOBAL_SETTINGS_TMP_FILE, O_WRONLY|O_CREAT|O_TRUNC); + + if (sfd < 0) + return false; + + if (rb->write(sfd, &GLOBAL_SETTINGS_HEADER, GLOBAL_SETTINGS_H_SIZE) + != GLOBAL_SETTINGS_H_SIZE || + !viewer_write_preferences(sfd)) + { + rb->close(sfd); + rb->remove(GLOBAL_SETTINGS_TMP_FILE); + return false; + } + rb->close(sfd); + rb->remove(GLOBAL_SETTINGS_FILE); + rb->rename(GLOBAL_SETTINGS_TMP_FILE, GLOBAL_SETTINGS_FILE); + return true; +} + static void viewer_load_settings(void) { unsigned char buf[MAX_PATH+2]; @@ -1881,8 +1951,9 @@ static void viewer_load_settings(void) read_end: if (!res) { - /* set default preference */ - viewer_default_preferences(); + /* load global settings */ + if (!viewer_load_global_settings()) + viewer_default_preferences(); file_pos = 0; screen_top_ptr = buffer; @@ -2464,7 +2535,7 @@ MAKE_MENU(option_menu, "Viewer Options", NULL, Icon_NOICON, #endif &scroll_mode_item, &autoscroll_speed_item); -static bool viewer_options_menu(void) +static bool viewer_options_menu(bool is_global) { bool result; struct preferences tmp_prefs; @@ -2473,7 +2544,7 @@ static bool viewer_options_menu(void) result = (rb->do_menu(&option_menu, NULL, NULL, false) == MENU_ATTACHED_USB); - if (rb->memcmp(&tmp_prefs, &prefs, sizeof(struct preferences))) + if (!is_global && rb->memcmp(&tmp_prefs, &prefs, sizeof(struct preferences))) { if (rb->strcmp(prefs.font, sys_font)) set_font(prefs.font); @@ -2499,7 +2570,7 @@ static void viewer_menu(void) MENUITEM_STRINGLIST(menu, "Viewer Menu", NULL, "Return", "Viewer Options", "Show Playback Menu", "Select Bookmark", - "Quit"); + "Global Settings", "Quit"); result = rb->do_menu(&menu, NULL, NULL, false); switch (result) @@ -2507,7 +2578,7 @@ static void viewer_menu(void) case 0: /* return */ break; case 1: /* change settings */ - done = viewer_options_menu(); + done = viewer_options_menu(false); break; case 2: /* playback control */ playback_control(NULL); @@ -2519,7 +2590,19 @@ static void viewer_menu(void) if (prefs.scroll_mode == PAGE && cline > 1) viewer_scroll_to_top_line(); break; - case 4: /* quit */ + case 4: /* change global settings */ + { + struct preferences orig_prefs; + + rb->memcpy(&orig_prefs, &prefs, sizeof(struct preferences)); + if (!viewer_load_global_settings()) + viewer_default_preferences(); + done = viewer_options_menu(true); + viewer_save_global_settings(); + rb->memcpy(&prefs, &orig_prefs, sizeof(struct preferences)); + } + break; + case 5: /* quit */ viewer_exit(NULL); done = true; break;