/
androidprioritystore.cpp
92 lines (81 loc) · 3.25 KB
/
androidprioritystore.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
/***************************************************************************
**
** Copyright (c) 2015 Jolla Ltd.
**
** This file is part of lipstick.
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation
** and appearing in the file LICENSE.LGPL included in the packaging
** of this file.
**
****************************************************************************/
#include "androidprioritystore.h"
#include <QFile>
#include <QtDebug>
namespace {
const int InteractiveAndroidPriority = 120;
const int ElevatedAndroidPriority = 100;
const int StandardAndroidPriority = 50;
AndroidPriorityStore::PriorityDetails detailsForFeedback(const QString &feedback)
{
if (!feedback.isEmpty()) {
QSet<QString> tokens(feedback.split(",", QString::SkipEmptyParts).toSet());
// If the only feedback tokens are for email, this is a medium-priority notification
tokens.remove(QStringLiteral("email"));
tokens.remove(QStringLiteral("email_exists"));
if (!tokens.isEmpty()) {
// This is a top-priority notification
return qMakePair(InteractiveAndroidPriority, feedback);
}
}
return qMakePair(ElevatedAndroidPriority, feedback);
}
}
AndroidPriorityStore::AndroidPriorityStore(const QString &path, QObject *parent)
: QObject(parent)
{
QFile definitionFile(path);
if (definitionFile.exists()) {
if (definitionFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
while (!definitionFile.atEnd()) {
const QByteArray lineChars(definitionFile.readLine());
const QString line(QString::fromUtf8(lineChars.left(lineChars.length() - 1)));
if (!line.isEmpty()) {
QString appName;
QString feedback;
int index = line.indexOf(QLatin1Char(';'));
if (index > 0) {
appName = line.left(index);
feedback = line.mid(index + 1);
} else {
appName = line;
}
m_priorityDefinitions.insert(appName, feedback);
}
}
} else {
qWarning() << Q_FUNC_INFO << "Unable to open file for reading:" << path;
}
} else {
qWarning() << Q_FUNC_INFO << "No priority definition file exists:" << path;
}
}
AndroidPriorityStore::PriorityDetails AndroidPriorityStore::appDetails(const QString &appName) const
{
QHash<QString, QString>::const_iterator it = m_priorityDefinitions.constFind(appName);
if (it != m_priorityDefinitions.constEnd()) {
return detailsForFeedback(it.value());
}
return qMakePair(StandardAndroidPriority, QString());
}
AndroidPriorityStore::PriorityDetails AndroidPriorityStore::packageDetails(const QString &packageName) const
{
const QString key(QStringLiteral("package:") + packageName);
QHash<QString, QString>::const_iterator it = m_priorityDefinitions.constFind(key);
if (it != m_priorityDefinitions.constEnd()) {
return detailsForFeedback(it.value());
}
return qMakePair(StandardAndroidPriority, QString());
}