qtui: Hide "fallback" and "Override" if no system theme is configured
[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 <QMessageBox>
22
23 #include "coreinfodlg.h"
24
25 #include "client.h"
26 #include "bufferwidget.h"
27
28 CoreInfoDlg::CoreInfoDlg(QWidget *parent) : QDialog(parent) {
29     ui.setupUi(this);
30     CoreInfo *coreInfo = Client::coreInfo();
31     connect(coreInfo, SIGNAL(coreDataChanged(const QVariantMap &)), this, SLOT(coreInfoChanged(const QVariantMap &)));
32
33     coreInfoChanged(coreInfo->coreData());
34
35     // Warning icon
36     ui.coreUnsupportedIcon->setPixmap(QIcon::fromTheme("dialog-warning").pixmap(16));
37
38     updateUptime();
39     startTimer(1000);
40 }
41
42
43 void CoreInfoDlg::coreInfoChanged(const QVariantMap &coreInfo) {
44     ui.labelCoreVersion->setText(coreInfo["quasselVersion"].toString());
45     ui.labelCoreVersionDate->setText(coreInfo["quasselBuildDate"].toString()); // "BuildDate" for compatibility
46     ui.labelClientCount->setNum(coreInfo["sessionConnectedClients"].toInt());
47
48     auto coreSessionSupported = false;
49     auto ids = _widgets.keys();
50     for (const auto &peerData : coreInfo["sessionConnectedClientData"].toList()) {
51         coreSessionSupported = true;
52
53         auto peerMap = peerData.toMap();
54         int peerId = peerMap["id"].toInt();
55
56         ids.removeAll(peerId);
57
58         bool isNew = false;
59         CoreSessionWidget *coreSessionWidget = _widgets[peerId];
60         if (coreSessionWidget == nullptr) {
61             coreSessionWidget = new CoreSessionWidget(ui.coreSessionScrollContainer);
62             isNew = true;
63         }
64         coreSessionWidget->setData(peerMap);
65         if (isNew) {
66             _widgets[peerId] = coreSessionWidget;
67             // Add this to the end of the session list, but before the default layout stretch item.
68             // The layout stretch item should never be removed, so count should always be >= 1.
69             ui.coreSessionContainer->insertWidget(ui.coreSessionContainer->count() - 1,
70                                                   coreSessionWidget, 0, Qt::AlignTop);
71             connect(coreSessionWidget, SIGNAL(disconnectClicked(int)), this, SLOT(disconnectClicked(int)));
72         }
73     }
74
75     for (const auto &key : ids) {
76         delete _widgets[key];
77         _widgets.remove(key);
78     }
79
80     ui.coreSessionScrollArea->setVisible(coreSessionSupported);
81
82     // Hide the information bar when core sessions are supported
83     ui.coreUnsupportedWidget->setVisible(!coreSessionSupported);
84 }
85
86
87 void CoreInfoDlg::updateUptime() {
88     CoreInfo *coreInfo = Client::coreInfo();
89     if (coreInfo) {
90         QDateTime startTime = coreInfo->at("startTime").toDateTime();
91
92         int64_t uptime = startTime.secsTo(QDateTime::currentDateTime().toUTC());
93         int64_t updays = uptime / 86400;
94         uptime %= 86400;
95         int64_t uphours = uptime / 3600;
96         uptime %= 3600;
97         int64_t upmins = uptime / 60;
98         uptime %= 60;
99
100         QString uptimeText = tr("%n Day(s)", "", updays) +
101                              tr(" %1:%2:%3 (since %4)")
102                                      .arg(uphours, 2, 10, QChar('0'))
103                                      .arg(upmins, 2, 10, QChar('0'))
104                                      .arg(uptime, 2, 10, QChar('0'))
105                                      .arg(startTime.toLocalTime().toString(Qt::TextDate));
106         ui.labelUptime->setText(uptimeText);
107     }
108 }
109
110 void CoreInfoDlg::disconnectClicked(int peerId) {
111     Client::kickClient(peerId);
112 }
113
114 void CoreInfoDlg::on_coreUnsupportedDetails_clicked()
115 {
116     QMessageBox::warning(this,
117                          tr("Active sessions unsupported"),
118                          QString("<p><b>%1</b></p></br><p>%2</p>"
119                                  ).arg(tr("Your Quassel core is too old to show active sessions"),
120                                        tr("You need a Quassel core v0.13.0 or newer to view and "
121                                           "disconnect other connected clients.")));
122 }