1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
|
/***************************************************************************
* Copyright (C) 2012 by Daniel Nicoletti <dantti12@gmail.com> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; see the file COPYING. If not, write to *
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, *
* Boston, MA 02110-1301, USA. *
***************************************************************************/
#include "ApperdThread.h"
#include "RefreshCacheTask.h"
#include "Updater.h"
#include "DistroUpgrade.h"
#include "TransactionWatcher.h"
#include "DBusInterface.h"
#include "RebootListener.h"
#include <Enum.h>
#include <Daemon>
#include <KStandardDirs>
#include <KConfig>
#include <KConfigGroup>
#include <KDirWatch>
#include <KProtocolManager>
#include <KLocalizedString>
#include <Solid/PowerManagement>
#include <KGlobal>
#include <QStringBuilder>
#include <QtDBus/QDBusConnection>
#include <QtDBus/QDBusReply>
#include <QDBusServiceWatcher>
#include <limits.h>
#include <KDebug>
#define FIVE_MIN 360000
#define ONE_MIN 72000
/*
* What we need:
* - Refresh the package cache periodicaly (implies listenning to PK's updates changed)
* - Update or Display Package Updates
* - Display Distro Upgrades
* - Start Sentinel to keep an eye on running transactions
*/
using namespace PackageKit;
using namespace Solid;
ApperdThread::ApperdThread(QObject *parent) :
QObject(parent),
m_proxyChanged(true),
m_AptRebootListener(new AptRebootListener(this))
{
}
ApperdThread::~ApperdThread()
{
}
void ApperdThread::init()
{
connect(PowerManagement::notifier(), SIGNAL(appShouldConserveResourcesChanged(bool)),
this, SLOT(appShouldConserveResourcesChanged()));
// This timer keeps polling to see if it has
// to refresh the cache
m_qtimer = new QTimer(this);
m_qtimer->setInterval(FIVE_MIN);
connect(m_qtimer, SIGNAL(timeout()), this, SLOT(poll()));
m_qtimer->start();
//check if any changes to the file occour
//this also prevents from reading when a checkUpdate happens
KDirWatch *confWatch = new KDirWatch(this);
confWatch->addFile(KStandardDirs::locateLocal("config", "apper"));
connect(confWatch, SIGNAL(dirty(QString)), this, SLOT(configFileChanged()));
connect(confWatch, SIGNAL(created(QString)), this, SLOT(configFileChanged()));
connect(confWatch, SIGNAL(deleted(QString)), this, SLOT(configFileChanged()));
confWatch->startScan();
// Watch for changes in the KDE proxy settings
KDirWatch *proxyWatch = new KDirWatch(this);
proxyWatch->addFile(KStandardDirs::locateLocal("config", "kioslaverc"));
connect(proxyWatch, SIGNAL(dirty(QString)), this, SLOT(proxyChanged()));
connect(proxyWatch, SIGNAL(created(QString)), this, SLOT(proxyChanged()));
connect(proxyWatch, SIGNAL(deleted(QString)), this, SLOT(proxyChanged()));
proxyWatch->startScan();
QString locale(KGlobal::locale()->language() % QLatin1Char('.') % KGlobal::locale()->encoding());
Daemon::global()->setHints(QLatin1String("locale=") % locale);
connect(Daemon::global(), SIGNAL(updatesChanged()),
SLOT(updatesChanged()));
m_interface = new DBusInterface(this);
m_refreshCache = new RefreshCacheTask(this);
connect(m_interface, SIGNAL(refreshCache()),
m_refreshCache, SLOT(refreshCache()));
m_updater = new Updater(this);
m_distroUpgrade = new DistroUpgrade(this);
// read the current settings
configFileChanged();
// In case PackageKit is not running watch for it's registration to configure proxy
QDBusServiceWatcher *watcher;
watcher = new QDBusServiceWatcher(QLatin1String("org.freedesktop.PackageKit"),
QDBusConnection::systemBus(),
QDBusServiceWatcher::WatchForRegistration,
this);
connect(watcher, SIGNAL(serviceRegistered(QString)), SLOT(setProxy()));
// if PackageKit is running check to see if there are running transactons already
bool packagekitIsRunning = nameHasOwner(QLatin1String("org.freedesktop.PackageKit"),
QDBusConnection::systemBus());
m_transactionWatcher = new TransactionWatcher(packagekitIsRunning, this);
// connect the watch transaction coming from the updater icon to our watcher
connect(m_interface, SIGNAL(watchTransaction(QDBusObjectPath)),
m_transactionWatcher, SLOT(watchTransaction(QDBusObjectPath)));
// listen to Debian/Apt reboot signals from other sources (apt)
connect(m_AptRebootListener, SIGNAL(requestReboot()), m_transactionWatcher, SLOT(showRebootNotificationApt()));
QTimer::singleShot(2 /*minutes*/ * 60 /*seconds*/ * 1000 /*msec*/, m_AptRebootListener, SLOT(checkForReboot()));
if (packagekitIsRunning) {
// PackageKit is running set the session Proxy
setProxy();
// If packagekit is already running go check
// for updates
updatesChanged();
} else {
// Initial check for updates
QTimer::singleShot(ONE_MIN, this, SLOT(updatesChanged()));
}
}
// This is called every 5 minutes
void ApperdThread::poll()
{
if (m_lastRefreshCache.isNull()) {
// This value wasn't set
// convert this to QDateTime
m_lastRefreshCache = getTimeSinceRefreshCache();
}
// If check for updates is active
if (m_configs[CFG_INTERVAL].value<uint>() != Enum::Never) {
// Find out how many seconds passed since last refresh cache
uint secsSinceLastRefresh;
secsSinceLastRefresh = QDateTime::currentDateTime().toTime_t() - m_lastRefreshCache.toTime_t();
// If lastRefreshCache is null it means that the cache was never refreshed
if (m_lastRefreshCache.isNull() || secsSinceLastRefresh > m_configs[CFG_INTERVAL].value<uint>()) {
bool ignoreBattery = m_configs[CFG_CHECK_UP_BATTERY].value<bool>();
bool ignoreMobile = m_configs[CFG_CHECK_UP_MOBILE].value<bool>();
if (isSystemReady(ignoreBattery, ignoreMobile)) {
m_refreshCache->refreshCache();
}
// Invalidate the last time the cache was refreshed
m_lastRefreshCache = QDateTime();
}
}
}
void ApperdThread::configFileChanged()
{
KConfig config("apper");
KConfigGroup checkUpdateGroup(&config, "CheckUpdate");
m_configs[CFG_CHECK_UP_BATTERY] = checkUpdateGroup.readEntry(CFG_CHECK_UP_BATTERY, DEFAULT_CHECK_UP_BATTERY);
m_configs[CFG_CHECK_UP_MOBILE] = checkUpdateGroup.readEntry(CFG_CHECK_UP_MOBILE, DEFAULT_CHECK_UP_MOBILE);
m_configs[CFG_INSTALL_UP_BATTERY] = checkUpdateGroup.readEntry(CFG_INSTALL_UP_BATTERY, DEFAULT_INSTALL_UP_BATTERY);
m_configs[CFG_INSTALL_UP_MOBILE] = checkUpdateGroup.readEntry(CFG_INSTALL_UP_MOBILE, DEFAULT_INSTALL_UP_MOBILE);
m_configs[CFG_AUTO_UP] = checkUpdateGroup.readEntry(CFG_AUTO_UP, Enum::AutoUpdateDefault);
m_configs[CFG_INTERVAL] = checkUpdateGroup.readEntry(CFG_INTERVAL, Enum::TimeIntervalDefault);
m_configs[CFG_DISTRO_UPGRADE] = checkUpdateGroup.readEntry(CFG_DISTRO_UPGRADE, Enum::DistroUpgradeDefault);
m_updater->setConfig(m_configs);
m_distroUpgrade->setConfig(m_configs);
KDirWatch *confWatch = qobject_cast<KDirWatch*>(sender());
if (confWatch) {
// Check for updates again since the config changed
updatesChanged();
}
}
void ApperdThread::proxyChanged()
{
// We must reparse the configuration since the values are all cached
KProtocolManager::reparseConfiguration();
QHash<QString, QString> proxyConfig;
if (KProtocolManager::proxyType() == KProtocolManager::ManualProxy) {
proxyConfig["http"] = KProtocolManager::proxyFor("http");
proxyConfig["https"] = KProtocolManager::proxyFor("https");
proxyConfig["ftp"] = KProtocolManager::proxyFor("ftp");
proxyConfig["socks"] = KProtocolManager::proxyFor("socks");
}
// Check if the proxy settings really changed to avoid setting them twice
if (proxyConfig != m_proxyConfig) {
m_proxyConfig = proxyConfig;
m_proxyChanged = true;
setProxy();
}
}
void ApperdThread::setProxy()
{
if (!m_proxyChanged) {
return;
}
// If we were called by the watcher it is because PackageKit is running
bool packagekitIsRunning = true;
QDBusServiceWatcher *watcher = qobject_cast<QDBusServiceWatcher*>(sender());
if (!watcher) {
packagekitIsRunning = nameHasOwner(QLatin1String("org.freedesktop.PackageKit"),
QDBusConnection::systemBus());
}
if (packagekitIsRunning) {
// Apply the proxy changes only if packagekit is running
// use value() to not insert items on the hash
Daemon::global()->setProxy(m_proxyConfig.value("http"),
m_proxyConfig.value("https"),
m_proxyConfig.value("ftp"),
m_proxyConfig.value("socks"),
QString(),
QString());
m_proxyChanged = false;
}
}
void ApperdThread::updatesChanged()
{
// update the last time the cache was refreshed
QDateTime lastCacheRefresh;
lastCacheRefresh = getTimeSinceRefreshCache();
if (lastCacheRefresh != m_lastRefreshCache) {
m_lastRefreshCache = lastCacheRefresh;
}
bool ignoreBattery = m_configs[CFG_INSTALL_UP_BATTERY].value<bool>();
bool ignoreMobile = m_configs[CFG_INSTALL_UP_MOBILE].value<bool>();
// Make sure the user sees the updates
m_updater->checkForUpdates(isSystemReady(ignoreBattery, ignoreMobile));
m_distroUpgrade->checkDistroUpgrades();
}
void ApperdThread::appShouldConserveResourcesChanged()
{
bool ignoreBattery = m_configs[CFG_INSTALL_UP_BATTERY].value<bool>();
bool ignoreMobile = m_configs[CFG_INSTALL_UP_MOBILE].value<bool>();
if (isSystemReady(ignoreBattery, ignoreMobile)) {
m_updater->setSystemReady();
}
}
QDateTime ApperdThread::getTimeSinceRefreshCache() const
{
uint value = Daemon::global()->getTimeSinceAction(Transaction::RoleRefreshCache);
// When the refresh cache value was not yet defined UINT_MAX is returned
if (value == UINT_MAX) {
return QDateTime();
} else {
// Calculate the last time the cache was refreshed by
// subtracting the seconds from the current time
return QDateTime::currentDateTime().addSecs(value * -1);
}
}
bool ApperdThread::nameHasOwner(const QString &name, const QDBusConnection &connection)
{
QDBusMessage message;
message = QDBusMessage::createMethodCall(QLatin1String("org.freedesktop.DBus"),
QLatin1String("/"),
QLatin1String("org.freedesktop.DBus"),
QLatin1String("NameHasOwner"));
message << qVariantFromValue(name);
QDBusReply<bool> reply = connection.call(message);
return reply.value();
}
bool ApperdThread::isSystemReady(bool ignoreBattery, bool ignoreMobile) const
{
// First check if we should conserve resources
// check how applications should behave (e.g. on battery power)
if (!ignoreBattery && Solid::PowerManagement::appShouldConserveResources()) {
kDebug() << "System is not ready, application should conserve resources";
// This was fixed for KDElibs 4.8.5
return false;
}
// TODO it would be nice is Solid provided this
// so we wouldn't be waking up PackageKit for this Solid task.
Daemon::Network network = Daemon::global()->networkState();
// test whether network is connected
if (network == Daemon::NetworkOffline || network == Daemon::NetworkUnknown) {
kDebug() << "System is not ready, network state" << network;
return false;
}
// check how applications should behave (e.g. on battery power)
if (!ignoreMobile && network == Daemon::NetworkMobile) {
kDebug() << "System is not ready, network state" << network;
return false;
}
return true;
}
|