Bug 406478 ? add dateCreated and lastModifiedTime to the transaction service (r=sspitzer, a=beltzner)
authordietrich@mozilla.com
Fri, 28 Dec 2007 13:48:37 -0800
changeset 9706 b02e6360a69e53ec077760612c89f51c6e15bef7
parent 9705 d1b21ff51425ebdf496ace7dfb418eb3717c20a6
child 9707 4165906564250c6f3a5f3988827804316b999ba2
push idunknown
push userunknown
push dateunknown
reviewerssspitzer, beltzner
bugs406478
milestone1.9b3pre
Bug 406478 ? add dateCreated and lastModifiedTime to the transaction service (r=sspitzer, a=beltzner)
browser/components/places/content/utils.js
browser/components/places/public/nsIPlacesTransactionsService.idl
browser/components/places/src/nsPlacesTransactionsService.js
browser/components/places/tests/unit/test_placesTxn.js
--- a/browser/components/places/content/utils.js
+++ b/browser/components/places/content/utils.js
@@ -664,19 +664,24 @@ var PlacesUtils = {
     var keyword = aData.keyword;
     var annos = aData.annos;
     if (aExcludeAnnotations) {
       annos =
         annos.filter(function(aValue, aIndex, aArray) {
                        return aExcludeAnnotations.indexOf(aValue.name) == -1;
                     });
     }
+    var childTxns = [];
+    if (aData.dateAdded)
+      childTxns.push(this.ptm.editItemDateAdded(null, aData.dateAdded));
+    if (aData.lastModified)
+      childTxns.push(this.ptm.editItemLastModified(null, aData.lastModified));
 
     return this.ptm.createItem(itemURL, aContainer, aIndex, itemTitle, keyword,
-                               annos);
+                               annos, childTxns);
   },
 
   /**
    * Gets a transaction for copying (recursively nesting to include children)
    * a folder (or container) and its contents from one folder to another.
    *
    * @param   aData
    *          Unwrapped dropped folder data - Obj containing folder and children
@@ -727,19 +732,22 @@ var PlacesUtils = {
         if (txn)
           childItemsTransactions.push(txn);
       }
       return childItemsTransactions;
     }
 
     var title = aData.folder.title;
     var annos = aData.folder.annos;
-
-    return this.ptm.createFolder(title, aContainer, aIndex, annos,
-                                 getChildItemsTransactions(aData.children));
+    var childItems = getChildItemsTransactions(aData.children);
+    if (aData.folder.dateAdded)
+      childItems.push(this.ptm.editItemDateAdded(null, aData.folder.dateAdded));
+    if (aData.folder.lastModified)
+      childItems.push(this.ptm.editItemLastModified(null, aData.folder.lastModified));
+    return this.ptm.createFolder(title, aContainer, aIndex, annos, childItems);
   },
 
   /**
    * Unwraps data from the Clipboard or the current Drag Session.
    * @param   blob
    *          A blob (string) of data, in some format we potentially know how
    *          to parse.
    * @param   type
--- a/browser/components/places/public/nsIPlacesTransactionsService.idl
+++ b/browser/components/places/public/nsIPlacesTransactionsService.idl
@@ -47,17 +47,17 @@ interface nsITransaction;
 
 /**
  * nsIPlacesTransactionService is a service designed to handle
  * nsITransactions that correspond to changes in Places. It is here as a
  * service so that we can keep the transactions around without holding onto
  * the global scope of a js window.
  */
 
-[scriptable, uuid(89f61a91-c8f7-4abb-b880-895cb9852c35)]
+[scriptable, uuid(310f22ff-35e3-44b2-8593-08a119933f7e)]
 interface nsIPlacesTransactionsService : nsITransactionManager
 {
   /**
    * Transaction for performing several Places Transactions in a single batch. 
    * 
    * @param aName
    *        title of the aggregate transactions
    * @param aTransactions
@@ -282,16 +282,40 @@ interface nsIPlacesTransactionsService :
    * @param aNewMicrosummary
    *        new microsummary for the bookmark
    * @returns nsITransaction object
    */
   nsITransaction editBookmarkMicrosummary(in long long aItemId,
                                           in nsIMicrosummary aNewMicrosummary);
 
   /**
+   * Transaction for editing an item's date added property.
+   *
+   * @param aItemId
+   *        id of the item to edit
+   * @param aNewDateAdded
+   *        new date added for the item 
+   * @returns nsITransaction object
+   */
+  nsITransaction editItemDateAdded(in long long aItemId,
+                                   in PRTime aNewDateAdded);
+
+  /**
+   * Transaction for editing an item's last modified time.
+   *
+   * @param aItemId
+   *        id of the item to edit
+   * @param aNewLastModified
+   *        new last modified date for the item 
+   * @returns nsITransaction object
+   */
+  nsITransaction editItemLastModified(in long long aItemId,
+                                      in PRTime aNewLastModified);
+
+  /**
    * Transaction for sorting a folder by name
    *
    * @param aFolderId
    *        id of the folder to sort
    * @oaram aFolderIndex
    *        index of the folder to sort
    * @returns nsITransaction object
    */
--- a/browser/components/places/src/nsPlacesTransactionsService.js
+++ b/browser/components/places/src/nsPlacesTransactionsService.js
@@ -131,16 +131,24 @@ placesTransactionsService.prototype = {
   editLivemarkFeedURI: function placesEditLvmkFeedURI(folderId, uri) {
     return new placesEditLivemarkFeedURITransactions(folderId, uri);
   },
 
   editBookmarkMicrosummary: function placesEditBkmkMicrosummary(aID, newMicrosummary) {
     return new placesEditBookmarkMicrosummaryTransactions(aID, newMicrosummary);
   },
 
+  editItemDateAdded: function placesEditItemDateAdded(aID, aNewDateAdded) {
+    return new placesEditItemDateAddedTransaction(aID, aNewDateAdded);
+  },
+
+  editItemLastModified: function placesEditItemLastModified(aID, aNewLastModified) {
+    return new placesEditItemLastModifiedTransaction(aID, aNewLastModified);
+  },
+
   sortFolderByName: function placesSortFldrByName(aFolderId, aFolderIndex) {
    return new placesSortFolderByNameTransactions(aFolderId, aFolderIndex);
   },
 
   // Update commands in the undo group of the active window
   // commands in inactive windows will are updated on-focus
   _updateCommands: function() {
     var wm = Cc["@mozilla.org/appshell/window-mediator;1"].
@@ -725,16 +733,64 @@ placesEditBookmarkMicrosummaryTransactio
   undoTransaction: function PEBMT_undoTransaction() {
     if (this._oldMicrosummary)
       this._mss.setMicrosummary(this.id, this._oldMicrosummary);
     else
       this._mss.removeMicrosummary(this.id);
   }
 };
 
+function placesEditItemDateAddedTransaction(id, newDateAdded) {
+  this.id = id;
+  this._newDateAdded = newDateAdded;
+  this._oldDateAdded = null;
+  this.redoTransaction = this.doTransaction;
+}
+
+placesEditItemDateAddedTransaction.prototype = {
+  __proto__: placesBaseTransaction.prototype,
+
+  // to support folders as well
+  get container() { return this.id; },
+  set container(val) { return this.id = val; },
+
+  doTransaction: function PEITT_doTransaction() {
+    this._oldDateAdded = PlacesUtils.bookmarks.getItemDateAdded(this.id);
+    PlacesUtils.bookmarks.setItemDateAdded(this.id, this._newDateAdded);
+  },
+
+  undoTransaction: function PEITT_undoTransaction() {
+    PlacesUtils.bookmarks.setItemDateAdded(this.id, this._oldDateAdded);
+  }
+};
+
+function placesEditItemLastModifiedTransaction(id, newLastModified) {
+  this.id = id;
+  this._newLastModified = newLastModified;
+  this._oldLastModified = null;
+  this.redoTransaction = this.doTransaction;
+}
+
+placesEditItemLastModifiedTransaction.prototype = {
+  __proto__: placesBaseTransaction.prototype,
+
+  // to support folders as well
+  get container() { return this.id; },
+  set container(val) { return this.id = val; },
+
+  doTransaction: function PEITT_doTransaction() {
+    this._oldLastModified = PlacesUtils.bookmarks.getItemLastModified(this.id);
+    PlacesUtils.bookmarks.setItemLastModified(this.id, this._newLastModified);
+  },
+
+  undoTransaction: function PEITT_undoTransaction() {
+    PlacesUtils.bookmarks.setItemLastModified(this.id, this._oldLastModified);
+  }
+};
+
 function placesSortFolderByNameTransactions(aFolderId, aFolderIndex) {
   this._folderId = aFolderId;
   this._folderIndex = aFolderIndex;
   this._oldOrder = null,
   this.redoTransaction = this.doTransaction;
 }
 
 placesSortFolderByNameTransactions.prototype = {
--- a/browser/components/places/tests/unit/test_placesTxn.js
+++ b/browser/components/places/tests/unit/test_placesTxn.js
@@ -390,9 +390,27 @@ function run_test() {
     ptSvc.createItem(postDataURI, root, -1, "postdata test", null, null, null));
   var postDataId = (bmsvc.getBookmarkIdsForURI(postDataURI,{}))[0];
   var postDataTxn = ptSvc.editBookmarkPostData(postDataId, postData);
   postDataTxn.doTransaction();
   do_check_true(annotationService.itemHasAnnotation(postDataId, POST_DATA_ANNO))
   do_check_eq(annotationService.getItemAnnotation(postDataId, POST_DATA_ANNO), postData);
   postDataTxn.undoTransaction();
   do_check_false(annotationService.itemHasAnnotation(postDataId, POST_DATA_ANNO))
+
+  // Test editing item date added
+  var oldAdded = bmsvc.getItemDateAdded(bkmk1Id);
+  var newAdded = Date.now();
+  var eidaTxn = ptSvc.editItemDateAdded(bkmk1Id, newAdded);
+  eidaTxn.doTransaction();
+  do_check_eq(newAdded, bmsvc.getItemDateAdded(bkmk1Id));
+  eidaTxn.undoTransaction();
+  do_check_eq(oldAdded, bmsvc.getItemDateAdded(bkmk1Id));
+
+  // Test editing item last modified 
+  var oldModified = bmsvc.getItemLastModified(bkmk1Id);
+  var newModified = Date.now();
+  var eilmTxn = ptSvc.editItemLastModified(bkmk1Id, newModified);
+  eilmTxn.doTransaction();
+  do_check_eq(newModified, bmsvc.getItemLastModified(bkmk1Id));
+  eilmTxn.undoTransaction();
+  do_check_eq(oldModified, bmsvc.getItemLastModified(bkmk1Id));
 }