~joel-auterson/ubuntu/maverick/ibus/newmenuname

« back to all changes in this revision

Viewing changes to src/ibusshare.c

  • Committer: Bazaar Package Importer
  • Author(s): LI Daobing
  • Date: 2009-10-05 20:45:18 UTC
  • mfrom: (1.1.5 upstream) (6.1.15 sid)
  • Revision ID: james.westby@ubuntu.com-20091005204518-069vlwrl3r8v7bbr
Tags: 1.2.0.20090927-2
* create po template when build (LP: #188690)
  - debian/rules: updated.
  - debian/clean: remove pot file when clean.
* debian/control: build depends on python-rsvg (LP: #432375)

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 * Boston, MA 02111-1307, USA.
19
19
 */
20
20
 
 
21
#include <glib.h>
 
22
#include <glib/gstdio.h>
21
23
#include <glib-object.h>
 
24
#include <sys/types.h>
22
25
#include <unistd.h>
23
26
#include <stdlib.h>
24
27
#include <stdio.h>
115
118
}
116
119
 
117
120
const gchar *
118
 
ibus_get_socket_folder (void)
119
 
{
120
 
    static gchar *folder = NULL;
121
 
 
122
 
    if (folder == NULL) {
123
 
        const gchar *session = ibus_get_session_id ();
124
 
        if (session && session[0] != '\0') {
125
 
            folder = g_strdup_printf ("/tmp/ibus-%s-%s",
126
 
                ibus_get_user_name (), session);
127
 
        }
128
 
        else {
129
 
            folder = g_strdup_printf ("/tmp/ibus-%s",
130
 
                ibus_get_user_name ());
131
 
        }
132
 
    }
133
 
    return folder;
134
 
}
135
 
 
136
 
const gchar *
137
121
ibus_get_socket_path (void)
138
122
{
139
123
    static gchar *path = NULL;
143
127
        gchar *display;
144
128
        gchar *displaynumber = "0";
145
129
        gchar *screennumber = "0";
146
 
        const gchar *folder= NULL;
147
130
        gchar *p;
148
131
 
149
132
        if (_display == NULL) {
176
159
            }
177
160
        }
178
161
 
179
 
        folder = ibus_get_socket_folder ();
180
 
 
181
162
        if (hostname[0] == '\0')
182
163
            hostname = "unix";
183
164
 
199
180
const gchar *
200
181
ibus_get_address (void)
201
182
{
202
 
    const gchar *address = NULL;
 
183
    static gchar *address = NULL;
 
184
    pid_t pid = -1;
203
185
    static gchar buffer[1024];
204
186
    FILE *pf;
205
187
 
 
188
    /* free address */
 
189
    if (address != NULL) {
 
190
        g_free (address);
 
191
        address = NULL;
 
192
    }
 
193
 
206
194
    /* get address from evn variable */
207
 
    address = g_getenv ("IBUS_ADDRESS");
208
 
    if (address)
 
195
    address = g_strdup (g_getenv ("IBUS_ADDRESS"));
 
196
    if (address) {
209
197
        return address;
 
198
    }
210
199
 
211
200
    /* read address from ~/.config/ibus/bus/soketfile */
212
201
    pf = fopen (ibus_get_socket_path (), "r");
213
 
    if (pf == NULL)
 
202
    if (pf == NULL) {
214
203
        return NULL;
 
204
    }
215
205
 
216
206
    while (!feof (pf)) {
217
207
        gchar *p = buffer;
218
208
        if (fgets (buffer, sizeof (buffer), pf) == NULL)
219
209
            break;
 
210
 
 
211
        /* skip comment line */
220
212
        if (p[0] == '#')
221
213
            continue;
222
 
        if (strncmp (p, "IBUS_ADDRESS=", sizeof ("IBUS_ADDRESS=") - 1) != 0)
223
 
            continue;
224
 
        address = p + sizeof ("IBUS_ADDRESS=") - 1;
225
 
        for (p = (gchar *)address; *p != '\n' && *p != '\0'; p++);
226
 
        if (*p == '\n')
227
 
            *p = '\0';
228
 
        break;
 
214
        /* parse IBUS_ADDRESS */
 
215
        if (strncmp (p, "IBUS_ADDRESS=", sizeof ("IBUS_ADDRESS=") - 1) == 0) {
 
216
            address = p + sizeof ("IBUS_ADDRESS=") - 1;
 
217
            for (p = (gchar *)address; *p != '\n' && *p != '\0'; p++);
 
218
            if (*p == '\n')
 
219
                *p = '\0';
 
220
            address = g_strdup (address);
 
221
            continue;
 
222
        }
 
223
 
 
224
        /* parse IBUS_DAEMON_PID */
 
225
        if (strncmp (p, "IBUS_DAEMON_PID=", sizeof ("IBUS_DAEMON_PID=") - 1) == 0) {
 
226
            pid = atoi(p + sizeof ("IBUS_DAEMON_PID=") - 1);
 
227
            continue;
 
228
        }
 
229
 
229
230
    }
230
 
 
231
231
    fclose (pf);
 
232
 
 
233
    if (pid == -1 || kill (pid, 0) != 0) {
 
234
        return NULL;
 
235
    }
 
236
 
232
237
    return address;
233
238
}
234
239
 
243
248
    g_mkdir_with_parents (path, 0700);
244
249
    g_free (path);
245
250
 
 
251
    g_unlink (ibus_get_socket_path ());
246
252
    pf = fopen (ibus_get_socket_path (), "w");
247
253
    g_return_if_fail (pf != NULL);
248
254
 
249
 
    fprintf (pf, "# This file is created by ibus-daemon, please do not modify it\n");
250
 
    fprintf (pf, "IBUS_ADDRESS=%s\n", address);
 
255
    fprintf (pf,
 
256
        "# This file is created by ibus-daemon, please do not modify it\n"
 
257
        "IBUS_ADDRESS=%s\n"
 
258
        "IBUS_DAEMON_PID=%ld\n",
 
259
        address, (glong) getpid ());
251
260
    fclose (pf);
252
261
}
253
262