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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572 | diff --git a/CMakeLists.txt b/CMakeLists.txt
index fe19502..6561757 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -41,6 +41,7 @@ if(START_TIMEOUT)
add_definitions(-DSTART_TIMEOUT=${START_TIMEOUT})
endif()
+add_subdirectory(autotests)
add_subdirectory(data)
add_subdirectory(src)
diff --git a/autotests/CMakeLists.txt b/autotests/CMakeLists.txt
new file mode 100644
index 0000000..a4ce788
--- /dev/null
+++ b/autotests/CMakeLists.txt
@@ -0,0 +1,32 @@
+remove_definitions(-DQT_NO_CAST_FROM_ASCII)
+
+find_package(Qt5Test 5.0.0 REQUIRED NO_MODULE)
+set_package_properties(Qt5Test PROPERTIES PURPOSE "Required for tests")
+
+set(EXECUTABLE_OUTPUT_PATH ${CMAKE_CURRENT_BINARY_DIR})
+
+include(ECMAddTests)
+
+add_definitions( -DTEST_DATA="${CMAKE_CURRENT_SOURCE_DIR}/data" )
+
+ecm_add_test(TEST_NAME hooktest
+ hooktest.cpp
+ ../src/daemon/hookevent/hook.cpp
+ ../src/daemon/hookevent/locale.cpp
+ LINK_LIBRARIES
+ Qt5::Core
+ Qt5::Test
+ KF5::CoreAddons
+ KF5::Service
+)
+
+ecm_add_test(TEST_NAME localetest
+ localetest.cpp
+ ../src/daemon/hookevent/locale.cpp
+ LINK_LIBRARIES
+ Qt5::Core
+ Qt5::Test
+ KF5::CoreAddons
+ KF5::Service
+)
+
diff --git a/autotests/data/hooktest/invalidFile b/autotests/data/hooktest/invalidFile
new file mode 100644
index 0000000..e69de29
diff --git a/autotests/data/hooktest/validFile b/autotests/data/hooktest/validFile
new file mode 100644
index 0000000..6786366
--- /dev/null
+++ b/autotests/data/hooktest/validFile
@@ -0,0 +1,10 @@
+Name: apt-file update needed
+Name-de_DE: sauerkraut lederhosen
+Name-fr.UTF-8: Échec du téléchargement des données supplémentaires
+Priority: Medium
+Command: "/usr/share/apt-file/do-apt-file-update"
+Terminal: True
+DisplayIf: /usr/share/apt-file/is-cache-empty
+Description: description
+Description-de_DE: vieles sauerkraut lederhosen
+
diff --git a/autotests/hooktest.cpp b/autotests/hooktest.cpp
new file mode 100644
index 0000000..7d3606a
--- /dev/null
+++ b/autotests/hooktest.cpp
@@ -0,0 +1,86 @@
+/***************************************************************************
+ * Copyright © 2015 Harald Sitter <sitter@kde.org> *
+ * *
+ * This program is free software; you can redistribute it and/or *
+ * modify it under the terms of the GNU General Public License as *
+ * published by the Free Software Foundation; either version 2 of *
+ * the License or (at your option) version 3 or any later version *
+ * accepted by the membership of KDE e.V. (or its successor approved *
+ * by the membership of KDE e.V.), which shall act as a proxy *
+ * defined in Section 14 of version 3 of the license. *
+ * *
+ * This program is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY 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/>. *
+ ***************************************************************************/
+
+#include <QObject>
+#include <QtTest>
+
+#include "../src/daemon/hookevent/hook.h"
+
+class HookTest : public QObject
+{
+ Q_OBJECT
+private Q_SLOTS:
+ void initTestCase();
+ void init();
+
+ void ctor();
+ void validFile();
+ void invalidFile();
+
+private:
+ QString data(const QString func);
+
+ QString m_dataPath;
+};
+
+void HookTest::initTestCase()
+{
+ m_dataPath = QStringLiteral(TEST_DATA) + QStringLiteral("/hooktest");
+}
+
+void HookTest::init()
+{
+ setlocale(LC_ALL, "en_US.UTF-8");
+}
+
+void HookTest::ctor()
+{
+ Hook h(nullptr, QStringLiteral("/"));
+}
+
+void HookTest::validFile()
+{
+ Hook h(nullptr, data("validFile"));
+ QVERIFY(h.isValid());
+ QCOMPARE(h.getField("Name"), QString("apt-file update needed"));
+ QCOMPARE(h.getField("Terminal"), QString("True"));
+ QCOMPARE(h.getField("Command"), QString("\"/usr/share/apt-file/do-apt-file-update\""));
+ h.setLocale("de_DE.UTF-8");
+ QCOMPARE(h.getField("Name"), QString("sauerkraut lederhosen"));
+ QCOMPARE(h.getField("Terminal"), QString("True"));
+ QCOMPARE(h.getField("Command"), QString("\"/usr/share/apt-file/do-apt-file-update\""));
+ h.setLocale("fr_FR.UTF-8");
+ QCOMPARE(h.getField("Name"), QString("Échec du téléchargement des données supplémentaires"));
+}
+
+void HookTest::invalidFile()
+{
+ Hook h(nullptr, data("invalidFile"));
+ QVERIFY(!h.isValid());
+}
+
+QString HookTest::data(const QString func)
+{
+ return m_dataPath + "/" + func;
+}
+
+QTEST_GUILESS_MAIN(HookTest);
+
+#include "hooktest.moc"
diff --git a/autotests/localetest.cpp b/autotests/localetest.cpp
new file mode 100644
index 0000000..6c30fc6
--- /dev/null
+++ b/autotests/localetest.cpp
@@ -0,0 +1,103 @@
+/***************************************************************************
+ * Copyright © 2015 Harald Sitter <sitter@kde.org> *
+ * *
+ * This program is free software; you can redistribute it and/or *
+ * modify it under the terms of the GNU General Public License as *
+ * published by the Free Software Foundation; either version 2 of *
+ * the License or (at your option) version 3 or any later version *
+ * accepted by the membership of KDE e.V. (or its successor approved *
+ * by the membership of KDE e.V.), which shall act as a proxy *
+ * defined in Section 14 of version 3 of the license. *
+ * *
+ * This program is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY 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/>. *
+ ***************************************************************************/
+
+#include <QObject>
+#include <QtTest>
+
+#define private public
+#include "../src/daemon/hookevent/locale.h"
+#undef private
+
+class LocaleTest : public QObject
+{
+ Q_OBJECT
+private Q_SLOTS:
+ void en();
+ void enEncoding();
+ void en_US();
+ void en_USVariant();
+ void en_USVariantEncoding();
+ void en_USEncoding();
+};
+
+void LocaleTest::en()
+{
+ Locale l(QStringLiteral("en"));
+ QCOMPARE(l.m_language, QStringLiteral("en"));
+ QCOMPARE(l.m_country, QString());
+ QCOMPARE(l.m_variant, QString());
+ QCOMPARE(l.m_encoding, QString());
+ QCOMPARE(l.combinations(), QStringList() << "en");
+}
+
+void LocaleTest::enEncoding()
+{
+ Locale l(QStringLiteral("en.UTF-8"));
+ QCOMPARE(l.m_language, QStringLiteral("en"));
+ QCOMPARE(l.m_country, QString());
+ QCOMPARE(l.m_variant, QString());
+ QCOMPARE(l.m_encoding, QStringLiteral("UTF-8"));
+ QCOMPARE(l.combinations(), QStringList() << "en.UTF-8" << "en");
+}
+
+
+void LocaleTest::en_US()
+{
+ Locale l(QStringLiteral("en_US"));
+ QCOMPARE(l.m_language, QStringLiteral("en"));
+ QCOMPARE(l.m_country, QStringLiteral("US"));
+ QCOMPARE(l.m_variant, QString());
+ QCOMPARE(l.m_encoding, QString());
+ QCOMPARE(l.combinations(), QStringList() << "en_US" << "en");
+}
+
+void LocaleTest::en_USVariant()
+{
+ Locale l(QStringLiteral("en_US@foo"));
+ QCOMPARE(l.m_language, QStringLiteral("en"));
+ QCOMPARE(l.m_country, QStringLiteral("US"));
+ QCOMPARE(l.m_variant, QStringLiteral("foo"));
+ QCOMPARE(l.m_encoding, QString());
+ QCOMPARE(l.combinations(), QStringList() << "en_US@foo" << "en@foo" << "en_US" << "en");
+}
+
+void LocaleTest::en_USVariantEncoding()
+{
+ Locale l(QStringLiteral("en_US@foo.UTF-8"));
+ QCOMPARE(l.m_language, QStringLiteral("en"));
+ QCOMPARE(l.m_country, QStringLiteral("US"));
+ QCOMPARE(l.m_variant, QStringLiteral("foo"));
+ QCOMPARE(l.m_encoding, QStringLiteral("UTF-8"));
+ QCOMPARE(l.combinations(), QStringList() << "en_US@foo.UTF-8" << "en_US@foo" << "en@foo.UTF-8" << "en@foo" << "en_US.UTF-8" << "en_US" << "en.UTF-8" << "en");
+}
+
+void LocaleTest::en_USEncoding()
+{
+ Locale l(QStringLiteral("en_US.UTF-8"));
+ QCOMPARE(l.m_language, QStringLiteral("en"));
+ QCOMPARE(l.m_country, QStringLiteral("US"));
+ QCOMPARE(l.m_variant, QString());
+ QCOMPARE(l.m_encoding, QStringLiteral("UTF-8"));
+ QCOMPARE(l.combinations(), QStringList() << "en_US.UTF-8" << "en_US" << "en.UTF-8" << "en");
+}
+
+QTEST_GUILESS_MAIN(LocaleTest);
+
+#include "localetest.moc"
diff --git a/src/daemon/CMakeLists.txt b/src/daemon/CMakeLists.txt
index 6ffb9d8..aaae7c1 100644
--- a/src/daemon/CMakeLists.txt
+++ b/src/daemon/CMakeLists.txt
@@ -6,6 +6,7 @@ set(notificationhelper_SRCS
hookevent/hookevent.cpp
hookevent/hookgui.cpp
hookevent/hook.cpp
+ hookevent/locale.cpp
installevent/installdbuswatcher.cpp
installevent/installevent.cpp
installevent/installgui.cpp
diff --git a/src/daemon/hookevent/hook.cpp b/src/daemon/hookevent/hook.cpp
index 79167f0..98bda5c 100644
--- a/src/daemon/hookevent/hook.cpp
+++ b/src/daemon/hookevent/hook.cpp
@@ -1,7 +1,7 @@
/***************************************************************************
* Copyright © 2009 Jonathan Thomas <echidnaman@kubuntu.org> *
* Copyright © 2009 Amichai Rothman <amichai2@amichais.net> *
- * Copyright © 2014 Harald Sitter <apachelogger@kubuntu.org> *
+ * Copyright © 2014-2015 Harald Sitter <sitter@kde.org> *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License as *
@@ -36,6 +36,8 @@
#include <KConfig>
#include <KConfigGroup>
+#include "locale.h"
+
float getUptime()
{
float uptime = 0;
@@ -64,6 +66,7 @@ Hook::Hook(QObject *parent, const QString &hookPath)
: QObject(parent)
, m_hookPath(hookPath)
, m_finished(false)
+ , m_locale(QLatin1String(setlocale(LC_ALL, NULL)))
{
m_fields = parse(hookPath);
loadConfig();
@@ -72,19 +75,31 @@ Hook::Hook(QObject *parent, const QString &hookPath)
Hook::~Hook()
{}
+QString Hook::locale()
+{
+ return m_locale;
+}
+
+void Hook::setLocale(const QString &locale)
+{
+ m_locale = locale;
+}
+
QString Hook::getField(const QString &name) const
{
// Try to lookup the field with -LOCALE appended, then -LANGUAGE then without
// suffix.
- QString locale = setlocale(LC_ALL, NULL);
- locale = locale.section(QChar('.'), 0, -2); // Ditch encoding, not used.
- QString value = getField(name % QChar('-') % locale);
- if (value.isEmpty()) {
- locale = locale.section(QChar('_'), 0, -2); // Ditch country.
- value = getField(name % QChar('-') % locale);
+ Locale locale(m_locale);
+ QString prefix = name % QChar('-');
+ QString value;
+ for (QString locale_combo: locale.combinations()) {
+ value = m_fields.value(prefix + locale_combo);
+ if (!value.isEmpty()) {
+ break;
+ }
}
if (value.isEmpty()) {
- value = getField(name);
+ value = m_fields.value(name);
}
return value;
}
diff --git a/src/daemon/hookevent/hook.h b/src/daemon/hookevent/hook.h
index ee9bae4..698d077 100644
--- a/src/daemon/hookevent/hook.h
+++ b/src/daemon/hookevent/hook.h
@@ -1,6 +1,6 @@
/***************************************************************************
* Copyright © 2009 Jonathan Thomas <echidnaman@kubuntu.org> *
- * Copyright © 2009 Harald Sitter <apachelogger@ubuntu.com> *
+ * Copyright © 2014-2015 Harald Sitter <sitter@kde.org> *
* Copyright © 2009 Amichai Rothman <amichai2@amichais.net> *
* *
* This program is free software; you can redistribute it and/or *
@@ -25,16 +25,21 @@
#include <QObject>
#include <QString>
+#include <QStringList>
#include <QMap>
class Hook : public QObject
{
Q_OBJECT
public:
+ // FIXME: standard argument order is (stuff, QObject=nullptr)
Hook(QObject* parent, const QString &hookPath);
virtual ~Hook();
+ QString locale();
+ void setLocale(const QString &locale);
+
public Q_SLOTS:
bool isValid() const;
bool isNotificationRequired() const;
@@ -46,6 +51,7 @@ private:
QString m_hookPath;
QMap<QString, QString> m_fields;
bool m_finished;
+ QString m_locale;
private Q_SLOTS:
QMap<QString, QString> parse(const QString &hookPath);
diff --git a/src/daemon/hookevent/locale.cpp b/src/daemon/hookevent/locale.cpp
new file mode 100644
index 0000000..73144a2
--- /dev/null
+++ b/src/daemon/hookevent/locale.cpp
@@ -0,0 +1,115 @@
+/***************************************************************************
+ * Copyright © 2015 Harald Sitter <sitter@kde.org> *
+ * *
+ * This program is free software; you can redistribute it and/or *
+ * modify it under the terms of the GNU General Public License as *
+ * published by the Free Software Foundation; either version 2 of *
+ * the License or (at your option) version 3 or any later version *
+ * accepted by the membership of KDE e.V. (or its successor approved *
+ * by the membership of KDE e.V.), which shall act as a proxy *
+ * defined in Section 14 of version 3 of the license. *
+ * *
+ * This program is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY 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/>. *
+ ***************************************************************************/
+
+#include "locale.h"
+
+#include <algorithm>
+
+Locale::Locale(const QString &locale)
+ : m_language(QString())
+ , m_country(QString())
+ , m_variant(QString())
+ , m_encoding(QString())
+{
+ QString tmp = locale;
+ QStringList parts;
+
+ parts = tmp.split(QChar('.'));
+ if (parts.size() > 1) {
+ m_encoding = parts.last();
+ }
+ tmp = parts.first();
+
+ parts = tmp.split(QChar('@'));
+ if (parts.size() > 1) {
+ m_variant = parts.last();
+ }
+ tmp = parts.first();
+
+ parts = tmp.split(QChar('_'));
+ if (parts.size() > 1) {
+ m_country = parts.last();
+ }
+ m_language = parts.first();
+}
+
+// This function returns a combination of possible locale values.
+// The way this works is that it fills a list with all possibly combination on
+// top of language. All combiners can return a noop version of the string
+// passed into the combiner if the combinee is empty. This ultimately results
+// in duplicated entries (e.g. if variant is empty the list would contain
+// language and langauge.encoding twice as their respective variantified version
+// comes back without variant).
+// Once all combinations are attempted the by-design-duplicates are removed
+// and we have a nice clean list of language combinations.
+// This has slight overhead since we are partially working with duplicated
+// strings at times, it does however need substantially less code and less
+// iffing.
+QStringList Locale::combinations()
+{
+ QStringList list;
+ // language_country@variant.encoding
+ // language_country@variant
+ // language@variant.encoding
+ // language@variant
+ // language_country.encoding
+ // language_country
+ // language.encoding
+ // language
+ // ^ all built in reverse order for improved reasability
+ QString tmp;
+ if (!m_language.isEmpty()) {
+ list << (tmp = m_language);
+ list << encodify(tmp);
+
+ list << (tmp = countryfy(m_language));
+ list << encodify(tmp);
+
+ list << (tmp = variantify(m_language));
+ list << encodify(tmp);
+
+ list << (tmp = variantify(countryfy(m_language)));
+ list << encodify(tmp);
+ }
+ list.removeDuplicates();
+ std::reverse(std::begin(list), std::end(list));
+ return list;
+}
+
+QString Locale::countryfy(const QString &str)
+{
+ if (m_country.isEmpty())
+ return str;
+ return str + '_' + m_country;
+}
+
+QString Locale::variantify(const QString &str)
+{
+ if (m_variant.isEmpty())
+ return str;
+ return str + '@' + m_variant;
+}
+
+QString Locale::encodify(const QString &str)
+{
+ if (m_encoding.isEmpty())
+ return str;
+ return str + '.' + m_encoding;
+}
diff --git a/src/daemon/hookevent/locale.h b/src/daemon/hookevent/locale.h
new file mode 100644
index 0000000..547d1e2
--- /dev/null
+++ b/src/daemon/hookevent/locale.h
@@ -0,0 +1,52 @@
+/***************************************************************************
+ * Copyright © 2015 Harald Sitter <sitter@kde.org> *
+ * *
+ * This program is free software; you can redistribute it and/or *
+ * modify it under the terms of the GNU General Public License as *
+ * published by the Free Software Foundation; either version 2 of *
+ * the License or (at your option) version 3 or any later version *
+ * accepted by the membership of KDE e.V. (or its successor approved *
+ * by the membership of KDE e.V.), which shall act as a proxy *
+ * defined in Section 14 of version 3 of the license. *
+ * *
+ * This program is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY 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/>. *
+ ***************************************************************************/
+
+#ifndef LOCALE_H
+#define LOCALE_H
+
+#include <QStringList>
+
+/**
+ * @brief Helper class dealing with hook locale strings
+ * Hook locale strings are somewhat random nonesense (e.g. they can be
+ * language.encoding even though the string encoding always is utf-8 anyway).
+ * So, this class allows building all combinations off of a locale that could
+ * potentially be used for the locale identifier in a hook.
+ * This list is ordered by preference.
+ */
+class Locale
+{
+public:
+ Locale(const QString &locale);
+
+ QStringList combinations();
+
+private:
+ QString countryfy(const QString &str);
+ QString variantify(const QString &str);
+ QString encodify(const QString &str);
+
+ QString m_language;
+ QString m_country;
+ QString m_variant;
+ QString m_encoding;
+};
+
+#endif // LOCALE_H
|