Accept FS#9224 by Teruaki Kawashima, adding support for all RTC equipped targets...
[kugel-rb.git] / apps / plugins / calendar.c
blobb54cd981997e6e3f26b375834ad0d7fdb157560e
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 == ONDIO_PAD
51 #define CALENDAR_QUIT BUTTON_OFF
52 #define CALENDAR_SELECT (BUTTON_MENU|BUTTON_REL)
53 #define CALENDAR_NEXT_WEEK BUTTON_DOWN
54 #define CALENDAR_PREV_WEEK BUTTON_UP
55 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
56 #define CALENDAR_PREV_DAY BUTTON_LEFT
57 #define CALENDAR_NEXT_MONTH (BUTTON_MENU|BUTTON_DOWN)
58 #define CALENDAR_PREV_MONTH (BUTTON_MENU|BUTTON_UP)
60 #elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
61 (CONFIG_KEYPAD == IRIVER_H300_PAD)
62 #define CALENDAR_QUIT BUTTON_OFF
63 #define CALENDAR_SELECT BUTTON_SELECT
64 #define CALENDAR_NEXT_WEEK BUTTON_DOWN
65 #define CALENDAR_PREV_WEEK BUTTON_UP
66 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
67 #define CALENDAR_PREV_DAY BUTTON_LEFT
68 #define CALENDAR_NEXT_MONTH BUTTON_MODE
69 #define CALENDAR_PREV_MONTH BUTTON_REC
71 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
72 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
73 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
74 #define CALENDAR_QUIT (BUTTON_SELECT|BUTTON_MENU)
75 #define CALENDAR_SELECT (BUTTON_SELECT|BUTTON_PLAY)
76 #define CALENDAR_NEXT_WEEK BUTTON_SCROLL_FWD
77 #define CALENDAR_PREV_WEEK BUTTON_SCROLL_BACK
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_MENU
83 #elif (CONFIG_KEYPAD == IAUDIO_X5M5_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_PLAY|BUTTON_DOWN)
91 #define CALENDAR_PREV_MONTH (BUTTON_PLAY|BUTTON_UP)
93 #elif CONFIG_KEYPAD == GIGABEAT_PAD
94 #define CALENDAR_QUIT BUTTON_POWER
95 #define CALENDAR_SELECT BUTTON_SELECT
96 #define CALENDAR_NEXT_WEEK BUTTON_DOWN
97 #define CALENDAR_PREV_WEEK BUTTON_UP
98 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
99 #define CALENDAR_PREV_DAY BUTTON_LEFT
100 #define CALENDAR_NEXT_MONTH BUTTON_VOL_DOWN
101 #define CALENDAR_PREV_MONTH BUTTON_VOL_UP
103 #elif CONFIG_KEYPAD == SANSA_E200_PAD
104 #define CALENDAR_QUIT BUTTON_POWER
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 #define CALENDAR_QUIT BUTTON_POWER
115 #define CALENDAR_SELECT BUTTON_SELECT
116 #define CALENDAR_NEXT_WEEK BUTTON_DOWN
117 #define CALENDAR_PREV_WEEK BUTTON_UP
118 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
119 #define CALENDAR_PREV_DAY BUTTON_LEFT
120 #define CALENDAR_NEXT_MONTH BUTTON_VOL_UP
121 #define CALENDAR_PREV_MONTH BUTTON_VOL_DOWN
123 #elif CONFIG_KEYPAD == IRIVER_H10_PAD
124 #define CALENDAR_QUIT BUTTON_POWER
125 #define CALENDAR_SELECT BUTTON_PLAY
126 #define CALENDAR_NEXT_WEEK BUTTON_SCROLL_DOWN
127 #define CALENDAR_PREV_WEEK BUTTON_SCROLL_UP
128 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
129 #define CALENDAR_PREV_DAY BUTTON_LEFT
130 #define CALENDAR_NEXT_MONTH BUTTON_FF
131 #define CALENDAR_PREV_MONTH BUTTON_REW
133 #elif CONFIG_KEYPAD == GIGABEAT_S_PAD
134 #define CALENDAR_QUIT BUTTON_BACK
135 #define CALENDAR_SELECT BUTTON_SELECT
136 #define CALENDAR_NEXT_WEEK BUTTON_DOWN
137 #define CALENDAR_PREV_WEEK BUTTON_UP
138 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
139 #define CALENDAR_PREV_DAY BUTTON_LEFT
140 #define CALENDAR_NEXT_MONTH BUTTON_NEXT
141 #define CALENDAR_PREV_MONTH BUTTON_PREV
143 #elif CONFIG_KEYPAD == MROBE100_PAD
144 #define CALENDAR_QUIT BUTTON_POWER
145 #define CALENDAR_SELECT BUTTON_SELECT
146 #define CALENDAR_NEXT_WEEK BUTTON_DOWN
147 #define CALENDAR_PREV_WEEK BUTTON_UP
148 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
149 #define CALENDAR_PREV_DAY BUTTON_LEFT
150 #define CALENDAR_NEXT_MONTH (BUTTON_MENU|BUTTON_DOWN)
151 #define CALENDAR_PREV_MONTH (BUTTON_MENU|BUTTON_UP)
153 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
154 #define CALENDAR_QUIT BUTTON_RC_REC
155 #define CALENDAR_SELECT BUTTON_RC_PLAY
156 #define CALENDAR_NEXT_WEEK BUTTON_RC_VOL_DOWN
157 #define CALENDAR_PREV_WEEK BUTTON_RC_VOL_UP
158 #define CALENDAR_NEXT_DAY BUTTON_RC_FF
159 #define CALENDAR_PREV_DAY BUTTON_RC_REW
160 #define CALENDAR_NEXT_MONTH BUTTON_RC_MODE
161 #define CALENDAR_PREV_MONTH BUTTON_RC_MENU
163 #elif (CONFIG_KEYPAD == COWOND2_PAD)
164 #define CALENDAR_QUIT BUTTON_POWER
165 #define CALENDAR_SELECT BUTTON_CENTER
166 #define CALENDAR_NEXT_WEEK BUTTON_DOWN
167 #define CALENDAR_PREV_WEEK BUTTON_UP
168 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
169 #define CALENDAR_PREV_DAY BUTTON_LEFT
170 #define CALENDAR_NEXT_MONTH BUTTON_BOTTOMRIGHT
171 #define CALENDAR_PREV_MONTH BUTTON_BOTTOMLEFT
173 #else
174 #error "No keypad setting."
175 #endif
177 #define X_OFFSET ((LCD_WIDTH%7)/2)
178 #if LCD_HEIGHT <= 80
179 #define Y_OFFSET 1
180 #else
181 #define Y_OFFSET 4
182 #endif
183 #define CELL_WIDTH (LCD_WIDTH / 7)
184 #define CELL_HEIGHT (LCD_HEIGHT / 7)
186 static const struct plugin_api* rb;
188 static bool leap_year;
189 /* days_in_month[][0] is for December */
190 static const int days_in_month[2][13] = {
191 {31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
192 {31, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
195 static const char *dayname_long[7] = {"Mon","Tue","Wed","Thu","Fri","Sat","Sun"};
196 static const char *dayname_short[7] = {"M","T","W","T","F","S","S"};
198 struct today {
199 int mday; /* day of the month */
200 int mon; /* month */
201 int year; /* year since 1900 */
202 int wday; /* day of the week */
205 struct shown {
206 int mday; /* day of the month */
207 int mon; /* month */
208 int year; /* year since 1900 */
209 int wday; /* day of the week */
210 int firstday; /* first (w)day of month */
211 int lastday; /* last (w)day of month */
214 static bool use_system_font = false;
216 static bool been_in_usb_mode = false;
218 /* leap year -- account for gregorian reformation in 1752 */
219 static int is_leap_year(int yr)
221 return ((yr) <= 1752 ? !((yr) % 4) : \
222 (!((yr) % 4) && ((yr) % 100)) || !((yr) % 400)) ? 1:0 ;
225 /* searches the weekday of the first day in month,
226 * relative to the given values */
227 static int calc_weekday( struct shown *shown )
229 return ( shown->wday + 36 - shown->mday ) % 7 ;
233 static void calendar_init(struct today *today, struct shown *shown)
235 int w,h;
236 #if CONFIG_RTC
237 struct tm *tm;
238 #else
239 (void)today;
240 #endif
241 rb->lcd_getstringsize("A",&w,&h);
242 if ( ((w * 14) > LCD_WIDTH) || ((h * 7) > LCD_HEIGHT) )
244 rb->lcd_setfont(FONT_SYSFIXED);
245 use_system_font = true;
247 rb->lcd_clear_display();
248 #if CONFIG_RTC
249 tm = rb->get_time();
250 today->mon = tm->tm_mon +1;
251 today->year = 2000+tm->tm_year%100;
252 today->wday = tm->tm_wday-1;
253 today->mday = tm->tm_mday;
254 shown->mday = today->mday;
255 shown->mon = today->mon;
256 shown->year = today->year;
257 shown->wday = today->wday;
258 #endif
259 shown->firstday = calc_weekday(shown);
260 leap_year = is_leap_year(shown->year);
263 static void draw_headers(void)
265 int i,w,h;
266 int x = X_OFFSET;
267 const char **dayname = (const char**)&dayname_long;
269 for (i = 0; i < 7; i++)
271 rb->lcd_getstringsize(dayname[i],&w,&h);
272 if (w > CELL_WIDTH)
274 dayname = (const char**)&dayname_short;
275 break;
279 rb->lcd_getstringsize("A",&w,&h);
280 for (i = 0; i < 7; i++)
282 rb->lcd_putsxy(x, 0 , dayname[i]);
283 x += CELL_WIDTH;
285 rb->lcd_hline(0, LCD_WIDTH-1 ,h);
288 static bool day_has_memo[32];
289 static bool wday_has_memo[7];
290 static void draw_calendar(struct shown *shown)
292 int w,h;
293 int x,y,pos,days_per_month,j;
294 char buffer[9];
295 const char *monthname[] = {
296 "Jan",
297 "Feb",
298 "Mar",
299 "Apr",
300 "May",
301 "Jun",
302 "Jul",
303 "Aug",
304 "Sep",
305 "Oct",
306 "Nov",
307 "Dec"
309 if(use_system_font)
310 rb->lcd_setfont(FONT_SYSFIXED);
311 rb->lcd_getstringsize("A",&w,&h);
312 rb->lcd_clear_display();
313 draw_headers();
314 if (shown->firstday > 6)
315 shown->firstday -= 7;
316 pos = shown->firstday;
317 days_per_month = days_in_month[leap_year][shown->mon];
318 x = X_OFFSET + (pos * CELL_WIDTH);
319 y = Y_OFFSET + h;
320 for (j = 1; j <= days_per_month; j++)
322 if ( (day_has_memo[j]) || (wday_has_memo[pos]) )
323 rb->snprintf(buffer,4,"%02d.", j);
324 else
325 rb->snprintf(buffer,4,"%02d", j);
326 if (shown->mday == j)
328 rb->lcd_set_drawmode(DRMODE_SOLID);
329 rb->lcd_fillrect(x, y - 1, CELL_WIDTH - 1, CELL_HEIGHT);
330 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
331 shown->wday = pos;
333 else
335 rb->lcd_set_drawmode(DRMODE_SOLID);
337 rb->lcd_putsxy(x, y, buffer);
338 x += CELL_WIDTH;
339 pos++;
340 if (pos >= 7)
342 pos = 0;
343 x = X_OFFSET;
344 y += CELL_HEIGHT;
347 rb->lcd_set_drawmode(DRMODE_SOLID);
348 rb->lcd_vline(LCD_WIDTH-w*8-10,LCD_HEIGHT-h-3,LCD_HEIGHT-1);
349 rb->lcd_hline(LCD_WIDTH-w*8-10,LCD_WIDTH-1,LCD_HEIGHT-h-3);
350 rb->snprintf(buffer,9,"%s %04d",monthname[shown->mon-1],shown->year);
351 rb->lcd_putsxy(LCD_WIDTH-w*8-8,LCD_HEIGHT-h-1,buffer);
352 shown->lastday = pos;
353 rb->lcd_update();
356 #define MAX_CHAR_MEMO_LEN 64
357 #define MAX_MEMOS_IN_A_MONTH 128
358 struct memo {
359 char message[MAX_CHAR_MEMO_LEN];
360 int day;
361 int month;
362 int file_pointer_start;
363 int file_pointer_end;
364 int year;
365 int wday;
366 int type;
367 } memos[MAX_MEMOS_IN_A_MONTH];
368 static int pointer_array[MAX_MEMOS_IN_A_MONTH];
369 static int memos_in_memory = 0;
370 static int memos_in_shown_memory = 0;
372 static void load_memo(struct shown *shown)
374 int i, k, fp;
375 bool exit = false;
376 char temp_memo1[2];
377 char temp_memo2[3];
378 char temp_memo4[5];
379 temp_memo1[1] = 0;
380 temp_memo2[2] = 0;
381 temp_memo4[4] = 0;
382 for (k = 0; k < memos_in_memory; k++)
384 memos[k].day = 0;
385 memos[k].month = 0;
386 memos[k].file_pointer_start = 0;
387 memos[k].file_pointer_end = 0;
388 memos[k].year = 0;
389 memos[k].type = 0;
390 memos[k].wday = 0;
391 for (i = 0; i < MAX_CHAR_MEMO_LEN; i++)
392 memos[k].message[i] = 0;
394 for (k = 1; k < 32; k++)
395 day_has_memo[k] = false;
396 for (k = 0; k < 7; k++)
397 wday_has_memo[k] = false;
398 memos_in_memory = 0;
399 fp = rb->open(ROCKBOX_DIR "/.memo",O_RDONLY);
400 if (fp > -1)
402 int count = rb->filesize(fp);
403 rb->lseek(fp, 0, SEEK_SET);
404 while (!exit)
406 memos[memos_in_memory].file_pointer_start = rb->lseek(fp, 0,
407 SEEK_CUR);
408 if (rb->read(fp, temp_memo2, 2) == 2)
409 memos[memos_in_memory].day = rb->atoi(&temp_memo2[0]);
410 else
411 memos[memos_in_memory].day = 0;
412 if (rb->read(fp, temp_memo2, 2) == 2)
413 memos[memos_in_memory].month = rb->atoi(&temp_memo2[0]);
414 else
415 memos[memos_in_memory].month = 0;
416 if (rb->read(fp, temp_memo4, 4) == 4)
417 memos[memos_in_memory].year = rb->atoi(&temp_memo4[0]);
418 else
419 memos[memos_in_memory].year = 0;
420 /* as the year returned is sometimes yearmonth, ie if yr should =
421 2003, and month = 06, then it returns 200306 */
422 if (memos[memos_in_memory].year > (shown->year * 10))
423 memos[memos_in_memory].year = (memos[memos_in_memory].year -
424 memos[memos_in_memory].month) /
425 100;
426 if (rb->read(fp, temp_memo1, 1) == 1)
427 memos[memos_in_memory].wday = rb->atoi(&temp_memo1[0]);
428 else
429 memos[memos_in_memory].wday = 0;
430 if (rb->read(fp, temp_memo1, 1) == 1)
431 memos[memos_in_memory].type = rb->atoi(&temp_memo1[0]);
432 else
433 memos[memos_in_memory].type = 0;
434 for (k = 0; k <= count; k++)
436 if (rb->read(fp, temp_memo1, 1) == 1)
438 if (
439 (memos[memos_in_memory].type < 2)
442 (memos[memos_in_memory].type == 2)
444 (memos[memos_in_memory].month == shown->mon)
448 (memos[memos_in_memory].type > 2)
450 (memos[memos_in_memory].month == shown->mon)
452 (memos[memos_in_memory].year == shown->year)
456 if (temp_memo1[0] == '\n')
458 if (memos[memos_in_memory].type > 0)
459 day_has_memo[memos[memos_in_memory].day] =
460 true;
461 else
462 wday_has_memo[memos[memos_in_memory].wday] =
463 true;
464 memos[memos_in_memory++].file_pointer_end =
465 rb->lseek(fp, 0, SEEK_CUR);
467 else if ( (temp_memo1[0] != '\r') &&
468 (temp_memo1[0] != '\t') &&
469 k < MAX_CHAR_MEMO_LEN-1 )
470 memos[memos_in_memory].message[k] = temp_memo1[0];
472 if (temp_memo1[0] == '\n')
473 break;
475 else
477 memos[memos_in_memory].day = 0;
478 memos[memos_in_memory].month = 0;
479 memos[memos_in_memory].file_pointer_start = 0;
480 memos[memos_in_memory].file_pointer_end = 0;
481 memos[memos_in_memory].year = 0;
482 memos[memos_in_memory].type = 0;
483 memos[memos_in_memory].wday = 0;
484 memos[memos_in_memory].message[0] = 0;
485 exit = true;
486 break;
491 rb->close(fp);
494 static bool save_memo(int changed, bool new_mod, struct shown *shown)
496 int fp,fq;
497 fp = rb->open(ROCKBOX_DIR "/.memo",O_RDONLY | O_CREAT);
498 fq = rb->creat(ROCKBOX_DIR "/~temp");
499 if ( (fq != -1) && (fp != -1) )
501 int i;
502 char temp[MAX_CHAR_MEMO_LEN];
503 rb->lseek(fp, 0, SEEK_SET);
504 for (i = 0; i < memos[changed].file_pointer_start; i++)
506 rb->read(fp, temp, 1);
507 rb->write(fq,temp,1);
509 if (new_mod)
511 rb->fdprintf(fq, "%02d%02d%04d%01d%01d%s\n",
512 memos[changed].day,
513 memos[changed].month,
514 memos[changed].year,
515 memos[changed].wday,
516 memos[changed].type,
517 memos[changed].message);
519 rb->lseek(fp, memos[changed].file_pointer_end, SEEK_SET);
520 while(rb->read(fp, temp, 1) == 1)
522 rb->write(fq,temp,1);
524 rb->close(fp);
525 rb->close(fq);
526 rb->remove(ROCKBOX_DIR "/.memo");
527 rb->rename(ROCKBOX_DIR "/~temp", ROCKBOX_DIR "/.memo");
528 load_memo(shown);
529 return true;
531 else if (fp != -1)
532 rb->close(fp);
533 else if (fq != -1)
534 rb->close(fq);
535 return false;
538 static void add_memo(struct shown *shown, int type)
540 bool saved = false;
541 if (rb->kbd_input(memos[memos_in_memory].message,
542 sizeof memos[memos_in_memory].message) != -1)
544 if (rb->strlen(memos[memos_in_memory].message))
546 memos[memos_in_memory].file_pointer_start = 0;
547 memos[memos_in_memory].file_pointer_end = 0;
548 memos[memos_in_memory].day = shown->mday;
549 memos[memos_in_memory].month = shown->mon;
550 memos[memos_in_memory].wday = shown->wday;
551 memos[memos_in_memory].year = shown->year;
552 memos[memos_in_memory].type = type;
553 if (save_memo(memos_in_memory,true,shown))
555 saved = true;
557 else
559 memos[memos_in_memory].file_pointer_start = 0;
560 memos[memos_in_memory].file_pointer_end = 0;
561 memos[memos_in_memory].day = 0;
562 memos[memos_in_memory].month = 0;
563 memos[memos_in_memory].year = 0;
564 memos[memos_in_memory].type = 0;
565 memos[memos_in_memory].wday = 0;
569 rb->lcd_clear_display();
570 if(use_system_font)
571 rb->lcd_setfont(FONT_SYSFIXED);
572 if (saved)
573 rb->splash(HZ/2,"Event added");
574 else
575 rb->splash(HZ/2,"Event not added");
578 static int edit_menu_cb(int action, const struct menu_item_ex *this_item)
580 (void) this_item;
581 if (action == ACTION_REQUEST_MENUITEM && memos_in_shown_memory <= 0)
582 return ACTION_EXIT_MENUITEM;
583 return action;
586 static bool edit_memo(int change, struct shown *shown)
588 bool exit = false;
589 int selected = 0;
591 MENUITEM_RETURNVALUE(edit_menu_remove, "Remove", 0,
592 edit_menu_cb, Icon_NOICON);
593 MENUITEM_RETURNVALUE(edit_menu_edit, "Edit", 1,
594 edit_menu_cb, Icon_NOICON);
595 MENUITEM_RETURNVALUE(edit_menu_weekly, "New Weekly", 2,
596 NULL, Icon_NOICON);
597 MENUITEM_RETURNVALUE(edit_menu_monthly, "New Monthly", 3,
598 NULL, Icon_NOICON);
599 MENUITEM_RETURNVALUE(edit_menu_yearly, "New Yearly", 4,
600 NULL, Icon_NOICON);
601 MENUITEM_RETURNVALUE(edit_menu_oneoff, "New One off", 5,
602 NULL, Icon_NOICON);
604 MAKE_MENU(edit_menu, "Edit menu",
605 NULL, Icon_NOICON,
606 &edit_menu_remove, &edit_menu_edit,
607 &edit_menu_weekly, &edit_menu_monthly,
608 &edit_menu_yearly, &edit_menu_oneoff);
610 while (!exit)
612 switch (rb->do_menu(&edit_menu, &selected, NULL, false))
614 case 0: /* remove */
615 save_memo(pointer_array[change],false,shown);
616 return false;
618 case 1: /* edit */
619 if(rb->kbd_input(memos[pointer_array[change]].message,
620 sizeof memos[pointer_array[change]].message) != -1)
621 save_memo(pointer_array[change],true,shown);
622 return false;
624 case 2: /* weekly */
625 add_memo(shown,0);
626 return false;
628 case 3: /* monthly */
629 add_memo(shown,1);
630 return false;
632 case 4: /* yearly */
633 add_memo(shown,2);
634 return false;
636 case 5: /* one off */
637 add_memo(shown,3);
638 return false;
640 case GO_TO_PREVIOUS:
641 return false;
643 case MENU_ATTACHED_USB:
644 been_in_usb_mode = true;
645 break;
648 return false;
651 static char * get_event_text(int selected, void *data,
652 char *buffer, size_t buffer_len)
654 struct shown *shown = (struct shown *) data;
655 if (selected < 0 || memos_in_shown_memory <= selected)
657 return NULL;
659 if (memos[pointer_array[selected]].type == 2)
660 rb->snprintf(buffer, buffer_len, "%s (%d yrs)",
661 memos[pointer_array[selected]].message,
662 shown->year - memos[pointer_array[selected]].year);
663 else
664 rb->snprintf(buffer, buffer_len, "%s",
665 memos[pointer_array[selected]].message);
666 return buffer;
669 static bool view_events(int selected, struct shown *shown)
671 struct gui_synclist gui_memos;
672 bool exit=false;
673 int button;
675 rb->gui_synclist_init(&gui_memos, &get_event_text, shown, false, 1, NULL);
676 rb->gui_synclist_set_title(&gui_memos, "Events (play : menu)", NOICON);
677 rb->gui_synclist_set_nb_items(&gui_memos, memos_in_shown_memory);
678 rb->gui_synclist_select_item(&gui_memos, selected);
679 rb->gui_synclist_draw(&gui_memos);
681 while (!exit)
683 rb->gui_syncstatusbar_draw(rb->statusbars, true);
684 button = rb->get_action(CONTEXT_LIST,TIMEOUT_BLOCK);
685 rb->gui_synclist_do_button(&gui_memos,&button,LIST_WRAP_UNLESS_HELD);
687 switch (button)
689 case ACTION_STD_OK:
690 selected = rb->gui_synclist_get_sel_pos(&gui_memos);
691 return edit_memo(selected, shown);
692 break;
694 case ACTION_STD_CANCEL:
695 return false;
696 break;
698 default:
699 if(rb->default_event_handler(button) == SYS_USB_CONNECTED)
700 been_in_usb_mode = true;
701 break;
705 return false;
708 static void update_memos_shown(struct shown *shown)
710 int i;
711 memos_in_shown_memory = 0;
712 for (i = 0; i < memos_in_memory; i++)
713 if (
715 (memos[i].type >= 1)
717 (memos[i].day == shown->mday)
721 (memos[i].type < 1)
723 (memos[i].wday == shown->wday)
726 pointer_array[memos_in_shown_memory++] = i;
729 static bool any_events(struct shown *shown, bool force)
731 update_memos_shown(shown);
733 if (memos_in_shown_memory > 0)
734 return view_events(0, shown);
735 else if (force)
736 return edit_memo(0, shown);
737 else
738 return false;
740 return false;
743 static void next_month(struct shown *shown, int step)
745 shown->mon++;
746 if (shown->mon > 12)
748 shown->mon=1;
749 shown->year++;
750 leap_year = is_leap_year(shown->year);
752 if (step > 0)
753 shown->mday = shown->mday - days_in_month[leap_year][shown->mon-1];
754 else if (shown->mday > days_in_month[leap_year][shown->mon])
755 shown->mday = days_in_month[leap_year][shown->mon];
756 shown->firstday = shown->lastday;
757 load_memo(shown);
758 draw_calendar(shown);
761 static void prev_month(struct shown *shown, int step)
763 shown->mon--;
764 if (shown->mon < 1)
766 shown->mon = 12;
767 shown->year--;
768 leap_year = is_leap_year(shown->year);
770 if (step > 0)
771 shown->mday = shown->mday + days_in_month[leap_year][shown->mon];
772 else if (shown->mday > days_in_month[leap_year][shown->mon])
773 shown->mday = days_in_month[leap_year][shown->mon];
774 shown->firstday += 7 - (days_in_month[leap_year][shown->mon] % 7);
775 load_memo(shown);
776 draw_calendar(shown);
779 static void next_day(struct shown *shown, int step)
781 shown->mday += step;
782 if (shown->mday > days_in_month[leap_year][shown->mon])
783 next_month(shown, step);
784 else
785 draw_calendar(shown);
788 static void prev_day(struct shown *shown, int step)
790 shown->mday -= step;
791 if (shown->mday < 1)
792 prev_month(shown, step);
793 else
794 draw_calendar(shown);
797 enum plugin_status plugin_start(const struct plugin_api* api, const void* parameter)
799 struct today today;
800 struct shown shown;
801 bool exit = false;
802 int button;
804 (void)(parameter);
806 rb = api;
808 calendar_init(&today, &shown);
809 load_memo(&shown);
810 any_events(&shown, false);
811 draw_calendar(&shown);
812 while (!exit)
814 button = rb->button_get(true);
815 switch (button)
817 case CALENDAR_QUIT:
818 return PLUGIN_OK;
820 case CALENDAR_NEXT_MONTH:
821 case CALENDAR_NEXT_MONTH | BUTTON_REPEAT:
822 next_month(&shown, 0);
823 break;
825 case CALENDAR_PREV_MONTH:
826 case CALENDAR_PREV_MONTH | BUTTON_REPEAT:
827 prev_month(&shown, 0);
828 break;
830 case CALENDAR_NEXT_WEEK:
831 case CALENDAR_NEXT_WEEK | BUTTON_REPEAT:
832 next_day(&shown, 7);
833 break;
835 case CALENDAR_PREV_WEEK:
836 case CALENDAR_PREV_WEEK | BUTTON_REPEAT:
837 prev_day(&shown, 7);
838 break;
840 case CALENDAR_PREV_DAY:
841 case CALENDAR_PREV_DAY | BUTTON_REPEAT:
842 prev_day(&shown, 1);
843 break;
845 case CALENDAR_NEXT_DAY:
846 case CALENDAR_NEXT_DAY | BUTTON_REPEAT:
847 next_day(&shown, 1);
848 break;
850 case CALENDAR_SELECT:
851 any_events(&shown, true);
852 draw_calendar(&shown);
853 break;
855 default:
856 if(rb->default_event_handler(button) == SYS_USB_CONNECTED)
857 been_in_usb_mode = true;
858 draw_calendar(&shown);
859 break;
862 return been_in_usb_mode?PLUGIN_USB_CONNECTED:PLUGIN_OK;
865 #endif