evolution-3.6.4/calendar/gui/misc.c

No issues found

  1 /*
  2  * Evolution calendar - Miscellaneous utility functions
  3  *
  4  * This program is free software; you can redistribute it and/or
  5  * modify it under the terms of the GNU Lesser General Public
  6  * License as published by the Free Software Foundation; either
  7  * version 2 of the License, or (at your option) version 3.
  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  * Lesser General Public License for more details.
 13  *
 14  * You should have received a copy of the GNU Lesser General Public
 15  * License along with the program; if not, see <http://www.gnu.org/licenses/>
 16  *
 17  *
 18  * Authors:
 19  *		Federico Mena-Quintero <federico@ximian.com>
 20  *
 21  * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 22  *
 23  */
 24 
 25 #ifdef HAVE_CONFIG_H
 26 #include <config.h>
 27 #endif
 28 
 29 #include <ctype.h>
 30 #include <time.h>
 31 #include <glib/gi18n.h>
 32 
 33 #include "misc.h"
 34 
 35 /**
 36  * string_is_empty:
 37  * @value: A string.
 38  *
 39  * Returns whether a string is NULL, the empty string, or completely made up of
 40  * whitespace characters.
 41  *
 42  * Return value: TRUE if the string is empty, FALSE otherwise.
 43  **/
 44 gboolean
 45 string_is_empty (const gchar *value)
 46 {
 47 	const gchar *p;
 48 	gboolean empty;
 49 
 50 	empty = TRUE;
 51 
 52 	if (value) {
 53 		p = value;
 54 		while (*p) {
 55 			if (!isspace ((guchar) *p)) {
 56 				empty = FALSE;
 57 				break;
 58 			}
 59 			p++;
 60 		}
 61 	}
 62 	return empty;
 63 
 64 }
 65 
 66 gint
 67 get_position_in_array (GPtrArray *objects,
 68                        gpointer item)
 69 {
 70 	gint i;
 71 
 72 	for (i = 0; i < objects->len; i++) {
 73 		if (g_ptr_array_index (objects, i) == item)
 74 			return i;
 75 	}
 76 
 77 	return -1;
 78 }
 79 
 80 gchar *
 81 calculate_time (time_t start,
 82                 time_t end)
 83 {
 84 	time_t difference = end - start;
 85 	gchar *str;
 86 	gint   hours, minutes;
 87 	gchar *times[4];
 88 	gchar *joined;
 89 	gint   i;
 90 
 91 	i = 0;
 92 	if (difference >= 3600) {
 93 		hours = difference / 3600;
 94 		difference %= 3600;
 95 
 96 		times[i++] = g_strdup_printf (ngettext ("%d hour", "%d hours", hours), hours);
 97 	}
 98 	if (difference >= 60) {
 99 		minutes = difference / 60;
100 		difference %= 60;
101 
102 		times[i++] = g_strdup_printf (ngettext ("%d minute", "%d minutes", minutes), minutes);
103 	}
104 	if (i == 0 || difference != 0) {
105 		/* TRANSLATORS: here, "second" is the time division (like "minute"), not the ordinal number (like "third") */
106 		times[i++] = g_strdup_printf (ngettext ("%d second", "%d seconds", difference), (gint) difference);
107 	}
108 
109 	times[i] = NULL;
110 	joined = g_strjoinv (" ", times);
111 	str = g_strconcat ("(", joined, ")", NULL);
112 	while (i > 0)
113 		g_free (times[--i]);
114 	g_free (joined);
115 
116 	return str;
117 }