1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2012,2013 Intel Corporation
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.
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.
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.
19 * Author: Mathias Hasselmann <mathias@openismus.com>
26 #include "e-phone-number.h"
28 #include <glib/gi18n-lib.h>
30 #include "e-phone-number-private.h"
32 #ifndef ENABLE_PHONENUMBER
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,
41 #endif /* ENABLE_PHONENUMBER */
43 G_DEFINE_QUARK (e-phone-number-error-quark, e_phone_number_error)
46 e_phone_number_error_to_string (EPhoneNumberError 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");
65 return _("Unknown error");
69 _e_phone_number_set_error (GError **error,
70 EPhoneNumberError code)
72 const gchar *message = e_phone_number_error_to_string (code);
73 g_set_error_literal (error, E_PHONE_NUMBER_ERROR, code, message);
77 * e_phone_number_is_supported:
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
84 * Returns: %TRUE if phone number support is available.
87 e_phone_number_is_supported (void)
89 #ifdef ENABLE_PHONENUMBER
93 #else /* ENABLE_PHONENUMBER */
97 #endif /* ENABLE_PHONENUMBER */
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
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
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.
116 * Returns: (transfer full): a new EPhoneNumber instance on success,
117 * or %NULL on error. Call e_phone_number_free() to release this instance.
122 e_phone_number_from_string (const gchar *phone_number,
123 const gchar *country_code,
126 #ifdef ENABLE_PHONENUMBER
128 return _e_phone_number_cxx_from_string (phone_number, country_code, error);
130 #else /* ENABLE_PHONENUMBER */
132 _e_phone_number_set_error (error, E_PHONE_NUMBER_ERROR_NOT_IMPLEMENTED);
135 #endif /* ENABLE_PHONENUMBER */
139 * e_phone_number_to_string:
140 * @phone_number: the phone number to format
141 * @format: the phone number format to apply
143 * Describes the @phone_number according to the rules applying to @format.
145 * Returns: (transfer full): A formatted string for @phone_number.
150 e_phone_number_to_string (const EPhoneNumber *phone_number,
151 EPhoneNumberFormat format)
153 #ifdef ENABLE_PHONENUMBER
155 return _e_phone_number_cxx_to_string (phone_number, format);
157 #else /* ENABLE_PHONENUMBER */
159 g_warning ("%s: The library was built without phone number support.", G_STRFUNC);
162 #endif /* ENABLE_PHONENUMBER */
166 * e_phone_number_compare:
167 * @first_number: the first EPhoneNumber to compare
168 * @second_number: the second EPhoneNumber to compare
170 * Compares two phone numbers.
172 * Returns: The quality of matching for the two phone numbers.
177 e_phone_number_compare (const EPhoneNumber *first_number,
178 const EPhoneNumber *second_number)
180 #ifdef ENABLE_PHONENUMBER
182 return _e_phone_number_cxx_compare (first_number, second_number);
184 #else /* ENABLE_PHONENUMBER */
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;
192 #endif /* ENABLE_PHONENUMBER */
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
201 * Compares two phone numbers.
203 * Returns: The quality of matching for the two phone numbers.
208 e_phone_number_compare_strings (const gchar *first_number,
209 const gchar *second_number,
212 #ifdef ENABLE_PHONENUMBER
214 return _e_phone_number_cxx_compare_strings (first_number, second_number, error);
216 #else /* ENABLE_PHONENUMBER */
218 _e_phone_number_set_error (error, E_PHONE_NUMBER_ERROR_NOT_IMPLEMENTED);
219 return E_PHONE_NUMBER_MATCH_NONE;
221 #endif /* ENABLE_PHONENUMBER */
225 * e_phone_number_copy:
226 * @phone_number: the EPhoneNumber to copy
228 * Makes a copy of @phone_number.
230 * Returns: (transfer full): A newly allocated EPhoneNumber instance.
231 * Call e_phone_number_free() to release this instance.
236 e_phone_number_copy (const EPhoneNumber *phone_number)
238 #ifdef ENABLE_PHONENUMBER
240 return _e_phone_number_cxx_copy (phone_number);
242 #else /* ENABLE_PHONENUMBER */
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);
249 #endif /* ENABLE_PHONENUMBER */
253 * e_phone_number_free:
254 * @phone_number: the EPhoneNumber to free
256 * Released the memory occupied by @phone_number.
261 e_phone_number_free (EPhoneNumber *phone_number)
263 #ifdef ENABLE_PHONENUMBER
265 _e_phone_number_cxx_free (phone_number);
267 #else /* ENABLE_PHONENUMBER */
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);
273 #endif /* ENABLE_PHONENUMBER */