Code cleanup
[gregoa/zavai.git] / src / zavai-calendar.vala
1 /*
2  * zavai-calendar - simple calendar tool
3  *
4  * Copyright (C) 2009  Enrico Zini <enrico@enricozini.org>
5  *
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.
10  *
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.
15  *
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
19  */
20
21 using GLib;
22
23 //string VERSION = "0.1";
24
25 static Posix.pid_t is_running()
26 {
27         string pidfile = zavai.config.homedir + "/calendar.pid";
28         string contents;
29         try {
30                 if (!FileUtils.get_contents(pidfile, out contents))
31                         return 0;
32         } catch (FileError e) {
33                 return 0;
34         }
35         int val = contents.to_int();
36         string procdir = "/proc/%d".printf(val);
37         if (FileUtils.test(procdir, FileTest.IS_DIR))
38                 return (Posix.pid_t)val;
39         else
40                 return 0;
41 }
42
43 static void make_pidfile()
44 {
45         string pidfile = zavai.config.homedir + "/calendar.pid";
46         FileUtils.set_contents(pidfile, "%d".printf(Posix.getpid()));
47 }
48
49 static void on_kill(int sig)
50 {
51         Gtk.main_quit();
52 }
53
54 static int main (string[] args) {
55         bool opt_popup = false;
56         GLib.OptionEntry[] entries = new GLib.OptionEntry[] {
57                 OptionEntry() {
58                         long_name = "popup",
59                         short_name = 'p',
60                         flags = 0,
61                         arg = OptionArg.NONE,
62                         arg_data = &opt_popup,
63                         description = "run as a popup at the specified location on screen",
64                         arg_description = null },
65                 OptionEntry()
66         };
67         Gtk.init_with_args(ref args, "", entries, null);
68
69         // parser = Parser(usage="usage: %prog [options]",
70         //                 version="%prog "+ VERSION,
71         //                 description="Simple interactive interface for the OpenMoko")
72         // parser.add_option("-v", "--verbose", action="store_true", help="verbose mode")
73         // 
74         // (opts, args) = parser.parse_args()
75         // 
76         // if not opts.verbose:
77         //     zavai.set_quiet()
78         // 
79         // # Read configuration
80         // zavai.info("Loading configuration")
81         // conf = zavai.Config()
82
83         // Set up zavai
84
85         // Core infrastructure
86         zavai.config = new zavai.Config();
87
88         // User interface
89         var calendar = new zavai.widgets.Calendar();
90
91         Gtk.Window win = new Gtk.Window(Gtk.WindowType.TOPLEVEL);
92         if (opt_popup)
93         {
94                 Posix.pid_t pid = is_running();
95                 if (pid != 0)
96                 {
97                         // Kill a running calendar
98                         Posix.kill(pid, Posix.SIGINT);
99                         FileUtils.unlink(zavai.config.homedir + "/calendar.pid");
100                         return 0;
101                 }
102
103                 make_pidfile();
104
105                 win.set_decorated(false);
106                 //win.set_resizable(false);
107                 win.set_border_width(5);
108                 win.set_skip_taskbar_hint(true);
109                 win.set_skip_pager_hint(true);
110                 //    gtk_window_set_type_hint(GTK_WINDOW(win), GDK_WINDOW_TYPE_HINT_DOCK);
111                 win.set_position(Gtk.WindowPosition.MOUSE);
112                 win.stick();
113         }
114
115         win.title = "Zavai calendar";
116         win.destroy += Gtk.main_quit;
117         win.add(calendar);
118         win.set_size_request(300, 500);
119         win.show_all();
120         win.show();
121
122         // Shutdown the main loop on SIGINT
123         Posix.signal(Posix.SIGINT, on_kill);
124         Posix.signal(Posix.SIGTERM, on_kill);
125
126         Gtk.main();
127
128         calendar.flush();
129
130         return 0;
131 }