Fix bug 379097 - export should default to ics. r=philipp

This commit is contained in:
Archaeopteryx 2009-02-12 19:31:35 +01:00
Родитель 2dce1a40f9
Коммит 29b466a270
1 изменённых файлов: 105 добавлений и 96 удалений

Просмотреть файл

@ -26,6 +26,7 @@
* Michiel van Leeuwen <mvl@exedo.nl>
* Stefan Sitter <ssitter@gmail.com>
* Philipp Kewisch <mozilla@kewis.ch>
* Archaeopteryx <archaeopteryx@coole-files.de>
*
* 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
@ -52,8 +53,7 @@ const MODE_SYNC = 0x40;
const MODE_EXCL = 0x80;
/**
* loadEventsFromFile
* shows a file dialog, reads the selected file(s) and tries to parse events from it.
* Shows a file dialog, reads the selected file(s) and tries to parse events from it.
*
* @param aCalendar (optional) If specified, the items will be imported directly
* into the calendar
@ -61,43 +61,50 @@ const MODE_EXCL = 0x80;
function loadEventsFromFile(aCalendar) {
const nsIFilePicker = Components.interfaces.nsIFilePicker;
var fp = Components.classes["@mozilla.org/filepicker;1"]
let fp = Components.classes["@mozilla.org/filepicker;1"]
.createInstance(nsIFilePicker);
fp.init(window, calGetString("calendar", "filepickerTitleImport"), nsIFilePicker.modeOpen);
fp.init(window,
calGetString("calendar", "filepickerTitleImport"),
nsIFilePicker.modeOpen);
fp.defaultExtension = "ics";
// Get a list of exporters
var contractids = new Array();
var catman = Components.classes["@mozilla.org/categorymanager;1"]
let contractids = new Array();
let catman = Components.classes["@mozilla.org/categorymanager;1"]
.getService(Components.interfaces.nsICategoryManager);
var catenum = catman.enumerateCategory('cal-importers');
let catenum = catman.enumerateCategory('cal-importers');
let currentListLength = 0;
while (catenum.hasMoreElements()) {
var entry = catenum.getNext();
let entry = catenum.getNext();
entry = entry.QueryInterface(Components.interfaces.nsISupportsCString);
var contractid = catman.getCategoryEntry('cal-importers', entry);
var exporter = Components.classes[contractid]
let contractid = catman.getCategoryEntry('cal-importers', entry);
let exporter = Components.classes[contractid]
.getService(Components.interfaces.calIImporter);
var types = exporter.getFileTypes({});
var type;
let types = exporter.getFileTypes({});
let type;
for each (type in types) {
fp.appendFilter(type.description, type.extensionFilter);
if (type.extensionFilter=="*." + fp.defaultExtension) {
fp.filterIndex = currentListLength;
}
contractids.push(contractid);
currentListLength++;
}
}
fp.show();
if (fp.file && fp.file.path && fp.file.path.length > 0) {
var filePath = fp.file.path;
var importer = Components.classes[contractids[fp.filterIndex]]
let filePath = fp.file.path;
let importer = Components.classes[contractids[fp.filterIndex]]
.getService(Components.interfaces.calIImporter);
const nsIFileInputStream = Components.interfaces.nsIFileInputStream;
const nsIScriptableInputStream = Components.interfaces.nsIScriptableInputStream;
var inputStream = Components.classes["@mozilla.org/network/file-input-stream;1"]
let inputStream = Components.classes["@mozilla.org/network/file-input-stream;1"]
.createInstance(nsIFileInputStream);
var items = [];
let items = [];
try {
inputStream.init( fp.file, MODE_RDONLY, 0444, {});
@ -119,7 +126,7 @@ function loadEventsFromFile(aCalendar) {
return;
}
var calendars = getCalendarManager().getCalendars({});
let calendars = cal.getCalendarManager().getCalendars({});
calendars = calendars.filter(isCalendarWritable);
if (calendars.length < 1) {
@ -131,7 +138,7 @@ function loadEventsFromFile(aCalendar) {
putItemsIntoCal(calendars[0], items, filePath);
} else {
// Ask what calendar to import into
var args = new Object();
let args = new Object();
args.onOk = function putItems(aCal) { putItemsIntoCal(aCal, items, filePath); };
args.calendars = calendars;
args.promptText = calGetString("calendar", "importPrompt");
@ -212,8 +219,6 @@ function putItemsIntoCal(destCal, aItems, aFilePath) {
}
/**
* saveEventsToFile
*
* Save data to a file. Create the file or overwrite an existing file.
*
* @param calendarEventArray (required) Array of calendar events that should
@ -221,16 +226,19 @@ function putItemsIntoCal(destCal, aItems, aFilePath) {
* @param aDefaultFileName (optional) Initial filename shown in SaveAs dialog.
*/
function saveEventsToFile(calendarEventArray, aDefaultFileName) {
if (!calendarEventArray)
if (!calendarEventArray || !calendarEventArray.length) {
return;
}
// Show the 'Save As' dialog and ask for a filename to save to
const nsIFilePicker = Components.interfaces.nsIFilePicker;
var fp = Components.classes["@mozilla.org/filepicker;1"]
let fp = Components.classes["@mozilla.org/filepicker;1"]
.createInstance(nsIFilePicker);
fp.init(window, calGetString("calendar", "filepickerTitleExport"), nsIFilePicker.modeSave);
fp.init(window,
calGetString("calendar", "filepickerTitleExport"),
nsIFilePicker.modeSave);
if (aDefaultFileName && aDefaultFileName.length && aDefaultFileName.length > 0) {
fp.defaultString = aDefaultFileName;
@ -243,65 +251,66 @@ function saveEventsToFile(calendarEventArray, aDefaultFileName) {
fp.defaultExtension = "ics";
// Get a list of exporters
var contractids = new Array();
var catman = Components.classes["@mozilla.org/categorymanager;1"]
let contractids = new Array();
let catman = Components.classes["@mozilla.org/categorymanager;1"]
.getService(Components.interfaces.nsICategoryManager);
var catenum = catman.enumerateCategory('cal-exporters');
let catenum = catman.enumerateCategory('cal-exporters');
currentListLength = 0;
while (catenum.hasMoreElements()) {
var entry = catenum.getNext();
let entry = catenum.getNext();
entry = entry.QueryInterface(Components.interfaces.nsISupportsCString);
var contractid = catman.getCategoryEntry('cal-exporters', entry);
var exporter = Components.classes[contractid]
let contractid = catman.getCategoryEntry('cal-exporters', entry);
let exporter = Components.classes[contractid]
.getService(Components.interfaces.calIExporter);
var types = exporter.getFileTypes({});
var type;
for each (type in types) {
let types = exporter.getFileTypes({});
let type;
for each (let type in types) {
fp.appendFilter(type.description, type.extensionFilter);
if (type.extensionFilter=="*." + fp.defaultExtension) {
fp.filterIndex = currentListLength;
}
contractids.push(contractid);
currentListLength++;
}
}
fp.show();
// Now find out as what to save, convert the events and save to file.
if (fp.file && fp.file.path.length > 0)
{
if (fp.file && fp.file.path.length > 0) {
const UTF8 = "UTF-8";
var aDataStream;
var extension;
var charset;
let aDataStream;
let extension;
let charset;
var exporter = Components.classes[contractids[fp.filterIndex]]
let exporter = Components.classes[contractids[fp.filterIndex]]
.getService(Components.interfaces.calIExporter);
var filePath = fp.file.path;
if(filePath.indexOf(".") == -1 )
let filePath = fp.file.path;
if (filePath.indexOf(".") == -1) {
filePath += "."+exporter.getFileTypes({})[0].defaultExtension;
}
const LOCALFILE_CTRID = "@mozilla.org/file/local;1";
const FILEOUT_CTRID = "@mozilla.org/network/file-output-stream;1";
const nsILocalFile = Components.interfaces.nsILocalFile;
const nsIFileOutputStream = Components.interfaces.nsIFileOutputStream;
var outputStream;
var localFileInstance = Components.classes[LOCALFILE_CTRID]
let outputStream;
let localFileInstance = Components.classes["@mozilla.org/file/local;1"]
.createInstance(nsILocalFile);
localFileInstance.initWithPath(filePath);
outputStream = Components.classes[FILEOUT_CTRID]
outputStream = Components.classes["@mozilla.org/network/file-output-stream;1"]
.createInstance(nsIFileOutputStream);
try
{
try {
outputStream.init(localFileInstance, MODE_WRONLY | MODE_CREATE | MODE_TRUNCATE, 0664, 0);
// XXX Do the right thing with unicode and stuff. Or, again, should the
// exporter handle that?
exporter.exportToStream(outputStream, calendarEventArray.length, calendarEventArray, null);
exporter.exportToStream(outputStream,
calendarEventArray.length,
calendarEventArray,
null);
outputStream.close();
}
catch(ex)
{
} catch(ex) {
showError(calGetString("calendar", "unableToWrite") + filePath);
}
}