Clean up Tomboy's code
[tilda-gobject.git] / tilda.c
1 #include <stdlib.h>
2 #include <signal.h>
3
4 #include "tilda.h"
5 #include "tilda-controller.h"
6 #include "tomboykeybinder.h"
7
8 /* Project-global variables */
9 DBusGConnection *dbus_connection;
10
11 static void
12 tilda_dbus_init ()
13 {
14         debug_enter  ();
15
16         static const gchar service_name[] = "net.sourceforge.Tilda";
17         GError *error = NULL;
18         DBusGProxy *driver_proxy;
19         guint request_ret;
20         gboolean 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\n"), 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         ret = org_freedesktop_DBus_request_name (driver_proxy,
38                                                                                          service_name,
39                                                                                          DBUS_NAME_FLAG_DO_NOT_QUEUE,
40                                                                                          &request_ret,
41                                                                                          &error);
42
43         if (!ret)
44         {
45                 g_warning (_("Unable to communicate with DBus: %s\n"), error->message);
46                 g_error_free (error);
47         }
48
49         if (request_ret == DBUS_REQUEST_NAME_REPLY_EXISTS)
50         {
51                 g_critical (_("There is already an instance of Tilda running\n"));
52                 exit (EXIT_FAILURE);
53         }
54
55         g_object_unref (driver_proxy);
56 }
57
58 static void
59 tilda_parse_command_line (gint argc, gchar *argv[])
60 {
61         debug_enter ();
62
63         gboolean version = FALSE;
64
65         /* All of the various command-line options */
66         const GOptionEntry cl_opts[] = {
67                 { "version",                    'V', 0, G_OPTION_ARG_NONE,              &version,                       N_("Show version information"), NULL },
68                 { NULL },
69         };
70
71         /* Set up the command-line parser */
72         GError *error = NULL;
73         GOptionContext *context = g_option_context_new (NULL);
74         g_option_context_add_main_entries (context, cl_opts, NULL);
75         g_option_context_add_group (context, gtk_get_option_group (TRUE));
76         g_option_context_parse (context, &argc, &argv, &error);
77         g_option_context_free (context);
78
79         /* Check for unknown options, and give a nice message if there are some */
80         if (error)
81         {
82                 g_printerr (_("Error parsing command-line options: %s\n"), error->message);
83                 g_printerr (_("The command \"tilda --help\" will show all possible options\n"));
84                 g_error_free (error);
85                 exit (EXIT_FAILURE);
86         }
87
88         /* If we need to show the version, show it then exit normally */
89         if (version)
90         {
91                 g_print ("%s\n\n", TILDA_VERSION);
92
93                 g_print ("Copyright (c) 2005-2008 Tristan Sloughter (sloutri@iit.edu)\n");
94                 g_print ("Copyright (c) 2005-2008 Ira W. Snyder (tilda@irasnyder.com)\n\n");
95
96                 g_print ("This program comes with ABSOLUTELY NO WARRANTY.\n");
97                 g_print ("This is free software, and you are welcome to redistribute it\n");
98                 g_print ("under certain conditions. See the file COPYING for details.\n");
99
100                 exit (EXIT_SUCCESS);
101         }
102 }
103
104 static void
105 tilda_termination_handler (gint signum)
106 {
107         debug_enter  ();
108         debug_printf ("signum: %d\n", signum);
109
110         gtk_main_quit ();
111 }
112
113 /* Hook up all system signal handlers */
114 static void
115 tilda_signal_handlers_init ()
116 {
117         struct sigaction sa;
118
119         /* Hook up signal handlers */
120         sa.sa_handler = tilda_termination_handler;
121         sigemptyset (&sa.sa_mask);
122         sa.sa_flags = 0;
123
124         sigaction (SIGINT,  &sa, NULL);
125         sigaction (SIGQUIT, &sa, NULL);
126         sigaction (SIGABRT, &sa, NULL);
127         sigaction (SIGTERM, &sa, NULL);
128
129         /* SIGKILL cannot be caught according to sigaction(2) and signal(7) */
130         /* sigaction (SIGKILL, &sa, NULL); */
131 }
132
133 int main (int argc, char *argv[])
134 {
135         debug_enter ();
136
137         TildaController *tilda;
138
139 #if ENABLE_NLS
140         /* Gettext Initialization */
141         setlocale (LC_ALL, "");
142         bindtextdomain (PACKAGE, LOCALEDIR);
143         textdomain (PACKAGE);
144 #endif
145
146         /* Parse the command-line options */
147         tilda_parse_command_line (argc, argv);
148
149         /* Initialize GTK+ (and the GObject system) */
150         gtk_init (&argc, &argv);
151
152         /* Hook up the signal handlers */
153         tilda_signal_handlers_init ();
154
155         /* Initialize the keybinder */
156         tomboy_keybinder_init ();
157
158         /* Start our connection to DBus */
159         tilda_dbus_init ();
160
161         /* Create a TildaController, which manages TildaWindows, which in turn manages
162          * TildaTerminals. Exit when it does. */
163         tilda = g_object_new (TILDA_TYPE_CONTROLLER, NULL);
164
165         debug_printf ("Starting gtk_main()!\n");
166         gtk_main ();
167         debug_printf ("Out of gtk_main(), going down\n");
168
169         /* Unref the TildaController that controls this whole operation */
170         g_object_unref (G_OBJECT(tilda));
171
172         return 0;
173 }
174
175 /* vim: set ts=4 sts=4 sw=4 noet tw=112: */