2001-12-20 18:53:03 +03:00
/ * * * * * * B E G I N L I C E N S E B L O C K * * * * *
* 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 31 st , 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 * * * * * * /
/ * * * * * c a l e n d a r / c a - c a l e n d e r - e v e n t . j s
* 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 )
{
2002-03-28 00:39:08 +03:00
try {
var iCalLibComponent = Components . classes [ "@mozilla.org/ical;1" ] . createInstance ( ) ;
} catch ( e ) {
2002-04-02 23:51:48 +04:00
alert ( "The ICAL Componenet is not registered properly. Please add yourself to the cc list at http://bugzilla.mozilla.org/show_bug.cgi?id=134432.\nError returned:" + e + "\n" ) ;
2002-03-28 00:39:08 +03:00
return ;
2002-03-21 18:26:19 +03:00
}
2002-03-28 00:39:08 +03:00
2001-12-20 18:53:03 +03:00
this . gICalLib = iCalLibComponent . QueryInterface ( Components . interfaces . oeIICal ) ;
2002-01-30 20:06:14 +03:00
/ *
* * 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
2002-01-30 20:06:14 +03:00
const JS _DIR _UTILS _I _PROPS = "nsIProperties" ;
2001-12-20 23:13:53 +03:00
2002-01-30 20:06:14 +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
2002-01-30 20:06:14 +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 ( ) ;
}
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" ) ;
}
}
/ * * P U B L I C
*
* NOTES
* Called at start up after all services have been inited .
* /
CalendarEventDataSource . prototype . onServiceStartup = function ( root )
{
}
/ * * P U B L I C
*
* 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 . getAllEvents ( ) ;
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 ;
}
}
}
}
}
}
2002-03-05 17:18:27 +03:00
searchEventTable . sort ( this . orderRawEventsByDate ) ;
2001-12-20 18:53:03 +03:00
return searchEventTable ;
}
/ * * P U B L I C
*
* 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 ( ) ;
var eventList = this . gICalLib . getEventsForDay ( date , displayDates ) ;
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 ;
}
2002-03-20 21:53:59 +03:00
eventDisplays . sort ( this . orderEventsByDate ) ;
2001-12-20 18:53:03 +03:00
return eventDisplays ;
}
/ * * P U B L I C
*
* 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 ( ) ;
var eventList = this . gICalLib . getEventsForWeek ( date , displayDates ) ;
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 ;
}
2002-03-20 21:53:59 +03:00
eventDisplays . sort ( this . orderEventsByDate ) ;
2001-12-20 18:53:03 +03:00
return eventDisplays ;
}
/ * * P U B L I C
*
* 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 ( ) ;
var eventList = this . gICalLib . getEventsForMonth ( date , displayDates ) ;
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 ;
}
2002-03-20 21:53:59 +03:00
eventDisplays . sort ( this . orderEventsByDate ) ;
2002-03-28 00:39:08 +03:00
2001-12-20 18:53:03 +03:00
return eventDisplays ;
}
/ * * P U B L I C
*
* 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 ( ) ;
var displayDates = new Object ( ) ;
var eventList = this . gICalLib . getNextNEvents ( today , EventsToGet , displayDates ) ;
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 ;
}
2002-03-20 21:53:59 +03:00
eventDisplays . sort ( this . orderRawEventsByDate ) ;
2001-12-20 18:53:03 +03:00
return eventDisplays ;
}
/ * * P U B L I C
*
* CalendarEventDataSource / getAllEvents .
*
* RETURN
* array - of ALL events
* /
CalendarEventDataSource . prototype . getAllEvents = function ( )
{
// clone the array in case the caller messes with it
var eventList = this . gICalLib . getAllEvents ( ) ;
var eventArray = new Array ( ) ;
while ( eventList . hasMoreElements ( ) )
{
var tmpevent = eventList . getNext ( ) . QueryInterface ( Components . interfaces . oeIICalEvent ) ;
2002-03-05 17:18:27 +03:00
2001-12-20 18:53:03 +03:00
eventArray [ eventArray . length ] = tmpevent ;
}
2002-03-05 17:18:27 +03:00
eventArray . sort ( this . orderRawEventsByDate ) ;
2001-12-20 18:53:03 +03:00
return eventArray ;
}
/ * * P U B L I C
*
* 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 ) ;
}
2002-03-05 17:18:27 +03:00
/ * * P A C K A G E S T A T I C
* 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 ( ) ) ;
}
/ * * P A C K A G E S T A T I C
* 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 . onLoad = function ( calendarEvent )
{
}
CalendarAlarmObserver . prototype . onAddItem = function ( calendarEvent )
{
}
CalendarAlarmObserver . prototype . onModifyItem = function ( calendarEvent , originalEvent )
{
}
CalendarAlarmObserver . prototype . onDeleteItem = function ( calendarEvent )
{
}
CalendarAlarmObserver . prototype . onAlarm = function ( calendarEvent )
{
2002-03-28 00:39:08 +03:00
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 ) ;
2002-03-22 21:11:07 +03:00
addEventToDialog ( calendarEvent ) ;
2001-12-20 18:53:03 +03:00
if ( calendarEvent . alarmEmailAddress )
{
//debug( "about to send an email to "+ calendarEvent.alarmEmailAddress + "with subject "+ calendarEvent.title );
2002-03-21 23:35:11 +03:00
//send an email for the event
// TO DO
2001-12-20 18:53:03 +03:00
}
2001-12-21 00:52:12 +03:00
}
2002-03-21 23:35:11 +03:00
2002-03-28 00:39:08 +03:00
function debug ( str )
2001-12-21 00:52:12 +03:00
{
dump ( "\n CalendarEvent.js DEBUG: " + str + "\n" ) ;
}