gecko-dev/calendar/libxpical/oeICalTodoImpl.cpp

278 lines
7.9 KiB
C++
Raw Normal View History

/* ***** 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 Mozilla Calendar Code.
*
* The Initial Developer of the Original Code is
* Chris Charabaruk <ccharabaruk@meldstar.com>.
* Portions created by the Initial Developer are Copyright (C) 2001
* the Initial Developer. All Rights Reserved.
*
* Contributor(s): Chris Charabaruk <ccharabaruk@meldstar.com>
* Mostafa Hosseini <mostafah@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 ***** */
#ifndef WIN32
#include <unistd.h>
#endif
#include "oeICalEventImpl.h"
#include "oeICalTodoImpl.h"
#include "nsMemory.h"
#include "nsCOMPtr.h"
#define strcasecmp strcmp
#define RECUR_NONE 0
#define RECUR_DAILY 1
#define RECUR_WEEKLY 2
#define RECUR_MONTHLY_MDAY 3
#define RECUR_MONTHLY_WDAY 4
#define RECUR_YEARLY 5
/* Implementation file */
NS_IMPL_ISUPPORTS1(oeICalTodoImpl, oeIICalTodo)
icaltimetype ConvertFromPrtime( PRTime indate );
PRTime ConvertToPrtime ( icaltimetype indate );
nsresult
NS_NewICalTodo( oeIICalTodo** inst )
{
NS_PRECONDITION(inst != nsnull, "null ptr");
if (! inst)
return NS_ERROR_NULL_POINTER;
*inst = new oeICalTodoImpl();
if (! *inst)
return NS_ERROR_OUT_OF_MEMORY;
NS_ADDREF(*inst);
return NS_OK;
}
oeICalTodoImpl::oeICalTodoImpl()
{
#ifdef ICAL_DEBUG
printf( "oeICalTodoImpl::oeICalTodoImpl()\n" );
#endif
NS_INIT_ISUPPORTS();
mEvent = new oeICalEventImpl();
/* member initializers and constructor code */
nsresult rv;
if( NS_FAILED( rv = NS_NewDateTime((oeIDateTime**) &m_due ))) {
m_due = nsnull;
}
if( NS_FAILED( rv = NS_NewDateTime((oeIDateTime**) &m_completed ))) {
m_completed = nsnull;
}
m_percent = 0;
}
oeICalTodoImpl::~oeICalTodoImpl()
{
#ifdef ICAL_DEBUG
printf( "oeICalTodoImpl::~oeICalTodoImpl( %d )\n", mRefCnt );
#endif
/* destructor code */
if( m_due )
m_due->Release();
if( m_completed )
m_completed->Release();
mEvent->Release();
}
/* readonly attribute oeIDateTime due; */
NS_IMETHODIMP oeICalTodoImpl::GetDue(oeIDateTime * *due)
{
*due = m_due;
NS_ADDREF(*due);
return NS_OK;
}
/* areadonly attribute oeIDateTime completed; */
NS_IMETHODIMP oeICalTodoImpl::GetCompleted(oeIDateTime * *completed)
{
*completed = m_completed;
NS_ADDREF(*completed);
return NS_OK;
}
/* attribute short percent; */
NS_IMETHODIMP oeICalTodoImpl::GetPercent(PRInt16 *aRetVal)
{
#ifdef ICAL_DEBUG_ALL
printf( "GetPercent() = " );
#endif
*aRetVal = m_percent;
#ifdef ICAL_DEBUG_ALL
printf( "%d\n", *aRetVal );
#endif
return NS_OK;
}
NS_IMETHODIMP oeICalTodoImpl::SetPercent(PRInt16 aNewVal)
{
#ifdef ICAL_DEBUG_ALL
printf( "SetPercent( %d )\n", aNewVal );
#endif
m_percent = aNewVal;
return NS_OK;
}
NS_IMETHODIMP oeICalTodoImpl::Clone( oeIICalTodo **ev )
{
#ifdef ICAL_DEBUG_ALL
printf( "oeICalTodoImpl::Clone()\n" );
#endif
nsresult rv;
oeICalTodoImpl *icaltodo =nsnull;
if( NS_FAILED( rv = NS_NewICalTodo( (oeIICalTodo**) &icaltodo ) ) ) {
return rv;
}
icalcomponent *vcalendar = AsIcalComponent();
if ( !vcalendar ) {
#ifdef ICAL_DEBUG
printf( "oeICalTodoImpl::Clone() failed!\n" );
#endif
icaltodo->Release();
return NS_OK;
}
icalcomponent *vtodo = icalcomponent_get_first_component( vcalendar, ICAL_VTODO_COMPONENT );
if( !(icaltodo->ParseIcalComponent( vtodo )) ) {
#ifdef ICAL_DEBUG
printf( "oeICalTodoImpl::Clone() failed.\n" );
#endif
icaltodo->Release();
return NS_OK;
}
*ev = icaltodo;
return NS_OK;
}
bool oeICalTodoImpl::ParseIcalComponent( icalcomponent *comp )
{
#ifdef ICAL_DEBUG_ALL
printf( "ParseIcalComponent()\n" );
#endif
//something like this first
//mEvent->ParseIcalComponent( comp );
//then go on with the extra properties
icalcomponent *vtodo=nsnull;
icalcomponent_kind kind = icalcomponent_isa( comp );
if( kind == ICAL_VCALENDAR_COMPONENT )
vtodo = icalcomponent_get_first_component( comp , ICAL_VTODO_COMPONENT );
else if( kind == ICAL_VTODO_COMPONENT )
vtodo = comp;
if ( !vtodo ) {
#ifdef ICAL_DEBUG
printf( "oeICalTodoImpl::ParseIcalComponent() failed: vtodo is NULL!\n" );
#endif
return false;
}
//percent
icalproperty *prop = icalcomponent_get_first_property( vtodo, ICAL_PERCENTCOMPLETE_PROPERTY );
if ( prop != 0) {
m_percent = icalproperty_get_percentcomplete( prop );
} else {
m_percent = 0;
}
//completed
prop = icalcomponent_get_first_property( vtodo, ICAL_COMPLETED_PROPERTY );
if (prop != 0) {
icaltimetype completed;
completed = icalproperty_get_completed( prop );
m_completed->m_datetime = completed;
} else {
m_completed->m_datetime = icaltime_null_time();
}
return true;
}
icalcomponent* oeICalTodoImpl::AsIcalComponent()
{
#ifdef ICAL_DEBUG_ALL
printf( "AsIcalComponent()\n" );
#endif
icalcomponent *newcalendar;
newcalendar = icalcomponent_new_vcalendar();
if ( !newcalendar ) {
#ifdef ICAL_DEBUG
printf( "oeICalEventImpl::AsIcalComponent() failed: Cannot create VCALENDAR!\n" );
#endif
return nsnull;
}
icalcomponent *vtodo = icalcomponent_new_vtodo();
//percent
icalproperty *prop = icalproperty_new_percentcomplete( m_percent );
icalcomponent_add_property( vtodo, prop );
//Create due if does not exist
if( icaltime_is_null_time( m_due->m_datetime ) ) {
//Set to the same as start date 23:59
//MUST USE mEvent's STARTDATE m_due->m_datetime = m_start->m_datetime;
m_due->SetHour( 23 ); m_due->SetMinute( 59 );
}
//due
PRBool m_allday;
GetAllDay ( &m_allday );
if( m_allday ) {
m_due->SetHour( 23 );
m_due->SetMinute( 59 );
}
prop = icalproperty_new_due( m_due->m_datetime );
icalcomponent_add_property( vtodo, prop );
//completed
if( m_completed ) {
prop = icalproperty_new_completed( m_completed->m_datetime );
icalcomponent_add_property( vtodo, prop );
}
//add event to newcalendar
icalcomponent_add_component( newcalendar, vtodo );
//!!!!!!!!!!!!!!
//NOW FIND A WAY TO ADD PROPERTIES FROM mEvent
return newcalendar;
}
/* End of implementation class template. */