Split note handling in separate widget
authorEnrico Zini <enrico@enricozini.org>
Fri, 6 Nov 2009 23:24:39 +0000 (23:24 +0000)
committerEnrico Zini <enrico@enricozini.org>
Fri, 6 Nov 2009 23:24:39 +0000 (23:24 +0000)
src/widgets/calendar.vala

index 061abfbfa686caa8baaaac61432b5d1aad95c781..d27a91c245d12878ca7b95f90779f21f12e6a8c6 100644 (file)
 namespace zavai {
 namespace widgets {
 
-public class Calendar : Gtk.VBox
+public class FileNotes : Gtk.ScrolledWindow
 {
-       protected Gtk.Calendar calendar;
-       protected Gtk.ScrolledWindow scroll;
        protected Gtk.TextBuffer text_buffer;
        protected Gtk.TextView text;
+       protected bool text_dirty;
+       protected string fname;
+
+       public FileNotes()
+       {
+               fname = null;
+               text_dirty = false;
+               text_buffer = new Gtk.TextBuffer(null);
+               text_buffer.changed += on_textbuffer_changed;
+               text = new Gtk.TextView.with_buffer(text_buffer);
+               text.wrap_mode = Gtk.WrapMode.WORD;
+               text.cursor_visible = true;
+               text.editable = true;
+
+               set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC);
+               add(text);
+       }
+       
+       private void on_textbuffer_changed()
+       {
+               text_dirty = true;
+       }
+
+       public void load(string new_fname)
+       {
+               save();
+               fname = new_fname;
+
+               if (!FileUtils.test (fname, FileTest.IS_REGULAR))
+                       text_buffer.text = "";
+               else
+               {
+                       try {
+                               string text;
+                               FileUtils.get_contents(fname, out text);
+                               text_buffer.text = text;
+                       } catch (FileError e) {
+                               zavai.log.error(e.message);
+                               text_buffer.text = "";
+                       }
+               }
+
+               // Scroll to beginning
+               Gtk.TextIter iter;
+               text_buffer.get_iter_at_offset(out iter, 0);
+               Gtk.TextMark mark = text_buffer.create_mark(null, iter, true);
+               text.scroll_mark_onscreen(mark);
+
+               text_dirty = false;
+       }
+
+       public void save()
+       {
+               if (fname == null) return;
+               if (text_dirty == false) return;
+
+               string mpath = Path.get_dirname(fname);
+               if (DirUtils.create_with_parents(mpath, 0777) < 0)
+               {
+                       zavai.log.error("Cannot create directory " + mpath);
+                       return;
+               }
+               try {
+                       if (text_buffer.text == "")
+                       {
+                               if (FileUtils.test(fname, FileTest.EXISTS))
+                                       FileUtils.unlink(fname);
+                       } else {
+                               FileUtils.set_contents(fname, text_buffer.text);
+                       }
+               } catch (FileError e) {
+                       zavai.log.error(e.message);
+               }
+               text_dirty = false;
+       }
+}
+
+public class Calendar : Gtk.VBox
+{
+       protected Gtk.Calendar calendar;
+       protected Gtk.Notebook notebook;
+       protected FileNotes day_notes;
        protected int cur_year;
        protected int cur_month;
        protected int cur_day;
-       protected bool text_dirty;
        protected Regex re_dayfile;
 
        public Calendar()
@@ -40,27 +119,17 @@ public class Calendar : Gtk.VBox
                calendar.month_changed += on_month_changed;
                pack_start(calendar, false, false, 0);
 
-               text_buffer = new Gtk.TextBuffer(null);
-               text_buffer.changed += on_textbuffer_changed;
-               text = new Gtk.TextView.with_buffer(text_buffer);
-               text.wrap_mode = Gtk.WrapMode.WORD;
-               text.cursor_visible = true;
-               text.editable = true;
-               scroll = new Gtk.ScrolledWindow (null, null);
-               scroll.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC);
-               scroll.add(text);
-               pack_start(scroll, true, true, 0);
+               notebook = new Gtk.Notebook();
+
+               day_notes = new FileNotes();
+               notebook.append_page(day_notes, new Gtk.Label("Day notes"));
+
+               pack_start(notebook, true, true, 0);
 
                re_dayfile = new Regex("[0-9][0-9]\\.txt");
 
-               text_dirty = false;
                on_month_changed();
        }
-       
-       private void on_textbuffer_changed()
-       {
-               text_dirty = true;
-       }
 
        private string month_path(int year, int month)
        {
@@ -98,60 +167,14 @@ public class Calendar : Gtk.VBox
        private void on_day_selected()
        {
                flush();
-               load(calendar.year, calendar.month + 1, calendar.day);
-               // Scroll to beginning
-               Gtk.TextIter iter;
-               text_buffer.get_iter_at_offset(out iter, 0);
-               Gtk.TextMark mark = text_buffer.create_mark(null, iter, true);
-               text.scroll_mark_onscreen(mark);
-       }
-
-       private void load(int year, int month, int day)
-       {
-               string path = day_path(year, month, day);
-               if (!FileUtils.test (path, FileTest.IS_REGULAR))
-                       text_buffer.text = "";
-               else
-               {
-                       try {
-                               string text;
-                               FileUtils.get_contents(path, out text);
-                               text_buffer.text = text;
-                       } catch (FileError e) {
-                               zavai.log.error(e.message);
-                               text_buffer.text = "";
-                       }
-               }
-               text_dirty = false;
-               cur_year = year; cur_month = month; cur_day = day;
-       }
-
-       private void save(int year, int month, int day)
-       {
-               string mpath = month_path(year, month);
-               if (DirUtils.create_with_parents(mpath, 0777) < 0)
-               {
-                       zavai.log.error("Cannot create directory " + mpath);
-                       return;
-               }
-               string path = day_path(year, month, day);
-               try {
-                       FileUtils.set_contents(path, text_buffer.text);
-               } catch (FileError e) {
-                       zavai.log.error(e.message);
-               }
-               text_dirty = false;
-
-               if (calendar.year == year && calendar.month + 1 == month)
-               {
-                       calendar.mark_day(day);
-               }
+               cur_year = calendar.year; cur_month = calendar.month + 1; cur_day = calendar.day;
+               string path = day_path(cur_year, cur_month, cur_day);
+               day_notes.load(path);
        }
 
        public void flush()
        {
-               if (text_dirty)
-                       save(cur_year, cur_month, cur_day);
+               day_notes.save();
        }
 
        public void show_today()