~ubuntuone-control-tower/bindwood/trunk

« back to all changes in this revision

Viewing changes to modules/sync.jsm

Add code to the new synchroniser for exporting local bookmark items to CouchDB.

Show diffs side-by-side

added added

removed removed

Lines of Context:
243
243
            this.folders_to_reorder[doc._id] = doc.children;
244
244
            break;
245
245
        case TYPE_FEED:
246
 
            var site_uri = ioService.newURI(doc.site_uri, null, null);
 
246
            var site_uri = doc.site_uri ?
 
247
                ioService.newURI(doc.site_uri, null, null) : null;
247
248
            var feed_uri = ioService.newURI(doc.feed_uri, null, null);
248
249
            item_id = livemarkService.createLivemark(
249
250
                parent_id, doc.title, site_uri, feed_uri,
294
295
            this.folders_to_reorder[doc._id] = doc.children;
295
296
            break;
296
297
        case TYPE_FEED:
297
 
            var site_uri = ioService.newURI(doc.site_uri, null, null);
 
298
            var site_uri = doc.site_uri ?
 
299
                ioService.newURI(doc.site_uri, null, null) : null;
298
300
            var feed_uri = ioService.newURI(doc.feed_uri, null, null);
299
301
            bookmarksService.setItemTitle(item_id, doc.title);
300
302
            livemarkService.setSiteURI(item_id, site_uri);
374
376
        }
375
377
        this.folders_to_reorder = {}
376
378
    },
 
379
 
 
380
    exportItem: function(item_guid) {
 
381
        var item_id = this.guid_to_id(item_guid);
 
382
        var item_type = null, deleted = false;
 
383
        if (item_id) {
 
384
            // Get the item type, which also checks whether the item exists.
 
385
            try {
 
386
                item_type = bookmarksService.getItemType(item_id);
 
387
            } catch (e) {
 
388
                deleted = true;
 
389
            }
 
390
        } else {
 
391
            deleted = true;
 
392
        }
 
393
 
 
394
        var doc = this.couch.open(item_guid);
 
395
        if (deleted) {
 
396
            if (doc != null) {
 
397
                this.couch.deleteDoc(doc);
 
398
            }
 
399
            delete this.guid_item_map[item_guid];
 
400
            return;
 
401
        }
 
402
 
 
403
        var changed = false;
 
404
        if (doc == null) {
 
405
            doc = { _id: item_guid };
 
406
            changed = true;
 
407
        }
 
408
 
 
409
        var _setattr = function(doc, attr, value) {
 
410
            if (attr == 'children') {
 
411
                // Special handling, since 'children' is an array
 
412
                if (!doc[attr] || value.join('\n') != doc[attr].join('\n')) {
 
413
                    doc[attr] = value;
 
414
                    changed = true;
 
415
                }
 
416
            } else if (doc[attr] != value) {
 
417
                doc[attr] = value;
 
418
                changed = true;
 
419
            }
 
420
        }
 
421
 
 
422
        if (!doc.application_annotations)
 
423
            doc.application_annotations = {};
 
424
        if (!doc.application_annotations.Firefox)
 
425
            doc.application_annotations.Firefox = {};
 
426
        _setattr(doc.application_annotations.Firefox, "profile", this.profile);
 
427
 
 
428
        var parent_id = bookmarksService.getFolderIdForItem(item_id);
 
429
        if (parent_id > 0) {
 
430
            var parent_guid = this.guid_from_id(parent_id);
 
431
            _setattr(doc, "parent_guid", parent_guid);
 
432
        }
 
433
        switch (item_type) {
 
434
        case bookmarksService.TYPE_BOOKMARK:
 
435
            _setattr(doc, "record_type", TYPE_BOOKMARK);
 
436
            _setattr(doc, "uri", bookmarksService.getBookmarkURI(item_id).spec);
 
437
            _setattr(doc, "title", bookmarksService.getItemTitle(item_id));
 
438
            break;
 
439
        case bookmarksService.TYPE_FOLDER:
 
440
            _setattr(doc, "title", bookmarksService.getItemTitle(item_id));
 
441
            if (livemarkService.isLivemark(item_id)) {
 
442
                _setattr(doc, "record_type", TYPE_FEED);
 
443
                var site_uri = livemarkService.getSiteURI(item_id);
 
444
                if (site_uri)
 
445
                    _setattr(doc, "site_uri", site_uri.spec);
 
446
                _setattr(
 
447
                    doc, "feed_uri", livemarkService.getFeedURI(item_id).spec);
 
448
            } else {
 
449
                _setattr(doc, "record_type", TYPE_FOLDER);
 
450
                var children = [
 
451
                    this.guid_from_id(child_id)
 
452
                    for each (child_id in this.get_folder_children(item_id))];
 
453
                _setattr(doc, "children", children);
 
454
            }
 
455
            break;
 
456
        case bookmarksService.TYPE_SEPARATOR:
 
457
            _setattr(doc, "record_type", TYPE_SEPARATOR);
 
458
            break;
 
459
        default:
 
460
            dump("Can not handle item " + item_id + " of type " +
 
461
                 item_type + "\n");
 
462
            return;
 
463
        }
 
464
        if (changed) {
 
465
            this.couch.save(doc);
 
466
        }
 
467
    },
377
468
};
378
469
 
379
470