2 * Copyright (c) 2007, 2008, 2009, Czirkos Zoltan <cirix@fw.hu>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <glib/gi18n.h>
22 GList
*gd_errors
=NULL
;
23 static gboolean has_new_error
=FALSE
;
24 static char *error_context
=NULL
;
28 error_free(GdErrorMessage
*error
)
30 g_free(error
->message
);
43 g_list_foreach(gd_errors
, (GFunc
) error_free
, NULL
);
44 g_list_free(gd_errors
);
46 gd_clear_error_flag();
50 gd_error_set_context(const char *format
, ...)
54 g_free(error_context
);
59 error_context
=g_strdup_vprintf(format
, ap
);
64 log_func(const gchar
*log_domain
, GLogLevelFlags log_level
, const gchar
*message
, gpointer user_data
)
66 GdErrorMessage
*error
;
67 error
=g_new(GdErrorMessage
, 1);
68 error
->flags
=log_level
& G_LOG_LEVEL_MASK
;
70 error
->message
=g_strdup_printf("%s: %s", error_context
, message
);
72 error
->message
=g_strdup(message
);
74 if ((log_level
&G_LOG_LEVEL_MASK
) <= G_LOG_LEVEL_WARNING
)
77 gd_errors
=g_list_append(gd_errors
, error
);
78 /* also call default handler to print to console; but with processed string */
79 g_log_default_handler(log_domain
, log_level
, error
->message
, user_data
);
82 gboolean
gd_has_new_error()
88 gd_install_log_handler()
90 g_log_set_default_handler (log_func
, NULL
);
98 /* returns a static string which contains the utf8 representation of the filename in system encoding */
100 gd_filename_to_utf8(const char *filename
)
102 static char *utf8
=NULL
;
106 utf8
=g_filename_to_utf8(filename
, -1, NULL
, NULL
, &error
);
109 utf8
=g_strdup(filename
); /* use without conversion */
118 find_file_try_path(const char *path
, const char *filename
)
120 static char *result
=NULL
;
124 result
=g_build_path(G_DIR_SEPARATOR_S
, path
, filename
, NULL
);
125 /* if the file exists, return the full filename */
126 if (g_file_test(result
, G_FILE_TEST_EXISTS
))
129 /* otherwise return nothing */
135 /* tries to find a file in the gdash installation and returns a path (owned by the above function, not to be g_free()d) */
137 gd_find_file(const char *filename
)
141 result
=find_file_try_path(gd_user_config_dir
, filename
);
145 result
=find_file_try_path(gd_system_data_dir
, filename
);
149 result
=find_file_try_path(gd_system_sound_dir
, filename
);
153 result
=find_file_try_path(".", filename
);
158 result
=find_file_try_path("./sound", filename
);
162 g_critical("cannot find file: %s", gd_filename_to_utf8(filename
));
171 gd_wrap_text(const char *orig
, int width
)
177 g_assert(orig
!=NULL
);
178 wrapped
=g_string_new(NULL
);
179 lines
=g_strsplit_set(orig
, "\n", -1);
180 for (l
=0; lines
[l
]!=NULL
; l
++) {
185 words
=g_strsplit_set(lines
[l
], " ", -1);
187 for (w
=0; words
[w
]!=NULL
; w
++) {
190 curlen
=strlen(words
[w
]);
191 if (curwidth
==0 || curwidth
+curlen
<width
) {
194 g_string_append_c(wrapped
, ' ');
196 g_string_append(wrapped
, words
[w
]);
199 g_string_append_c(wrapped
, '\n');
200 g_string_append(wrapped
, words
[w
]);
206 g_string_append_c(wrapped
, '\n');
210 return g_string_free(wrapped
, FALSE
);
214 gd_lines_in_text(const char *text
)
218 g_assert(text
!=NULL
);
220 for (i
=0; text
[i
]!=0; i
++)
227 /* return current date in 2008-12-04 format */
229 gd_get_current_date()
232 static char dats
[128];
235 g_date_set_time_t(dat
, time(NULL
));
236 g_date_strftime(dats
, sizeof(dats
), "%Y-%m-%d", dat
);
242 /* return current date in 2008-12-04 12:34 format */
244 gd_get_current_date_time()
247 static char dats
[128];
250 g_date_set_time_t(dat
, time(NULL
));
251 g_date_strftime(dats
, sizeof(dats
), "%Y-%m-%d %H:%I", dat
);
258 gd_clamp(int val
, int min
, int max
)