Fremantle: change "Control Panel" to "Settings menu" in package description
[browser-switch] / main.c
1 /*
2  * main.c -- config file parsing and main loop for browser-switchboard
3  *
4  * Copyright (C) 2009-2010 Steven Luo
5  * Derived from a Python implementation by Jason Simpson and Steven Luo
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20  * USA.
21  */
22
23 #include <stdlib.h>
24 #include <string.h>
25 #include <signal.h>
26 #include <errno.h>
27 #include <sys/types.h>
28 #include <sys/wait.h>
29 #include <dbus/dbus-glib.h>
30
31 #include "browser-switchboard.h"
32 #include "launcher.h"
33 #include "dbus-server-bindings.h"
34 #include "config.h"
35 #include "log.h"
36
37 struct swb_context ctx;
38
39 static void waitforzombies(int signalnum) {
40         while (waitpid(-1, NULL, WNOHANG) > 0)
41                 log_msg("Waited for a zombie\n");
42 }
43
44 static void read_config(int signalnum) {
45         struct swb_config cfg;
46
47         swb_config_init(&cfg);
48
49         swb_config_load(&cfg);
50
51         log_config(cfg.logging);
52         ctx.continuous_mode = cfg.continuous_mode;
53         free(ctx.other_browser_cmd);
54         if (cfg.other_browser_cmd) {
55                 if (!(ctx.other_browser_cmd = strdup(cfg.other_browser_cmd))) {
56                         log_perror(errno, "Failed to set other_browser_cmd");
57                         exit(1);
58                 }
59         } else
60                 ctx.other_browser_cmd = NULL;
61         update_default_browser(&ctx, cfg.default_browser);
62
63         log_msg("continuous_mode: %d\n", cfg.continuous_mode);
64         log_msg("default_browser: '%s'\n", cfg.default_browser);
65         log_msg("other_browser_cmd: '%s'\n",
66                 cfg.other_browser_cmd?cfg.other_browser_cmd:"NULL");
67         log_msg("logging: '%s'\n", cfg.logging);
68
69         swb_config_free(&cfg);
70         return;
71 }
72
73 int main() {
74         OssoBrowser *obj_osso_browser, *obj_osso_browser_req;
75         GMainLoop *mainloop;
76         GError *error = NULL;
77         int reqname_result;
78
79         read_config(0);
80
81         if (ctx.continuous_mode) {
82                 /* Install signal handlers */
83                 struct sigaction act;
84                 act.sa_flags = SA_RESTART;
85                 sigemptyset(&(act.sa_mask));
86
87                 /* SIGCHLD -- clean up after zombies */
88                 act.sa_handler = waitforzombies;
89                 if (sigaction(SIGCHLD, &act, NULL) == -1) {
90                         log_msg("Installing signal handler failed\n");
91                         return 1;
92                 }
93
94                 /* SIGHUP -- reread config file */
95                 act.sa_handler = read_config;
96                 if (sigaction(SIGHUP, &act, NULL) == -1) {
97                         log_msg("Installing signal handler failed\n");
98                         return 1;
99                 }
100         }
101
102         g_type_init();
103
104         dbus_g_object_type_install_info(OSSO_BROWSER_TYPE,
105                         &dbus_glib_osso_browser_object_info);
106
107         /* Get a connection to the D-Bus session bus */
108         ctx.session_bus = dbus_g_bus_get(DBUS_BUS_SESSION, &error);
109         if (!ctx.session_bus) {
110                 log_msg("Couldn't get a D-Bus bus connection\n");
111                 return 1;
112         }
113         ctx.dbus_proxy = dbus_g_proxy_new_for_name(ctx.session_bus,
114                         "org.freedesktop.DBus", "/org/freedesktop/DBus",
115                         "org.freedesktop.DBus");
116         if (!ctx.dbus_proxy) {
117                 log_msg("Couldn't get an org.freedesktop.DBus proxy\n");
118                 return 1;
119         }
120
121         /* Get the org.maemo.garage.browser-switchboard name from D-Bus, as
122            a form of locking to ensure that not more than one
123            browser-switchboard process is active at any time.  With
124            DBUS_NAME_FLAG_DO_NOT_QUEUE set and DBUS_NAME_FLAG_REPLACE_EXISTING
125            not set, getting the name succeeds if and only if no other
126            process owns the name. */
127         if (!dbus_g_proxy_call(ctx.dbus_proxy, "RequestName", &error,
128                                G_TYPE_STRING, "org.maemo.garage.browser-switchboard",
129                                G_TYPE_UINT, DBUS_NAME_FLAG_DO_NOT_QUEUE,
130                                G_TYPE_INVALID,
131                                G_TYPE_UINT, &reqname_result,
132                                G_TYPE_INVALID)) {
133                 log_msg("Couldn't acquire browser-switchboard lock: %s\n",
134                         error->message);
135                 return 1;
136         }
137         if (reqname_result != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {  
138                 log_msg("Another browser-switchboard already running\n");
139                 return 1;
140         }
141
142         dbus_request_osso_browser_name(&ctx);
143
144         /* Register ourselves to handle the osso_browser D-Bus methods */
145         obj_osso_browser = g_object_new(OSSO_BROWSER_TYPE, NULL);
146         obj_osso_browser_req = g_object_new(OSSO_BROWSER_TYPE, NULL);
147         dbus_g_connection_register_g_object(ctx.session_bus,
148                         "/com/nokia/osso_browser", G_OBJECT(obj_osso_browser));
149         dbus_g_connection_register_g_object(ctx.session_bus,
150                         "/com/nokia/osso_browser/request",
151                         G_OBJECT(obj_osso_browser_req));
152
153         mainloop = g_main_loop_new(NULL, FALSE);
154         log_msg("Starting main loop\n");
155         g_main_loop_run(mainloop);
156         log_msg("Main loop completed\n");
157
158         return 0;
159 }