source: calamares/trunk/fuentes/src/modules/tracking/TrackingJobs.cpp @ 7538

Last change on this file since 7538 was 7538, checked in by kbut, 17 months ago

sync with github

File size: 4.3 KB
Line 
1/* === This file is part of Calamares - <https://github.com/calamares> ===
2 *
3 *   Copyright 2017, Adriaan de Groot <groot@kde..org>
4 *
5 *   Calamares is free software: you can redistribute it and/or modify
6 *   it under the terms of the GNU General Public License as published by
7 *   the Free Software Foundation, either version 3 of the License, or
8 *   (at your option) any later version.
9 *
10 *   Calamares is distributed in the hope that it will be useful,
11 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
12 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 *   GNU General Public License for more details.
14 *
15 *   You should have received a copy of the GNU General Public License
16 *   along with Calamares. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include "TrackingJobs.h"
20
21#include "utils/CalamaresUtilsSystem.h"
22#include "utils/Logger.h"
23
24#include <QEventLoop>
25#include <QNetworkAccessManager>
26#include <QNetworkReply>
27#include <QSemaphore>
28#include <QTimer>
29
30TrackingInstallJob::TrackingInstallJob( const QString& url )
31    : m_url( url )
32    , m_networkManager( nullptr )
33{
34}
35
36TrackingInstallJob::~TrackingInstallJob()
37{
38    delete m_networkManager;
39}
40
41QString TrackingInstallJob::prettyName() const
42{
43    return tr( "Installation feedback" );
44}
45
46QString TrackingInstallJob::prettyDescription() const
47{
48    return prettyName();
49}
50
51QString TrackingInstallJob::prettyStatusMessage() const
52{
53    return tr( "Sending installation feedback." );
54}
55
56Calamares::JobResult TrackingInstallJob::exec()
57{
58    m_networkManager = new QNetworkAccessManager();
59
60    QNetworkRequest request;
61    request.setUrl( QUrl( m_url ) );
62    // Follows all redirects except unsafe ones (https to http).
63    request.setAttribute( QNetworkRequest::FollowRedirectsAttribute, true );
64    // Not everybody likes the default User Agent used by this class (looking at you,
65    // sourceforge.net), so let's set a more descriptive one.
66    request.setRawHeader( "User-Agent", "Mozilla/5.0 (compatible; Calamares)" );
67
68    QTimer timeout;
69    timeout.setSingleShot(true);
70
71    QEventLoop loop;
72
73    connect( m_networkManager, &QNetworkAccessManager::finished,
74             this, &TrackingInstallJob::dataIsHere );
75    connect( m_networkManager, &QNetworkAccessManager::finished,
76             &loop, &QEventLoop::quit );
77    connect( &timeout, &QTimer::timeout,
78             &loop, &QEventLoop::quit );
79
80    m_networkManager->get( request );  // The semaphore is released when data is received
81    timeout.start( 5000 /* ms */ );
82
83    loop.exec();
84
85    if ( !timeout.isActive() )
86    {
87        cWarning() << "install-tracking request timed out.";
88        return Calamares::JobResult::error( tr( "Internal error in install-tracking." ),
89                                            tr( "HTTP request timed out." ) );
90    }
91    timeout.stop();
92
93    return Calamares::JobResult::ok();
94}
95
96void TrackingInstallJob::dataIsHere( QNetworkReply* reply )
97{
98    cDebug() << "Installation feedback request OK";
99    reply->deleteLater();
100}
101
102QString TrackingMachineNeonJob::prettyName() const
103{
104    return tr( "Machine feedback" );
105}
106
107QString TrackingMachineNeonJob::prettyDescription() const
108{
109    return prettyName();
110}
111
112QString TrackingMachineNeonJob::prettyStatusMessage() const
113{
114    return tr( "Configuring machine feedback." );
115}
116
117Calamares::JobResult TrackingMachineNeonJob::exec()
118{
119    int r = CalamaresUtils::System::instance()->targetEnvCall(
120        "/bin/sh",
121        QString(),  // Working dir
122        QString(
123R"x(MACHINE_ID=`cat /etc/machine-id`
124sed -i "s,URI =.*,URI = http://releases.neon.kde.org/meta-release/${MACHINE_ID}," /etc/update-manager/meta-release
125sed -i "s,URI_LTS =.*,URI_LTS = http://releases.neon.kde.org/meta-release-lts/${MACHINE_ID}," /etc/update-manager/meta-release
126true
127)x"),
128        1);
129
130    if ( r == 0 )
131        return Calamares::JobResult::ok();
132    else if ( r > 0 )
133        return Calamares::JobResult::error( tr( "Error in machine feedback configuration." ),
134                                            tr( "Could not configure machine feedback correctly, script error %1." ).arg( r ) );
135    else
136        return Calamares::JobResult::error( tr( "Error in machine feedback configuration." ),
137                                            tr( "Could not configure machine feedback correctly, Calamares error %1." ).arg( r ) );
138}
Note: See TracBrowser for help on using the repository browser.