~ubuntu-branches/ubuntu/precise/me-tv/precise-proposed

« back to all changes in this revision

Viewing changes to src/me-tv.cc

  • Committer: Bazaar Package Importer
  • Author(s): Michael Lamothe
  • Date: 2009-02-15 12:41:57 UTC
  • mfrom: (1.1.4 upstream) (3.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20090215124157-v3evstjm4zs1x9pp
Tags: 0.7.14-1
* New upstream release
* Switched to dh 7

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2009 Michael Lamothe
 
3
 *
 
4
 * This file is part of Me TV
 
5
 *
 
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.
 
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 Library General Public License for more details.
 
15
 * 
 
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
 
19
 */
 
20
 
 
21
#include "me-tv.h"
 
22
#include "me-tv-i18n.h"
 
23
#include "exception.h"
 
24
#include <glib/gprintf.h>
 
25
 
 
26
bool verbose_logging;
 
27
bool safe_mode;
 
28
bool minimised_mode;
 
29
 
 
30
StringSignal signal_error;
 
31
 
 
32
StringSignal& get_signal_error()
 
33
{
 
34
        return signal_error;
 
35
}
 
36
 
 
37
void replace_text(Glib::ustring& text, const Glib::ustring& from, const Glib::ustring& to)
 
38
{
 
39
        Glib::ustring::size_type position = 0;
 
40
        while ((position = text.find(from, position)) != Glib::ustring::npos)
 
41
        {
 
42
                text.replace(position, from.size(), to);
 
43
                position += to.size();
 
44
        }
 
45
}
 
46
 
 
47
Glib::ustring get_local_time_text(const gchar* format)
 
48
{
 
49
        return get_local_time_text(time(NULL), format);
 
50
}
 
51
 
 
52
Glib::ustring get_local_time_text(time_t t, const gchar* format)
 
53
{
 
54
        struct tm tp;
 
55
        char buffer[100];
 
56
 
 
57
        if (localtime_r(&t, &tp) == NULL)
 
58
        {
 
59
                throw Exception(_("Failed to get time"));
 
60
        }
 
61
        strftime(buffer, 100, format, &tp);
 
62
        
 
63
        return buffer;
 
64
}
 
65
 
 
66
Glib::ustring encode_xml(const Glib::ustring& s)
 
67
{
 
68
        Glib::ustring result = s;
 
69
        
 
70
        replace_text(result, "&", "&amp;");
 
71
        replace_text(result, "<", "&lt;");
 
72
        replace_text(result, ">", "&gt;");
 
73
        replace_text(result, "'", "&apos;");
 
74
        replace_text(result, "\"", "&quot;");
 
75
 
 
76
        return result;
 
77
}
 
78
 
 
79
guint convert_to_local_time(guint utc)
 
80
{
 
81
        return utc + timezone;
 
82
}
 
83
 
 
84
guint convert_to_utc_time(guint local_time)
 
85
{
 
86
        return local_time - timezone;
 
87
}
 
88
 
 
89
void log_handler(const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer user_data)
 
90
{
 
91
        if (log_level != G_LOG_LEVEL_DEBUG || verbose_logging)
 
92
        {
 
93
                Glib::ustring time_text = get_local_time_text("%x %T");
 
94
                g_printf("%s: %s\n", time_text.c_str(), message);
 
95
        }
 
96
}
 
97
 
 
98
void on_error(const Glib::ustring& message)
 
99
{
 
100
        g_message("%s", message.c_str());
 
101
}
 
102
 
 
103
guint convert_string_to_value(const StringTable* table, const Glib::ustring& text)
 
104
{
 
105
        gboolean found = false;
 
106
        const StringTable*      current = table;
 
107
 
 
108
        while (current->text != NULL && !found)
 
109
        {
 
110
                if (text == current->text)
 
111
                {
 
112
                        found = true;
 
113
                }
 
114
                else
 
115
                {
 
116
                        current++;
 
117
                }
 
118
        }
 
119
        
 
120
        if (!found)
 
121
        {
 
122
                throw Exception(Glib::ustring::compose(_("Failed to find a value for '%1'"), text));
 
123
        }
 
124
        
 
125
        return (guint)current->value;
 
126
}
 
127
 
 
128
Glib::ustring convert_value_to_string(const StringTable* table, guint value)
 
129
{
 
130
        gboolean found = false;
 
131
        const StringTable*      current = table;
 
132
 
 
133
        while (current->text != NULL && !found)
 
134
        {
 
135
                if (value == current->value)
 
136
                {
 
137
                        found = true;
 
138
                }
 
139
                else
 
140
                {
 
141
                        current++;
 
142
                }
 
143
        }
 
144
        
 
145
        if (!found)
 
146
        {
 
147
                throw Exception(Glib::ustring::compose(_("Failed to find a text value for '%1'"), value));
 
148
        }
 
149
        
 
150
        return current->text;
 
151
}