aboutsummaryrefslogtreecommitdiff
path: root/src/formatter-desktop.cpp
blob: 3f942f47a07c6d3196057a517a999c8ca9abf5ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
 * Copyright 2013 Canonical Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 3, as published
 * by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranties of
 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authors:
 *   Charles Kerr <charles.kerr@canonical.com>
 */

#include <datetime/formatter.h>
#include <datetime/settings-shared.h>

#include <glib.h>
#include <gio/gio.h>

namespace unity {
namespace indicator {
namespace datetime {

/***
****
***/

class DesktopFormatter::Impl
{
public:

Impl(DesktopFormatter * owner, const std::shared_ptr<Clock>& clock):
    m_owner(owner),
    m_clock(clock),
    m_settings(g_settings_new(SETTINGS_INTERFACE))
{
    const gchar * const keys[] = { "changed::" SETTINGS_SHOW_SECONDS_S,
                                   "changed::" SETTINGS_TIME_FORMAT_S,
                                   "changed::" SETTINGS_TIME_FORMAT_S,
                                   "changed::" SETTINGS_CUSTOM_TIME_FORMAT_S,
                                   "changed::" SETTINGS_SHOW_DAY_S,
                                   "changed::" SETTINGS_SHOW_DATE_S,
                                   "changed::" SETTINGS_SHOW_YEAR_S };
    for(const auto& key : keys)
        g_signal_connect(m_settings, key, G_CALLBACK(onSettingsChanged), this);

    rebuildHeaderFormat();
}

~Impl()
{
    g_signal_handlers_disconnect_by_data(m_settings, this);
    g_object_unref(m_settings);
}

private:

static void onSettingsChanged(GSettings   * /*changed*/,
                              const gchar * /*key*/,
                              gpointer      gself)
{
    static_cast<Impl*>(gself)->rebuildHeaderFormat();
}

void rebuildHeaderFormat()
{
    auto fmt = getHeaderLabelFormatString(m_settings);
    m_owner->headerFormat.set(fmt);
    g_free(fmt);
}

private:

gchar* getHeaderLabelFormatString(GSettings* s) const
{
    char * fmt;
    const auto mode = g_settings_get_enum(s, SETTINGS_TIME_FORMAT_S);

    if (mode == TIME_FORMAT_MODE_CUSTOM)
    {
        fmt = g_settings_get_string(s, SETTINGS_CUSTOM_TIME_FORMAT_S);
    }
    else
    {
        const auto show_day = g_settings_get_boolean(s, SETTINGS_SHOW_DAY_S);
        const auto show_date = g_settings_get_boolean(s, SETTINGS_SHOW_DATE_S);
        const auto show_year = show_date && g_settings_get_boolean(s, SETTINGS_SHOW_YEAR_S);
        const auto date_fmt = getDateFormat(show_day, show_date, show_year);
        const auto time_fmt = getFullTimeFormatString(s);
        fmt = joinDateAndTimeFormatStrings(date_fmt, time_fmt);
    }

    return fmt;
}

const gchar* T_(const gchar* in) const
{
    return m_owner->T_(in);
}

const gchar* getDateFormat(bool show_day, bool show_date, bool show_year) const
{
    const char * fmt;

    if (show_day && show_date && show_year)
        /* TRANSLATORS: a strftime(3) format showing the weekday, date, and year */
        fmt = T_("%a %b %e %Y");
    else if (show_day && show_date)
        /* TRANSLATORS: a strftime(3) format showing the weekday and date */
        fmt = T_("%a %b %e");
    else if (show_day && show_year)
        /* TRANSLATORS: a strftime(3) format showing the weekday and year. */
        fmt = T_("%a %Y");
    else if (show_day)
        /* TRANSLATORS: a strftime(3) format showing the weekday. */
        fmt = T_("%a");
    else if (show_date && show_year)
        /* TRANSLATORS: a strftime(3) format showing the date and year */
        fmt = T_("%b %e %Y");
    else if (show_date)
        /* TRANSLATORS: a strftime(3) format showing the date */
        fmt = T_("%b %e");
    else if (show_year)
        /* TRANSLATORS: a strftime(3) format showing the year */
        fmt = T_("%Y");
    else
        fmt = nullptr;

    return fmt;
}

const gchar* getFullTimeFormatString(GSettings* settings) const
{
    auto show_seconds = g_settings_get_boolean(settings, SETTINGS_SHOW_SECONDS_S);

    bool twelvehour;
    switch (g_settings_get_enum(settings, SETTINGS_TIME_FORMAT_S))
    {
    case TIME_FORMAT_MODE_LOCALE_DEFAULT:
        twelvehour = is_locale_12h();
        break;

    case TIME_FORMAT_MODE_24_HOUR:
        twelvehour = false;
        break;

    default:
        twelvehour = true;
        break;
    }

    return m_owner->getDefaultHeaderTimeFormat(twelvehour, show_seconds);
}

gchar* joinDateAndTimeFormatStrings(const char* date_string,
                                    const char* time_string) const
{
    gchar * str;

    if (date_string && time_string)
    {
        /* TRANSLATORS: This is a format string passed to strftime to
         * combine the date and the time.  The value of "%s\u2003%s"
         * will result in a string like this in US English 12-hour time:
         * 'Fri Jul 16 11:50 AM'. The space in between date and time is
         * a Unicode en space (E28082 in UTF-8 hex). */
        str =  g_strdup_printf("%s\u2003%s", date_string, time_string);
    }
    else if (date_string)
    {
        str = g_strdup(date_string);
    }
    else // time_string
    {
        str = g_strdup(time_string);
    }

    return str;
}

private:

DesktopFormatter * const m_owner;
std::shared_ptr<Clock> m_clock;
GSettings * m_settings;
};

/***
****
***/

DesktopFormatter::DesktopFormatter(const std::shared_ptr<Clock>& clock):
    Formatter(clock),
    p(new Impl(this, clock))
{
}

DesktopFormatter::~DesktopFormatter() = default;

/***
****
***/

} // namespace datetime
} // namespace indicator
} // namespace unity