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

« back to all changes in this revision

Viewing changes to extra/kipi-plugins/dlnaexport/extra/avtest/controlpoint_navigator.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 an application named HUpnpAvSimpleTestApp
 
7
 *  used for demonstrating how to use the Herqq UPnP A/V (HUPnPAv) library.
 
8
 *
 
9
 *  HUpnpAvSimpleTestApp is free software: you can redistribute it and/or modify
 
10
 *  it under the terms of the GNU General Public License as published by
 
11
 *  the Free Software Foundation, either version 3 of the License, or
 
12
 *  (at your option) any later version.
 
13
 *
 
14
 *  HUpnpAvSimpleTestApp is distributed in the hope that it will be useful,
 
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 
17
 *  GNU General Public License for more details.
 
18
 *
 
19
 *  You should have received a copy of the GNU General Public License
 
20
 *  along with HUpnpAvSimpleTestApp. If not, see <http://www.gnu.org/licenses/>.
 
21
 */
 
22
 
 
23
#include "controlpoint_navigator.h"
 
24
#include "controlpoint_navigatoritem.h"
 
25
 
 
26
#include <HUpnpAv/HContainer>
 
27
#include <HUpnpAv/HProtocolInfo>
 
28
#include <HUpnpAv/HMediaBrowser>
 
29
#include <HUpnpAv/HCdsDataSource>
 
30
#include <HUpnpAv/HMediaServerAdapter>
 
31
#include <HUpnpAv/HMediaRendererAdapter>
 
32
#include <HUpnpAv/HContentDirectoryAdapter>
 
33
 
 
34
#include <HUpnpCore/HClientService>
 
35
 
 
36
#include <QSet>
 
37
#include <QVariant>
 
38
#include <QEventLoop>
 
39
 
 
40
using namespace Herqq::Upnp;
 
41
using namespace Herqq::Upnp::Av;
 
42
 
 
43
ControlPointNavigator::ControlPointNavigator(QObject* parent) :
 
44
    QAbstractItemModel(parent),
 
45
        m_rootItem(new RootItem()), m_renderersItem(), m_serversItem()
 
46
{
 
47
    m_renderersItem = new ContainerItem("Media Renderers", m_rootItem);
 
48
    m_rootItem->appendChild(m_renderersItem);
 
49
 
 
50
    m_serversItem = new ContainerItem("Media Servers", m_rootItem);
 
51
    m_rootItem->appendChild(m_serversItem);
 
52
}
 
53
 
 
54
ControlPointNavigator::~ControlPointNavigator()
 
55
{
 
56
    delete m_rootItem;
 
57
}
 
58
 
 
59
void ControlPointNavigator::mediaRendererOnline(HMediaRendererAdapter* mediaRenderer)
 
60
{
 
61
    HClientAdapterOp<qint32> op = mediaRenderer->getCurrentConnections();
 
62
    if (op.isNull())
 
63
    {
 
64
        return;
 
65
    }
 
66
 
 
67
    bool ok = connect(
 
68
        mediaRenderer,
 
69
        SIGNAL(connectionReady(Herqq::Upnp::Av::HMediaRendererAdapter*, qint32)),
 
70
        this,
 
71
        SLOT(connectionReady(Herqq::Upnp::Av::HMediaRendererAdapter*, qint32)));
 
72
    Q_ASSERT(ok); Q_UNUSED(ok)
 
73
 
 
74
    ok = connect(
 
75
        mediaRenderer,
 
76
        SIGNAL(error(Herqq::Upnp::Av::HMediaRendererAdapter*,
 
77
                     Herqq::Upnp::HClientAdapterOp<qint32>)),
 
78
        this,
 
79
        SLOT(error(Herqq::Upnp::Av::HMediaRendererAdapter*,
 
80
                   Herqq::Upnp::HClientAdapterOp<qint32>)));
 
81
    Q_ASSERT(ok);
 
82
 
 
83
    RendererItem* rendererItem = new RendererItem(mediaRenderer, m_renderersItem);
 
84
 
 
85
    beginInsertRows(
 
86
        QModelIndex(), m_rootItem->childCount(), m_rootItem->childCount());
 
87
 
 
88
    m_renderersItem->appendChild(rendererItem);
 
89
 
 
90
    endInsertRows();
 
91
}
 
92
 
 
93
void ControlPointNavigator::error(Herqq::Upnp::Av::HMediaRendererAdapter*,
 
94
    const Herqq::Upnp::HClientAdapterOp<qint32>& op)
 
95
{
 
96
    Q_UNUSED(op)
 
97
}
 
98
 
 
99
void ControlPointNavigator::connectionReady(HMediaRendererAdapter* source, qint32 id)
 
100
{
 
101
    RendererItem* parentItem = 0;
 
102
    for(qint32 i = 0; i < m_renderersItem->childCount(); ++i)
 
103
    {
 
104
        RendererItem* deviceItem =
 
105
            static_cast<RendererItem*>(m_renderersItem->child(i));
 
106
 
 
107
        if (deviceItem->renderer() == source)
 
108
        {
 
109
            parentItem = deviceItem;
 
110
            break;
 
111
        }
 
112
    }
 
113
 
 
114
    if (parentItem)
 
115
    {
 
116
        HConnection* connection = source->connection(id);
 
117
        ConnectionItem* connectionItem = new ConnectionItem(connection, parentItem);
 
118
 
 
119
        bool ok = connect(
 
120
            connection, SIGNAL(destroyed(QObject*)),
 
121
            this, SLOT(connectionDestroyed(QObject*)));
 
122
        Q_ASSERT(ok); Q_UNUSED(ok)
 
123
 
 
124
        beginInsertRows(
 
125
            QModelIndex(), m_rootItem->childCount(), m_rootItem->childCount());
 
126
 
 
127
        parentItem->appendChild(connectionItem);
 
128
 
 
129
        endInsertRows();
 
130
    }
 
131
}
 
132
 
 
133
void ControlPointNavigator::connectionDestroyed(QObject* conn)
 
134
{
 
135
    for(qint32 i = 0; i < m_renderersItem->childCount(); ++i)
 
136
    {
 
137
        RendererItem* rendererItem =
 
138
            static_cast<RendererItem*>(m_renderersItem->child(i));
 
139
 
 
140
        for (int j = 0; j < rendererItem->childCount(); ++j)
 
141
        {
 
142
            ConnectionItem* conItem =
 
143
                static_cast<ConnectionItem*>(rendererItem->child(j));
 
144
 
 
145
            if (!conItem->connection() || conItem->connection() == conn)
 
146
            {
 
147
                beginRemoveRows(index(j, 0, renderersIndex()), j, j);
 
148
                rendererItem->removeChild(j);
 
149
                endRemoveRows();
 
150
                return;
 
151
            }
 
152
        }
 
153
    }
 
154
}
 
155
 
 
156
void ControlPointNavigator::mediaRendererOffline(HMediaRendererAdapter* device)
 
157
{
 
158
    for(qint32 i = 0; i < m_renderersItem->childCount(); ++i)
 
159
    {
 
160
        RendererItem* rendererItem =
 
161
            static_cast<RendererItem*>(m_renderersItem->child(i));
 
162
 
 
163
        if (rendererItem->renderer() == device)
 
164
        {
 
165
            beginRemoveRows(renderersIndex(), i, i);
 
166
            m_renderersItem->removeChild(i);
 
167
            endRemoveRows();
 
168
            break;
 
169
        }
 
170
    }
 
171
}
 
172
 
 
173
HMediaBrowser* ControlPointNavigator::mediaServerOnline(HMediaServerAdapter* device)
 
174
{
 
175
    HMediaBrowser* browser = new HMediaBrowser(this);
 
176
    if (!browser->reset(device->contentDirectory(), false))
 
177
    {
 
178
        return 0;
 
179
    }
 
180
 
 
181
    bool ok = connect(
 
182
        browser,
 
183
        SIGNAL(objectsBrowsed(Herqq::Upnp::Av::HMediaBrowser*,QSet<QString>)),
 
184
        this,
 
185
        SLOT(objectsBrowsed(Herqq::Upnp::Av::HMediaBrowser*,QSet<QString>)));
 
186
    Q_ASSERT(ok); Q_UNUSED(ok)
 
187
 
 
188
    ok = connect(
 
189
        browser,
 
190
        SIGNAL(browseFailed(Herqq::Upnp::Av::HMediaBrowser*)),
 
191
        this,
 
192
        SLOT(browseFailed(Herqq::Upnp::Av::HMediaBrowser*)));
 
193
    Q_ASSERT(ok); Q_UNUSED(ok)
 
194
 
 
195
    if (!browser->browseRoot())
 
196
    {
 
197
        delete browser; browser = 0;
 
198
    }
 
199
    else
 
200
    {
 
201
        ContentDirectoryItem* cdItem =
 
202
            new ContentDirectoryItem(browser, m_serversItem);
 
203
 
 
204
        beginInsertRows(QModelIndex(), m_rootItem->childCount(), m_rootItem->childCount());
 
205
        m_serversItem->appendChild(cdItem);
 
206
        endInsertRows();
 
207
    }
 
208
 
 
209
    return browser;
 
210
}
 
211
 
 
212
CdsContainerItem* ControlPointNavigator::findParentContainer(
 
213
    CdsContainerItem* parent, const QString& id)
 
214
{
 
215
    for (int i = 0; i < parent->childCount(); ++i)
 
216
    {
 
217
        CdsContainerItem* item = static_cast<CdsContainerItem*>(parent->child(i));
 
218
        if (item->container()->id() == id)
 
219
        {
 
220
            return item;
 
221
        }
 
222
        else
 
223
        {
 
224
            item = findParentContainer(item, id);
 
225
            if (item)
 
226
            {
 
227
                return item;
 
228
            }
 
229
        }
 
230
    }
 
231
    return 0;
 
232
}
 
233
 
 
234
namespace
 
235
{
 
236
bool hasContainerItem(const CdsContainerItem* item, const HContainer* container)
 
237
{
 
238
    for(int i = 0; i < item->childCount(); ++i)
 
239
    {
 
240
        const CdsContainerItem* cdsContainerItem =
 
241
            dynamic_cast<const CdsContainerItem*>(item->child(i));
 
242
 
 
243
        if (cdsContainerItem && cdsContainerItem->container() == container)
 
244
        {
 
245
            return true;
 
246
        }
 
247
        else if (hasContainerItem(cdsContainerItem, container))
 
248
        {
 
249
            return true;
 
250
        }
 
251
    }
 
252
    return false;
 
253
}
 
254
}
 
255
 
 
256
void ControlPointNavigator::objectsBrowsed(
 
257
    HMediaBrowser* browser, const QSet<QString>& ids)
 
258
{
 
259
    HCdsDataSource* dataSource = browser->dataSource();
 
260
 
 
261
    ContentDirectoryItem* cdsItem = 0;
 
262
    for(int i = 0; i < m_serversItem->childCount(); ++i)
 
263
    {
 
264
        cdsItem = static_cast<ContentDirectoryItem*>(m_serversItem->child(i));
 
265
        if (cdsItem->browser() == browser)
 
266
        {
 
267
            break;
 
268
        }
 
269
    }
 
270
    Q_ASSERT(cdsItem);
 
271
 
 
272
    foreach(const QString& id, ids)
 
273
    {
 
274
        HContainer* container = dataSource->findContainer(id);
 
275
        if (!container)
 
276
        {
 
277
            continue;
 
278
        }
 
279
 
 
280
        if (id != "0")
 
281
        {
 
282
            CdsContainerItem* rootItem =
 
283
                static_cast<CdsContainerItem*>(cdsItem->child(0));
 
284
 
 
285
            if (hasContainerItem(rootItem, container))
 
286
            {
 
287
                continue;
 
288
            }
 
289
 
 
290
            if ("0" == container->parentId())
 
291
            {
 
292
                CdsContainerItem* newContainerItem =
 
293
                    new CdsContainerItem(container, dataSource, rootItem);
 
294
 
 
295
                rootItem->appendChild(newContainerItem);
 
296
            }
 
297
            else
 
298
            {
 
299
                CdsContainerItem* item =
 
300
                    findParentContainer(rootItem, container->parentId());
 
301
 
 
302
                if (item)
 
303
                {
 
304
                    CdsContainerItem* newContainerItem =
 
305
                        new CdsContainerItem(container, dataSource, item);
 
306
 
 
307
                    item->appendChild(newContainerItem);
 
308
                }
 
309
            }
 
310
        }
 
311
        else
 
312
        {
 
313
            CdsContainerItem* newContainerItem =
 
314
                new CdsContainerItem(container, dataSource, cdsItem);
 
315
 
 
316
            cdsItem->appendChild(newContainerItem);
 
317
        }
 
318
    }
 
319
 
 
320
    beginInsertRows(QModelIndex(), m_rootItem->childCount(), m_rootItem->childCount());
 
321
    endInsertRows();
 
322
}
 
323
 
 
324
void ControlPointNavigator::browseFailed(HMediaBrowser*)
 
325
{
 
326
}
 
327
 
 
328
void ControlPointNavigator::mediaServerOffline(HMediaServerAdapter* device)
 
329
{
 
330
    for(qint32 i = 0; i < m_serversItem->childCount(); ++i)
 
331
    {
 
332
        ContentDirectoryItem* cdItem =
 
333
            static_cast<ContentDirectoryItem*>(m_serversItem->child(i));
 
334
 
 
335
        if (cdItem->browser()->contentDirectory()->service()->parentDevice() == device->device())
 
336
        {
 
337
            beginRemoveRows(serversIndex(), i, i);
 
338
            m_serversItem->removeChild(i);
 
339
            endRemoveRows();
 
340
            break;
 
341
        }
 
342
    }
 
343
}
 
344
 
 
345
int ControlPointNavigator::columnCount(const QModelIndex& parent) const
 
346
{
 
347
    if (parent.isValid())
 
348
    {
 
349
        return static_cast<ControlPointNavigatorItem*>(
 
350
            parent.internalPointer())->columnCount();
 
351
    }
 
352
    else
 
353
    {
 
354
        return m_rootItem->columnCount();
 
355
    }
 
356
}
 
357
 
 
358
QVariant ControlPointNavigator::data(const QModelIndex& index, int role) const
 
359
{
 
360
    if (!index.isValid())
 
361
    {
 
362
        return QVariant();
 
363
    }
 
364
 
 
365
    if (role != Qt::DisplayRole)
 
366
    {
 
367
        return QVariant();
 
368
    }
 
369
 
 
370
    ControlPointNavigatorItem* item =
 
371
        static_cast<ControlPointNavigatorItem*>(index.internalPointer());
 
372
 
 
373
    Q_ASSERT(item);
 
374
 
 
375
    return item->data(index.column());
 
376
}
 
377
 
 
378
Qt::ItemFlags ControlPointNavigator::flags(const QModelIndex& index) const
 
379
{
 
380
    if (!index.isValid())
 
381
    {
 
382
        return 0;
 
383
    }
 
384
 
 
385
    return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
 
386
}
 
387
 
 
388
QVariant ControlPointNavigator::headerData(
 
389
    int section, Qt::Orientation orientation, int role) const
 
390
{
 
391
    if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
 
392
    {
 
393
        return m_rootItem->data(section);
 
394
    }
 
395
 
 
396
    return QVariant();
 
397
}
 
398
 
 
399
QModelIndex ControlPointNavigator::index(
 
400
    int row, int column, const QModelIndex& parent) const
 
401
{
 
402
    if (!hasIndex(row, column, parent))
 
403
    {
 
404
        return QModelIndex();
 
405
    }
 
406
 
 
407
    ControlPointNavigatorItem* parentItem = 0;
 
408
    if (!parent.isValid())
 
409
    {
 
410
        parentItem = m_rootItem;
 
411
    }
 
412
    else
 
413
    {
 
414
        parentItem =
 
415
            static_cast<ControlPointNavigatorItem*>(parent.internalPointer());
 
416
    }
 
417
    Q_ASSERT(parentItem);
 
418
 
 
419
    ControlPointNavigatorItem* childItem = parentItem->child(row);
 
420
    if (childItem)
 
421
    {
 
422
        return createIndex(row, column, childItem);
 
423
    }
 
424
    else
 
425
    {
 
426
        return QModelIndex();
 
427
    }
 
428
}
 
429
 
 
430
QModelIndex ControlPointNavigator::parent(const QModelIndex& itemIndex) const
 
431
{
 
432
    if (!itemIndex.isValid() || !itemIndex.internalPointer())
 
433
    {
 
434
        return QModelIndex();
 
435
    }
 
436
 
 
437
    ControlPointNavigatorItem* childItem  =
 
438
        static_cast<ControlPointNavigatorItem*>(itemIndex.internalPointer());
 
439
 
 
440
    ControlPointNavigatorItem* parentItem = childItem->parent();
 
441
    Q_ASSERT(parentItem);
 
442
 
 
443
    if (parentItem == m_rootItem)
 
444
    {
 
445
        return QModelIndex();
 
446
    }
 
447
 
 
448
    return createIndex(0, 0, parentItem);
 
449
}
 
450
 
 
451
int ControlPointNavigator::rowCount(const QModelIndex& parent) const
 
452
{
 
453
    ControlPointNavigatorItem* parentItem = 0;
 
454
 
 
455
    if (parent.column() > 0)
 
456
    {
 
457
        return 0;
 
458
    }
 
459
 
 
460
    if (!parent.isValid())
 
461
    {
 
462
        parentItem = m_rootItem;
 
463
    }
 
464
    else
 
465
    {
 
466
        parentItem =
 
467
            static_cast<ControlPointNavigatorItem*>(parent.internalPointer());
 
468
    }
 
469
 
 
470
    Q_ASSERT(parentItem);
 
471
 
 
472
    return parentItem->childCount();
 
473
}