Make use of G_DEFINE_QUARK()
[platform/upstream/evolution-data-server.git] / addressbook / libebook / e-phone-number.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2012,2013 Intel Corporation
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of version 2 of the GNU Lesser General Public
7  * License as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this program; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  *
19  * Author: Mathias Hasselmann <mathias@openismus.com>
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include "e-phone-number.h"
27
28 #include <glib/gi18n-lib.h>
29
30 #include "e-phone-number-private.h"
31
32 #ifndef ENABLE_PHONENUMBER
33
34 /* With phonenumber support enabled the boxed type must be defined in
35  * the C++ code because we cannot compute the size of C++ types here. */
36 G_DEFINE_BOXED_TYPE (EPhoneNumber,
37                      e_phone_number,
38                      e_phone_number_copy,
39                      e_phone_number_free)
40
41 #endif /* ENABLE_PHONENUMBER */
42
43 G_DEFINE_QUARK (e-phone-number-error-quark, e_phone_number_error)
44
45 static const gchar *
46 e_phone_number_error_to_string (EPhoneNumberError code)
47 {
48         switch (code) {
49         case E_PHONE_NUMBER_ERROR_NOT_IMPLEMENTED:
50                 return _("The library was built without phone number support.");
51         case E_PHONE_NUMBER_ERROR_UNKNOWN:
52                 return _("The phone number parser reported an yet unkown error code.");
53         case E_PHONE_NUMBER_ERROR_NOT_A_NUMBER:
54                 return _("Not a phone number");
55         case E_PHONE_NUMBER_ERROR_INVALID_COUNTRY_CODE:
56                 return _("Invalid country code");
57         case E_PHONE_NUMBER_ERROR_TOO_SHORT_AFTER_IDD:
58                 return _("Remaining text after the country code is to short for a phone number");
59         case E_PHONE_NUMBER_ERROR_TOO_SHORT:
60                 return _("Text is too short for a phone number");
61         case E_PHONE_NUMBER_ERROR_TOO_LONG:
62                 return _("Text is too long for a phone number");
63         }
64
65         return _("Unknown error");
66 }
67
68 void
69 _e_phone_number_set_error (GError **error,
70                            EPhoneNumberError code)
71 {
72         const gchar *message = e_phone_number_error_to_string (code);
73         g_set_error_literal (error, E_PHONE_NUMBER_ERROR, code, message);
74 }
75
76 /**
77  * e_phone_number_is_supported:
78  *
79  * Checks if phone number support is available. It is recommended to call this
80  * function before using any of the phone-utils functions to ensure that the
81  * required functionality is available, and to pick alternative mechnisms if
82  * needed.
83  *
84  * Returns: %TRUE if phone number support is available.
85  **/
86 gboolean
87 e_phone_number_is_supported (void)
88 {
89 #ifdef ENABLE_PHONENUMBER
90
91         return TRUE;
92
93 #else /* ENABLE_PHONENUMBER */
94
95         return FALSE;
96
97 #endif /* ENABLE_PHONENUMBER */
98 }
99
100 /**
101  * e_phone_number_from_string:
102  * @phone_number: the phone number to parse
103  * @country_code: (allow-none): a 2-letter country code, or %NULL
104  * @error: (out): a #GError to set an error, if any
105  *
106  * Parses the string passed in @phone_number. Note that no validation is
107  * performed whether the recognized phone number is valid for a particular
108  * region.
109  *
110  * The 2-letter country code passed in @country_code only is used if the
111  * @phone_number is not written in international format. The applications's
112  * currently locale is consulted if %NULL gets passed for @country_code.
113  * If the number is guaranteed to start with a '+' followed by the country
114  * calling code, then "ZZ" can be passed here.
115  *
116  * Returns: (transfer full): a new EPhoneNumber instance on success,
117  * or %NULL on error. Call e_phone_number_free() to release this instance.
118  *
119  * Since: 3.8
120  **/
121 EPhoneNumber *
122 e_phone_number_from_string (const gchar *phone_number,
123                             const gchar *country_code,
124                             GError **error)
125 {
126 #ifdef ENABLE_PHONENUMBER
127
128         return _e_phone_number_cxx_from_string (phone_number, country_code, error);
129
130 #else /* ENABLE_PHONENUMBER */
131
132         _e_phone_number_set_error (error, E_PHONE_NUMBER_ERROR_NOT_IMPLEMENTED);
133         return NULL;
134
135 #endif /* ENABLE_PHONENUMBER */
136 }
137
138 /**
139  * e_phone_number_to_string:
140  * @phone_number: the phone number to format
141  * @format: the phone number format to apply
142  *
143  * Describes the @phone_number according to the rules applying to @format.
144  *
145  * Returns: (transfer full): A formatted string for @phone_number.
146  *
147  * Since: 3.8
148  **/
149 gchar *
150 e_phone_number_to_string (const EPhoneNumber *phone_number,
151                           EPhoneNumberFormat format)
152 {
153 #ifdef ENABLE_PHONENUMBER
154
155         return _e_phone_number_cxx_to_string (phone_number, format);
156
157 #else /* ENABLE_PHONENUMBER */
158
159         g_warning ("%s: The library was built without phone number support.", G_STRFUNC);
160         return NULL;
161
162 #endif /* ENABLE_PHONENUMBER */
163 }
164
165 /**
166  * e_phone_number_compare:
167  * @first_number: the first EPhoneNumber to compare
168  * @second_number: the second EPhoneNumber to compare
169  *
170  * Compares two phone numbers.
171  *
172  * Returns: The quality of matching for the two phone numbers.
173  *
174  * Since: 3.8
175  **/
176 EPhoneNumberMatch
177 e_phone_number_compare (const EPhoneNumber *first_number,
178                         const EPhoneNumber *second_number)
179 {
180 #ifdef ENABLE_PHONENUMBER
181
182         return _e_phone_number_cxx_compare (first_number, second_number);
183
184 #else /* ENABLE_PHONENUMBER */
185
186         /* NOTE: This calls for a dedicated return value, but I sense broken
187          * client code that only checks for E_PHONE_NUMBER_MATCH_NONE and then
188          * treats the "not-implemented" return value as a match */
189         g_warning ("%s: The library was built without phone number support.", G_STRFUNC);
190         return E_PHONE_NUMBER_MATCH_NONE;
191
192 #endif /* ENABLE_PHONENUMBER */
193 }
194
195 /**
196  * e_phone_number_compare_strings:
197  * @first_number: the first EPhoneNumber to compare
198  * @second_number: the second EPhoneNumber to compare
199  * @error: (out): a #GError to set an error, if any
200  *
201  * Compares two phone numbers.
202  *
203  * Returns: The quality of matching for the two phone numbers.
204  *
205  * Since: 3.8
206  **/
207 EPhoneNumberMatch
208 e_phone_number_compare_strings (const gchar *first_number,
209                                 const gchar *second_number,
210                                 GError **error)
211 {
212 #ifdef ENABLE_PHONENUMBER
213
214         return _e_phone_number_cxx_compare_strings (first_number, second_number, error);
215
216 #else /* ENABLE_PHONENUMBER */
217
218         _e_phone_number_set_error (error, E_PHONE_NUMBER_ERROR_NOT_IMPLEMENTED);
219         return E_PHONE_NUMBER_MATCH_NONE;
220
221 #endif /* ENABLE_PHONENUMBER */
222 }
223
224 /**
225  * e_phone_number_copy:
226  * @phone_number: the EPhoneNumber to copy
227  *
228  * Makes a copy of @phone_number.
229  *
230  * Returns: (transfer full): A newly allocated EPhoneNumber instance.
231  * Call e_phone_number_free() to release this instance.
232  *
233  * Since: 3.8
234  **/
235 EPhoneNumber *
236 e_phone_number_copy (const EPhoneNumber *phone_number)
237 {
238 #ifdef ENABLE_PHONENUMBER
239
240         return _e_phone_number_cxx_copy (phone_number);
241
242 #else /* ENABLE_PHONENUMBER */
243
244         /* Without phonenumber support there are no instances.
245          * Any non-NULL value is a programming error in this setup. */
246         g_warn_if_fail (phone_number == NULL);
247         return NULL;
248
249 #endif /* ENABLE_PHONENUMBER */
250 }
251
252 /**
253  * e_phone_number_free:
254  * @phone_number: the EPhoneNumber to free
255  *
256  * Released the memory occupied by @phone_number.
257  *
258  * Since: 3.8
259  **/
260 void
261 e_phone_number_free (EPhoneNumber *phone_number)
262 {
263 #ifdef ENABLE_PHONENUMBER
264
265         _e_phone_number_cxx_free (phone_number);
266
267 #else /* ENABLE_PHONENUMBER */
268
269         /* Without phonenumber support there are no instances.
270          * Any non-NULL value is a programming error in this setup. */
271         g_warn_if_fail (phone_number == NULL);
272
273 #endif /* ENABLE_PHONENUMBER */
274 }