first pass at publish and import calendars (bugs 285016 and 285019)

This commit is contained in:
pavlov%pavlov.net 2005-03-22 21:37:26 +00:00
Родитель 51e6317cd9
Коммит 229acd7e9d
2 изменённых файлов: 231 добавлений и 218 удалений

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

@ -763,6 +763,41 @@ function jsDateToDateTime(date)
return newDate; return newDate;
} }
function appendCalendars(to, froms, listener)
{
var getListener = {
onOperationComplete: function(aCalendar, aStatus, aOperationType, aId, aDetail)
{
if (listener)
listener.onOperationComplete(aCalendar, aStatus, aOperationType,
aId, aDetail);
},
onGetResult: function(aCalendar, aStatus, aItemType, aDetail, aCount, aItems)
{
if (!Components.isSuccessCode(aStatus)) {
aborted = true;
return;
}
if (aCount) {
for (var i=0; i<aCount; ++i) {
// Store a (short living) reference to the item.
var itemCopy = aItems[i].clone();
to.addItem(itemCopy, null);
}
}
}
};
for each(var from in froms) {
from.getItems(Components.interfaces.calICalendar.ITEM_FILTER_TYPE_ALL,
0, null, null, getListener);
}
}
/* /*
* returns true if lastModified match * returns true if lastModified match
* *
@ -1391,13 +1426,10 @@ function publishEntireCalendar()
var args = new Object(); var args = new Object();
args.onOk = self.publishEntireCalendarDialogResponse; args.onOk = self.publishEntireCalendarDialogResponse;
var name = gCalendarWindow.calendarManager.getSelectedCalendarId();
var node = gCalendarWindow.calendarManager.rdf.getNode( name );
var remotePath = node.getAttribute( "http://home.netscape.com/NC-rdf#remotePath" ); var remotePath = ""; // get a remote path as a pref of the calendar
if( remotePath != "" && remotePath != null ) if (remotePath != "" && remotePath != null) {
{
var publishObject = new Object( ); var publishObject = new Object( );
publishObject.remotePath = remotePath; publishObject.remotePath = remotePath;
args.publishObject = publishObject; args.publishObject = publishObject;
@ -1408,24 +1440,28 @@ function publishEntireCalendar()
function publishEntireCalendarDialogResponse( CalendarPublishObject ) function publishEntireCalendarDialogResponse( CalendarPublishObject )
{ {
//update the calendar object with the publish information var icsURL = makeURL(CalendarPublishObject.remotePath);
var name = gCalendarWindow.calendarManager.getSelectedCalendarId();
//get the node var oldCalendar = getCalendar(); // get the currently selected calendar
var node = gCalendarWindow.calendarManager.rdf.getNode( name );
node.setAttribute( "http://home.netscape.com/NC-rdf#remotePath", CalendarPublishObject.remotePath ); // create an ICS calendar, but don't register it
var calManager = getCalendarManager();
try {
var newCalendar = calManager.createCalendar(oldCalendar.name, "ics", icsURL);
} catch (ex) {
dump(ex);
return false;
}
if( node.getAttribute("http://home.netscape.com/NC-rdf#publishAutomatically") != "true" ) var getListener = {
node.setAttribute("http://home.netscape.com/NC-rdf#publishAutomatically", "false"); onOperationComplete: function(aCalendar, aStatus, aOperationType, aId, aDetail)
{
// delete the new calendar now that we're done with it
calManager.deleteCalendar(newCalendar);
}
};
gCalendarWindow.calendarManager.rdf.flush(); appendCalendars(newCalendar, [oldCalendar], getListener);
calendarUploadFile(node.getAttribute( "http://home.netscape.com/NC-rdf#path" ),
CalendarPublishObject.remotePath,
"text/calendar");
return( false );
} }
function publishCalendarData() function publishCalendarData()

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

@ -1,4 +1,5 @@
/* ***** BEGIN LICENSE BLOCK ***** /* -*- Mode: javascript; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
* *
* The contents of this file are subject to the Mozilla Public License Version * The contents of this file are subject to the Mozilla Public License Version
@ -117,8 +118,7 @@ function loadEventsFromFile()
fp.show(); fp.show();
var filesToAppend = fp.files; var filesToAppend = fp.files;
if (filesToAppend && filesToAppend.hasMoreElements()) if (filesToAppend && filesToAppend.hasMoreElements()) {
{
var calendarEventArray = new Array(); var calendarEventArray = new Array();
var duplicateEventArray = new Array(); var duplicateEventArray = new Array();
var calendarToDoArray = new Array(); var calendarToDoArray = new Array();
@ -144,8 +144,10 @@ function loadEventsFromFile()
var importDupTasksTitle = gCalendarBundle.getString( "aboutToImportDupTasksTitle" ); var importDupTasksTitle = gCalendarBundle.getString( "aboutToImportDupTasksTitle" );
var fromFileNames = ""; var fromFileNames = "";
while (filesToAppend.hasMoreElements()) // create a temp memory calendar to put all the events and todos in to
{ var tmpCalendar = getCalendarManager().createCalendar("tmpcal", "memory", null);
while (filesToAppend.hasMoreElements()) {
currentFile = filesToAppend.getNext().QueryInterface(Components.interfaces.nsILocalFile); currentFile = filesToAppend.getNext().QueryInterface(Components.interfaces.nsILocalFile);
fromFileNames += (fromFileNames == "" ? "" : ", ") + currentFile.leafName; fromFileNames += (fromFileNames == "" ? "" : ", ") + currentFile.leafName;
aDataStream = readDataFromFile( currentFile.path, "UTF-8" ); aDataStream = readDataFromFile( currentFile.path, "UTF-8" );
@ -156,36 +158,31 @@ function loadEventsFromFile()
// fall thru to process ics data // fall thru to process ics data
case 0 : // ics case 0 : // ics
case 3 : // vcs case 3 : // vcs
parsedEventArray = parseIcalEvents( aDataStream ); parseIcalEvents(tmpCalendar, aDataStream);
parsedToDoArray = parseIcalToDos( aDataStream ); parseIcalToDos(tmpCalendar, aDataStream);
break; break;
case 2: // csv case 2: // csv
parsedEventArray = parseOutlookCSVEvents( aDataStream ); parseOutlookCSVEvents(tmpCalendar, aDataStream);
break; break;
default: default:
break; break;
} }
if( parsedEventArray ) {
for( i = 0; i < parsedEventArray.length; i++ ) {
var parsedEvent = parsedEventArray[i];
date.setTime( parsedEvent.start.getTime() );
if( eventExists( date, parsedEvent.title ) )
duplicateEventArray[duplicateEventArray.length] = parsedEvent;
else
calendarEventArray[calendarEventArray.length] = parsedEvent;
}
}
if( parsedToDoArray ) {
for( i = 0; i < parsedToDoArray.length; i++ ) {
var parsedToDo = parsedToDoArray[i];
date.setTime( parsedToDo.start.getTime() );
if( toDoExists( date, parsedToDo.title ) )
duplicateToDoArray[duplicateToDoArray.length] = parsedToDo;
else
calendarToDoArray[calendarToDoArray.length] = parsedToDo;
}
} }
var listener = {
onOperationComplete: function(aCalendar, aStatus, aOperationType, aId, aDetail)
{
// delete the new calendar now that we're done with it
getCalendarManager().deleteCalendar(tmpCalendar);
} }
};
appendCalendars(getCalendar(), [tmpCalendar], listener);
return true;
// XXX handle all this stuff
var result = {value:0}; var result = {value:0};
var buttonPressed; var buttonPressed;
@ -193,9 +190,11 @@ function loadEventsFromFile()
// EVENTS: // EVENTS:
if (calendarEventArray.length > 0) { if (calendarEventArray.length > 0) {
// Ask user what to import (all / prompt each / none) // Ask user what to import (all / prompt each / none)
var importNewEventsText = gCalendarBundle.getFormattedString( "aboutToImportNewEvents", [calendarEventArray.length, intoCalName, fromFileNames]); var importNewEventsText = gCalendarBundle.getFormattedString( "aboutToImportNewEvents",
buttonPressed = promptService.confirmEx( window, importNewEventsTitle, importNewEventsText, flags, [calendarEventArray.length,
importAllStr, discardAllStr, promptStr, intoCalName, fromFileNames]);
buttonPressed = promptService.confirmEx( window, importNewEventsTitle, importNewEventsText,
flags, importAllStr, discardAllStr, promptStr,
null, result ); null, result );
if(buttonPressed == 0) // Import all if(buttonPressed == 0) // Import all
@ -207,7 +206,9 @@ function loadEventsFromFile()
if (duplicateEventArray.length > 0) { if (duplicateEventArray.length > 0) {
// Ask user what to do with duplicates // Ask user what to do with duplicates
var importDupEventsText = gCalendarBundle.getFormattedString( "aboutToImportDupEvents", [duplicateEventArray.length, intoCalName, fromFileNames]); var importDupEventsText = gCalendarBundle.getFormattedString( "aboutToImportDupEvents",
[duplicateEventArray.length,
intoCalName, fromFileNames]);
buttonPressed = promptService.confirmEx( window, importDupEventsTitle, importDupEventsText, flags, buttonPressed = promptService.confirmEx( window, importDupEventsTitle, importDupEventsText, flags,
importAllStr, discardAllStr, promptStr, importAllStr, discardAllStr, promptStr,
null, result ); null, result );
@ -895,36 +896,22 @@ function parseOutlookTextField(args, textIndexName, eventFields)
/**** parseIcalEvents /**** parseIcalEvents
* *
* Takes a text block of iCalendar events and tries to split that into individual events. * Takes a text block of iCalendar events and tries to split that into individual events.
* Parses those events and returns an array of calendarEvents. * Parses those events and adds them to a calendar.
*/ */
function parseIcalEvents( icalStr ) function parseIcalEvents(calendar, icalStr)
{ {
var calendarEventArray = new Array(); for (var i=0, j=0; (i = icalStr.indexOf("BEGIN:VEVENT", j)) != -1; ) {
for(var i=0, j=0; (i = icalStr.indexOf("BEGIN:VEVENT", j)) != -1; )
{
// try to find the begin and end of an event. ParseIcalString does not support VCALENDAR // try to find the begin and end of an event. ParseIcalString does not support VCALENDAR
j = icalStr.indexOf("END:VEVENT", i + "BEGIN:VEVENT".length); j = icalStr.indexOf("END:VEVENT", i + "BEGIN:VEVENT".length);
j = (j == -1? icalStr.length : j + "END:VEVENT".length); j = (j == -1? icalStr.length : j + "END:VEVENT".length);
var eventData = icalStr.substring(i, j); var eventData = icalStr.substring(i, j);
var calendarEvent = createEvent(); var calendarEvent = createEvent();
calendarEvent.icalString = eventData;
// if parsing import iCalendar failed, add data as description calendar.addItem(calendarEvent, null);
if ( !calendarEvent.parseIcalString(eventData) )
{
// initialize start and end dates.
initCalendarEvent( calendarEvent );
// Save the parsed text as description.
calendarEvent.description = eventData;
} }
calendarEventArray[ calendarEventArray.length ] = calendarEvent;
}
return calendarEventArray;
} }
/**** parseIcalToDos /**** parseIcalToDos
@ -933,33 +920,19 @@ function parseIcalEvents( icalStr )
* Parses those toDos and returns an array of calendarToDos. * Parses those toDos and returns an array of calendarToDos.
*/ */
function parseIcalToDos( icalStr ) function parseIcalToDos(calendar, icalStr)
{ {
var calendarToDoArray = new Array(); for(var i=0, j=0; (i = icalStr.indexOf("BEGIN:VTODO", j)) != -1; ) {
for(var i=0, j=0; (i = icalStr.indexOf("BEGIN:VTODO", j)) != -1; )
{
// try to find the begin and end of an toDo. ParseIcalString does not support VCALENDAR // try to find the begin and end of an toDo. ParseIcalString does not support VCALENDAR
j = icalStr.indexOf("END:VTODO", i + "BEGIN:VTODO".length); j = icalStr.indexOf("END:VTODO", i + "BEGIN:VTODO".length);
j = (j == -1? icalStr.length : j + "END:VTODO".length); j = (j == -1? icalStr.length : j + "END:VTODO".length);
var toDoData = icalStr.substring(i, j);
var eventData = icalStr.substring(i, j);
var calendarToDo = createToDo(); var calendarToDo = createToDo();
calendarToDo.icalString = eventData;
// if parsing import iCalendar failed, add data as description calendar.addItem(calendarToDo, null);
if ( !calendarToDo.parseIcalString(toDoData) )
{
// initialize start and end dates.
initCalendarToDo( calendarToDo );
// Save the parsed text as description.
calendarToDo.description = toDoData;
} }
calendarToDoArray[ calendarToDoArray.length ] = calendarToDo;
}
return calendarToDoArray;
} }
/**** transformXCSData: transform into ics data /**** transformXCSData: transform into ics data
@ -2013,6 +1986,7 @@ function getXmlDocument ( eventList )
} }
function startImport() { function startImport() {
/*
var ImportExportErrorHandler = { var ImportExportErrorHandler = {
errorreport : "", errorreport : "",
onLoad : function() {}, onLoad : function() {},
@ -2033,7 +2007,10 @@ function startImport() {
} }
gICalLib.addObserver( ImportExportErrorHandler ); gICalLib.addObserver( ImportExportErrorHandler );
loadEventsFromFile();
ImportExportErrorHandler.showErrors(); ImportExportErrorHandler.showErrors();
gICalLib.removeObserver( ImportExportErrorHandler ); gICalLib.removeObserver( ImportExportErrorHandler );
*/
loadEventsFromFile();
} }