File: AGUserConfig.c

package info (click to toggle)
agsync 0.2-pre-9
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 2,044 kB
  • ctags: 1,183
  • sloc: ansic: 9,979; sh: 8,390; makefile: 86
file content (544 lines) | stat: -rw-r--r-- 15,920 bytes parent folder | download | duplicates (12)
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
/* The contents of this file are subject to the Mozilla Public License
 * Version 1.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 *
 * The Original Code is Mobile Application Link.
 *
 * The Initial Developer of the Original Code is AvantGo, Inc.
 * Portions created by AvantGo, Inc. are Copyright (C) 1997-1999
 * AvantGo, Inc. All Rights Reserved.
 *
 * Contributor(s):
 */

/* Owner:  miket */

#include <AGUserConfig.h>
#include <AGServerConfig.h>
#include <AGUtil.h>
#ifndef REMOVE_SYNCHRONIZE_FEATURE
#include <AGSynchronize.h>
#endif

#define AG_UC_DESKTOP_OFFSET (0x40000000)

typedef AGServerConfig * LPAGSC;

/* ----------------------------------------------------------------------------
*/
static int32 getNextUID(AGUserConfig * uc, AGBool device)
{
    if (device)
        return uc->nextUID++;
    else
        return AG_UC_DESKTOP_OFFSET + uc->nextUID++;
}

#ifdef DEBUG
/* ----------------------------------------------------------------------------
*/
static AGBool serverConfigUidIsUnique(AGArray * array, int32 uid)
{
    int32 i, n;
    n = AGArrayCount(array);
    for (i = 0; i < n; ++i) {
        if (((LPAGSC)AGArrayElementAt(array, i))->uid == uid)
            return FALSE;
    }
    return TRUE;
}
#endif

/* ----------------------------------------------------------------------------
*/
void AGUserConfigAddServer(AGUserConfig * uc, LPAGSC sc, AGBool device)
{
    if (0 == sc->uid)
        sc->uid = getNextUID(uc, device);
#ifdef DEBUG
#ifndef _WIN32_WCE
    assert(serverConfigUidIsUnique(uc->servers, sc->uid));
#endif
#endif
    AGArrayAppend(uc->servers, sc);
    uc->dirty = TRUE;
}

/* ----------------------------------------------------------------------------
*/
LPAGSC AGUserConfigGetServer(AGUserConfig * uc, int32 uid)
{
    LPAGSC result = NULL;

    int32 n = AGArrayCount(uc->servers);
    while (n--) {
        result = (LPAGSC)AGArrayElementAt(uc->servers, n);
        if (uid == result->uid)
            return result;
    }
    return NULL;
}

/* ----------------------------------------------------------------------------
*/
static void addToDeleteList(AGUserConfig * uc, int32 uid)
{
    AGArrayAppend(uc->uidDeletes, (void*)uid);
}

/* ----------------------------------------------------------------------------
*/
void AGUserConfigRemoveServer(AGUserConfig * uc, int32 uid)
{
    LPAGSC sc = AGUserConfigGetServer(uc, uid);
    if (NULL != sc) {
        AGArrayRemoveAt(uc->servers, AGArrayIndexOf(uc->servers, sc, 0));
        AGServerConfigFree(sc);
        if (uid < AG_UC_DESKTOP_OFFSET)
            addToDeleteList(uc, uid);
        uc->dirty = TRUE;
    }
}

#ifndef REMOVE_SYNCHRONIZE_FEATURE
/* ----------------------------------------------------------------------------
*/
static void syncExistingServers(AGUserConfig * result,
                                AGUserConfig * agreed,
                                AGUserConfig * device,
                                AGUserConfig * desktop,
                                AGBool preferDesktop)
{
    int n = AGUserConfigCount(device);
    while (n--) {
        LPAGSC sc1 = NULL, sc2 = NULL;
        sc1 = AGUserConfigGetServerByIndex(device, n);
        sc2 = AGUserConfigGetServer(desktop, sc1->uid);
        if (NULL != sc2) {
            LPAGSC scAgreed = NULL, scResult = NULL;
            if (NULL != agreed)
                scAgreed = AGUserConfigGetServer(agreed, sc1->uid);
            if (NULL != scAgreed)
                scResult = AGServerConfigSynchronize(scAgreed,
                    sc1,
                    sc2,
                    preferDesktop);
            else
                scResult = AGServerConfigDup(sc1);
            AGUserConfigAddServer(result, scResult, TRUE);
        }
    }
}

/* ----------------------------------------------------------------------------
*/
static void addNewServers(AGUserConfig * result,
                          AGUserConfig * first,
                          AGUserConfig * second)
{
    int n = AGUserConfigCount(first);
    while (n--) {
        LPAGSC sc = NULL;
        sc = AGUserConfigGetServerByIndex(first, n);
        if (NULL == AGUserConfigGetServer(second, sc->uid)) {
            LPAGSC tsc = AGServerConfigDup(sc);
            if (NULL != tsc) {
                if (tsc->uid >= AG_UC_DESKTOP_OFFSET)
                    tsc->uid = 0;
                AGUserConfigAddServer(result, tsc, TRUE);
            }
        }
    }
}

/* ----------------------------------------------------------------------------
*/
static void mergeUserConfigs(AGUserConfig * result,
                             AGUserConfig * agreed,
                             AGUserConfig * device,
                             AGUserConfig * desktop,
                             AGBool preferDesktop)
{
    syncExistingServers(result, agreed, device, desktop, preferDesktop);
    addNewServers(result, device, desktop);
    addNewServers(result, desktop, device);
}

/* ----------------------------------------------------------------------------
*/
static void deleteMarkedServerConfigs(AGUserConfig * uc,
                                      AGArray * list)
{
    int n = AGArrayCount(list);
    while (n--) {
        AGUserConfigRemoveServer(uc, (int32)AGArrayElementAt(list, n));
    }
}

/* ----------------------------------------------------------------------------
*/
static void handleDeletes(AGUserConfig * result,
                          AGUserConfig * device,
                          AGUserConfig * desktop)
{
    deleteMarkedServerConfigs(result, device->uidDeletes);
    deleteMarkedServerConfigs(result, desktop->uidDeletes);
}

/* ----------------------------------------------------------------------------
*/
static void resetDeleteList(AGUserConfig * uc)
{
    AGArrayRemoveAll(uc->uidDeletes);
}

/* ----------------------------------------------------------------------------
*/
static void convertTempUIDs(AGUserConfig * obj)
{
    int n;

    n = AGArrayCount(obj->servers);
    while (n--) {
        AGServerConfig * sc = AGUserConfigGetServerByIndex(obj, n);
        if (sc->uid >= AG_UC_DESKTOP_OFFSET)
            sc->uid -= AG_UC_DESKTOP_OFFSET;
    }
}

/* ----------------------------------------------------------------------------
*/
static void checkForCookieReset(AGUserConfig * obj)
{
    int n;

    n = AGArrayCount(obj->servers);
    while (n--) {
        AGServerConfig * sc = AGUserConfigGetServerByIndex(obj, n);
        if (sc->resetCookie) {
            AGServerConfigResetCookie(sc);
            AGServerConfigResetNonce(sc);
            sc->resetCookie = FALSE;
        }
    }
}

/* ----------------------------------------------------------------------------
*/
AGUserConfig * AGUserConfigSynchronize(AGUserConfig *agreed,
                                       AGUserConfig *device,
                                       AGUserConfig *desktop,
                                       AGBool preferDesktop)
{
    AGUserConfig * result = NULL;
    AGUserConfig * cw = NULL;

    cw = preferDesktop ? desktop : device;

    /* If we were handed nothing, return a new uc. */
    if (NULL == device && NULL == desktop)
        return AGUserConfigNew();

    /* If either one is empty, return the other one. */
    if (NULL == device)
        result = desktop;
    if (NULL == desktop)
        result = device;
    if (NULL != result) {
        result = AGUserConfigDup(result);
        if (NULL != result) {

            /*  We're just duplicating one of the two userConfigs
                because we don't have another to sync against.  That's
                fine, but we should also do whatever processing the
                sync would have done. pending(miket): this is poorly
                structured. All syncs should go through the same pipeline
                regardless of any special cases. So eliminate this special
                case and the following code.
            */
            convertTempUIDs(result);
            checkForCookieReset(result);
            resetDeleteList(result);
        }
        return result;
    }

    /* Have two UserConfigs, so we need to merge them.
    Begin with a brand-new UserConfig structure. */
    result = AGUserConfigNew();

    if (NULL == result)
        return NULL;

    /* Put in the easy fields. */
    result->dirty = FALSE;
    result->nextUID = (desktop->nextUID > device->nextUID)
        ? desktop->nextUID : device->nextUID;
    result->reservedLen = cw->reservedLen;
    AGSynchronizeData(&result->reserved,
        &result->reservedLen,
        agreed->reserved,
        agreed->reservedLen,
        device->reserved,
        device->reservedLen,
        desktop->reserved,
        desktop->reservedLen);

    /* Merge the two userConfigs. */
    mergeUserConfigs(result, agreed, device, desktop, preferDesktop);

    /* Delete the serverConfigs marked for deletion. */
    handleDeletes(result, device, desktop);

    return result;
}
#endif /* #ifndef REMOVE_SYNCHRONIZE_FEATURE */

/* ----------------------------------------------------------------------------
*/
int32 AGUserConfigCount(AGUserConfig * uc)
{
    if (NULL != uc)
        return AGArrayCount(uc->servers);
    else
        return 0;
}

/* ----------------------------------------------------------------------------
*/
static void finalizeServers(AGUserConfig * uc)
{
    int i, n;

    if (!uc->servers)
        return;
    
    /* Free the server list. */
    n = AGArrayCount(uc->servers);
    for (i = 0; i < n; ++i) {
        AGServerConfigFree((AGServerConfig*)
            AGArrayElementAt(uc->servers, i));
    }
    AGArrayRemoveAll(uc->servers);
}

/* ----------------------------------------------------------------------------
*/
AGUserConfig *AGUserConfigCopy(AGUserConfig *dst, AGUserConfig *src)
{
    int32 i, n;

    if (NULL == src || NULL == dst)
        return NULL;

    dst->dirty = src->dirty;
    dst->nextUID = src->nextUID;

    finalizeServers(dst);
    n = AGArrayCount(src->servers);
    for (i = 0; i < n; ++i)
        AGArrayAppend(dst->servers,
            AGServerConfigDup((LPAGSC)AGArrayElementAt(src->servers, i)));
    AGArrayRemoveAll(dst->uidDeletes);
    n = AGArrayCount(src->uidDeletes);
    for (i = 0; i < n; ++i)
        AGArrayAppend(dst->uidDeletes, AGArrayElementAt(src->uidDeletes, i));

    dst->expansion1 = src->expansion1;
    dst->expansion2 = src->expansion2;
    dst->expansion3 = src->expansion3;
    dst->expansion4 = src->expansion4;

    dst->reservedLen = src->reservedLen;
    CHECKANDFREE(dst->reserved);
    if (NULL != src->reserved) {
        dst->reserved = malloc(dst->reservedLen);
        memcpy(dst->reserved, src->reserved, dst->reservedLen);
    }

    return dst;
}

/* ----------------------------------------------------------------------------
*/
AGUserConfig *AGUserConfigDup(AGUserConfig *src)
{
    return AGUserConfigCopy(AGUserConfigNew(), src);
}

/* ----------------------------------------------------------------------------
*/
LPAGSC AGUserConfigGetServerByIndex(AGUserConfig * uc,
                                    int32 index)
{
    int32 n;
    n = AGArrayCount(uc->servers);
    return (index < 0 || index >= n)
        ? NULL
        : (LPAGSC)AGArrayElementAt(uc->servers, index);
}

/* ----------------------------------------------------------------------------
*/
void AGUserConfigInit(AGUserConfig * uc)
{
    bzero(uc, sizeof(AGUserConfig));
    uc->nextUID = 1;
    uc->dirty = TRUE;
    uc->servers = AGArrayNew(AGUnownedPointerElements, 0);
    uc->uidDeletes = AGArrayNew(AGIntegerElements, 0);
    uc->reservedLen = 0;
    uc->reserved = NULL;
}

/* ----------------------------------------------------------------------------
*/
AGUserConfig * AGUserConfigNew()
{
    AGUserConfig * result = (AGUserConfig *)malloc(sizeof(AGUserConfig));
    if (NULL != result)
        AGUserConfigInit(result);
    return result;
}

/* ----------------------------------------------------------------------------
*/
void AGUserConfigFinalize(AGUserConfig * uc)
{
    /* Free the server list. */
    finalizeServers(uc);
    if (uc->servers)
        AGArrayFree(uc->servers);

    /* Free the delete list. */
    AGArrayFree(uc->uidDeletes);

    /* Free the future stuff. */
    CHECKANDFREE(uc->reserved);

    /* Clear out everything. */
    bzero(uc, sizeof(AGUserConfig));
}

/* ----------------------------------------------------------------------------
*/
void AGUserConfigFree(AGUserConfig * uc)
{
    AGUserConfigFinalize(uc);
    free(uc);
}

#define agSIGNATURE_HIGH      (0xDE)
#define agSIGNATURE_LOW       (0xAA)
#define agVERSION_MAJ_0       (0)
#define agVERSION_MIN_0       (0)
#define agCURRENT_MAJ_VER     agVERSION_MAJ_0
#define agCURRENT_MIN_VER     agVERSION_MIN_0

/* Define future flag constants here. For example:

    #define agFLAG_SAY_HELLO (0x00000001)
*/

/* ----------------------------------------------------------------------------
*/
int32 AGUserConfigReadData(AGUserConfig * obj, AGReader *r)
{
    int32 flags;
    int32 i, n;
    int32 majver, minver;

    if (AGReadInt16(r) != ((agSIGNATURE_HIGH << 8) | agSIGNATURE_LOW))
        return AG_ERROR_INVALID_SIGNATURE;

    majver = AGReadCompactInt(r);
    minver = AGReadCompactInt(r);

    /* Read UID. */
    obj->nextUID = AGReadCompactInt(r);

    /*  Read bit flags. Note that we don't currently do anything
        with these flags.*/
    flags = AGReadCompactInt(r);

    /* Read uid deletes. */
    AGArrayRemoveAll(obj->uidDeletes);
    n = AGReadCompactInt(r);
    for (i = 0; i < n; ++i)
        AGArrayAppend(obj->uidDeletes, (void*)AGReadCompactInt(r));

    /* Read servers. */
    finalizeServers(obj);
    n = AGReadCompactInt(r);
    for (i = 0; i < n; ++i) {
        AGServerConfig * sc;
        sc = AGServerConfigNew();
        if (NULL == sc)
            return AG_ERROR_OUT_OF_MEMORY;
        AGServerConfigReadData(sc, r);
        AGArrayAppend(obj->servers, sc);
    }

    obj->dirty = FALSE;

    obj->expansion1 = AGReadCompactInt(r);
    obj->expansion2 = AGReadCompactInt(r);
    obj->expansion3 = AGReadCompactInt(r);
    obj->expansion4 = AGReadCompactInt(r);

    obj->reservedLen = AGReadCompactInt(r);
    CHECKANDFREE(obj->reserved);
    if (obj->reservedLen > 0) {
        obj->reserved = malloc(obj->reservedLen);
        AGReadBytes(r, obj->reserved, obj->reservedLen);
    }

    if (majver > agCURRENT_MAJ_VER)
        return AG_ERROR_UNKNOWN_VERSION;

    return AG_ERROR_NONE;
}

void AGUserConfigWriteData(AGUserConfig * obj, AGWriter *w)
{
    int32 flags;
    int32 i, n;

    AGWriteInt16(w, (agSIGNATURE_HIGH << 8) | agSIGNATURE_LOW);
    AGWriteCompactInt(w, agCURRENT_MAJ_VER);
    AGWriteCompactInt(w, agCURRENT_MIN_VER);
    AGWriteCompactInt(w, obj->nextUID);

    /*  Future use: set bit flags here.
    */
    flags = 0;
    AGWriteCompactInt(w, flags);
    n = AGArrayCount(obj->uidDeletes);
    AGWriteCompactInt(w, n);
    for (i = 0; i < n; ++i) {
        AGWriteCompactInt(w,
            (uint32)AGArrayElementAt(obj->uidDeletes, i));
    }
    n = AGArrayCount(obj->servers);
    AGWriteCompactInt(w, n);
    for (i = 0; i < n; ++i) {
        AGServerConfigWriteData(
            (AGServerConfig*)AGArrayElementAt(obj->servers, i), w);
    }
    obj->dirty = FALSE;

    AGWriteCompactInt(w, obj->expansion1);
    AGWriteCompactInt(w, obj->expansion2);
    AGWriteCompactInt(w, obj->expansion3);
    AGWriteCompactInt(w, obj->expansion4);

    AGWriteCompactInt(w, obj->reservedLen);
    if (obj->reservedLen > 0)
        AGWriteBytes(w, obj->reserved, obj->reservedLen);
}