2 * app_calendar - zavai calendar applet
4 * Copyright (C) 2009 Enrico Zini <enrico@enricozini.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 public class Calendar : Applet
27 protected Gtk.Calendar calendar;
28 protected Gtk.ScrolledWindow scroll;
29 protected Gtk.TextBuffer text_buffer;
30 protected Gtk.TextView text;
31 protected int cur_year;
32 protected int cur_month;
33 protected int cur_day;
34 protected bool text_dirty;
35 protected Regex re_dayfile;
40 calendar = new Gtk.Calendar();
41 calendar.day_selected += on_day_selected;
42 calendar.month_changed += on_month_changed;
43 pack_start(calendar, false, false, 0);
45 text_buffer = new Gtk.TextBuffer(null);
46 text_buffer.changed += on_textbuffer_changed;
47 text = new Gtk.TextView.with_buffer(text_buffer);
48 text.wrap_mode = Gtk.WrapMode.WORD;
49 text.cursor_visible = true;
51 scroll = new Gtk.ScrolledWindow (null, null);
52 scroll.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC);
54 pack_start(scroll, true, true, 0);
56 re_dayfile = new Regex("[0-9][0-9]\\.txt");
61 private void on_textbuffer_changed()
66 private string month_path(int year, int month)
68 return "%s/cal/%04d/%02d".printf(zavai.config.homedir, year, month);
71 private string day_path(int year, int month, int day)
73 return "%s/%02d.txt".printf(month_path(year, month), day);
79 save(cur_year, cur_month, cur_day);
82 private void on_month_changed()
85 calendar.clear_marks();
86 string mpath = month_path(calendar.year, calendar.month);
89 dir = Dir.open(mpath);
90 } catch (FileError e) {
91 zavai.log.error(e.message);
96 var d = dir.read_name();
98 if (re_dayfile.match(d))
100 calendar.mark_day((int)d.to_ulong(null, 10));
105 private void on_day_selected()
108 load(calendar.year, calendar.month, calendar.day);
109 // Scroll to beginning
111 text_buffer.get_iter_at_offset(out iter, 0);
112 Gtk.TextMark mark = text_buffer.create_mark(null, iter, true);
113 text.scroll_mark_onscreen(mark);
116 private void load(int year, int month, int day)
118 string path = day_path(year, month, day);
119 if (!FileUtils.test (path, FileTest.IS_REGULAR))
120 text_buffer.text = "";
125 FileUtils.get_contents(path, out text);
126 text_buffer.text = text;
127 } catch (FileError e) {
128 zavai.log.error(e.message);
129 text_buffer.text = "";
133 cur_year = year; cur_month = month; cur_day = day;
136 private void save(int year, int month, int day)
138 string mpath = month_path(year, month);
139 if (DirUtils.create_with_parents(mpath, 0777) < 0)
141 zavai.log.error("Cannot create directory " + mpath);
144 string path = day_path(year, month, day);
146 FileUtils.set_contents(path, text_buffer.text);
147 } catch (FileError e) {
148 zavai.log.error(e.message);
153 public override void start()
155 // Go to current date
156 var now = Time.local(time_t());
157 calendar.year = now.year + 1900;
158 calendar.month = now.month;
159 calendar.day = now.day;
161 public override void stop()
171 calendar = new Calendar();
172 zavai.registry.register_applet("ui.calendar", calendar);
173 zavai.registry.getmenu("menu.misc").add_applet("ui.calendar");