forked from linuxdeepin/dde-shell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotifyitem.cpp
More file actions
230 lines (193 loc) · 4.89 KB
/
notifyitem.cpp
File metadata and controls
230 lines (193 loc) · 4.89 KB
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
// SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later
#include "notifyitem.h"
#include <QDateTime>
#include <QLoggingCategory>
#include "notifyaccessor.h"
namespace notification {
Q_DECLARE_LOGGING_CATEGORY(notifyLog)
}
namespace notifycenter {
AppNotifyItem::AppNotifyItem(const NotifyEntity &entity)
: m_appId(entity.appName())
{
setEntity(entity);
auto pin = NotifyAccessor::instance()->applicationPin(m_entity.appName());
setPinned(pin);
}
void AppNotifyItem::setEntity(const NotifyEntity &entity)
{
m_entity = entity;
refresh();
}
NotifyEntity AppNotifyItem::entity() const
{
Q_ASSERT(m_entity.isValid());
return m_entity;
}
NotifyType AppNotifyItem::type() const
{
return NotifyType::Normal;
}
QString AppNotifyItem::appId() const
{
return m_appId;
}
QString AppNotifyItem::appName() const
{
Q_ASSERT(m_entity.isValid());
return m_entity.appName();
}
qint64 AppNotifyItem::id() const
{
Q_ASSERT(m_entity.isValid());
return m_entity.id();
}
QString AppNotifyItem::time() const
{
return m_time;
}
void AppNotifyItem::updateTime()
{
QDateTime time = QDateTime::fromMSecsSinceEpoch(m_entity.cTime());
if (!time.isValid())
return;
QString ret;
QDateTime currentTime = QDateTime::currentDateTime();
auto elapsedDay = time.daysTo(currentTime);
if (elapsedDay == 0) {
qint64 msec = QDateTime::currentMSecsSinceEpoch() - m_entity.cTime();
auto minute = msec / 1000 / 60;
if (minute <= 0) {
ret = tr("Just now");
} else if (minute > 0 && minute < 60) {
ret = tr("%1 minutes ago").arg(minute);
} else {
ret = tr("%1 hours ago").arg(minute / 60);
}
} else if (elapsedDay >= 1 && elapsedDay < 2) {
ret = tr("Yesterday ") + " " + time.toString("hh:mm");
} else if (elapsedDay >= 2 && elapsedDay < 7) {
ret = time.toString("ddd hh:mm");
} else {
ret = time.toString("yyyy/MM/dd");
}
m_time = ret;
}
bool AppNotifyItem::strongInteractive() const
{
return m_strongInteractive;
}
QString AppNotifyItem::contentIcon() const
{
Q_ASSERT(m_entity.isValid());
const auto path = m_entity.bodyIcon();
return path;
}
QString AppNotifyItem::defaultAction() const
{
return m_defaultAction;
}
QVariantList AppNotifyItem::actions() const
{
return m_actions;
}
void AppNotifyItem::updateActions()
{
QStringList actions = m_entity.actions();
if (actions.contains(QLatin1String("default"))) {
actions.removeAll(QLatin1String("default"));
m_defaultAction = QLatin1String("default");
}
Q_ASSERT(actions.size() % 2 != 1);
if (actions.size() % 2 == 1) {
qWarning(notifyLog) << "Actions must be an even number except for default, The notify appName:" << m_entity.appName()
<< ", actions:" << m_entity.actions();
return;
}
QVariantList array;
for (int i = 0; i < actions.size(); i += 2) {
const auto id = actions[i];
const auto text = actions[i + 1];
QVariantMap item;
item["id"] = id;
item["text"] = text;
array.append(item);
}
m_actions = array;
}
void AppNotifyItem::updateStrongInteractive()
{
QMap<QString, QVariant> hints = m_entity.hints();
if (hints.isEmpty())
return;
bool ret = false;
QMap<QString, QVariant>::const_iterator i = hints.constBegin();
while (i != hints.constEnd()) {
if (i.key() == QLatin1String("urgency")) {
ret = i.value().toString() == QLatin1String("SOH");
break;
}
++i;
}
m_strongInteractive = ret;
}
void AppNotifyItem::refresh()
{
updateTime();
updateActions();
updateStrongInteractive();
}
bool AppNotifyItem::pinned() const
{
return m_pinned;
}
void AppNotifyItem::setPinned(bool newPinned)
{
m_pinned = newPinned;
}
OverlapAppNotifyItem::OverlapAppNotifyItem(const NotifyEntity &entity)
: AppNotifyItem(entity)
{
}
NotifyType OverlapAppNotifyItem::type()const
{
return NotifyType::Overlap;
}
void OverlapAppNotifyItem::updateCount(int source)
{
int count = source - 1;
if (count > FullCount) {
m_count = FullCount;
} else if (count <= EmptyCount) {
m_count = EmptyCount;
} else {
m_count = count;
}
}
int OverlapAppNotifyItem::count() const
{
return m_count;
}
bool OverlapAppNotifyItem::isEmpty() const
{
return m_count <= EmptyCount;
}
AppGroupNotifyItem::AppGroupNotifyItem(const QString &appName)
: AppNotifyItem(NotifyEntity(std::numeric_limits<qint64>().max(), appName))
{
}
NotifyType AppGroupNotifyItem::type() const
{
return NotifyType::Group;
}
void AppGroupNotifyItem::updateLastEntity(const NotifyEntity &entity)
{
m_lastEntity = entity;
}
NotifyEntity AppGroupNotifyItem::lastEntity() const
{
return m_lastEntity;
}
}