2
* Copyright (C) 2009 Michael Lamothe
4
* This file is part of Me TV
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
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 Library General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor Boston, MA 02110-1301, USA
22
#include "me-tv-i18n.h"
23
#include "exception.h"
24
#include <glib/gprintf.h>
30
StringSignal signal_error;
32
StringSignal& get_signal_error()
37
void replace_text(Glib::ustring& text, const Glib::ustring& from, const Glib::ustring& to)
39
Glib::ustring::size_type position = 0;
40
while ((position = text.find(from, position)) != Glib::ustring::npos)
42
text.replace(position, from.size(), to);
43
position += to.size();
47
Glib::ustring get_local_time_text(const gchar* format)
49
return get_local_time_text(time(NULL), format);
52
Glib::ustring get_local_time_text(time_t t, const gchar* format)
57
if (localtime_r(&t, &tp) == NULL)
59
throw Exception(_("Failed to get time"));
61
strftime(buffer, 100, format, &tp);
66
Glib::ustring encode_xml(const Glib::ustring& s)
68
Glib::ustring result = s;
70
replace_text(result, "&", "&");
71
replace_text(result, "<", "<");
72
replace_text(result, ">", ">");
73
replace_text(result, "'", "'");
74
replace_text(result, "\"", """);
79
guint convert_to_local_time(guint utc)
81
return utc + timezone;
84
guint convert_to_utc_time(guint local_time)
86
return local_time - timezone;
89
void log_handler(const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer user_data)
91
if (log_level != G_LOG_LEVEL_DEBUG || verbose_logging)
93
Glib::ustring time_text = get_local_time_text("%x %T");
94
g_printf("%s: %s\n", time_text.c_str(), message);
98
void on_error(const Glib::ustring& message)
100
g_message("%s", message.c_str());
103
guint convert_string_to_value(const StringTable* table, const Glib::ustring& text)
105
gboolean found = false;
106
const StringTable* current = table;
108
while (current->text != NULL && !found)
110
if (text == current->text)
122
throw Exception(Glib::ustring::compose(_("Failed to find a value for '%1'"), text));
125
return (guint)current->value;
128
Glib::ustring convert_value_to_string(const StringTable* table, guint value)
130
gboolean found = false;
131
const StringTable* current = table;
133
while (current->text != NULL && !found)
135
if (value == current->value)
147
throw Exception(Glib::ustring::compose(_("Failed to find a text value for '%1'"), value));
150
return current->text;