1
/*  This file is part of the KDE project.
2
3
Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4
5
This library is free software: you can redistribute it and/or modify
6
it under the terms of the GNU Lesser General Public License as published by
7
the Free Software Foundation, either version 2.1 or 3 of the License.
8
9
This library is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
GNU Lesser General Public License for more details.
13
14
You should have received a copy of the GNU Lesser General Public License
15
along with this library.  If not, see <http://www.gnu.org/licenses/>.
16
17
*/
18
19
#include "download.h"
20
#include "utils.h"
21
#include <QtCore/QDir>
22
#include <QtCore/private/qcore_symbian_p.h>
23
#include <mmf/common/mmfcontrollerframeworkbase.h>
24
25
QT_BEGIN_NAMESPACE
26
27
using namespace Phonon;
28
using namespace Phonon::MMF;
29
30
static const TBool InheritDownloads = EFalse;
31
32
DownloadPrivate::DownloadPrivate(Download *parent)
33
    :   QObject(parent)
34
    ,   m_parent(parent)
35
    ,   m_download(0)
36
    ,   m_length(0)
37
{
38
39
}
40
41
DownloadPrivate::~DownloadPrivate()
42
{
43
    if (m_download)
44
        m_download->Delete();
45
    m_downloadManager.Disconnect();
46
    m_downloadManager.Close();
47
}
48
49
bool DownloadPrivate::start(int iap)
50
{
51
    TRACE_CONTEXT(DownloadPrivate::start, EVideoApi);
52
    Q_ASSERT(!m_download);
53
    // Connect to download manager
54
    RProcess process;
55
    const TUid uid3 = process.SecureId();
56
    TRAPD(err, m_downloadManager.ConnectL(uid3, *this, InheritDownloads));
57
    TRACE("connect err %d", err);
58
    if (KErrNone == err) {
59
        // Start download
60
        if (KUseDefaultIap != iap)
61
            m_downloadManager.SetIntAttribute(EDlMgrIap, iap);
62
        QHBufC url(m_parent->sourceUrl().toString());
63
        TPtr8 url8 = url->Des().Collapse();
64
        TRAP(err, m_download = &m_downloadManager.CreateDownloadL(url8));
65
        TRACE("start err %d", err);
66
        if (KErrNone == err)
67
            m_download->Start();
68
    }
69
    return (KErrNone == err);
70
}
71
72
void DownloadPrivate::resume()
73
{
74
75
}
76
77
void DownloadPrivate::HandleDMgrEventL(RHttpDownload &aDownload, THttpDownloadEvent aEvent)
78
{
79
    TRACE_CONTEXT(DownloadPrivate::HandleDMgrEventL, EVideoApi);
80
    Q_ASSERT(&aDownload == m_download);
81
    switch (aEvent.iDownloadState) {
82
    case EHttpDlPaused:
83
        if (EHttpContentTypeReceived == aEvent.iProgressState) {
84
            TRACE_0("paused, content type received");
85
            m_download->Start();
86
        }
87
        break;
88
    case EHttpDlInprogress:
89
        switch (aEvent.iProgressState) {
90
        case EHttpProgResponseHeaderReceived:
91
            {
92
            TFileName fileName;
93
            m_download->GetStringAttribute(EDlAttrDestFilename, fileName);
94
            TRACE("in progress, response header received, filename %S", &fileName);
95
            const QString fileNameQt = QDir::fromNativeSeparators(qt_TDesC2QString(fileName));
96
            m_parent->downloadStarted(fileNameQt);
97
            }
98
            break;
99
        case EHttpProgResponseBodyReceived:
100
            {
101
            TInt32 length = 0;
102
            m_download->GetIntAttribute(EDlAttrDownloadedSize, length);
103
            if (length != m_length) {
104
                TRACE("in progress, length %d", length);
105
                m_length = length;
106
                emit lengthChanged(m_length);
107
            }
108
            }
109
            break;
110
        }
111
        break;
112
    case EHttpDlCompleted:
113
        TRACE_0("complete");
114
        m_parent->complete();
115
        break;
116
    case EHttpDlFailed:
117
        TRACE_0("failed");
118
        m_parent->error();
119
        break;
120
    }
121
}
122
123
Download::Download(const QUrl &url, QObject *parent)
124
    :   QObject(parent)
125
    ,   m_private(new DownloadPrivate(this))
126
    ,   m_sourceUrl(url)
127
    ,   m_state(Idle)
128
{
129
    qRegisterMetaType<Download::State>();
130
    connect(m_private, SIGNAL(lengthChanged(qint64)), this, SIGNAL(lengthChanged(qint64)));
131
}
132
133
Download::~Download()
134
{
135
136
}
137
138
const QUrl &Download::sourceUrl() const
139
{
140
    return m_sourceUrl;
141
}
142
143
const QString &Download::targetFileName() const
144
{
145
    return m_targetFileName;
146
}
147
148
void Download::start(int iap)
149
{
150
    TRACE_CONTEXT(Download::start, EVideoApi);
151
    TRACE_ENTRY_0();
152
    Q_ASSERT(Idle == m_state);
153
    const bool ok = m_private->start(iap);
154
    setState(ok ? Initializing : Error);
155
    TRACE_EXIT_0();
156
}
157
158
void Download::resume()
159
{
160
    TRACE_CONTEXT(Download::resume, EVideoApi);
161
    TRACE_ENTRY_0();
162
    m_private->resume();
163
    TRACE_EXIT_0();
164
}
165
166
void Download::setState(State state)
167
{
168
    TRACE_CONTEXT(Download::setState, EVideoApi);
169
    TRACE("oldState %d newState %d", m_state, state);
170
    const State oldState = m_state;
171
    m_state = state;
172
    if (oldState != m_state)
173
        emit stateChanged(m_state);
174
}
175
176
void Download::error()
177
{
178
    TRACE_CONTEXT(Download::error, EVideoApi);
179
    TRACE_0("");
180
    setState(Error);
181
}
182
183
void Download::downloadStarted(const QString &targetFileName)
184
{
185
    TRACE_CONTEXT(Download::downloadStarted, EVideoApi);
186
    TRACE_0("downloadStarted");
187
    m_targetFileName = targetFileName;
188
    setState(Downloading);
189
}
190
191
void Download::complete()
192
{
193
    TRACE_CONTEXT(Download::complete, EVideoApi);
194
    TRACE_0("");
195
    setState(Complete);
196
}
197
198
QT_END_NAMESPACE