2008-06-07 06:28:01 +04:00
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (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 Bookmarks Sync.
|
|
|
|
*
|
2010-12-15 02:48:05 +03:00
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* the Mozilla Foundation.
|
2008-06-07 06:28:01 +04:00
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2007
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
2010-12-15 02:48:05 +03:00
|
|
|
* Dan Mills <thunder@mozilla.com>
|
|
|
|
* Jono DiCarlo <jdicarlo@mozilla.org>
|
|
|
|
* Anant Narayanan <anant@kix.in>
|
|
|
|
* Philipp von Weitershausen <philipp@weitershausen.de>
|
2011-02-01 10:22:13 +03:00
|
|
|
* Richard Newman <rnewman@mozilla.com>
|
2008-06-07 06:28:01 +04:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
2011-01-19 03:23:20 +03:00
|
|
|
const EXPORTED_SYMBOLS = ['BookmarksEngine', "PlacesItem", "Bookmark",
|
|
|
|
"BookmarkFolder", "BookmarkMicsum", "BookmarkQuery",
|
|
|
|
"Livemark", "BookmarkSeparator"];
|
2008-06-03 23:38:48 +04:00
|
|
|
|
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
const Cu = Components.utils;
|
|
|
|
|
2010-12-07 02:51:38 +03:00
|
|
|
const GUID_ANNO = "sync/guid";
|
2011-01-24 21:45:27 +03:00
|
|
|
const MOBILE_ANNO = "mobile/bookmarksRoot";
|
2010-12-07 02:51:48 +03:00
|
|
|
const PARENT_ANNO = "sync/parent";
|
2008-12-12 01:26:20 +03:00
|
|
|
const SERVICE_NOT_SUPPORTED = "Service not supported on this platform";
|
2011-01-26 23:36:38 +03:00
|
|
|
const SMART_BOOKMARKS_ANNO = "Places/SmartBookmark";
|
2010-11-10 00:53:50 +03:00
|
|
|
const FOLDER_SORTINDEX = 1000000;
|
2008-12-12 01:26:20 +03:00
|
|
|
|
2010-05-04 23:14:48 +04:00
|
|
|
try {
|
|
|
|
Cu.import("resource://gre/modules/PlacesUtils.jsm");
|
|
|
|
}
|
|
|
|
catch(ex) {
|
|
|
|
Cu.import("resource://gre/modules/utils.js");
|
|
|
|
}
|
2008-06-29 22:44:27 +04:00
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
2010-06-17 01:30:08 +04:00
|
|
|
Cu.import("resource://services-sync/engines.js");
|
2011-01-19 03:23:30 +03:00
|
|
|
Cu.import("resource://services-sync/record.js");
|
2010-06-17 01:30:08 +04:00
|
|
|
Cu.import("resource://services-sync/util.js");
|
2008-06-03 23:38:48 +04:00
|
|
|
|
2011-01-25 10:06:42 +03:00
|
|
|
Cu.import("resource://services-sync/main.js"); // For access to Service.
|
2011-01-19 03:23:20 +03:00
|
|
|
|
|
|
|
function PlacesItem(collection, id, type) {
|
|
|
|
CryptoWrapper.call(this, collection, id);
|
|
|
|
this.type = type || "item";
|
|
|
|
}
|
|
|
|
PlacesItem.prototype = {
|
|
|
|
decrypt: function PlacesItem_decrypt() {
|
|
|
|
// Do the normal CryptoWrapper decrypt, but change types before returning
|
|
|
|
let clear = CryptoWrapper.prototype.decrypt.apply(this, arguments);
|
|
|
|
|
|
|
|
// Convert the abstract places item to the actual object type
|
|
|
|
if (!this.deleted)
|
|
|
|
this.__proto__ = this.getTypeObject(this.type).prototype;
|
|
|
|
|
|
|
|
return clear;
|
|
|
|
},
|
|
|
|
|
|
|
|
getTypeObject: function PlacesItem_getTypeObject(type) {
|
|
|
|
switch (type) {
|
|
|
|
case "bookmark":
|
|
|
|
return Bookmark;
|
|
|
|
case "microsummary":
|
|
|
|
return BookmarkMicsum;
|
|
|
|
case "query":
|
|
|
|
return BookmarkQuery;
|
|
|
|
case "folder":
|
|
|
|
return BookmarkFolder;
|
|
|
|
case "livemark":
|
|
|
|
return Livemark;
|
|
|
|
case "separator":
|
|
|
|
return BookmarkSeparator;
|
|
|
|
case "item":
|
|
|
|
return PlacesItem;
|
|
|
|
}
|
|
|
|
throw "Unknown places item object type: " + type;
|
|
|
|
},
|
|
|
|
|
|
|
|
__proto__: CryptoWrapper.prototype,
|
|
|
|
_logName: "Record.PlacesItem",
|
|
|
|
};
|
|
|
|
|
|
|
|
Utils.deferGetSet(PlacesItem, "cleartext", ["hasDupe", "parentid", "parentName",
|
|
|
|
"type"]);
|
|
|
|
|
|
|
|
function Bookmark(collection, id, type) {
|
|
|
|
PlacesItem.call(this, collection, id, type || "bookmark");
|
|
|
|
}
|
|
|
|
Bookmark.prototype = {
|
|
|
|
__proto__: PlacesItem.prototype,
|
|
|
|
_logName: "Record.Bookmark",
|
|
|
|
};
|
|
|
|
|
|
|
|
Utils.deferGetSet(Bookmark, "cleartext", ["title", "bmkUri", "description",
|
|
|
|
"loadInSidebar", "tags", "keyword"]);
|
|
|
|
|
|
|
|
function BookmarkMicsum(collection, id) {
|
|
|
|
Bookmark.call(this, collection, id, "microsummary");
|
|
|
|
}
|
|
|
|
BookmarkMicsum.prototype = {
|
|
|
|
__proto__: Bookmark.prototype,
|
|
|
|
_logName: "Record.BookmarkMicsum",
|
|
|
|
};
|
|
|
|
|
|
|
|
Utils.deferGetSet(BookmarkMicsum, "cleartext", ["generatorUri", "staticTitle"]);
|
|
|
|
|
|
|
|
function BookmarkQuery(collection, id) {
|
|
|
|
Bookmark.call(this, collection, id, "query");
|
|
|
|
}
|
|
|
|
BookmarkQuery.prototype = {
|
|
|
|
__proto__: Bookmark.prototype,
|
|
|
|
_logName: "Record.BookmarkQuery",
|
|
|
|
};
|
|
|
|
|
2011-01-26 23:36:38 +03:00
|
|
|
Utils.deferGetSet(BookmarkQuery, "cleartext", ["folderName",
|
|
|
|
"queryId"]);
|
2011-01-19 03:23:20 +03:00
|
|
|
|
|
|
|
function BookmarkFolder(collection, id, type) {
|
|
|
|
PlacesItem.call(this, collection, id, type || "folder");
|
|
|
|
}
|
|
|
|
BookmarkFolder.prototype = {
|
|
|
|
__proto__: PlacesItem.prototype,
|
|
|
|
_logName: "Record.Folder",
|
|
|
|
};
|
|
|
|
|
|
|
|
Utils.deferGetSet(BookmarkFolder, "cleartext", ["description", "title",
|
|
|
|
"children"]);
|
|
|
|
|
|
|
|
function Livemark(collection, id) {
|
|
|
|
BookmarkFolder.call(this, collection, id, "livemark");
|
|
|
|
}
|
|
|
|
Livemark.prototype = {
|
|
|
|
__proto__: BookmarkFolder.prototype,
|
|
|
|
_logName: "Record.Livemark",
|
|
|
|
};
|
|
|
|
|
|
|
|
Utils.deferGetSet(Livemark, "cleartext", ["siteUri", "feedUri"]);
|
|
|
|
|
|
|
|
function BookmarkSeparator(collection, id) {
|
|
|
|
PlacesItem.call(this, collection, id, "separator");
|
|
|
|
}
|
|
|
|
BookmarkSeparator.prototype = {
|
|
|
|
__proto__: PlacesItem.prototype,
|
|
|
|
_logName: "Record.Separator",
|
|
|
|
};
|
|
|
|
|
|
|
|
Utils.deferGetSet(BookmarkSeparator, "cleartext", "pos");
|
|
|
|
|
|
|
|
|
2010-04-21 22:10:32 +04:00
|
|
|
function archiveBookmarks() {
|
|
|
|
// Some nightly builds of 3.7 don't have this function
|
|
|
|
try {
|
|
|
|
PlacesUtils.archiveBookmarksFile(null, true);
|
|
|
|
}
|
|
|
|
catch(ex) {}
|
|
|
|
}
|
|
|
|
|
2011-01-24 21:45:27 +03:00
|
|
|
let kSpecialIds = {
|
|
|
|
|
|
|
|
// Special IDs. Note that mobile can attempt to create a record on
|
|
|
|
// dereference; special accessors are provided to prevent recursion within
|
|
|
|
// observers.
|
|
|
|
get guids()
|
|
|
|
["menu", "places", "tags", "toolbar", "unfiled", "mobile"],
|
|
|
|
|
|
|
|
// Create the special mobile folder to store mobile bookmarks.
|
|
|
|
createMobileRoot: function createMobileRoot() {
|
|
|
|
let root = Svc.Bookmark.placesRoot;
|
|
|
|
let mRoot = Svc.Bookmark.createFolder(root, "mobile", -1);
|
|
|
|
Utils.anno(mRoot, MOBILE_ANNO, 1);
|
|
|
|
return mRoot;
|
|
|
|
},
|
|
|
|
|
|
|
|
findMobileRoot: function findMobileRoot(create) {
|
|
|
|
// Use the (one) mobile root if it already exists.
|
|
|
|
let root = Svc.Annos.getItemsWithAnnotation(MOBILE_ANNO, {});
|
|
|
|
if (root.length != 0)
|
|
|
|
return root[0];
|
|
|
|
|
|
|
|
if (create)
|
|
|
|
return this.createMobileRoot();
|
|
|
|
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
|
|
|
|
// Accessors for IDs.
|
|
|
|
isSpecialGUID: function isSpecialGUID(g) {
|
|
|
|
return this.guids.indexOf(g) != -1;
|
|
|
|
},
|
|
|
|
|
|
|
|
specialIdForGUID: function specialIdForGUID(guid, create) {
|
|
|
|
if (guid == "mobile") {
|
|
|
|
return this.findMobileRoot(create);
|
|
|
|
}
|
|
|
|
return this[guid];
|
|
|
|
},
|
|
|
|
|
|
|
|
// Don't bother creating mobile: if it doesn't exist, this ID can't be it!
|
|
|
|
specialGUIDForId: function specialGUIDForId(id) {
|
|
|
|
for each (let guid in this.guids)
|
|
|
|
if (this.specialIdForGUID(guid, false) == id)
|
|
|
|
return guid;
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
|
|
|
|
get menu() Svc.Bookmark.bookmarksMenuFolder,
|
|
|
|
get places() Svc.Bookmark.placesRoot,
|
|
|
|
get tags() Svc.Bookmark.tagsFolder,
|
|
|
|
get toolbar() Svc.Bookmark.toolbarFolder,
|
|
|
|
get unfiled() Svc.Bookmark.unfiledBookmarksFolder,
|
|
|
|
get mobile() this.findMobileRoot(true),
|
|
|
|
};
|
2009-06-17 20:28:15 +04:00
|
|
|
|
2008-12-09 23:26:14 +03:00
|
|
|
function BookmarksEngine() {
|
2010-02-12 02:29:15 +03:00
|
|
|
SyncEngine.call(this, "Bookmarks");
|
2008-06-25 08:15:14 +04:00
|
|
|
}
|
|
|
|
BookmarksEngine.prototype = {
|
2008-12-05 11:39:54 +03:00
|
|
|
__proto__: SyncEngine.prototype,
|
2009-04-13 23:54:31 +04:00
|
|
|
_recordObj: PlacesItem,
|
2009-01-07 00:54:18 +03:00
|
|
|
_storeObj: BookmarksStore,
|
2009-07-30 22:52:26 +04:00
|
|
|
_trackerObj: BookmarksTracker,
|
2010-12-07 02:51:38 +03:00
|
|
|
version: 2,
|
2009-07-30 22:52:26 +04:00
|
|
|
|
2009-09-01 05:04:40 +04:00
|
|
|
_sync: Utils.batchSync("Bookmark", SyncEngine),
|
2009-09-01 04:27:30 +04:00
|
|
|
|
2009-09-01 04:54:21 +04:00
|
|
|
_syncStartup: function _syncStart() {
|
|
|
|
SyncEngine.prototype._syncStartup.call(this);
|
|
|
|
|
2010-04-21 22:10:32 +04:00
|
|
|
// For first-syncs, make a backup for the user to restore
|
|
|
|
if (this.lastSync == 0)
|
|
|
|
archiveBookmarks();
|
|
|
|
|
2009-09-09 10:33:58 +04:00
|
|
|
// Lazily create a mapping of folder titles and separator positions to GUID
|
2009-11-20 08:34:17 +03:00
|
|
|
this.__defineGetter__("_lazyMap", function() {
|
|
|
|
delete this._lazyMap;
|
2009-09-01 04:54:21 +04:00
|
|
|
|
2009-11-20 08:34:17 +03:00
|
|
|
let lazyMap = {};
|
2009-09-01 04:54:21 +04:00
|
|
|
for (let guid in this._store.getAllIDs()) {
|
2009-11-20 08:34:17 +03:00
|
|
|
// Figure out what key to store the mapping
|
|
|
|
let key;
|
2010-12-07 02:51:38 +03:00
|
|
|
let id = this._store.idForGUID(guid);
|
2009-09-09 10:33:58 +04:00
|
|
|
switch (Svc.Bookmark.getItemType(id)) {
|
2010-01-25 20:33:49 +03:00
|
|
|
case Svc.Bookmark.TYPE_BOOKMARK:
|
2011-01-28 11:14:08 +03:00
|
|
|
|
|
|
|
// Smart bookmarks map to their annotation value.
|
|
|
|
let queryId;
|
|
|
|
try {
|
|
|
|
queryId = Utils.anno(id, SMART_BOOKMARKS_ANNO);
|
|
|
|
} catch(ex) {}
|
|
|
|
|
|
|
|
if (queryId)
|
|
|
|
key = "q" + queryId;
|
|
|
|
else
|
|
|
|
key = "b" + Svc.Bookmark.getBookmarkURI(id).spec + ":" +
|
|
|
|
Svc.Bookmark.getItemTitle(id);
|
2010-01-25 20:33:49 +03:00
|
|
|
break;
|
2009-09-09 10:33:58 +04:00
|
|
|
case Svc.Bookmark.TYPE_FOLDER:
|
2009-11-20 08:34:17 +03:00
|
|
|
key = "f" + Svc.Bookmark.getItemTitle(id);
|
2009-09-09 10:33:58 +04:00
|
|
|
break;
|
|
|
|
case Svc.Bookmark.TYPE_SEPARATOR:
|
2009-11-20 08:34:17 +03:00
|
|
|
key = "s" + Svc.Bookmark.getItemIndex(id);
|
2009-09-09 10:33:58 +04:00
|
|
|
break;
|
2009-11-20 08:34:17 +03:00
|
|
|
default:
|
|
|
|
continue;
|
2009-09-09 10:33:58 +04:00
|
|
|
}
|
2009-11-20 08:34:17 +03:00
|
|
|
|
|
|
|
// The mapping is on a per parent-folder-name basis
|
|
|
|
let parent = Svc.Bookmark.getFolderIdForItem(id);
|
2010-12-07 02:51:56 +03:00
|
|
|
if (parent <= 0)
|
|
|
|
continue;
|
|
|
|
|
2009-11-20 08:34:17 +03:00
|
|
|
let parentName = Svc.Bookmark.getItemTitle(parent);
|
|
|
|
if (lazyMap[parentName] == null)
|
|
|
|
lazyMap[parentName] = {};
|
|
|
|
|
2010-04-30 01:36:09 +04:00
|
|
|
// If the entry already exists, remember that there are explicit dupes
|
|
|
|
let entry = new String(guid);
|
|
|
|
entry.hasDupe = lazyMap[parentName][key] != null;
|
|
|
|
|
2009-11-20 08:34:17 +03:00
|
|
|
// Remember this item's guid for its parent-name/key pair
|
2010-04-30 01:36:09 +04:00
|
|
|
lazyMap[parentName][key] = entry;
|
|
|
|
this._log.trace("Mapped: " + [parentName, key, entry, entry.hasDupe]);
|
2009-09-01 04:54:21 +04:00
|
|
|
}
|
2009-09-09 10:33:58 +04:00
|
|
|
|
2009-11-20 08:34:17 +03:00
|
|
|
// Expose a helper function to get a dupe guid for an item
|
|
|
|
return this._lazyMap = function(item) {
|
|
|
|
// Figure out if we have something to key with
|
|
|
|
let key;
|
2011-01-28 11:14:08 +03:00
|
|
|
let altKey;
|
2009-11-20 08:34:17 +03:00
|
|
|
switch (item.type) {
|
2010-01-25 20:33:49 +03:00
|
|
|
case "query":
|
2011-01-28 11:14:08 +03:00
|
|
|
// Prior to Bug 610501, records didn't carry their Smart Bookmark
|
|
|
|
// anno, so we won't be able to dupe them correctly. This altKey
|
|
|
|
// hack should get them to dupe correctly.
|
|
|
|
if (item.queryId) {
|
|
|
|
key = "q" + item.queryId;
|
|
|
|
altKey = "b" + item.bmkUri + ":" + item.title;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// No queryID? Fall through to the regular bookmark case.
|
|
|
|
case "bookmark":
|
2010-01-25 20:33:49 +03:00
|
|
|
case "microsummary":
|
|
|
|
key = "b" + item.bmkUri + ":" + item.title;
|
|
|
|
break;
|
2009-11-20 08:34:17 +03:00
|
|
|
case "folder":
|
|
|
|
case "livemark":
|
|
|
|
key = "f" + item.title;
|
|
|
|
break;
|
|
|
|
case "separator":
|
|
|
|
key = "s" + item.pos;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
2009-09-09 10:33:58 +04:00
|
|
|
|
2009-11-20 08:34:17 +03:00
|
|
|
// Give the guid if we have the matching pair
|
2010-01-25 20:33:49 +03:00
|
|
|
this._log.trace("Finding mapping: " + item.parentName + ", " + key);
|
2009-11-20 08:34:17 +03:00
|
|
|
let parent = lazyMap[item.parentName];
|
2011-01-28 11:14:08 +03:00
|
|
|
|
|
|
|
if (!parent) {
|
|
|
|
this._log.trace("No parent => no dupe.");
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
let dupe = parent[key];
|
|
|
|
|
|
|
|
if (dupe) {
|
|
|
|
this._log.trace("Mapped dupe: " + dupe);
|
|
|
|
return dupe;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (altKey) {
|
|
|
|
dupe = parent[altKey];
|
|
|
|
if (dupe) {
|
|
|
|
this._log.trace("Mapped dupe using altKey " + altKey + ": " + dupe);
|
|
|
|
return dupe;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this._log.trace("No dupe found for key " + key + "/" + altKey + ".");
|
|
|
|
return undefined;
|
2009-11-20 08:34:17 +03:00
|
|
|
};
|
|
|
|
});
|
2010-12-07 02:51:56 +03:00
|
|
|
|
|
|
|
this._store._childrenToOrder = {};
|
|
|
|
},
|
|
|
|
|
|
|
|
_processIncoming: function _processIncoming() {
|
2011-01-05 02:04:18 +03:00
|
|
|
try {
|
|
|
|
SyncEngine.prototype._processIncoming.call(this);
|
|
|
|
} finally {
|
|
|
|
// Reorder children.
|
|
|
|
this._tracker.ignoreAll = true;
|
|
|
|
this._store._orderChildren();
|
|
|
|
this._tracker.ignoreAll = false;
|
|
|
|
delete this._store._childrenToOrder;
|
|
|
|
}
|
2009-09-01 04:54:21 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
_syncFinish: function _syncFinish() {
|
|
|
|
SyncEngine.prototype._syncFinish.call(this);
|
2009-11-20 08:34:17 +03:00
|
|
|
delete this._lazyMap;
|
2009-12-16 01:21:13 +03:00
|
|
|
this._tracker._ensureMobileQuery();
|
2009-09-01 04:54:21 +04:00
|
|
|
},
|
|
|
|
|
2010-04-30 01:36:09 +04:00
|
|
|
_createRecord: function _createRecord(id) {
|
|
|
|
// Create the record like normal but mark it as having dupes if necessary
|
|
|
|
let record = SyncEngine.prototype._createRecord.call(this, id);
|
|
|
|
let entry = this._lazyMap(record);
|
|
|
|
if (entry != null && entry.hasDupe)
|
|
|
|
record.hasDupe = true;
|
|
|
|
return record;
|
|
|
|
},
|
|
|
|
|
2009-09-01 04:27:30 +04:00
|
|
|
_findDupe: function _findDupe(item) {
|
2010-04-30 01:36:09 +04:00
|
|
|
// Don't bother finding a dupe if the incoming item has duplicates
|
|
|
|
if (item.hasDupe)
|
|
|
|
return;
|
2010-01-25 20:33:49 +03:00
|
|
|
return this._lazyMap(item);
|
2009-09-09 10:33:15 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
_handleDupe: function _handleDupe(item, dupeId) {
|
2010-12-08 08:49:22 +03:00
|
|
|
// Always change the local GUID to the incoming one.
|
2009-09-09 10:33:15 +04:00
|
|
|
this._store.changeItemID(dupeId, item.id);
|
|
|
|
this._deleteId(dupeId);
|
2010-04-02 02:54:53 +04:00
|
|
|
this._tracker.addChangedID(item.id, 0);
|
2010-12-08 08:49:22 +03:00
|
|
|
if (item.parentid) {
|
|
|
|
this._tracker.addChangedID(item.parentid, 0);
|
|
|
|
}
|
2009-07-30 22:52:26 +04:00
|
|
|
}
|
2008-06-03 23:38:48 +04:00
|
|
|
};
|
|
|
|
|
2010-02-12 02:29:15 +03:00
|
|
|
function BookmarksStore(name) {
|
|
|
|
Store.call(this, name);
|
2010-08-04 16:50:44 +04:00
|
|
|
|
|
|
|
// Explicitly nullify our references to our cached services so we don't leak
|
2010-08-06 19:30:58 +04:00
|
|
|
Svc.Obs.add("places-shutdown", function() {
|
2010-08-04 16:50:44 +04:00
|
|
|
this.__bms = null;
|
|
|
|
this.__hsvc = null;
|
|
|
|
this.__ls = null;
|
|
|
|
this.__ms = null;
|
|
|
|
this.__ts = null;
|
2010-12-07 02:51:38 +03:00
|
|
|
for each ([query, stmt] in Iterator(this._stmts))
|
|
|
|
stmt.finalize();
|
2010-08-04 16:50:44 +04:00
|
|
|
}, this);
|
2008-06-03 23:38:48 +04:00
|
|
|
}
|
|
|
|
BookmarksStore.prototype = {
|
2008-11-07 06:18:46 +03:00
|
|
|
__proto__: Store.prototype,
|
2008-06-03 23:38:48 +04:00
|
|
|
|
|
|
|
__bms: null,
|
|
|
|
get _bms() {
|
|
|
|
if (!this.__bms)
|
|
|
|
this.__bms = Cc["@mozilla.org/browser/nav-bookmarks-service;1"].
|
|
|
|
getService(Ci.nsINavBookmarksService);
|
|
|
|
return this.__bms;
|
|
|
|
},
|
|
|
|
|
|
|
|
__hsvc: null,
|
|
|
|
get _hsvc() {
|
|
|
|
if (!this.__hsvc)
|
|
|
|
this.__hsvc = Cc["@mozilla.org/browser/nav-history-service;1"].
|
2010-12-07 02:51:38 +03:00
|
|
|
getService(Ci.nsINavHistoryService).
|
|
|
|
QueryInterface(Ci.nsPIPlacesDatabase);
|
2008-06-03 23:38:48 +04:00
|
|
|
return this.__hsvc;
|
|
|
|
},
|
|
|
|
|
|
|
|
__ls: null,
|
|
|
|
get _ls() {
|
|
|
|
if (!this.__ls)
|
|
|
|
this.__ls = Cc["@mozilla.org/browser/livemark-service;2"].
|
2010-08-04 16:50:44 +04:00
|
|
|
getService(Ci.nsILivemarkService);
|
2008-06-03 23:38:48 +04:00
|
|
|
return this.__ls;
|
|
|
|
},
|
|
|
|
|
2010-08-04 16:50:44 +04:00
|
|
|
__ms: null,
|
2008-06-03 23:38:48 +04:00
|
|
|
get _ms() {
|
2010-08-04 16:50:44 +04:00
|
|
|
if (!this.__ms) {
|
|
|
|
try {
|
|
|
|
this.__ms = Cc["@mozilla.org/microsummary/service;1"].
|
|
|
|
getService(Ci.nsIMicrosummaryService);
|
|
|
|
} catch (e) {
|
|
|
|
this._log.warn("Could not load microsummary service");
|
|
|
|
this._log.debug(e);
|
|
|
|
// Redefine our getter so we won't keep trying to get the service
|
|
|
|
this.__defineGetter__("_ms", function() null);
|
|
|
|
}
|
2008-12-29 06:59:44 +03:00
|
|
|
}
|
2010-08-04 16:50:44 +04:00
|
|
|
return this.__ms;
|
2008-06-03 23:38:48 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
__ts: null,
|
|
|
|
get _ts() {
|
|
|
|
if (!this.__ts)
|
|
|
|
this.__ts = Cc["@mozilla.org/browser/tagging-service;1"].
|
|
|
|
getService(Ci.nsITaggingService);
|
|
|
|
return this.__ts;
|
|
|
|
},
|
|
|
|
|
2008-12-29 06:59:44 +03:00
|
|
|
|
2008-12-30 10:28:17 +03:00
|
|
|
itemExists: function BStore_itemExists(id) {
|
2011-01-24 21:45:27 +03:00
|
|
|
return this.idForGUID(id, true) > 0;
|
2008-11-20 03:21:12 +03:00
|
|
|
},
|
2011-01-28 11:18:36 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If the record is a tag query, rewrite it to refer to the local tag ID.
|
|
|
|
*
|
|
|
|
* Otherwise, just return.
|
|
|
|
*/
|
|
|
|
preprocessTagQuery: function preprocessTagQuery(record) {
|
|
|
|
if (record.type != "query" ||
|
|
|
|
record.bmkUri == null ||
|
|
|
|
record.folderName == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Yes, this works without chopping off the "place:" prefix.
|
|
|
|
let uri = record.bmkUri
|
|
|
|
let queriesRef = {};
|
|
|
|
let queryCountRef = {};
|
|
|
|
let optionsRef = {};
|
|
|
|
Svc.History.queryStringToQueries(uri, queriesRef, queryCountRef, optionsRef);
|
|
|
|
|
|
|
|
// We only process tag URIs.
|
|
|
|
if (optionsRef.value.resultType != optionsRef.value.RESULTS_AS_TAG_CONTENTS)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Tag something to ensure that the tag exists.
|
|
|
|
let tag = record.folderName;
|
|
|
|
let dummyURI = Utils.makeURI("about:weave#BStore_preprocess");
|
|
|
|
this._ts.tagURI(dummyURI, [tag]);
|
|
|
|
|
|
|
|
// Look for the id of the tag, which might just have been added.
|
|
|
|
let tags = this._getNode(this._bms.tagsFolder);
|
|
|
|
if (!(tags instanceof Ci.nsINavHistoryQueryResultNode)) {
|
|
|
|
this._log.debug("tags isn't an nsINavHistoryQueryResultNode; aborting.");
|
|
|
|
return;
|
|
|
|
}
|
2008-11-20 03:21:12 +03:00
|
|
|
|
2011-01-28 11:18:36 +03:00
|
|
|
tags.containerOpen = true;
|
|
|
|
for (let i = 0; i < tags.childCount; i++) {
|
|
|
|
let child = tags.getChild(i);
|
|
|
|
if (child.title == tag) {
|
|
|
|
// Found the tag, so fix up the query to use the right id.
|
|
|
|
this._log.debug("Tag query folder: " + tag + " = " + child.itemId);
|
|
|
|
|
|
|
|
this._log.trace("Replacing folders in: " + uri);
|
|
|
|
for each (let q in queriesRef.value)
|
|
|
|
q.setFolders([child.itemId], 1);
|
|
|
|
|
|
|
|
record.bmkUri = Svc.History.queriesToQueryString(queriesRef.value,
|
|
|
|
queryCountRef.value,
|
|
|
|
optionsRef.value);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2009-08-15 12:00:32 +04:00
|
|
|
applyIncoming: function BStore_applyIncoming(record) {
|
2011-01-15 00:41:09 +03:00
|
|
|
// Don't bother with pre and post-processing for deletions.
|
|
|
|
if (record.deleted) {
|
|
|
|
Store.prototype.applyIncoming.apply(this, arguments);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-12-07 02:51:56 +03:00
|
|
|
// For special folders we're only interested in child ordering.
|
|
|
|
if ((record.id in kSpecialIds) && record.children) {
|
|
|
|
this._log.debug("Processing special node: " + record.id);
|
|
|
|
// Reorder children later
|
|
|
|
this._childrenToOrder[record.id] = record.children;
|
2009-08-15 12:00:32 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-01-28 11:18:36 +03:00
|
|
|
// Preprocess the record before doing the normal apply.
|
|
|
|
this.preprocessTagQuery(record);
|
2009-08-15 12:00:32 +04:00
|
|
|
|
2009-08-15 12:00:46 +04:00
|
|
|
// Figure out the local id of the parent GUID if available
|
|
|
|
let parentGUID = record.parentid;
|
2011-01-15 00:41:09 +03:00
|
|
|
if (!parentGUID) {
|
|
|
|
throw "Record " + record.id + " has invalid parentid: " + parentGUID;
|
|
|
|
}
|
2009-08-15 12:00:46 +04:00
|
|
|
|
2011-01-15 00:41:09 +03:00
|
|
|
let parentId = this.idForGUID(parentGUID);
|
|
|
|
if (parentId > 0) {
|
2009-08-15 12:00:46 +04:00
|
|
|
// Save the parent id for modifying the bookmark later
|
|
|
|
record._parent = parentId;
|
2011-01-15 00:41:09 +03:00
|
|
|
record._orphan = false;
|
|
|
|
} else {
|
|
|
|
this._log.trace("Record " + record.id +
|
|
|
|
" is an orphan: could not find parent " + parentGUID);
|
|
|
|
record._orphan = true;
|
2009-08-15 12:00:46 +04:00
|
|
|
}
|
|
|
|
|
2009-08-15 12:00:32 +04:00
|
|
|
// Do the normal processing of incoming records
|
|
|
|
Store.prototype.applyIncoming.apply(this, arguments);
|
2009-08-15 12:00:46 +04:00
|
|
|
|
|
|
|
// Do some post-processing if we have an item
|
2010-12-07 02:51:38 +03:00
|
|
|
let itemId = this.idForGUID(record.id);
|
2009-08-15 12:00:46 +04:00
|
|
|
if (itemId > 0) {
|
2009-09-12 03:44:26 +04:00
|
|
|
// Move any children that are looking for this folder as a parent
|
2010-12-07 02:51:56 +03:00
|
|
|
if (record.type == "folder") {
|
2009-09-09 10:33:15 +04:00
|
|
|
this._reparentOrphans(itemId);
|
2010-12-07 02:51:56 +03:00
|
|
|
// Reorder children later
|
|
|
|
if (record.children)
|
|
|
|
this._childrenToOrder[record.id] = record.children;
|
|
|
|
}
|
2009-09-09 10:33:15 +04:00
|
|
|
|
2011-01-15 00:41:09 +03:00
|
|
|
// Create an annotation to remember that it needs reparenting.
|
2009-08-15 12:00:46 +04:00
|
|
|
if (record._orphan)
|
2010-12-07 02:51:48 +03:00
|
|
|
Utils.anno(itemId, PARENT_ANNO, parentGUID);
|
2009-08-15 12:00:46 +04:00
|
|
|
}
|
2009-07-14 04:43:15 +04:00
|
|
|
},
|
|
|
|
|
2009-08-15 12:07:40 +04:00
|
|
|
/**
|
|
|
|
* Find all ids of items that have a given value for an annotation
|
|
|
|
*/
|
|
|
|
_findAnnoItems: function BStore__findAnnoItems(anno, val) {
|
|
|
|
return Svc.Annos.getItemsWithAnnotation(anno, {}).filter(function(id)
|
|
|
|
Utils.anno(id, anno) == val);
|
|
|
|
},
|
|
|
|
|
2009-09-09 10:33:15 +04:00
|
|
|
/**
|
|
|
|
* For the provided parent item, attach its children to it
|
|
|
|
*/
|
|
|
|
_reparentOrphans: function _reparentOrphans(parentId) {
|
|
|
|
// Find orphans and reunite with this folder parent
|
2010-12-07 02:51:38 +03:00
|
|
|
let parentGUID = this.GUIDForId(parentId);
|
2009-09-09 10:33:15 +04:00
|
|
|
let orphans = this._findAnnoItems(PARENT_ANNO, parentGUID);
|
|
|
|
|
|
|
|
this._log.debug("Reparenting orphans " + orphans + " to " + parentId);
|
|
|
|
orphans.forEach(function(orphan) {
|
|
|
|
// Move the orphan to the parent and drop the missing parent annotation
|
2011-01-15 00:41:09 +03:00
|
|
|
if (this._reparentItem(orphan, parentId)) {
|
|
|
|
Svc.Annos.removeItemAnnotation(orphan, PARENT_ANNO);
|
|
|
|
}
|
2009-08-19 05:42:15 +04:00
|
|
|
}, this);
|
|
|
|
},
|
|
|
|
|
2011-01-15 00:41:09 +03:00
|
|
|
_reparentItem: function _reparentItem(itemId, parentId) {
|
|
|
|
this._log.trace("Attempting to move item " + itemId + " to new parent " +
|
|
|
|
parentId);
|
|
|
|
try {
|
|
|
|
if (parentId > 0) {
|
|
|
|
Svc.Bookmark.moveItem(itemId, parentId, Svc.Bookmark.DEFAULT_INDEX);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} catch(ex) {
|
|
|
|
this._log.debug("Failed to reparent item. " + Utils.exceptionStr(ex));
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
2011-02-23 23:14:11 +03:00
|
|
|
// Turn a record's nsINavBookmarksService constant and other attributes into
|
|
|
|
// a granular type for comparison.
|
|
|
|
_recordType: function _recordType(itemId) {
|
|
|
|
let bms = this._bms;
|
|
|
|
let type = bms.getItemType(itemId);
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case bms.TYPE_FOLDER:
|
|
|
|
if (this._ls.isLivemark(itemId))
|
|
|
|
return "livemark";
|
|
|
|
return "folder";
|
|
|
|
|
|
|
|
case bms.TYPE_BOOKMARK:
|
|
|
|
if (this._ms && this._ms.hasMicrosummary(itemId))
|
|
|
|
return "microsummary";
|
|
|
|
let bmkUri = bms.getBookmarkURI(itemId).spec;
|
|
|
|
if (bmkUri.search(/^place:/) == 0)
|
|
|
|
return "query";
|
|
|
|
return "bookmark";
|
|
|
|
|
|
|
|
case bms.TYPE_SEPARATOR:
|
|
|
|
return "separator";
|
|
|
|
|
|
|
|
default:
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2008-12-17 04:06:45 +03:00
|
|
|
create: function BStore_create(record) {
|
2011-01-15 00:41:09 +03:00
|
|
|
// Default to unfiled if we don't have the parent yet
|
|
|
|
if (!record._parent) {
|
|
|
|
record._parent = kSpecialIds.unfiled;
|
|
|
|
}
|
|
|
|
|
2008-06-03 23:38:48 +04:00
|
|
|
let newId;
|
2009-04-14 01:39:29 +04:00
|
|
|
switch (record.type) {
|
2008-06-03 23:38:48 +04:00
|
|
|
case "bookmark":
|
2009-07-14 04:43:15 +04:00
|
|
|
case "query":
|
2008-06-03 23:38:48 +04:00
|
|
|
case "microsummary": {
|
2009-04-14 01:39:29 +04:00
|
|
|
let uri = Utils.makeURI(record.bmkUri);
|
2010-12-07 02:51:56 +03:00
|
|
|
newId = this._bms.insertBookmark(record._parent, uri,
|
|
|
|
Svc.Bookmark.DEFAULT_INDEX, record.title);
|
|
|
|
this._log.debug(["created bookmark", newId, "under", record._parent,
|
|
|
|
"as", record.title, record.bmkUri].join(" "));
|
2009-08-16 23:39:30 +04:00
|
|
|
|
2011-01-26 23:36:38 +03:00
|
|
|
// Smart bookmark annotations are strings.
|
|
|
|
if (record.queryId) {
|
|
|
|
Utils.anno(newId, SMART_BOOKMARKS_ANNO, record.queryId);
|
|
|
|
}
|
|
|
|
|
2011-01-15 00:41:09 +03:00
|
|
|
if (Utils.isArray(record.tags)) {
|
|
|
|
this._tagURI(uri, record.tags);
|
|
|
|
}
|
2009-04-14 01:39:29 +04:00
|
|
|
this._bms.setKeywordForBookmark(newId, record.keyword);
|
2009-08-14 04:59:26 +04:00
|
|
|
if (record.description)
|
|
|
|
Utils.anno(newId, "bookmarkProperties/description", record.description);
|
2008-06-03 23:38:48 +04:00
|
|
|
|
2009-06-17 03:16:39 +04:00
|
|
|
if (record.loadInSidebar)
|
2009-08-14 04:59:26 +04:00
|
|
|
Utils.anno(newId, "bookmarkProperties/loadInSidebar", true);
|
2009-06-17 03:16:39 +04:00
|
|
|
|
2009-04-14 01:39:29 +04:00
|
|
|
if (record.type == "microsummary") {
|
2008-06-03 23:38:48 +04:00
|
|
|
this._log.debug(" \-> is a microsummary");
|
2009-08-14 04:59:26 +04:00
|
|
|
Utils.anno(newId, "bookmarks/staticTitle", record.staticTitle || "");
|
2009-04-14 01:39:29 +04:00
|
|
|
let genURI = Utils.makeURI(record.generatorUri);
|
2010-04-03 03:37:53 +04:00
|
|
|
if (this._ms) {
|
2008-12-12 01:26:20 +03:00
|
|
|
try {
|
2008-12-29 06:59:44 +03:00
|
|
|
let micsum = this._ms.createMicrosummary(uri, genURI);
|
2008-12-12 01:26:20 +03:00
|
|
|
this._ms.setMicrosummary(newId, micsum);
|
|
|
|
}
|
|
|
|
catch(ex) { /* ignore "missing local generator" exceptions */ }
|
2010-04-03 03:37:53 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
this._log.warn("Can't create microsummary -- not supported.");
|
2008-06-03 23:38:48 +04:00
|
|
|
}
|
|
|
|
} break;
|
|
|
|
case "folder":
|
2009-08-16 23:39:15 +04:00
|
|
|
newId = this._bms.createFolder(record._parent, record.title,
|
2010-12-07 02:51:56 +03:00
|
|
|
Svc.Bookmark.DEFAULT_INDEX);
|
|
|
|
this._log.debug(["created folder", newId, "under", record._parent,
|
|
|
|
"as", record.title].join(" "));
|
2010-04-02 02:11:42 +04:00
|
|
|
|
|
|
|
if (record.description)
|
|
|
|
Utils.anno(newId, "bookmarkProperties/description", record.description);
|
2010-12-07 02:51:56 +03:00
|
|
|
|
|
|
|
// record.children will be dealt with in _orderChildren.
|
2008-06-03 23:38:48 +04:00
|
|
|
break;
|
|
|
|
case "livemark":
|
2009-08-27 01:22:11 +04:00
|
|
|
let siteURI = null;
|
|
|
|
if (record.siteUri != null)
|
|
|
|
siteURI = Utils.makeURI(record.siteUri);
|
|
|
|
|
|
|
|
newId = this._ls.createLivemark(record._parent, record.title, siteURI,
|
2010-12-07 02:51:56 +03:00
|
|
|
Utils.makeURI(record.feedUri),
|
|
|
|
Svc.Bookmark.DEFAULT_INDEX);
|
|
|
|
this._log.debug(["created livemark", newId, "under", record._parent, "as",
|
|
|
|
record.title, record.siteUri, record.feedUri].join(" "));
|
2008-06-03 23:38:48 +04:00
|
|
|
break;
|
|
|
|
case "separator":
|
2010-12-07 02:51:56 +03:00
|
|
|
newId = this._bms.insertSeparator(record._parent,
|
|
|
|
Svc.Bookmark.DEFAULT_INDEX);
|
|
|
|
this._log.debug(["created separator", newId, "under", record._parent]
|
|
|
|
.join(" "));
|
2008-06-03 23:38:48 +04:00
|
|
|
break;
|
2009-07-10 04:15:00 +04:00
|
|
|
case "item":
|
|
|
|
this._log.debug(" -> got a generic places item.. do nothing?");
|
2009-08-15 12:04:06 +04:00
|
|
|
return;
|
2008-06-03 23:38:48 +04:00
|
|
|
default:
|
2009-04-14 01:39:29 +04:00
|
|
|
this._log.error("_create: Unknown item type: " + record.type);
|
2009-08-15 12:04:06 +04:00
|
|
|
return;
|
2008-12-05 11:39:54 +03:00
|
|
|
}
|
2009-08-15 12:04:06 +04:00
|
|
|
|
|
|
|
this._log.trace("Setting GUID of new item " + newId + " to " + record.id);
|
|
|
|
this._setGUID(newId, record.id);
|
2008-06-03 23:38:48 +04:00
|
|
|
},
|
|
|
|
|
2011-02-23 23:14:11 +03:00
|
|
|
// Factored out of `remove` to avoid redundant DB queries when the Places ID
|
|
|
|
// is already known.
|
|
|
|
removeById: function removeById(itemId, guid) {
|
|
|
|
let type = this._bms.getItemType(itemId);
|
2008-06-03 23:38:48 +04:00
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case this._bms.TYPE_BOOKMARK:
|
2011-02-23 23:14:11 +03:00
|
|
|
this._log.debug(" -> removing bookmark " + guid);
|
2009-04-08 08:17:40 +04:00
|
|
|
this._ts.untagURI(this._bms.getBookmarkURI(itemId), null);
|
2008-06-03 23:38:48 +04:00
|
|
|
this._bms.removeItem(itemId);
|
|
|
|
break;
|
|
|
|
case this._bms.TYPE_FOLDER:
|
2011-02-23 23:14:11 +03:00
|
|
|
this._log.debug(" -> removing folder " + guid);
|
2009-11-18 21:24:09 +03:00
|
|
|
Svc.Bookmark.removeItem(itemId);
|
2008-06-03 23:38:48 +04:00
|
|
|
break;
|
|
|
|
case this._bms.TYPE_SEPARATOR:
|
2011-02-23 23:14:11 +03:00
|
|
|
this._log.debug(" -> removing separator " + guid);
|
2008-06-03 23:38:48 +04:00
|
|
|
this._bms.removeItem(itemId);
|
|
|
|
break;
|
|
|
|
default:
|
2008-12-17 04:06:45 +03:00
|
|
|
this._log.error("remove: Unknown item type: " + type);
|
2008-06-03 23:38:48 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2011-02-23 23:14:11 +03:00
|
|
|
remove: function BStore_remove(record) {
|
|
|
|
let itemId = this.idForGUID(record.id);
|
|
|
|
if (itemId <= 0) {
|
|
|
|
this._log.debug("Item " + record.id + " already removed");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.removeById(itemId, record.id);
|
|
|
|
},
|
|
|
|
|
2008-12-17 04:06:45 +03:00
|
|
|
update: function BStore_update(record) {
|
2010-12-07 02:51:38 +03:00
|
|
|
let itemId = this.idForGUID(record.id);
|
2008-12-17 04:06:45 +03:00
|
|
|
|
2009-01-07 00:54:18 +03:00
|
|
|
if (itemId <= 0) {
|
2008-12-23 22:30:31 +03:00
|
|
|
this._log.debug("Skipping update for unknown item: " + record.id);
|
2008-06-03 23:38:48 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-02-23 23:14:11 +03:00
|
|
|
// Two items are the same type if they have the same ItemType in Places,
|
|
|
|
// and also share some key characteristics (e.g., both being livemarks).
|
|
|
|
// We figure this out by examining the item to find the equivalent granular
|
|
|
|
// (string) type.
|
|
|
|
// If they're not the same type, we can't just update attributes. Delete
|
|
|
|
// then recreate the record instead.
|
|
|
|
let localItemType = this._recordType(itemId);
|
|
|
|
let remoteRecordType = record.type;
|
|
|
|
this._log.trace("Local type: " + localItemType + ". " +
|
|
|
|
"Remote type: " + remoteRecordType + ".");
|
|
|
|
|
|
|
|
if (localItemType != remoteRecordType) {
|
|
|
|
this._log.debug("Local record and remote record differ in type. " +
|
|
|
|
"Deleting and recreating.");
|
|
|
|
this.removeById(itemId, record.id);
|
|
|
|
this.create(record);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-12-23 22:30:31 +03:00
|
|
|
this._log.trace("Updating " + record.id + " (" + itemId + ")");
|
|
|
|
|
2010-12-07 02:51:56 +03:00
|
|
|
// Move the bookmark to a new parent or new position if necessary
|
2011-01-15 00:41:09 +03:00
|
|
|
if (record._parent > 0 &&
|
|
|
|
Svc.Bookmark.getFolderIdForItem(itemId) != record._parent) {
|
|
|
|
this._reparentItem(itemId, record._parent);
|
2008-12-23 22:30:31 +03:00
|
|
|
}
|
|
|
|
|
2009-04-14 01:18:11 +04:00
|
|
|
for (let [key, val] in Iterator(record.cleartext)) {
|
2008-06-03 23:38:48 +04:00
|
|
|
switch (key) {
|
|
|
|
case "title":
|
2011-01-15 00:41:09 +03:00
|
|
|
val = val || "";
|
2009-04-14 01:18:11 +04:00
|
|
|
this._bms.setItemTitle(itemId, val);
|
2008-06-03 23:38:48 +04:00
|
|
|
break;
|
2009-04-14 01:39:29 +04:00
|
|
|
case "bmkUri":
|
2009-04-14 01:18:11 +04:00
|
|
|
this._bms.changeBookmarkURI(itemId, Utils.makeURI(val));
|
2008-06-03 23:38:48 +04:00
|
|
|
break;
|
2009-07-14 00:40:49 +04:00
|
|
|
case "tags":
|
2011-01-15 00:41:09 +03:00
|
|
|
if (Utils.isArray(val)) {
|
|
|
|
this._tagURI(this._bms.getBookmarkURI(itemId), val);
|
|
|
|
}
|
2009-07-14 00:40:49 +04:00
|
|
|
break;
|
2008-06-03 23:38:48 +04:00
|
|
|
case "keyword":
|
2009-04-14 01:18:11 +04:00
|
|
|
this._bms.setKeywordForBookmark(itemId, val);
|
2008-06-03 23:38:48 +04:00
|
|
|
break;
|
|
|
|
case "description":
|
2011-01-15 00:41:09 +03:00
|
|
|
val = val || "";
|
2009-08-14 04:59:26 +04:00
|
|
|
Utils.anno(itemId, "bookmarkProperties/description", val);
|
2008-06-03 23:38:48 +04:00
|
|
|
break;
|
2009-06-17 03:16:39 +04:00
|
|
|
case "loadInSidebar":
|
|
|
|
if (val)
|
2009-08-14 04:59:26 +04:00
|
|
|
Utils.anno(itemId, "bookmarkProperties/loadInSidebar", true);
|
2009-06-17 03:16:39 +04:00
|
|
|
else
|
2009-08-14 04:59:26 +04:00
|
|
|
Svc.Annos.removeItemAnnotation(itemId, "bookmarkProperties/loadInSidebar");
|
2009-06-17 03:16:39 +04:00
|
|
|
break;
|
2009-04-14 01:39:29 +04:00
|
|
|
case "generatorUri": {
|
2009-01-22 06:04:13 +03:00
|
|
|
try {
|
2009-04-14 01:39:29 +04:00
|
|
|
let micsumURI = this._bms.getBookmarkURI(itemId);
|
2009-04-14 01:18:11 +04:00
|
|
|
let genURI = Utils.makeURI(val);
|
2010-04-03 03:37:53 +04:00
|
|
|
if (this._ms == SERVICE_NOT_SUPPORTED)
|
|
|
|
this._log.warn("Can't create microsummary -- not supported.");
|
|
|
|
else {
|
2009-01-22 06:04:13 +03:00
|
|
|
let micsum = this._ms.createMicrosummary(micsumURI, genURI);
|
|
|
|
this._ms.setMicrosummary(itemId, micsum);
|
2010-04-03 03:37:53 +04:00
|
|
|
}
|
2009-01-22 06:04:13 +03:00
|
|
|
} catch (e) {
|
|
|
|
this._log.debug("Could not set microsummary generator URI: " + e);
|
|
|
|
}
|
2008-06-03 23:38:48 +04:00
|
|
|
} break;
|
2011-01-26 23:36:38 +03:00
|
|
|
case "queryId":
|
|
|
|
Utils.anno(itemId, SMART_BOOKMARKS_ANNO, val);
|
|
|
|
break;
|
2009-04-14 01:39:29 +04:00
|
|
|
case "siteUri":
|
2009-04-14 01:18:11 +04:00
|
|
|
this._ls.setSiteURI(itemId, Utils.makeURI(val));
|
2008-06-03 23:38:48 +04:00
|
|
|
break;
|
2009-04-14 01:39:29 +04:00
|
|
|
case "feedUri":
|
2009-04-14 01:18:11 +04:00
|
|
|
this._ls.setFeedURI(itemId, Utils.makeURI(val));
|
2008-06-03 23:38:48 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2010-12-07 02:51:56 +03:00
|
|
|
_orderChildren: function _orderChildren() {
|
|
|
|
for (let [guid, children] in Iterator(this._childrenToOrder)) {
|
|
|
|
// Reorder children according to the GUID list. Gracefully deal
|
|
|
|
// with missing items, e.g. locally deleted.
|
|
|
|
let delta = 0;
|
2011-02-17 02:53:23 +03:00
|
|
|
let parent = null;
|
2010-12-07 02:51:56 +03:00
|
|
|
for (let idx = 0; idx < children.length; idx++) {
|
|
|
|
let itemid = this.idForGUID(children[idx]);
|
|
|
|
if (itemid == -1) {
|
|
|
|
delta += 1;
|
|
|
|
this._log.trace("Could not locate record " + children[idx]);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
try {
|
2011-02-17 02:53:23 +03:00
|
|
|
// This code path could be optimized by caching the parent earlier.
|
|
|
|
// Doing so should take in count any edge case due to reparenting
|
|
|
|
// or parent invalidations though.
|
|
|
|
if (!parent)
|
|
|
|
parent = Svc.Bookmark.getFolderIdForItem(itemid);
|
|
|
|
Svc.Bookmark.moveItem(itemid, parent, idx - delta);
|
2010-12-07 02:51:56 +03:00
|
|
|
} catch (ex) {
|
|
|
|
this._log.debug("Could not move item " + children[idx] + ": " + ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2008-12-09 00:21:25 +03:00
|
|
|
changeItemID: function BStore_changeItemID(oldID, newID) {
|
2010-12-08 08:49:22 +03:00
|
|
|
this._log.debug("Changing GUID " + oldID + " to " + newID);
|
2009-09-09 10:33:15 +04:00
|
|
|
|
|
|
|
// Make sure there's an item to change GUIDs
|
2010-12-07 02:51:38 +03:00
|
|
|
let itemId = this.idForGUID(oldID);
|
2009-09-09 10:33:15 +04:00
|
|
|
if (itemId <= 0)
|
2008-12-05 11:39:54 +03:00
|
|
|
return;
|
2008-11-26 18:25:28 +03:00
|
|
|
|
2009-08-15 12:04:06 +04:00
|
|
|
this._setGUID(itemId, newID);
|
|
|
|
},
|
|
|
|
|
2008-12-29 06:59:44 +03:00
|
|
|
_getNode: function BStore__getNode(folder) {
|
2008-06-03 23:38:48 +04:00
|
|
|
let query = this._hsvc.getNewQuery();
|
|
|
|
query.setFolders([folder], 1);
|
|
|
|
return this._hsvc.executeQuery(query, this._hsvc.getNewQueryOptions()).root;
|
|
|
|
},
|
|
|
|
|
2008-12-29 06:59:44 +03:00
|
|
|
_getTags: function BStore__getTags(uri) {
|
|
|
|
try {
|
|
|
|
if (typeof(uri) == "string")
|
|
|
|
uri = Utils.makeURI(uri);
|
|
|
|
} catch(e) {
|
|
|
|
this._log.warn("Could not parse URI \"" + uri + "\": " + e);
|
2008-06-03 23:38:48 +04:00
|
|
|
}
|
2008-12-29 06:59:44 +03:00
|
|
|
return this._ts.getTagsForURI(uri, {});
|
|
|
|
},
|
2008-06-03 23:38:48 +04:00
|
|
|
|
2008-12-29 06:59:44 +03:00
|
|
|
_getDescription: function BStore__getDescription(id) {
|
|
|
|
try {
|
2009-08-14 04:59:26 +04:00
|
|
|
return Utils.anno(id, "bookmarkProperties/description");
|
2008-12-29 06:59:44 +03:00
|
|
|
} catch (e) {
|
2011-01-15 00:41:09 +03:00
|
|
|
return null;
|
2008-12-29 06:59:44 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2009-06-17 03:16:39 +04:00
|
|
|
_isLoadInSidebar: function BStore__isLoadInSidebar(id) {
|
2009-08-14 04:59:26 +04:00
|
|
|
return Svc.Annos.itemHasAnnotation(id, "bookmarkProperties/loadInSidebar");
|
2009-06-17 03:16:39 +04:00
|
|
|
},
|
|
|
|
|
2008-12-29 06:59:44 +03:00
|
|
|
_getStaticTitle: function BStore__getStaticTitle(id) {
|
|
|
|
try {
|
2009-08-14 04:59:26 +04:00
|
|
|
return Utils.anno(id, "bookmarks/staticTitle");
|
2008-12-29 06:59:44 +03:00
|
|
|
} catch (e) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2011-01-14 01:03:11 +03:00
|
|
|
__childGUIDsStm: null,
|
|
|
|
get _childGUIDsStm() {
|
|
|
|
if (this.__childGUIDsStm) {
|
|
|
|
return this.__childGUIDsStm;
|
|
|
|
}
|
|
|
|
|
|
|
|
let stmt;
|
|
|
|
if (this._haveGUIDColumn) {
|
|
|
|
stmt = this._getStmt(
|
|
|
|
"SELECT id AS item_id, guid " +
|
|
|
|
"FROM moz_bookmarks " +
|
|
|
|
"WHERE parent = :parent " +
|
|
|
|
"ORDER BY position");
|
|
|
|
} else {
|
|
|
|
stmt = this._getStmt(
|
|
|
|
"SELECT b.id AS item_id, " +
|
|
|
|
"(SELECT id FROM moz_anno_attributes WHERE name = '" + GUID_ANNO + "') AS name_id," +
|
|
|
|
"a.content AS guid " +
|
|
|
|
"FROM moz_bookmarks b " +
|
|
|
|
"LEFT JOIN moz_items_annos a ON a.item_id = b.id " +
|
|
|
|
"AND a.anno_attribute_id = name_id " +
|
|
|
|
"WHERE b.parent = :parent " +
|
|
|
|
"ORDER BY b.position");
|
2011-01-14 01:03:09 +03:00
|
|
|
}
|
2011-01-14 01:03:11 +03:00
|
|
|
return this.__childGUIDsStm = stmt;
|
|
|
|
},
|
|
|
|
|
|
|
|
_getChildGUIDsForId: function _getChildGUIDsForId(itemid) {
|
|
|
|
let stmt = this._childGUIDsStm;
|
|
|
|
stmt.params.parent = itemid;
|
|
|
|
let rows = Utils.queryAsync(stmt, ["item_id", "guid"]);
|
|
|
|
return rows.map(function (row) {
|
|
|
|
if (row.guid) {
|
|
|
|
return row.guid;
|
|
|
|
}
|
|
|
|
// A GUID hasn't been assigned to this item yet, do this now.
|
|
|
|
return this.GUIDForId(row.item_id);
|
|
|
|
}, this);
|
2010-12-07 02:51:56 +03:00
|
|
|
},
|
|
|
|
|
2008-12-29 06:59:44 +03:00
|
|
|
// Create a record starting from the weave id (places guid)
|
2010-11-30 03:41:17 +03:00
|
|
|
createRecord: function createRecord(id, collection) {
|
2010-12-07 02:51:38 +03:00
|
|
|
let placeId = this.idForGUID(id);
|
2010-03-25 23:58:27 +03:00
|
|
|
let record;
|
2009-01-07 00:54:18 +03:00
|
|
|
if (placeId <= 0) { // deleted item
|
2010-11-30 03:41:17 +03:00
|
|
|
record = new PlacesItem(collection, id);
|
2009-04-03 21:38:47 +04:00
|
|
|
record.deleted = true;
|
2008-12-29 06:59:44 +03:00
|
|
|
return record;
|
|
|
|
}
|
|
|
|
|
2009-11-20 08:34:17 +03:00
|
|
|
let parent = Svc.Bookmark.getFolderIdForItem(placeId);
|
2008-12-29 06:59:44 +03:00
|
|
|
switch (this._bms.getItemType(placeId)) {
|
|
|
|
case this._bms.TYPE_BOOKMARK:
|
2009-07-14 04:43:15 +04:00
|
|
|
let bmkUri = this._bms.getBookmarkURI(placeId).spec;
|
2008-12-29 06:59:44 +03:00
|
|
|
if (this._ms && this._ms.hasMicrosummary(placeId)) {
|
2010-11-30 03:41:17 +03:00
|
|
|
record = new BookmarkMicsum(collection, id);
|
2008-12-29 06:59:44 +03:00
|
|
|
let micsum = this._ms.getMicrosummary(placeId);
|
2009-04-14 01:39:29 +04:00
|
|
|
record.generatorUri = micsum.generator.uri.spec; // breaks local generators
|
2008-12-29 06:59:44 +03:00
|
|
|
record.staticTitle = this._getStaticTitle(placeId);
|
2009-07-14 04:43:15 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (bmkUri.search(/^place:/) == 0) {
|
2010-11-30 03:41:17 +03:00
|
|
|
record = new BookmarkQuery(collection, id);
|
2008-06-25 05:09:41 +04:00
|
|
|
|
2009-07-14 04:43:15 +04:00
|
|
|
// Get the actual tag name instead of the local itemId
|
|
|
|
let folder = bmkUri.match(/[:&]folder=(\d+)/);
|
|
|
|
try {
|
|
|
|
// There might not be the tag yet when creating on a new client
|
|
|
|
if (folder != null) {
|
|
|
|
folder = folder[1];
|
|
|
|
record.folderName = this._bms.getItemTitle(folder);
|
2011-01-26 23:36:38 +03:00
|
|
|
this._log.trace("query id: " + folder + " = " + record.folderName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(ex) {}
|
|
|
|
|
|
|
|
// Persist the Smart Bookmark anno, if found.
|
|
|
|
try {
|
|
|
|
let anno = Utils.anno(placeId, SMART_BOOKMARKS_ANNO);
|
|
|
|
if (anno != null) {
|
|
|
|
this._log.trace("query anno: " + SMART_BOOKMARKS_ANNO +
|
|
|
|
" = " + anno);
|
|
|
|
record.queryId = anno;
|
2009-07-14 04:43:15 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(ex) {}
|
|
|
|
}
|
|
|
|
else
|
2010-11-30 03:41:17 +03:00
|
|
|
record = new Bookmark(collection, id);
|
2008-12-29 06:59:44 +03:00
|
|
|
record.title = this._bms.getItemTitle(placeId);
|
2008-06-03 23:38:48 +04:00
|
|
|
}
|
|
|
|
|
2010-01-25 20:33:49 +03:00
|
|
|
record.parentName = Svc.Bookmark.getItemTitle(parent);
|
2009-07-14 04:43:15 +04:00
|
|
|
record.bmkUri = bmkUri;
|
2008-12-29 06:59:44 +03:00
|
|
|
record.tags = this._getTags(record.bmkUri);
|
|
|
|
record.keyword = this._bms.getKeywordForBookmark(placeId);
|
|
|
|
record.description = this._getDescription(placeId);
|
2009-06-17 03:16:39 +04:00
|
|
|
record.loadInSidebar = this._isLoadInSidebar(placeId);
|
2008-12-29 06:59:44 +03:00
|
|
|
break;
|
2008-06-03 23:38:48 +04:00
|
|
|
|
2008-12-29 06:59:44 +03:00
|
|
|
case this._bms.TYPE_FOLDER:
|
|
|
|
if (this._ls.isLivemark(placeId)) {
|
2010-11-30 03:41:17 +03:00
|
|
|
record = new Livemark(collection, id);
|
2009-08-27 01:22:11 +04:00
|
|
|
|
|
|
|
let siteURI = this._ls.getSiteURI(placeId);
|
|
|
|
if (siteURI != null)
|
|
|
|
record.siteUri = siteURI.spec;
|
2009-04-14 01:39:29 +04:00
|
|
|
record.feedUri = this._ls.getFeedURI(placeId).spec;
|
2008-06-05 00:40:53 +04:00
|
|
|
|
2008-12-29 06:59:44 +03:00
|
|
|
} else {
|
2010-11-30 03:41:17 +03:00
|
|
|
record = new BookmarkFolder(collection, id);
|
2008-06-05 00:40:53 +04:00
|
|
|
}
|
2009-04-02 02:12:08 +04:00
|
|
|
|
2010-12-07 02:51:56 +03:00
|
|
|
if (parent > 0)
|
|
|
|
record.parentName = Svc.Bookmark.getItemTitle(parent);
|
2009-04-02 02:12:08 +04:00
|
|
|
record.title = this._bms.getItemTitle(placeId);
|
2010-04-02 02:11:42 +04:00
|
|
|
record.description = this._getDescription(placeId);
|
2010-12-07 02:51:56 +03:00
|
|
|
record.children = this._getChildGUIDsForId(placeId);
|
2008-12-29 06:59:44 +03:00
|
|
|
break;
|
2008-06-05 00:40:53 +04:00
|
|
|
|
2008-12-29 06:59:44 +03:00
|
|
|
case this._bms.TYPE_SEPARATOR:
|
2010-11-30 03:41:17 +03:00
|
|
|
record = new BookmarkSeparator(collection, id);
|
2010-12-07 02:51:56 +03:00
|
|
|
if (parent > 0)
|
|
|
|
record.parentName = Svc.Bookmark.getItemTitle(parent);
|
|
|
|
// Create a positioning identifier for the separator, used by _lazyMap
|
2009-11-20 08:34:17 +03:00
|
|
|
record.pos = Svc.Bookmark.getItemIndex(placeId);
|
2008-12-29 06:59:44 +03:00
|
|
|
break;
|
2008-06-03 23:38:48 +04:00
|
|
|
|
2008-12-29 06:59:44 +03:00
|
|
|
case this._bms.TYPE_DYNAMIC_CONTAINER:
|
2010-11-30 03:41:17 +03:00
|
|
|
record = new PlacesItem(collection, id);
|
2008-12-29 06:59:44 +03:00
|
|
|
this._log.warn("Don't know how to serialize dynamic containers yet");
|
|
|
|
break;
|
2008-06-03 23:38:48 +04:00
|
|
|
|
2008-12-29 06:59:44 +03:00
|
|
|
default:
|
2010-11-30 03:41:17 +03:00
|
|
|
record = new PlacesItem(collection, id);
|
2008-12-29 06:59:44 +03:00
|
|
|
this._log.warn("Unknown item type, cannot serialize: " +
|
|
|
|
this._bms.getItemType(placeId));
|
2008-06-03 23:38:48 +04:00
|
|
|
}
|
|
|
|
|
2010-12-07 02:51:56 +03:00
|
|
|
record.parentid = this.GUIDForId(parent);
|
2009-10-17 03:19:28 +04:00
|
|
|
record.sortindex = this._calculateIndex(record);
|
2008-06-03 23:38:48 +04:00
|
|
|
|
2008-12-29 06:59:44 +03:00
|
|
|
return record;
|
2008-06-03 23:38:48 +04:00
|
|
|
},
|
|
|
|
|
2010-12-07 02:51:38 +03:00
|
|
|
_stmts: {},
|
|
|
|
_getStmt: function(query) {
|
|
|
|
if (query in this._stmts)
|
|
|
|
return this._stmts[query];
|
|
|
|
|
|
|
|
this._log.trace("Creating SQL statement: " + query);
|
|
|
|
return this._stmts[query] = Utils.createStatement(this._hsvc.DBConnection,
|
|
|
|
query);
|
|
|
|
},
|
|
|
|
|
2010-12-16 03:08:04 +03:00
|
|
|
__haveGUIDColumn: null,
|
|
|
|
get _haveGUIDColumn() {
|
|
|
|
if (this.__haveGUIDColumn !== null) {
|
|
|
|
return this.__haveGUIDColumn;
|
|
|
|
}
|
|
|
|
let stmt;
|
|
|
|
try {
|
|
|
|
stmt = this._hsvc.DBConnection.createStatement(
|
|
|
|
"SELECT guid FROM moz_places");
|
|
|
|
stmt.finalize();
|
|
|
|
return this.__haveGUIDColumn = true;
|
|
|
|
} catch(ex) {
|
|
|
|
return this.__haveGUIDColumn = false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2009-10-17 03:19:28 +04:00
|
|
|
get _frecencyStm() {
|
2010-12-07 02:51:38 +03:00
|
|
|
return this._getStmt(
|
2010-08-04 16:50:44 +04:00
|
|
|
"SELECT frecency " +
|
|
|
|
"FROM moz_places " +
|
2010-08-09 20:38:18 +04:00
|
|
|
"WHERE url = :url " +
|
|
|
|
"LIMIT 1");
|
2010-12-07 02:51:38 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
get _addGUIDAnnotationNameStm() {
|
|
|
|
let stmt = this._getStmt(
|
|
|
|
"INSERT OR IGNORE INTO moz_anno_attributes (name) VALUES (:anno_name)");
|
|
|
|
stmt.params.anno_name = GUID_ANNO;
|
|
|
|
return stmt;
|
|
|
|
},
|
|
|
|
|
|
|
|
get _checkGUIDItemAnnotationStm() {
|
2010-12-15 02:48:05 +03:00
|
|
|
// Gecko <2.0 only
|
2010-12-07 02:51:38 +03:00
|
|
|
let stmt = this._getStmt(
|
|
|
|
"SELECT b.id AS item_id, " +
|
|
|
|
"(SELECT id FROM moz_anno_attributes WHERE name = :anno_name) AS name_id, " +
|
|
|
|
"a.id AS anno_id, a.dateAdded AS anno_date " +
|
|
|
|
"FROM moz_bookmarks b " +
|
|
|
|
"LEFT JOIN moz_items_annos a ON a.item_id = b.id " +
|
|
|
|
"AND a.anno_attribute_id = name_id " +
|
|
|
|
"WHERE b.id = :item_id");
|
|
|
|
stmt.params.anno_name = GUID_ANNO;
|
|
|
|
return stmt;
|
|
|
|
},
|
|
|
|
|
|
|
|
get _addItemAnnotationStm() {
|
|
|
|
return this._getStmt(
|
|
|
|
"INSERT OR REPLACE INTO moz_items_annos " +
|
|
|
|
"(id, item_id, anno_attribute_id, mime_type, content, flags, " +
|
|
|
|
"expiration, type, dateAdded, lastModified) " +
|
|
|
|
"VALUES (:id, :item_id, :name_id, :mime_type, :content, :flags, " +
|
|
|
|
":expiration, :type, :date_added, :last_modified)");
|
|
|
|
},
|
|
|
|
|
2010-12-15 02:48:05 +03:00
|
|
|
__setGUIDStm: null,
|
|
|
|
get _setGUIDStm() {
|
|
|
|
if (this.__setGUIDStm !== null) {
|
|
|
|
return this.__setGUIDStm;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Obtains a statement to set the guid iff the guid column exists.
|
|
|
|
let stmt;
|
2010-12-16 03:08:04 +03:00
|
|
|
if (this._haveGUIDColumn) {
|
2010-12-15 02:48:05 +03:00
|
|
|
stmt = this._getStmt(
|
|
|
|
"UPDATE moz_bookmarks " +
|
|
|
|
"SET guid = :guid " +
|
|
|
|
"WHERE id = :item_id");
|
2010-12-16 03:08:04 +03:00
|
|
|
} else {
|
2010-12-15 02:48:05 +03:00
|
|
|
stmt = false;
|
|
|
|
}
|
|
|
|
return this.__setGUIDStm = stmt;
|
|
|
|
},
|
|
|
|
|
2010-12-07 02:51:38 +03:00
|
|
|
// Some helper functions to handle GUIDs
|
|
|
|
_setGUID: function _setGUID(id, guid) {
|
|
|
|
if (arguments.length == 1)
|
|
|
|
guid = Utils.makeGUID();
|
|
|
|
|
2010-12-15 02:48:05 +03:00
|
|
|
// If we can, set the GUID on moz_bookmarks and do not do any other work.
|
|
|
|
let (stmt = this._setGUIDStm) {
|
|
|
|
if (stmt) {
|
|
|
|
stmt.params.guid = guid;
|
|
|
|
stmt.params.item_id = id;
|
|
|
|
Utils.queryAsync(stmt);
|
|
|
|
return guid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-07 02:51:38 +03:00
|
|
|
// Ensure annotation name exists
|
|
|
|
Utils.queryAsync(this._addGUIDAnnotationNameStm);
|
|
|
|
|
|
|
|
let stmt = this._checkGUIDItemAnnotationStm;
|
|
|
|
stmt.params.item_id = id;
|
|
|
|
let result = Utils.queryAsync(stmt, ["item_id", "name_id", "anno_id",
|
|
|
|
"anno_date"])[0];
|
|
|
|
if (!result) {
|
2010-12-08 08:49:22 +03:00
|
|
|
this._log.warn("Couldn't annotate bookmark id " + id);
|
2010-12-07 02:51:38 +03:00
|
|
|
return guid;
|
|
|
|
}
|
|
|
|
|
|
|
|
stmt = this._addItemAnnotationStm;
|
|
|
|
if (result.anno_id) {
|
|
|
|
stmt.params.id = result.anno_id;
|
|
|
|
stmt.params.date_added = result.anno_date;
|
|
|
|
} else {
|
|
|
|
stmt.params.id = null;
|
|
|
|
stmt.params.date_added = Date.now() * 1000;
|
2010-08-04 16:50:44 +04:00
|
|
|
}
|
2010-12-07 02:51:38 +03:00
|
|
|
stmt.params.item_id = result.item_id;
|
|
|
|
stmt.params.name_id = result.name_id;
|
|
|
|
stmt.params.content = guid;
|
|
|
|
stmt.params.flags = 0;
|
|
|
|
stmt.params.expiration = Ci.nsIAnnotationService.EXPIRE_NEVER;
|
|
|
|
stmt.params.type = Ci.nsIAnnotationService.TYPE_STRING;
|
|
|
|
stmt.params.last_modified = Date.now() * 1000;
|
|
|
|
Utils.queryAsync(stmt);
|
|
|
|
|
|
|
|
return guid;
|
|
|
|
},
|
|
|
|
|
2010-12-15 02:48:05 +03:00
|
|
|
__guidForIdStm: null,
|
2010-12-07 02:51:38 +03:00
|
|
|
get _guidForIdStm() {
|
2010-12-15 02:48:05 +03:00
|
|
|
if (this.__guidForIdStm) {
|
|
|
|
return this.__guidForIdStm;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to first read from moz_bookmarks. Creating the statement will
|
|
|
|
// fail, however, if the guid column does not exist. We fallback to just
|
|
|
|
// reading the annotation table in this case.
|
|
|
|
let stmt;
|
2010-12-16 03:08:04 +03:00
|
|
|
if (this._haveGUIDColumn) {
|
2010-12-15 02:48:05 +03:00
|
|
|
stmt = this._getStmt(
|
|
|
|
"SELECT guid " +
|
|
|
|
"FROM moz_bookmarks " +
|
|
|
|
"WHERE id = :item_id");
|
2010-12-16 03:08:04 +03:00
|
|
|
} else {
|
2010-12-15 02:48:05 +03:00
|
|
|
stmt = this._getStmt(
|
|
|
|
"SELECT a.content AS guid " +
|
|
|
|
"FROM moz_items_annos a " +
|
|
|
|
"JOIN moz_anno_attributes n ON n.id = a.anno_attribute_id " +
|
|
|
|
"JOIN moz_bookmarks b ON b.id = a.item_id " +
|
|
|
|
"WHERE n.name = '" + GUID_ANNO + "' " +
|
|
|
|
"AND b.id = :item_id");
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.__guidForIdStm = stmt;
|
2010-12-07 02:51:38 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
GUIDForId: function GUIDForId(id) {
|
2011-01-24 21:45:27 +03:00
|
|
|
let special = kSpecialIds.specialGUIDForId(id);
|
|
|
|
if (special)
|
|
|
|
return special;
|
2010-12-07 02:51:38 +03:00
|
|
|
|
|
|
|
let stmt = this._guidForIdStm;
|
|
|
|
stmt.params.item_id = id;
|
|
|
|
|
|
|
|
// Use the existing GUID if it exists
|
|
|
|
let result = Utils.queryAsync(stmt, ["guid"])[0];
|
2010-12-15 02:48:05 +03:00
|
|
|
if (result && result.guid)
|
2010-12-07 02:51:38 +03:00
|
|
|
return result.guid;
|
|
|
|
|
|
|
|
// Give the uri a GUID if it doesn't have one
|
|
|
|
return this._setGUID(id);
|
|
|
|
},
|
|
|
|
|
2010-12-15 02:48:05 +03:00
|
|
|
__idForGUIDStm: null,
|
2010-12-07 02:51:38 +03:00
|
|
|
get _idForGUIDStm() {
|
2010-12-15 02:48:05 +03:00
|
|
|
if (this.__idForGUIDStm) {
|
|
|
|
return this.__idForGUIDStm;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Try to first read from moz_bookmarks. Creating the statement will
|
|
|
|
// fail, however, if the guid column does not exist. We fallback to just
|
|
|
|
// reading the annotation table in this case.
|
|
|
|
let stmt;
|
2010-12-16 03:08:04 +03:00
|
|
|
if (this._haveGUIDColumn) {
|
2010-12-15 02:48:05 +03:00
|
|
|
stmt = this._getStmt(
|
|
|
|
"SELECT id AS item_id " +
|
|
|
|
"FROM moz_bookmarks " +
|
|
|
|
"WHERE guid = :guid");
|
2010-12-16 03:08:04 +03:00
|
|
|
} else {
|
2011-01-25 23:37:23 +03:00
|
|
|
// Order results by lastModified so we can preserve the ID of the oldest bookmark.
|
|
|
|
// Copying a record preserves its dateAdded, and only modifying the
|
|
|
|
// bookmark alters its lastModified, so we also order by its item_id --
|
|
|
|
// lowest wins ties. Of course, Places can still screw us by reassigning IDs...
|
2010-12-15 02:48:05 +03:00
|
|
|
stmt = this._getStmt(
|
|
|
|
"SELECT a.item_id AS item_id " +
|
|
|
|
"FROM moz_items_annos a " +
|
|
|
|
"JOIN moz_anno_attributes n ON n.id = a.anno_attribute_id " +
|
|
|
|
"WHERE n.name = '" + GUID_ANNO + "' " +
|
2011-01-25 23:37:23 +03:00
|
|
|
"AND a.content = :guid " +
|
|
|
|
"ORDER BY a.lastModified, a.item_id");
|
2010-12-15 02:48:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return this.__idForGUIDStm = stmt;
|
2010-12-07 02:51:38 +03:00
|
|
|
},
|
|
|
|
|
2011-01-24 21:45:27 +03:00
|
|
|
// noCreate is provided as an optional argument to prevent the creation of
|
|
|
|
// non-existent special records, such as "mobile".
|
|
|
|
idForGUID: function idForGUID(guid, noCreate) {
|
|
|
|
if (kSpecialIds.isSpecialGUID(guid))
|
|
|
|
return kSpecialIds.specialIdForGUID(guid, !noCreate);
|
2010-12-07 02:51:38 +03:00
|
|
|
|
|
|
|
let stmt = this._idForGUIDStm;
|
|
|
|
// guid might be a String object rather than a string.
|
|
|
|
stmt.params.guid = guid.toString();
|
|
|
|
|
2011-01-25 23:37:23 +03:00
|
|
|
let results = Utils.queryAsync(stmt, ["item_id"]);
|
2011-02-07 22:25:47 +03:00
|
|
|
this._log.trace("Rows matching GUID " + guid + ": " +
|
|
|
|
results.map(function(x) x.item_id));
|
2011-01-25 23:37:23 +03:00
|
|
|
|
|
|
|
// Here's the one we care about: the first.
|
|
|
|
let result = results[0];
|
|
|
|
|
|
|
|
if (!result)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (!this._haveGUIDColumn) {
|
|
|
|
try {
|
|
|
|
// Assign new GUIDs to any that came later.
|
|
|
|
for (let i = 1; i < results.length; ++i) {
|
|
|
|
let surplus = results[i];
|
|
|
|
this._log.debug("Assigning new GUID to copied row " + surplus.item_id);
|
|
|
|
this._setGUID(surplus.item_id);
|
|
|
|
}
|
|
|
|
} catch (ex) {
|
|
|
|
// Just skip it and carry on. This shouldn't happen, but if it does we
|
|
|
|
// don't want to fail hard.
|
|
|
|
this._log.debug("Got exception assigning new GUIDs: " +
|
|
|
|
Utils.exceptionStr(ex));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result.item_id;
|
2009-10-17 03:19:28 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
_calculateIndex: function _calculateIndex(record) {
|
2010-11-10 00:53:50 +03:00
|
|
|
// Ensure folders have a very high sort index so they're not synced last.
|
|
|
|
if (record.type == "folder")
|
|
|
|
return FOLDER_SORTINDEX;
|
|
|
|
|
2009-10-17 03:19:28 +04:00
|
|
|
// For anything directly under the toolbar, give it a boost of more than an
|
|
|
|
// unvisited bookmark
|
|
|
|
let index = 0;
|
|
|
|
if (record.parentid == "toolbar")
|
|
|
|
index += 150;
|
|
|
|
|
|
|
|
// Add in the bookmark's frecency if we have something
|
|
|
|
if (record.bmkUri != null) {
|
2010-08-09 20:38:18 +04:00
|
|
|
this._frecencyStm.params.url = record.bmkUri;
|
|
|
|
let result = Utils.queryAsync(this._frecencyStm, ["frecency"]);
|
|
|
|
if (result.length)
|
2010-08-10 03:59:26 +04:00
|
|
|
index += result[0].frecency;
|
2009-10-17 03:19:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return index;
|
|
|
|
},
|
|
|
|
|
2009-08-14 05:50:54 +04:00
|
|
|
_getChildren: function BStore_getChildren(guid, items) {
|
2009-01-03 08:13:32 +03:00
|
|
|
let node = guid; // the recursion case
|
2011-01-24 21:45:27 +03:00
|
|
|
if (typeof(node) == "string") { // callers will give us the guid as the first arg
|
|
|
|
let nodeID = this.idForGUID(guid, true);
|
|
|
|
if (!nodeID) {
|
|
|
|
this._log.debug("No node for GUID " + guid + "; returning no children.");
|
|
|
|
return items;
|
|
|
|
}
|
|
|
|
node = this._getNode(nodeID);
|
|
|
|
}
|
|
|
|
|
2008-12-29 06:59:44 +03:00
|
|
|
if (node.type == node.RESULT_TYPE_FOLDER &&
|
|
|
|
!this._ls.isLivemark(node.itemId)) {
|
|
|
|
node.QueryInterface(Ci.nsINavHistoryQueryResultNode);
|
|
|
|
node.containerOpen = true;
|
2009-08-14 05:50:54 +04:00
|
|
|
|
|
|
|
// Remember all the children GUIDs and recursively get more
|
2008-12-29 06:59:44 +03:00
|
|
|
for (var i = 0; i < node.childCount; i++) {
|
|
|
|
let child = node.getChild(i);
|
2010-12-07 02:51:38 +03:00
|
|
|
items[this.GUIDForId(child.itemId)] = true;
|
2009-08-14 05:50:54 +04:00
|
|
|
this._getChildren(child, items);
|
2008-12-29 06:59:44 +03:00
|
|
|
}
|
|
|
|
}
|
2008-12-08 20:53:32 +03:00
|
|
|
|
2008-12-29 06:59:44 +03:00
|
|
|
return items;
|
2008-12-08 20:53:32 +03:00
|
|
|
},
|
|
|
|
|
2009-07-14 00:40:49 +04:00
|
|
|
_tagURI: function BStore_tagURI(bmkURI, tags) {
|
|
|
|
// Filter out any null/undefined/empty tags
|
|
|
|
tags = tags.filter(function(t) t);
|
|
|
|
|
|
|
|
// Temporarily tag a dummy uri to preserve tag ids when untagging
|
2009-07-14 04:43:15 +04:00
|
|
|
let dummyURI = Utils.makeURI("about:weave#BStore_tagURI");
|
2009-07-14 00:40:49 +04:00
|
|
|
this._ts.tagURI(dummyURI, tags);
|
|
|
|
this._ts.untagURI(bmkURI, null);
|
|
|
|
this._ts.tagURI(bmkURI, tags);
|
|
|
|
this._ts.untagURI(dummyURI, null);
|
|
|
|
},
|
2009-01-03 08:13:32 +03:00
|
|
|
|
|
|
|
getAllIDs: function BStore_getAllIDs() {
|
2010-12-07 02:51:56 +03:00
|
|
|
let items = {"menu": true,
|
|
|
|
"toolbar": true};
|
2011-01-24 21:45:27 +03:00
|
|
|
for each (let guid in kSpecialIds.guids) {
|
2009-08-15 11:59:49 +04:00
|
|
|
if (guid != "places" && guid != "tags")
|
|
|
|
this._getChildren(guid, items);
|
2011-01-24 21:45:27 +03:00
|
|
|
}
|
2009-01-03 00:49:19 +03:00
|
|
|
return items;
|
|
|
|
},
|
|
|
|
|
2008-06-03 23:38:48 +04:00
|
|
|
wipe: function BStore_wipe() {
|
2010-04-21 22:10:32 +04:00
|
|
|
// Save a backup before clearing out all bookmarks
|
|
|
|
archiveBookmarks();
|
2009-11-26 04:09:32 +03:00
|
|
|
|
2011-01-24 21:45:27 +03:00
|
|
|
for each (let guid in kSpecialIds.guids)
|
|
|
|
if (guid != "places") {
|
|
|
|
let id = kSpecialIds.specialIdForGUID(guid);
|
|
|
|
if (id)
|
|
|
|
this._bms.removeFolderChildren(id);
|
|
|
|
}
|
2008-06-03 23:38:48 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-02-12 02:29:15 +03:00
|
|
|
function BookmarksTracker(name) {
|
|
|
|
Tracker.call(this, name);
|
2010-02-12 02:25:31 +03:00
|
|
|
|
2010-08-06 19:30:58 +04:00
|
|
|
Svc.Obs.add("places-shutdown", this);
|
|
|
|
Svc.Obs.add("weave:engine:start-tracking", this);
|
|
|
|
Svc.Obs.add("weave:engine:stop-tracking", this);
|
2008-06-03 23:38:48 +04:00
|
|
|
}
|
|
|
|
BookmarksTracker.prototype = {
|
2008-11-07 06:18:46 +03:00
|
|
|
__proto__: Tracker.prototype,
|
2008-12-05 11:39:54 +03:00
|
|
|
|
2010-08-06 19:30:58 +04:00
|
|
|
_enabled: false,
|
|
|
|
observe: function observe(subject, topic, data) {
|
|
|
|
switch (topic) {
|
|
|
|
case "weave:engine:start-tracking":
|
|
|
|
if (!this._enabled) {
|
|
|
|
Svc.Bookmark.addObserver(this, true);
|
2011-01-25 10:06:42 +03:00
|
|
|
Svc.Obs.add("bookmarks-restore-begin", this);
|
|
|
|
Svc.Obs.add("bookmarks-restore-success", this);
|
|
|
|
Svc.Obs.add("bookmarks-restore-failed", this);
|
2010-08-06 19:30:58 +04:00
|
|
|
this._enabled = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "weave:engine:stop-tracking":
|
|
|
|
if (this._enabled) {
|
|
|
|
Svc.Bookmark.removeObserver(this);
|
2011-01-25 10:06:42 +03:00
|
|
|
Svc.Obs.remove("bookmarks-restore-begin", this);
|
|
|
|
Svc.Obs.remove("bookmarks-restore-success", this);
|
|
|
|
Svc.Obs.remove("bookmarks-restore-failed", this);
|
2010-08-06 19:30:58 +04:00
|
|
|
this._enabled = false;
|
|
|
|
}
|
|
|
|
// Fall through to clean up.
|
|
|
|
case "places-shutdown":
|
|
|
|
// Explicitly nullify our references to our cached services so
|
|
|
|
// we don't leak
|
|
|
|
this.__ls = null;
|
|
|
|
this.__bms = null;
|
|
|
|
break;
|
2011-01-25 10:06:42 +03:00
|
|
|
|
|
|
|
case "bookmarks-restore-begin":
|
|
|
|
this._log.debug("Ignoring changes from importing bookmarks.");
|
|
|
|
this.ignoreAll = true;
|
|
|
|
break;
|
|
|
|
case "bookmarks-restore-success":
|
|
|
|
this._log.debug("Tracking all items on successful import.");
|
|
|
|
this.ignoreAll = false;
|
|
|
|
|
|
|
|
this._log.debug("Restore succeeded: wiping server and other clients.");
|
|
|
|
Weave.Service.resetClient([this.name]);
|
|
|
|
Weave.Service.wipeServer([this.name]);
|
|
|
|
Weave.Service.prepCommand("wipeEngine", [this.name]);
|
|
|
|
break;
|
|
|
|
case "bookmarks-restore-failed":
|
|
|
|
this._log.debug("Tracking all items on failed import.");
|
|
|
|
this.ignoreAll = false;
|
|
|
|
break;
|
2010-08-06 19:30:58 +04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2010-08-04 16:50:44 +04:00
|
|
|
__bms: null,
|
2008-12-05 11:39:54 +03:00
|
|
|
get _bms() {
|
2010-08-04 16:50:44 +04:00
|
|
|
if (!this.__bms)
|
|
|
|
this.__bms = Cc["@mozilla.org/browser/nav-bookmarks-service;1"].
|
|
|
|
getService(Ci.nsINavBookmarksService);
|
|
|
|
return this.__bms;
|
2008-12-05 11:39:54 +03:00
|
|
|
},
|
2008-06-03 23:38:48 +04:00
|
|
|
|
2010-08-04 16:50:44 +04:00
|
|
|
__ls: null,
|
2009-01-16 00:58:59 +03:00
|
|
|
get _ls() {
|
2010-08-04 16:50:44 +04:00
|
|
|
if (!this.__ls)
|
|
|
|
this.__ls = Cc["@mozilla.org/browser/livemark-service;2"].
|
|
|
|
getService(Ci.nsILivemarkService);
|
|
|
|
return this.__ls;
|
2009-01-16 00:58:59 +03:00
|
|
|
},
|
|
|
|
|
2009-06-17 19:51:54 +04:00
|
|
|
QueryInterface: XPCOMUtils.generateQI([
|
|
|
|
Ci.nsINavBookmarkObserver,
|
2010-08-04 23:07:53 +04:00
|
|
|
Ci.nsINavBookmarkObserver_MOZILLA_1_9_1_ADDITIONS,
|
|
|
|
Ci.nsISupportsWeakReference
|
2009-06-17 19:51:54 +04:00
|
|
|
]),
|
2008-06-03 23:38:48 +04:00
|
|
|
|
2011-02-07 22:25:47 +03:00
|
|
|
_idForGUID: function _idForGUID(item_id) {
|
|
|
|
// Isn't indirection fun...
|
|
|
|
return Engines.get("bookmarks")._store.idForGUID(item_id);
|
|
|
|
},
|
|
|
|
|
2010-12-07 02:51:38 +03:00
|
|
|
_GUIDForId: function _GUIDForId(item_id) {
|
|
|
|
// Isn't indirection fun...
|
|
|
|
return Engines.get("bookmarks")._store.GUIDForId(item_id);
|
|
|
|
},
|
|
|
|
|
2009-06-17 19:51:54 +04:00
|
|
|
/**
|
|
|
|
* Add a bookmark (places) id to be uploaded and bump up the sync score
|
|
|
|
*
|
|
|
|
* @param itemId
|
|
|
|
* Places internal id of the bookmark to upload
|
|
|
|
*/
|
|
|
|
_addId: function BMT__addId(itemId) {
|
2011-02-07 22:25:47 +03:00
|
|
|
if (this.addChangedID(this._GUIDForId(itemId)))
|
2009-06-17 19:51:54 +04:00
|
|
|
this._upScore();
|
|
|
|
},
|
|
|
|
|
2008-11-20 03:21:12 +03:00
|
|
|
/* Every add/remove/change is worth 10 points */
|
2008-12-06 11:12:40 +03:00
|
|
|
_upScore: function BMT__upScore() {
|
2009-11-09 20:57:58 +03:00
|
|
|
this.score += 10;
|
2008-06-03 23:38:48 +04:00
|
|
|
},
|
2008-11-20 03:21:12 +03:00
|
|
|
|
2009-04-08 07:56:04 +04:00
|
|
|
/**
|
|
|
|
* Determine if a change should be ignored: we're ignoring everything or the
|
|
|
|
* folder is for livemarks
|
|
|
|
*
|
2009-06-17 19:51:54 +04:00
|
|
|
* @param itemId
|
|
|
|
* Item under consideration to ignore
|
|
|
|
* @param folder (optional)
|
2009-04-08 07:56:04 +04:00
|
|
|
* Folder of the item being changed
|
|
|
|
*/
|
2009-06-17 19:51:54 +04:00
|
|
|
_ignore: function BMT__ignore(itemId, folder) {
|
2009-04-08 07:56:04 +04:00
|
|
|
// Ignore unconditionally if the engine tells us to
|
|
|
|
if (this.ignoreAll)
|
|
|
|
return true;
|
|
|
|
|
2009-12-11 03:12:14 +03:00
|
|
|
// Ensure that the mobile bookmarks query is correct in the UI
|
|
|
|
this._ensureMobileQuery();
|
|
|
|
|
|
|
|
// Make sure to remove items that have the exclude annotation
|
|
|
|
if (Svc.Annos.itemHasAnnotation(itemId, "places/excludeFromBackup")) {
|
2011-02-07 22:25:47 +03:00
|
|
|
this.removeChangedID(this._GUIDForId(itemId));
|
2009-12-11 03:12:14 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-06-17 19:51:54 +04:00
|
|
|
// Get the folder id if we weren't given one
|
|
|
|
if (folder == null)
|
|
|
|
folder = this._bms.getFolderIdForItem(itemId);
|
|
|
|
|
2009-08-15 11:59:49 +04:00
|
|
|
let tags = kSpecialIds.tags;
|
2009-04-08 08:17:40 +04:00
|
|
|
// Ignore changes to tags (folders under the tags folder)
|
|
|
|
if (folder == tags)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Ignore tag items (the actual instance of a tag for a bookmark)
|
|
|
|
if (this._bms.getFolderIdForItem(folder) == tags)
|
|
|
|
return true;
|
|
|
|
|
2009-04-08 07:56:04 +04:00
|
|
|
// Ignore livemark children
|
|
|
|
return this._ls.isLivemark(folder);
|
|
|
|
},
|
|
|
|
|
2008-12-06 11:12:40 +03:00
|
|
|
onItemAdded: function BMT_onEndUpdateBatch(itemId, folder, index) {
|
2009-06-17 19:51:54 +04:00
|
|
|
if (this._ignore(itemId, folder))
|
2009-01-16 00:58:59 +03:00
|
|
|
return;
|
2009-02-02 22:43:06 +03:00
|
|
|
|
2008-12-19 22:48:09 +03:00
|
|
|
this._log.trace("onItemAdded: " + itemId);
|
2009-06-17 19:51:54 +04:00
|
|
|
this._addId(itemId);
|
2010-12-07 02:51:56 +03:00
|
|
|
this._addId(folder);
|
2008-12-06 11:12:40 +03:00
|
|
|
},
|
|
|
|
|
2009-06-17 19:51:54 +04:00
|
|
|
onBeforeItemRemoved: function BMT_onBeforeItemRemoved(itemId) {
|
2009-06-17 19:51:54 +04:00
|
|
|
if (this._ignore(itemId))
|
2009-01-16 00:58:59 +03:00
|
|
|
return;
|
2009-02-02 22:43:06 +03:00
|
|
|
|
2009-06-17 19:51:54 +04:00
|
|
|
this._log.trace("onBeforeItemRemoved: " + itemId);
|
|
|
|
this._addId(itemId);
|
2010-12-07 02:51:56 +03:00
|
|
|
let folder = Svc.Bookmark.getFolderIdForItem(itemId);
|
|
|
|
this._addId(folder);
|
2008-06-03 23:38:48 +04:00
|
|
|
},
|
2008-11-20 03:21:12 +03:00
|
|
|
|
2009-12-11 03:12:14 +03:00
|
|
|
_ensureMobileQuery: function _ensureMobileQuery() {
|
|
|
|
let anno = "PlacesOrganizer/OrganizerQuery";
|
|
|
|
let find = function(val) Svc.Annos.getItemsWithAnnotation(anno, {}).filter(
|
|
|
|
function(id) Utils.anno(id, anno) == val);
|
2009-02-02 22:43:06 +03:00
|
|
|
|
2009-12-11 03:12:14 +03:00
|
|
|
// Don't continue if the Library isn't ready
|
|
|
|
let all = find("AllBookmarks");
|
|
|
|
if (all.length == 0)
|
2009-08-26 03:15:05 +04:00
|
|
|
return;
|
2009-12-11 03:12:14 +03:00
|
|
|
|
|
|
|
// Disable handling of notifications while changing the mobile query
|
|
|
|
this.ignoreAll = true;
|
|
|
|
|
|
|
|
let mobile = find("MobileBookmarks");
|
|
|
|
let queryURI = Utils.makeURI("place:folder=" + kSpecialIds.mobile);
|
|
|
|
let title = Str.sync.get("mobile.label");
|
2009-12-16 01:21:13 +03:00
|
|
|
|
|
|
|
// Don't add OR do remove the mobile bookmarks if there's nothing
|
|
|
|
if (Svc.Bookmark.getIdForItemAt(kSpecialIds.mobile, 0) == -1) {
|
|
|
|
if (mobile.length != 0)
|
|
|
|
Svc.Bookmark.removeItem(mobile[0]);
|
|
|
|
}
|
|
|
|
// Add the mobile bookmarks query if it doesn't exist
|
|
|
|
else if (mobile.length == 0) {
|
2009-12-11 03:12:14 +03:00
|
|
|
let query = Svc.Bookmark.insertBookmark(all[0], queryURI, -1, title);
|
|
|
|
Utils.anno(query, anno, "MobileBookmarks");
|
|
|
|
Utils.anno(query, "places/excludeFromBackup", 1);
|
2009-08-26 03:15:05 +04:00
|
|
|
}
|
2009-12-11 03:12:14 +03:00
|
|
|
// Make sure the existing title is correct
|
|
|
|
else if (Svc.Bookmark.getItemTitle(mobile[0]) != title)
|
|
|
|
Svc.Bookmark.setItemTitle(mobile[0], title);
|
|
|
|
|
|
|
|
this.ignoreAll = false;
|
|
|
|
},
|
|
|
|
|
|
|
|
onItemChanged: function BMT_onItemChanged(itemId, property, isAnno, value) {
|
|
|
|
if (this._ignore(itemId))
|
|
|
|
return;
|
2009-08-26 03:15:05 +04:00
|
|
|
|
2011-02-07 22:25:47 +03:00
|
|
|
// Allocate a new GUID if necessary.
|
|
|
|
// We only want to do it if there's a dupe, so use idForGUID to achieve that.
|
|
|
|
if (isAnno && (property == GUID_ANNO)) {
|
|
|
|
this._log.trace("onItemChanged for " + GUID_ANNO +
|
|
|
|
": probably needs a new one.");
|
|
|
|
this._idForGUID(this._GUIDForId(itemId));
|
|
|
|
this._addId(itemId);
|
2011-02-08 04:49:42 +03:00
|
|
|
return;
|
2011-02-07 22:25:47 +03:00
|
|
|
}
|
|
|
|
|
2009-02-02 22:43:06 +03:00
|
|
|
// ignore annotations except for the ones that we sync
|
2009-06-17 03:16:39 +04:00
|
|
|
let annos = ["bookmarkProperties/description",
|
|
|
|
"bookmarkProperties/loadInSidebar", "bookmarks/staticTitle",
|
|
|
|
"livemark/feedURI", "livemark/siteURI", "microsummary/generatorURI"];
|
|
|
|
if (isAnno && annos.indexOf(property) == -1)
|
|
|
|
return;
|
2009-02-02 22:43:06 +03:00
|
|
|
|
2010-01-19 22:24:00 +03:00
|
|
|
// Ignore favicon changes to avoid unnecessary churn
|
|
|
|
if (property == "favicon")
|
|
|
|
return;
|
|
|
|
|
2009-01-14 02:55:35 +03:00
|
|
|
this._log.trace("onItemChanged: " + itemId +
|
2009-01-14 03:55:51 +03:00
|
|
|
(", " + property + (isAnno? " (anno)" : "")) +
|
2011-02-07 22:25:47 +03:00
|
|
|
(value ? (" = \"" + value + "\"") : ""));
|
2009-06-17 19:51:54 +04:00
|
|
|
this._addId(itemId);
|
2008-06-03 23:38:48 +04:00
|
|
|
},
|
|
|
|
|
2008-12-03 03:46:24 +03:00
|
|
|
onItemMoved: function BMT_onItemMoved(itemId, oldParent, oldIndex, newParent, newIndex) {
|
2009-06-17 19:51:54 +04:00
|
|
|
if (this._ignore(itemId))
|
2009-02-02 22:43:06 +03:00
|
|
|
return;
|
|
|
|
|
2008-12-06 11:12:40 +03:00
|
|
|
this._log.trace("onItemMoved: " + itemId);
|
2010-12-07 02:51:56 +03:00
|
|
|
this._addId(oldParent);
|
|
|
|
if (oldParent != newParent) {
|
|
|
|
this._addId(itemId);
|
|
|
|
this._addId(newParent);
|
|
|
|
}
|
2010-02-04 02:34:42 +03:00
|
|
|
|
|
|
|
// Remove any position annotations now that the user moved the item
|
|
|
|
Svc.Annos.removeItemAnnotation(itemId, PARENT_ANNO);
|
2008-11-20 03:21:12 +03:00
|
|
|
},
|
2008-06-03 23:38:48 +04:00
|
|
|
|
2008-11-20 03:21:12 +03:00
|
|
|
onBeginUpdateBatch: function BMT_onBeginUpdateBatch() {},
|
|
|
|
onEndUpdateBatch: function BMT_onEndUpdateBatch() {},
|
2009-06-17 19:51:54 +04:00
|
|
|
onItemRemoved: function BMT_onItemRemoved(itemId, folder, index) {},
|
2008-12-03 03:46:24 +03:00
|
|
|
onItemVisited: function BMT_onItemVisited(itemId, aVisitID, time) {}
|
2008-11-07 06:18:46 +03:00
|
|
|
};
|