~ci-train-bot/unity-scope-click/unity-scope-click-ubuntu-yakkety-landing-072

« back to all changes in this revision

Viewing changes to scope/download-manager.cpp

  • Committer: Michael McCracken
  • Date: 2014-01-13 19:14:10 UTC
  • mto: This revision was merged to the branch mainline in revision 105.
  • Revision ID: mike.mccracken@canonical.com-20140113191410-0241f3ykr7k92e1p
re-add the vala sources and autotools stuff alongside cmake and CXX new stuff

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2014 Canonical Ltd.
 
3
 *
 
4
 * This program is free software: you can redistribute it and/or modify it
 
5
 * under the terms of the GNU General Public License version 3, as published
 
6
 * by the Free Software Foundation.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful, but
 
9
 * WITHOUT ANY WARRANTY; without even the implied warranties of
 
10
 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 
11
 * PURPOSE.  See the GNU General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU General Public License along
 
14
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 *
 
16
 * In addition, as a special exception, the copyright holders give
 
17
 * permission to link the code of portions of this program with the
 
18
 * OpenSSL library under certain conditions as described in each
 
19
 * individual source file, and distribute linked combinations
 
20
 * including the two.
 
21
 * You must obey the GNU General Public License in all respects
 
22
 * for all of the code used other than OpenSSL.  If you modify
 
23
 * file(s) with this exception, you may extend this exception to your
 
24
 * version of the file(s), but you are not obligated to do so.  If you
 
25
 * do not wish to do so, delete this exception statement from your
 
26
 * version.  If you delete this exception statement from all source
 
27
 * files in the program, then also delete it here.
 
28
 */
 
29
 
 
30
#include <QDebug>
 
31
#include <QObject>
 
32
#include <QString>
 
33
#include <QTimer> 
 
34
 
 
35
#include "ssoservice.h"
 
36
#include "token.h"
 
37
 
 
38
#include "download-manager.h"
 
39
 
 
40
namespace ClickScope {
 
41
 
 
42
 
 
43
DownloadManager::DownloadManager(QObject *parent) :
 
44
    QObject(parent)
 
45
{
 
46
    QObject::connect(&service, SIGNAL(credentialsFound(const Token&)),
 
47
                     this, SLOT(handleCredentialsFound(Token)));
 
48
    QObject::connect(&service, SIGNAL(credentialsNotFound()),
 
49
                     this, SLOT(handleCredentialsNotFound()));
 
50
    QObject::connect(&nam, SIGNAL(finished(QNetworkReply*)),
 
51
                     this, SLOT(handleNetworkFinished(QNetworkReply*)));
 
52
 
 
53
}
 
54
 
 
55
DownloadManager::~DownloadManager(){
 
56
}
 
57
 
 
58
void DownloadManager::fetchClickToken(QString downloadUrl)
 
59
{
 
60
    service.getCredentials();
 
61
    _downloadUrl = downloadUrl;
 
62
}
 
63
 
 
64
void DownloadManager::handleCredentialsFound(UbuntuOne::Token token)
 
65
{
 
66
    qDebug() << "Credentials found, signing url " << _downloadUrl;
 
67
 
 
68
    QString authHeader = token.signUrl(_downloadUrl, QStringLiteral("HEAD"));
 
69
 
 
70
    qDebug() << "URL Signed, authHeader is:" << authHeader; // TODO: remove this log
 
71
 
 
72
    QNetworkRequest req;
 
73
    req.setRawHeader(QStringLiteral("Authorization").toUtf8(),
 
74
                     authHeader.toUtf8());
 
75
    req.setUrl(_downloadUrl);
 
76
    nam.get(req);
 
77
}
 
78
 
 
79
void DownloadManager::handleCredentialsNotFound()
 
80
{
 
81
    qDebug() << "No credentials were found.";
 
82
    emit clickTokenFetchError(QString("No creds found"));
 
83
}
 
84
 
 
85
void DownloadManager::handleNetworkFinished(QNetworkReply *reply)
 
86
{
 
87
    // TODO: actually get the header
 
88
    QString clickTokenHeader;
 
89
    QVariant statusAttr = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
 
90
    if(!statusAttr.isValid()) {
 
91
        qDebug() << "Invalid HTTP response.";
 
92
        return;
 
93
    }
 
94
 
 
95
    int status = statusAttr.toInt();
 
96
    qDebug() << "HTTP Status " << status;
 
97
 
 
98
    if (status != 200){
 
99
        qDebug() << reply->rawHeaderPairs();
 
100
    }
 
101
 
 
102
    qDebug() << reply->readAll();
 
103
        
 
104
    emit clickTokenFetched(clickTokenHeader);
 
105
}
 
106
 
 
107
} // namespace ClickScope