Remove some unused code.
[platform/upstream/ibus.git] / src / ibusshare.c
1 /* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
2 /* vim:set et sts=4: */
3 /* ibus - The Input Bus
4  * Copyright (C) 2008-2010 Peng Huang <shawn.p.huang@gmail.com>
5  * Copyright (C) 2008-2010 Red Hat, Inc.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library 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 GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #include "ibusshare.h"
24 #include <glib.h>
25 #include <glib/gstdio.h>
26 #include <glib-object.h>
27 #include <sys/time.h>
28 #include <sys/types.h>
29 #include <unistd.h>
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <ibus.h>
34
35 static gchar *_display = NULL;
36
37 const gchar *
38 ibus_get_local_machine_id (void)
39 {
40     static gchar *machine_id = NULL;
41
42     if (machine_id == NULL) {
43         GError *error = NULL;
44         if (!g_file_get_contents ("/var/lib/dbus/machine-id",
45                                   &machine_id,
46                                   NULL,
47                                   &error)) {
48             g_warning ("Unable to load /var/lib/dbus/machine-id: %s", error->message);
49             g_error_free (error);
50             machine_id = "machine-id";
51         }
52         else {
53             g_strstrip (machine_id);
54         }
55     }
56
57     return machine_id;
58 }
59
60 void
61 ibus_set_display (const gchar *display)
62 {
63     if (_display != NULL)
64         g_free (_display);
65     _display = g_strdup (display);
66 }
67
68 const gchar *
69 ibus_get_user_name (void)
70 {
71     return g_get_user_name ();
72 }
73
74 glong
75 ibus_get_daemon_uid (void)
76 {
77     return getuid ();
78 }
79
80 const gchar *
81 ibus_get_session_id (void)
82 {
83     return g_getenv("IBUS_SESSION_ID");
84 }
85
86 const gchar *
87 ibus_get_socket_path (void)
88 {
89     static gchar *path = NULL;
90
91     if (path == NULL) {
92         gchar *hostname = "unix";
93         gchar *display;
94         gchar *displaynumber = "0";
95         /* gchar *screennumber = "0"; */
96         gchar *p;
97
98         path = g_strdup (g_getenv ("IBUS_ADDRESS_FILE"));
99         if (path != NULL) {
100             return path;
101         }
102
103         if (_display == NULL) {
104             display = g_strdup (g_getenv ("DISPLAY"));
105         }
106         else {
107             display = g_strdup (_display);
108         }
109
110         if (display == NULL) {
111             g_warning ("DISPLAY is empty! We use default DISPLAY (:0.0)");
112         }
113         else {
114             p = display;
115             hostname = display;
116             for (; *p != ':' && *p != '\0'; p++);
117
118             if (*p == ':') {
119                 *p = '\0';
120                 p++;
121                 displaynumber = p;
122             }
123
124             for (; *p != '.' && *p != '\0'; p++);
125
126             if (*p == '.') {
127                 *p = '\0';
128                 p++;
129                 /* Do not use screennumber
130                  screennumber = p; */
131             }
132         }
133
134         if (hostname[0] == '\0')
135             hostname = "unix";
136
137         p = g_strdup_printf ("%s-%s-%s",
138                              ibus_get_local_machine_id (),
139                              hostname,
140                              displaynumber);
141         path = g_build_filename (g_get_user_config_dir (),
142                                  "ibus",
143                                  "bus",
144                                  p,
145                                  NULL);
146         g_free (p);
147         g_free (display);
148     }
149     return path;
150 }
151
152 gint
153 ibus_get_timeout (void)
154 {
155     /* 6000 ms is the default timeout on the ibus-daemon side (5 sec) plus 1. */
156     static const gint default_timeout = 6000;
157
158     static gint64 timeout = -2;
159     if (timeout == -2) {
160         const gchar *timeout_str = g_getenv ("IBUS_TIMEOUT");
161         if (timeout_str == NULL) {
162             timeout = default_timeout;
163         } else {
164             timeout = g_ascii_strtoll(timeout_str, NULL, 10);
165             if (timeout < -1 || timeout == 0 || timeout > G_MAXINT) {
166                 timeout = default_timeout;
167             }
168         }
169     }
170     return timeout;
171 }
172
173 const gchar *
174 ibus_get_address (void)
175 {
176     static gchar *address = NULL;
177     pid_t pid = -1;
178     static gchar buffer[1024];
179     FILE *pf;
180
181     /* free address */
182     if (address != NULL) {
183         g_free (address);
184         address = NULL;
185     }
186
187     /* get address from env variable */
188     address = g_strdup (g_getenv ("IBUS_ADDRESS"));
189     if (address) {
190         return address;
191     }
192
193     /* read address from ~/.config/ibus/bus/soketfile */
194     pf = fopen (ibus_get_socket_path (), "r");
195     if (pf == NULL) {
196         return NULL;
197     }
198
199     while (!feof (pf)) {
200         gchar *p = buffer;
201         if (fgets (buffer, sizeof (buffer), pf) == NULL)
202             break;
203
204         /* skip comment line */
205         if (p[0] == '#')
206             continue;
207         /* parse IBUS_ADDRESS */
208         if (strncmp (p, "IBUS_ADDRESS=", sizeof ("IBUS_ADDRESS=") - 1) == 0) {
209             address = p + sizeof ("IBUS_ADDRESS=") - 1;
210             for (p = (gchar *)address; *p != '\n' && *p != '\0'; p++);
211             if (*p == '\n')
212                 *p = '\0';
213             address = g_strdup (address);
214             continue;
215         }
216
217         /* parse IBUS_DAEMON_PID */
218         if (strncmp (p, "IBUS_DAEMON_PID=", sizeof ("IBUS_DAEMON_PID=") - 1) == 0) {
219             pid = atoi(p + sizeof ("IBUS_DAEMON_PID=") - 1);
220             continue;
221         }
222
223     }
224     fclose (pf);
225
226     if (pid == -1 || kill (pid, 0) != 0) {
227         return NULL;
228     }
229
230     return address;
231 }
232
233 void
234 ibus_write_address (const gchar *address)
235 {
236     FILE *pf;
237     gchar *path;
238     g_return_if_fail (address != NULL);
239
240     path = g_path_get_dirname (ibus_get_socket_path ());
241     g_mkdir_with_parents (path, 0700);
242     g_free (path);
243
244     g_unlink (ibus_get_socket_path ());
245     pf = fopen (ibus_get_socket_path (), "w");
246     g_return_if_fail (pf != NULL);
247
248     fprintf (pf,
249         "# This file is created by ibus-daemon, please do not modify it\n"
250         "IBUS_ADDRESS=%s\n"
251         "IBUS_DAEMON_PID=%ld\n",
252         address, (glong) getpid ());
253     fclose (pf);
254 }
255
256 void
257 ibus_free_strv (gchar **strv)
258 {
259     gchar **p;
260
261     if (strv == NULL)
262         return;
263
264     for (p = strv; *p != NULL; p++) {
265         g_free (*p);
266     }
267
268     g_free (strv);
269 }
270
271 void
272 ibus_init (void)
273 {
274     g_type_init ();
275     IBUS_ERROR;
276     IBUS_TYPE_TEXT;
277     IBUS_TYPE_ATTRIBUTE;
278     IBUS_TYPE_ATTR_LIST;
279     IBUS_TYPE_LOOKUP_TABLE;
280     IBUS_TYPE_COMPONENT;
281     IBUS_TYPE_ENGINE_DESC;
282 }
283
284 static GMainLoop *main_loop = NULL;
285
286 void
287 ibus_main (void)
288 {
289     main_loop = g_main_loop_new (NULL, FALSE);
290
291     g_main_loop_run (main_loop);
292
293     g_main_loop_unref (main_loop);
294     main_loop = NULL;
295 }
296
297 void
298 ibus_quit (void)
299 {
300     if (main_loop) {
301         g_main_loop_quit (main_loop);
302     }
303 }
304
305 static gboolean ibus_log_handler_is_verbose = FALSE;
306 static guint ibus_log_handler_id = 0;
307
308 static void
309 ibus_log_handler (const gchar    *log_domain,
310                   GLogLevelFlags  log_level,
311                   const gchar    *message,
312                   gpointer        user_data)
313 {
314     // In the quiet mode (i.e. not verbose), we'll ignore DEBUG and
315     // WARNING messages.
316     if (!ibus_log_handler_is_verbose &&
317         ((log_level & G_LOG_LEVEL_DEBUG) ||
318          (log_level & G_LOG_LEVEL_WARNING))) {
319         return;
320     }
321     // Add timing info like "17:34:57.680038" (hour, min, sec, microsecond).
322     struct timeval time_val;
323     gettimeofday (&time_val, NULL);
324     struct tm local_time;
325     localtime_r (&time_val.tv_sec, &local_time);
326     char* new_message =
327         g_strdup_printf ("%02d:%02d:%02d.%6d: %s",
328                          local_time.tm_hour,
329                          local_time.tm_min,
330                          local_time.tm_sec,
331                          (int)time_val.tv_usec,
332                          message);
333     g_log_default_handler (log_domain, log_level, new_message, user_data);
334     g_free (new_message);
335 }
336
337 void
338 ibus_set_log_handler (gboolean verbose)
339 {
340     if (ibus_log_handler_id != 0) {
341         g_log_remove_handler (G_LOG_DOMAIN, ibus_log_handler_id);
342     }
343
344     ibus_log_handler_is_verbose = verbose;
345     ibus_log_handler_id = g_log_set_handler (G_LOG_DOMAIN,
346                                              G_LOG_LEVEL_MASK,
347                                              ibus_log_handler,
348                                              NULL);
349 }
350
351 void
352 ibus_unset_log_handler (void)
353 {
354     if (ibus_log_handler_id != 0) {
355         g_log_remove_handler (G_LOG_DOMAIN, ibus_log_handler_id);
356         ibus_log_handler_id = 0;
357     }
358 }