2008-08-05 01:29:39 +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 Snowl.
|
|
|
|
*
|
|
|
|
* 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):
|
|
|
|
* Myk Melez <myk@mozilla.org>
|
2009-01-31 20:06:43 +03:00
|
|
|
* alta88 <alta88@gmail.com>
|
2008-08-05 01:29:39 +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 ***** */
|
|
|
|
|
2008-07-15 10:45:49 +04:00
|
|
|
const EXPORTED_SYMBOLS = ["SnowlIdentity", "SnowlPerson"];
|
|
|
|
|
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
const Cr = Components.results;
|
|
|
|
const Cu = Components.utils;
|
|
|
|
|
2009-05-13 04:47:02 +04:00
|
|
|
// modules that are generic
|
|
|
|
Cu.import("resource://snowl/modules/log4moz.js");
|
|
|
|
|
|
|
|
// modules that are Snowl-specific
|
2008-07-15 10:45:49 +04:00
|
|
|
Cu.import("resource://snowl/modules/datastore.js");
|
|
|
|
Cu.import("resource://snowl/modules/source.js");
|
|
|
|
Cu.import("resource://snowl/modules/URI.js");
|
|
|
|
|
2009-06-10 04:00:30 +04:00
|
|
|
// FIXME: make SnowlIdentity records have a source property
|
|
|
|
// referencing a source object rather than a sourceID property
|
|
|
|
// referencing a source object's ID.
|
2009-05-08 05:45:17 +04:00
|
|
|
|
2009-06-08 03:45:36 +04:00
|
|
|
function SnowlIdentity(id, sourceID, externalID, person) {
|
2008-07-15 10:45:49 +04:00
|
|
|
this.id = id;
|
2009-05-13 04:47:02 +04:00
|
|
|
this.sourceID = sourceID;
|
2008-07-15 10:45:49 +04:00
|
|
|
this.externalID = externalID;
|
2009-06-08 03:45:36 +04:00
|
|
|
this.person = person;
|
2008-07-15 10:45:49 +04:00
|
|
|
}
|
|
|
|
|
2009-05-13 04:47:02 +04:00
|
|
|
SnowlIdentity.__defineGetter__("_log", function() {
|
|
|
|
delete this._log;
|
|
|
|
return this._log = Log4Moz.repository.getLogger("Snowl.Identity");
|
|
|
|
});
|
|
|
|
|
|
|
|
SnowlIdentity.retrieve = function(id) {
|
|
|
|
this._log.debug("retrieving " + id);
|
|
|
|
|
|
|
|
let identity;
|
|
|
|
|
|
|
|
let statement = SnowlDatastore.createStatement(
|
|
|
|
"SELECT sourceID, externalID, personID FROM identities WHERE id = :id"
|
|
|
|
);
|
|
|
|
|
|
|
|
try {
|
|
|
|
statement.params.id = id;
|
|
|
|
if (statement.step()) {
|
|
|
|
identity = new SnowlIdentity(id,
|
|
|
|
statement.row.sourceID,
|
2009-05-14 05:01:14 +04:00
|
|
|
statement.row.externalID);
|
2009-05-13 04:47:02 +04:00
|
|
|
identity.person = SnowlPerson.retrieve(statement.row.personID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
finally {
|
|
|
|
statement.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return identity;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2009-05-08 05:45:17 +04:00
|
|
|
SnowlIdentity.prototype = {
|
|
|
|
id: null,
|
2009-05-13 04:47:02 +04:00
|
|
|
sourceID: null,
|
2009-05-08 05:45:17 +04:00
|
|
|
externalID: null,
|
|
|
|
person: null,
|
2008-07-15 10:45:49 +04:00
|
|
|
|
2009-05-13 04:47:02 +04:00
|
|
|
get _log() {
|
|
|
|
this.__defineGetter__("_log", function() SnowlIdentity._log);
|
|
|
|
return this._log;
|
|
|
|
},
|
|
|
|
|
2009-05-08 05:45:17 +04:00
|
|
|
persist: function() {
|
2009-05-21 05:39:36 +04:00
|
|
|
this._log.debug("persisting " + this.externalID + " from source " + this.sourceID);
|
2009-05-13 04:47:02 +04:00
|
|
|
|
2009-05-21 05:39:36 +04:00
|
|
|
this.person.persist(this);
|
2008-07-15 10:45:49 +04:00
|
|
|
|
2009-05-21 05:39:36 +04:00
|
|
|
if (!this.id)
|
|
|
|
this.id = this._getInternalID();
|
|
|
|
|
|
|
|
if (this.id) {
|
|
|
|
// FIXME: update the existing record as appropriate.
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
let statement = SnowlDatastore.createStatement(
|
|
|
|
"INSERT INTO identities ( sourceID, externalID, personID) " +
|
|
|
|
"VALUES (:sourceID, :externalID, :personID)"
|
|
|
|
);
|
|
|
|
|
|
|
|
try {
|
|
|
|
statement.params.sourceID = this.sourceID;
|
|
|
|
statement.params.externalID = this.externalID;
|
|
|
|
statement.params.personID = this.person.id;
|
|
|
|
statement.step();
|
|
|
|
this.id = SnowlDatastore.dbConnection.lastInsertRowID;
|
|
|
|
}
|
|
|
|
finally {
|
|
|
|
statement.reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
get _getInternalIDStmt() {
|
2009-05-08 05:45:17 +04:00
|
|
|
let statement = SnowlDatastore.createStatement(
|
2009-05-21 05:39:36 +04:00
|
|
|
"SELECT id FROM identities WHERE sourceID = :sourceID AND externalID = :externalID"
|
2009-05-08 05:45:17 +04:00
|
|
|
);
|
2009-05-21 05:39:36 +04:00
|
|
|
this.__defineGetter__("_getInternalIDStmt", function() statement);
|
|
|
|
return this._getInternalIDStmt;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the internal ID of the message.
|
|
|
|
*
|
|
|
|
* @returns {Number}
|
|
|
|
* the internal ID of the message, or undefined if the message
|
|
|
|
* doesn't exist in the datastore
|
|
|
|
*/
|
|
|
|
_getInternalID: function() {
|
|
|
|
this._log.debug("_getInternalID");
|
|
|
|
|
|
|
|
let internalID;
|
2009-05-08 05:45:17 +04:00
|
|
|
|
|
|
|
try {
|
2009-05-21 05:39:36 +04:00
|
|
|
this._getInternalIDStmt.params.sourceID = this.sourceID;
|
|
|
|
this._getInternalIDStmt.params.externalID = this.externalID;
|
|
|
|
if (this._getInternalIDStmt.step()) {
|
|
|
|
internalID = this._getInternalIDStmt.row["id"];
|
|
|
|
this._log.debug("got internal ID " + internalID);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this._log.debug("didn't find internal ID");
|
|
|
|
}
|
2009-05-08 05:45:17 +04:00
|
|
|
}
|
|
|
|
finally {
|
2009-05-21 05:39:36 +04:00
|
|
|
this._getInternalIDStmt.reset();
|
2008-07-15 10:45:49 +04:00
|
|
|
}
|
2009-05-21 05:39:36 +04:00
|
|
|
|
|
|
|
return internalID;
|
2008-07-15 10:45:49 +04:00
|
|
|
}
|
2009-05-08 05:45:17 +04:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
function SnowlPerson(id, name, placeID, homeURL, iconURL) {
|
|
|
|
this.id = id;
|
|
|
|
this.name = name;
|
|
|
|
this.placeID = placeID;
|
2009-05-13 04:47:02 +04:00
|
|
|
this.homeURL = homeURL;
|
|
|
|
this.iconURL = iconURL;
|
2009-05-08 05:45:17 +04:00
|
|
|
}
|
|
|
|
|
2009-05-13 04:47:02 +04:00
|
|
|
SnowlPerson.__defineGetter__("_log", function() {
|
|
|
|
delete this._log;
|
|
|
|
return this._log = Log4Moz.repository.getLogger("Snowl.Person");
|
|
|
|
});
|
|
|
|
|
|
|
|
SnowlPerson.retrieve = function(id) {
|
|
|
|
this._log.debug("retrieving " + id);
|
|
|
|
|
|
|
|
let person;
|
|
|
|
|
|
|
|
let statement = SnowlDatastore.createStatement(
|
|
|
|
"SELECT name, placeID, homeURL, iconURL FROM people WHERE id = :id"
|
|
|
|
);
|
|
|
|
|
|
|
|
try {
|
|
|
|
statement.params.id = id;
|
|
|
|
if (statement.step())
|
|
|
|
person = new SnowlPerson(id,
|
|
|
|
statement.row.name,
|
|
|
|
statement.row.placeID,
|
|
|
|
statement.row.homeURL,
|
|
|
|
statement.row.iconURL);
|
|
|
|
}
|
|
|
|
finally {
|
|
|
|
statement.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
return person;
|
|
|
|
};
|
|
|
|
|
2009-05-08 05:45:17 +04:00
|
|
|
SnowlPerson.prototype = {
|
2009-05-13 04:47:02 +04:00
|
|
|
id: null,
|
2009-05-08 05:45:17 +04:00
|
|
|
name: null,
|
|
|
|
placeID: null,
|
|
|
|
homeURL: null,
|
|
|
|
iconURL: null,
|
|
|
|
|
2009-05-13 04:47:02 +04:00
|
|
|
get _log() {
|
|
|
|
this.__defineGetter__("_log", function() SnowlPerson._log);
|
|
|
|
return this._log;
|
|
|
|
},
|
|
|
|
|
2009-05-21 05:39:36 +04:00
|
|
|
persist: function(identity) {
|
2009-05-13 04:47:02 +04:00
|
|
|
this._log.debug("persisting " + this.name);
|
|
|
|
|
2009-05-21 05:39:36 +04:00
|
|
|
if (!this.id) {
|
|
|
|
[this.id, this.placeID] = this._getIDs(identity);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.id) {
|
|
|
|
// FIXME: update the existing record as appropriate.
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
let statement = SnowlDatastore.createStatement(
|
|
|
|
"INSERT INTO people ( name, homeURL, iconURL) " +
|
|
|
|
"VALUES (:name, :homeURL, :iconURL)"
|
|
|
|
);
|
|
|
|
|
|
|
|
try {
|
|
|
|
statement.params.name = this.name;
|
|
|
|
statement.params.homeURL = this.homeURL;
|
|
|
|
statement.params.iconURL = this.iconURL;
|
|
|
|
statement.step();
|
|
|
|
this.id = SnowlDatastore.dbConnection.lastInsertRowID;
|
|
|
|
|
|
|
|
// XXX lookup favicon in collections table rather than hardcoding
|
|
|
|
let iconURI =
|
|
|
|
this.iconURL ? URI.get(this.iconURL) :
|
|
|
|
this.homeURL ? SnowlSource.faviconSvc.getFaviconForPage(this.homeURL) :
|
|
|
|
URI.get("chrome://snowl/skin/person-16.png");
|
|
|
|
|
|
|
|
// Create places record, placeID stored into people table record.
|
|
|
|
//SnowlPlaces._log.info("Author name:iconURI.spec - " + name + " : " + iconURI.spec);
|
|
|
|
// FIXME: break the dependency on the identity's sourceID and externalID,
|
|
|
|
// since those are attributes of identities, not people.
|
|
|
|
this.placeID = SnowlPlaces.persistPlace("people",
|
|
|
|
this.id,
|
|
|
|
this.name,
|
|
|
|
this.homeURL,
|
|
|
|
identity.externalID,
|
|
|
|
iconURI,
|
|
|
|
identity.sourceID);
|
|
|
|
// Store placeID back into messages for DB integrity.
|
|
|
|
SnowlDatastore.dbConnection.executeSimpleSQL(
|
|
|
|
"UPDATE people " +
|
|
|
|
"SET placeID = " + this.placeID +
|
|
|
|
" WHERE id = " + this.id);
|
|
|
|
}
|
|
|
|
finally {
|
|
|
|
statement.reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
get _getIDsStmt() {
|
2009-05-08 05:45:17 +04:00
|
|
|
let statement = SnowlDatastore.createStatement(
|
2009-05-21 05:39:36 +04:00
|
|
|
"SELECT people.id AS id, people.placeID as placeID " +
|
|
|
|
"FROM identities JOIN people ON identities.personID = people.id " +
|
|
|
|
"WHERE identities.sourceID = :sourceID " +
|
|
|
|
"AND identities.externalID = :externalID"
|
2009-05-08 05:45:17 +04:00
|
|
|
);
|
2009-05-21 05:39:36 +04:00
|
|
|
this.__defineGetter__("_getIDsStmt", function() statement);
|
|
|
|
return this._getIDsStmt;
|
|
|
|
},
|
2009-05-08 05:45:17 +04:00
|
|
|
|
2009-05-21 05:39:36 +04:00
|
|
|
/**
|
|
|
|
* Get the person's internal ID and place ID.
|
|
|
|
*
|
|
|
|
* @returns {Array}
|
|
|
|
* the internal and place IDs of the person, if any
|
|
|
|
*/
|
|
|
|
_getIDs: function(identity) {
|
|
|
|
let id, placeID;
|
2009-05-08 05:45:17 +04:00
|
|
|
try {
|
2009-05-21 05:39:36 +04:00
|
|
|
this._getIDsStmt.params.sourceID = identity.sourceID;
|
|
|
|
this._getIDsStmt.params.externalID = identity.externalID;
|
|
|
|
if (this._getIDsStmt.step()) {
|
|
|
|
id = this._getIDsStmt.row["id"];
|
|
|
|
placeID = this._getIDsStmt.row["placeID"];
|
|
|
|
}
|
2009-05-08 05:45:17 +04:00
|
|
|
}
|
|
|
|
finally {
|
2009-05-21 05:39:36 +04:00
|
|
|
this._getIDsStmt.reset();
|
2009-05-08 05:45:17 +04:00
|
|
|
}
|
2009-05-21 05:39:36 +04:00
|
|
|
|
|
|
|
return [id, placeID];
|
2008-07-15 10:45:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2009-05-08 05:45:17 +04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//SnowlIdentity.get = function(sourceID, externalID) {
|
|
|
|
// let identity;
|
|
|
|
//
|
|
|
|
// let statement = SnowlDatastore.createStatement(
|
|
|
|
// "SELECT id, personID " +
|
|
|
|
// "FROM identities " +
|
|
|
|
// "WHERE externalID = :externalID AND sourceID = :sourceID"
|
|
|
|
// );
|
|
|
|
//
|
|
|
|
// try {
|
|
|
|
// statement.params.sourceID = sourceID;
|
|
|
|
// statement.params.externalID = externalID;
|
|
|
|
// if (statement.step()) {
|
|
|
|
// identity = new SnowlIdentity(statement.row.id,
|
|
|
|
// sourceID,
|
|
|
|
// externalID,
|
|
|
|
// statement.row.personID);
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// finally {
|
|
|
|
// statement.reset();
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// return identity;
|
|
|
|
//};
|
|
|
|
|
2008-07-23 11:45:57 +04:00
|
|
|
SnowlIdentity.create = function(sourceID, externalID, name, homeURL, iconURL) {
|
2008-07-15 10:45:49 +04:00
|
|
|
let identity;
|
|
|
|
|
|
|
|
let personStatement = SnowlDatastore.createStatement(
|
2009-01-31 20:06:43 +03:00
|
|
|
"INSERT INTO people (name, homeURL, iconURL, placeID) " +
|
|
|
|
"VALUES (:name, :homeURL, :iconURL, :placeID)"
|
2008-07-15 10:45:49 +04:00
|
|
|
);
|
|
|
|
|
|
|
|
let identityStatement = SnowlDatastore.createStatement(
|
|
|
|
"INSERT INTO identities (sourceID, externalID, personID) " +
|
2009-01-31 20:06:43 +03:00
|
|
|
"VALUES (:sourceID, :externalID, :personID)"
|
2008-07-15 10:45:49 +04:00
|
|
|
);
|
|
|
|
|
|
|
|
try {
|
|
|
|
personStatement.params.name = name;
|
2008-07-28 00:50:16 +04:00
|
|
|
personStatement.params.homeURL = homeURL || null;
|
|
|
|
personStatement.params.iconURL = iconURL || null;
|
2008-07-15 10:45:49 +04:00
|
|
|
personStatement.step();
|
|
|
|
let personID = SnowlDatastore.dbConnection.lastInsertRowID;
|
|
|
|
|
2009-01-29 23:19:56 +03:00
|
|
|
// XXX lookup favicon in collections table rather than hardcoding
|
|
|
|
let iconURI =
|
|
|
|
iconURL ? URI.get(iconURL) :
|
|
|
|
homeURL ? SnowlSource.faviconSvc.getFaviconForPage(homeURL) :
|
|
|
|
URI.get("chrome://snowl/skin/person-16.png");
|
|
|
|
|
2009-01-31 20:06:43 +03:00
|
|
|
// Create places record, placeID stored into people table record.
|
2009-01-29 23:19:56 +03:00
|
|
|
//SnowlPlaces._log.info("Author name:iconURI.spec - " + name + " : " + iconURI.spec);
|
2009-01-31 20:06:43 +03:00
|
|
|
let placeID = SnowlPlaces.persistPlace("people",
|
|
|
|
personID,
|
|
|
|
name,
|
|
|
|
null, // homeURL,
|
2009-03-02 01:43:47 +03:00
|
|
|
externalID, // externalID,
|
2009-01-31 20:06:43 +03:00
|
|
|
iconURI,
|
|
|
|
sourceID);
|
|
|
|
// Store placeID back into messages for db integrity
|
|
|
|
SnowlDatastore.dbConnection.executeSimpleSQL(
|
|
|
|
"UPDATE people " +
|
|
|
|
"SET placeID = " + placeID +
|
|
|
|
" WHERE id = " + personID);
|
2009-01-29 23:19:56 +03:00
|
|
|
|
2008-07-15 10:45:49 +04:00
|
|
|
identityStatement.params.sourceID = sourceID;
|
|
|
|
identityStatement.params.externalID = externalID;
|
|
|
|
identityStatement.params.personID = personID;
|
|
|
|
identityStatement.step();
|
|
|
|
let identityID = SnowlDatastore.dbConnection.lastInsertRowID;
|
|
|
|
|
|
|
|
identity = new SnowlIdentity(identityID, sourceID, externalID, personID);
|
|
|
|
}
|
|
|
|
finally {
|
|
|
|
personStatement.reset();
|
|
|
|
identityStatement.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
return identity;
|
|
|
|
};
|
|
|
|
|
|
|
|
SnowlPerson.__defineGetter__("_getAllStatement",
|
|
|
|
function() {
|
|
|
|
let statement = SnowlDatastore.createStatement(
|
2009-07-13 06:28:35 +04:00
|
|
|
"SELECT id, name, placeID FROM people ORDER BY name COLLATE NOCASE"
|
2008-07-15 10:45:49 +04:00
|
|
|
);
|
|
|
|
this.__defineGetter__("_getAllStatement", function() { return statement });
|
|
|
|
return this._getAllStatement;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
SnowlPerson.getAll = function() {
|
|
|
|
let people = [];
|
|
|
|
|
|
|
|
let statement = this._getAllStatement;
|
|
|
|
|
|
|
|
try {
|
|
|
|
while (statement.step())
|
2009-01-24 01:56:04 +03:00
|
|
|
people.push(new SnowlPerson(statement.row.id,
|
|
|
|
statement.row.name,
|
|
|
|
statement.row.placeID));
|
2008-07-15 10:45:49 +04:00
|
|
|
}
|
|
|
|
finally {
|
|
|
|
statement.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
return people;
|
|
|
|
}
|