aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/updateinfo/updateinfoplugin.cpp
blob: 919b874f1cd9d66d93096ff59a3264c10c67168a (plain)
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
/****************************************************************************
**
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/

#include "updateinfoplugin.h"
#include "updateinfobutton.h"

#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/actionmanager/actioncontainer.h>
#include <coreplugin/coreconstants.h>
#include <coreplugin/icore.h>
#include <coreplugin/modemanager.h>
#include <coreplugin/progressmanager/progressmanager.h>
#include <coreplugin/progressmanager/futureprogress.h>

#include <qtconcurrentrun.h>

#include <QtPlugin>
#include <QProcess>
#include <QTimer>
#include <QTimerEvent>
#include <QDebug>
#include <QFile>
#include <QThread>
#include <QCoreApplication>
#include <QProcess>
#include <QDomDocument>
#include <QMessageBox>
#include <QMenu>
#include <QFutureWatcher>

namespace {
    static const quint32 OneMinute = 60000;
}

namespace UpdateInfo {
namespace Internal {

class UpdateInfoPluginPrivate
{
public:
    UpdateInfoPluginPrivate()
        : startUpdaterAction(0),
          currentTimerId(0),
          progressUpdateInfoButton(0),
          checkUpdateInfoWatcher(0)
    {}
    ~UpdateInfoPluginPrivate()
    {
    }

    QAction *startUpdaterAction;
    QString updaterProgram;
    QString updaterCheckOnlyArgument;
    QString updaterRunUiArgument;
    int currentTimerId;
    QFuture<QDomDocument> lastCheckUpdateInfoTask;
    QPointer<Core::FutureProgress> updateInfoProgress;
    UpdateInfoButton *progressUpdateInfoButton;
    QFutureWatcher<QDomDocument> *checkUpdateInfoWatcher;
};


UpdateInfoPlugin::UpdateInfoPlugin()
    : d(new UpdateInfoPluginPrivate)
{
}

UpdateInfoPlugin::~UpdateInfoPlugin()
{
    delete d;
}

void UpdateInfoPlugin::startCheckTimer(uint milliseconds)
{
    if (d->currentTimerId != 0) {
        stopCurrentCheckTimer();
    }
    d->currentTimerId = startTimer(milliseconds);
}

void UpdateInfoPlugin::stopCurrentCheckTimer()
{
    killTimer(d->currentTimerId);
    d->currentTimerId = 0;
}


/*! Initializes the plugin. Returns true on success.
    Plugins want to register objects with the plugin manager here.

    \a errorMessage can be used to pass an error message to the plugin system,
       if there was any.
*/
bool UpdateInfoPlugin::initialize(const QStringList & /* arguments */, QString *errorMessage)
{
    d->checkUpdateInfoWatcher = new QFutureWatcher<QDomDocument>(this);
    connect(d->checkUpdateInfoWatcher, SIGNAL(finished()), this, SLOT(reactOnUpdaterOutput()));

    QSettings *settings = Core::ICore::settings();
    d->updaterProgram = settings->value(QLatin1String("Updater/Application")).toString();
    d->updaterCheckOnlyArgument = settings->value(QLatin1String("Updater/CheckOnlyArgument")).toString();
    d->updaterRunUiArgument = settings->value(QLatin1String("Updater/RunUiArgument")).toString();

    if (d->updaterProgram.isEmpty()) {
        *errorMessage = tr("Could not determine location of maintenance tool. Please check "
                           "your installation if you did not enable this plugin manually.");
        return false;
    }

    if (!QFile::exists(d->updaterProgram)) {
        *errorMessage = tr("Could not find maintenance tool at '%1'. Check your installation.")
                .arg(d->updaterProgram);
        return false;
    }

    Core::ActionContainer* const helpActionContainer = Core::ActionManager::actionContainer(Core::Constants::M_HELP);
    helpActionContainer->menu()->addAction(tr("Start Updater"), this, SLOT(startUpdaterUiApplication()));

    //wait some time before we want to have the first check
    startCheckTimer(OneMinute / 10);

    return true;
}

QDomDocument UpdateInfoPlugin::checkForUpdates()
{
    if (QThread::currentThread() == QCoreApplication::instance()->thread()) {
        qWarning() << Q_FUNC_INFO << " Was not designed to run in main/gui thread -> it is using updaterProcess.waitForFinished()";
    }

    //starting
    QProcess updaterProcess;

    updaterProcess.start(d->updaterProgram,
                         QStringList() << d->updaterCheckOnlyArgument);
    updaterProcess.waitForFinished();

    //process return value
    if (updaterProcess.exitStatus() == QProcess::CrashExit) {
        qWarning() << "Get update info application crashed.";
        //return; //maybe there is some output
    }
    QString updaterOutput = updaterProcess.readAllStandardOutput();
    QDomDocument updatesDomDocument;
    updatesDomDocument.setContent(updaterOutput);

    return updatesDomDocument;
}


void UpdateInfoPlugin::reactOnUpdaterOutput()
{
    QDomDocument updatesDomDocument = d->checkUpdateInfoWatcher->result();

    if (updatesDomDocument.isNull() ||
        !updatesDomDocument.firstChildElement().hasChildNodes())
    { // no updates are available
        startCheckTimer(60 * OneMinute);
    } else {
        //added the current almost finished task to the progressmanager
        d->updateInfoProgress = Core::ICore::progressManager()->addTask(
                d->lastCheckUpdateInfoTask, tr("Update"), QLatin1String("Update.GetInfo"), Core::ProgressManager::KeepOnFinish);

        d->updateInfoProgress->setKeepOnFinish(Core::FutureProgress::KeepOnFinish);

        d->progressUpdateInfoButton = new UpdateInfoButton();
        //the old widget is deleted inside this function
        //and the current widget becomes a child of updateInfoProgress
        d->updateInfoProgress->setWidget(d->progressUpdateInfoButton);

        //d->progressUpdateInfoButton->setText(tr("Update")); //we have this information over the progressbar
        connect(d->progressUpdateInfoButton, SIGNAL(released()),
                this, SLOT(startUpdaterUiApplication()));
    }
}

void UpdateInfoPlugin::startUpdaterUiApplication()
{
    QProcess::startDetached(d->updaterProgram, QStringList() << d->updaterRunUiArgument);
    if (!d->updateInfoProgress.isNull()) {
        d->updateInfoProgress->setKeepOnFinish(Core::FutureProgress::HideOnFinish); //this is fading out the last updateinfo
    }
    startCheckTimer(OneMinute);
}

void UpdateInfoPlugin::timerEvent(QTimerEvent *event)
{
    if (event->timerId() == d->currentTimerId && !d->lastCheckUpdateInfoTask.isRunning())
    {
        stopCurrentCheckTimer();
        d->lastCheckUpdateInfoTask = QtConcurrent::run(this, &UpdateInfoPlugin::checkForUpdates);
        d->checkUpdateInfoWatcher->setFuture(d->lastCheckUpdateInfoTask);
    }
}

void UpdateInfoPlugin::extensionsInitialized()
{
}

} //namespace Internal
} //namespace UpdateInfo

Q_EXPORT_PLUGIN(UpdateInfo::Internal::UpdateInfoPlugin)