Bug 463060 - Clean-up and move clipboard.js. r=philipp
authorMartin Schroeder <mschroeder@mozilla.x-home.org>
Thu, 06 Nov 2008 12:17:22 +0100
changeset 1030 020882e3433b905fda85bca60e8b5701bfb4300a
parent 1029 36cd2272f995a921f7096c20a84168f2ff3b2ad0
child 1031 5748fe6d63df4fcd393fc01fa91c14cfc6451fa4
push idunknown
push userunknown
push dateunknown
reviewersphilipp
bugs463060
Bug 463060 - Clean-up and move clipboard.js. r=philipp
calendar/base/content/calendar-clipboard.js
calendar/base/content/calendar-scripts.inc
calendar/base/jar.mn
calendar/lightning/jar.mn
calendar/resources/content/clipboard.js
calendar/resources/jar.mn
rename from calendar/resources/content/clipboard.js
rename to calendar/base/content/calendar-clipboard.js
--- a/calendar/resources/content/clipboard.js
+++ b/calendar/base/content/calendar-clipboard.js
@@ -13,284 +13,235 @@
  *
  * The Original Code is Mozilla Calendar code.
  *
  * The Initial Developer of the Original Code is
  * ArentJan Banck <ajbanck@planet.nl>.
  * Portions created by the Initial Developer are Copyright (C) 2002
  * the Initial Developer. All Rights Reserved.
  *
- * Contributor(s): ArentJan Banck <ajbanck@planet.nl>
- *                 Joey Minta <jminta@gmail.com>
- *                 Philipp Kewisch <mozilla@kewis.ch>
+ * Contributor(s):
+ *   Joey Minta <jminta@gmail.com>
+ *   Philipp Kewisch <mozilla@kewis.ch>
+ *   Martin Schroeder <mschroeder@mozilla.x-home.org>
  *
  * 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 ***** */
 
-/***** calendarClipboard
-*
-* NOTES 
-*   TODO items
-*     - Add a clipboard listener, to enable/disable menu-items depending if 
-*       valid clipboard data is available.
-*
-******/
+Components.utils.import("resource://calendar/modules/calUtils.jsm");
 
-
-function getClipboard()
-{
-    const kClipboardContractID = "@mozilla.org/widget/clipboard;1";
-    const kClipboardIID = Components.interfaces.nsIClipboard;
-    return Components.classes[kClipboardContractID].getService(kClipboardIID);
+function getClipboard() {
+    return Components.classes["@mozilla.org/widget/clipboard;1"]
+                     .getService(Components.interfaces.nsIClipboard);
 }
 
-var Transferable = Components.Constructor("@mozilla.org/widget/transferable;1", Components.interfaces.nsITransferable);
-var SupportsArray = Components.Constructor("@mozilla.org/supports-array;1", Components.interfaces.nsISupportsArray);
-var SupportsCString = (("nsISupportsCString" in Components.interfaces)
-                       ? Components.Constructor("@mozilla.org/supports-cstring;1", Components.interfaces.nsISupportsCString)
-                       : Components.Constructor("@mozilla.org/supports-string;1", Components.interfaces.nsISupportsString)
-                      );
-var SupportsString = (("nsISupportsWString" in Components.interfaces)
-                      ? Components.Constructor("@mozilla.org/supports-wstring;1", Components.interfaces.nsISupportsWString)
-                      : Components.Constructor("@mozilla.org/supports-string;1", Components.interfaces.nsISupportsString)
-                     );
-
-/** 
-* Test a writable calendar is selecte and
-* if the clipboard has items that can be pasted into Calendar.
-* This must be of type "text/calendar" or "text/unicode"
-*/
-
-function canPaste()
-{
-    var cal = getSelectedCalendar();
-    if (!cal || !isCalendarWritable(cal)) {
+/**
+ * Test if a writable calendar is selected, and if the clipboard has items that
+ * can be pasted into Calendar. The data must be of type "text/calendar" or
+ * "text/unicode".
+ */
+function canPaste() {
+    let selectedCal = getSelectedCalendar();
+    if (!selectedCal || !cal.isCalendarWritable(selectedCal)) {
         return false;
     }
 
-    const kClipboardIID = Components.interfaces.nsIClipboard;
-
-    var clipboard = getClipboard();
-    var flavourArray = new SupportsArray;
-    var flavours = ["text/calendar", "text/unicode"];
+    const flavors = ["text/calendar", "text/unicode"];
+    return getClipboard().hasDataMatchingFlavors(flavors,
+                                                 flavors.length,
+                                                 Components.interfaces.nsIClipboard.kGlobalClipboard);
+}
 
-    if (kClipboardIID.number == "{8b5314ba-db01-11d2-96ce-0060b0fb9956}") { // on branch
-        for (var i = 0; i < flavours.length; ++i) {
-            var kSuppString = new SupportsCString;
-            kSuppString.data = flavours[i];
-            flavourArray.AppendElement(kSuppString);
-        }
-        return clipboard.hasDataMatchingFlavors(flavourArray,
-                                                kClipboardIID.kGlobalClipboard);
-    } else {
-        return clipboard.hasDataMatchingFlavors(flavours, flavours.length,
-                                                kClipboardIID.kGlobalClipboard);
+/**
+ * Copy iCalendar data to the Clipboard, and delete the selected items. Does
+ * not use calendarItemArray parameter, because selected items are deleted.
+ */
+function cutToClipboard(/* calendarItemArray */) {
+    let calendarItemArray = currentView().getSelectedItems({});
+
+    if (copyToClipboard(calendarItemArray)) {
+        deleteSelectedEvents();
     }
 }
 
-/** 
-* Copy iCalendar data to the Clipboard, and delete the selected events.
-* Does not use eventarray parameter, because DeletCcommand delete selected events.
-*/
-
-function cutToClipboard( /* calendarEventArray */)
-{
-    var calendarEventArray = currentView().getSelectedItems({});
-
-    if( copyToClipboard( calendarEventArray ) )
-    {
-         deleteSelectedEvents();
-    }
-}
-
-
-/** 
-* Copy iCalendar data to the Clipboard. The data is copied to both 
-* text/calendar and text/unicode. 
-**/
-
-function copyToClipboard( calendarItemArray )
-{  
+/**
+ * Copy iCalendar data to the Clipboard. The data is copied to both
+ * text/calendar and text/unicode.
+ **/
+function copyToClipboard(calendarItemArray) {
     if (!calendarItemArray) {
         calendarItemArray = currentView().getSelectedItems({});
     }
 
     if (!calendarItemArray.length) {
-        dump("Tried to cut/copy 0 events");
         return false;
     }
 
-    var calComp = getIcsService().createIcalComponent("VCALENDAR");
-    calSetProdidVersion(calComp);
+    let calComp = cal.getIcsService().createIcalComponent("VCALENDAR");
+    cal.calSetProdidVersion(calComp);
 
-    for each (item in calendarItemArray) {
+    for each (let item in calendarItemArray) {
         // If we copy an item and paste it again, it will have the same ID as
-        // the original.  Therefore, give every item a new ID.
-        var dummyItem = Components.classes["@mozilla.org/calendar/event;1"]
-                                  .createInstance(Components.interfaces.calIEvent);
-        var newItem = item.clone();
-        newItem.id = dummyItem.id;
+        // the original. Therefore, give every item a new ID.
+        let newItem = item.clone();
+        newItem.id = cal.getUUID();
         calComp.addSubcomponent(newItem.icalComponent);
     }
 
     // XXX This might not be enough to be Outlook compatible
-    var sTextiCalendar = calComp.serializeToICS();
-
-    // 1. get the clipboard service
-    var clipboard = getClipboard();
+    let sTextiCalendar = calComp.serializeToICS();
 
-    // 2. create the transferable
-    var trans = new Transferable;
+    let clipboard = getClipboard();
+    let trans = Components.classes["@mozilla.org/widget/transferable;1"]
+                          .createInstance(Components.interfaces.nsITransferable);
 
-    if ( trans && clipboard) {
-        // 3. register the data flavors
+    if (trans && clipboard) {
+        // Register supported data flavors
         trans.addDataFlavor("text/calendar");
         trans.addDataFlavor("text/unicode");
 
-        // 4. create the data objects
-        var icalWrapper = new SupportsString;
-
-        // get the data
+        // Create the data objects
+        let icalWrapper = Components.classes["@mozilla.org/supports-string;1"]
+                                    .createInstance(Components.interfaces.nsISupportsString);
         icalWrapper.data = sTextiCalendar;
 
-        // 5. add data objects to transferable
-        // Both Outlook 2000 client and Lotus Organizer use text/unicode 
-        // when pasting iCalendar data
-        trans.setTransferData("text/calendar", icalWrapper,
-                              icalWrapper.data.length*2 ); // double byte data
-        trans.setTransferData("text/unicode", icalWrapper, 
-                              icalWrapper.data.length*2 );
+        // Add data objects to transferable
+        // Both Outlook 2000 client and Lotus Organizer use text/unicode
+        // when pasting iCalendar data.
+        trans.setTransferData("text/calendar",
+                              icalWrapper,
+                              icalWrapper.data.length * 2); // double byte data
+        trans.setTransferData("text/unicode",
+                              icalWrapper,
+                              icalWrapper.data.length * 2);
 
-        clipboard.setData(trans, null,
-                          Components.interfaces.nsIClipboard.kGlobalClipboard );
+        clipboard.setData(trans,
+                          null,
+                          Components.interfaces.nsIClipboard.kGlobalClipboard);
 
-        return true;         
+        return true;
     }
-    return true;
+    return false;
 }
 
-
-/** 
-* Paste iCalendar events from the clipboard, 
-* or paste clipboard text into description of new event
-*/
-
-function pasteFromClipboard()
-{
+/**
+ * Paste iCalendar data from the clipboard, or paste clipboard text into
+ * description of new item.
+ */
+function pasteFromClipboard() {
     if (!canPaste()) {
         return;
     }
 
-    // 1. get the clipboard service
-    var clipboard = getClipboard();
-
-    // 2. create the transferable
-    var trans = new Transferable;
+    let clipboard = getClipboard();
+    let trans = Components.classes["@mozilla.org/widget/transferable;1"]
+                          .createInstance(Components.interfaces.nsITransferable);
 
     if (!trans || !clipboard) {
-        dump("Failed to get either a transferable or a clipboard");
         return;
     }
-    // 3. register the data flavors you want, highest fidelity first!
+
+    // Register the wanted data flavors (highest fidelity first!)
     trans.addDataFlavor("text/calendar");
     trans.addDataFlavor("text/unicode");
 
-    // 4. get transferable from clipboard
-    clipboard.getData ( trans, Components.interfaces.nsIClipboard.kGlobalClipboard);
+    // Get transferable from clipboard
+    clipboard.getData(trans, Components.interfaces.nsIClipboard.kGlobalClipboard);
 
-    // 5. ask transferable for the best flavor. Need to create new JS
-    //    objects for the out params.
-    var flavour = { };
-    var data = { };
-    trans.getAnyTransferData(flavour, data, {});
+    // Ask transferable for the best flavor.
+    let flavor = {};
+    let data = {};
+    trans.getAnyTransferData(flavor, data, {});
     data = data.value.QueryInterface(Components.interfaces.nsISupportsString).data;
-    var items = new Array();
-    switch (flavour.value) {
+    let items = new Array();
+    switch (flavor.value) {
         case "text/calendar":
         case "text/unicode":
-            // Moving this test up before processing
-            var destCal = getSelectedCalendar();
+            let destCal = getSelectedCalendar();
             if (!destCal) {
                 return;
             }
-            var calComp = getIcsService().parseICS(data, null);
-            var subComp = calComp.getFirstSubcomponent("ANY");
+
+            let calComp = cal.getIcsService().parseICS(data, null);
+            let subComp = calComp.getFirstSubcomponent("ANY");
             while (subComp) {
                 switch (subComp.componentType) {
                     case "VEVENT":
-                        var event = Components.classes["@mozilla.org/calendar/event;1"]
-                                              .createInstance
-                                              (Components.interfaces.calIEvent);
+                        let event = cal.createEvent();
                         event.icalComponent = subComp;
                         items.push(event);
                         break;
                     case "VTODO":
-                        var todo = Components.classes["@mozilla.org/calendar/todo;1"]
-                                             .createInstance
-                                             (Components.interfaces.calITodo);
+                        let todo = cal.createTodo();
                         todo.icalComponent = subComp;
                         items.push(todo);
                         break;
-                    default: break;
+                    default:
+                        break;
                 }
                 subComp = calComp.getNextSubcomponent("ANY");
             }
+
             // If there are multiple items on the clipboard, the earliest
             // should be set to the selected day and the rest adjusted.
-            var earliestDate = null;
-            for each(item in items) {
-                var date = null;
-                if (item.startDate) 
+            let earliestDate = null;
+            for each (let item in items) {
+                let date = null;
+                if (item.startDate) {
                     date = item.startDate.clone();
-                else if (item.entryDate)
+                } else if (item.entryDate) {
                     date = item.entryDate.clone();
-                else if (item.dueDate)
+                } else if (item.dueDate) {
                     date = item.dueDate.clone();
+                }
 
-                if (!date)
+                if (!date) {
                     continue;
-                if (!earliestDate || date.compare(earliestDate) < 0)
+                }
+
+                if (!earliestDate || date.compare(earliestDate) < 0) {
                     earliestDate = date;
+                }
             }
-            var firstDate = currentView().selectedDay; 
+            let firstDate = currentView().selectedDay;
 
-            // Timezones and DT/DST time may differ between the earliest item  
-            // and the selected day. Determine the offset between the 
-            // earliestDate in local time and the selected day in whole days. 
+            // Timezones and DT/DST time may differ between the earliest item
+            // and the selected day. Determine the offset between the
+            // earliestDate in local time and the selected day in whole days.
             earliestDate = earliestDate.getInTimezone(calendarDefaultTimezone());
             earliestDate.isDate = true;
-            var offset = firstDate.subtractDate(earliestDate);
-            var deltaDST = firstDate.timezoneOffset - earliestDate.timezoneOffset;
+            let offset = firstDate.subtractDate(earliestDate);
+            let deltaDST = firstDate.timezoneOffset - earliestDate.timezoneOffset;
             offset.inSeconds += deltaDST;
 
             startBatchTransaction();
-            for each(item in items) {
-                var newItem = item.clone();
+            for each (let item in items) {
+                let newItem = item.clone();
                 if (item.startDate) {
                     newItem.startDate.addDuration(offset);
                     newItem.endDate.addDuration(offset);
                 } else {
                     if (item.entryDate) {
                         newItem.entryDate.addDuration(offset);
                     }
                     if (item.dueDate) {
                         newItem.dueDate.addDuration(offset);
                     }
                 }
                 doTransaction('add', newItem, destCal, null, null);
             }
             endBatchTransaction();
             break;
-        default: 
-            dump("Unknown clipboard type: " + flavour.value);
+        default:
+            break;
     }
 }
--- a/calendar/base/content/calendar-scripts.inc
+++ b/calendar/base/content/calendar-scripts.inc
@@ -41,17 +41,17 @@
 # the terms of any one of the MPL, the GPL or the LGPL.
 #
 # ***** END LICENSE BLOCK *****
 
   <!-- NEEDED FOR MULTIPLE CALENDAR SUPPORT -->
   <script type="application/javascript" src="chrome://calendar/content/calendar-management.js"/>
 
   <!-- NEEDED FOR CLIPBOARD SUPPORT -->
-  <script type="application/javascript" src="chrome://calendar/content/clipboard.js"/>
+  <script type="application/javascript" src="chrome://calendar/content/calendar-clipboard.js"/>
 
   <!-- NEEDED FOR IMPORT / EXPORT SUPPORT -->
   <script type="application/javascript" src="chrome://calendar/content/import-export.js"/>
 
   <!-- NEEDED FOR PUBLICATION SUPPORT -->
   <script type="application/javascript" src="chrome://calendar/content/publish.js"/>
 
   <script type="application/javascript" src="chrome://calendar/content/calendar-item-editing.js"/>
--- a/calendar/base/jar.mn
+++ b/calendar/base/jar.mn
@@ -7,16 +7,17 @@ calendar.jar:
     content/calendar/calendar-alarm-dialog.js              (content/calendar-alarm-dialog.js)
     content/calendar/calendar-alarm-dialog.xul             (content/calendar-alarm-dialog.xul)
     content/calendar/calendar-alarm-snooze-popup.xul       (content/calendar-alarm-snooze-popup.xul)
     content/calendar/calendar-alarm-snooze-popup.js        (content/calendar-alarm-snooze-popup.js)
     content/calendar/calendar-alarm-widget.xml             (content/calendar-alarm-widget.xml)
     content/calendar/calendar-bindings.css                 (content/calendar-bindings.css)
     content/calendar/calendar-calendars-list.xul           (content/calendar-calendars-list.xul)
     content/calendar/calendar-chrome-startup.js            (content/calendar-chrome-startup.js)
+    content/calendar/calendar-clipboard.js                 (content/calendar-clipboard.js)
     content/calendar/calendar-common-sets.xul              (content/calendar-common-sets.xul)
     content/calendar/calendar-common-sets.js               (content/calendar-common-sets.js)
     content/calendar/calendar-creation.js                  (content/calendar-creation.js)
     content/calendar/calendar-daypicker.xml                (content/calendar-daypicker.xml)
     content/calendar/calendar-decorated-base.xml           (content/calendar-decorated-base.xml)
     content/calendar/calendar-decorated-multiday-base-view.xml  (content/calendar-decorated-multiday-base-view.xml)
     content/calendar/calendar-decorated-day-view.xml       (content/calendar-decorated-day-view.xml)
     content/calendar/calendar-decorated-week-view.xml      (content/calendar-decorated-week-view.xml)
--- a/calendar/lightning/jar.mn
+++ b/calendar/lightning/jar.mn
@@ -50,17 +50,16 @@ lightning.jar:
     skin/lightning/imip.css                                (themes/@THEME@/imip.css)
     skin/lightning/lightning.css                           (themes/@THEME@/lightning.css)
     skin/lightning/accountCentral.css                      (themes/@THEME@/accountCentral.css)
     skin/lightning/lightning-widgets.css                   (themes/@THEME@/lightning-widgets.css)
 
 calendar.jar:
     content/calendar/calendarCreation.xul                  (/calendar/resources/content/calendarCreation.xul)
     content/calendar/calendarCreation.js                   (/calendar/resources/content/calendarCreation.js)
-    content/calendar/clipboard.js                          (/calendar/resources/content/clipboard.js)
     content/calendar/datetimepickers/datetimepickers.css   (/calendar/resources/content/datetimepickers/datetimepickers.css)
 *   content/calendar/datetimepickers/datetimepickers.xml   (/calendar/resources/content/datetimepickers/datetimepickers.xml)
     content/calendar/mouseoverPreviews.js                  (/calendar/resources/content/mouseoverPreviews.js)
 *   content/calendar/printDialog.js                        (/calendar/resources/content/printDialog.js)
     content/calendar/printDialog.xul                       (/calendar/resources/content/printDialog.xul)
     content/calendar/publish.js                            (/calendar/resources/content/publish.js)
     content/calendar/publishDialog.js                      (/calendar/resources/content/publishDialog.js)
     content/calendar/publishDialog.xul                     (/calendar/resources/content/publishDialog.xul)
--- a/calendar/resources/jar.mn
+++ b/calendar/resources/jar.mn
@@ -1,15 +1,14 @@
 #filter substitution
 calendar.jar:
 % content calendar %content/calendar/
     content/calendar/calendar.js                           (content/calendar.js)
     content/calendar/calendarCreation.xul                  (content/calendarCreation.xul)
     content/calendar/calendarCreation.js                   (content/calendarCreation.js)
-    content/calendar/clipboard.js                          (content/clipboard.js)
 *   content/calendar/printDialog.js                        (content/printDialog.js)    
     content/calendar/printDialog.xul                       (content/printDialog.xul)
     content/calendar/publish.js                            (content/publish.js)
     content/calendar/publishDialog.js                      (content/publishDialog.js)    
     content/calendar/publishDialog.xul                     (content/publishDialog.xul)
     content/calendar/mouseoverPreviews.js                  (content/mouseoverPreviews.js)
     content/calendar/sound.wav                             (content/sound.wav)
     content/calendar/datetimepickers/datetimepickers.css   (content/datetimepickers/datetimepickers.css)