Commit e5a8ed88 authored by Fushan Wen's avatar Fushan Wen 💬
Browse files

wallpapers/potd: add option to disable updating over metered connection

parent 51b1b04a
......@@ -61,6 +61,16 @@ find_package(KF5 ${KF5_MIN_VERSION} REQUIRED COMPONENTS
NewStuff
)
find_package(KF5NetworkManagerQt CONFIG QUIET)
set_package_properties(KF5NetworkManagerQt PROPERTIES
DESCRIPTION "Qt wrapper for NetworkManager API"
PURPOSE "Disable wallpaper update when using metered connections"
TYPE OPTIONAL
)
if(KF5NetworkManagerQt_FOUND)
set(HAVE_NetworkManagerQt TRUE)
endif()
find_package(KF5Purpose CONFIG QUIET)
set_package_properties(KF5Purpose PROPERTIES
DESCRIPTION "Framework for cross-application services and actions"
......
......@@ -22,6 +22,10 @@
<label>Color of the wallpaper</label>
<default>#000000</default>
</entry>
<entry name="UpdateOverMeteredConnection" type="Int">
<label>Update wallpaper over metered connection. 0 - False, 1 - True, 2 - True when data usage is below limit (not implemented)</label>
<default>0</default>
</entry>
</group>
</kcfg>
......@@ -22,6 +22,7 @@ Kirigami.FormLayout {
property string cfg_Category
property int cfg_FillMode
property alias cfg_Color: colorButton.color
property int cfg_UpdateOverMeteredConnection
property alias formLayout: root
PotdBackend {
......@@ -56,6 +57,18 @@ Kirigami.FormLayout {
}
}
QQC2.CheckBox {
id: updateOverMeteredConnectionCheckBox
checked: root.cfg_UpdateOverMeteredConnection === 1
visible: backend.networkManagerQtAvailable
text: i18nc("@option:check", "Update when using metered network connection")
onToggled: {
root.cfg_UpdateOverMeteredConnection = checked ? 1 : 0;
}
}
QQC2.ComboBox {
id: categoryComboBox
visible: cfg_Provider === 'unsplash'
......
......@@ -30,6 +30,7 @@ Rectangle {
}
return [];
}
updateOverMeteredConnection: wallpaper.configuration.UpdateOverMeteredConnection
}
Rectangle {
......
configure_file(config-NetworkManagerQt.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/config-NetworkManagerQt.h)
set(potd_engine_SRCS
cachedprovider.cpp
potdbackend.cpp
......@@ -20,6 +22,10 @@ target_link_libraries(plasma_wallpaper_potdplugin plasmapotdprovidercore
Qt::Qml
Qt::Widgets # QFileDialog
)
if (HAVE_NetworkManagerQt)
target_link_libraries(plasma_wallpaper_potdplugin KF5::NetworkManagerQt)
endif()
install(TARGETS plasma_wallpaper_potdplugin DESTINATION ${KDE_INSTALL_QMLDIR}/org/kde/plasma/wallpapers/potd)
install(FILES qmldir DESTINATION ${KDE_INSTALL_QMLDIR}/org/kde/plasma/wallpapers/potd)
......
......@@ -5,6 +5,7 @@
*/
#include "potdbackend.h"
#include "config-NetworkManagerQt.h"
#include <QDBusConnection>
#include <QFileDialog>
......@@ -23,6 +24,9 @@ static int s_instanceCount = 0;
PotdBackend::PotdBackend(QObject *parent)
: QObject(parent)
#if HAVE_NetworkManagerQt
, m_networkManagerQtAvailable(true)
#endif
{
if (!s_engine) {
Q_ASSERT(s_instanceCount == 0);
......@@ -53,6 +57,11 @@ void PotdBackend::componentComplete()
// Register the identifier in the data engine
registerClient();
#if HAVE_NetworkManagerQt
if (m_client) {
m_client->setUpdateOverMeteredConnection(m_doesUpdateOverMeteredConnection);
}
#endif
}
QString PotdBackend::identifier() const
......@@ -158,6 +167,27 @@ QString PotdBackend::author() const
return m_client->m_data.wallpaperAuthor;
}
int PotdBackend::doesUpdateOverMeteredConnection() const
{
return m_doesUpdateOverMeteredConnection;
}
void PotdBackend::setUpdateOverMeteredConnection(int value)
{
if (m_doesUpdateOverMeteredConnection == std::clamp(0, 2, value)) {
return;
}
m_doesUpdateOverMeteredConnection = std::clamp(0, 2, value);
Q_EMIT updateOverMeteredConnectionChanged();
#if HAVE_NetworkManagerQt
if (m_ready && m_client) {
m_client->setUpdateOverMeteredConnection(m_doesUpdateOverMeteredConnection);
}
#endif
}
void PotdBackend::saveImage()
{
if (m_client->m_data.wallpaperLocalUrl.isEmpty()) {
......
......@@ -62,6 +62,17 @@ class PotdBackend : public QObject, public QQmlParserStatus
*/
Q_PROPERTY(QUrl savedUrl MEMBER m_savedUrl CONSTANT)
/**
* @return NetworkMangerQt is available on the system
*/
Q_PROPERTY(bool networkManagerQtAvailable MEMBER m_networkManagerQtAvailable CONSTANT)
/**
* @return Whether to update wallpapers over metered connection
*/
Q_PROPERTY(
int updateOverMeteredConnection READ doesUpdateOverMeteredConnection WRITE setUpdateOverMeteredConnection NOTIFY updateOverMeteredConnectionChanged)
public:
enum class FileOperationStatus {
None,
......@@ -90,6 +101,9 @@ public:
QString title() const;
QString author() const;
int doesUpdateOverMeteredConnection() const;
void setUpdateOverMeteredConnection(int value);
/**
* Opens a Save dialog to choose the save location, and copies the source file to the
* selected destination.
......@@ -109,11 +123,13 @@ Q_SIGNALS:
void authorChanged();
void saveStatusChanged();
void updateOverMeteredConnectionChanged();
private:
void registerClient();
bool m_ready = false;
bool m_networkManagerQtAvailable = false;
QString m_identifier;
QVariantList m_args;
......@@ -123,5 +139,7 @@ private:
FileOperationStatus m_saveStatus = FileOperationStatus::None;
QString m_saveStatusMessage;
int m_doesUpdateOverMeteredConnection = 0;
PotdClient *m_client = nullptr;
};
......@@ -11,6 +11,9 @@
#include <QDBusConnection>
#include <QThreadPool>
#if HAVE_NetworkManagerQt
#include <NetworkManagerQt/Manager>
#endif
#include <KPluginFactory>
#include "cachedprovider.h"
......@@ -18,6 +21,19 @@
using namespace std::chrono_literals;
namespace
{
#if HAVE_NetworkManagerQt
bool isUsingMeteredConnection()
{
const auto metered = NetworkManager::metered();
return metered == NetworkManager::Device::MeteredStatus::GuessYes //
|| metered == NetworkManager::Device::MeteredStatus::Yes;
}
#endif
}
PotdClient::PotdClient(const KPluginMetaData &metadata, const QVariantList &args, QObject *parent)
: QObject(parent)
, m_metadata(metadata)
......@@ -32,7 +48,12 @@ void PotdClient::updateSource(bool refresh)
setLoading(true);
// Check whether it is cached already...
#if HAVE_NetworkManagerQt
// Use cache even if it's outdated when using metered connection
if (!refresh && (CachedProvider::isCached(m_identifier, m_args, false) || (m_doesUpdateOverMeteredConnection == 0 && isUsingMeteredConnection()))) {
#else
if (!refresh && CachedProvider::isCached(m_identifier, m_args, false)) {
#endif
qCDebug(WALLPAPERPOTD) << "A local cache is available for" << m_identifier << "with arguments" << m_args;
CachedProvider *provider = new CachedProvider(m_identifier, m_args, this);
......@@ -49,6 +70,15 @@ void PotdClient::updateSource(bool refresh)
}
}
#if HAVE_NetworkManagerQt
if (m_doesUpdateOverMeteredConnection == 0 && isUsingMeteredConnection()) {
qCDebug(WALLPAPERPOTD) << "Skip updating wallpapers for" << m_identifier << m_args << "due to metered connection.";
setLoading(false);
Q_EMIT done(this, false);
return;
}
#endif
const auto pluginResult = KPluginFactory::instantiatePlugin<PotdProvider>(m_metadata, this, m_args);
if (pluginResult) {
......@@ -59,6 +89,20 @@ void PotdClient::updateSource(bool refresh)
}
}
#if HAVE_NetworkManagerQt
void PotdClient::setUpdateOverMeteredConnection(int value)
{
if (m_doesUpdateOverMeteredConnection == value) {
return;
}
m_doesUpdateOverMeteredConnection = value;
if (m_doesUpdateOverMeteredConnection == 1 && isUsingMeteredConnection()) {
updateSource(true);
}
}
#endif
void PotdClient::slotFinished(PotdProvider *provider)
{
setImage(provider->image());
......
......@@ -14,6 +14,7 @@
#include <KPluginMetaData>
#include "config-NetworkManagerQt.h"
#include "potdprovider.h"
/**
......@@ -27,6 +28,9 @@ public:
PotdClient(const KPluginMetaData &metadata, const QVariantList &args, QObject *parent = nullptr);
void updateSource(bool refresh = false);
#if HAVE_NetworkManagerQt
void setUpdateOverMeteredConnection(int value);
#endif
KPluginMetaData m_metadata;
PotdProviderData m_data;
......@@ -58,6 +62,9 @@ private:
QString m_identifier;
QVariantList m_args;
#if HAVE_NetworkManagerQt
int m_doesUpdateOverMeteredConnection = 0;
#endif
friend class PotdEngine;
};
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment