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 #if !defined(INCLUDED_CAL_UTILS_H)
5 #  define INCLUDED_CAL_UTILS_H
6 
7 #  include "nsCRT.h"
8 #  include "nsString.h"
9 
10 #  include "nsIStringEnumerator.h"
11 
12 #  include "calITimezone.h"
13 #  include "calITimezoneProvider.h"
14 #  include "calIICSService.h"
15 #  include "nsIConsoleService.h"
16 #  include "nsServiceManagerUtils.h"
17 #  include "nsIClassInfoImpl.h"
18 #  include "nsCOMPtr.h"
19 
20 #  include "calBaseCID.h"
21 
22 #  define CAL_STRLEN_ARGS(x) x, sizeof(x) - 1
23 #  define CAL_ENSURE_MEMORY(p) NS_ENSURE_TRUE(p, NS_ERROR_OUT_OF_MEMORY)
24 
25 typedef struct _icaltimezone icaltimezone;
26 typedef struct icaltimetype icaltimetype;
27 
28 namespace cal {
29 
30 /**
31  * Gets the global console service.
32  */
getConsoleService()33 inline nsCOMPtr<nsIConsoleService> getConsoleService() {
34   return do_GetService("@mozilla.org/consoleservice;1");
35 }
36 
37 /**
38  * Gets the global ICS service.
39  */
getICSService()40 inline nsCOMPtr<calIICSService> getICSService() {
41   return do_GetService(CAL_ICSSERVICE_CONTRACTID);
42 }
43 
44 /**
45  * Gets the global timezone service.
46  */
getTimezoneService()47 inline nsCOMPtr<calITimezoneService> getTimezoneService() {
48   nsresult rv;
49   nsCOMPtr<calITimezoneService> tzs;
50 
51   tzs = do_GetService(CAL_TIMEZONESERVICE_CONTRACTID, &rv);
52   if (NS_FAILED(rv)) {
53     MOZ_CRASH(
54         "Could not load timezone service, brace yourself and prepare for "
55         "crash");
56   }
57   return tzs;
58 }
59 
60 /**
61  * Logs an error.
62  */
63 nsresult logError(const nsAString& msg);
logError(char const * msg)64 inline nsresult logError(char const* msg) {
65   return logError(NS_ConvertASCIItoUTF16(msg));
66 }
logError(nsACString const & msg)67 inline nsresult logError(nsACString const& msg) {
68   return logError(NS_ConvertASCIItoUTF16(msg));
69 }
70 
71 /**
72  * Logs a warning.
73  */
74 nsresult logWarning(const nsAString& msg);
logWarning(char const * msg)75 inline nsresult logWarning(char const* msg) {
76   return logWarning(NS_ConvertASCIItoUTF16(msg));
77 }
logWarning(nsACString const & msg)78 inline nsresult logWarning(nsACString const& msg) {
79   return logWarning(NS_ConvertASCIItoUTF16(msg));
80 }
81 
82 /**
83  * Just logs.
84  */
85 nsresult log(char16_t const* msg);
log(char const * msg)86 inline nsresult log(char const* msg) {
87   return log(NS_ConvertASCIItoUTF16(msg).get());
88 }
log(nsACString const & msg)89 inline nsresult log(nsACString const& msg) {
90   return log(NS_ConvertASCIItoUTF16(msg).get());
91 }
92 
93 // some timezone helpers
94 
95 /**
96  * Gets the "UTC" timezone.
97  */
UTC()98 inline nsCOMPtr<calITimezone> UTC() {
99   nsresult rv;
100   nsCOMPtr<calITimezone> tz;
101 
102   rv = getTimezoneService()->GetUTC(getter_AddRefs(tz));
103   if (NS_FAILED(rv)) {
104     MOZ_CRASH(
105         "Could not load UTC timezone, brace yourself and prepare for crash");
106   }
107 
108   return tz;
109 }
110 
111 /**
112  * Gets the "floating" timezone
113  */
floating()114 inline nsCOMPtr<calITimezone> floating() {
115   nsresult rv;
116   nsCOMPtr<calITimezone> tz;
117 
118   rv = getTimezoneService()->GetFloating(getter_AddRefs(tz));
119   if (NS_FAILED(rv)) {
120     MOZ_CRASH(
121         "Could not load floating timezone, brace yourself and prepare for "
122         "crash");
123   }
124 
125   return tz;
126 }
127 
128 /**
129  * Returns the libical VTIMEZONE component, null if floating.
130  *
131  * @attention
132  * Every timezone provider needs to use calICSService for
133  * creating its timezone components since we need to stick to the
134  * same libical.
135  */
136 icaltimezone* getIcalTimezone(calITimezone* tz);
137 
138 /**
139  * Detects the timezone icalt refers to, either using the
140  * passed timezone provider or the global timezone service.
141  *
142  * @param icalt      an icaltime
143  * @param tzProvider timezone provider or null which
144  *                   defaults to the timezone service
145  */
146 nsCOMPtr<calITimezone> detectTimezone(icaltimetype const& icalt,
147                                       calITimezoneProvider* tzProvider);
148 
149 /**
150  * Logs a missing timezone into the js console.
151  */
152 void logMissingTimezone(char const* tzid);
153 
154 /**
155  * Common base class for XPCOM object implementations:
156  * - disallows public deletion (virtual protected dtor)
157  * - disallows copy semantics (no assignment, no copy ctor)
158  */
159 class XpcomBase {
160  protected:
XpcomBase()161   XpcomBase() {}
162   virtual ~XpcomBase();
163 
164  private:
165   XpcomBase(XpcomBase const&);                   // left unimplemented
166   XpcomBase const& operator=(XpcomBase const&);  // left unimplemented
167 };
168 
169 }  // namespace cal
170 
171 #endif  // !defined(INCLUDED_CAL_UTILS_H)
172