No issues found
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
2 *
3 * Copyright (C) 2000 Eazel, Inc.
4 * Copyright (C) 2002 Jorn Baayen
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Library General Public License
8 * as published by the Free Software Foundation; either version 2, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public
17 * License along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 * Authors: John Sullivan <sullivan@eazel.com>
21 * Jorn Baayen
22 */
23
24 #include <config.h>
25
26 #include <string.h>
27 #include <glib/gi18n.h>
28
29 #include "rb-cut-and-paste-code.h"
30
31 /*LGPL'd*/
32 GdkPixbuf *
33 eel_create_colorized_pixbuf (GdkPixbuf *src,
34 int red_value,
35 int green_value,
36 int blue_value)
37 {
38 int i, j;
39 int width, height, has_alpha, src_row_stride, dst_row_stride;
40 guchar *target_pixels;
41 guchar *original_pixels;
42 guchar *pixsrc;
43 guchar *pixdest;
44 GdkPixbuf *dest;
45
46 g_return_val_if_fail (gdk_pixbuf_get_colorspace (src) == GDK_COLORSPACE_RGB, NULL);
47 g_return_val_if_fail ((!gdk_pixbuf_get_has_alpha (src)
48 && gdk_pixbuf_get_n_channels (src) == 3)
49 || (gdk_pixbuf_get_has_alpha (src)
50 && gdk_pixbuf_get_n_channels (src) == 4), NULL);
51 g_return_val_if_fail (gdk_pixbuf_get_bits_per_sample (src) == 8, NULL);
52
53 dest = gdk_pixbuf_new (gdk_pixbuf_get_colorspace (src),
54 gdk_pixbuf_get_has_alpha (src),
55 gdk_pixbuf_get_bits_per_sample (src),
56 gdk_pixbuf_get_width (src),
57 gdk_pixbuf_get_height (src));
58
59 has_alpha = gdk_pixbuf_get_has_alpha (src);
60 width = gdk_pixbuf_get_width (src);
61 height = gdk_pixbuf_get_height (src);
62 src_row_stride = gdk_pixbuf_get_rowstride (src);
63 dst_row_stride = gdk_pixbuf_get_rowstride (dest);
64 target_pixels = gdk_pixbuf_get_pixels (dest);
65 original_pixels = gdk_pixbuf_get_pixels (src);
66
67 for (i = 0; i < height; i++) {
68 pixdest = target_pixels + i*dst_row_stride;
69 pixsrc = original_pixels + i*src_row_stride;
70 for (j = 0; j < width; j++) {
71 *pixdest++ = (*pixsrc++ * red_value) >> 8;
72 *pixdest++ = (*pixsrc++ * green_value) >> 8;
73 *pixdest++ = (*pixsrc++ * blue_value) >> 8;
74 if (has_alpha) {
75 *pixdest++ = *pixsrc++;
76 }
77 }
78 }
79 return dest;
80 }
81
82 /* Based on evolution/mail/message-list.c:filter_date() */
83 char *
84 rb_utf_friendly_time (time_t date)
85 {
86 GDateTime *datetime, *now, *yesterday;
87 int d, m, y;
88 int nd, nm, ny;
89 int yd, ym, yy;
90 const char *format = NULL;
91 char *str = NULL;
92
93 if (date == 0) {
94 return g_strdup (_("Never"));
95 }
96
97 now = g_date_time_new_now_local ();
98 datetime = g_date_time_new_from_unix_local (date);
99
100 g_date_time_get_ymd (datetime, &y, &m, &d);
101 g_date_time_get_ymd (now, &ny, &nm, &nd);
102
103 if (y == ny && m == nm && d == nd) {
104 /* Translators: "friendly time" string for the current day, strftime format. like "Today 12:34 am" */
105 format = _("Today %I:%M %p");
106 }
107
108 if (format == NULL) {
109 yesterday = g_date_time_add_days (now, -1);
110
111 g_date_time_get_ymd (yesterday, &yy, &ym, &yd);
112 if (y == yy && m == ym && d == yd) {
113 /* Translators: "friendly time" string for the previous day,
114 * strftime format. e.g. "Yesterday 12:34 am"
115 */
116 format = _("Yesterday %I:%M %p");
117 }
118 g_date_time_unref (yesterday);
119 }
120
121 if (format == NULL) {
122 int i;
123 for (i = 2; i < 7; i++) {
124 yesterday = g_date_time_add_days (now, -i);
125 g_date_time_get_ymd (yesterday, &yy, &ym, &yd);
126 if (y == yy && m == ym && d == yd) {
127 /* Translators: "friendly time" string for a day in the current week,
128 * strftime format. e.g. "Wed 12:34 am"
129 */
130 format = _("%a %I:%M %p");
131 g_date_time_unref (yesterday);
132 break;
133 }
134 g_date_time_unref (yesterday);
135 }
136 }
137
138 if (format == NULL) {
139 if (y == ny) {
140 /* Translators: "friendly time" string for a day in the current year,
141 * strftime format. e.g. "Feb 12 12:34 am"
142 */
143 format = _("%b %d %I:%M %p");
144 } else {
145 /* Translators: "friendly time" string for a day in a different year,
146 * strftime format. e.g. "Feb 12 1997"
147 */
148 format = _("%b %d %Y");
149 }
150 }
151
152 if (format != NULL) {
153 str = g_date_time_format (datetime, format);
154 }
155
156 if (str == NULL) {
157 /* impossible time or broken locale settings */
158 str = g_strdup (_("Unknown"));
159 }
160
161 g_date_time_unref (datetime);
162 g_date_time_unref (now);
163
164 return str;
165 }
166
167 /* Copied from eel-vfs-extensions.c from eel CVS HEAD on 2004-05-09
168 * This function is (C) 1999, 2000 Eazel, Inc.
169 */
170 char *
171 rb_make_valid_utf8 (const char *name, char substitute)
172 {
173 GString *string;
174 const char *remainder, *invalid;
175 int remaining_bytes, valid_bytes;
176
177 string = NULL;
178 remainder = name;
179 remaining_bytes = strlen (name);
180
181 while (remaining_bytes != 0) {
182 if (g_utf8_validate (remainder, remaining_bytes, &invalid)) {
183 break;
184 }
185 valid_bytes = invalid - remainder;
186
187 if (string == NULL) {
188 string = g_string_sized_new (remaining_bytes);
189 }
190 g_string_append_len (string, remainder, valid_bytes);
191 g_string_append_c (string, substitute);
192
193 remaining_bytes -= valid_bytes + 1;
194 remainder = invalid + 1;
195 }
196
197 if (string == NULL) {
198 return g_strdup (name);
199 }
200
201 g_string_append (string, remainder);
202 g_assert (g_utf8_validate (string->str, -1, NULL));
203
204 return g_string_free (string, FALSE);
205 }