-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.cpp
220 lines (193 loc) · 6.13 KB
/
utils.cpp
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
213
214
215
216
217
218
219
220
#include "utils.h"
#include <libalarm.h>
#include "gconfitem.h"
#include <QDateTime>
#include <QLocale>
#include <QProcess>
#include <QMaemo5InformationBox>
#include <QCoreApplication>
#include <glib.h>
bool is24HoursClock()
{
return GConfItem("/apps/clock/time-format").value().toBool();
}
QString formatDateTime(const time_t tick, DateTime what)
{
char buf[256];
size_t len = 0;
const struct tm *t = localtime(&tick);
switch (what)
{
case Time:
{
if (is24HoursClock())
len = _strftime(buf, sizeof(buf), "wdgt_va_24h_time", t);
else
{
if (t->tm_hour > 11)
len = _strftime(buf, sizeof(buf), "wdgt_va_12h_time_pm", t);
else
len = _strftime(buf, sizeof(buf), "wdgt_va_12h_time_am", t);
}
break;
}
case Hour12:
{
len = _strftime(buf, sizeof(buf), "wdgt_va_12h_hours", t);
break;
}
case Time12:
{
return formatDateTime(tick, Hour12) + ":" +
formatDateTime(tick, Minutes);
}
case TimeSeconds:
{
len = _strftime(buf, sizeof(buf), "wdgt_va_full_24h_time", t);
break;
}
case Minutes:
{
len = _strftime(buf, sizeof(buf), "wdgt_va_minutes", t);
break;
}
case MinutesSeconds:
{
len = _strftime(buf, sizeof(buf), "wdgt_va_minutes_seconds", t);
break;
}
case amPm:
{
if (t->tm_hour > 11)
len = _strftime(buf, sizeof(buf), "wdgt_va_pm", t);
else
len = _strftime(buf, sizeof(buf), "wdgt_va_am", t);
break;
}
case DayOfWeek:
len = _strftime(buf, sizeof(buf), "wdgt_va_week", t);
break;
case Date:
len = _strftime(buf, sizeof(buf), "wdgt_va_date", t);
break;
case FullDateShort:
{
QString rv;
len = _strftime(buf, sizeof(buf), "wdgt_va_week_name_short", t);
rv = QString::fromUtf8(buf, len) + ", ";
len = _strftime(buf, sizeof(buf), "wdgt_va_day_numeric", t);
rv += QString::fromUtf8(buf, len) + " ";
len = _strftime(buf, sizeof(buf), "wdgt_va_month_name_short", t);
rv += QString::fromUtf8(buf, len) + " ";
len = _strftime(buf, sizeof(buf), "wdgt_va_year", t);
rv += QString::fromUtf8(buf, len);
return rv;
}
case FullDateLong:
len = _strftime(buf, sizeof(buf), "wdgt_va_date_long", t);
break;
}
return QString::fromUtf8(buf, len);
}
QStringList daysFromWday(uint32_t mask_wday)
{
QStringList rv;
QLocale locale;
if (!mask_wday)
rv << _("cloc_va_never");
else if (mask_wday == ALARM_RECUR_WDAY_ALL)
rv << _("cloc_va_everyday");
else
{
for (int i = 1; i < 8; i ++)
{
/* Sunday is 1 << 0 */
if (mask_wday & (1 << (i % 7)))
rv << locale.dayName(i, QLocale::ShortFormat);
}
}
return rv;
}
#define SECONDS_PER_MINUTE 60
#define MINUTES_PER_HOUR 60
#define HOURS_PER_DAY 24
#define SECONDS_PER_HOUR (MINUTES_PER_HOUR * SECONDS_PER_MINUTE)
#define SECONDS_PER_DAY (HOURS_PER_DAY * SECONDS_PER_HOUR)
/* FIXME - try to not use glib */
static QString formatAlarmTimeBanner(time_t tick)
{
time_t now = QDateTime::currentDateTime().toTime_t();
struct tm t;
char buf[256];
gchar *d, *h, *s;
QString rv;
const char *cloc_notify_alarm_set = "cloc_notify_alarm_set";
intl("osso-clock");
memset(&t, 0, sizeof(t));
if (now <= tick)
{
time_t diff = tick - now;
if (diff < SECONDS_PER_DAY)
{
t.tm_hour = (diff % SECONDS_PER_DAY) / SECONDS_PER_HOUR;
t.tm_min = (diff % SECONDS_PER_HOUR) / SECONDS_PER_MINUTE;
}
else
{
int days = diff / SECONDS_PER_DAY;
int hours = (diff % SECONDS_PER_DAY) / SECONDS_PER_HOUR;
const char *cloc_va_amount_day = "cloc_va_amount_day";
const char *cloc_va_amount_hour2 = "cloc_va_amount_hour2";
const char *cloc_notify_alarm_set_days =
"cloc_notify_alarm_set_days";
d = g_strdup_printf(dngettext(NULL, cloc_va_amount_day,
"cloc_va_amount_days", days),
days);
h = g_strdup_printf(dngettext(NULL, cloc_va_amount_hour2,
"cloc_va_amount_hours2", hours), d,
hours);
s = g_strdup_printf(dgettext(NULL, cloc_notify_alarm_set_days), h);
g_free(d);
g_free(h);
rv = QString::fromUtf8(s);
g_free(s);
goto out;
}
}
_strftime(buf, sizeof(buf), "wdgt_va_24h_time", &t);
s = g_strdup_printf(dgettext(NULL, cloc_notify_alarm_set), buf);
rv = QString::fromUtf8(s);
g_free(s);
out:
return rv;
}
void showAlarmTimeBanner(time_t tick)
{
QMaemo5InformationBox::information(NULL, formatAlarmTimeBanner(tick));
QCoreApplication::processEvents();
}
QString formatTimeMarkup(time_t tick, const QString &timeSize,
const QString &amPmSize, const QString &align,
bool seconds)
{
QString rv;
if (is24HoursClock())
{
QString time;
if (seconds)
time = formatDateTime(tick, TimeSeconds);
else
time = formatDateTime(tick, Time);
rv = "<p align=" + align + " style=" + timeSize +
"margin-top:0px;margin-bottom:0px;>" + time + "</p>";
}
else
{
QString tmp = formatDateTime(tick, Hour12) + ":";
tmp += formatDateTime(tick, seconds ? MinutesSeconds : Minutes);
rv = "<p align=" + align + " style=" + timeSize + ">" + tmp;
rv += "<span style=" + amPmSize + "> ";
rv += formatDateTime(tick, amPm) + "</span></p>";
}
return rv;
}