cmake: avoid de-duplication of user's CXXFLAGS
[quassel.git] / src / qtui / coreinfodlg.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2020 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 #include "util.h"
29
30 CoreInfoDlg::CoreInfoDlg(QWidget* parent)
31     : QDialog(parent)
32 {
33     ui.setupUi(this);
34
35     // Listen for resynchronization events (pre-0.13 cores only)
36     connect(Client::instance(), &Client::coreInfoResynchronized, this, &CoreInfoDlg::coreInfoResynchronized);
37
38     // Update legacy core info for Quassel cores earlier than 0.13.  This does nothing on modern
39     // cores.
40     refreshLegacyCoreInfo();
41
42     // Display existing core info, set up signal handlers
43     coreInfoResynchronized();
44
45     // Warning icon
46     ui.coreUnsupportedIcon->setPixmap(icon::get({"emblem-unavailable", "dialog-warning"}).pixmap(16));
47
48     updateUptime();
49     startTimer(1000);
50 }
51
52 void CoreInfoDlg::refreshLegacyCoreInfo()
53 {
54     if (!Client::isConnected() || Client::isCoreFeatureEnabled(Quassel::Feature::SyncedCoreInfo)) {
55         // If we're not connected, or the core supports SyncedCoreInfo (0.13+), bail out
56         return;
57     }
58
59     // Request legacy (pre-0.13) CoreInfo object to be resynchronized (does nothing on modern cores)
60     Client::refreshLegacyCoreInfo();
61
62     // On legacy cores, CoreInfo data does not send signals.  Periodically poll for information.
63     // 15 seconds seems like a reasonable trade-off as this only happens while the dialog is open.
64     QTimer::singleShot(15 * 1000, this, &CoreInfoDlg::refreshLegacyCoreInfo);
65 }
66
67 void CoreInfoDlg::coreInfoResynchronized()
68 {
69     // CoreInfo object has been recreated, or this is the first time the dialog's been shown
70
71     CoreInfo* coreInfo = Client::coreInfo();
72     // Listen for changes to core information
73     connect(coreInfo, &CoreInfo::coreDataChanged, this, &CoreInfoDlg::coreInfoChanged);
74
75     // Update with any known core information set before connecting the signal.  This is needed for
76     // both modern (0.13+) and legacy cores.
77     coreInfoChanged(coreInfo->coreData());
78 }
79
80 void CoreInfoDlg::coreInfoChanged(const QVariantMap& coreInfo)
81 {
82     if (coreInfo.isEmpty()) {
83         // We're missing data for some reason
84         if (Client::isConnected()) {
85             // Core info is entirely empty despite being connected.  Something's probably wrong.
86             ui.labelCoreVersion->setText(tr("Unknown"));
87             ui.labelCoreVersionDate->setText(tr("Unknown"));
88         }
89         else {
90             // We're disconnected.  Mark as such.
91             ui.labelCoreVersion->setText(tr("Disconnected from core"));
92             ui.labelCoreVersionDate->setText(tr("Not available"));
93         }
94         ui.labelClientCount->setNum(0);
95         // Don't return, allow the code below to remove any existing session widgets
96     }
97     else {
98         ui.labelCoreVersion->setText(coreInfo["quasselVersion"].toString());
99         // "BuildDate" for compatibility
100         if (coreInfo["quasselBuildDate"].toString().isEmpty()) {
101             ui.labelCoreVersionDate->setText(QString("<i>%1</i>").arg(tr("Unknown date")));
102         }
103         else {
104             ui.labelCoreVersionDate->setText(
105                 tryFormatUnixEpoch(coreInfo["quasselBuildDate"].toString(), Qt::DateFormat::DefaultLocaleShortDate));
106         }
107         ui.labelClientCount->setNum(coreInfo["sessionConnectedClients"].toInt());
108     }
109
110     auto coreSessionSupported = false;
111     auto ids = _widgets.keys();
112     for (const auto& peerData : coreInfo["sessionConnectedClientData"].toList()) {
113         coreSessionSupported = true;
114
115         auto peerMap = peerData.toMap();
116         int peerId = peerMap["id"].toInt();
117
118         ids.removeAll(peerId);
119
120         bool isNew = false;
121         CoreSessionWidget* coreSessionWidget = _widgets[peerId];
122         if (coreSessionWidget == nullptr) {
123             coreSessionWidget = new CoreSessionWidget(ui.coreSessionScrollContainer);
124             isNew = true;
125         }
126         coreSessionWidget->setData(peerMap);
127         if (isNew) {
128             _widgets[peerId] = coreSessionWidget;
129             // Add this to the end of the session list, but before the default layout stretch item.
130             // The layout stretch item should never be removed, so count should always be >= 1.
131             ui.coreSessionContainer->insertWidget(ui.coreSessionContainer->count() - 1, coreSessionWidget, 0, Qt::AlignTop);
132             connect(coreSessionWidget, &CoreSessionWidget::disconnectClicked, this, &CoreInfoDlg::disconnectClicked);
133         }
134     }
135
136     for (const auto& key : ids) {
137         delete _widgets[key];
138         _widgets.remove(key);
139     }
140
141     ui.coreSessionScrollArea->setVisible(coreSessionSupported);
142
143     // Hide the information bar when core sessions are supported or when disconnected
144     ui.coreUnsupportedWidget->setVisible(!(coreSessionSupported || Client::isConnected() == false));
145
146     // Update uptime for immediate display, don't wait for the timer
147     updateUptime();
148 }
149
150 void CoreInfoDlg::updateUptime()
151 {
152     CoreInfo* coreInfo = Client::coreInfo();
153
154     if (!Client::isConnected()) {
155         // Not connected, don't bother trying to calculate the uptime
156         ui.labelUptime->setText(tr("Not available"));
157     }
158     else if (coreInfo->coreData().isEmpty()) {
159         // Core info is entirely empty despite being connected.  Something's probably wrong.
160         ui.labelUptime->setText(tr("Unknown"));
161     }
162     else {
163         // Connected, format the uptime for display
164         QDateTime startTime = coreInfo->at("startTime").toDateTime();
165
166         int64_t uptime = startTime.secsTo(QDateTime::currentDateTime().toUTC());
167         int64_t updays = uptime / 86400;
168         uptime %= 86400;
169         int64_t uphours = uptime / 3600;
170         uptime %= 3600;
171         int64_t upmins = uptime / 60;
172         uptime %= 60;
173
174         QString uptimeText = tr("%n Day(s)", "", updays)
175                              + tr(" %1:%2:%3 (since %4)")
176                                    .arg(uphours, 2, 10, QChar('0'))
177                                    .arg(upmins, 2, 10, QChar('0'))
178                                    .arg(uptime, 2, 10, QChar('0'))
179                                    .arg(startTime.toLocalTime().toString(Qt::DefaultLocaleShortDate));
180         ui.labelUptime->setText(uptimeText);
181     }
182 }
183
184 void CoreInfoDlg::disconnectClicked(int peerId)
185 {
186     Client::kickClient(peerId);
187 }
188
189 void CoreInfoDlg::on_coreUnsupportedDetails_clicked()
190 {
191     QMessageBox::warning(this,
192                          tr("Active sessions unsupported"),
193                          QString("<p><b>%1</b></p></br><p>%2</p>")
194                              .arg(tr("Your Quassel core is too old to show active sessions"),
195                                   tr("You need a Quassel core v0.13.0 or newer to view and "
196                                      "disconnect other connected clients.")));
197 }