~ubuntu-branches/ubuntu/trusty/digikam/trusty

« back to all changes in this revision

Viewing changes to extra/kipi-plugins/dlnaexport/extra/hupnp_av/src/mediaserver/hconnectionmanager_sourceservice_p.cpp

  • Committer: Package Import Robot
  • Author(s): Rohan Garg
  • Date: 2012-11-26 18:24:20 UTC
  • mfrom: (1.9.1) (3.1.23 experimental)
  • Revision ID: package-import@ubuntu.com-20121126182420-qoy6z0nx4ai0wzcl
Tags: 4:3.0.0~beta3-0ubuntu1
* New upstream release
  - Add build-deps :  libhupnp-dev, libqtgstreamer-dev, libmagickcore-dev
* Merge from debian, remaining changes:
  - Make sure libqt4-opengl-dev, libgl1-mesa-dev and libglu1-mesa-dev only
    install on i386,amd64 and powerpc
  - Depend on libtiff-dev instead of libtiff4-dev
  - Drop digikam breaks/replaces kipi-plugins-common since we're past the
    LTS release now
  - digikam to recommend mplayerthumbs | ffmpegthumbs. We currently only
    have latter in the archives, even though former is also supposed to
    be part of kdemultimedia. (LP: #890059)
  - kipi-plugins to recommend www-browser rather than konqueror directly
    since 2.8 no direct usage of konqueror is present in the flickr
    plugin anymore (LP: #1011211)
  - Keep kubuntu_mysqld_executable_name.diff
  - Don't install libkipi translations
  - Keep deps on libcv-dev, libcvaux-dev
  - Keep split packaging of libraries
  - Replace icons from KDE 3 time in debian/xpm.d/*.xpm with the new
    versions (LP: #658047)
* Update debian/not-installed

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  Copyright (C) 2011 Tuomo Penttinen, all rights reserved.
 
3
 *
 
4
 *  Author: Tuomo Penttinen <tp@herqq.org>
 
5
 *
 
6
 *  This file is part of Herqq UPnP Av (HUPnPAv) library.
 
7
 *
 
8
 *  Herqq UPnP Av is free software: you can redistribute it and/or modify
 
9
 *  it under the terms of the GNU General Public License as published by
 
10
 *  the Free Software Foundation, either version 3 of the License, or
 
11
 *  (at your option) any later version.
 
12
 *
 
13
 *  Herqq UPnP Av is distributed in the hope that it will be useful,
 
14
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 
16
 *  GNU General Public License for more details.
 
17
 *
 
18
 *  You should have received a copy of the GNU General Public License
 
19
 *  along with Herqq UPnP Av. If not, see <http://www.gnu.org/licenses/>.
 
20
 */
 
21
 
 
22
#include "hconnectionmanager_sourceservice_p.h"
 
23
 
 
24
#include "../connectionmanager/hconnectioninfo.h"
 
25
#include "../connectionmanager/hconnectionmanager_service_p.h"
 
26
 
 
27
#include "../hav_global_p.h"
 
28
#include "../common/hresource.h"
 
29
#include "../common/hprotocolinfo.h"
 
30
#include "../cds_model/cds_objects/hitem.h"
 
31
#include "../cds_model/cds_objects/hcontainer.h"
 
32
#include "../cds_model/datasource/hcds_datasource.h"
 
33
 
 
34
#include <HUpnpCore/private/hlogger_p.h>
 
35
#include <HUpnpCore/private/hhttp_server_p.h>
 
36
#include <HUpnpCore/private/hhttp_header_p.h>
 
37
#include <HUpnpCore/private/hhttp_messagecreator_p.h>
 
38
 
 
39
#include <QtNetwork/QTcpSocket>
 
40
 
 
41
namespace Herqq
 
42
{
 
43
 
 
44
namespace Upnp
 
45
{
 
46
 
 
47
namespace Av
 
48
{
 
49
 
 
50
HHttpStreamer::HHttpStreamer(
 
51
    HMessagingInfo* mi, const QByteArray& header, QIODevice* data, QObject* parent) :
 
52
        QObject(parent),
 
53
            m_bufSize(1024*64), m_buf(new char[m_bufSize]), m_dataToSend(data),
 
54
            m_mi(mi), m_header(header), m_read(0), m_written(0)
 
55
{
 
56
    bool ok = connect(
 
57
        &m_mi->socket(), SIGNAL(bytesWritten(qint64)),
 
58
        this, SLOT(bytesWritten(qint64)));
 
59
    Q_ASSERT(ok); Q_UNUSED(ok)
 
60
}
 
61
 
 
62
HHttpStreamer::~HHttpStreamer()
 
63
{
 
64
    delete m_mi;
 
65
    delete m_dataToSend;
 
66
    delete m_buf;
 
67
}
 
68
 
 
69
void HHttpStreamer::bytesWritten(qint64 written)
 
70
{
 
71
    HLOG(H_AT, H_FUN);
 
72
 
 
73
    if (m_dataToSend->atEnd())
 
74
    {
 
75
        deleteLater();
 
76
        return;
 
77
    }
 
78
    else if (m_dataToSend->pos() > 0)
 
79
    {
 
80
        // We've past the HTTP header.
 
81
        m_written += written;
 
82
    }
 
83
 
 
84
    if (m_written >= m_read)
 
85
    {
 
86
        m_read = m_dataToSend->read(m_buf, m_bufSize);
 
87
        if (m_read <= 0)
 
88
        {
 
89
            HLOG_WARN(QString("Failed to read data from the data source: [%1]").arg(
 
90
                m_dataToSend->errorString()));
 
91
 
 
92
            deleteLater();
 
93
            return;
 
94
        }
 
95
 
 
96
        m_written = 0;
 
97
    }
 
98
 
 
99
    written = m_mi->socket().write(m_buf + m_written, m_read - m_written);
 
100
    if (written == -1)
 
101
    {
 
102
        HLOG_WARN(QString("Failed to send data: %1").arg(m_mi->socket().errorString()));
 
103
        deleteLater();
 
104
    }
 
105
}
 
106
 
 
107
void HHttpStreamer::send()
 
108
{
 
109
    HLOG(H_AT, H_FUN);
 
110
    Q_ASSERT(m_dataToSend->pos() == 0);
 
111
 
 
112
    qint64 wrote = m_mi->socket().write(m_header);
 
113
    if (wrote < m_header.size())
 
114
    {
 
115
        HLOG_WARN(QString(
 
116
            "Failed to send HTTP header to the destination: [%1]. "
 
117
            "Aborting data transfer.").arg(m_mi->socket().errorString()));
 
118
 
 
119
        deleteLater();
 
120
    }
 
121
}
 
122
 
 
123
/*******************************************************************************
 
124
 * HConnectionManagerHttpServer
 
125
 ******************************************************************************/
 
126
HConnectionManagerHttpServer::HConnectionManagerHttpServer(
 
127
    const QByteArray& loggingId, HConnectionManagerSourceService* owner) :
 
128
        HHttpServer(loggingId, owner), m_owner(owner)
 
129
{
 
130
    Q_ASSERT(owner);
 
131
}
 
132
 
 
133
HConnectionManagerHttpServer::~HConnectionManagerHttpServer()
 
134
{
 
135
}
 
136
 
 
137
void HConnectionManagerHttpServer::incomingUnknownGetRequest(
 
138
    HMessagingInfo* mi, const HHttpRequestHeader& hdr)
 
139
{
 
140
    HLOG2(H_AT, H_FUN, m_loggingIdentifier);
 
141
 
 
142
    QScopedPointer<QIODevice> dev(
 
143
        m_owner->m_dataSource->loadItemData(hdr.path().remove('/')));
 
144
 
 
145
    if (dev)
 
146
    {
 
147
        if (dev->isSequential())
 
148
        {
 
149
            // TODO send in chunks
 
150
            Q_ASSERT_X(false, "", "Currently sequential data sources are not supported");
 
151
        }
 
152
        else if (dev->size() < maxBytesToLoad())
 
153
        {
 
154
            QByteArray data = dev->readAll();
 
155
            mi->setKeepAlive(true);
 
156
            m_httpHandler->send(mi, HHttpMessageCreator::createResponse(Ok, *mi, data));
 
157
        }
 
158
        else
 
159
        {
 
160
            HHttpStreamer* streamer =
 
161
                new HHttpStreamer(mi, HHttpMessageCreator::createHeaderData(
 
162
                    Ok, *mi, dev->size()), dev.take(), this);
 
163
 
 
164
            streamer->send();
 
165
        }
 
166
    }
 
167
    else
 
168
    {
 
169
        mi->setKeepAlive(true);
 
170
        m_httpHandler->send(mi, HHttpMessageCreator::createResponse(BadRequest, *mi));
 
171
    }
 
172
}
 
173
 
 
174
/*******************************************************************************
 
175
 * HConnectionManagerSourceService
 
176
 ******************************************************************************/
 
177
HConnectionManagerSourceService::HConnectionManagerSourceService(
 
178
    HAbstractCdsDataSource* dataSource) :
 
179
        HConnectionManagerService(),
 
180
            m_dataSource(0),
 
181
            m_httpServer(new HConnectionManagerHttpServer(
 
182
                h_ptr->m_loggingIdentifier, this))
 
183
{
 
184
    Q_ASSERT_X(dataSource, "", "Valid HCdsDataSource has to be provided");
 
185
    m_dataSource = dataSource;
 
186
}
 
187
 
 
188
HConnectionManagerSourceService::~HConnectionManagerSourceService()
 
189
{
 
190
    HLOG2(H_AT, H_FUN, h_ptr->m_loggingIdentifier);
 
191
    delete m_httpServer;
 
192
}
 
193
 
 
194
bool HConnectionManagerSourceService::finalizeInit(QString* /*errDescription*/)
 
195
{
 
196
    setSourceProtocolInfo(HProtocolInfo("http-get:*:*:*"));
 
197
    createDefaultConnection(sourceProtocolInfo().at(0));
 
198
 
 
199
    bool ok = connect(
 
200
        m_dataSource, SIGNAL(objectModified(Herqq::Upnp::Av::HObject*, Herqq::Upnp::Av::HObjectEventInfo)),
 
201
        this, SLOT(objectModified(Herqq::Upnp::Av::HObject*, Herqq::Upnp::Av::HObjectEventInfo)));
 
202
    Q_ASSERT(ok); Q_UNUSED(ok)
 
203
 
 
204
    ok = connect(
 
205
        m_dataSource, SIGNAL(containerModified(Herqq::Upnp::Av::HContainer*, Herqq::Upnp::Av::HContainerEventInfo)),
 
206
        this, SLOT(containerModified(Herqq::Upnp::Av::HContainer*, Herqq::Upnp::Av::HContainerEventInfo)));
 
207
    Q_ASSERT(ok);
 
208
 
 
209
    return true;
 
210
}
 
211
 
 
212
void HConnectionManagerSourceService::objectModified(
 
213
    HObject* source, const HObjectEventInfo& eventInfo)
 
214
{
 
215
    Q_UNUSED(source)
 
216
    Q_UNUSED(eventInfo)
 
217
}
 
218
 
 
219
void HConnectionManagerSourceService::containerModified(
 
220
    HContainer* source, const HContainerEventInfo& eventInfo)
 
221
{
 
222
    Q_UNUSED(source)
 
223
    if (eventInfo.type() == HContainerEventInfo::ChildAdded)
 
224
    {
 
225
        HItem* item = m_dataSource->findItem(eventInfo.childId());
 
226
        if (item)
 
227
        {
 
228
            addLocation(item);
 
229
        }
 
230
    }
 
231
}
 
232
 
 
233
void HConnectionManagerSourceService::addLocation(HItem* item)
 
234
{
 
235
    QList<QUrl> rootUrls = m_httpServer->rootUrls();
 
236
    Q_ASSERT(!rootUrls.isEmpty());
 
237
 
 
238
    HResources resources = item->resources();
 
239
 
 
240
    if (resources.size())
 
241
    {
 
242
        bool added = false;
 
243
        int urlsIndex = 0;
 
244
        for(int i = 0; i < resources.size(); ++i)
 
245
        {
 
246
            if (resources[i].location().isEmpty())
 
247
            {
 
248
                QString rootUrl =
 
249
                    rootUrls[urlsIndex++ % rootUrls.size()].
 
250
                        toString().append('/').append(item->id());
 
251
 
 
252
                resources[i].setLocation(rootUrl);
 
253
 
 
254
                HProtocolInfo pi = resources[i].protocolInfo();
 
255
                pi.setProtocol("http-get");
 
256
 
 
257
                resources[i].setProtocolInfo(pi);
 
258
 
 
259
                added = true;
 
260
            }
 
261
        }
 
262
        if (!added)
 
263
        {
 
264
            HProtocolInfo pi = resources[0].protocolInfo();
 
265
            pi.setProtocol("http-get");
 
266
 
 
267
            HResource res(
 
268
                rootUrls[0].toString().append('/').append(item->id()), pi);
 
269
 
 
270
            resources.append(res);
 
271
        }
 
272
    }
 
273
    else
 
274
    {
 
275
        foreach(const QUrl& rootUrl, rootUrls)
 
276
        {
 
277
            QUrl location(rootUrl.toString().append('/').append(item->id()));
 
278
            HResource resource(location, sourceProtocolInfo()[0]);
 
279
            resources.append(resource);
 
280
        }
 
281
    }
 
282
 
 
283
    item->setResources(resources);
 
284
}
 
285
 
 
286
bool HConnectionManagerSourceService::init()
 
287
{
 
288
    HLOG2(H_AT, H_FUN, h_ptr->m_loggingIdentifier);
 
289
 
 
290
    if (!m_httpServer->init())
 
291
    {
 
292
        HLOG_WARN("Failed to initialize HTTP server");
 
293
        return false;
 
294
    }
 
295
    else if (m_httpServer->rootUrls().isEmpty())
 
296
    {
 
297
        return false;
 
298
    }
 
299
 
 
300
    HItems items = m_dataSource->items();
 
301
    foreach(HItem* item, items)
 
302
    {
 
303
        addLocation(item);
 
304
    }
 
305
 
 
306
    return true;
 
307
}
 
308
 
 
309
bool HConnectionManagerSourceService::isInitialized() const
 
310
{
 
311
    return m_httpServer->isInitialized();
 
312
}
 
313
 
 
314
}
 
315
}
 
316
}