1 /* Copyright (C) 2006 - 2014 Jan Kundrát <jkt@flaska.net>
2    Copyright (C) 2014 Thomas Lübking <thomas.luebking@gmail.com>
3 
4    This file is part of the Trojita Qt IMAP e-mail client,
5    http://trojita.flaska.net/
6 
7    This program is free software; you can redistribute it and/or
8    modify it under the terms of the GNU General Public License as
9    published by the Free Software Foundation; either version 2 of
10    the License or (at your option) version 3 or any later version
11    accepted by the membership of KDE e.V. (or its successor approved
12    by the membership of KDE e.V.), which shall act as a proxy
13    defined in Section 14 of version 3 of the license.
14 
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19 
20    You should have received a copy of the GNU General Public License
21    along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 */
23 
24 #include "TaskProgressIndicator.h"
25 #include <QApplication>
26 #include <QMouseEvent>
27 #include <QTimer>
28 #include "Gui/Spinner.h"
29 #include "Imap/Model/Model.h"
30 #include "Imap/Model/VisibleTasksModel.h"
31 
32 namespace Gui
33 {
34 
TaskProgressIndicator(QWidget * parent)35 TaskProgressIndicator::TaskProgressIndicator(QWidget *parent) :
36     Spinner(parent), m_busyCursorTrigger(new QTimer(this)), m_busy(false)
37 {
38     connect(m_busyCursorTrigger, &QTimer::timeout, []() {
39         QApplication::setOverrideCursor(Qt::BusyCursor);
40     });
41     m_busyCursorTrigger->setSingleShot(true);
42     m_busyCursorTrigger->setInterval(666);
43 
44     setType(Spinner::Elastic);
45     setContext(Spinner::Throbber);
46 }
47 
48 /** @short Connect to the specified IMAP model as the source of the activity information */
setImapModel(Imap::Mailbox::Model * model)49 void TaskProgressIndicator::setImapModel(Imap::Mailbox::Model *model)
50 {
51     if (model) {
52         m_visibleTasksModel = new Imap::Mailbox::VisibleTasksModel(model, model->taskModel());
53         connect(m_visibleTasksModel.data(), &QAbstractItemModel::layoutChanged, this, &TaskProgressIndicator::updateActivityIndication);
54         connect(m_visibleTasksModel.data(), &QAbstractItemModel::modelReset, this, &TaskProgressIndicator::updateActivityIndication);
55         connect(m_visibleTasksModel.data(), &QAbstractItemModel::rowsInserted, this, &TaskProgressIndicator::updateActivityIndication);
56         connect(m_visibleTasksModel.data(), &QAbstractItemModel::rowsRemoved, this, &TaskProgressIndicator::updateActivityIndication);
57     }
58 }
59 
60 /** @short Reflect a possible change in the activity in the GUI */
updateActivityIndication()61 void TaskProgressIndicator::updateActivityIndication()
62 {
63     if (!m_visibleTasksModel)
64         return;
65 
66     bool busy = m_visibleTasksModel->hasChildren();
67     if (!m_busy && busy) {
68         start();
69         if (!m_busyCursorTrigger->isActive()) {
70             // do NOT restart, we don't want to prolong this delay
71             m_busyCursorTrigger->start();
72         }
73     } else if (m_busy && !busy) {
74         stop();
75         if (!m_busyCursorTrigger->isActive()) {
76             // don't restore unless we've alredy set it
77             QApplication::restoreOverrideCursor();
78         }
79         // don't cause future timeout
80         m_busyCursorTrigger->stop();
81     }
82 
83     if (busy) {
84         setToolTip(tr("%n ongoing actions", 0, m_visibleTasksModel->rowCount()));
85     } else {
86         setToolTip(tr("IMAP connection idle"));
87     }
88 
89     m_busy = busy;
90 }
91 
92 }
93