2f8736c1ec064a9725980aa55c5c373bbbcf75ff
[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 "client.h"
24 #include "bufferwidget.h"
25
26 CoreInfoDlg::CoreInfoDlg(QWidget *parent) : QDialog(parent) {
27     ui.setupUi(this);
28     CoreInfo *coreInfo = Client::coreInfo();
29     connect(coreInfo, SIGNAL(coreDataChanged(const QVariantMap &)), this, SLOT(coreInfoChanged(const QVariantMap &)));
30
31     coreInfoChanged(coreInfo->coreData());
32
33     updateUptime();
34     startTimer(1000);
35 }
36
37
38 void CoreInfoDlg::coreInfoChanged(const QVariantMap &coreInfo) {
39     ui.labelCoreVersion->setText(coreInfo["quasselVersion"].toString());
40     ui.labelCoreVersionDate->setText(coreInfo["quasselBuildDate"].toString()); // "BuildDate" for compatibility
41     ui.labelClientCount->setNum(coreInfo["sessionConnectedClients"].toInt());
42
43     auto coreSessionSupported = false;
44     auto ids = _widgets.keys();
45     for (const auto &peerData : coreInfo["sessionConnectedClientData"].toList()) {
46         coreSessionSupported = true;
47
48         auto peerMap = peerData.toMap();
49         int peerId = peerMap["id"].toInt();
50
51         ids.removeAll(peerId);
52
53         bool isNew = false;
54         CoreSessionWidget *coreSessionWidget = _widgets[peerId];
55         if (coreSessionWidget == nullptr) {
56             coreSessionWidget = new CoreSessionWidget(ui.coreSessionScrollContainer);
57             isNew = true;
58         }
59         coreSessionWidget->setData(peerMap);
60         if (isNew) {
61             _widgets[peerId] = coreSessionWidget;
62             // Add this to the end of the session list, but before the default layout stretch item.
63             // The layout stretch item should never be removed, so count should always be >= 1.
64             ui.coreSessionContainer->insertWidget(ui.coreSessionContainer->count() - 1,
65                                                   coreSessionWidget, 0, Qt::AlignTop);
66             connect(coreSessionWidget, SIGNAL(disconnectClicked(int)), this, SLOT(disconnectClicked(int)));
67         }
68     }
69
70     for (const auto &key : ids) {
71         delete _widgets[key];
72         _widgets.remove(key);
73     }
74
75     ui.coreSessionScrollArea->setVisible(coreSessionSupported);
76 }
77
78
79 void CoreInfoDlg::updateUptime() {
80     CoreInfo *coreInfo = Client::coreInfo();
81     if (coreInfo) {
82         QDateTime startTime = coreInfo->at("startTime").toDateTime();
83
84         int64_t uptime = startTime.secsTo(QDateTime::currentDateTime().toUTC());
85         int64_t updays = uptime / 86400;
86         uptime %= 86400;
87         int64_t uphours = uptime / 3600;
88         uptime %= 3600;
89         int64_t upmins = uptime / 60;
90         uptime %= 60;
91
92         QString uptimeText = tr("%n Day(s)", "", updays) +
93                              tr(" %1:%2:%3 (since %4)")
94                                      .arg(uphours, 2, 10, QChar('0'))
95                                      .arg(upmins, 2, 10, QChar('0'))
96                                      .arg(uptime, 2, 10, QChar('0'))
97                                      .arg(startTime.toLocalTime().toString(Qt::TextDate));
98         ui.labelUptime->setText(uptimeText);
99     }
100 }
101
102 void CoreInfoDlg::disconnectClicked(int peerId) {
103     Client::kickClient(peerId);
104 }