Fix bug 636058 - Errors on import, mac only (Components.classes[contractids[fp.filterIndex]] is undefined). r=mmecca,a=philipp
authorPhilipp Kewisch <mozilla@kewis.ch>
Mon, 25 Jul 2011 01:22:00 +0200
changeset 8836 894ab91e98b5da37fde1a5541324b04504415fca
parent 8835 b12547a628cf769c1083801d3bdf022444752b84
child 8837 8efe8b2979c1613d44904d72d883366db3889545
push idunknown
push userunknown
push dateunknown
reviewersmmecca, philipp
bugs636058
Fix bug 636058 - Errors on import, mac only (Components.classes[contractids[fp.filterIndex]] is undefined). r=mmecca,a=philipp
calendar/base/content/import-export.js
--- a/calendar/base/content/import-export.js
+++ b/calendar/base/content/import-export.js
@@ -68,16 +68,17 @@ function loadEventsFromFile(aCalendar) {
     fp.defaultExtension = "ics";
 
     // Get a list of importers
     let contractids = new Array();
     let catman = Components.classes["@mozilla.org/categorymanager;1"]
                            .getService(Components.interfaces.nsICategoryManager);
     let catenum = catman.enumerateCategory('cal-importers');
     let currentListLength = 0;
+    let defaultCIDIndex = 0;
     while (catenum.hasMoreElements()) {
         let entry = catenum.getNext();
         entry = entry.QueryInterface(Components.interfaces.nsISupportsCString);
         let contractid = catman.getCategoryEntry('cal-importers', entry);
         let importer;
         try {
             importer = Components.classes[contractid]
                                  .getService(Components.interfaces.calIImporter);
@@ -85,28 +86,36 @@ function loadEventsFromFile(aCalendar) {
             cal.WARN("Could not initialize importer: " + contractid + "\nError: " + e);
             continue;
         }
         let types = importer.getFileTypes({});
         for each (let type in types) {
             fp.appendFilter(type.description, type.extensionFilter);
             if (type.extensionFilter=="*." + fp.defaultExtension) {
                 fp.filterIndex = currentListLength;
+                defaultCIDIndex = currentListLength;
             }
             contractids.push(contractid);
             currentListLength++;
         }
     }
 
     let rv = fp.show();
 
     if (rv != nsIFilePicker.returnCancel &&
         fp.file && fp.file.path && fp.file.path.length > 0) {
+
+        let filterIndex = fp.filterIndex;
+        if (fp.filterIndex < 0 || fp.filterIndex > contractids.length) {
+            // For some reason the wrong filter was selected, assume default extension
+            filterIndex = defaultCIDIndex;
+        }
+
         let filePath = fp.file.path;
-        let importer = Components.classes[contractids[fp.filterIndex]]
+        let importer = Components.classes[contractids[filterIndex]]
                                  .getService(Components.interfaces.calIImporter);
 
         const nsIFileInputStream = Components.interfaces.nsIFileInputStream;
         const nsIScriptableInputStream = Components.interfaces.nsIScriptableInputStream;
 
         let inputStream = Components.classes["@mozilla.org/network/file-input-stream;1"]
                                     .createInstance(nsIFileInputStream);
         let items = [];
@@ -256,16 +265,17 @@ function saveEventsToFile(calendarEventA
     fp.defaultExtension = "ics";
 
     // Get a list of exporters
     let contractids = new Array();
     let catman = Components.classes["@mozilla.org/categorymanager;1"]
                            .getService(Components.interfaces.nsICategoryManager);
     let catenum = catman.enumerateCategory('cal-exporters');
     let currentListLength = 0;
+    let defaultCIDIndex = 0;
     while (catenum.hasMoreElements()) {
         let entry = catenum.getNext();
         entry = entry.QueryInterface(Components.interfaces.nsISupportsCString);
         let contractid = catman.getCategoryEntry('cal-exporters', entry);
         let exporter;
         try {
             exporter = Components.classes[contractid]
                                  .getService(Components.interfaces.calIExporter);
@@ -273,33 +283,40 @@ function saveEventsToFile(calendarEventA
             cal.WARN("Could not initialize exporter: " + contractid + "\nError: " + e);
             continue;
         }
         let types = exporter.getFileTypes({});
         for each (let type in types) {
             fp.appendFilter(type.description, type.extensionFilter);
             if (type.extensionFilter=="*." + fp.defaultExtension) {
                 fp.filterIndex = currentListLength;
+                defaultCIDIndex = currentListLength;
             }
             contractids.push(contractid);
             currentListLength++;
         }
     }
 
     let rv = fp.show();
 
     // Now find out as what to save, convert the events and save to file.
     if (rv != nsIFilePicker.returnCancel &&
         fp.file && fp.file.path.length > 0) {
         const UTF8 = "UTF-8";
         let aDataStream;
         let extension;
         let charset;
 
-        let exporter = Components.classes[contractids[fp.filterIndex]]
+        let filterIndex = fp.filterIndex;
+        if (fp.filterIndex < 0 || fp.filterIndex > contractids.length) {
+            // For some reason the wrong filter was selected, assume default extension
+            filterIndex = defaultCIDIndex;
+        }
+
+        let exporter = Components.classes[contractids[filterIndex]]
                                  .getService(Components.interfaces.calIExporter);
 
         let filePath = fp.file.path;
         if (filePath.indexOf(".") == -1) {
             filePath += "."+exporter.getFileTypes({})[0].defaultExtension;
         }
 
         const nsILocalFile = Components.interfaces.nsILocalFile;