~nick-dedekind/ubuntu-system-settings/lp1336715.check.sync

« back to all changes in this revision

Viewing changes to plugins/system-update/plugin/update-plugin.cpp

  • Committer: Nick Dedekind
  • Date: 2014-11-04 12:08:12 UTC
  • mfrom: (1153.1.29 ubuntu-system-settings)
  • Revision ID: nick.dedekind@canonical.com-20141104120812-xy1yrfdu4qi99ei4
merged with trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
 
23
23
#include <QDebug>
24
24
#include <QStringList>
 
25
#include <QTimer>
25
26
#include <SystemSettings/ItemBase>
26
27
 
27
 
#include <token.h>
28
 
#include <ssoservice.h>
29
 
#include <QProcess>
30
 
#include <QJsonDocument>
31
 
#include <QJsonObject>
32
 
#include <QJsonArray>
33
 
#include <QJsonValue>
34
 
#include <QHash>
35
 
#include "../system_update.h"
36
 
#include "../update.h"
37
 
#include "../network/network.h"
 
28
#include "../update_manager.h"
38
29
 
39
30
using namespace SystemSettings;
40
 
using namespace UbuntuOne;
41
31
using namespace UpdatePlugin;
42
32
 
43
33
class UpdateItem: public ItemBase
46
36
 
47
37
public:
48
38
    explicit UpdateItem(const QVariantMap &staticData, QObject *parent = 0);
49
 
    void setVisibility(bool visible);
 
39
    Q_INVOKABLE void setVisibility(bool visible);
50
40
    ~UpdateItem();
51
41
 
52
 
Q_SIGNALS:
53
 
    void checkFinished();
54
 
    void modelChanged();
55
 
    void updatesNotFound();
56
 
    void credentialsNotFound();
57
 
    void updateAvailableFound(bool downloading);
58
 
    void errorFound();
59
 
    void downloadModeChanged();
60
 
    void systemUpdateDownloaded();
61
 
    void updateProcessFailed(QString message);
62
 
    void systemUpdateFailed(int consecutiveFailureCount, QString lastReason);
63
 
 
64
42
private Q_SLOTS:
65
 
    void changeVisibility(const QString&, Update*);
66
 
    void processOutput();
67
 
    void processUpdates();
68
 
    void handleCredentialsFound(Token token);
 
43
    void onUpdateAvailableFound(bool);
 
44
    void onModelChanged();
 
45
    void shouldShow();
69
46
 
70
47
private:
71
 
    SystemUpdate m_systemUpdate;
72
 
    Token m_token;
73
 
    UpdatePlugin::Network m_network;
74
 
    QProcess m_process;
75
 
    SSOService m_service;
76
 
    QHash<QString, Update*> m_apps;
 
48
    UpdateManager *m_updateManager;
77
49
};
78
50
 
79
51
UpdateItem::UpdateItem(const QVariantMap &staticData, QObject *parent):
80
 
    ItemBase(staticData, parent),
81
 
    m_systemUpdate(this)
82
 
{
83
 
    setVisibility(m_systemUpdate.checkTarget());
84
 
    // SYSTEM UPDATE
85
 
    QObject::connect(&m_systemUpdate, SIGNAL(updateAvailable(const QString&, Update*)),
86
 
                  this, SLOT(changeVisibility(const QString&, Update*)));
87
 
 
88
 
    // SSO SERVICE
89
 
    QObject::connect(&m_service, SIGNAL(credentialsFound(const Token&)),
90
 
                     this, SLOT(handleCredentialsFound(Token)));
91
 
    // PROCESS
92
 
    QObject::connect(&m_process, SIGNAL(finished(int)),
93
 
                  this, SLOT(processOutput()));
94
 
    // NETWORK
95
 
    QObject::connect(&m_network, SIGNAL(updatesFound()),
96
 
                  this, SLOT(processUpdates()));
97
 
 
98
 
    m_service.getCredentials();
99
 
}
100
 
 
101
 
void UpdateItem::handleCredentialsFound(Token token)
102
 
{
103
 
    m_token = token;
104
 
    QStringList args("list");
105
 
    args << "--manifest";
106
 
    QString command("click");
107
 
    m_process.start(command, args);
108
 
}
109
 
 
110
 
void UpdateItem::processOutput()
111
 
{
112
 
    QString output(m_process.readAllStandardOutput());
113
 
 
114
 
    QJsonDocument document = QJsonDocument::fromJson(output.toUtf8());
115
 
 
116
 
    QJsonArray array = document.array();
117
 
 
118
 
    int i;
119
 
    for (i = 0; i < array.size(); i++) {
120
 
        QJsonObject object = array.at(i).toObject();
121
 
        QString name = object.value("name").toString();
122
 
        QString title = object.value("title").toString();
123
 
        QString version = object.value("version").toString();
124
 
        Update *app = new Update();
125
 
        app->initializeApplication(name, title, version);
126
 
        m_apps[app->getPackageName()] = app;
127
 
    }
128
 
 
129
 
    m_network.checkForNewVersions(m_apps);
130
 
}
131
 
 
132
 
 
133
 
void UpdateItem::processUpdates()
134
 
{
135
 
    foreach (QString id, m_apps.keys()) {
136
 
        Update *app = m_apps.value(id);
137
 
        if(app->updateRequired()) {
138
 
            setVisibility(true);
139
 
            break;
140
 
        }
141
 
    }
142
 
}
143
 
 
144
 
void UpdateItem::changeVisibility(const QString&, Update*)
145
 
{
146
 
    setVisibility(true);
 
52
    ItemBase(staticData, parent)
 
53
{
 
54
    setVisibility(false);
 
55
    m_updateManager = UpdateManager::instance();    
 
56
    QObject::connect(m_updateManager, SIGNAL(updateAvailableFound(bool)),
 
57
                  this, SLOT(onUpdateAvailableFound(bool)));
 
58
    QObject::connect(m_updateManager, SIGNAL(modelChanged()),
 
59
                  this, SLOT(onModelChanged()));
 
60
    QTimer::singleShot(100, this, SLOT(shouldShow()));
 
61
}
 
62
 
 
63
void UpdateItem::onUpdateAvailableFound(bool)
 
64
{
 
65
    if (m_updateManager->model().count() > 0)
 
66
        setVisibility(true);
147
67
}
148
68
 
149
69
void UpdateItem::setVisibility(bool visible)
151
71
    setVisible(visible);
152
72
}
153
73
 
 
74
void UpdateItem::onModelChanged()
 
75
{
 
76
    if (m_updateManager->model().count() > 0)
 
77
        setVisibility(true);
 
78
    else 
 
79
        setVisibility(false);
 
80
}
 
81
 
 
82
void UpdateItem::shouldShow()
 
83
{
 
84
    if (m_updateManager->checkTarget())
 
85
        setVisibility(true);
 
86
}
 
87
 
154
88
UpdateItem::~UpdateItem()
155
89
{
156
90
}