1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 #include "nsComponentManagerUtils.h"
5 
6 #include "calUtils.h"
7 #include "nsIScriptError.h"
8 
9 extern "C" {
10 #include "ical.h"
11 }
12 
13 namespace cal {
14 
logError(const nsAString & msg)15 nsresult logError(const nsAString& msg) {
16   nsresult rc;
17   nsCOMPtr<nsIScriptError> const scriptError(
18       do_CreateInstance("@mozilla.org/scripterror;1", &rc));
19   NS_ENSURE_SUCCESS(rc, rc);
20   rc = scriptError->Init(msg, EmptyString(), EmptyString(), 0, 0,
21                          nsIScriptError::errorFlag, "calendar", false, false);
22   return getConsoleService()->LogMessage(scriptError);
23 }
24 
logWarning(const nsAString & msg)25 nsresult logWarning(const nsAString& msg) {
26   nsresult rc;
27   nsCOMPtr<nsIScriptError> const scriptError(
28       do_CreateInstance("@mozilla.org/scripterror;1", &rc));
29   NS_ENSURE_SUCCESS(rc, rc);
30   rc = scriptError->Init(msg, EmptyString(), EmptyString(), 0, 0,
31                          nsIScriptError::warningFlag, "calendar", false, false);
32   return getConsoleService()->LogMessage(scriptError);
33 }
34 
log(char16_t const * msg)35 nsresult log(char16_t const* msg) {
36   return getConsoleService()->LogStringMessage(msg);
37 }
38 
detectTimezone(icaltimetype const & icalt,calITimezoneProvider * tzProvider)39 nsCOMPtr<calITimezone> detectTimezone(icaltimetype const& icalt,
40                                       calITimezoneProvider* tzProvider) {
41   if (icalt.is_utc) {
42     return UTC();
43   }
44   if (icalt.zone) {
45     char const* const tzid =
46         icaltimezone_get_tzid(const_cast<icaltimezone*>(icalt.zone));
47     if (tzid) {
48       nsCOMPtr<calITimezone> tz;
49       if (tzProvider) {
50         tzProvider->GetTimezone(nsDependentCString(tzid), getter_AddRefs(tz));
51       } else {
52         getTimezoneService()->GetTimezone(nsDependentCString(tzid),
53                                           getter_AddRefs(tz));
54       }
55       if (tz) {
56         return tz;
57       }
58       NS_ASSERTION(tz, "no timezone found, falling back to floating!");
59       logMissingTimezone(tzid);
60     }
61   }
62   return floating();
63 }
64 
logMissingTimezone(char const * tzid)65 void logMissingTimezone(char const* tzid) {
66   // xxx todo: needs l10n
67   nsString msg(u"Timezone \""_ns);
68   msg += NS_ConvertUTF8toUTF16(tzid);
69   msg += u"\" not found, falling back to floating!"_ns;
70   logError(msg);
71 }
72 
getIcalTimezone(calITimezone * tz)73 icaltimezone* getIcalTimezone(calITimezone* tz) {
74   icaltimezone* icaltz = nullptr;
75   if (!tz) {
76     NS_ASSERTION(false, "No Timezone passed to getIcalTimezone");
77     return nullptr;
78   }
79 
80   bool b;
81   tz->GetIsUTC(&b);
82   if (b) {
83     icaltz = icaltimezone_get_utc_timezone();
84   } else {
85     nsCOMPtr<calIIcalComponent> tzComp;
86     tz->GetIcalComponent(getter_AddRefs(tzComp));
87     if (tzComp) {
88       nsCOMPtr<calIIcalComponentLibical> tzCompLibical =
89           do_QueryInterface(tzComp);
90       icaltz = tzCompLibical->GetLibicalTimezone();
91     }  // else floating or phantom timezone
92   }
93   return icaltz;
94 }
95 
~XpcomBase()96 XpcomBase::~XpcomBase() {}
97 
98 }  // namespace cal
99