Back to index

lightning-sunbird  0.9+nobinonly
calUtils.cpp
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is Sun Microsystems code.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Sun Microsystems, Inc.
00018  * Portions created by the Initial Developer are Copyright (C) 2007
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *   Daniel Boelzle <daniel.boelzle@sun.com>
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 #include "nsComponentManagerUtils.h"
00038 #include "nsServiceManagerUtils.h"
00039 #include "nsIScriptError.h"
00040 
00041 #include "calBaseCID.h"
00042 #include "calUtils.h"
00043 
00044 extern "C" {
00045 #include "ical.h"
00046 }
00047 
00048 namespace cal {
00049 
00050 class UTF8StringEnumerator : public nsIUTF8StringEnumerator,
00051                              public XpcomBase
00052 {
00053 public:
00054     NS_DECL_ISUPPORTS
00055     NS_DECL_NSIUTF8STRINGENUMERATOR
00056 
00057     explicit UTF8StringEnumerator(nsAutoPtr<nsCStringArray> & takeOverArray)
00058         : mArray(takeOverArray), mPos(0) {}
00059 private:
00060     nsAutoPtr<nsCStringArray> const mArray;
00061     PRInt32 mPos;
00062 };
00063 
00064 NS_IMPL_ISUPPORTS1(UTF8StringEnumerator, nsIUTF8StringEnumerator)
00065 
00066 NS_IMETHODIMP UTF8StringEnumerator::HasMore(PRBool *_retval)
00067 {
00068     NS_ENSURE_ARG_POINTER(_retval);
00069     *_retval = (mPos < mArray->Count());
00070     return NS_OK;
00071 }
00072 
00073 NS_IMETHODIMP UTF8StringEnumerator::GetNext(nsACString & _retval)
00074 {
00075     if (mPos < mArray->Count()) {
00076         mArray->CStringAt(mPos, _retval);
00077         ++mPos;
00078         return NS_OK;
00079     } else {
00080         return NS_ERROR_UNEXPECTED;
00081     }
00082 }
00083 
00084 nsresult createUTF8StringEnumerator(nsAutoPtr<nsCStringArray> & takeOverArray,
00085                                     nsIUTF8StringEnumerator ** ppRet)
00086 {
00087     NS_ENSURE_ARG_POINTER(takeOverArray.get());
00088     NS_ENSURE_ARG_POINTER(ppRet);
00089     *ppRet = new UTF8StringEnumerator(takeOverArray);
00090     CAL_ENSURE_MEMORY(*ppRet);
00091     NS_ADDREF(*ppRet);
00092     return NS_OK;
00093 }
00094 
00095 nsresult logError(PRUnichar const* msg) {
00096     nsresult rc;
00097     nsCOMPtr<nsIScriptError> const scriptError(do_CreateInstance("@mozilla.org/scripterror;1", &rc));
00098     NS_ENSURE_SUCCESS(rc, rc);
00099     rc = scriptError->Init(msg, nsnull, nsnull, 0, 0, nsIScriptError::errorFlag, "calendar");
00100     return getConsoleService()->LogMessage(scriptError);
00101 }
00102 
00103 nsresult logWarning(PRUnichar const* msg) {
00104     nsresult rc;
00105     nsCOMPtr<nsIScriptError> const scriptError(do_CreateInstance("@mozilla.org/scripterror;1", &rc));
00106     NS_ENSURE_SUCCESS(rc, rc);
00107     rc = scriptError->Init(msg, nsnull, nsnull, 0, 0, nsIScriptError::warningFlag, "calendar");
00108     return getConsoleService()->LogMessage(scriptError);
00109 }
00110 
00111 nsresult log(PRUnichar const* msg) {
00112     return getConsoleService()->LogStringMessage(msg);
00113 }
00114 
00115 nsCOMPtr<nsIConsoleService> const& getConsoleService() {
00116     static nsCOMPtr<nsIConsoleService> sObj;
00117     if (!sObj) {
00118         sObj = do_GetService("@mozilla.org/consoleservice;1");
00119         NS_ASSERTION(sObj, "Could not get console service!");
00120     }
00121     return sObj;
00122 }
00123 
00124 nsCOMPtr<calIICSService> const& getICSService() {
00125     static nsCOMPtr<calIICSService> sIcsService;
00126     if (!sIcsService) {
00127         sIcsService = do_GetService(CAL_ICSSERVICE_CONTRACTID);
00128         NS_ASSERTION(sIcsService, "Could not init ics service! Will crash now...");
00129     }
00130     return sIcsService;
00131 }
00132 
00133 nsCOMPtr<calITimezoneService> const& getTimezoneService() {
00134     static nsCOMPtr<calITimezoneService> sTzService;
00135     if (!sTzService) {
00136         sTzService = do_GetService(CAL_TIMEZONESERVICE_CONTRACTID);
00137         NS_ASSERTION(sTzService, "Could not init timezone service! Will crash now...");
00138     }
00139     return sTzService;
00140 }
00141 
00142 nsCOMPtr<calITimezone> const& floating() {
00143     static nsCOMPtr<calITimezone> sFloating;
00144     if (!sFloating) {
00145         getTimezoneService()->GetFloating(getter_AddRefs(sFloating));
00146     }
00147     return sFloating;
00148 }
00149 
00150 nsCOMPtr<calITimezone> const& UTC() {
00151     static nsCOMPtr<calITimezone> sUTC;
00152     if (!sUTC) {
00153         getTimezoneService()->GetUTC(getter_AddRefs(sUTC));
00154     }
00155     return sUTC;
00156 }
00157 
00158 nsCOMPtr<calITimezone> detectTimezone(icaltimetype const& icalt,
00159                                       calITimezoneProvider * tzProvider)
00160 {
00161     if (!tzProvider) {
00162         tzProvider = getTimezoneService();
00163     }
00164     if (icalt.is_utc) {
00165         return UTC();
00166     }
00167     if (icalt.zone) {
00168         char const* const tzid = icaltimezone_get_tzid(const_cast<icaltimezone *>(icalt.zone));
00169         if (tzid) {
00170             nsCOMPtr<calITimezone> tz;
00171             tzProvider->GetTimezone(nsDependentCString(tzid), getter_AddRefs(tz));
00172             if (tz) {
00173                 return tz;
00174             }
00175             NS_ASSERTION(tz, "no timezone found, falling back to floating!");
00176             logMissingTimezone(tzid);
00177         }
00178     }
00179     return floating();
00180 }
00181 
00182 void logMissingTimezone(char const* tzid) {
00183     // xxx todo: needs l10n
00184     nsString msg(NS_LITERAL_STRING("Timezone \""));
00185     msg += NS_ConvertUTF8toUTF16(tzid);
00186     msg += NS_LITERAL_STRING("\" not found, falling back to floating!");
00187     logError(msg.get());
00188 }
00189 
00190 icaltimezone * getIcalTimezone(calITimezone * tz) {
00191     icaltimezone * icaltz = nsnull;
00192     if (!tz) {
00193         NS_ASSERTION(false, "No Timezone passed to getIcalTimezone");
00194         return nsnull;
00195     }
00196 
00197     PRBool b;
00198     tz->GetIsUTC(&b);
00199     if (b) {
00200         icaltz = icaltimezone_get_utc_timezone();
00201     } else {
00202         nsCOMPtr<calIIcalComponent> tzComp;
00203         tz->GetIcalComponent(getter_AddRefs(tzComp));
00204         if (tzComp) {
00205             icaltz = tzComp->GetIcalTimezone();
00206         } // else floating
00207     }
00208     return icaltz;
00209 }
00210 
00211 XpcomBase::~XpcomBase() {
00212 }
00213 
00214 }