icons: Warn on missing icons
[quassel.git] / src / qtui / coreinfodlg.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2018 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) version 3.                                           *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20
21 #include "coreinfodlg.h"
22
23 #include <QMessageBox>
24
25 #include "bufferwidget.h"
26 #include "client.h"
27 #include "icon.h"
28
29 CoreInfoDlg::CoreInfoDlg(QWidget *parent) : QDialog(parent) {
30     ui.setupUi(this);
31     CoreInfo *coreInfo = Client::coreInfo();
32     connect(coreInfo, SIGNAL(coreDataChanged(const QVariantMap &)), this, SLOT(coreInfoChanged(const QVariantMap &)));
33
34     coreInfoChanged(coreInfo->coreData());
35
36     // Warning icon
37     ui.coreUnsupportedIcon->setPixmap(icon::get("dialog-warning").pixmap(16));
38
39     updateUptime();
40     startTimer(1000);
41 }
42
43
44 void CoreInfoDlg::coreInfoChanged(const QVariantMap &coreInfo) {
45     ui.labelCoreVersion->setText(coreInfo["quasselVersion"].toString());
46     ui.labelCoreVersionDate->setText(coreInfo["quasselBuildDate"].toString()); // "BuildDate" for compatibility
47     ui.labelClientCount->setNum(coreInfo["sessionConnectedClients"].toInt());
48
49     auto coreSessionSupported = false;
50     auto ids = _widgets.keys();
51     for (const auto &peerData : coreInfo["sessionConnectedClientData"].toList()) {
52         coreSessionSupported = true;
53
54         auto peerMap = peerData.toMap();
55         int peerId = peerMap["id"].toInt();
56
57         ids.removeAll(peerId);
58
59         bool isNew = false;
60         CoreSessionWidget *coreSessionWidget = _widgets[peerId];
61         if (coreSessionWidget == nullptr) {
62             coreSessionWidget = new CoreSessionWidget(ui.coreSessionScrollContainer);
63             isNew = true;
64         }
65         coreSessionWidget->setData(peerMap);
66         if (isNew) {
67             _widgets[peerId] = coreSessionWidget;
68             // Add this to the end of the session list, but before the default layout stretch item.
69             // The layout stretch item should never be removed, so count should always be >= 1.
70             ui.coreSessionContainer->insertWidget(ui.coreSessionContainer->count() - 1,
71                                                   coreSessionWidget, 0, Qt::AlignTop);
72             connect(coreSessionWidget, SIGNAL(disconnectClicked(int)), this, SLOT(disconnectClicked(int)));
73         }
74     }
75
76     for (const auto &key : ids) {
77         delete _widgets[key];
78         _widgets.remove(key);
79     }
80
81     ui.coreSessionScrollArea->setVisible(coreSessionSupported);
82
83     // Hide the information bar when core sessions are supported
84     ui.coreUnsupportedWidget->setVisible(!coreSessionSupported);
85 }
86
87
88 void CoreInfoDlg::updateUptime() {
89     CoreInfo *coreInfo = Client::coreInfo();
90     if (coreInfo) {
91         QDateTime startTime = coreInfo->at("startTime").toDateTime();
92
93         int64_t uptime = startTime.secsTo(QDateTime::currentDateTime().toUTC());
94         int64_t updays = uptime / 86400;
95         uptime %= 86400;
96         int64_t uphours = uptime / 3600;
97         uptime %= 3600;
98         int64_t upmins = uptime / 60;
99         uptime %= 60;
100
101         QString uptimeText = tr("%n Day(s)", "", updays) +
102                              tr(" %1:%2:%3 (since %4)")
103                                      .arg(uphours, 2, 10, QChar('0'))
104                                      .arg(upmins, 2, 10, QChar('0'))
105                                      .arg(uptime, 2, 10, QChar('0'))
106                                      .arg(startTime.toLocalTime().toString(Qt::TextDate));
107         ui.labelUptime->setText(uptimeText);
108     }
109 }
110
111 void CoreInfoDlg::disconnectClicked(int peerId) {
112     Client::kickClient(peerId);
113 }
114
115 void CoreInfoDlg::on_coreUnsupportedDetails_clicked()
116 {
117     QMessageBox::warning(this,
118                          tr("Active sessions unsupported"),
119                          QString("<p><b>%1</b></p></br><p>%2</p>"
120                                  ).arg(tr("Your Quassel core is too old to show active sessions"),
121                                        tr("You need a Quassel core v0.13.0 or newer to view and "
122                                           "disconnect other connected clients.")));
123 }