summaryrefslogtreecommitdiff
path: root/apps
diff options
context:
space:
mode:
Diffstat (limited to 'apps')
-rw-r--r--apps/plugins/text_viewer/tv_action.c27
-rw-r--r--apps/plugins/text_viewer/tv_bookmark.c16
-rw-r--r--apps/plugins/text_viewer/tv_bookmark.h3
-rw-r--r--apps/plugins/text_viewer/tv_menu.c2
-rw-r--r--apps/plugins/text_viewer/tv_preferences.c7
-rw-r--r--apps/plugins/text_viewer/tv_preferences.h10
-rw-r--r--apps/plugins/text_viewer/tv_settings.c127
-rw-r--r--apps/plugins/text_viewer/tv_settings.h1
8 files changed, 130 insertions, 63 deletions
diff --git a/apps/plugins/text_viewer/tv_action.c b/apps/plugins/text_viewer/tv_action.c
index 54cec38f72..3a0d895331 100644
--- a/apps/plugins/text_viewer/tv_action.c
+++ b/apps/plugins/text_viewer/tv_action.c
@@ -28,6 +28,9 @@
28#include "tv_settings.h" 28#include "tv_settings.h"
29#include "tv_window.h" 29#include "tv_window.h"
30 30
31bool bookmarks_changed = false;
32bool scrolled = false;
33
31bool tv_init_action(unsigned char **buf, size_t *size) 34bool tv_init_action(unsigned char **buf, size_t *size)
32{ 35{
33 /* initialize bookmarks and window modules */ 36 /* initialize bookmarks and window modules */
@@ -36,10 +39,6 @@ bool tv_init_action(unsigned char **buf, size_t *size)
36 39
37static void tv_finalize_action(void) 40static void tv_finalize_action(void)
38{ 41{
39 /* save preference and bookmarks */
40 if (!tv_save_settings())
41 rb->splash(HZ, "Can't save preferences and bookmarks");
42
43 /* finalize bookmark modules */ 42 /* finalize bookmark modules */
44 tv_finalize_bookmark(); 43 tv_finalize_bookmark();
45 44
@@ -50,8 +49,19 @@ static void tv_finalize_action(void)
50void tv_exit(void) 49void tv_exit(void)
51{ 50{
52 /* save preference and bookmarks */ 51 /* save preference and bookmarks */
53 if (!tv_save_settings()) 52 DEBUGF("preferences_changed=%d\tbookmarks_changed=%d\tscrolled=%d\n",preferences_changed,bookmarks_changed,scrolled);
54 rb->splash(HZ, "Can't save preferences and bookmarks"); 53 if ( preferences_changed || bookmarks_changed
54#ifndef HAVE_DISK_STORAGE
55 || scrolled
56#endif
57 )
58 {
59 DEBUGF("Saving settings\n");
60 if (!tv_save_settings())
61 rb->splash(HZ, "Can't save preferences and bookmarks");
62 }
63 else
64 DEBUGF("Skip saving settings\n");
55 65
56 /* finalize modules */ 66 /* finalize modules */
57 tv_finalize_action(); 67 tv_finalize_action();
@@ -95,6 +105,7 @@ void tv_scroll_up(unsigned mode)
95#endif 105#endif
96 } 106 }
97 tv_move_screen(offset_page, offset_line, SEEK_CUR); 107 tv_move_screen(offset_page, offset_line, SEEK_CUR);
108 scrolled = true;
98} 109}
99 110
100void tv_scroll_down(unsigned mode) 111void tv_scroll_down(unsigned mode)
@@ -111,6 +122,7 @@ void tv_scroll_down(unsigned mode)
111#endif 122#endif
112 } 123 }
113 tv_move_screen(offset_page, offset_line, SEEK_CUR); 124 tv_move_screen(offset_page, offset_line, SEEK_CUR);
125 scrolled = true;
114} 126}
115 127
116void tv_scroll_left(unsigned mode) 128void tv_scroll_left(unsigned mode)
@@ -130,6 +142,7 @@ void tv_scroll_left(unsigned mode)
130 offset_window--; 142 offset_window--;
131 } 143 }
132 tv_move_window(offset_window, offset_column); 144 tv_move_window(offset_window, offset_column);
145 scrolled = true;
133} 146}
134 147
135void tv_scroll_right(unsigned mode) 148void tv_scroll_right(unsigned mode)
@@ -149,6 +162,7 @@ void tv_scroll_right(unsigned mode)
149 offset_window++; 162 offset_window++;
150 } 163 }
151 tv_move_window(offset_window, offset_column); 164 tv_move_window(offset_window, offset_column);
165 scrolled = true;
152} 166}
153 167
154void tv_top(void) 168void tv_top(void)
@@ -186,4 +200,5 @@ unsigned tv_menu(void)
186void tv_add_or_remove_bookmark(void) 200void tv_add_or_remove_bookmark(void)
187{ 201{
188 tv_toggle_bookmark(); 202 tv_toggle_bookmark();
203 bookmarks_changed = true;
189} 204}
diff --git a/apps/plugins/text_viewer/tv_bookmark.c b/apps/plugins/text_viewer/tv_bookmark.c
index 815e7a1f91..5f77d2a92c 100644
--- a/apps/plugins/text_viewer/tv_bookmark.c
+++ b/apps/plugins/text_viewer/tv_bookmark.c
@@ -32,7 +32,6 @@ enum {
32 TV_BOOKMARK_USER = 2, 32 TV_BOOKMARK_USER = 2,
33}; 33};
34 34
35#define SERIALIZE_BOOKMARK_SIZE 8
36 35
37struct tv_bookmark_info { 36struct tv_bookmark_info {
38 struct tv_screen_pos pos; 37 struct tv_screen_pos pos;
@@ -287,11 +286,8 @@ bool tv_deserialize_bookmarks(int fd)
287 return res; 286 return res;
288} 287}
289 288
290static bool tv_write_bookmark_info(int fd, const struct tv_bookmark_info *b) 289static void tv_write_bookmark_info(unsigned char *p, const struct tv_bookmark_info *b)
291{ 290{
292 unsigned char buf[SERIALIZE_BOOKMARK_SIZE];
293 unsigned char *p = buf;
294
295 *p++ = b->pos.file_pos >> 24; 291 *p++ = b->pos.file_pos >> 24;
296 *p++ = b->pos.file_pos >> 16; 292 *p++ = b->pos.file_pos >> 16;
297 *p++ = b->pos.file_pos >> 8; 293 *p++ = b->pos.file_pos >> 8;
@@ -302,21 +298,17 @@ static bool tv_write_bookmark_info(int fd, const struct tv_bookmark_info *b)
302 298
303 *p++ = b->pos.line; 299 *p++ = b->pos.line;
304 *p = b->flag; 300 *p = b->flag;
305
306 return (rb->write(fd, buf, SERIALIZE_BOOKMARK_SIZE) >= 0);
307} 301}
308 302
309int tv_serialize_bookmarks(int fd) 303int tv_serialize_bookmarks(unsigned char *buf)
310{ 304{
311 int i; 305 int i;
312 306
313 if (rb->write(fd, &bookmark_count, 1) < 0) 307 buf[0] = bookmark_count;
314 return 0;
315 308
316 for (i = 0; i < bookmark_count; i++) 309 for (i = 0; i < bookmark_count; i++)
317 { 310 {
318 if (!tv_write_bookmark_info(fd, &bookmarks[i])) 311 tv_write_bookmark_info(buf + i * SERIALIZE_BOOKMARK_SIZE + 1, &bookmarks[i]);
319 break;
320 } 312 }
321 return i * SERIALIZE_BOOKMARK_SIZE + 1; 313 return i * SERIALIZE_BOOKMARK_SIZE + 1;
322} 314}
diff --git a/apps/plugins/text_viewer/tv_bookmark.h b/apps/plugins/text_viewer/tv_bookmark.h
index b0b91077d4..85b52b2c4f 100644
--- a/apps/plugins/text_viewer/tv_bookmark.h
+++ b/apps/plugins/text_viewer/tv_bookmark.h
@@ -29,6 +29,7 @@
29 29
30/* Maximum amount of register possible bookmarks */ 30/* Maximum amount of register possible bookmarks */
31#define TV_MAX_BOOKMARKS 16 31#define TV_MAX_BOOKMARKS 16
32#define SERIALIZE_BOOKMARK_SIZE 8
32 33
33/* 34/*
34 * initialize the bookmark module 35 * initialize the bookmark module
@@ -83,7 +84,7 @@ void tv_create_system_bookmark(void);
83 * Return 84 * Return
84 * the size of the result 85 * the size of the result
85 */ 86 */
86int tv_serialize_bookmarks(int fd); 87int tv_serialize_bookmarks(unsigned char *buf);
87 88
88/* 89/*
89 * deserialize the bookmark array 90 * deserialize the bookmark array
diff --git a/apps/plugins/text_viewer/tv_menu.c b/apps/plugins/text_viewer/tv_menu.c
index 5e79fd469a..9be8312732 100644
--- a/apps/plugins/text_viewer/tv_menu.c
+++ b/apps/plugins/text_viewer/tv_menu.c
@@ -329,6 +329,8 @@ unsigned tv_display_menu(void)
329 case 1: /* change settings */ 329 case 1: /* change settings */
330 tv_copy_preferences(&new_prefs); 330 tv_copy_preferences(&new_prefs);
331 result = tv_options_menu(); 331 result = tv_options_menu();
332 if (tv_compare_preferences(&new_prefs))
333 preferences_changed = true;
332 if (!tv_set_preferences(&new_prefs)) 334 if (!tv_set_preferences(&new_prefs))
333 result = TV_MENU_RESULT_ERROR; 335 result = TV_MENU_RESULT_ERROR;
334 break; 336 break;
diff --git a/apps/plugins/text_viewer/tv_preferences.c b/apps/plugins/text_viewer/tv_preferences.c
index 5e6677ea58..6d5c1127fc 100644
--- a/apps/plugins/text_viewer/tv_preferences.c
+++ b/apps/plugins/text_viewer/tv_preferences.c
@@ -28,6 +28,8 @@ static struct tv_preferences prefs;
28/* read-only preferences pointer, for access by other files */ 28/* read-only preferences pointer, for access by other files */
29const struct tv_preferences * const preferences = &prefs; 29const struct tv_preferences * const preferences = &prefs;
30 30
31bool preferences_changed = false;
32
31static int listner_count = 0; 33static int listner_count = 0;
32 34
33#define TV_MAX_LISTNERS 5 35#define TV_MAX_LISTNERS 5
@@ -91,6 +93,11 @@ void tv_copy_preferences(struct tv_preferences *copy_prefs)
91 rb->memcpy(copy_prefs, preferences, sizeof(struct tv_preferences)); 93 rb->memcpy(copy_prefs, preferences, sizeof(struct tv_preferences));
92} 94}
93 95
96bool tv_compare_preferences(struct tv_preferences *copy_prefs)
97{
98 return rb->memcmp(copy_prefs, preferences, sizeof(struct tv_preferences)) != 0;
99}
100
94void tv_set_default_preferences(struct tv_preferences *p) 101void tv_set_default_preferences(struct tv_preferences *p)
95{ 102{
96 p->word_mode = WM_WRAP; 103 p->word_mode = WM_WRAP;
diff --git a/apps/plugins/text_viewer/tv_preferences.h b/apps/plugins/text_viewer/tv_preferences.h
index bc871a7255..bb448b0f4b 100644
--- a/apps/plugins/text_viewer/tv_preferences.h
+++ b/apps/plugins/text_viewer/tv_preferences.h
@@ -104,6 +104,7 @@ struct tv_preferences {
104 * global pointer to the preferences (read-only) 104 * global pointer to the preferences (read-only)
105 */ 105 */
106extern const struct tv_preferences * const preferences; 106extern const struct tv_preferences * const preferences;
107extern bool preferences_changed;
107 108
108/* 109/*
109 * change the preferences 110 * change the preferences
@@ -126,6 +127,15 @@ bool tv_set_preferences(const struct tv_preferences *new_prefs);
126void tv_copy_preferences(struct tv_preferences *copy_prefs); 127void tv_copy_preferences(struct tv_preferences *copy_prefs);
127 128
128/* 129/*
130 * compare the preferences structs (binary)
131 *
132 * return
133 * true differs
134 * false identical
135 */
136bool tv_compare_preferences(struct tv_preferences *copy_prefs);
137
138/*
129 * set the default settings 139 * set the default settings
130 * 140 *
131 * [Out] p 141 * [Out] p
diff --git a/apps/plugins/text_viewer/tv_settings.c b/apps/plugins/text_viewer/tv_settings.c
index 3004ac3d60..20e8212147 100644
--- a/apps/plugins/text_viewer/tv_settings.c
+++ b/apps/plugins/text_viewer/tv_settings.c
@@ -120,6 +120,10 @@
120#define TV_SETTINGS_FIRST_VERSION 0x32 120#define TV_SETTINGS_FIRST_VERSION 0x32
121 121
122#define TV_PREFERENCES_SIZE (28 + MAX_PATH) 122#define TV_PREFERENCES_SIZE (28 + MAX_PATH)
123#define TV_MAX_FILE_RECORD_SIZE (MAX_PATH+2 + TV_PREFERENCES_SIZE + TV_MAX_BOOKMARKS*SERIALIZE_BOOKMARK_SIZE+1)
124
125static off_t stored_preferences_offset = 0;
126static int stored_preferences_size = 0;
123 127
124/* ---------------------------------------------------------------------------- 128/* ----------------------------------------------------------------------------
125 * read/write the preferences 129 * read/write the preferences
@@ -220,9 +224,8 @@ static bool tv_read_preferences(int pfd, int version, struct tv_preferences *pre
220 return true; 224 return true;
221} 225}
222 226
223static bool tv_write_preferences(int pfd, const struct tv_preferences *prefs) 227static void tv_serialize_preferences(unsigned char *buf, const struct tv_preferences *prefs)
224{ 228{
225 unsigned char buf[TV_PREFERENCES_SIZE];
226 unsigned char *p = buf; 229 unsigned char *p = buf;
227 230
228 rb->memset(buf, 0, TV_PREFERENCES_SIZE); 231 rb->memset(buf, 0, TV_PREFERENCES_SIZE);
@@ -248,6 +251,13 @@ static bool tv_write_preferences(int pfd, const struct tv_preferences *prefs)
248#ifdef HAVE_LCD_BITMAP 251#ifdef HAVE_LCD_BITMAP
249 rb->strlcpy(buf + 28, prefs->font_name, MAX_PATH); 252 rb->strlcpy(buf + 28, prefs->font_name, MAX_PATH);
250#endif 253#endif
254}
255
256static bool tv_write_preferences(int pfd, const struct tv_preferences *prefs)
257{
258 unsigned char buf[TV_PREFERENCES_SIZE];
259
260 tv_serialize_preferences(buf, prefs);
251 261
252 return (rb->write(pfd, buf, TV_PREFERENCES_SIZE) >= 0); 262 return (rb->write(pfd, buf, TV_PREFERENCES_SIZE) >= 0);
253} 263}
@@ -427,6 +437,7 @@ bool tv_load_settings(const unsigned char *file_name)
427 int version; 437 int version;
428 unsigned int size; 438 unsigned int size;
429 struct tv_preferences prefs; 439 struct tv_preferences prefs;
440 off_t current_pref_offset;
430 441
431 if (!rb->file_exists(TV_SETTINGS_FILE)) 442 if (!rb->file_exists(TV_SETTINGS_FILE))
432 tv_convert_settings_file(); 443 tv_convert_settings_file();
@@ -438,6 +449,8 @@ bool tv_load_settings(const unsigned char *file_name)
438 { 449 {
439 version = buf[TV_SETTINGS_HEADER_SIZE - 1] - TV_SETTINGS_FIRST_VERSION; 450 version = buf[TV_SETTINGS_HEADER_SIZE - 1] - TV_SETTINGS_FIRST_VERSION;
440 fcount = (buf[TV_SETTINGS_HEADER_SIZE] << 8) | buf[TV_SETTINGS_HEADER_SIZE+1]; 451 fcount = (buf[TV_SETTINGS_HEADER_SIZE] << 8) | buf[TV_SETTINGS_HEADER_SIZE+1];
452
453 current_pref_offset = rb->lseek(fd, 0, SEEK_CUR);
441 454
442 for (i = 0; i < fcount; i++) 455 for (i = 0; i < fcount; i++)
443 { 456 {
@@ -448,10 +461,15 @@ bool tv_load_settings(const unsigned char *file_name)
448 { 461 {
449 if (tv_read_preferences(fd, version, &prefs)) 462 if (tv_read_preferences(fd, version, &prefs))
450 res = tv_deserialize_bookmarks(fd); 463 res = tv_deserialize_bookmarks(fd);
464
465 if (res) {
466 stored_preferences_offset = current_pref_offset;
467 stored_preferences_size = size;
468 }
451 469
452 break; 470 break;
453 } 471 }
454 rb->lseek(fd, size, SEEK_CUR); 472 current_pref_offset = rb->lseek(fd, size, SEEK_CUR);
455 } 473 }
456 } 474 }
457 rb->close(fd); 475 rb->close(fd);
@@ -473,38 +491,74 @@ bool tv_load_settings(const unsigned char *file_name)
473 return tv_set_preferences(&prefs); 491 return tv_set_preferences(&prefs);
474} 492}
475 493
476static bool tv_copy_settings(int sfd, int dfd, int size)
477{
478 unsigned char buf[MAX_PATH];
479 int i = size / MAX_PATH;
480
481 size %= MAX_PATH;
482
483 while (i--)
484 {
485 if ((rb->read(sfd, buf, MAX_PATH) < 0) || (rb->write(dfd, buf, MAX_PATH) < 0))
486 return false;
487 }
488
489 return ((rb->read(sfd, buf, size) >= 0) && (rb->write(dfd, buf, size) >= 0));
490}
491
492bool tv_save_settings(void) 494bool tv_save_settings(void)
493{ 495{
494 unsigned char buf[MAX_PATH+2]; 496 unsigned char buf[TV_MAX_FILE_RECORD_SIZE];
497 unsigned char preferences_buf[TV_MAX_FILE_RECORD_SIZE];
495 unsigned int fcount = 0; 498 unsigned int fcount = 0;
499 unsigned int new_fcount = 0;
496 unsigned int i; 500 unsigned int i;
497 int ofd = -1; 501 int ofd = -1;
498 int tfd; 502 int tfd;
499 off_t size; 503 off_t size;
504 off_t preferences_buf_size;
500 bool res = true; 505 bool res = true;
501 506
502 /* add reading page to bookmarks */ 507 /* add reading page to bookmarks */
503 tv_create_system_bookmark(); 508 tv_create_system_bookmark();
504 509
510 /* storing preferences record in memory */
511 rb->memset(preferences_buf, 0, MAX_PATH);
512 rb->strlcpy(preferences_buf, preferences->file_name, MAX_PATH);
513 preferences_buf_size = MAX_PATH + 2;
514
515 tv_serialize_preferences(preferences_buf + preferences_buf_size, preferences);
516 preferences_buf_size += TV_PREFERENCES_SIZE;
517 preferences_buf_size += tv_serialize_bookmarks(preferences_buf + preferences_buf_size);
518 size = preferences_buf_size - (MAX_PATH + 2);
519 preferences_buf[MAX_PATH + 0] = size >> 8;
520 preferences_buf[MAX_PATH + 1] = size;
521
522
523 /* Just overwrite preferences if possible*/
524 if ( (stored_preferences_offset > 0) && (stored_preferences_size == size) )
525 {
526 DEBUGF("Saving preferences: overwriting\n");
527 if ((tfd = rb->open(TV_SETTINGS_FILE, O_WRONLY)) < 0)
528 return false;
529 rb->lseek(tfd, stored_preferences_offset, SEEK_SET);
530 res = (rb->write(tfd, preferences_buf, preferences_buf_size) >= 0);
531 rb->close(tfd);
532 return res;
533 }
534
535
505 if (!rb->file_exists(TV_SETTINGS_FILE)) 536 if (!rb->file_exists(TV_SETTINGS_FILE))
506 tv_convert_settings_file(); 537 tv_convert_settings_file();
507 538
539
540 /* Try appending preferences */
541 if ( (stored_preferences_offset == 0) &&
542 ( (tfd = rb->open(TV_SETTINGS_FILE, O_RDWR)) >= 0) )
543 {
544 DEBUGF("Saving preferences: appending\n");
545 rb->lseek(tfd, 0, SEEK_END);
546 if (rb->write(tfd, preferences_buf, preferences_buf_size) < 0)
547 return false;
548
549 rb->lseek(tfd, TV_SETTINGS_HEADER_SIZE, SEEK_SET);
550 rb->read(tfd, buf, 2);
551 fcount = (buf[0] << 8) | buf[1];
552 fcount ++;
553 buf[0] = fcount >> 8;
554 buf[1] = fcount;
555 rb->lseek(tfd, TV_SETTINGS_HEADER_SIZE, SEEK_SET);
556 res = rb->write(tfd, buf, 2) >= 0;
557
558 rb->close(tfd);
559 return res;
560 }
561
508 /* create header for the temporary file */ 562 /* create header for the temporary file */
509 rb->memcpy(buf, TV_SETTINGS_HEADER, TV_SETTINGS_HEADER_SIZE - 1); 563 rb->memcpy(buf, TV_SETTINGS_HEADER, TV_SETTINGS_HEADER_SIZE - 1);
510 buf[TV_SETTINGS_HEADER_SIZE - 1] = TV_SETTINGS_VERSION; 564 buf[TV_SETTINGS_HEADER_SIZE - 1] = TV_SETTINGS_VERSION;
@@ -539,12 +593,13 @@ bool tv_save_settings(void)
539 rb->lseek(ofd, size, SEEK_CUR); 593 rb->lseek(ofd, size, SEEK_CUR);
540 else 594 else
541 { 595 {
542 if ((rb->write(tfd, buf, MAX_PATH + 2) < 0) || 596 if ((rb->read(ofd, buf + (MAX_PATH + 2), size) < 0) ||
543 (!tv_copy_settings(ofd, tfd, size))) 597 (rb->write(tfd, buf, size + MAX_PATH + 2) < 0))
544 { 598 {
545 res = false; 599 res = false;
546 break; 600 break;
547 } 601 }
602 new_fcount++;
548 } 603 }
549 } 604 }
550 } 605 }
@@ -555,31 +610,15 @@ bool tv_save_settings(void)
555 { 610 {
556 /* save to current read file's preferences and bookmarks */ 611 /* save to current read file's preferences and bookmarks */
557 res = false; 612 res = false;
558 rb->memset(buf, 0, MAX_PATH);
559 rb->strlcpy(buf, preferences->file_name, MAX_PATH);
560 613
561 if (rb->write(tfd, buf, MAX_PATH + 2) >= 0) 614 if (rb->write(tfd, preferences_buf, preferences_buf_size) >= 0)
562 { 615 {
563 if (tv_write_preferences(tfd, preferences)) 616 rb->lseek(tfd, TV_SETTINGS_HEADER_SIZE, SEEK_SET);
564 {
565 size = tv_serialize_bookmarks(tfd);
566 if (size > 0)
567 {
568 size += TV_PREFERENCES_SIZE;
569 rb->lseek(tfd, -size - 2, SEEK_CUR);
570 buf[0] = size >> 8;
571 buf[1] = size;
572 if (rb->write(tfd, buf, 2) >= 0)
573 {
574 rb->lseek(tfd, TV_SETTINGS_HEADER_SIZE, SEEK_SET);
575 617
576 fcount++; 618 new_fcount++;
577 buf[0] = fcount >> 8; 619 buf[0] = new_fcount >> 8;
578 buf[1] = fcount; 620 buf[1] = new_fcount;
579 res = (rb->write(tfd, buf, 2) >= 0); 621 res = (rb->write(tfd, buf, 2) >= 0);
580 }
581 }
582 }
583 } 622 }
584 } 623 }
585 rb->close(tfd); 624 rb->close(tfd);
diff --git a/apps/plugins/text_viewer/tv_settings.h b/apps/plugins/text_viewer/tv_settings.h
index 65b31ecd69..afcf532cd0 100644
--- a/apps/plugins/text_viewer/tv_settings.h
+++ b/apps/plugins/text_viewer/tv_settings.h
@@ -63,6 +63,7 @@ bool tv_load_settings(const unsigned char *file_name);
63 63
64/* 64/*
65 * save the settings at each file 65 * save the settings at each file
66 * supposed to be called only once at plugin exit
66 * 67 *
67 * return 68 * return
68 * true success 69 * true success