1 | /* ============================================================ |
2 | * |
3 | * This file is a part of kipi-plugins project |
4 | * http://www.digikam.org |
5 | * |
6 | * Date : 2012-05-28 |
7 | * Description : a KIPI plugin to export pics through DLNA technology. |
8 | * |
9 | * Copyright (C) 2012 by Smit Mehta <smit dot meh at gmail dot com> |
10 | * Copyright (C) 2011 by Tuomo Penttinen <tp at herqq dot org> |
11 | * |
12 | * This program is free software; you can redistribute it |
13 | * and/or modify it under the terms of the GNU General |
14 | * Public License as published by the Free Software Foundation; |
15 | * either version 2, or (at your option) any later version. |
16 | * |
17 | * This program is distributed in the hope that it will be useful, |
18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
20 | * GNU General Public License for more details. |
21 | * |
22 | * ============================================================ */ |
23 | |
24 | #include "hupnpmediaserver.moc" |
25 | |
26 | // KDE includes |
27 | |
28 | #include <kstandarddirs.h> |
29 | #include <kdebug.h> |
30 | #include <kurl.h> |
31 | |
32 | // libHUpnp includes |
33 | |
34 | #include <HUpnpCore/HUpnp> |
35 | #include <HUpnpCore/HDeviceInfo> |
36 | #include <HUpnpCore/HDeviceHost> |
37 | #include <HUpnpCore/HDeviceHostConfiguration> |
38 | #include <HUpnpAv/HUpnpAv> |
39 | #include <HUpnpAv/HRootDir> |
40 | #include <HUpnpAv/HImageItem> |
41 | #include <HUpnpAv/HPhoto> |
42 | #include <HUpnpAv/HContainer> |
43 | #include <HUpnpAv/HAvDeviceModelCreator> |
44 | #include <HUpnpAv/HMediaServerDeviceConfiguration> |
45 | #include <HUpnpAv/HFileSystemDataSource> |
46 | #include <HUpnpAv/HContentDirectoryServiceConfiguration> |
47 | |
48 | // Qt includes |
49 | |
50 | #include <QList> |
51 | #include <QFile> |
52 | |
53 | using namespace Herqq::Upnp; |
54 | using namespace Herqq::Upnp::Av; |
55 | |
56 | namespace KIPIDLNAExportPlugin |
57 | { |
58 | |
59 | class MediaServer::Private |
60 | { |
61 | public: |
62 | |
63 | Private() |
64 | { |
65 | deviceHost = 0; |
66 | datasource = 0; |
67 | } |
68 | |
69 | HDeviceHost* deviceHost; |
70 | HFileSystemDataSource* datasource; |
71 | }; |
72 | |
73 | MediaServer::MediaServer(QObject* const parent) |
74 | : QObject(parent), d(new Private) |
75 | { |
76 | // Configure a data source |
77 | HFileSystemDataSourceConfiguration datasourceConfig; |
78 | |
79 | // Here you could configure the data source in more detail if needed. For example, |
80 | // you could add "root directories" to the configuration and the data source |
81 | // would scan those directories for media content upon initialization. |
82 | d->datasource = new HFileSystemDataSource(datasourceConfig); |
83 | |
84 | // Configure ContentDirectoryService by providing it access to the desired data source. |
85 | HContentDirectoryServiceConfiguration cdsConfig; |
86 | cdsConfig.setDataSource(d->datasource, false); |
87 | |
88 | // Configure MediaServer by giving it the ContentDirectoryService configuration. |
89 | HMediaServerDeviceConfiguration mediaServerConfig; |
90 | mediaServerConfig.setContentDirectoryConfiguration(cdsConfig); |
91 | |
92 | // Setup the "Device Model Cretor" that HUPnP will use to create |
93 | // appropriate UPnP A/V device and service instances. Here you provide the |
94 | // MediaServer configuration HUPnP will pass to the MediaServer device instance. |
95 | HAvDeviceModelCreator creator; |
96 | creator.setMediaServerConfiguration(mediaServerConfig); |
97 | |
98 | // Setup the HDeviceHost with desired configuration info. |
99 | HDeviceConfiguration config; |
100 | |
101 | QString filePath = KStandardDirs::locate("data" , "kipiplugin_dlnaexport/xml/dlnaexport_mediaserver_description.xml" ); |
102 | |
103 | config.setPathToDeviceDescription(filePath); |
104 | |
105 | kDebug() << "filepath properly set : " << filePath; |
106 | |
107 | config.setCacheControlMaxAge(180); |
108 | |
109 | HDeviceHostConfiguration hostConfiguration; |
110 | hostConfiguration.setDeviceModelCreator(creator); |
111 | hostConfiguration.add(config); |
112 | |
113 | // Initialize the HDeviceHost. |
114 | d->deviceHost = new HDeviceHost(this); |
115 | |
116 | if (!d->deviceHost->init(hostConfiguration)) |
117 | { |
118 | kDebug() << "Initialization failed. Description : " << d->deviceHost->errorDescription().toLocal8Bit(); |
119 | } |
120 | } |
121 | |
122 | MediaServer::~MediaServer() |
123 | { |
124 | delete d->datasource; |
125 | delete d; |
126 | } |
127 | |
128 | void MediaServer::addImagesOnServer(const KUrl::List& imageUrlList) |
129 | { |
130 | QList<HItem*> itemList; |
131 | |
132 | for (int i = 0; i<imageUrlList.size(); i++) |
133 | { |
134 | itemList.append(new HItem(QString(imageUrlList.at(i).fileName()), QString("0" ), QString())); |
135 | d->datasource->add(itemList.at(i), imageUrlList.at(i).path()); |
136 | } |
137 | } |
138 | |
139 | void MediaServer::addImagesOnServer(const QMap<QString, KUrl::List>& collectionMap) |
140 | { |
141 | QList<HContainer*> containerList; |
142 | QList<HItem*> itemList; |
143 | QList<QString> keys = collectionMap.uniqueKeys(); |
144 | KUrl::List imageUrlList; |
145 | int currentSize = 0; |
146 | |
147 | for (int i = 0; i<keys.size(); i++) |
148 | { |
149 | containerList.append(new HContainer(keys.at(i), QString("0" ))); |
150 | d->datasource->add(containerList.at(i)); |
151 | |
152 | imageUrlList.clear(); |
153 | imageUrlList = collectionMap.value(keys.at(i)); |
154 | currentSize = itemList.size(); |
155 | |
156 | for (int j = 0; j<imageUrlList.size(); j++) |
157 | { |
158 | itemList.append(new HItem(QString(imageUrlList.at(j).fileName()), containerList.at(i)->id(), QString())); |
159 | d->datasource->add(itemList.at(j + currentSize), imageUrlList.at(j).path()); |
160 | } |
161 | } |
162 | } |
163 | |
164 | } // namespace KIPIDLNAExportPlugin |
165 | |