More work on PDBox by Wincent Balin. The PDBox plug-in is now working with the pdpod_...
[kugel-rb.git] / apps / plugins / calendar.c
blob873a09b97030e8aab12e788b31f0d8917cc7deda
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 * (based upon 1.1 by calpefrosch) updated by www.HuwSy.ukhackers.net
11 * Copyright (C) 2002
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ****************************************************************************/
22 #include "plugin.h"
24 #if defined(HAVE_LCD_BITMAP) && (CONFIG_RTC != 0)
26 #include <timefuncs.h>
28 PLUGIN_HEADER
30 #if CONFIG_KEYPAD == RECORDER_PAD
31 #define CALENDAR_QUIT BUTTON_OFF
32 #define CALENDAR_SELECT BUTTON_PLAY
33 #define CALENDAR_NEXT_WEEK BUTTON_DOWN
34 #define CALENDAR_PREV_WEEK BUTTON_UP
35 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
36 #define CALENDAR_PREV_DAY BUTTON_LEFT
37 #define CALENDAR_NEXT_MONTH (BUTTON_ON|BUTTON_DOWN)
38 #define CALENDAR_PREV_MONTH (BUTTON_ON|BUTTON_UP)
40 #elif CONFIG_KEYPAD == ARCHOS_AV300_PAD
41 #define CALENDAR_QUIT BUTTON_OFF
42 #define CALENDAR_SELECT BUTTON_SELECT
43 #define CALENDAR_NEXT_WEEK BUTTON_DOWN
44 #define CALENDAR_PREV_WEEK BUTTON_UP
45 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
46 #define CALENDAR_PREV_DAY BUTTON_LEFT
47 #define CALENDAR_NEXT_MONTH (BUTTON_ON|BUTTON_DOWN)
48 #define CALENDAR_PREV_MONTH (BUTTON_ON|BUTTON_UP)
50 #elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
51 (CONFIG_KEYPAD == IRIVER_H300_PAD)
52 #define CALENDAR_QUIT BUTTON_OFF
53 #define CALENDAR_SELECT BUTTON_SELECT
54 #define CALENDAR_NEXT_WEEK BUTTON_DOWN
55 #define CALENDAR_PREV_WEEK BUTTON_UP
56 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
57 #define CALENDAR_PREV_DAY BUTTON_LEFT
58 #define CALENDAR_NEXT_MONTH BUTTON_MODE
59 #define CALENDAR_PREV_MONTH BUTTON_REC
61 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
62 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
63 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
64 #define CALENDAR_QUIT (BUTTON_SELECT|BUTTON_MENU)
65 #define CALENDAR_SELECT (BUTTON_SELECT|BUTTON_REL)
66 #define CALENDAR_NEXT_WEEK BUTTON_SCROLL_FWD
67 #define CALENDAR_PREV_WEEK BUTTON_SCROLL_BACK
68 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
69 #define CALENDAR_PREV_DAY BUTTON_LEFT
70 #define CALENDAR_NEXT_MONTH BUTTON_PLAY
71 #define CALENDAR_PREV_MONTH (BUTTON_MENU|BUTTON_REL)
73 #elif (CONFIG_KEYPAD == IAUDIO_X5M5_PAD)
74 #define CALENDAR_QUIT BUTTON_POWER
75 #define CALENDAR_SELECT BUTTON_SELECT
76 #define CALENDAR_NEXT_WEEK BUTTON_DOWN
77 #define CALENDAR_PREV_WEEK BUTTON_UP
78 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
79 #define CALENDAR_PREV_DAY BUTTON_LEFT
80 #define CALENDAR_NEXT_MONTH BUTTON_PLAY
81 #define CALENDAR_PREV_MONTH BUTTON_REC
83 #elif CONFIG_KEYPAD == GIGABEAT_PAD
84 #define CALENDAR_QUIT BUTTON_POWER
85 #define CALENDAR_SELECT BUTTON_SELECT
86 #define CALENDAR_NEXT_WEEK BUTTON_DOWN
87 #define CALENDAR_PREV_WEEK BUTTON_UP
88 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
89 #define CALENDAR_PREV_DAY BUTTON_LEFT
90 #define CALENDAR_NEXT_MONTH BUTTON_VOL_DOWN
91 #define CALENDAR_PREV_MONTH BUTTON_VOL_UP
93 #elif CONFIG_KEYPAD == SANSA_E200_PAD
94 #define CALENDAR_QUIT BUTTON_POWER
95 #define CALENDAR_SELECT BUTTON_SELECT
96 #define CALENDAR_NEXT_WEEK BUTTON_SCROLL_FWD
97 #define CALENDAR_PREV_WEEK BUTTON_SCROLL_BACK
98 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
99 #define CALENDAR_PREV_DAY BUTTON_LEFT
100 #define CALENDAR_NEXT_MONTH BUTTON_DOWN
101 #define CALENDAR_PREV_MONTH BUTTON_UP
103 #elif CONFIG_KEYPAD == SANSA_FUZE_PAD
104 #define CALENDAR_QUIT (BUTTON_HOME|BUTTON_REPEAT)
105 #define CALENDAR_SELECT BUTTON_SELECT
106 #define CALENDAR_NEXT_WEEK BUTTON_SCROLL_FWD
107 #define CALENDAR_PREV_WEEK BUTTON_SCROLL_BACK
108 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
109 #define CALENDAR_PREV_DAY BUTTON_LEFT
110 #define CALENDAR_NEXT_MONTH BUTTON_DOWN
111 #define CALENDAR_PREV_MONTH BUTTON_UP
113 #elif CONFIG_KEYPAD == SANSA_C200_PAD || \
114 CONFIG_KEYPAD == SANSA_CLIP_PAD || \
115 CONFIG_KEYPAD == SANSA_M200_PAD
116 #define CALENDAR_QUIT BUTTON_POWER
117 #define CALENDAR_SELECT BUTTON_SELECT
118 #define CALENDAR_NEXT_WEEK BUTTON_DOWN
119 #define CALENDAR_PREV_WEEK BUTTON_UP
120 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
121 #define CALENDAR_PREV_DAY BUTTON_LEFT
122 #define CALENDAR_NEXT_MONTH BUTTON_VOL_UP
123 #define CALENDAR_PREV_MONTH BUTTON_VOL_DOWN
125 #elif CONFIG_KEYPAD == IRIVER_H10_PAD
126 #define CALENDAR_QUIT BUTTON_POWER
127 #define CALENDAR_SELECT BUTTON_PLAY
128 #define CALENDAR_NEXT_WEEK BUTTON_SCROLL_DOWN
129 #define CALENDAR_PREV_WEEK BUTTON_SCROLL_UP
130 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
131 #define CALENDAR_PREV_DAY BUTTON_LEFT
132 #define CALENDAR_NEXT_MONTH BUTTON_FF
133 #define CALENDAR_PREV_MONTH BUTTON_REW
135 #elif CONFIG_KEYPAD == GIGABEAT_S_PAD
136 #define CALENDAR_QUIT BUTTON_BACK
137 #define CALENDAR_SELECT BUTTON_SELECT
138 #define CALENDAR_NEXT_WEEK BUTTON_DOWN
139 #define CALENDAR_PREV_WEEK BUTTON_UP
140 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
141 #define CALENDAR_PREV_DAY BUTTON_LEFT
142 #define CALENDAR_NEXT_MONTH BUTTON_NEXT
143 #define CALENDAR_PREV_MONTH BUTTON_PREV
145 #elif CONFIG_KEYPAD == MROBE100_PAD
146 #define CALENDAR_QUIT BUTTON_POWER
147 #define CALENDAR_SELECT BUTTON_SELECT
148 #define CALENDAR_NEXT_WEEK BUTTON_DOWN
149 #define CALENDAR_PREV_WEEK BUTTON_UP
150 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
151 #define CALENDAR_PREV_DAY BUTTON_LEFT
152 #define CALENDAR_NEXT_MONTH (BUTTON_MENU|BUTTON_DOWN)
153 #define CALENDAR_PREV_MONTH (BUTTON_MENU|BUTTON_UP)
155 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
156 #define CALENDAR_QUIT BUTTON_RC_REC
157 #define CALENDAR_SELECT BUTTON_RC_PLAY
158 #define CALENDAR_NEXT_WEEK BUTTON_RC_VOL_DOWN
159 #define CALENDAR_PREV_WEEK BUTTON_RC_VOL_UP
160 #define CALENDAR_NEXT_DAY BUTTON_RC_FF
161 #define CALENDAR_PREV_DAY BUTTON_RC_REW
162 #define CALENDAR_NEXT_MONTH BUTTON_RC_MODE
163 #define CALENDAR_PREV_MONTH BUTTON_RC_MENU
165 #elif (CONFIG_KEYPAD == COWOND2_PAD)
166 #define CALENDAR_QUIT BUTTON_POWER
167 #define CALENDAR_SELECT BUTTON_CENTER
168 #define CALENDAR_NEXT_WEEK BUTTON_DOWN
169 #define CALENDAR_PREV_WEEK BUTTON_UP
170 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
171 #define CALENDAR_PREV_DAY BUTTON_LEFT
172 #define CALENDAR_NEXT_MONTH BUTTON_BOTTOMRIGHT
173 #define CALENDAR_PREV_MONTH BUTTON_BOTTOMLEFT
175 #elif CONFIG_KEYPAD == PHILIPS_HDD1630_PAD
176 #define CALENDAR_QUIT BUTTON_POWER
177 #define CALENDAR_SELECT BUTTON_SELECT
178 #define CALENDAR_NEXT_WEEK BUTTON_DOWN
179 #define CALENDAR_PREV_WEEK BUTTON_UP
180 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
181 #define CALENDAR_PREV_DAY BUTTON_LEFT
182 #define CALENDAR_NEXT_MONTH BUTTON_VOL_DOWN
183 #define CALENDAR_PREV_MONTH BUTTON_VOL_UP
185 #elif (CONFIG_KEYPAD == ONDAVX747_PAD)
186 #define CALENDAR_QUIT BUTTON_POWER
187 #define CALENDAR_SELECT BUTTON_MENU
188 #define CALENDAR_NEXT_WEEK BUTTON_VOL_DOWN
189 #define CALENDAR_PREV_WEEK BUTTON_VOL_UP
190 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
191 #define CALENDAR_PREV_DAY BUTTON_LEFT
192 #define CALENDAR_NEXT_MONTH BUTTON_BOTTOMRIGHT
193 #define CALENDAR_PREV_MONTH BUTTON_BOTTOMLEFT
195 #elif CONFIG_KEYPAD == MROBE500_PAD
196 #define CALENDAR_QUIT BUTTON_POWER
198 #else
199 #error "No keypad setting."
200 #endif
202 #ifdef HAVE_TOUCHSCREEN
203 #ifndef CALENDAR_QUIT
204 #define CALENDAR_QUIT BUTTON_MIDLEFT
205 #endif
206 #ifndef CALENDAR_SELECT
207 #define CALENDAR_SELECT BUTTON_CENTER
208 #endif
209 #ifndef CALENDAR_NEXT_DAY
210 #define CALENDAR_NEXT_DAY BUTTON_TOPLEFT
211 #endif
212 #ifndef CALENDAR_PREV_DAY
213 #define CALENDAR_PREV_DAY BUTTON_BOTTOMLEFT
214 #endif
215 #ifndef CALENDAR_NEXT_WEEK
216 #define CALENDAR_NEXT_WEEK BUTTON_TOPMIDDLE
217 #endif
218 #ifndef CALENDAR_PREV_WEEK
219 #define CALENDAR_PREV_WEEK BUTTON_BOTTOMMIDDLE
220 #endif
221 #ifndef CALENDAR_NEXT_MONTH
222 #define CALENDAR_NEXT_MONTH BUTTON_TOPRIGHT
223 #endif
224 #ifndef CALENDAR_PREV_MONTH
225 #define CALENDAR_PREV_MONTH BUTTON_BOTTOMRIGHT
226 #endif
227 #endif
229 #define X_OFFSET ((LCD_WIDTH%7)/2)
230 #if LCD_HEIGHT <= 80
231 #define Y_OFFSET 1
232 #else
233 #define Y_OFFSET 4
234 #endif
235 #define CELL_WIDTH (LCD_WIDTH / 7)
236 #define CELL_HEIGHT (LCD_HEIGHT / 7)
238 static bool leap_year;
239 /* days_in_month[][0] is for December */
240 static const int days_in_month[2][13] = {
241 {31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
242 {31, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
245 static const char *dayname_long[7] = {"Mon","Tue","Wed","Thu","Fri","Sat","Sun"};
246 static const char *dayname_short[7] = {"M","T","W","T","F","S","S"};
248 struct today {
249 int mday; /* day of the month */
250 int mon; /* month */
251 int year; /* year since 1900 */
252 int wday; /* day of the week */
255 struct shown {
256 int mday; /* day of the month */
257 int mon; /* month */
258 int year; /* year since 1900 */
259 int wday; /* day of the week */
260 int firstday; /* first (w)day of month */
261 int lastday; /* last (w)day of month */
264 static bool use_system_font = false;
266 static bool been_in_usb_mode = false;
268 /* leap year -- account for gregorian reformation in 1752 */
269 static int is_leap_year(int yr)
271 return ((yr) <= 1752 ? !((yr) % 4) : \
272 (!((yr) % 4) && ((yr) % 100)) || !((yr) % 400)) ? 1:0 ;
275 /* searches the weekday of the first day in month,
276 * relative to the given values */
277 static int calc_weekday( struct shown *shown )
279 return ( shown->wday + 36 - shown->mday ) % 7 ;
283 static void calendar_init(struct today *today, struct shown *shown)
285 int w,h;
286 #if CONFIG_RTC
287 struct tm *tm;
288 #else
289 (void)today;
290 #endif
291 rb->lcd_getstringsize("A",&w,&h);
292 if ( ((w * 14) > LCD_WIDTH) || ((h * 7) > LCD_HEIGHT) )
294 rb->lcd_setfont(FONT_SYSFIXED);
295 use_system_font = true;
297 rb->lcd_clear_display();
298 #if CONFIG_RTC
299 tm = rb->get_time();
300 today->mon = tm->tm_mon +1;
301 today->year = 2000+tm->tm_year%100;
302 today->wday = tm->tm_wday-1;
303 today->mday = tm->tm_mday;
304 shown->mday = today->mday;
305 shown->mon = today->mon;
306 shown->year = today->year;
307 shown->wday = today->wday;
308 #endif
309 shown->firstday = calc_weekday(shown);
310 leap_year = is_leap_year(shown->year);
313 static void draw_headers(void)
315 int i,w,h;
316 int x = X_OFFSET;
317 const char **dayname = (const char**)&dayname_long;
319 for (i = 0; i < 7; i++)
321 rb->lcd_getstringsize(dayname[i],&w,&h);
322 if (w > CELL_WIDTH)
324 dayname = (const char**)&dayname_short;
325 break;
329 rb->lcd_getstringsize("A",&w,&h);
330 for (i = 0; i < 7; i++)
332 rb->lcd_putsxy(x, 0 , dayname[i]);
333 x += CELL_WIDTH;
335 rb->lcd_hline(0, LCD_WIDTH-1 ,h);
338 static bool day_has_memo[32];
339 static bool wday_has_memo[7];
340 static void draw_calendar(struct shown *shown)
342 int w,h;
343 int x,y,pos,days_per_month,j;
344 char buffer[9];
345 const char *monthname[] = {
346 "Jan",
347 "Feb",
348 "Mar",
349 "Apr",
350 "May",
351 "Jun",
352 "Jul",
353 "Aug",
354 "Sep",
355 "Oct",
356 "Nov",
357 "Dec"
359 if(use_system_font)
360 rb->lcd_setfont(FONT_SYSFIXED);
361 rb->lcd_getstringsize("A",&w,&h);
362 rb->lcd_clear_display();
363 draw_headers();
364 if (shown->firstday > 6)
365 shown->firstday -= 7;
366 pos = shown->firstday;
367 days_per_month = days_in_month[leap_year][shown->mon];
368 x = X_OFFSET + (pos * CELL_WIDTH);
369 y = Y_OFFSET + h;
370 for (j = 1; j <= days_per_month; j++)
372 if ( (day_has_memo[j]) || (wday_has_memo[pos]) )
373 rb->snprintf(buffer,4,"%02d.", j);
374 else
375 rb->snprintf(buffer,4,"%02d", j);
376 if (shown->mday == j)
378 rb->lcd_set_drawmode(DRMODE_SOLID);
379 rb->lcd_fillrect(x, y - 1, CELL_WIDTH - 1, CELL_HEIGHT);
380 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
381 shown->wday = pos;
383 else
385 rb->lcd_set_drawmode(DRMODE_SOLID);
387 rb->lcd_putsxy(x, y, buffer);
388 x += CELL_WIDTH;
389 pos++;
390 if (pos >= 7)
392 pos = 0;
393 x = X_OFFSET;
394 y += CELL_HEIGHT;
397 rb->lcd_set_drawmode(DRMODE_SOLID);
398 rb->lcd_vline(LCD_WIDTH-w*8-10,LCD_HEIGHT-h-3,LCD_HEIGHT-1);
399 rb->lcd_hline(LCD_WIDTH-w*8-10,LCD_WIDTH-1,LCD_HEIGHT-h-3);
400 rb->snprintf(buffer,9,"%s %04d",monthname[shown->mon-1],shown->year);
401 rb->lcd_putsxy(LCD_WIDTH-w*8-8,LCD_HEIGHT-h-1,buffer);
402 shown->lastday = pos;
403 rb->lcd_update();
406 #define MAX_CHAR_MEMO_LEN 64
407 #define MAX_MEMOS_IN_A_MONTH 128
408 struct memo {
409 char message[MAX_CHAR_MEMO_LEN];
410 int day;
411 int month;
412 int file_pointer_start;
413 int file_pointer_end;
414 int year;
415 int wday;
416 int type;
417 } memos[MAX_MEMOS_IN_A_MONTH];
418 static int pointer_array[MAX_MEMOS_IN_A_MONTH];
419 static int memos_in_memory = 0;
420 static int memos_in_shown_memory = 0;
422 static void load_memo(struct shown *shown)
424 int i, k, fp;
425 bool exit = false;
426 char temp_memo1[2];
427 char temp_memo2[3];
428 char temp_memo4[5];
429 temp_memo1[1] = 0;
430 temp_memo2[2] = 0;
431 temp_memo4[4] = 0;
432 for (k = 0; k < memos_in_memory; k++)
434 memos[k].day = 0;
435 memos[k].month = 0;
436 memos[k].file_pointer_start = 0;
437 memos[k].file_pointer_end = 0;
438 memos[k].year = 0;
439 memos[k].type = 0;
440 memos[k].wday = 0;
441 for (i = 0; i < MAX_CHAR_MEMO_LEN; i++)
442 memos[k].message[i] = 0;
444 for (k = 1; k < 32; k++)
445 day_has_memo[k] = false;
446 for (k = 0; k < 7; k++)
447 wday_has_memo[k] = false;
448 memos_in_memory = 0;
449 fp = rb->open(ROCKBOX_DIR "/.memo",O_RDONLY);
450 if (fp > -1)
452 int count = rb->filesize(fp);
453 rb->lseek(fp, 0, SEEK_SET);
454 while (!exit)
456 memos[memos_in_memory].file_pointer_start = rb->lseek(fp, 0,
457 SEEK_CUR);
458 if (rb->read(fp, temp_memo2, 2) == 2)
459 memos[memos_in_memory].day = rb->atoi(&temp_memo2[0]);
460 else
461 memos[memos_in_memory].day = 0;
462 if (rb->read(fp, temp_memo2, 2) == 2)
463 memos[memos_in_memory].month = rb->atoi(&temp_memo2[0]);
464 else
465 memos[memos_in_memory].month = 0;
466 if (rb->read(fp, temp_memo4, 4) == 4)
467 memos[memos_in_memory].year = rb->atoi(&temp_memo4[0]);
468 else
469 memos[memos_in_memory].year = 0;
470 /* as the year returned is sometimes yearmonth, ie if yr should =
471 2003, and month = 06, then it returns 200306 */
472 if (memos[memos_in_memory].year > (shown->year * 10))
473 memos[memos_in_memory].year = (memos[memos_in_memory].year -
474 memos[memos_in_memory].month) /
475 100;
476 if (rb->read(fp, temp_memo1, 1) == 1)
477 memos[memos_in_memory].wday = rb->atoi(&temp_memo1[0]);
478 else
479 memos[memos_in_memory].wday = 0;
480 if (rb->read(fp, temp_memo1, 1) == 1)
481 memos[memos_in_memory].type = rb->atoi(&temp_memo1[0]);
482 else
483 memos[memos_in_memory].type = 0;
484 for (k = 0; k <= count; k++)
486 if (rb->read(fp, temp_memo1, 1) == 1)
488 if (
489 (memos[memos_in_memory].type < 2)
492 (memos[memos_in_memory].type == 2)
494 (memos[memos_in_memory].month == shown->mon)
498 (memos[memos_in_memory].type > 2)
500 (memos[memos_in_memory].month == shown->mon)
502 (memos[memos_in_memory].year == shown->year)
506 if (temp_memo1[0] == '\n')
508 if (memos[memos_in_memory].type > 0)
509 day_has_memo[memos[memos_in_memory].day] =
510 true;
511 else
512 wday_has_memo[memos[memos_in_memory].wday] =
513 true;
514 memos[memos_in_memory++].file_pointer_end =
515 rb->lseek(fp, 0, SEEK_CUR);
517 else if ( (temp_memo1[0] != '\r') &&
518 (temp_memo1[0] != '\t') &&
519 k < MAX_CHAR_MEMO_LEN-1 )
520 memos[memos_in_memory].message[k] = temp_memo1[0];
522 if (temp_memo1[0] == '\n')
523 break;
525 else
527 memos[memos_in_memory].day = 0;
528 memos[memos_in_memory].month = 0;
529 memos[memos_in_memory].file_pointer_start = 0;
530 memos[memos_in_memory].file_pointer_end = 0;
531 memos[memos_in_memory].year = 0;
532 memos[memos_in_memory].type = 0;
533 memos[memos_in_memory].wday = 0;
534 memos[memos_in_memory].message[0] = 0;
535 exit = true;
536 break;
541 rb->close(fp);
544 static bool save_memo(int changed, bool new_mod, struct shown *shown)
546 int fp,fq;
547 fp = rb->open(ROCKBOX_DIR "/.memo",O_RDONLY | O_CREAT);
548 fq = rb->creat(ROCKBOX_DIR "/~temp");
549 if ( (fq != -1) && (fp != -1) )
551 int i;
552 char temp[MAX_CHAR_MEMO_LEN];
553 rb->lseek(fp, 0, SEEK_SET);
554 for (i = 0; i < memos[changed].file_pointer_start; i++)
556 rb->read(fp, temp, 1);
557 rb->write(fq,temp,1);
559 if (new_mod)
561 rb->fdprintf(fq, "%02d%02d%04d%01d%01d%s\n",
562 memos[changed].day,
563 memos[changed].month,
564 memos[changed].year,
565 memos[changed].wday,
566 memos[changed].type,
567 memos[changed].message);
569 rb->lseek(fp, memos[changed].file_pointer_end, SEEK_SET);
570 while(rb->read(fp, temp, 1) == 1)
572 rb->write(fq,temp,1);
574 rb->close(fp);
575 rb->close(fq);
576 rb->remove(ROCKBOX_DIR "/.memo");
577 rb->rename(ROCKBOX_DIR "/~temp", ROCKBOX_DIR "/.memo");
578 load_memo(shown);
579 return true;
581 else if (fp != -1)
582 rb->close(fp);
583 else if (fq != -1)
584 rb->close(fq);
585 return false;
588 static void add_memo(struct shown *shown, int type)
590 bool saved = false;
591 if (rb->kbd_input(memos[memos_in_memory].message,
592 sizeof memos[memos_in_memory].message) != -1)
594 if (rb->strlen(memos[memos_in_memory].message))
596 memos[memos_in_memory].file_pointer_start = 0;
597 memos[memos_in_memory].file_pointer_end = 0;
598 memos[memos_in_memory].day = shown->mday;
599 memos[memos_in_memory].month = shown->mon;
600 memos[memos_in_memory].wday = shown->wday;
601 memos[memos_in_memory].year = shown->year;
602 memos[memos_in_memory].type = type;
603 if (save_memo(memos_in_memory,true,shown))
605 saved = true;
607 else
609 memos[memos_in_memory].file_pointer_start = 0;
610 memos[memos_in_memory].file_pointer_end = 0;
611 memos[memos_in_memory].day = 0;
612 memos[memos_in_memory].month = 0;
613 memos[memos_in_memory].year = 0;
614 memos[memos_in_memory].type = 0;
615 memos[memos_in_memory].wday = 0;
619 rb->lcd_clear_display();
620 if(use_system_font)
621 rb->lcd_setfont(FONT_SYSFIXED);
622 if (saved)
623 rb->splash(HZ/2,"Event added");
624 else
625 rb->splash(HZ/2,"Event not added");
628 static int edit_menu_cb(int action, const struct menu_item_ex *this_item)
630 int i = (intptr_t)this_item;
631 if (action == ACTION_REQUEST_MENUITEM
632 && memos_in_shown_memory <= 0 && (i==0 || i==1))
633 return ACTION_EXIT_MENUITEM;
634 return action;
637 static bool edit_memo(int change, struct shown *shown)
639 bool exit = false;
640 int selected = 0;
642 MENUITEM_STRINGLIST(edit_menu, "Edit menu", edit_menu_cb,
643 "Remove", "Edit",
644 "New Weekly", "New Monthly",
645 "New Yearly", "New One off");
647 while (!exit)
649 switch (rb->do_menu(&edit_menu, &selected, NULL, false))
651 case 0: /* remove */
652 save_memo(pointer_array[change],false,shown);
653 return false;
655 case 1: /* edit */
656 if(rb->kbd_input(memos[pointer_array[change]].message,
657 sizeof memos[pointer_array[change]].message) != -1)
658 save_memo(pointer_array[change],true,shown);
659 return false;
661 case 2: /* weekly */
662 add_memo(shown,0);
663 return false;
665 case 3: /* monthly */
666 add_memo(shown,1);
667 return false;
669 case 4: /* yearly */
670 add_memo(shown,2);
671 return false;
673 case 5: /* one off */
674 add_memo(shown,3);
675 return false;
677 case GO_TO_PREVIOUS:
678 return false;
680 case MENU_ATTACHED_USB:
681 been_in_usb_mode = true;
682 break;
685 return false;
688 static char * get_event_text(int selected, void *data,
689 char *buffer, size_t buffer_len)
691 struct shown *shown = (struct shown *) data;
692 if (selected < 0 || memos_in_shown_memory <= selected)
694 return NULL;
696 if (memos[pointer_array[selected]].type == 2)
697 rb->snprintf(buffer, buffer_len, "%s (%d yrs)",
698 memos[pointer_array[selected]].message,
699 shown->year - memos[pointer_array[selected]].year);
700 else
701 rb->snprintf(buffer, buffer_len, "%s",
702 memos[pointer_array[selected]].message);
703 return buffer;
706 static bool view_events(int selected, struct shown *shown)
708 struct gui_synclist gui_memos;
709 bool exit=false;
710 int button;
712 rb->gui_synclist_init(&gui_memos, &get_event_text, shown, false, 1, NULL);
713 rb->gui_synclist_set_title(&gui_memos, "Events (play : menu)", NOICON);
714 rb->gui_synclist_set_nb_items(&gui_memos, memos_in_shown_memory);
715 rb->gui_synclist_select_item(&gui_memos, selected);
716 rb->gui_synclist_draw(&gui_memos);
718 while (!exit)
720 button = rb->get_action(CONTEXT_LIST,TIMEOUT_BLOCK);
721 rb->gui_synclist_do_button(&gui_memos,&button,LIST_WRAP_UNLESS_HELD);
723 switch (button)
725 case ACTION_STD_OK:
726 selected = rb->gui_synclist_get_sel_pos(&gui_memos);
727 return edit_memo(selected, shown);
728 break;
730 case ACTION_STD_CANCEL:
731 return false;
732 break;
734 default:
735 if(rb->default_event_handler(button) == SYS_USB_CONNECTED)
736 been_in_usb_mode = true;
737 break;
741 return false;
744 static void update_memos_shown(struct shown *shown)
746 int i;
747 memos_in_shown_memory = 0;
748 for (i = 0; i < memos_in_memory; i++)
749 if (
751 (memos[i].type >= 1)
753 (memos[i].day == shown->mday)
757 (memos[i].type < 1)
759 (memos[i].wday == shown->wday)
762 pointer_array[memos_in_shown_memory++] = i;
765 static bool any_events(struct shown *shown, bool force)
767 update_memos_shown(shown);
769 if (memos_in_shown_memory > 0)
770 return view_events(0, shown);
771 else if (force)
772 return edit_memo(0, shown);
773 else
774 return false;
776 return false;
779 static void next_month(struct shown *shown, int step)
781 shown->mon++;
782 if (shown->mon > 12)
784 shown->mon=1;
785 shown->year++;
786 leap_year = is_leap_year(shown->year);
788 if (step > 0)
789 shown->mday = shown->mday - days_in_month[leap_year][shown->mon-1];
790 else if (shown->mday > days_in_month[leap_year][shown->mon])
791 shown->mday = days_in_month[leap_year][shown->mon];
792 shown->firstday = shown->lastday;
793 load_memo(shown);
794 draw_calendar(shown);
797 static void prev_month(struct shown *shown, int step)
799 shown->mon--;
800 if (shown->mon < 1)
802 shown->mon = 12;
803 shown->year--;
804 leap_year = is_leap_year(shown->year);
806 if (step > 0)
807 shown->mday = shown->mday + days_in_month[leap_year][shown->mon];
808 else if (shown->mday > days_in_month[leap_year][shown->mon])
809 shown->mday = days_in_month[leap_year][shown->mon];
810 shown->firstday += 7 - (days_in_month[leap_year][shown->mon] % 7);
811 load_memo(shown);
812 draw_calendar(shown);
815 static void next_day(struct shown *shown, int step)
817 shown->mday += step;
818 if (shown->mday > days_in_month[leap_year][shown->mon])
819 next_month(shown, step);
820 else
821 draw_calendar(shown);
824 static void prev_day(struct shown *shown, int step)
826 shown->mday -= step;
827 if (shown->mday < 1)
828 prev_month(shown, step);
829 else
830 draw_calendar(shown);
833 enum plugin_status plugin_start(const void* parameter)
835 struct today today;
836 struct shown shown;
837 bool exit = false;
838 int button;
840 (void)(parameter);
842 calendar_init(&today, &shown);
843 load_memo(&shown);
844 any_events(&shown, false);
845 draw_calendar(&shown);
846 while (!exit)
848 button = rb->button_get(true);
849 switch (button)
851 case CALENDAR_QUIT:
852 return PLUGIN_OK;
854 case CALENDAR_NEXT_MONTH:
855 case CALENDAR_NEXT_MONTH | BUTTON_REPEAT:
856 next_month(&shown, 0);
857 break;
859 case CALENDAR_PREV_MONTH:
860 case CALENDAR_PREV_MONTH | BUTTON_REPEAT:
861 prev_month(&shown, 0);
862 break;
864 case CALENDAR_NEXT_WEEK:
865 case CALENDAR_NEXT_WEEK | BUTTON_REPEAT:
866 next_day(&shown, 7);
867 break;
869 case CALENDAR_PREV_WEEK:
870 case CALENDAR_PREV_WEEK | BUTTON_REPEAT:
871 prev_day(&shown, 7);
872 break;
874 case CALENDAR_PREV_DAY:
875 case CALENDAR_PREV_DAY | BUTTON_REPEAT:
876 prev_day(&shown, 1);
877 break;
879 case CALENDAR_NEXT_DAY:
880 case CALENDAR_NEXT_DAY | BUTTON_REPEAT:
881 next_day(&shown, 1);
882 break;
884 case CALENDAR_SELECT:
885 any_events(&shown, true);
886 draw_calendar(&shown);
887 break;
889 default:
890 if(rb->default_event_handler(button) == SYS_USB_CONNECTED)
891 been_in_usb_mode = true;
892 draw_calendar(&shown);
893 break;
896 return been_in_usb_mode?PLUGIN_USB_CONNECTED:PLUGIN_OK;
899 #endif