Back to index

lightning-sunbird  0.9+nobinonly
calRecurrenceDate.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is Oracle Corporation code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  *  Oracle Corporation
00019  * Portions created by the Initial Developer are Copyright (C) 2004
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Vladimir Vukicevic <vladimir.vukicevic@oracle.com>
00024  *   Philipp Kewisch <mozilla@kewis.ch>
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either the GNU General Public License Version 2 or later (the "GPL"), or
00028  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 #include "calRecurrenceDate.h"
00041 
00042 #include "calDateTime.h"
00043 #include "calIItemBase.h"
00044 #include "calIEvent.h"
00045 #include "calIErrors.h"
00046 #include "nsServiceManagerUtils.h"
00047 
00048 #include "calICSService.h"
00049 
00050 #ifndef MOZILLA_1_8_BRANCH
00051 #include "nsIClassInfoImpl.h"
00052 #endif
00053 
00054 #include "calBaseCID.h"
00055 
00056 static NS_DEFINE_CID(kCalICSService, CAL_ICSSERVICE_CID);
00057 
00058 extern "C" {
00059     #include "ical.h"
00060 }
00061 
00062 NS_IMPL_ISUPPORTS2_CI(calRecurrenceDate, calIRecurrenceItem, calIRecurrenceDate)
00063 
00064 calRecurrenceDate::calRecurrenceDate()
00065     : mImmutable(PR_FALSE),
00066       mIsNegative(PR_FALSE)
00067 {
00068 }
00069 
00070 NS_IMETHODIMP
00071 calRecurrenceDate::GetIsMutable(PRBool *aResult)
00072 {
00073     NS_ENSURE_ARG_POINTER(aResult);
00074 
00075     *aResult = !mImmutable;
00076     return NS_OK;
00077 }
00078 
00079 NS_IMETHODIMP
00080 calRecurrenceDate::MakeImmutable()
00081 {
00082     if (mImmutable)
00083         return NS_ERROR_FAILURE; // XXX another error code
00084 
00085     mImmutable = PR_TRUE;
00086     return NS_OK;
00087 }
00088 
00089 NS_IMETHODIMP
00090 calRecurrenceDate::Clone(calIRecurrenceItem **_retval)
00091 {
00092     calRecurrenceDate *crd = new calRecurrenceDate;
00093     if (!crd)
00094         return NS_ERROR_OUT_OF_MEMORY;
00095 
00096     crd->mIsNegative = mIsNegative;
00097     if (mDate)
00098         mDate->Clone(getter_AddRefs(crd->mDate));
00099     else
00100         crd->mDate = nsnull;
00101 
00102     NS_ADDREF(*_retval = crd);
00103     return NS_OK;
00104 }
00105 
00106 /* attribute boolean isNegative; */
00107 NS_IMETHODIMP
00108 calRecurrenceDate::GetIsNegative(PRBool *_retval)
00109 {
00110     NS_ENSURE_ARG_POINTER(_retval);
00111 
00112     *_retval = mIsNegative;
00113     return NS_OK;
00114 }
00115 
00116 NS_IMETHODIMP
00117 calRecurrenceDate::SetIsNegative(PRBool aIsNegative)
00118 {
00119     if (mImmutable)
00120         return NS_ERROR_FAILURE; // XXX CAL_ERROR_ITEM_IS_IMMUTABLE
00121 
00122     mIsNegative = aIsNegative;
00123     return NS_OK;
00124 }
00125 
00126 /* readonly attribute boolean isFinite; */
00127 NS_IMETHODIMP
00128 calRecurrenceDate::GetIsFinite(PRBool *_retval)
00129 {
00130     NS_ENSURE_ARG_POINTER(_retval);
00131     *_retval = PR_TRUE;
00132     return NS_OK;
00133 }
00134 
00135 NS_IMETHODIMP
00136 calRecurrenceDate::GetDate(calIDateTime **aDate)
00137 {
00138     NS_ENSURE_ARG_POINTER(aDate);
00139 
00140     NS_IF_ADDREF(*aDate = mDate);
00141     return NS_OK;
00142 }
00143 
00144 NS_IMETHODIMP
00145 calRecurrenceDate::SetDate(calIDateTime *aDate)
00146 {
00147     NS_ENSURE_ARG_POINTER(aDate);
00148 
00149     mDate = aDate;
00150     return NS_OK;
00151 }
00152 
00153 NS_IMETHODIMP
00154 calRecurrenceDate::GetNextOccurrence(calIDateTime *aStartTime,
00155                                      calIDateTime *aOccurrenceTime,
00156                                      calIDateTime **_retval)
00157 {
00158     NS_ENSURE_ARG_POINTER(aStartTime);
00159     NS_ENSURE_ARG_POINTER(aOccurrenceTime);
00160     NS_ENSURE_ARG_POINTER(_retval);
00161 
00162     if (mDate) {
00163         PRInt32 result;
00164         if (NS_SUCCEEDED(mDate->Compare(aStartTime, &result)) && result > 0) {
00165             NS_ADDREF (*_retval = mDate);
00166             return NS_OK;
00167         }
00168     }
00169 
00170     *_retval = nsnull;
00171     return NS_OK;
00172 }
00173 
00174 NS_IMETHODIMP
00175 calRecurrenceDate::GetOccurrences(calIDateTime *aStartTime,
00176                                   calIDateTime *aRangeStart,
00177                                   calIDateTime *aRangeEnd,
00178                                   PRUint32 aMaxCount,
00179                                   PRUint32 *aCount, calIDateTime ***aDates)
00180 {
00181     NS_ENSURE_ARG_POINTER(aStartTime);
00182     NS_ENSURE_ARG_POINTER(aRangeStart);
00183 
00184     PRInt32 r1, r2;
00185 
00186     if (mDate) {
00187         if (NS_SUCCEEDED(mDate->Compare(aRangeStart, &r1)) && r1 >= 0 &&
00188             (!aRangeEnd || NS_SUCCEEDED(mDate->Compare(aRangeEnd, &r2)) && r2 < 0))
00189         {
00190             calIDateTime **dates = (calIDateTime **) nsMemory::Alloc(sizeof(calIDateTime*));
00191             NS_ADDREF (dates[0] = mDate);
00192             *aDates = dates;
00193             *aCount = 1;
00194             return NS_OK;
00195         }
00196     }
00197 
00198     *aDates = nsnull;
00199     *aCount = 0;
00200     return NS_OK;
00201 }
00202 
00206 NS_IMETHODIMP
00207 calRecurrenceDate::GetIcalProperty(calIIcalProperty **aProp)
00208 {
00209     NS_ENSURE_ARG_POINTER(aProp);
00210     if (!mDate)
00211         return NS_ERROR_FAILURE;
00212 
00213     nsresult rc = cal::getICSService()->CreateIcalProperty(
00214         (mIsNegative ? nsDependentCString("EXDATE") : nsDependentCString("RDATE")), aProp);
00215     if (NS_FAILED(rc))
00216         return rc;
00217 
00218     return (*aProp)->SetValueAsDatetime(mDate);
00219 }
00220 
00221 NS_IMETHODIMP
00222 calRecurrenceDate::SetIcalProperty(calIIcalProperty *aProp)
00223 {
00224     NS_ENSURE_ARG_POINTER(aProp);
00225 
00226     nsCAutoString name;
00227     nsresult rc = aProp->GetPropertyName(name);
00228     if (NS_FAILED(rc))
00229         return rc;
00230     if (name.EqualsLiteral("RDATE"))
00231         mIsNegative = PR_FALSE;
00232     else if (name.EqualsLiteral("EXDATE"))
00233         mIsNegative = PR_TRUE;
00234     else
00235         return NS_ERROR_INVALID_ARG;
00236 
00237     return aProp->GetValueAsDatetime(getter_AddRefs(mDate));
00238 }