aboutsummaryrefslogtreecommitdiffstats
path: root/calendar/conduits/common/libecalendar-common-conduit.c
blob: cdb4a10d2d45a53d88d6d9080a89d829f6cd4a46 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*
 * Evolution calendar - ToDo Conduit
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) version 3.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with the program; if not, see <http://www.gnu.org/licenses/>
 *
 *
 * Authors:
 *      Tom Billet <mouse256@ulyssis.org>
 *      Nathan Owens <pianocomp81@yahoo.com>
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 */

#include <string.h>

#include <libedataserver/e-categories.h>
#include <e-pilot-util.h>
#include <pi-appinfo.h>
#include <glib.h>
#include <gconf/gconf-client.h>

#include "libecalendar-common-conduit.h"

/*make debugging possible if it's required for a conduit */
#define LOG(x)
#ifdef DEBUG_CALCONDUIT
#define LOG(x) x
#endif
#ifdef DEBUG_MEMOCONDUIT
#define LOG(x) x
#endif
#ifdef DEBUG_TODOCONDUIT
#define LOG(x) x
#endif
#ifdef DEBUG_CONDUIT
#define LOG(x) x
#endif

/*
 * Adds a category to the category app info structure (name and ID),
 * sets category->renamed[i] to true if possible to rename.
 *
 * This will be packed and written to the app info block during post_sync.
 *
 * NOTE: cat_to_add MUST be in PCHAR format. Evolution stores categories
 *       in UTF-8 format. A conversion must take place before calling
 *       this function (see e_pilot_utf8_to_pchar() in e-pilot-util.c)
 */
int
e_pilot_add_category_if_possible(char *cat_to_add, struct CategoryAppInfo *category)
{
    int i, j;
    int retval = 0; /* 0 is the Unfiled category */
    LOG(g_message("e_pilot_add_category_if_possible\n"));

    for(i=0; i<PILOT_MAX_CATEGORIES; i++){
        /* if strlen is 0, then the category is empty
           the PalmOS doesn't let 0-length strings for
           categories */
        if(strlen(category->name[i]) == 0){
            int cat_to_add_len;
            int desktopUniqueID;

            cat_to_add_len = strlen(cat_to_add);
            retval = i;

            if(cat_to_add_len > 15){
                /* Have to truncate the category name */
                cat_to_add_len = 15;
            }

            /* only 15 characters for category, 16th is
             * '\0' can't do direct mem transfer due to
             * declaration type
             */
            for(j=0; j<cat_to_add_len; j++){
                category->name[i][j] = cat_to_add[j];
            }

            for(j=cat_to_add_len; j<16; j++) {
                category->name[i][j] = '\0';
            }

            /* find a desktop id that is not in use between 128 and 255 */
            for (desktopUniqueID = 128; desktopUniqueID <= 255; desktopUniqueID++) {
                int found = 0;
                for(j=0; j<PILOT_MAX_CATEGORIES; j++){
                    if (category->ID[i] == desktopUniqueID) {
                        found = 1;
                    }
                }
                if (found == 0) {
                    break;
                }
                if (desktopUniqueID == 255) {
                    LOG (g_warning ("*** no more categories available on PC ***"));
                }
            }
            category->ID[i] = desktopUniqueID;

            category->renamed[i] = TRUE;

            break;
        }
    }

    if(retval == 0){
        LOG (g_warning ("*** not adding category - category list already full ***"));
    }

    return retval;
}

/*
 *conversion from an evolution category to a palm category
 */
void e_pilot_local_category_to_remote(int * pilotCategory, ECalComponent *comp, struct CategoryAppInfo *category)
{
    GSList *c_list = NULL;
    char * category_string;
    int i;
    e_cal_component_get_categories_list (comp, &c_list);
    if (c_list) {
        /* list != 0, so at least 1 category is assigned */
        category_string = e_pilot_utf8_to_pchar((const char *)c_list->data);
        if (c_list->next != 0) {
            LOG (g_message ("Note: item has more categories in evolution, first chosen"));
        }
        i=1;
        while (1) {
            if (strcmp(category_string,category->name[i]) == 0) {
                *pilotCategory = i;
                break;
            }
            i++;
            if (i == PILOT_MAX_CATEGORIES) {
                /* category not available on palm, try to create it */
                *pilotCategory = e_pilot_add_category_if_possible(category_string,category);
                break;
            }
        }
        e_cal_component_free_categories_list(c_list);
        c_list = NULL;
    } else {
        *pilotCategory = 0;
    }
    /*end category*/
}

/*
 *conversion from a palm category to an evolution category
 */
void e_pilot_remote_category_to_local(int pilotCategory, ECalComponent *comp, struct CategoryAppInfo *category)
{
    char *category_string = NULL;

    if (pilotCategory != 0) {
        /* pda has category assigned */
        category_string = e_pilot_utf8_from_pchar(category->name[pilotCategory]);

        LOG(g_message("Category: %s\n", category_string));

        /* TODO The calendar editor page and search bar are not updated until a restart of the evolution client */
        if(e_categories_exist(category_string) == FALSE){
            /* add if it doesn't exist */
            LOG(g_message("Category created on pc\n"));
            e_categories_add(category_string, NULL, NULL, TRUE);
        }
    }

    /* store the data on in evolution */
    if (category_string == NULL) {
        /* note: this space is needed to make sure evolution clears the category */
        e_cal_component_set_categories (comp, " ");
    }
    else {

        /* Since only the first category is synced with the PDA, add the PDA's
         * category to the beginning of the category list */
        GSList *c_list = NULL;
        GSList *newcat_in_list;
        e_cal_component_get_categories_list (comp, &c_list);

        /* remove old item from list so we don't have duplicate entries */
        newcat_in_list = g_slist_find_custom(c_list, category_string, (GCompareFunc)strcmp);
        if(newcat_in_list != NULL)
        {
            c_list = g_slist_remove(c_list, newcat_in_list->data);
        }

        c_list = g_slist_prepend(c_list, category_string);
        e_cal_component_set_categories_list (comp, c_list);
        e_cal_component_free_categories_list(c_list);
    }
}

static char *
build_setup_path (const char *path, const char *key)
{
    return g_strconcat ("/apps/evolution/conduit", "/", path, "/", key, NULL);
}

gboolean
e_pilot_setup_get_bool (const char *path, const char *key, gboolean def)
{
    gboolean res = def;
    char *full_path;
    GConfValue *value;
    GConfClient *gconf;

    g_return_val_if_fail (path != NULL, res);
    g_return_val_if_fail (key != NULL, res);

    gconf = gconf_client_get_default ();
    full_path = build_setup_path (path, key);

    value = gconf_client_get (gconf, full_path, NULL);
    if (value) {
        if (value->type == GCONF_VALUE_BOOL)
            res = gconf_value_get_bool (value);

        gconf_value_free (value);
    }

    g_free (full_path);
    g_object_unref (gconf);

    return res;
}

void
e_pilot_setup_set_bool (const char *path, const char *key, gboolean value)
{
    GError *error = NULL;
    char *full_path;
    GConfClient *gconf;

    g_return_if_fail (path != NULL);
    g_return_if_fail (key != NULL);

    gconf = gconf_client_get_default ();
    full_path = build_setup_path (path, key);

    gconf_client_set_bool (gconf, full_path, value, &error);

    g_free (full_path);
    g_object_unref (gconf);

    if (error) {
        g_message ("%s: Failed to write: %s", G_STRFUNC, error->message);
        g_error_free (error);
    }
}

int
e_pilot_setup_get_int (const char *path, const char *key, int def)
{
    int res = def;
    char *full_path;
    GConfValue *value;
    GConfClient *gconf;

    g_return_val_if_fail (path != NULL, res);
    g_return_val_if_fail (key != NULL, res);

    gconf = gconf_client_get_default ();
    full_path = build_setup_path (path, key);

    value = gconf_client_get (gconf, full_path, NULL);
    if (value) {
        if (value->type == GCONF_VALUE_INT)
            res = gconf_value_get_int (value);

        gconf_value_free (value);
    }

    g_free (full_path);
    g_object_unref (gconf);

    return res;
}

void
e_pilot_setup_set_int (const char *path, const char *key, int value)
{
    GError *error = NULL;
    char *full_path;
    GConfClient *gconf;

    g_return_if_fail (path != NULL);
    g_return_if_fail (key != NULL);

    gconf = gconf_client_get_default ();
    full_path = build_setup_path (path, key);

    gconf_client_set_int (gconf, full_path, value, &error);

    g_free (full_path);
    g_object_unref (gconf);

    if (error) {
        g_message ("%s: Failed to write: %s", G_STRFUNC, error->message);
        g_error_free (error);
    }
}

char *
e_pilot_setup_get_string (const char *path, const char *key, const char *def)
{
    char *res = g_strdup (def);
    char *full_path;
    GConfValue *value;
    GConfClient *gconf;

    g_return_val_if_fail (path != NULL, res);
    g_return_val_if_fail (key != NULL, res);

    gconf = gconf_client_get_default ();
    full_path = build_setup_path (path, key);

    value = gconf_client_get (gconf, full_path, NULL);
    if (value) {
        if (value->type == GCONF_VALUE_STRING) {
            g_free (res);
            res = g_strdup (gconf_value_get_string (value));
        }

        gconf_value_free (value);
    }

    g_free (full_path);
    g_object_unref (gconf);

    return res;
}

void
e_pilot_setup_set_string (const char *path, const char *key, const char *value)
{
    GError *error = NULL;
    char *full_path;
    GConfClient *gconf;

    g_return_if_fail (path != NULL);
    g_return_if_fail (key != NULL);
    g_return_if_fail (value != NULL);

    gconf = gconf_client_get_default ();
    full_path = build_setup_path (path, key);

    gconf_client_set_string (gconf, full_path, value, &error);

    g_free (full_path);
    g_object_unref (gconf);

    if (error) {
        g_message ("%s: Failed to write: %s", G_STRFUNC, error->message);
        g_error_free (error);
    }
}