diff --git a/calendar/libxpical/caltst.js b/calendar/libxpical/caltst.js index 9b57411254f4..85054c7a182c 100644 --- a/calendar/libxpical/caltst.js +++ b/calendar/libxpical/caltst.js @@ -35,6 +35,7 @@ * */ +const DEFAULT_SERVER="file:///tmp/.oecalendar"; const DEFAULT_TITLE="Lunch Time"; const DEFAULT_DESCRIPTION = "Will be out for one hour"; const DEFAULT_LOCATION = "Restaurant"; @@ -49,6 +50,7 @@ const DEFAULT_RECUR = true; const DEFAULT_RECURINTERVAL = 7; const DEFAULT_RECURUNITS = "days"; const DEFAULT_RECURFOREVER = true; +const DEFAULT_ATTACHMENT = DEFAULT_SERVER var iCalLib = null; var gObserver = null; @@ -101,7 +103,7 @@ function Test() iCalLib = iCalLibComponent.QueryInterface(Components.interfaces.oeIICal); } - iCalLib.setServer( "/tmp/.oecalendar" ); + iCalLib.setServer( DEFAULT_SERVER ); iCalLib.Test(); alert( "Finished Test" ); } @@ -121,7 +123,7 @@ function TestAll() gTodoObserver = new Observer(); iCalLib.addTodoObserver( gTodoObserver ); } - iCalLib.setServer( "/tmp/.oecalendar" ); + iCalLib.setServer( DEFAULT_SERVER ); var id = TestAddEvent(); if( id == 0 ) { alert( "Stopped Test" ); @@ -184,6 +186,10 @@ function TestAddEvent() var snoozetime = new Date(); iCalEvent.setSnoozeTime( snoozetime ); + Attachment = Components.classes["@mozilla.org/messengercompose/attachment;1"].createInstance( Components.interfaces.nsIMsgAttachment ); + Attachment.url = DEFAULT_ATTACHMENT; + iCalEvent.addAttachment( Attachment ); + var id = iCalLib.addEvent( iCalEvent ); if( id == null ){ @@ -323,7 +329,15 @@ function TestFetchEvent( id ) alert( "TestFetchEvent(): Invalid Recur Forever" ); return null; } - + if( !iCalEvent.attachmentsArray.Count() ) { + alert( "TestFetchEvent(): No attachment found" ); + return null; + } + attachment = iCalEvent.attachmentsArray.QueryElementAt( 0, Components.interfaces.nsIMsgAttachment ); + if ( attachment.url != DEFAULT_ATTACHMENT ) { + alert( "TestFetchEvent(): Invalid attachment" ); + return null; + } //TODO: Check for start and end date return iCalEvent; diff --git a/calendar/libxpical/oeICalEventImpl.cpp b/calendar/libxpical/oeICalEventImpl.cpp index d1381849c7f2..106440f1c9e7 100644 --- a/calendar/libxpical/oeICalEventImpl.cpp +++ b/calendar/libxpical/oeICalEventImpl.cpp @@ -42,6 +42,8 @@ #include "oeICalEventImpl.h" #include "nsMemory.h" #include "nsCOMPtr.h" +#include "plbase64.h" +#include "nsMsgCompCID.h" #define strcasecmp strcmp @@ -169,6 +171,7 @@ oeICalEventImpl::oeICalEventImpl() SetAlarmUnits( "minutes" ); SetRecurUnits( "weeks" ); SetSyncId( "" ); + NS_NewISupportsArray(getter_AddRefs(m_attachments)); } oeICalEventImpl::~oeICalEventImpl() @@ -1191,6 +1194,83 @@ NS_IMETHODIMP oeICalEventImpl::Clone( oeIICalEvent **ev ) return NS_OK; } +NS_IMETHODIMP oeICalEventImpl::GetAttachmentsArray(nsISupportsArray * *aAttachmentsArray) +{ + NS_ENSURE_ARG_POINTER(aAttachmentsArray); + *aAttachmentsArray = m_attachments; + NS_IF_ADDREF(*aAttachmentsArray); + return NS_OK; +} + +NS_IMETHODIMP oeICalEventImpl::AddAttachment(nsIMsgAttachment *attachment) +{ +#ifdef ICAL_DEBUG + printf( "oeICalEventImpl::AddAttachment()\n" ); +#endif + PRUint32 i; + PRUint32 attachmentCount = 0; + m_attachments->Count(&attachmentCount); + + //Don't add twice the same attachment. + nsCOMPtr element; + PRBool sameUrl; + for (i = 0; i < attachmentCount; i ++) + { + m_attachments->QueryElementAt(i, NS_GET_IID(nsIMsgAttachment), getter_AddRefs(element)); + if (element) + { + element->EqualsUrl(attachment, &sameUrl); + if (sameUrl) + return NS_OK; + } + } + + return m_attachments->InsertElementAt(attachment, attachmentCount); +} + +NS_IMETHODIMP oeICalEventImpl::RemoveAttachment(nsIMsgAttachment *attachment) +{ +#ifdef ICAL_DEBUG + printf( "oeICalEventImpl::RemoveAttachment()\n" ); +#endif + PRUint32 i; + PRUint32 attachmentCount = 0; + m_attachments->Count(&attachmentCount); + + nsCOMPtr element; + PRBool sameUrl; + for (i = 0; i < attachmentCount; i ++) + { + m_attachments->QueryElementAt(i, NS_GET_IID(nsIMsgAttachment), getter_AddRefs(element)); + if (element) + { + element->EqualsUrl(attachment, &sameUrl); + if (sameUrl) + { + m_attachments->DeleteElementAt(i); + break; + } + } + } + + return NS_OK; +} + +NS_IMETHODIMP oeICalEventImpl::RemoveAttachments() +{ +#ifdef ICAL_DEBUG + printf( "oeICalEventImpl::RemoveAttachments()\n" ); +#endif + PRUint32 i; + PRUint32 attachmentCount = 0; + m_attachments->Count(&attachmentCount); + + for (i = 0; i < attachmentCount; i ++) + m_attachments->DeleteElementAt(0); + + return NS_OK; +} + bool oeICalEventImpl::ParseIcalComponent( icalcomponent *comp ) { #ifdef ICAL_DEBUG_ALL @@ -1529,6 +1609,25 @@ bool oeICalEventImpl::ParseIcalComponent( icalcomponent *comp ) m_snoozetimes.push_back( snoozetimeinms ); } } + + //attachments + for( prop = icalcomponent_get_first_property( vevent, ICAL_X_PROPERTY ); + prop != 0 ; + prop = icalcomponent_get_next_property( vevent, ICAL_X_PROPERTY ) ) { + icalparameter *tmppar = icalproperty_get_first_parameter( prop, ICAL_MEMBER_PARAMETER ); + if ( tmppar != 0 ) { + tmpstr = icalparameter_get_member( tmppar ); + if( strcmp( tmpstr, "Attachment" ) == 0 ) { + nsresult rv; + tmpstr = (char *)icalproperty_get_value_as_string( prop ); + nsCOMPtr attachment = do_CreateInstance(NS_MSGATTACHMENT_CONTRACTID, &rv); + if ( NS_SUCCEEDED(rv) && attachment ) { + attachment->SetUrl( tmpstr ); + AddAttachment( attachment ); + } + } + } + } return true; } @@ -1836,7 +1935,8 @@ icalcomponent* oeICalEventImpl::AsIcalComponent() //snoozetimes icalcomponent *tmpcomp=NULL; - for( unsigned int i=0; iCount(&attachmentCount); + nsCOMPtr element; + for (i = 0; i < attachmentCount; i ++) { + m_attachments->QueryElementAt(i, NS_GET_IID(nsIMsgAttachment), getter_AddRefs(element)); + if (element) + { + char *url; + element->GetUrl( &url ); + icalparameter *tmppar = icalparameter_new_member( "Attachment" ); + prop = icalproperty_new_x( url ); + icalproperty_add_parameter( prop, tmppar ); + icalcomponent_add_property( vevent, prop ); +/* icalattach *attach= icalattach_new_from_url( url ); + if( attach ) { + char tst[100]= "testing"; + char *buffer; + buffer = PL_Base64Encode( tst, strlen(tst), nsnull ); + +// strcpy( buffer, "salam" ); +// icalattachtype_set_base64( attachtype, buffer, 0 ); + prop = icalproperty_new_attach( attach ); + +// tmppar = icalparameter_new_fmttype( url ); +// icalproperty_add_parameter( prop, tmppar ); +// tmppar = icalparameter_new_encoding( ICAL_ENCODING_BASE64 ); +// icalproperty_add_parameter( prop, tmppar ); + + icalcomponent_add_property( vevent, prop ); + }*/ + nsMemory::Free( url ); + } + } + + //add event to newcalendar icalcomponent_add_component( newcalendar, vevent ); return newcalendar; diff --git a/calendar/libxpical/oeICalEventImpl.h b/calendar/libxpical/oeICalEventImpl.h index 47f3753d1047..e088f1779d03 100644 --- a/calendar/libxpical/oeICalEventImpl.h +++ b/calendar/libxpical/oeICalEventImpl.h @@ -41,6 +41,7 @@ #include "nsISimpleEnumerator.h" #include "nsISupportsPrimitives.h" #include "nsSupportsPrimitives.h" +#include "nsISupportsArray.h" #include extern "C" { @@ -117,6 +118,7 @@ private: std::vector m_snoozetimes; icaltimetype CalculateAlarmTime( icaltimetype date ); bool IsExcepted( PRTime date ); + nsCOMPtr m_attachments; }; /*******************************************************************************************/ diff --git a/calendar/libxpical/oeICalImpl.cpp b/calendar/libxpical/oeICalImpl.cpp index 5ee576978823..35656c9e35d3 100644 --- a/calendar/libxpical/oeICalImpl.cpp +++ b/calendar/libxpical/oeICalImpl.cpp @@ -296,7 +296,8 @@ oeICalImpl::~oeICalImpl() #ifdef ICAL_DEBUG printf( "oeICalImpl::~oeICalImpl()\n" ); #endif - for( unsigned int i=0; iRelease(); } m_observerlist.clear(); @@ -764,7 +765,8 @@ oeICalImpl::SetServer( const char *str ) { icalfileset_free(stream); - for( unsigned int i=0; iOnLoad(); #ifdef ICAL_DEBUG @@ -815,7 +817,8 @@ NS_IMETHODIMP oeICalImpl::SetBatchMode(PRBool aBatchMode) // tell observers about the change - for( unsigned int i=0; i