2008-11-19 16:15:34 -08: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 Weave.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Mozilla.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2008
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Dan Mills <thunder@mozilla.com>
|
|
|
|
*
|
|
|
|
* 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 ***** */
|
|
|
|
|
|
|
|
const EXPORTED_SYMBOLS = ['Collection'];
|
|
|
|
|
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
const Cr = Components.results;
|
|
|
|
const Cu = Components.utils;
|
|
|
|
|
2010-06-16 14:30:08 -07:00
|
|
|
Cu.import("resource://services-sync/resource.js");
|
|
|
|
Cu.import("resource://services-sync/util.js");
|
2008-11-19 16:15:34 -08:00
|
|
|
|
2009-01-06 13:54:18 -08:00
|
|
|
function Collection(uri, recordObj) {
|
2010-02-11 15:25:31 -08:00
|
|
|
Resource.call(this, uri);
|
2009-01-06 13:54:18 -08:00
|
|
|
this._recordObj = recordObj;
|
2010-02-11 15:25:31 -08:00
|
|
|
|
|
|
|
this._full = false;
|
|
|
|
this._ids = null;
|
|
|
|
this._limit = 0;
|
|
|
|
this._older = 0;
|
|
|
|
this._newer = 0;
|
|
|
|
this._data = [];
|
2008-11-19 16:15:34 -08:00
|
|
|
}
|
|
|
|
Collection.prototype = {
|
|
|
|
__proto__: Resource.prototype,
|
|
|
|
_logName: "Collection",
|
|
|
|
|
|
|
|
_rebuildURL: function Coll__rebuildURL() {
|
|
|
|
// XXX should consider what happens if it's not a URL...
|
|
|
|
this.uri.QueryInterface(Ci.nsIURL);
|
|
|
|
|
|
|
|
let args = [];
|
2009-02-03 15:54:30 -08:00
|
|
|
if (this.older)
|
|
|
|
args.push('older=' + this.older);
|
2009-02-10 00:52:48 -08:00
|
|
|
else if (this.newer) {
|
2009-02-10 00:12:11 -08:00
|
|
|
args.push('newer=' + this.newer);
|
2009-02-10 00:52:48 -08:00
|
|
|
}
|
2008-11-19 16:15:34 -08:00
|
|
|
if (this.full)
|
|
|
|
args.push('full=1');
|
2008-12-19 11:48:09 -08:00
|
|
|
if (this.sort)
|
|
|
|
args.push('sort=' + this.sort);
|
2009-08-31 18:30:44 -07:00
|
|
|
if (this.ids != null)
|
|
|
|
args.push("ids=" + this.ids);
|
2010-03-19 11:35:01 -07:00
|
|
|
if (this.limit > 0 && this.limit != Infinity)
|
2009-09-10 14:18:31 -07:00
|
|
|
args.push("limit=" + this.limit);
|
2008-11-19 16:15:34 -08:00
|
|
|
|
|
|
|
this.uri.query = (args.length > 0)? '?' + args.join('&') : '';
|
|
|
|
},
|
|
|
|
|
|
|
|
// get full items
|
|
|
|
get full() { return this._full; },
|
|
|
|
set full(value) {
|
|
|
|
this._full = value;
|
|
|
|
this._rebuildURL();
|
|
|
|
},
|
|
|
|
|
2009-08-31 18:30:44 -07:00
|
|
|
// Apply the action to a certain set of ids
|
|
|
|
get ids() this._ids,
|
|
|
|
set ids(value) {
|
|
|
|
this._ids = value;
|
|
|
|
this._rebuildURL();
|
|
|
|
},
|
|
|
|
|
2009-09-10 14:18:31 -07:00
|
|
|
// Limit how many records to get
|
|
|
|
get limit() this._limit,
|
|
|
|
set limit(value) {
|
|
|
|
this._limit = value;
|
|
|
|
this._rebuildURL();
|
|
|
|
},
|
|
|
|
|
2009-02-10 00:12:11 -08:00
|
|
|
// get only items modified before some date
|
2009-02-03 15:54:30 -08:00
|
|
|
get older() { return this._older; },
|
|
|
|
set older(value) {
|
|
|
|
this._older = value;
|
2008-11-19 16:15:34 -08:00
|
|
|
this._rebuildURL();
|
|
|
|
},
|
|
|
|
|
2009-02-10 00:12:11 -08:00
|
|
|
// get only items modified since some date
|
|
|
|
get newer() { return this._newer; },
|
|
|
|
set newer(value) {
|
|
|
|
this._newer = value;
|
|
|
|
this._rebuildURL();
|
|
|
|
},
|
|
|
|
|
2008-12-19 17:00:12 -08:00
|
|
|
// get items sorted by some criteria. valid values:
|
|
|
|
// oldest (oldest first)
|
|
|
|
// newest (newest first)
|
2008-12-19 11:48:09 -08:00
|
|
|
// index
|
|
|
|
get sort() { return this._sort; },
|
|
|
|
set sort(value) {
|
|
|
|
this._sort = value;
|
|
|
|
this._rebuildURL();
|
|
|
|
},
|
|
|
|
|
2009-01-27 13:35:10 -08:00
|
|
|
pushData: function Coll_pushData(data) {
|
|
|
|
this._data.push(data);
|
2008-12-23 11:19:33 -08:00
|
|
|
},
|
|
|
|
|
2008-11-19 16:15:34 -08:00
|
|
|
clearRecords: function Coll_clearRecords() {
|
|
|
|
this._data = [];
|
|
|
|
},
|
|
|
|
|
2009-07-22 16:38:34 -07:00
|
|
|
set recordHandler(onRecord) {
|
|
|
|
// Save this because onProgress is called with this as the ChannelListener
|
|
|
|
let coll = this;
|
|
|
|
|
2009-08-26 18:09:41 -07:00
|
|
|
// Switch to newline separated records for incremental parsing
|
|
|
|
coll.setHeader("Accept", "application/newlines");
|
|
|
|
|
2009-07-22 16:38:34 -07:00
|
|
|
this._onProgress = function() {
|
2009-08-26 18:09:41 -07:00
|
|
|
let newline;
|
|
|
|
while ((newline = this._data.indexOf("\n")) > 0) {
|
|
|
|
// Split the json record from the rest of the data
|
|
|
|
let json = this._data.slice(0, newline);
|
|
|
|
this._data = this._data.slice(newline + 1);
|
2009-07-22 16:38:34 -07:00
|
|
|
|
|
|
|
// Deserialize a record from json and give it to the callback
|
|
|
|
let record = new coll._recordObj();
|
|
|
|
record.deserialize(json);
|
2010-03-16 16:31:56 -07:00
|
|
|
record.baseUri = coll.uri;
|
2009-07-22 16:38:34 -07:00
|
|
|
onRecord(record);
|
2009-08-26 18:09:41 -07:00
|
|
|
}
|
2009-07-22 16:38:34 -07:00
|
|
|
};
|
2008-11-19 16:15:34 -08:00
|
|
|
}
|
2009-03-31 12:09:38 -07:00
|
|
|
};
|