gecko-dev/calendar/resources/content/calendarEvent.js

698 строки
19 KiB
JavaScript
Исходник Обычный вид История

2001-12-20 18:53:03 +03:00
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is OEone Calendar Code, released October 31st, 2001.
*
* The Initial Developer of the Original Code is
* OEone Corporation.
* Portions created by the Initial Developer are Copyright (C) 2001
* the Initial Developer. All Rights Reserved.
*
* Contributor(s): Garth Smedley <garths@oeone.com>
* Mike Potter <mikep@oeone.com>
*
* 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 ***** */
/***** calendar/ca-calender-event.js
* AUTHOR
* Garth Smedley
* REQUIRED INCLUDES
*
* NOTES
* CalendarEventDataSource class:
* Saves and loads calendar events, provides methods
* for adding, deleting, modifying and searching calendar events.
*
* CalendarEvent class:
* Contains info about calendar events.
*
* Also provides an observer interface for clients that need to be
* notified when calendar event data changes.
*
* IMPLEMENTATION NOTES
* Currently uses the Ical library to store events.
* Access to ical is through the libxpical xpcom object.
*
**********
*/
/*-----------------------------------------------------------------
* G L O B A L V A R I A B L E S
*/
/*-----------------------------------------------------------------
* CalendarEventDataSource Class
*
* Maintains all of the calendar events.
*
* PROPERTIES
* observerList - array of observers, see constructor notes.
*/
/**
* CalendarEventDataSource Constructor.
*
* PARAMETERS
* observer - An object that implements methods that are called when the
* data source is modified. The following methods should be implemented
* by the observer object.
*
{
onLoad : function() {}, // called when server is ready
onAddItem : function( calendarEvent ) {},
onModifyItem : function( calendarEvent, originalEvent ) {},
onDeleteItem : function( calendarEvent ) {},
onAlarm : function( calendarEvent ) {},
};
These methods are now called synchronously, if you add an event the onAddItem
method will be called during your call to add the event.
*
* NOTES
* Is a singleton, the first invocation creates an instance, subsequent calls return the same instance.
*/
function CalendarEventDataSource( observer, UserPath, syncPath )
{
try {
var iCalLibComponent = Components.classes["@mozilla.org/ical;1"].createInstance();
} catch ( e ) {
alert( "The ICAL Componenet is not registered properly. Please follow the instructions below:\n"+
"Windows Users:\n"+
"-Please quit Mozilla, run regxpcom and run Mozilla again.\n\n"+
"Linux users:\n"+
"1)Make sure you have write access to the component.reg file.\n"+
"2)Make sure libical is installed properly. (See http://www.mozilla.org/projects/calendar/ for detailed instructions)\n"+
"3)Quit Mozilla, cd to your mozilla/bin directory. Run ./regxpcom and run Mozilla again.\n"+
"Note: If you get this error:\n"+
"'./regxpcom: error while loading shared libraries: libxpcom.so: cannot open\n"+
"shared object file: No such file or directory',\n"+
"set LD_LIBRARY_PATH and MOZILLA_FIVE_HOME to your mozilla/bin directory first.\n"+
"Example: export LD_LIBRARY_PATH=/home/user/mozilla/dist/bin\n"+
"Example: export MOZILLA_FIVE_HOME=/home/user/mozilla/dist/bin\n\n"+
"If these instructions don't solve the problem, please add yourself to the cc list of\n"+
"bug 134432 at http://bugzilla.mozilla.org/show_bug.cgi?id=134432.\n"+
"and give more feedback on your platform, Mozilla version, calendar install type:\n"+
"(build or xpi), any errors you see on the console that you think is related and any\n"+
" other problems you come across when following the above insructions.\n" );
}
2001-12-20 18:53:03 +03:00
this.gICalLib = iCalLibComponent.QueryInterface(Components.interfaces.oeIICal);
/*
** FROM HERE TO "<<HERE" IS A HACK FROM JSLIB, REPLACE THIS IF YOU KNOW HOW
*/
const JS_DIR_UTILS_FILE_DIR_CID = "@mozilla.org/file/directory_service;1";
2001-12-20 23:13:53 +03:00
const JS_DIR_UTILS_I_PROPS = "nsIProperties";
2001-12-20 23:13:53 +03:00
const JS_DIR_UTILS_DIR = new Components.Constructor(
JS_DIR_UTILS_FILE_DIR_CID,
JS_DIR_UTILS_I_PROPS);
const JS_DIR_UTILS_NSIFILE = Components.interfaces.nsIFile;
var rv;
try { rv=(new JS_DIR_UTILS_DIR()).get("ProfD", JS_DIR_UTILS_NSIFILE).path; }
catch (e)
{
dump("ERROR! IN CALENDAR EVENT.JS");
rv=null;
}
/*
** <<HERE
*/
2001-12-20 23:13:53 +03:00
this.UserPath = rv;
2001-12-20 18:53:03 +03:00
2002-02-13 21:55:15 +03:00
this.gICalLib.setServer( this.UserPath+"/CalendarDataFile.ics" );
2001-12-20 18:53:03 +03:00
this.gICalLib.addObserver( observer );
this.prepareAlarms( );
this.onlyFutureEvents = false;
2001-12-20 18:53:03 +03:00
}
CalendarEventDataSource.InitService = function( root )
{
return new CalendarEventDataSource( null, root.getUserPath() );
}
// turn on/off debuging
CalendarEventDataSource.gDebug = true;
// Singleton CalendarEventDataSource variable.
CalendarEventDataSource.debug = function( str )
{
if( CalendarEventDataSource.gDebug )
{
dump( "\n CalendarEventDataSource DEBUG: "+ str + "\n");
}
}
/** PUBLIC
*
* NOTES
* Called at start up after all services have been inited.
*/
CalendarEventDataSource.prototype.onServiceStartup = function( root )
{
}
/** PUBLIC
*
* CalendarEventDataSource/search.
*
* First hacked in implementation of search
*
* NOTES
* This should be search ing by all content, not just the beginning
* of the title field, A LOT remains to be done..
*/
CalendarEventDataSource.prototype.search = function( searchText, fieldName )
{
searchText = searchText.toLowerCase();
/*
** Try to get rid of all the spaces in the search text.
** At present, this only gets rid of one.. I don't know why.
*/
var regexp = "\s+";
searchText = searchText.replace( regexp, "" );
var searchEventTable = new Array();
if( searchText != "" )
{
var eventTable = this.getCurrentEvents();
2001-12-20 18:53:03 +03:00
for( var index = 0; index < eventTable.length; ++index )
{
var calendarEvent = eventTable[ index ];
if ( typeof fieldName == "string")
{
var value = calendarEvent[ fieldName ].toLowerCase();
if( value )
{
if( value.indexOf( searchText ) != -1 )
{
searchEventTable[ searchEventTable.length ] = calendarEvent;
break;
}
}
}
else if ( typeof fieldName == "object" )
{
for ( i in fieldName )
{
var objValue = calendarEvent[ fieldName[i] ].toLowerCase();
if( objValue )
{
if( objValue.indexOf( searchText ) != -1 )
{
searchEventTable[ searchEventTable.length ] = calendarEvent;
break;
}
}
}
}
}
}
searchEventTable.sort( this.orderRawEventsByDate );
2001-12-20 18:53:03 +03:00
return searchEventTable;
}
CalendarEventDataSource.prototype.searchBySql = function( Query )
{
var eventDisplays = new Array();
var eventList = this.gICalLib.searchBySQL( Query );
while( eventList.hasMoreElements() )
{
eventDisplays[ eventDisplays.length ] = eventList.getNext().QueryInterface(Components.interfaces.oeIICalEvent);
}
eventDisplays.sort( this.orderRawEventsByDate );
return eventDisplays;
}
2001-12-20 18:53:03 +03:00
/** PUBLIC
*
* CalendarEventDataSource/getEventsForDay.
*
* PARAMETERS
* date - Date object, uses the month and year and date to get all events
* for the given day.
* RETURN
* array - of events for the day
*/
CalendarEventDataSource.prototype.getEventsForDay = function( date )
{
var eventDisplays = new Array();
var displayDates = new Object();
2001-12-20 18:53:03 +03:00
var eventList = this.gICalLib.getEventsForDay( date, displayDates );
2001-12-20 18:53:03 +03:00
while( eventList.hasMoreElements() )
{
var tmpevent = eventList.getNext().QueryInterface(Components.interfaces.oeIICalEvent);
var displayDate = new Date( displayDates.value.getNext().QueryInterface(Components.interfaces.nsISupportsPRTime).data );
var EventObject = new Object;
EventObject.event = tmpevent;
EventObject.displayDate = displayDate;
eventDisplays[ eventDisplays.length ] = EventObject;
}
eventDisplays.sort( this.orderEventsByDate );
2001-12-20 18:53:03 +03:00
return eventDisplays;
}
/** PUBLIC
*
* CalendarEventDataSource/getEventsForWeek.
*
* PARAMETERS
* date - Date object, uses the month and year and date to get all events
* for the given day.
* RETURN
* array - of events for the day
*/
CalendarEventDataSource.prototype.getEventsForWeek = function( date )
{
var eventDisplays = new Array();
var displayDates = new Object();
2001-12-20 18:53:03 +03:00
var eventList = this.gICalLib.getEventsForWeek( date, displayDates );
2001-12-20 18:53:03 +03:00
while( eventList.hasMoreElements() )
{
var tmpevent = eventList.getNext().QueryInterface(Components.interfaces.oeIICalEvent);
var displayDate = new Date( displayDates.value.getNext().QueryInterface(Components.interfaces.nsISupportsPRTime).data );
var EventObject = new Object;
EventObject.event = tmpevent;
EventObject.displayDate = displayDate;
eventDisplays[ eventDisplays.length ] = EventObject;
}
eventDisplays.sort( this.orderEventsByDate );
2001-12-20 18:53:03 +03:00
return eventDisplays;
}
/** PUBLIC
*
* CalendarEventDataSource/getEventsForMonth.
*
* PARAMETERS
* date - Date object, uses the month and year to get all events
* for the given month.
* RETURN
* array - of events for the month
*/
CalendarEventDataSource.prototype.getEventsForMonth = function( date )
{
var eventDisplays = new Array();
var displayDates = new Object();
2001-12-20 18:53:03 +03:00
var eventList = this.gICalLib.getEventsForMonth( date, displayDates );
2001-12-20 18:53:03 +03:00
while( eventList.hasMoreElements() )
{
var tmpevent = eventList.getNext().QueryInterface(Components.interfaces.oeIICalEvent);
var displayDate = new Date( displayDates.value.getNext().QueryInterface(Components.interfaces.nsISupportsPRTime).data );
var EventObject = new Object;
EventObject.event = tmpevent;
EventObject.displayDate = displayDate;
eventDisplays[ eventDisplays.length ] = EventObject;
}
eventDisplays.sort( this.orderEventsByDate );
2001-12-20 18:53:03 +03:00
return eventDisplays;
}
/** PUBLIC
*
* CalendarEventDataSource/getNextEvents.
*
* PARAMETERS
* EventsToGet - The number of events to return
*
* RETURN
* array - of the next "EventsToGet" events
*/
CalendarEventDataSource.prototype.getNextEvents = function( EventsToGet )
{
var eventDisplays = new Array();
var today = new Date();
2001-12-20 18:53:03 +03:00
var displayDates = new Object();
2001-12-20 18:53:03 +03:00
var eventList = this.gICalLib.getNextNEvents( today, EventsToGet, displayDates );
2001-12-20 18:53:03 +03:00
while( eventList.hasMoreElements() )
{
var tmpevent = eventList.getNext().QueryInterface(Components.interfaces.oeIICalEvent);
var displayDate = new Date( displayDates.value.getNext().QueryInterface(Components.interfaces.nsISupportsPRTime).data );
var EventObject = new Object;
EventObject.event = tmpevent;
EventObject.displayDate = displayDate;
eventDisplays[ eventDisplays.length ] = EventObject;
}
eventDisplays.sort( this.orderRawEventsByDate );
2001-12-20 18:53:03 +03:00
return eventDisplays;
}
CalendarEventDataSource.prototype.getCurrentEvents = function( )
{
if( this.onlyFutureEvents == true )
{
return( this.getAllFutureEvents() );
}
else
{
return( this.getAllEvents() );
}
}
2001-12-20 18:53:03 +03:00
/** PUBLIC
*
* CalendarEventDataSource/getAllEvents.
*
* RETURN
* array - of ALL events
*/
CalendarEventDataSource.prototype.getAllEvents = function( )
{
// clone the array in case the caller messes with it
2001-12-20 18:53:03 +03:00
var eventList = this.gICalLib.getAllEvents();
var eventArray = new Array();
while( eventList.hasMoreElements() )
{
var tmpevent = eventList.getNext().QueryInterface(Components.interfaces.oeIICalEvent);
2001-12-20 18:53:03 +03:00
eventArray[ eventArray.length ] = tmpevent;
}
eventArray.sort( this.orderRawEventsByDate );
2001-12-20 18:53:03 +03:00
return eventArray;
}
CalendarEventDataSource.prototype.getEventsWithAlarms = function()
{
//return( this.searchBySql( "SELECT * FROM VEVENT WHERE VALARM.DTSTART" ) );
}
CalendarEventDataSource.prototype.getAllFutureEvents = function()
{
var Today = new Date();
var Year = Today.getYear() + 1900;
var Month = Today.getMonth();
Month++;
if( Month < 10 )
Month = "0"+Month;
var Day = Today.getDate();
if( Day < 10 )
Day = "0"+Day;
return( this.searchBySql( "SELECT * FROM VEVENT WHERE DTSTART >= '"+Year+""+Month+""+Day+"T000000'" ) );
}
2001-12-20 18:53:03 +03:00
/** PUBLIC
*
* CalendarEventDataSource/makeNewEvent.
*
* RETURN
* new event, not SAVED yet, use addEvent to save it.
*/
CalendarEventDataSource.prototype.makeNewEvent = function( date )
{
var iCalEventComponent = Components.classes["@mozilla.org/icalevent;1"].createInstance();
var iCalEvent = iCalEventComponent.QueryInterface(Components.interfaces.oeIICalEvent);
if( date )
{
iCalEvent.start.setTime( date );
}
return iCalEvent;
}
CalendarEventDataSource.prototype.getICalLib = function()
{
return this.gICalLib;
}
/*
** Start time and end time are optional.
*/
CalendarEventDataSource.prototype.openNewEventDialog = function( onOK, startTime, endTime )
{
var args = new Object();
var iCalEventComponent = Components.classes["@mozilla.org/icalevent;1"].createInstance();
args.calendarEvent = iCalEventComponent.QueryInterface(Components.interfaces.oeIICalEvent);
args.mode = "new";
args.onOk = onOK;
if( !startTime )
args.calendarEvent.start.setTime( new Date() );
else
args.calendarEvent.start.setTime( startTime );
if( !endTime )
{
args.calendarEvent.end.setTime( new Date() );
args.calendarEvent.end.hour++;
}
else
args.calendarEvent.end.setTime( endTime );
//this doens't work yet
calendar.openDialog("caNewEvent", "chrome://calendar/content/ca-event-dialog.xul", false, args );
}
/** PACKAGE STATIC
* CalendarEvent orderEventsByDate.
*
* NOTES
* Used to sort table by date
*/
CalendarEventDataSource.prototype.orderEventsByDate = function( eventA, eventB )
{
/*
return( eventA.event.start.getTime() - eventB.event.start.getTime() );
*/
return( eventA.displayDate.getTime() - eventB.displayDate.getTime() );
}
/** PACKAGE STATIC
* CalendarEvent orderRawEventsByDate.
*
* NOTES
* Used to sort table by date
*/
CalendarEventDataSource.prototype.orderRawEventsByDate = function( eventA, eventB )
{
return( eventA.start.getTime() - eventB.start.getTime() );
}
2001-12-20 18:53:03 +03:00
/******************************************************************************************************
******************************************************************************************************
ALARM RELATED CODE
******************************************************************************************************
*******************************************************************************************************/
2002-03-21 23:35:11 +03:00
CalendarEventDataSource.prototype.prepareAlarms = function( )
2001-12-20 18:53:03 +03:00
{
2002-03-21 23:35:11 +03:00
this.alarmObserver = new CalendarAlarmObserver( this );
this.gICalLib.addObserver( this.alarmObserver );
2002-03-22 21:11:07 +03:00
this.alarmObserver.firePendingAlarms( this.alarmObserver );
2001-12-20 18:53:03 +03:00
}
2002-03-21 23:35:11 +03:00
function CalendarAlarmObserver( calendarService )
2001-12-20 18:53:03 +03:00
{
2002-03-21 23:35:11 +03:00
this.pendingAlarmList = new Array();
this.addToPending = true;
this.calendarService = calendarService;
2001-12-20 18:53:03 +03:00
}
2002-03-21 23:35:11 +03:00
CalendarAlarmObserver.prototype.firePendingAlarms = function( observer )
2001-12-20 18:53:03 +03:00
{
2002-03-21 23:35:11 +03:00
this.addToPending = false;
2001-12-20 18:53:03 +03:00
2002-03-21 23:35:11 +03:00
for( var i in this.pendingAlarmList )
2001-12-20 18:53:03 +03:00
{
2002-03-21 23:35:11 +03:00
this.fireAlarm( this.pendingAlarmList[ i ] );
2001-12-20 18:53:03 +03:00
2002-03-21 23:35:11 +03:00
observer.onAlarm( this.pendingAlarmList[ i ] );
2001-12-20 18:53:03 +03:00
}
2002-03-21 23:35:11 +03:00
this.pendingAlarmList = null;
2001-12-20 18:53:03 +03:00
}
CalendarAlarmObserver.prototype.onStartBatch = function()
{
}
CalendarAlarmObserver.prototype.onEndBatch = function()
{
}
2001-12-20 18:53:03 +03:00
CalendarAlarmObserver.prototype.onLoad = function( calendarEvent )
{
}
CalendarAlarmObserver.prototype.onAddItem = function( calendarEvent )
{
}
CalendarAlarmObserver.prototype.onModifyItem = function( calendarEvent, originalEvent )
{
}
CalendarAlarmObserver.prototype.onDeleteItem = function( calendarEvent )
{
}
CalendarAlarmObserver.prototype.onAlarm = function( calendarEvent )
{
debug( "caEvent.alarmWentOff is "+ calendarEvent );
2002-03-21 23:35:11 +03:00
if( this.addToPending )
{
debug( "defering alarm "+ calendarEvent );
this.pendingAlarmList.push( calendarEvent );
}
else
{
this.fireAlarm( calendarEvent )
}
}
2001-12-20 18:53:03 +03:00
2002-03-21 23:35:11 +03:00
CalendarAlarmObserver.prototype.fireAlarm = function( calendarEvent )
{
debug( "Fire alarm "+ calendarEvent );
if( gCalendarWindow.calendarPreferences.getPref( "alarmsplaysound" ) )
{
playSound();
}
2002-03-22 21:11:07 +03:00
addEventToDialog( calendarEvent );
2001-12-20 18:53:03 +03:00
if ( calendarEvent.alarmEmailAddress )
{
2002-03-21 23:35:11 +03:00
//send an email for the event
// TO DO
2001-12-20 18:53:03 +03:00
}
}
2002-03-21 23:35:11 +03:00
function debug( str )
{
dump( "\n CalendarEvent.js DEBUG: "+ str + "\n");
}