Warning Fixes
[tilda-gobject.git] / tilda.c
1 #include <glib.h>
2 #include <stdlib.h>
3
4 #include "tilda.h"
5 #include "tilda-window.h"
6 #include "tilda-terminal.h"
7 #include "tomboykeybinder.h"
8
9 DBusGConnection *dbus_connection;
10 GPtrArray *windows;
11
12 static void
13 tilda_initialize_dbus ()
14 {
15         debug_enter  ();
16
17         static const gchar service_name[] = "net.sourceforge.Tilda";
18         GError *error = NULL;
19         DBusGProxy *driver_proxy;
20         guint request_ret;
21
22         // Initialize the DBus connection
23         dbus_connection = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
24         if (dbus_connection == NULL)
25         {
26                 g_warning ("Unable to connect to dbus: %s", error->message);
27                 g_error_free (error);
28                 return;
29         }
30
31         // Register the service name
32         driver_proxy = dbus_g_proxy_new_for_name (dbus_connection,
33                                                                                           DBUS_SERVICE_DBUS,
34                                                                                           DBUS_PATH_DBUS,
35                                                                                           DBUS_INTERFACE_DBUS);
36
37         if (!org_freedesktop_DBus_request_name (driver_proxy, service_name, 0, &request_ret, &error))
38         {
39                 // FIXME: for whatever reason, this is wrong. The error message doesn't appear
40                 // FIXME: when we were unable to register the service. Perhaps there's a more
41                 // FIXME: GLib-y way of doing this?
42                 g_warning ("Unable to register service: %s", error->message);
43                 g_error_free (error);
44         }
45
46         g_object_unref (driver_proxy);
47 }
48
49 static gint
50 tilda_find_next_free_window_number ()
51 {
52         debug_enter  ();
53
54         gint i, j;
55         gboolean found;
56
57         for (i=0; i<INT_MAX; ++i)
58         {
59                 found = FALSE;
60
61                 for (j=0; j<windows->len; ++j)
62                 {
63                         TildaWindow *tw = g_ptr_array_index (windows, j);
64
65                         if (tw->number == i)
66                         {
67                                 found = TRUE;
68                                 break;
69                         }
70                 }
71
72                 if (!found)
73                         return i;
74         }
75
76         return 0;
77 }
78
79 static TildaWindow *
80 tilda_add_window ()
81 {
82         debug_enter ();
83
84         TildaWindow *ret;
85         gint number;
86
87         number = tilda_find_next_free_window_number ();
88         ret = g_object_new (TILDA_TYPE_WINDOW, "number", number, NULL);
89
90         g_ptr_array_add (windows, ret);
91
92         debug_printf ("Adding window: 0x%x (number %d of %d)\n", ret, ret->number, windows->len-1);
93         return ret;
94 }
95
96 static void
97 tilda_del_window (gint number)
98 {
99         debug_enter ();
100
101         gint i;
102         TildaWindow *win;
103
104         for (i=0; i<windows->len; ++i)
105         {
106                 win = g_ptr_array_index (windows, i);
107
108                 if (win->number == number)
109                 {
110                         debug_printf ("Deleting window 0x%x (number %d of %d)\n", win, win->number, windows->len-1);
111                         g_ptr_array_remove_index (windows, i);
112                         g_object_unref (G_OBJECT(win));
113                         break;
114                 }
115         }
116 }
117
118 static void
119 tilda_parse_command_line (gint argc, gchar *argv[])
120 {
121         debug_enter ();
122
123         gboolean version = FALSE;
124
125         /* All of the various command-line options */
126         const GOptionEntry cl_opts[] = {
127                 { "version",                    'V', 0, G_OPTION_ARG_NONE,              &version,                       N_("Show version information"), NULL },
128                 { NULL },
129         };
130
131         /* Set up the command-line parser */
132         GError *error = NULL;
133         GOptionContext *context = g_option_context_new (NULL);
134         g_option_context_add_main_entries (context, cl_opts, NULL);
135         g_option_context_add_group (context, gtk_get_option_group (TRUE));
136         g_option_context_parse (context, &argc, &argv, &error);
137         g_option_context_free (context);
138
139         /* Check for unknown options, and give a nice message if there are some */
140         if (error)
141         {
142                 g_printerr (_("Error parsing command-line options: %s\n"), error->message);
143                 g_printerr (_("The command \"tilda --help\" will show all possible options\n"));
144                 g_error_free (error);
145                 exit (EXIT_FAILURE);
146         }
147
148         /* If we need to show the version, show it then exit normally */
149         if (version)
150         {
151                 g_print ("%s\n\n", TILDA_VERSION);
152
153                 g_print ("Copyright (c) 2005-2008 Tristan Sloughter (sloutri@iit.edu)\n");
154                 g_print ("Copyright (c) 2005-2008 Ira W. Snyder (tilda@irasnyder.com)\n\n");
155
156                 g_print ("This program comes with ABSOLUTELY NO WARRANTY.\n");
157                 g_print ("This is free software, and you are welcome to redistribute it\n");
158                 g_print ("under certain conditions. See the file COPYING for details.\n");
159
160                 exit (EXIT_SUCCESS);
161         }
162 }
163
164 int main (int argc, char *argv[])
165 {
166         debug_enter ();
167
168         TildaWindow *tw;
169
170 #if ENABLE_NLS
171         /* Gettext Initialization */
172         setlocale (LC_ALL, "");
173         bindtextdomain (PACKAGE, LOCALEDIR);
174         textdomain (PACKAGE);
175 #endif
176
177         /* Parse the command-line options */
178         tilda_parse_command_line (argc, argv);
179
180         /* Initialize GTK+ (and the GObject system) */
181         gtk_init (&argc, &argv);
182
183         /* Initialize the keybinder */
184         tomboy_keybinder_init ();
185
186         /* Start our connection to DBus */
187         tilda_initialize_dbus ();
188
189         /* Initialize the array of windows */
190         windows = g_ptr_array_new ();
191
192         /* Create a TildaWindow, run it, and exit when it does, basically.
193          *
194          * This is nothing like what the real main() will be, but it's
195          * a good start for testing and integration of more of TildaWindow
196          * and TildaTerminal. */
197         tw = tilda_add_window ();
198
199         debug_printf ("Starting gtk_main()!\n");
200         gtk_main ();
201         debug_printf ("Out of gtk_main(), going down\n");
202
203         return 0;
204 }
205
206 /* vim: set ts=4 sts=4 sw=4 noet tw=112: */