Migrate certificates, icons, logs to XDG dirs
[pidgin-git.git] / libpurple / util.h
blob6371e3dd8a90a9ad1546587d3f7b722541cdef18
1 /* purple
3 * Purple is the legal property of its developers, whose names are too numerous
4 * to list here. Please refer to the COPYRIGHT file distributed with this
5 * source distribution.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
21 * TODO Rename the functions so that they live somewhere in the purple
22 * namespace.
25 #ifndef _PURPLE_UTIL_H_
26 #define _PURPLE_UTIL_H_
27 /**
28 * SECTION:util
29 * @section_id: libpurple-util
30 * @short_description: <filename>util.h</filename>
31 * @title: Utility Functions
34 #include <stdio.h>
36 /**
37 * PurpleMenuAction:
39 * A generic structure that contains information about an "action." One
40 * place this is is used is by protocols to tell the core the list of available
41 * right-click actions for a buddy list row.
43 typedef struct _PurpleMenuAction PurpleMenuAction;
45 /**
46 * PurpleKeyValuePair:
47 * @key: The key
48 * @value: The value
50 * A key-value pair.
52 * This is used by, among other things, purple_gtk_combo* functions to pass in a
53 * list of key-value pairs so it can display a user-friendly value.
55 typedef struct _PurpleKeyValuePair PurpleKeyValuePair;
57 #include "account.h"
58 #include "signals.h"
59 #include "xmlnode.h"
60 #include "notify.h"
61 #include "protocols.h"
64 typedef char *(*PurpleInfoFieldFormatCallback)(const char *field, size_t len);
66 struct _PurpleKeyValuePair
68 gchar *key;
69 void *value;
73 G_BEGIN_DECLS
75 /**
76 * purple_menu_action_new:
77 * @label: The text label to display for this action.
78 * @callback: The function to be called when the action is used on
79 * the selected item.
80 * @data: Additional data to be passed to the callback.
81 * @children: A GList of PurpleMenuActions to be added as a submenu
82 * of the action.
84 * Creates a new PurpleMenuAction.
86 * Returns: The PurpleMenuAction.
88 PurpleMenuAction *purple_menu_action_new(const char *label, PurpleCallback callback,
89 gpointer data, GList *children);
91 /**
92 * purple_menu_action_free:
93 * @act: The PurpleMenuAction to free.
95 * Frees a PurpleMenuAction
97 void purple_menu_action_free(PurpleMenuAction *act);
99 /**
100 * purple_menu_action_get_label:
101 * @act: The PurpleMenuAction.
103 * Returns the label of the PurpleMenuAction.
105 * Returns: The label string.
107 char * purple_menu_action_get_label(const PurpleMenuAction *act);
110 * purple_menu_action_get_callback:
111 * @act: The PurpleMenuAction.
113 * Returns the callback of the PurpleMenuAction.
115 * Returns: The callback function.
117 PurpleCallback purple_menu_action_get_callback(const PurpleMenuAction *act);
120 * purple_menu_action_get_data:
121 * @act: The PurpleMenuAction.
123 * Returns the data stored in the PurpleMenuAction.
125 * Returns: The data.
127 gpointer purple_menu_action_get_data(const PurpleMenuAction *act);
130 * purple_menu_action_get_children:
131 * @act: The PurpleMenuAction.
133 * Returns the children of the PurpleMenuAction.
135 * Returns: The GList of children.
137 GList* purple_menu_action_get_children(const PurpleMenuAction *act);
140 * purple_menu_action_set_label:
141 * @act: The menu action.
142 * @label: The label for the menu action.
144 * Set the label to the PurpleMenuAction.
146 void purple_menu_action_set_label(PurpleMenuAction *act, char *label);
149 * purple_menu_action_set_callback:
150 * @act: The menu action.
151 * @callback: The callback.
153 * Set the callback that will be used by the PurpleMenuAction.
155 void purple_menu_action_set_callback(PurpleMenuAction *act, PurpleCallback callback);
158 * purple_menu_action_set_data:
159 * @act: The menu action.
160 * @data: The data used by this PurpleMenuAction
162 * Set the label to the PurpleMenuAction.
164 void purple_menu_action_set_data(PurpleMenuAction *act, gpointer data);
167 * purple_menu_action_set_children:
168 * @act: The menu action.
169 * @children: The PurpleMenuAtion children
171 * Set the children of the PurpleMenuAction.
173 void purple_menu_action_set_children(PurpleMenuAction *act, GList *children);
176 * purple_menu_action_set_stock_icon:
177 * @act: The menu action.
178 * @stock: The stock icon identifier.
180 * Sets the icon for the PurpleMenuAction.
182 void purple_menu_action_set_stock_icon(PurpleMenuAction *act,
183 const gchar *stock);
186 * purple_menu_action_get_stock_icon:
187 * @act: The menu action.
189 * Gets the stock icon of the PurpleMenuAction.
191 * Returns: The stock icon identifier.
193 const gchar *
194 purple_menu_action_get_stock_icon(PurpleMenuAction *act);
197 * purple_util_set_current_song:
198 * @title: The title of the song, %NULL to unset the value.
199 * @artist: The artist of the song, can be %NULL.
200 * @album: The album of the song, can be %NULL.
202 * Set the appropriate presence values for the currently playing song.
204 void purple_util_set_current_song(const char *title, const char *artist,
205 const char *album);
208 * purple_util_format_song_info:
209 * @title: The title of the song, %NULL to unset the value.
210 * @artist: The artist of the song, can be %NULL.
211 * @album: The album of the song, can be %NULL.
212 * @unused: Currently unused, must be %NULL.
214 * Format song information.
216 * Returns: The formatted string. The caller must g_free the returned string.
218 char * purple_util_format_song_info(const char *title, const char *artist,
219 const char *album, gpointer unused);
221 /**************************************************************************/
222 /* Utility Subsystem */
223 /**************************************************************************/
226 * purple_util_init:
228 * Initializes the utility subsystem.
230 void purple_util_init(void);
233 * purple_util_uninit:
235 * Uninitializes the util subsystem.
237 void purple_util_uninit(void);
239 /**************************************************************************/
240 /* Base16 Functions */
241 /**************************************************************************/
244 * purple_base16_encode:
245 * @data: The data to convert.
246 * @len: The length of the data.
248 * Converts a chunk of binary data to its base-16 equivalent.
250 * See purple_base16_decode()
252 * Returns: The base-16 string in the ASCII encoding. Must be
253 * g_free'd when no longer needed.
255 gchar *purple_base16_encode(const guchar *data, gsize len);
258 * purple_base16_decode:
259 * @str: The base-16 string to convert to raw data.
260 * @ret_len: The length of the returned data. You can
261 * pass in NULL if you're sure that you know
262 * the length of the decoded data, or if you
263 * know you'll be able to use strlen to
264 * determine the length, etc.
266 * Converts an ASCII string of base-16 encoded data to
267 * the binary equivalent.
269 * See purple_base16_encode()
271 * Returns: The raw data. Must be g_free'd when no longer needed.
273 guchar *purple_base16_decode(const char *str, gsize *ret_len);
276 * purple_base16_encode_chunked:
277 * @data: The data to convert.
278 * @len: The length of the data.
280 * Converts a chunk of binary data to a chunked base-16 representation
281 * (handy for key fingerprints)
283 * Example output: 01:23:45:67:89:AB:CD:EF
285 * Returns: The base-16 string in the ASCII chunked encoding. Must be
286 * g_free'd when no longer needed.
288 gchar *purple_base16_encode_chunked(const guchar *data, gsize len);
291 /**************************************************************************/
292 /* Base64 Functions */
293 /**************************************************************************/
296 * purple_base64_encode:
297 * @data: The data to convert.
298 * @len: The length of the data.
300 * Converts a chunk of binary data to its base-64 equivalent.
302 * See purple_base64_decode()
304 * Returns: The base-64 string in the ASCII encoding. Must be
305 * g_free'd when no longer needed.
307 gchar *purple_base64_encode(const guchar *data, gsize len);
310 * purple_base64_decode:
311 * @str: The base-64 string to convert to raw data.
312 * @ret_len: The length of the returned data. You can
313 * pass in NULL if you're sure that you know
314 * the length of the decoded data, or if you
315 * know you'll be able to use strlen to
316 * determine the length, etc.
318 * Converts an ASCII string of base-64 encoded data to
319 * the binary equivalent.
321 * See purple_base64_encode()
323 * Returns: The raw data. Must be g_free'd when no longer needed.
325 guchar *purple_base64_decode(const char *str, gsize *ret_len);
327 /**************************************************************************/
328 /* Quoted Printable Functions */
329 /**************************************************************************/
332 * purple_quotedp_decode:
333 * @str: The quoted printable ASCII string to convert to raw data.
334 * @ret_len: The length of the returned data.
336 * Converts a quoted printable string back to its readable equivalent.
337 * What is a quoted printable string, you ask? It's an encoding used
338 * to transmit binary data as ASCII. It's intended purpose is to send
339 * emails containing non-ASCII characters. Wikipedia has a pretty good
340 * explanation. Also see RFC 2045.
342 * Returns: The readable string. Must be g_free'd when no longer needed.
344 guchar *purple_quotedp_decode(const char *str, gsize *ret_len);
346 /**************************************************************************/
347 /* MIME Functions */
348 /**************************************************************************/
351 * purple_mime_decode_field:
352 * @str: The ASCII string, possibly containing any number of
353 * encoded-word sections.
355 * Converts a MIME header field string back to its readable equivalent
356 * according to RFC 2047. Basically, a header is plain ASCII and can
357 * contain any number of sections called "encoded-words." The format
358 * of an encoded word is =?ISO-8859-1?Q?Keld_J=F8rn_Simonsen?=
359 * =? designates the beginning of the encoded-word
360 * ?= designates the end of the encoded-word
362 * An encoded word is segmented into three pieces by the use of a
363 * question mark. The first piece is the character set, the second
364 * piece is the encoding, and the third piece is the encoded text.
366 * Returns: The string, with any encoded-word sections decoded and
367 * converted to UTF-8. Must be g_free'd when no longer
368 * needed.
370 char *purple_mime_decode_field(const char *str);
373 /**************************************************************************/
374 /* Date/Time Functions */
375 /**************************************************************************/
378 * purple_utf8_strftime:
379 * @format: The format string, in UTF-8
380 * @tm: The time to format, or %NULL to use the current local time
382 * Formats a time into the specified format.
384 * This is essentially strftime(), but it has a static buffer
385 * and handles the UTF-8 conversion for the caller.
387 * This function also provides the GNU \%z formatter if the underlying C
388 * library doesn't. However, the format string parser is very naive, which
389 * means that conversions specifiers to \%z cannot be guaranteed. The GNU
390 * strftime(3) man page describes \%z as: 'The time-zone as hour offset from
391 * GMT. Required to emit RFC822-conformant dates
392 * (using "\%a, \%d \%b \%Y \%H:\%M:\%S \%z"). (GNU)'
394 * On Windows, this function also converts the results for \%Z from a timezone
395 * name (as returned by the system strftime() \%Z format string) to a timezone
396 * abbreviation (as is the case on Unix). As with \%z, conversion specifiers
397 * should not be used.
399 * Note: @format is required to be in UTF-8. This differs from strftime(),
400 * where the format is provided in the locale charset.
402 * Returns: The formatted time, in UTF-8.
404 const char *purple_utf8_strftime(const char *format, const struct tm *tm);
407 * purple_get_tzoff_str:
408 * @tm: The time to get the timezone for
409 * @iso: TRUE to format the offset according to ISO-8601, FALSE to
410 * not substitute 'Z' for 0 offset, and to not separate
411 * hours and minutes with a colon.
413 * Gets a string representation of the local timezone offset
415 const char *purple_get_tzoff_str(const struct tm *tm, gboolean iso);
418 * purple_date_format_short:
419 * @tm: The time to format, or %NULL to use the current local time
421 * Formats a time into the user's preferred short date format.
423 * The returned string is stored in a static buffer, so the result
424 * should be g_strdup()'d if it's going to be kept.
426 * Returns: The date, formatted as per the user's settings. In the USA this
427 * is something like "02/18/13"
429 const char *purple_date_format_short(const struct tm *tm);
432 * purple_date_format_long:
433 * @tm: The time to format, or %NULL to use the current local time
435 * Formats a time into the user's preferred short date plus time format.
437 * The returned string is stored in a static buffer, so the result
438 * should be g_strdup()'d if it's going to be kept.
440 * Returns: The timestamp, formatted as per the user's settings. In the USA
441 * this is something like "02/18/13 15:26:44"
443 const char *purple_date_format_long(const struct tm *tm);
446 * purple_date_format_full:
447 * @tm: The time to format, or %NULL to use the current local time
449 * Formats a time into the user's preferred full date and time format.
451 * The returned string is stored in a static buffer, so the result
452 * should be g_strdup()'d if it's going to be kept.
454 * Returns: The date and time, formatted as per the user's settings. In the
455 * USA this is something like "Mon Feb 18 15:26:44 2013"
457 const char *purple_date_format_full(const struct tm *tm);
460 * purple_time_format:
461 * @tm: The time to format, or %NULL to use the current local time
463 * Formats a time into the user's preferred time format.
465 * The returned string is stored in a static buffer, so the result
466 * should be g_strdup()'d if it's going to be kept.
468 * Returns: The time, formatted as per the user's settings. In the USA this
469 * is something like "15:26:44"
471 const char *purple_time_format(const struct tm *tm);
474 * purple_time_build:
475 * @year: The year.
476 * @month: The month.
477 * @day: The day.
478 * @hour: The hour.
479 * @min: The minute.
480 * @sec: The second.
482 * Builds a time_t from the supplied information.
484 * Returns: A time_t.
486 time_t purple_time_build(int year, int month, int day, int hour,
487 int min, int sec);
490 * PURPLE_NO_TZ_OFF:
492 * Used by purple_str_to_time to indicate no timezone offset was
493 * specified in the timestamp string.
495 #define PURPLE_NO_TZ_OFF -500000
498 * purple_str_to_time:
499 * @timestamp: The timestamp
500 * @utc: Assume UTC if no timezone specified
501 * @tm: If not %NULL, the caller can get a copy of the
502 * struct tm used to calculate the time_t return value.
503 * @tz_off: If not %NULL, the caller can get a copy of the
504 * timezone offset (from UTC) used to calculate the time_t
505 * return value. Note: Zero is a valid offset. As such,
506 * the value of the macro PURPLE_NO_TZ_OFF indicates no
507 * offset was specified (which means that the local
508 * timezone was used in the calculation).
509 * @rest: If not %NULL, the caller can get a pointer to the
510 * part of @timestamp left over after parsing is
511 * completed, if it's not the end of @timestamp.
513 * Parses a timestamp in jabber, ISO8601, or MM/DD/YYYY format and returns
514 * a time_t.
516 * Returns: A time_t.
518 time_t purple_str_to_time(const char *timestamp, gboolean utc,
519 struct tm *tm, long *tz_off, const char **rest);
522 * purple_uts35_to_str:
523 * @format: The formatting string, according to UTS \#35
524 * See http://unicode.org/reports/tr35/
525 * (NOTE: not all formats are supported)
526 * @len: The length of the formatting string
527 * @tm: The time to format, or %NULL to use the current local time
529 * Formats a datetime according to a UTS-35 Date Format Pattern.
531 * Returns: The time, formatted as per the user's settings.
533 char *purple_uts35_to_str(const char *format, size_t len, struct tm *tm);
536 /**************************************************************************/
537 /* Markup Functions */
538 /**************************************************************************/
541 * purple_markup_escape_text:
542 * @text: The text to escape
543 * @length: The length of the text, or -1 if #NULL terminated
545 * Escapes special characters in a plain-text string so they display
546 * correctly as HTML. For example, &amp; is replaced with &amp;amp; and &lt; is
547 * replaced with &amp;lt;
549 * This is exactly the same as g_markup_escape_text(), except that it
550 * does not change ' to &amp;apos; because &amp;apos; is not a valid HTML 4 entity,
551 * and is displayed literally in IE7.
553 gchar *purple_markup_escape_text(const gchar *text, gssize length);
556 * purple_markup_find_tag:
557 * @needle: The name of the tag
558 * @haystack: The null-delimited string to search in
559 * @start: A pointer to the start of the tag if found
560 * @end: A pointer to the end of the tag if found
561 * @attributes: The attributes, if the tag was found. This should
562 * be freed with g_datalist_clear().
564 * Finds an HTML tag matching the given name.
566 * This locates an HTML tag's start and end, and stores its attributes
567 * in a GData hash table. The names of the attributes are lower-cased
568 * in the hash table, and the name of the tag is case insensitive.
570 * Returns: TRUE if the tag was found
572 gboolean purple_markup_find_tag(const char *needle, const char *haystack,
573 const char **start, const char **end,
574 GData **attributes);
577 * purple_markup_extract_info_field:
578 * @str: The string to parse.
579 * @len: The size of str.
580 * @user_info: The destination PurpleNotifyUserInfo to which the new
581 * field info should be added.
582 * @start_token: The beginning token.
583 * @skip: The number of characters to skip after the
584 * start token.
585 * @end_token: The ending token.
586 * @check_value: The value that the last character must meet.
587 * @no_value_token: The token indicating no value is given.
588 * @display_name: The short descriptive name to display for this token.
589 * @is_link: TRUE if this should be a link, or FALSE otherwise.
590 * @link_prefix: The prefix for the link.
591 * @format_cb: A callback to format the value before adding it.
593 * Extracts a field of data from HTML.
595 * This is a scary function. See protocols/msn/msn.c and
596 * protocols/yahoo/yahoo_profile.c for example usage.
598 * Returns: TRUE if successful, or FALSE otherwise.
600 gboolean purple_markup_extract_info_field(const char *str, int len, PurpleNotifyUserInfo *user_info,
601 const char *start_token, int skip,
602 const char *end_token, char check_value,
603 const char *no_value_token,
604 const char *display_name, gboolean is_link,
605 const char *link_prefix,
606 PurpleInfoFieldFormatCallback format_cb);
609 * purple_markup_html_to_xhtml:
610 * @html: The HTML markup.
611 * @dest_xhtml: The destination XHTML output.
612 * @dest_plain: The destination plain-text output.
614 * Converts HTML markup to XHTML.
616 void purple_markup_html_to_xhtml(const char *html, char **dest_xhtml,
617 char **dest_plain);
620 * purple_markup_strip_html:
621 * @str: The string to strip HTML from.
623 * Strips HTML tags from a string.
625 * Returns: The new string without HTML. You must g_free this string
626 * when finished with it.
628 char *purple_markup_strip_html(const char *str);
631 * purple_markup_linkify:
632 * @str: The string to linkify.
634 * Adds the necessary HTML code to turn URIs into HTML links in a string.
636 * Returns: The new string with all URIs surrounded in standard
637 * HTML &lt;a href="whatever"&gt;&lt;/a&gt; tags. You must g_free()
638 * this string when finished with it.
640 char *purple_markup_linkify(const char *str);
643 * purple_unescape_text:
644 * @text: The string in which to unescape any HTML entities
646 * Unescapes HTML entities to their literal characters in the text.
647 * For example "&amp;amp;" is replaced by '&amp;' and so on. Also converts
648 * numerical entities (e.g. "&amp;\#38;" is also '&amp;').
650 * This function currently supports the following named entities:
651 * "&amp;amp;", "&amp;lt;", "&amp;gt;", "&amp;copy;", "&amp;quot;",
652 * "&amp;reg;", "&amp;apos;"
654 * purple_unescape_html() is similar, but also converts "&lt;br&gt;" into "\n".
656 * See purple_unescape_html()
658 * Returns: The text with HTML entities literalized. You must g_free
659 * this string when finished with it.
661 char *purple_unescape_text(const char *text);
664 * purple_unescape_html:
665 * @html: The string in which to unescape any HTML entities
667 * Unescapes HTML entities to their literal characters and converts
668 * "&lt;br&gt;" to "\n". See purple_unescape_text() for more details.
670 * See purple_unescape_text()
672 * Returns: The text with HTML entities literalized. You must g_free
673 * this string when finished with it.
675 char *purple_unescape_html(const char *html);
678 * purple_markup_slice:
679 * @str: The input NUL terminated, HTML, UTF-8 (or ASCII) string.
680 * @x: The character offset into an unformatted version of str to
681 * begin at.
682 * @y: The character offset (into an unformatted vesion of str) of
683 * one past the last character to include in the slice.
685 * Returns a newly allocated substring of the HTML UTF-8 string "str".
686 * The markup is preserved such that the substring will have the same
687 * formatting as original string, even though some tags may have been
688 * opened before "x", or may close after "y". All open tags are closed
689 * at the end of the returned string, in the proper order.
691 * Note that x and y are in character offsets, not byte offsets, and
692 * are offsets into an unformatted version of str. Because of this,
693 * this function may be sensitive to changes in GtkIMHtml and may break
694 * when used with other UI's. libpurple users are encouraged to report and
695 * work out any problems encountered.
697 * Returns: The HTML slice of string, with all formatting retained.
699 char *purple_markup_slice(const char *str, guint x, guint y);
702 * purple_markup_get_tag_name:
703 * @tag: The string starting a HTML tag.
705 * Returns a newly allocated string containing the name of the tag
706 * located at "tag". Tag is expected to point to a '<', and contain
707 * a '>' sometime after that. If there is no '>' and the string is
708 * not NUL terminated, this function can be expected to segfault.
710 * Returns: A string containing the name of the tag.
712 char *purple_markup_get_tag_name(const char *tag);
715 * purple_markup_unescape_entity:
716 * @text: A string containing an HTML entity.
717 * @length: If not %NULL, the string length of the entity is stored in this location.
719 * Returns a constant string of the character representation of the HTML
720 * entity pointed to by @text. For example, purple_markup_unescape_entity("&amp;amp;")
721 * will return "&amp;". The @text variable is expected to point to an '&amp;',
722 * the first character of the entity. If given an unrecognized entity, the function
723 * returns %NULL.
725 * Note that this function, unlike purple_unescape_html(), does not search
726 * the string for the entity, does not replace the entity, and does not
727 * return a newly allocated string.
729 * Returns: A constant string containing the character representation of the given entity.
731 const char * purple_markup_unescape_entity(const char *text, int *length);
734 * purple_markup_get_css_property:
735 * @style: A string containing the inline CSS text.
736 * @opt: The requested CSS property.
738 * Returns a newly allocated string containing the value of the CSS property specified
739 * in opt. The @style argument is expected to point to a HTML inline CSS.
740 * The function will seek for the CSS property and return its value.
742 * For example, purple_markup_get_css_property("direction:rtl;color:#dc4d1b;",
743 * "color") would return "#dc4d1b".
745 * On error or if the requested property was not found, the function returns
746 * %NULL.
748 * Returns: The value of the requested CSS property.
750 char * purple_markup_get_css_property(const gchar *style, const gchar *opt);
753 * purple_markup_is_rtl:
754 * @html: The HTML text.
756 * Check if the given HTML contains RTL text.
758 * Returns: TRUE if the text contains RTL text, FALSE otherwise.
760 gboolean purple_markup_is_rtl(const char *html);
763 /**************************************************************************/
764 /* Path/Filename Functions */
765 /**************************************************************************/
768 * purple_home_dir:
770 * Returns the user's home directory.
772 * See purple_user_dir()
774 * Returns: The user's home directory.
776 const gchar *purple_home_dir(void);
779 * purple_user_dir:
781 * Returns the purple settings directory in the user's home directory.
782 * This is usually ~/.purple
784 * See purple_home_dir()
786 * Returns: The purple settings directory.
788 const char *purple_user_dir(void);
791 * purple_cache_dir:
793 * Returns the purple cache directory according to XDG Base Directory Specification.
794 * This is usually ~/.cache/purple
796 * See purple_home_dir()
798 * Returns: The purple cache directory.
800 const char *purple_cache_dir(void);
803 * purple_config_dir:
805 * Returns the purple configuration directory according to XDG Base Directory Specification.
806 * This is usually ~/.config/purple
808 * See purple_home_dir()
810 * Returns: The purple configuration directory.
812 const char *purple_config_dir(void);
815 * purple_data_dir:
817 * Returns the purple data directory according to XDG Base Directory Specification.
818 * This is usually ~/.local/share/purple
820 * See purple_home_dir()
822 * Returns: The purple data directory.
824 const char *purple_data_dir(void);
827 * purple_util_set_user_dir:
828 * @dir: The custom settings directory
830 * Define a custom purple settings directory, overriding the default (user's home directory/.purple)
832 void purple_util_set_user_dir(const char *dir);
835 * purple_build_dir:
836 * @path: The path you wish to create. Note that it must start
837 * from the root or this function will fail.
838 * @mode: Unix-style permissions for this directory.
840 * Builds a complete path from the root, making any directories along
841 * the path which do not already exist.
843 * Returns: 0 for success, nonzero on any error.
845 int purple_build_dir(const char *path, int mode);
848 * purple_util_write_data_to_file:
849 * @filename: The basename of the file to write in the purple_user_dir.
850 * @data: A null-terminated string of data to write.
851 * @size: The size of the data to save. If data is
852 * null-terminated you can pass in -1.
854 * Write a string of data to a file of the given name in the Purple
855 * user directory ($HOME/.purple by default). The data is typically
856 * a serialized version of one of Purple's config files, such as
857 * prefs.xml, accounts.xml, etc. And the string is typically
858 * obtained using purple_xmlnode_to_formatted_str. However, this function
859 * should work fine for saving binary files as well.
861 * Returns: TRUE if the file was written successfully. FALSE otherwise.
863 gboolean purple_util_write_data_to_file(const char *filename, const char *data,
864 gssize size);
867 * purple_util_write_data_to_file_absolute:
868 * @filename_full: Filename to write to
869 * @data: A null-terminated string of data to write.
870 * @size: The size of the data to save. If data is
871 * null-terminated you can pass in -1.
873 * Write data to a file using the absolute path.
875 * This exists for Glib backwards compatibility reasons.
877 * See purple_util_write_data_to_file()
879 * Returns: TRUE if the file was written successfully. FALSE otherwise.
881 /* TODO: Remove this function (use g_file_set_contents instead) when 3.0.0
882 * rolls around. */
883 gboolean
884 purple_util_write_data_to_file_absolute(const char *filename_full, const char *data, gssize size);
887 * purple_util_read_xml_from_file:
888 * @filename: The basename of the file to open in the purple_user_dir.
889 * @description: A very short description of the contents of this
890 * file. This is used in error messages shown to the
891 * user when the file can not be opened. For example,
892 * "preferences," or "buddy pounces."
894 * Read the contents of a given file and parse the results into an
895 * PurpleXmlNode tree structure. This is intended to be used to read
896 * Purple's configuration xml files (prefs.xml, pounces.xml, etc.)
898 * Returns: An PurpleXmlNode tree of the contents of the given file. Or NULL, if
899 * the file does not exist or there was an error reading the file.
901 PurpleXmlNode *purple_util_read_xml_from_file(const char *filename,
902 const char *description);
905 * purple_mkstemp:
906 * @path: The returned path to the temp file.
907 * @binary: Text or binary, for platforms where it matters.
909 * Creates a temporary file and returns a file pointer to it.
911 * This is like mkstemp(), but returns a file pointer and uses a
912 * pre-set template. It uses the semantics of tempnam() for the
913 * directory to use and allocates the space for the file path.
915 * The caller is responsible for closing the file and removing it when
916 * done, as well as freeing the space pointed to by @path with
917 * g_free().
919 * Returns: A file pointer to the temporary file, or %NULL on failure.
921 FILE *purple_mkstemp(char **path, gboolean binary);
924 /**************************************************************************/
925 /* Environment Detection Functions */
926 /**************************************************************************/
929 * purple_program_is_valid:
930 * @program: The file name of the application.
932 * Checks if the given program name is valid and executable.
934 * Returns: TRUE if the program is runable.
936 gboolean purple_program_is_valid(const char *program);
939 * purple_running_gnome:
941 * Check if running GNOME.
943 * Returns: TRUE if running GNOME, FALSE otherwise.
945 gboolean purple_running_gnome(void);
948 * purple_running_kde:
950 * Check if running KDE.
952 * Returns: TRUE if running KDE, FALSE otherwise.
954 gboolean purple_running_kde(void);
957 * purple_running_osx:
959 * Check if running OS X.
961 * Returns: TRUE if running OS X, FALSE otherwise.
963 gboolean purple_running_osx(void);
966 * purple_fd_get_ip:
967 * @fd: The socket file descriptor.
969 * Returns the IP address from a socket file descriptor.
971 * Returns: The IP address, or %NULL on error.
973 char *purple_fd_get_ip(int fd);
976 * purple_socket_get_family:
977 * @fd: The socket file descriptor.
979 * Returns the address family of a socket.
981 * Returns: The address family of the socket (AF_INET, AF_INET6, etc) or -1
982 * on error.
984 int purple_socket_get_family(int fd);
987 * purple_socket_speaks_ipv4:
988 * @fd: The socket file descriptor
990 * Returns TRUE if a socket is capable of speaking IPv4.
992 * This is the case for IPv4 sockets and, on some systems, IPv6 sockets
993 * (due to the IPv4-mapped address functionality).
995 * Returns: TRUE if a socket can speak IPv4.
997 gboolean purple_socket_speaks_ipv4(int fd);
1000 /**************************************************************************/
1001 /* String Functions */
1002 /**************************************************************************/
1005 * purple_strequal:
1006 * @left: A string
1007 * @right: A string to compare with left
1009 * Tests two strings for equality.
1011 * Unlike strcmp(), this function will not crash if one or both of the
1012 * strings are %NULL.
1014 * Returns: %TRUE if the strings are the same, else %FALSE.
1016 gboolean purple_strequal(const gchar *left, const gchar *right);
1019 * purple_normalize:
1020 * @account: The account the string belongs to, or NULL if you do
1021 * not know the account. If you use NULL, the string
1022 * will still be normalized, but if the protocol uses a
1023 * custom normalization function then the string may
1024 * not be normalized correctly.
1025 * @str: The string to normalize.
1027 * Normalizes a string, so that it is suitable for comparison.
1029 * The returned string will point to a static buffer, so if the
1030 * string is intended to be kept long-term, you <emphasis>must</emphasis>
1031 * g_strdup() it. Also, calling normalize() twice in the same line
1032 * will lead to problems.
1034 * Returns: A pointer to the normalized version stored in a static buffer.
1036 const char *purple_normalize(const PurpleAccount *account, const char *str);
1039 * purple_normalize_nocase:
1040 * @account: The account the string belongs to.
1041 * @str: The string to normalize.
1043 * Normalizes a string, so that it is suitable for comparison.
1045 * This is one possible implementation for the protocol callback
1046 * function "normalize." It returns a lowercase and UTF-8
1047 * normalized version of the string.
1049 * Returns: A pointer to the normalized version stored in a static buffer.
1051 const char *purple_normalize_nocase(const PurpleAccount *account, const char *str);
1054 * purple_validate:
1055 * @protocol: The protocol the string belongs to.
1056 * @str: The string to validate.
1058 * Checks, if a string is valid.
1060 * Returns: TRUE, if string is valid, otherwise FALSE.
1062 gboolean purple_validate(const PurpleProtocol *protocol, const char *str);
1065 * purple_str_has_prefix:
1066 * @s: The string to check.
1067 * @p: The prefix in question.
1069 * Compares two strings to see if the first contains the second as
1070 * a proper prefix.
1072 * Returns: TRUE if p is a prefix of s, otherwise FALSE.
1074 gboolean purple_str_has_prefix(const char *s, const char *p);
1077 * purple_str_has_caseprefix:
1078 * @s: The string to check.
1079 * @p: The prefix in question.
1081 * Compares two strings to see if the first contains the second as
1082 * a proper case-insensitive prefix.
1084 * Returns: %TRUE if @p is a prefix of @s, otherwise %FALSE.
1086 gboolean
1087 purple_str_has_caseprefix(const gchar *s, const gchar *p);
1090 * purple_str_has_suffix:
1091 * @s: The string to check.
1092 * @x: The suffix in question.
1094 * Compares two strings to see if the second is a proper suffix
1095 * of the first.
1097 * Returns: TRUE if x is a a suffix of s, otherwise FALSE.
1099 gboolean purple_str_has_suffix(const char *s, const char *x);
1102 * purple_strdup_withhtml:
1103 * @src: The source string.
1105 * Duplicates a string and replaces all newline characters from the
1106 * source string with HTML linebreaks.
1108 * Returns: The new string. Must be g_free'd by the caller.
1110 gchar *purple_strdup_withhtml(const gchar *src);
1113 * purple_str_add_cr:
1114 * @str: The source string.
1116 * Ensures that all linefeeds have a matching carriage return.
1118 * Returns: The string with carriage returns.
1120 char *purple_str_add_cr(const char *str);
1123 * purple_str_strip_char:
1124 * @str: The string to strip characters from.
1125 * @thechar: The character to strip from the given string.
1127 * Strips all instances of the given character from the
1128 * given string. The string is modified in place. This
1129 * is useful for stripping new line characters, for example.
1131 * Example usage:
1132 * purple_str_strip_char(my_dumb_string, '\n');
1134 void purple_str_strip_char(char *str, char thechar);
1137 * purple_util_chrreplace:
1138 * @string: The string from which to replace stuff.
1139 * @delimiter: The character you want replaced.
1140 * @replacement: The character you want inserted in place
1141 * of the delimiting character.
1143 * Given a string, this replaces all instances of one character
1144 * with another. This happens inline (the original string IS
1145 * modified).
1147 void purple_util_chrreplace(char *string, char delimiter,
1148 char replacement);
1151 * purple_strreplace:
1152 * @string: The string from which to replace stuff.
1153 * @delimiter: The substring you want replaced.
1154 * @replacement: The substring you want inserted in place
1155 * of the delimiting substring.
1157 * Given a string, this replaces one substring with another
1158 * and returns a newly allocated string.
1160 * Returns: A new string, after performing the substitution.
1161 * free this with g_free().
1163 gchar *purple_strreplace(const char *string, const char *delimiter,
1164 const char *replacement);
1168 * purple_utf8_ncr_encode:
1169 * @in: The string which might contain utf-8 substrings
1171 * Given a string, this replaces any utf-8 substrings in that string with
1172 * the corresponding numerical character reference, and returns a newly
1173 * allocated string.
1175 * Returns: A new string, with utf-8 replaced with numerical character
1176 * references, free this with g_free()
1178 char *purple_utf8_ncr_encode(const char *in);
1182 * purple_utf8_ncr_decode:
1183 * @in: The string which might contain numerical character references.
1185 * Given a string, this replaces any numerical character references
1186 * in that string with the corresponding actual utf-8 substrings,
1187 * and returns a newly allocated string.
1189 * Returns: A new string, with numerical character references
1190 * replaced with actual utf-8, free this with g_free().
1192 char *purple_utf8_ncr_decode(const char *in);
1196 * purple_strcasereplace:
1197 * @string: The string from which to replace stuff.
1198 * @delimiter: The substring you want replaced.
1199 * @replacement: The substring you want inserted in place
1200 * of the delimiting substring.
1202 * Given a string, this replaces one substring with another
1203 * ignoring case and returns a newly allocated string.
1205 * Returns: A new string, after performing the substitution.
1206 * free this with g_free().
1208 gchar *purple_strcasereplace(const char *string, const char *delimiter,
1209 const char *replacement);
1212 * purple_strcasestr:
1213 * @haystack: The string to search in.
1214 * @needle: The substring to find.
1216 * This is like strstr, except that it ignores ASCII case in
1217 * searching for the substring.
1219 * Returns: the location of the substring if found, or NULL if not
1221 const char *purple_strcasestr(const char *haystack, const char *needle);
1224 * purple_str_size_to_units:
1225 * @size: The size
1227 * Returns a string representing a filesize in the appropriate
1228 * units (MB, KB, GB, etc.)
1230 * Returns: The string in units form. This must be freed.
1232 char *purple_str_size_to_units(goffset size);
1235 * purple_str_seconds_to_string:
1236 * @sec: The seconds.
1238 * Converts seconds into a human-readable form.
1240 * Returns: A human-readable form, containing days, hours, minutes, and
1241 * seconds.
1243 char *purple_str_seconds_to_string(guint sec);
1246 * purple_str_binary_to_ascii:
1247 * @binary: A string of random data, possibly with embedded NULs
1248 * and such.
1249 * @len: The length in bytes of the input string. Must not be 0.
1251 * Converts a binary string into a NUL terminated ascii string,
1252 * replacing nonascii characters and characters below SPACE (including
1253 * NUL) into \\xyy, where yy are two hex digits. Also backslashes are
1254 * changed into two backslashes (\\\\). The returned, newly allocated
1255 * string can be outputted to the console, and must be g_free()d.
1257 * Returns: A newly allocated ASCIIZ string.
1259 char *purple_str_binary_to_ascii(const unsigned char *binary, guint len);
1262 * purple_utf16_size:
1263 * @str: String to check.
1265 * Calculates UTF-16 string size (in bytes).
1267 * Returns: Number of bytes (including NUL character) that string occupies.
1269 size_t purple_utf16_size(const gunichar2 *str);
1272 * purple_str_wipe:
1273 * @str: A NUL-terminated string to free, or a NULL-pointer.
1275 * Fills a NUL-terminated string with zeros and frees it.
1277 * It should be used to free sensitive data, like passwords.
1279 void purple_str_wipe(gchar *str);
1282 * purple_utf16_wipe:
1283 * @str: A NUL-terminated string to free, or a NULL-pointer.
1285 * Fills a NUL-terminated UTF-16 string with zeros and frees it.
1287 * It should be used to free sensitive data, like passwords.
1289 void purple_utf16_wipe(gunichar2 *str);
1292 /**************************************************************************/
1293 /* URI/URL Functions */
1294 /**************************************************************************/
1296 void purple_got_protocol_handler_uri(const char *uri);
1299 * purple_url_decode:
1300 * @str: The string to translate.
1302 * Decodes a URL into a plain string.
1304 * This will change hex codes and such to their ascii equivalents.
1306 * Returns: The resulting string.
1308 const char *purple_url_decode(const char *str);
1311 * purple_url_encode:
1312 * @str: The string to translate.
1314 * Encodes a URL into an escaped string.
1316 * This will change non-alphanumeric characters to hex codes.
1318 * Returns: The resulting string.
1320 const char *purple_url_encode(const char *str);
1323 * purple_email_is_valid:
1324 * @address: The email address to validate.
1326 * Checks if the given email address is syntactically valid.
1328 * Returns: True if the email address is syntactically correct.
1330 gboolean purple_email_is_valid(const char *address);
1333 * purple_ip_address_is_valid:
1334 * @ip: The IP address to validate.
1336 * Checks if the given IP address is a syntactically valid IPv4 or
1337 * IPv6 address.
1338 * If you specifically want to check for an IPv4 address use
1339 * purple_ipv4_address_is_valid(), or for an IPv6 address use
1340 * purple_ipv6_address_is_valid().
1342 * Returns: True if the IP address is syntactically correct.
1344 gboolean purple_ip_address_is_valid(const char *ip);
1347 * purple_ipv4_address_is_valid:
1348 * @ip: The IP address to validate.
1350 * Checks if the given IP address is a syntactically valid IPv4 address.
1352 * Returns: True if the IP address is syntactically correct.
1354 gboolean purple_ipv4_address_is_valid(const char *ip);
1357 * purple_ipv6_address_is_valid:
1358 * @ip: The IP address to validate.
1360 * Checks if the given IP address is a syntactically valid IPv6 address.
1362 * Returns: True if the IP address is syntactically correct.
1364 gboolean purple_ipv6_address_is_valid(const char *ip);
1367 * purple_uri_list_extract_uris:
1368 * @uri_list: An uri-list in the standard format.
1370 * This function extracts a list of URIs from the a "text/uri-list"
1371 * string. It was "borrowed" from gnome_uri_list_extract_uris
1373 * Returns: A GList containing strings allocated with g_malloc
1374 * that have been splitted from uri-list.
1376 GList *purple_uri_list_extract_uris(const gchar *uri_list);
1379 * purple_uri_list_extract_filenames:
1380 * @uri_list: A uri-list in the standard format.
1382 * This function extracts a list of filenames from a
1383 * "text/uri-list" string. It was "borrowed" from
1384 * gnome_uri_list_extract_filenames
1386 * Returns: A GList containing strings allocated with g_malloc that
1387 * contain the filenames in the uri-list. Note that unlike
1388 * purple_uri_list_extract_uris() function, this will discard
1389 * any non-file uri from the result value.
1391 GList *purple_uri_list_extract_filenames(const gchar *uri_list);
1393 /**************************************************************************
1394 * UTF8 String Functions
1395 **************************************************************************/
1398 * purple_utf8_try_convert:
1399 * @str: The source string.
1401 * Attempts to convert a string to UTF-8 from an unknown encoding.
1403 * This function checks the locale and tries sane defaults.
1405 * Returns: The UTF-8 string, or %NULL if it could not be converted.
1407 gchar *purple_utf8_try_convert(const char *str);
1410 * purple_utf8_salvage:
1411 * @str: The source string.
1413 * Salvages the valid UTF-8 characters from a string, replacing any
1414 * invalid characters with a filler character (currently hardcoded to
1415 * '?').
1417 * Returns: A valid UTF-8 string.
1419 gchar *purple_utf8_salvage(const char *str);
1422 * purple_utf8_strip_unprintables:
1423 * @str: A valid UTF-8 string.
1425 * Removes unprintable characters from a UTF-8 string. These characters
1426 * (in particular low-ASCII characters) are invalid in XML 1.0 and thus
1427 * are not allowed in XMPP and are rejected by libxml2 by default.
1429 * The returned string must be freed by the caller.
1431 * Returns: A newly allocated UTF-8 string without the unprintable characters.
1433 gchar *purple_utf8_strip_unprintables(const gchar *str);
1436 * purple_gai_strerror:
1437 * @errnum: The error code.
1439 * Return the UTF-8 version of #gai_strerror. It calls #gai_strerror
1440 * then converts the result to UTF-8. This function is analogous to
1441 * g_strerror().
1443 * Returns: The UTF-8 error message.
1445 const gchar *purple_gai_strerror(gint errnum);
1448 * purple_utf8_strcasecmp:
1449 * @a: The first string.
1450 * @b: The second string.
1452 * Compares two UTF-8 strings case-insensitively. This comparison is
1453 * more expensive than a simple g_utf8_collate() comparison because
1454 * it calls g_utf8_casefold() on each string, which allocates new
1455 * strings.
1457 * Returns: -1 if @a is less than @b.
1458 * 0 if @a is equal to @b.
1459 * 1 if @a is greater than @b.
1461 int purple_utf8_strcasecmp(const char *a, const char *b);
1464 * purple_utf8_has_word:
1465 * @haystack: The string to search in.
1466 * @needle: The substring to find.
1468 * Case insensitive search for a word in a string. The needle string
1469 * must be contained in the haystack string and not be immediately
1470 * preceded or immediately followed by another alpha-numeric character.
1472 * Returns: TRUE if haystack has the word, otherwise FALSE
1474 gboolean purple_utf8_has_word(const char *haystack, const char *needle);
1477 * purple_print_utf8_to_console:
1478 * @filestream: The file stream (e.g. STDOUT or STDERR)
1479 * @message: The message to print.
1481 * Prints a UTF-8 message to the given file stream. The function
1482 * tries to convert the UTF-8 message to user's locale. If this
1483 * is not possible, the original UTF-8 text will be printed.
1485 void purple_print_utf8_to_console(FILE *filestream, char *message);
1488 * purple_message_meify:
1489 * @message: The message to check
1490 * @len: The message length, or -1
1492 * Checks for messages starting (post-HTML) with "/me ", including the space.
1494 * Returns: TRUE if it starts with "/me ", and it has been removed, otherwise
1495 * FALSE
1497 gboolean purple_message_meify(char *message, gssize len);
1500 * purple_text_strip_mnemonic:
1501 * @in: The string to strip
1503 * Removes the underscore characters from a string used identify the mnemonic
1504 * character.
1506 * Returns: The stripped string
1508 char *purple_text_strip_mnemonic(const char *in);
1511 * purple_unescape_filename:
1512 * @str: The string to translate.
1514 * Does the reverse of purple_escape_filename
1516 * This will change hex codes and such to their ascii equivalents.
1518 * Returns: The resulting string.
1520 const char *purple_unescape_filename(const char *str);
1523 * purple_escape_filename:
1524 * @str: The string to translate.
1526 * Escapes filesystem-unfriendly characters from a filename
1528 * Returns: The resulting string.
1530 const char *purple_escape_filename(const char *str);
1533 * purple_escape_js:
1534 * @str: The string to escape.
1536 * Escapes javascript-unfriendly substrings from a string.
1538 * Returns: The javascript-safe string (must be g_free'd after use).
1540 gchar * purple_escape_js(const gchar *str);
1543 * purple_restore_default_signal_handlers:
1545 * Restore default signal handlers for signals which might reasonably have
1546 * handlers. This should be called by a fork()'d child process, since child processes
1547 * inherit the handlers of the parent.
1549 void purple_restore_default_signal_handlers(void);
1552 * purple_get_host_name:
1554 * Gets the host name of the machine. If it not possible to determine the
1555 * host name, "localhost" is returned
1557 * Returns: The hostname
1559 const gchar *purple_get_host_name(void);
1562 * purple_uuid_random:
1564 * Returns a type 4 (random) UUID
1566 * Returns: A UUID, caller is responsible for freeing it
1568 gchar *purple_uuid_random(void);
1571 * purple_callback_set_zero:
1572 * @data: A pointer to variable, which should be set to NULL.
1574 * Sets given pointer to NULL.
1576 * Function designed to be used as a GDestroyNotify callback.
1578 void purple_callback_set_zero(gpointer data);
1581 * purple_value_new:
1582 * @type: The type of data to be held by the GValue
1584 * Creates a new GValue of the specified type.
1586 * Returns: The created GValue
1588 GValue *purple_value_new(GType type);
1591 * purple_value_dup:
1592 * @value: The GValue to duplicate
1594 * Duplicates a GValue.
1596 * Returns: The duplicated GValue
1598 GValue *purple_value_dup(GValue *value);
1601 * purple_value_free:
1602 * @value: The GValue to free.
1604 * Frees a GValue.
1606 void purple_value_free(GValue *value);
1609 * purple_http_digest_calculate_session_key:
1610 * @algorithm: The hash algorithm to use
1611 * @username: The username provided by the user
1612 * @realm: The authentication realm provided by the server
1613 * @password: The password provided by the user
1614 * @nonce: The nonce provided by the server
1615 * @client_nonce: The nonce provided by the client
1617 * Calculates a session key for HTTP Digest authentation
1619 * See RFC 2617 for more information.
1621 * Returns: The session key, or %NULL if an error occurred.
1623 gchar *purple_http_digest_calculate_session_key(
1624 const gchar *algorithm, const gchar *username,
1625 const gchar *realm, const gchar *password,
1626 const gchar *nonce, const gchar *client_nonce);
1629 * purple_http_digest_calculate_response:
1630 * @algorithm: The hash algorithm to use
1631 * @method: The HTTP method in use
1632 * @digest_uri: The URI from the initial request
1633 * @qop: The "quality of protection"
1634 * @entity: The entity body
1635 * @nonce: The nonce provided by the server
1636 * @nonce_count: The nonce count
1637 * @client_nonce: The nonce provided by the client
1638 * @session_key: The session key from purple_http_digest_calculate_session_key()
1640 * Calculate a response for HTTP Digest authentication
1642 * See RFC 2617 for more information.
1644 * Returns: The hashed response, or %NULL if an error occurred.
1646 gchar *purple_http_digest_calculate_response(
1647 const gchar *algorithm, const gchar *method,
1648 const gchar *digest_uri, const gchar *qop,
1649 const gchar *entity, const gchar *nonce,
1650 const gchar *nonce_count, const gchar *client_nonce,
1651 const gchar *session_key);
1653 G_END_DECLS
1655 #endif /* _PURPLE_UTIL_H_ */