Create and register two different OssoBrowser objects instead of reusing one
[browser-switch] / main.c
1 /*
2  * main.c -- config file parsing and main loop for browser-switchboard
3  *
4  * Copyright (C) 2009 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 <stdio.h>
26 #include <unistd.h>
27 #include <signal.h>
28 #include <sys/types.h>
29 #include <sys/wait.h>
30 #include <regex.h>
31 #include <dbus/dbus-glib.h>
32
33 #include "browser-switchboard.h"
34 #include "launcher.h"
35 #include "dbus-server-bindings.h"
36
37 #define DEFAULT_HOMEDIR "/home/user"
38 #define CONFIGFILE_LOC "/.config/browser-switchboard"
39 #define MAXLINE 1024
40
41 struct swb_context ctx;
42
43 static void set_config_defaults(struct swb_context *ctx) {
44         if (!ctx)
45                 return;
46         free(ctx->other_browser_cmd);
47         ctx->continuous_mode = 0;
48         ctx->default_browser_launcher = NULL;
49         ctx->other_browser_cmd = NULL;
50 }
51
52 static void waitforzombies(int signalnum) {
53         while (waitpid(-1, NULL, WNOHANG) > 0)
54                 printf("Waited for a zombie\n");
55 }
56
57 static void read_config(int signalnum) {
58         char *homedir, *configfile;
59         size_t len;
60         char buf[MAXLINE];
61         char *key, *value;
62         char *default_browser = NULL;
63         FILE *fp;
64         regex_t re_ignore, re_config1, re_config2;
65         regmatch_t substrs[3];
66
67         set_config_defaults(&ctx);
68
69         if (!(homedir = getenv("HOME")))
70                 homedir = DEFAULT_HOMEDIR;
71         len = strlen(homedir) + strlen(CONFIGFILE_LOC) + 1;
72         if (!(configfile = calloc(len, sizeof(char))))
73                 goto out_noopen;
74         snprintf(configfile, len, "%s%s", homedir, CONFIGFILE_LOC);
75
76         if (!(fp = fopen(configfile, "r")))
77                 goto out_noopen;
78
79         /* compile regex matching blank lines or comments */
80         if (regcomp(&re_ignore, "^[[:space:]]*(#|$)", REG_EXTENDED|REG_NOSUB))
81                 goto out_nore;
82         /* compile regex matching foo = "bar", with arbitrary whitespace at
83            beginning and end of line and surrounding the = */
84         if (regcomp(&re_config1,
85                     "^[[:space:]]*([^=[:space:]]+)[[:space:]]*=[[:space:]]*\"(.*)\"[[:space:]]*$",
86                     REG_EXTENDED)) {
87                 regfree(&re_ignore);
88                 goto out_nore;
89         }
90         /* compile regex matching foo = bar, with arbitrary whitespace at
91            beginning of line and surrounding the = */
92         if (regcomp(&re_config2,
93                     "^[[:space:]]*([^=[:space:]]+)[[:space:]]*=[[:space:]]*(.*)$",
94                     REG_EXTENDED|REG_NEWLINE)) {
95                 regfree(&re_ignore);
96                 regfree(&re_config1);
97                 goto out_nore;
98         }
99
100         /* Read in the config file one line at a time and parse it
101            XXX doesn't deal with lines longer than MAXLINE */
102         while (fgets(buf, MAXLINE, fp)) {
103                 /* skip blank lines and comments */
104                 if (!regexec(&re_ignore, buf, 0, NULL, 0))
105                         continue;
106
107                 /* Find the substrings corresponding to the key and value
108                    If the line doesn't match our idea of a config file entry,
109                    skip it */
110                 if (regexec(&re_config1, buf, 3, substrs, 0) &&
111                     regexec(&re_config2, buf, 3, substrs, 0))
112                         continue;
113                 if (substrs[1].rm_so == -1 || substrs[2].rm_so == -1)
114                         continue;
115
116                 /* copy the config value into a new string */
117                 len = substrs[2].rm_eo - substrs[2].rm_so;
118                 if (!(value = calloc(len+1, sizeof(char))))
119                         goto out;
120                 strncpy(value, buf+substrs[2].rm_so, len);
121                 /* calloc() zeroes the memory, so string is automatically
122                    null terminated */
123
124                 /* make key point to a null-terminated string holding the 
125                    config key */
126                 key = buf + substrs[1].rm_so;
127                 buf[substrs[1].rm_eo] = '\0';
128
129                 if (!strcmp(key, "continuous_mode")) {
130                         ctx.continuous_mode = atoi(value);
131                         free(value);
132                 } else if (!strcmp(key, "default_browser")) {
133                         if (!default_browser)
134                                 default_browser = value;
135                 } else if (!strcmp(key, "other_browser_cmd")) {
136                         if (!ctx.other_browser_cmd)
137                                 ctx.other_browser_cmd = value;
138                 } else {
139                         /* Don't need this line's contents */
140                         free(value);
141                 }
142                 value = NULL;
143         }
144
145         printf("continuous_mode: %d\n", ctx.continuous_mode);
146         printf("default_browser: '%s'\n", default_browser?default_browser:"NULL");
147         printf("other_browser_cmd: '%s'\n", ctx.other_browser_cmd?ctx.other_browser_cmd:"NULL");
148
149 out:
150         regfree(&re_ignore);
151         regfree(&re_config1);
152         regfree(&re_config2);
153 out_nore:
154         fclose(fp);
155 out_noopen:
156         update_default_browser(&ctx, default_browser);
157         free(configfile);
158         free(default_browser);
159         return;
160 }
161
162 int main() {
163         OssoBrowser *obj_osso_browser, *obj_osso_browser_req;
164         GMainLoop *mainloop;
165         GError *error = NULL;
166
167         read_config(0);
168
169         if (ctx.continuous_mode) {
170                 struct sigaction act;
171                 act.sa_flags = SA_RESTART;
172
173                 act.sa_handler = waitforzombies;
174                 if (sigaction(SIGCHLD, &act, NULL) == -1) {
175                         printf("Installing signal handler failed\n");
176                         return 1;
177                 }
178
179                 act.sa_handler = read_config;
180                 if (sigaction(SIGHUP, &act, NULL) == -1) {
181                         printf("Installing signal handler failed\n");
182                         return 1;
183                 }
184         }
185
186         g_type_init();
187
188         dbus_g_object_type_install_info(OSSO_BROWSER_TYPE,
189                         &dbus_glib_osso_browser_object_info);
190
191         ctx.session_bus = dbus_g_bus_get(DBUS_BUS_SESSION, &error);
192         if (!ctx.session_bus) {
193                 printf("Couldn't get a D-Bus bus connection\n");
194                 return 1;
195         }
196         ctx.dbus_proxy = dbus_g_proxy_new_for_name(ctx.session_bus,
197                         "org.freedesktop.DBus", "/org/freedesktop/DBus",
198                         "org.freedesktop.DBus");
199         if (!ctx.dbus_proxy) {
200                 printf("Couldn't get an org.freedesktop.DBus proxy\n");
201                 return 1;
202         }
203
204         dbus_request_osso_browser_name(&ctx);
205
206         obj_osso_browser = g_object_new(OSSO_BROWSER_TYPE, NULL);
207         obj_osso_browser_req = g_object_new(OSSO_BROWSER_TYPE, NULL);
208         dbus_g_connection_register_g_object(ctx.session_bus,
209                         "/com/nokia/osso_browser", G_OBJECT(obj_osso_browser));
210         dbus_g_connection_register_g_object(ctx.session_bus,
211                         "/com/nokia/osso_browser/request",
212                         G_OBJECT(obj_osso_browser_req));
213
214         mainloop = g_main_loop_new(NULL, FALSE);
215         printf("Starting main loop\n");
216         g_main_loop_run(mainloop);
217         printf("Main loop completed\n");
218
219         return 0;
220 }