Back to index

lightning-sunbird  0.9+nobinonly
calPeriod.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 mozilla calendar code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  *   Michiel van Leeuwen <mvl@exedo.nl>
00019  * Portions created by the Initial Developer are Copyright (C) 2006
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "calPeriod.h"
00039 #include "calBaseCID.h"
00040 
00041 #ifndef MOZILLA_1_8_BRANCH
00042 #include "nsIClassInfoImpl.h"
00043 #endif
00044 
00045 #include "calUtils.h"
00046 
00047 NS_IMPL_ISUPPORTS1_CI(calPeriod, calIPeriod)
00048 
00049 calPeriod::calPeriod()
00050     : mImmutable(PR_FALSE)
00051 {
00052 }
00053 
00054 calPeriod::calPeriod(const calPeriod& cpt)
00055     : mImmutable(PR_FALSE)
00056 {
00057     if (cpt.mStart)
00058         cpt.mStart->Clone(getter_AddRefs(mStart));
00059     if (cpt.mEnd)
00060         cpt.mEnd->Clone(getter_AddRefs(mEnd));
00061 }
00062 
00063 calPeriod::calPeriod(struct icalperiodtype const* aPeriodPtr)
00064     : mImmutable(PR_FALSE)
00065 {
00066     FromIcalPeriod(aPeriodPtr);
00067 }
00068 
00069 NS_IMETHODIMP
00070 calPeriod::GetIsMutable(PRBool *aResult)
00071 {
00072     NS_ENSURE_ARG_POINTER(aResult);
00073 
00074     *aResult = !mImmutable;
00075     return NS_OK;
00076 }
00077 
00078 NS_IMETHODIMP
00079 calPeriod::MakeImmutable()
00080 {
00081     mImmutable = PR_TRUE;
00082     return NS_OK;
00083 }
00084 
00085 NS_IMETHODIMP
00086 calPeriod::Clone(calIPeriod **aResult)
00087 {
00088     NS_ENSURE_ARG_POINTER(aResult);
00089     calPeriod *cpt = new calPeriod(*this);
00090     if (!cpt)
00091         return NS_ERROR_OUT_OF_MEMORY;
00092 
00093     NS_ADDREF(*aResult = cpt);
00094     return NS_OK;
00095 }
00096 
00097 
00098 NS_IMETHODIMP calPeriod::GetStart(calIDateTime **_retval)
00099 {
00100     NS_ENSURE_ARG_POINTER(_retval);
00101     *_retval = mStart;
00102     NS_IF_ADDREF(*_retval);
00103     return NS_OK;
00104 }
00105 NS_IMETHODIMP calPeriod::SetStart(calIDateTime *aValue)
00106 {
00107     NS_ENSURE_ARG_POINTER(aValue);
00108     if (mImmutable)
00109         return NS_ERROR_OBJECT_IS_IMMUTABLE;
00110     // rfc2445 says that periods are always in utc. libical ignore that,
00111     // so we need the conversion here.
00112     nsresult const rv = aValue->GetInTimezone(cal::UTC(), getter_AddRefs(mStart));
00113     NS_ENSURE_SUCCESS(rv, rv);
00114     return mStart->MakeImmutable();
00115 }
00116 
00117 NS_IMETHODIMP calPeriod::GetEnd(calIDateTime **_retval)
00118 {
00119     NS_ENSURE_ARG_POINTER(_retval);
00120     *_retval = mEnd;
00121     NS_IF_ADDREF(*_retval);
00122     return NS_OK;
00123 }
00124 NS_IMETHODIMP calPeriod::SetEnd(calIDateTime *aValue)
00125 {
00126     NS_ENSURE_ARG_POINTER(aValue);
00127     if (mImmutable)
00128         return NS_ERROR_OBJECT_IS_IMMUTABLE;
00129     nsresult const rv = aValue->GetInTimezone(cal::UTC(), getter_AddRefs(mEnd));
00130     NS_ENSURE_SUCCESS(rv, rv);
00131     return mEnd->MakeImmutable();
00132 }
00133 
00134 NS_IMETHODIMP calPeriod::GetDuration(calIDuration **_retval)
00135 {
00136     NS_ENSURE_ARG_POINTER(_retval);
00137     if (!mStart || !mEnd)
00138         return NS_ERROR_UNEXPECTED;
00139     return mEnd->SubtractDate(mStart, _retval);
00140 }
00141 
00142 NS_IMETHODIMP
00143 calPeriod::ToString(nsACString& aResult)
00144 {
00145     return GetIcalString(aResult);
00146 }
00147 
00148 NS_IMETHODIMP_(void)
00149 calPeriod::ToIcalPeriod(struct icalperiodtype *icalp)
00150 {
00151     // makes no sense to create a duration without bath a start and end
00152     if (!mStart || !mEnd) {
00153         *icalp = icalperiodtype_null_period();
00154         return;
00155     }
00156     
00157     mStart->ToIcalTime(&icalp->start);
00158     mEnd->ToIcalTime(&icalp->end);
00159 }
00160 
00161 void
00162 calPeriod::FromIcalPeriod(struct icalperiodtype const* icalp)
00163 {
00164     mStart = new calDateTime(&(icalp->start), nsnull);
00165     mStart->MakeImmutable();
00166     mEnd = new calDateTime(&(icalp->end), nsnull);
00167     mEnd->MakeImmutable();
00168     return;
00169 }
00170 
00171 NS_IMETHODIMP
00172 calPeriod::GetIcalString(nsACString& aResult)
00173 {
00174     struct icalperiodtype ip;
00175     ToIcalPeriod(&ip);
00176     
00177     // note that ics is owned by libical, so we don't need to free
00178     const char *ics = icalperiodtype_as_ical_string(ip);
00179     
00180     if (ics) {
00181         aResult.Assign(ics);
00182         return NS_OK;
00183     }
00184 
00185     return NS_ERROR_OUT_OF_MEMORY;
00186 }
00187 
00188 NS_IMETHODIMP
00189 calPeriod::SetIcalString(const nsACString& aIcalString)
00190 {
00191     if (mImmutable)
00192         return NS_ERROR_OBJECT_IS_IMMUTABLE;
00193     struct icalperiodtype ip;
00194     ip = icalperiodtype_from_string(PromiseFlatCString(aIcalString).get());
00195     //XXX Shortcut. Assumes nobody tried to overrule our impl. of calIDateTime
00196     //    Should use NS_NEWXPCOM()
00197     mStart = new calDateTime(&ip.start, nsnull);
00198     mEnd = new calDateTime(&ip.end, nsnull);
00199     return NS_OK;
00200 }