/* Copyright 2008-2013 Clipperz Srl This file is part of Clipperz, the online password manager. For further information about its features and functionalities please refer to http://www.clipperz.com. * Clipperz is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. * Clipperz is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. * You should have received a copy of the GNU Affero General Public License along with Clipperz. If not, see http://www.gnu.org/licenses/. */ if (typeof(Clipperz) == 'undefined') { Clipperz = {}; } if (typeof(Clipperz.PM) == 'undefined') { Clipperz.PM = {}; } if (typeof(Clipperz.PM.DataModel) == 'undefined') { Clipperz.PM.DataModel = {}; } Clipperz.PM.DataModel.Record = function(args) { Clipperz.PM.DataModel.Record.superclass.constructor.apply(this, arguments); this._updateDate = (args.updateDate ? Clipperz.PM.Date.parse(args.updateDate) : Clipperz.Base.exception.raise('MandatoryParameter')); this._retrieveIndexDataFunction = args.retrieveIndexDataFunction || Clipperz.Base.exception.raise('MandatoryParameter'); this._updateIndexDataFunction = args.updateIndexDataFunction || Clipperz.Base.exception.raise('MandatoryParameter'); this._retrieveDirectLoginIndexDataFunction = args.retrieveDirectLoginIndexDataFunction || null; this._setDirectLoginIndexDataFunction = args.setDirectLoginIndexDataFunction || null; this._removeDirectLoginIndexDataFunction = args.removeDirectLoginIndexDataFunction || null; this._createNewDirectLoginFunction = args.createNewDirectLoginFunction || null; this._directLogins = {}; this._versions = {}; this._currentRecordVersion = null; if (this.isBrandNew()) { var newVersion; this.setNotes(''); newVersion = new Clipperz.PM.DataModel.Record.Version({ 'retrieveKeyFunction': MochiKit.Base.method(this, 'getVersionKey'), 'getVersion': MochiKit.Base.method(this, 'getVersion') }); this._versions[newVersion.reference()] = newVersion; this._currentVersionReference = newVersion.reference(); // this.setLabel(''); } return this; } Clipperz.Base.extend(Clipperz.PM.DataModel.Record, Clipperz.PM.DataModel.EncryptedRemoteObject, { 'toString': function() { return "Record (" + this.reference() + ")"; }, //------------------------------------------------------------------------- 'reference': function () { return this._reference; }, //========================================================================= 'getIndexData': function () { return this._retrieveIndexDataFunction(this.reference()); }, //......................................................................... 'getIndexDataForKey': function (aKey) { return Clipperz.Async.callbacks("Record.getIndexDataForKey", [ MochiKit.Base.method(this, 'getIndexData'), MochiKit.Base.itemgetter(aKey) ], {trace:false}); }, //------------------------------------------------------------------------- 'setIndexDataForKey': function (aKey, aValue) { // return this._updateIndexDataFunction(this.reference(), aKey, aValue); var deferredResult; deferredResult = new Clipperz.Async.Deferred("Record.setIndexDataForKey", {trace:false}); deferredResult.addMethod(this, 'getIndexDataForKey', aKey); deferredResult.addCallback(MochiKit.Base.bind(function (aCurrentValue) { var result; var originalValue; originalValue = this.transientState().getValue('originalValues.indexData.' + aKey); if (originalValue == null) { originalValue = this.transientState().setValue('originalValues.indexData.' + aKey, aCurrentValue); } if (aCurrentValue != aValue) { if (originalValue != aValue) { this.transientState().setValue('hasPendingChanges.indexData.' + aKey, true); } else { this.transientState().setValue('hasPendingChanges.indexData.' + aKey, false); } result = this._updateIndexDataFunction(this.reference(), aKey, aValue); } else { result = MochiKit.Async.succeed(aValue); } return result; }, this)); deferredResult.callback(); return deferredResult; }, //========================================================================= /* 'key': function () { return this.getIndexDataForKey('key'); }, */ //========================================================================= 'label': function () { return this.getIndexDataForKey('label'); }, //......................................................................... 'setLabel': function (aValue) { return this.setIndexDataForKey('label', aValue); }, //========================================================================= 'headerNotes': function () { return this.getIndexDataForKey('notes'); }, //------------------------------------------------------------------------- 'notes': function () { return Clipperz.Async.callbacks("Record.notes", [ MochiKit.Base.method(this, 'headerNotes'), MochiKit.Base.bind(function (someHeaderNotes) { var result; if ((someHeaderNotes == null) || (typeof(someHeaderNotes) == 'undefined')) { result = this.getValue('notes'); } else { result = MochiKit.Async.succeed(someHeaderNotes); } return result; }, this) ], {trace:false}); }, //......................................................................... 'setNotes': function (aValue) { return this.setValue('notes', aValue); }, //========================================================================= 'updateDate': function () { return MochiKit.Async.succeed(this._updateDate); }, //========================================================================= 'favicon': function () { var result; var directLogins; directLogins = MochiKit.Base.values(this.directLogins()); if (directLogins.length > 0) { result = directLogins[0].favicon(); // } else if (/* is there an URL to use for searching a favicon */){ } else { result = null; // MochiKit.Async.succeed(Clipperz.PM.Strings['defaultFaviconUrl']); } return result; }, //------------------------------------------------------------------------- 'searchableContent': function () { var deferredResult; deferredResult = new Clipperz.Async.Deferred("Record.searchableContent", {trace:false}); deferredResult.collectResults({ 'recordLabel': MochiKit.Base.method(this, 'label'), 'directLoginLabels': [ MochiKit.Base.method(this, 'directLoginReferences'), MochiKit.Base.partial(MochiKit.Base.map, MochiKit.Base.itemgetter('label')) ] }) deferredResult.addCallback(function (someValues) { return someValues['recordLabel'] + ' ' + someValues['directLoginLabels'].join(' '); }); deferredResult.callback(); return deferredResult; }, //------------------------------------------------------------------------- 'isMatching': function (aRegExp) { return Clipperz.Async.callbacks("deferredFilterFunction", [ MochiKit.Base.method(this, 'searchableContent'), MochiKit.Base.method(aRegExp, 'test'), function (doesItMatch) { var result; if (doesItMatch) { result = MochiKit.Async.succeed('match'); } else { result = MochiKit.Async.fail('miss'); } return result; } ], {trace:false}); }, //========================================================================= 'content': function () { var deferredResult; var result; result = { 'fields': [], 'directLogins': [] }; deferredResult = new Clipperz.Async.Deferred("Record.content", {trace:false}); deferredResult.addMethod(this, 'reference'); deferredResult.addCallback(function (aValue) { result['reference'] = aValue; }); deferredResult.addMethod(this, 'label'); deferredResult.addCallback(function (aValue) { result['title'] = aValue; }); deferredResult.addMethod(this, 'notes'); deferredResult.addCallback(function (aValue) { result['notes'] = aValue; }); deferredResult.addMethod(this, 'fields'); deferredResult.addCallback(MochiKit.Base.values); deferredResult.addCallback(MochiKit.Base.map, MochiKit.Base.methodcaller('content')); deferredResult.addCallback(Clipperz.Async.collectAll); deferredResult.addCallback(MochiKit.Base.map, function (aValue) { result['fields'].push(aValue); }); deferredResult.addMethod(this, 'directLogins'); deferredResult.addCallback(MochiKit.Base.values); deferredResult.addCallback(MochiKit.Base.map, MochiKit.Base.methodcaller('content')); deferredResult.addCallback(Clipperz.Async.collectAll); deferredResult.addCallback(MochiKit.Base.map, function (aValue) { result['directLogins'].push(aValue); }); deferredResult.addCallback(function () { return result; }); deferredResult.callback(); return deferredResult; }, //========================================================================= 'directLogins': function () { return this._directLogins; }, 'addDirectLogin': function (aDirectLogin) { this._directLogins[aDirectLogin.reference()] = aDirectLogin; }, 'directLoginWithReference': function (aDirectLoginReference) { return this._directLogins[aDirectLoginReference]; }, 'createNewDirectLoginFunction': function () { return this._createNewDirectLoginFunction; }, 'saveOriginalDirectLoginStatusToTransientState': function () { if (this.transientState().getValue('directLogins') == null) { // this.transientState().setValue('directLogins', this._directLogins) MochiKit.Iter.forEach(MochiKit.Base.keys(this._directLogins), MochiKit.Base.bind(function(aKey) { this.transientState().setValue('directLogins' + '.' + aKey, this._directLogins[aKey]) }, this)) } }, 'createNewDirectLogin': function () { this.saveOriginalDirectLoginStatusToTransientState(); return this.createNewDirectLoginFunction()(this); }, 'removeDirectLogin': function(aDirectLogin) { this.saveOriginalDirectLoginStatusToTransientState(); return Clipperz.Async.callbacks("Record.removeDirectLogin", [ MochiKit.Base.method(this, 'removeValue', 'directLogins' + '.' + aDirectLogin.reference()), MochiKit.Base.bind(function () { delete this._directLogins[aDirectLogin.reference()] }, this) ], {trace:false}); }, 'directLoginReferences': function () { var result; result = Clipperz.Async.callbacks("Record.directLoginReferences", [ MochiKit.Base.method(this, 'directLogins'), MochiKit.Base.values, function (someDirectLogins) { var result; var i,c; result = []; c = someDirectLogins.length; for (i=0; i