forked from owncloud/client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotificationwidget.cpp
More file actions
147 lines (122 loc) · 4.88 KB
/
Copy pathnotificationwidget.cpp
File metadata and controls
147 lines (122 loc) · 4.88 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
/*
* Copyright (C) by Klaas Freitag <freitag@owncloud.com>
*
* 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; version 2 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.
*/
#include "notificationwidget.h"
#include "QProgressIndicator.h"
#include "utility.h"
#include <QPushButton>
#include "ocsjob.h"
namespace OCC {
NotificationWidget::NotificationWidget(QWidget *parent) : QWidget(parent)
{
_ui.setupUi(this);
_progressIndi = new QProgressIndicator(this);
_ui.horizontalLayout->addWidget(_progressIndi);
}
void NotificationWidget::setActivity(const Activity& activity)
{
_myActivity = activity;
Q_ASSERT( !activity._accName.isEmpty() );
_accountName = activity._accName;
// _ui._headerLabel->setText( );
_ui._subjectLabel->setVisible( !activity._subject.isEmpty() );
_ui._messageLabel->setVisible( !activity._message.isEmpty() );
_ui._subjectLabel->setText(activity._subject);
_ui._messageLabel->setText(activity._message);
_ui._notifIcon->setPixmap(QPixmap(":/client/resources/bell.png"));
_ui._notifIcon->setMinimumWidth(64);
_ui._notifIcon->setMinimumHeight(64);
_ui._notifIcon->show();
QString tText = tr("Created at %1").arg(Utility::timeAgoInWords(activity._dateTime));
_ui._timeLabel->setText(tText);
// always remove the buttons
foreach( auto button, _ui._buttonBox->buttons() ) {
_ui._buttonBox->removeButton(button);
}
_buttons.clear();
// display buttons for the links
if( activity._links.isEmpty() ) {
// in case there is no action defined, do a close button.
QPushButton *b = _ui._buttonBox->addButton( QDialogButtonBox::Close );
b->setDefault(true);
connect(b, SIGNAL(clicked()), this, SLOT(slotButtonClicked()));
_buttons.append(b);
} else {
foreach( auto link, activity._links ) {
QPushButton *b = _ui._buttonBox->addButton(link._label, QDialogButtonBox::AcceptRole);
b->setDefault(link._isPrimary);
connect(b, SIGNAL(clicked()), this, SLOT(slotButtonClicked()));
_buttons.append(b);
}
}
}
Activity NotificationWidget::activity() const
{
return _myActivity;
}
void NotificationWidget::slotButtonClicked()
{
QObject *buttonWidget = QObject::sender();
int index = -1;
if( buttonWidget ) {
// find the button that was clicked, it has to be in the list
// of buttons that were added to the button box before.
for( int i = 0; i < _buttons.count(); i++ ) {
if( _buttons.at(i) == buttonWidget ) {
index = i;
}
_buttons.at(i)->setEnabled(false);
}
// if the button was found, the link must be called
if( index > -1 && _myActivity._links.count() == 0 ) {
// no links, that means it was the close button
// empty link. Just close and remove the widget.
QString doneText = tr("Closing in a few seconds...");
_ui._timeLabel->setText(doneText);
emit requestCleanupAndBlacklist(_myActivity);
return;
}
if( index > -1 && index < _myActivity._links.count() ) {
ActivityLink triggeredLink = _myActivity._links.at(index);
_actionLabel = triggeredLink._label;
if( ! triggeredLink._link.isEmpty() ) {
qDebug() << Q_FUNC_INFO << "Notification Link: "<< triggeredLink._verb << triggeredLink._link;
_progressIndi->startAnimation();
emit sendNotificationRequest( _accountName, triggeredLink._link, triggeredLink._verb );
}
}
}
}
void NotificationWidget::slotNotificationRequestFinished(int statusCode)
{
int i = 0;
QString doneText;
QLocale locale;
QString timeStr = locale.toString(QTime::currentTime());
// the ocs API returns stat code 100 if it succeeded.
if( statusCode != OCS_SUCCESS_STATUS_CODE ) {
qDebug() << Q_FUNC_INFO << "Notification Request to Server failed, leave button visible.";
for( i = 0; i < _buttons.count(); i++ ) {
_buttons.at(i)->setEnabled(true);
}
//: The second parameter is a time, such as 'failed at 09:58pm'
doneText = tr("%1 request failed at %2").arg(_actionLabel, timeStr);
} else {
// the call to the ocs API succeeded.
_ui._buttonBox->hide();
//: The second parameter is a time, such as 'selected at 09:58pm'
doneText = tr("'%1' selected at %2").arg(_actionLabel, timeStr);
}
_ui._timeLabel->setText( doneText );
_progressIndi->stopAnimation();
}
}