refs #921, moved distribution info UI into an own component

This commit is contained in:
Klaus Basan
2017-03-29 02:30:40 +02:00
committed by Mathew Sutcliffe
parent cbf69d9847
commit 83a80bf739
6 changed files with 408 additions and 195 deletions

View File

@@ -0,0 +1,173 @@
/* Copyright (C) 2017
* swift project Community / Contributors
*
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
* including this file, may be copied, modified, propagated, or distributed except according to the terms
* contained in the LICENSE file.
*/
#include "distributioninfocomponent.h"
#include "ui_distributioninfocomponent.h"
#include "blackconfig/buildconfig.h"
#include "blackgui/guiapplication.h"
#include "blackmisc/network/networkutils.h"
#include "blackmisc/db/distributionlist.h"
#include "blackmisc/logmessage.h"
using namespace BlackConfig;
using namespace BlackCore::Application;
using namespace BlackMisc;
using namespace BlackMisc::Db;
using namespace BlackMisc::Network;
namespace BlackGui
{
namespace Components
{
CDistributionInfoComponent::CDistributionInfoComponent(QWidget *parent) :
QFrame(parent),
ui(new Ui::CDistributionInfoComponent)
{
ui->setupUi(this);
ui->lbl_NewVersionUrl->setTextFormat(Qt::RichText);
ui->lbl_NewVersionUrl->setTextInteractionFlags(Qt::TextBrowserInteraction);
ui->lbl_NewVersionUrl->setOpenExternalLinks(true);
// use version signal as trigger for completion
connect(sGui, &CGuiApplication::distributionInfoAvailable, this, &CDistributionInfoComponent::ps_loadedDistributionInfo);
QTimer::singleShot(10 * 1000, this, [ = ]
{
// use has time out failover with cache data
if (m_distributionsLoaded) { return; }
this->ps_loadedDistributionInfo(true); // failover
});
connect(ui->pb_CheckForUpdates, &QPushButton::pressed, this, &CDistributionInfoComponent::ps_loadSetup);
}
CDistributionInfoComponent::~CDistributionInfoComponent()
{ }
void CDistributionInfoComponent::ps_loadSetup()
{
if (!ui->le_LatestVersion->text().isEmpty())
{
ui->le_LatestVersion->setText("");
const CStatusMessageList msgs(sApp->requestReloadOfSetupAndVersion());
CLogMessage::preformatted(msgs);
}
}
void CDistributionInfoComponent::ps_loadedDistributionInfo(bool success)
{
if (!success)
{
CLogMessage(this).warning("Loading setup or distribution information failed");
return;
}
m_distributionsLoaded = true;
this->ps_channelChanged();
// emit only after all has been set
emit this->distributionInfoAvailable(success);
}
void CDistributionInfoComponent::ps_changedDistributionCache()
{
this->ps_loadedDistributionInfo(true);
}
void CDistributionInfoComponent::saveSettings()
{
const QString channel = ui->cb_Channels->currentText();
const QString currentPlatform = ui->cb_Platforms->currentText();
const QStringList settings({ channel, currentPlatform });
const CStatusMessage m = this->m_distributionSettings.setAndSave(settings);
if (m.isFailure())
{
CLogMessage(this).preformatted(m);
}
}
void CDistributionInfoComponent::ps_channelChanged()
{
const CDistributionList distributions(m_distributionInfo.get());
const QStringList channels = distributions.getChannels();
const QStringList settings = m_distributionSettings.get();
// default value
QString channel = ui->cb_Channels->currentText();
if (channel.isEmpty()) { channel = settings.front(); }
if (channel.isEmpty() && !channels.isEmpty()) { channel = channels.front(); }
// channels
ui->cb_Channels->disconnect();
ui->cb_Platforms->disconnect();
ui->cb_Channels->clear();
ui->cb_Channels->insertItems(0, channels);
if (!channel.isEmpty()) { ui->cb_Channels->setCurrentText(channel); }
// current distribution
const CDistribution currentDistribution = distributions.findByChannelOrDefault(channel);
const QStringList platforms = currentDistribution.getPlatforms();
m_currentDistribution = currentDistribution;
ui->le_CurrentVersion->setText(CBuildConfig::getVersionString());
// platforms
QString platform = ui->cb_Platforms->currentText();
if (platform.isEmpty()) { platform = settings.last(); }
if (platform.isEmpty()) { platform = currentDistribution.guessPlatform(); }
ui->cb_Platforms->clear();
ui->cb_Platforms->insertItems(0, platforms);
if (!platform.isEmpty()) { ui->cb_Platforms->setCurrentText(platform); }
// platform dependent stuff
this->ps_platformChanged();
connect(ui->cb_Channels, &QComboBox::currentTextChanged, this, &CDistributionInfoComponent::ps_channelChanged);
connect(ui->cb_Platforms, &QComboBox::currentTextChanged, this, &CDistributionInfoComponent::ps_platformChanged);
}
void CDistributionInfoComponent::ps_platformChanged()
{
this->saveSettings();
// defaults
ui->le_LatestVersion->clear();
ui->lbl_NewVersionInfo->setText("Nothing new");
ui->lbl_NewVersionInfo->setStyleSheet("background-color: green");
ui->lbl_NewVersionUrl->clear();
this->m_newVersionAvailable.clear();
const QString currentPlatform = ui->cb_Platforms->currentText();
if (!currentPlatform.isEmpty())
{
const QVersionNumber latestVersion = m_currentDistribution.getQVersion(currentPlatform);
const QString latestVersionStr = m_currentDistribution.getVersionString(currentPlatform);
ui->le_LatestVersion->setText(latestVersionStr);
ui->le_LatestVersion->setToolTip("");
CFailoverUrlList downloadUrls(m_currentDistribution.getDownloadUrls());
const CUrl downloadUrl(downloadUrls.obtainNextUrl());
const bool newer = CBuildConfig::getVersion() < latestVersion;
if (newer)
{
ui->lbl_NewVersionInfo->setText("New version!");
ui->lbl_NewVersionInfo->setToolTip("New version '" + latestVersionStr + "'");
ui->lbl_NewVersionInfo->setStyleSheet("background-color: red");
this->m_newVersionAvailable = latestVersionStr;
}
if (!downloadUrl.isEmpty())
{
const QString urlStr(downloadUrl.toQString());
const QString hl("<a href=\"%1\"><img src=\":/own/icons/own/drophere16.png\"></a> %2 %3");
ui->lbl_NewVersionUrl->setText(hl.arg(urlStr, latestVersionStr, currentPlatform));
ui->lbl_NewVersionUrl->setToolTip("Download '" + latestVersionStr + "' " + m_currentDistribution.getFilename(currentPlatform));
}
}
}
} // ns
} // ns

View File

@@ -0,0 +1,75 @@
/* Copyright (C) 2017
* swift project Community / Contributors
*
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
* including this file, may be copied, modified, propagated, or distributed except according to the terms
* contained in the LICENSE file.
*/
//! \file
#ifndef BLACKGUI_COMPONENTS_DISTRIBUTIONINFOCOMPONENT_H
#define BLACKGUI_COMPONENTS_DISTRIBUTIONINFOCOMPONENT_H
#include "blackgui/blackguiexport.h"
#include "blackmisc/db/distributionlist.h"
#include "blackmisc/settingscache.h"
#include <QFrame>
namespace Ui { class CDistributionInfoComponent; }
namespace BlackGui
{
namespace Components
{
/**
* Update info (distributions etc.)
*/
class BLACKGUI_EXPORT CDistributionInfoComponent : public QFrame
{
Q_OBJECT
public:
//! Ctor
explicit CDistributionInfoComponent(QWidget *parent = nullptr);
//! Dtor
virtual ~CDistributionInfoComponent();
//! Is there a new version available return version, else empty string
QString getNewVersionAvailable() const { return m_newVersionAvailable; }
signals:
//! Distribution info loaded
void distributionInfoAvailable(bool success);
private slots:
//! Load latest version
void ps_loadSetup();
//! Loaded latest version
void ps_loadedDistributionInfo(bool success);
//! Channel has been changed
void ps_channelChanged();
//! Platform changed
void ps_platformChanged();
//! Cache values have been changed
void ps_changedDistributionCache();
private:
QScopedPointer<Ui::CDistributionInfoComponent> ui;
bool m_distributionsLoaded = false; //!< distribution info loaded
QString m_newVersionAvailable; //!< new version number if any
BlackMisc::Db::CDistribution m_currentDistribution; //!< current distribution
BlackMisc::CDataReadOnly<BlackMisc::Db::TDistributionInfo> m_distributionInfo { this, &CDistributionInfoComponent::ps_changedDistributionCache }; //!< version cache
BlackMisc::CSetting<BlackMisc::Db::TDistributionSetting> m_distributionSettings { this }; //!< channel/platform selected
//! Save the current settings
void saveSettings();
};
} // ns
} // ns
#endif // guard

View File

@@ -0,0 +1,108 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>CDistributionInfoComponent</class>
<widget class="QFrame" name="CDistributionInfoComponent">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>307</width>
<height>90</height>
</rect>
</property>
<property name="minimumSize">
<size>
<width>0</width>
<height>90</height>
</size>
</property>
<property name="windowTitle">
<string>Distribution info</string>
</property>
<layout class="QGridLayout" name="gl_DistributionInfo">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item row="0" column="0">
<widget class="QLabel" name="lbl_CurrentVersion">
<property name="text">
<string>This version:</string>
</property>
</widget>
</item>
<item row="1" column="3">
<widget class="QComboBox" name="cb_Platforms"/>
</item>
<item row="0" column="2">
<widget class="QLabel" name="lbl_LatestVersion">
<property name="text">
<string>Latest:</string>
</property>
</widget>
</item>
<item row="0" column="3">
<widget class="QLineEdit" name="le_LatestVersion">
<property name="readOnly">
<bool>true</bool>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="lbl_Channel">
<property name="text">
<string>Channel:</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="le_CurrentVersion">
<property name="readOnly">
<bool>true</bool>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QComboBox" name="cb_Channels"/>
</item>
<item row="1" column="2">
<widget class="QLabel" name="lbl_Platforms">
<property name="text">
<string>Platform:</string>
</property>
</widget>
</item>
<item row="2" column="3">
<widget class="QPushButton" name="pb_CheckForUpdates">
<property name="text">
<string>check again</string>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="lbl_NewVersionInfo">
<property name="text">
<string>Nothing new</string>
</property>
</widget>
</item>
<item row="2" column="1" colspan="2">
<widget class="QLabel" name="lbl_NewVersionUrl">
<property name="text">
<string>URL goes here</string>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>