~ubuntu-branches/ubuntu/jaunty/kpackagekit/jaunty

« back to all changes in this revision

Viewing changes to KPackageKitD/kpackagekitd.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Mercatante
  • Date: 2009-02-24 11:16:02 UTC
  • mfrom: (1.2.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20090224111602-cskv8iurzafe9x0x
Tags: 0.4-0ubuntu1
* New upstream release
* Fixed kubuntu_01_editsources.patches to apply correctly.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
 ***************************************************************************/
20
20
 
21
21
#include "kpackagekitd.h"
 
22
#include "../libkpackagekit/KpkEnum.h"
22
23
 
23
24
#include <KGenericFactory>
24
25
#include <KStandardDirs>
25
26
#include <KConfigGroup>
26
27
#include <QDateTime>
27
28
#include <limits.h>
 
29
#include <solid/networking.h>
 
30
#include <solid/acadapter.h>
 
31
#include <solid/powermanagement.h>
28
32
 
29
33
#define FIVE_MIN 360000
30
34
 
31
35
K_PLUGIN_FACTORY(KPackageKitFactory, registerPlugin<KPackageKitD>(); )
32
36
K_EXPORT_PLUGIN(KPackageKitFactory("kpackagekitd"))
33
37
 
34
 
KPackageKitD::KPackageKitD(QObject *parent, const QList<QVariant>&)
 
38
KPackageKitD::KPackageKitD(QObject *parent, const QList<QVariant> &)
35
39
    : KDEDModule(parent), m_refreshCacheT(0)
36
40
{
37
41
    m_qtimer = new QTimer(this);
38
 
    connect( m_qtimer, SIGNAL( timeout() ), this, SLOT( init() ) ) ;
 
42
    connect(m_qtimer, SIGNAL(timeout()), this, SLOT(init()));
39
43
 
40
44
    // Create a new daemon
41
45
    m_client = Client::instance();
42
 
    connect(m_client, SIGNAL( transactionListChanged(const QList<PackageKit::Transaction*> &) ), this, SLOT( transactionListChanged(const QList<PackageKit::Transaction*> &) ) );
 
46
    connect(m_client, SIGNAL(transactionListChanged(const QList<PackageKit::Transaction*> &)),
 
47
            this, SLOT(transactionListChanged(const QList<PackageKit::Transaction*> &)));
43
48
 
44
49
    // Start after 5 minutes, 360000 msec
45
50
    // To keep the startup fast..
54
59
{
55
60
    m_qtimer->stop();
56
61
    m_qtimer->disconnect();
57
 
    connect( m_qtimer, SIGNAL( timeout() ), this, SLOT( read() ) );
58
 
 
 
62
    connect(m_qtimer, SIGNAL(timeout()), this, SLOT(read()));
59
63
 
60
64
    Client::Actions act = m_client->getActions();
61
65
 
62
66
    // check to see when the next check update will happen
63
67
    // if more that 15 minutes, call show updates
64
68
    KConfig config("KPackageKit");
65
 
    KConfigGroup checkUpdateGroup( &config, "CheckUpdate" );
 
69
    KConfigGroup checkUpdateGroup(&config, "CheckUpdate");
66
70
    // default to one day, 86400 sec
67
 
    uint interval = checkUpdateGroup.readEntry( "interval", 86400 );
68
 
 
 
71
    uint interval = checkUpdateGroup.readEntry("interval", KpkEnum::TimeIntervalDefault);
69
72
 
70
73
    // 1160 -> 15 minutes
71
 
    if ( ( (m_client->getTimeSinceAction(Client::ActionRefreshCache) - interval > 1160) && interval != 0 ) || !( act.contains(Client::ActionRefreshCache) ) )
72
 
        QProcess::execute("kpackagekit", QStringList() << "--smart-update");
73
 
 
74
 
 
75
 
    if ( !act.contains(Client::ActionRefreshCache) ) {
 
74
    if (((m_client->getTimeSinceAction(Client::ActionRefreshCache) - interval > 1160) && interval != 0 )
 
75
        || !act.contains(Client::ActionRefreshCache)) {
 
76
        // WE ARE NOT GOING TO REFRESH THE CACHE if it not time BUT
 
77
        // WE can SHOW the user his system update :D
 
78
        QProcess::execute("kpackagekit-smart-icon", QStringList() << "--update");
 
79
    }
 
80
 
 
81
    if (!act.contains(Client::ActionRefreshCache)) {
76
82
        //if the backend does not suport refreshing cache let's don't do nothing
77
83
        return;
78
84
    }
82
88
    //check if any changes to the file occour
83
89
    //this also prevents from reading when a checkUpdate happens
84
90
    m_confWatch = new KDirWatch(this);
85
 
    m_confWatch->addFile( KStandardDirs::locateLocal("config", "KPackageKit") );
86
 
    connect( m_confWatch, SIGNAL( dirty(const QString &) ), this, SLOT( read() ) );
87
 
    connect( m_confWatch, SIGNAL( created(const QString &) ), this, SLOT( read() ) );
88
 
    connect( m_confWatch, SIGNAL( deleted(const QString &) ), this, SLOT( read() ) );
 
91
    m_confWatch->addFile(KStandardDirs::locateLocal("config", "KPackageKit"));
 
92
    connect(m_confWatch, SIGNAL(dirty(const QString &)),   this, SLOT(read()));
 
93
    connect(m_confWatch, SIGNAL(created(const QString &)), this, SLOT(read()));
 
94
    connect(m_confWatch, SIGNAL(deleted(const QString &)), this, SLOT(read()));
89
95
    m_confWatch->startScan();
90
96
}
91
97
 
94
100
    KConfig config("KPackageKit");
95
101
    KConfigGroup checkUpdateGroup( &config, "CheckUpdate" );
96
102
    // default to one day, 86400 sec
97
 
    int interval = checkUpdateGroup.readEntry( "interval", 86400 );
 
103
    int interval = checkUpdateGroup.readEntry("interval", KpkEnum::TimeIntervalDefault);
98
104
    int actRefreshCache = m_client->getTimeSinceAction(Client::ActionRefreshCache);
99
 
    if ( interval == 0 )
 
105
    if (interval == KpkEnum::Never) {
100
106
        return;
101
 
    if ( actRefreshCache >= interval ) {
 
107
    }
 
108
    if (actRefreshCache >= interval) {
102
109
        checkUpdates();
103
 
    }
104
 
    else
105
 
    {
 
110
    } else {
106
111
        //check first to see any overflow...
107
 
        if ( ( interval - actRefreshCache ) > 4294966 )
108
 
            m_qtimer->start( UINT_MAX );
109
 
        else
110
 
            m_qtimer->start( ( interval - actRefreshCache ) * 1000 );
 
112
        if ((interval - actRefreshCache) > 4294966) {
 
113
            m_qtimer->start(UINT_MAX);
 
114
        } else {
 
115
            m_qtimer->start((interval - actRefreshCache) * 1000);
 
116
        }
111
117
    }
112
118
}
113
119
 
114
120
void KPackageKitD::finished(PackageKit::Transaction::ExitStatus status, uint)
115
121
{
116
 
    if ( status == Transaction::Success )
117
 
        QProcess::execute("kpackagekit", QStringList() << "--smart-update");
118
 
    else
 
122
    if (status == Transaction::Success) {
 
123
        QProcess::execute("kpackagekit-smart-icon", QStringList() << "--update");
 
124
    } else {
119
125
        // try again in 5 minutes
120
126
        m_qtimer->start(FIVE_MIN);
 
127
    }
 
128
}
 
129
 
 
130
bool KPackageKitD::systemIsReady()
 
131
{
 
132
    // test whether network is connected
 
133
    if (Solid::Networking::status() != Solid::Networking::Connected  &&
 
134
        Solid::Networking::status() != Solid::Networking::Unknown) {
 
135
        return false;
 
136
    }
 
137
 
 
138
    // check how applications should behave (e.g. on battery power)
 
139
    if (Solid::PowerManagement::appShouldConserveResources()) {
 
140
        return false;
 
141
    }
 
142
 
 
143
    return true;
121
144
}
122
145
 
123
146
void KPackageKitD::checkUpdates()
124
147
{
 
148
    // check whether system is ready for an updates check
 
149
    if (!systemIsReady()) {
 
150
        m_qtimer->start(FIVE_MIN);
 
151
        return;
 
152
    }
 
153
 
125
154
    m_refreshCacheT = m_client->refreshCache(true);
126
 
    if ( m_refreshCacheT == 0 )
 
155
    if (m_refreshCacheT == 0) {
127
156
        // try again in 5 minutes
128
 
        m_qtimer->start(FIVE_MIN);
129
 
    else
130
 
        connect( m_refreshCacheT, SIGNAL( finished(PackageKit::Transaction::ExitStatus, uint)), this, SLOT( finished(PackageKit::Transaction::ExitStatus, uint) ) );
 
157
        m_qtimer->start(FIVE_MIN);
 
158
    } else {
 
159
        connect(m_refreshCacheT, SIGNAL(finished(PackageKit::Transaction::ExitStatus, uint)),
 
160
                this, SLOT(finished(PackageKit::Transaction::ExitStatus, uint)));
 
161
    }
131
162
}
132
163
 
133
164
void KPackageKitD::transactionListChanged(const QList<PackageKit::Transaction*> &tids)
134
165
{
135
 
    if ( tids.size() )
136
 
        QProcess::execute("kpackagekit-smart-icon");
 
166
    if (tids.size()) {
 
167
        QProcess::execute("kpackagekit-smart-icon");
 
168
    }
137
169
}