Bug 793125 - Crash due to popup menus left attached too long
[evolution.git] / src / calendar / gui / misc.c
blobba4c52ebfb1d3b7c7efdd252bbca3e2450cc6d22
1 /*
2 * Evolution calendar - Miscellaneous utility functions
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
11 * for more details.
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program; if not, see <http://www.gnu.org/licenses/>.
17 * Authors:
18 * Federico Mena-Quintero <federico@ximian.com>
20 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
24 #include "evolution-config.h"
26 #include <ctype.h>
27 #include <time.h>
28 #include <glib/gi18n.h>
30 #include "misc.h"
32 /**
33 * string_is_empty:
34 * @value: A string.
36 * Returns whether a string is NULL, the empty string, or completely made up of
37 * whitespace characters.
39 * Return value: TRUE if the string is empty, FALSE otherwise.
40 **/
41 gboolean
42 string_is_empty (const gchar *value)
44 const gchar *p;
45 gboolean empty;
47 empty = TRUE;
49 if (value) {
50 p = value;
51 while (*p) {
52 if (!isspace ((guchar) *p)) {
53 empty = FALSE;
54 break;
56 p++;
59 return empty;
63 gint
64 get_position_in_array (GPtrArray *objects,
65 gpointer item)
67 gint i;
69 for (i = 0; i < objects->len; i++) {
70 if (g_ptr_array_index (objects, i) == item)
71 return i;
74 return -1;
77 gchar *
78 calculate_time (time_t start,
79 time_t end)
81 time_t difference = end - start;
82 gchar *str;
83 gint hours, minutes;
84 gchar *times[5];
85 gchar *joined;
86 gint i;
88 i = 0;
89 if (difference >= 24 * 3600) {
90 gint days;
92 days = difference / (24 * 3600);
93 difference %= (24 * 3600);
95 times[i++] = g_strdup_printf (ngettext ("%d day", "%d days", days), days);
97 if (difference >= 3600) {
98 hours = difference / 3600;
99 difference %= 3600;
101 times[i++] = g_strdup_printf (ngettext ("%d hour", "%d hours", hours), hours);
103 if (difference >= 60) {
104 minutes = difference / 60;
105 difference %= 60;
107 times[i++] = g_strdup_printf (ngettext ("%d minute", "%d minutes", minutes), minutes);
109 if (i == 0 || difference != 0) {
110 /* TRANSLATORS: here, "second" is the time division (like "minute"), not the ordinal number (like "third") */
111 times[i++] = g_strdup_printf (ngettext ("%d second", "%d seconds", difference), (gint) difference);
114 times[i] = NULL;
115 joined = g_strjoinv (" ", times);
116 str = g_strconcat ("(", joined, ")", NULL);
117 while (i > 0)
118 g_free (times[--i]);
119 g_free (joined);
121 return str;