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

« back to all changes in this revision

Viewing changes to extra/kipi-plugins/dlnaexport/extra/hupnp/src/devicemodel/hdevicemodel_validator.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) 2010, 2011 Tuomo Penttinen, all rights reserved.
 
3
 *
 
4
 *  Author: Tuomo Penttinen <tp@herqq.org>
 
5
 *
 
6
 *  This file is part of Herqq UPnP (HUPnP) library.
 
7
 *
 
8
 *  Herqq UPnP is free software: you can redistribute it and/or modify
 
9
 *  it under the terms of the GNU Lesser 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 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 Lesser General Public License for more details.
 
17
 *
 
18
 *  You should have received a copy of the GNU Lesser General Public License
 
19
 *  along with Herqq UPnP. If not, see <http://www.gnu.org/licenses/>.
 
20
 */
 
21
 
 
22
#include "hdevicemodel_validator.h"
 
23
#include "hdevicemodel_validator_p.h"
 
24
#include "hdevicemodel_infoprovider.h"
 
25
 
 
26
#include "client/hclientaction.h"
 
27
#include "client/hclientdevice.h"
 
28
#include "client/hclientservice.h"
 
29
#include "client/hclientstatevariable.h"
 
30
 
 
31
#include "server/hserveraction.h"
 
32
#include "server/hserverdevice.h"
 
33
#include "server/hserverservice.h"
 
34
#include "server/hserverstatevariable.h"
 
35
 
 
36
#include "../dataelements/hactioninfo.h"
 
37
#include "../dataelements/hdeviceinfo.h"
 
38
#include "../dataelements/hserviceinfo.h"
 
39
#include "../dataelements/hstatevariableinfo.h"
 
40
 
 
41
#include "hactionarguments.h"
 
42
#include "hactions_setupdata.h"
 
43
#include "hdevices_setupdata.h"
 
44
#include "hservices_setupdata.h"
 
45
#include "hstatevariables_setupdata.h"
 
46
 
 
47
#include <QtCore/QSet>
 
48
 
 
49
namespace Herqq
 
50
{
 
51
 
 
52
namespace Upnp
 
53
{
 
54
 
 
55
/*******************************************************************************
 
56
 * HDeviceModelValidatorPrivate
 
57
 ******************************************************************************/
 
58
HDeviceModelValidatorPrivate::HDeviceModelValidatorPrivate() :
 
59
    m_lastErrorDescription(), m_infoProvider(0)
 
60
{
 
61
}
 
62
 
 
63
template<typename Device, typename Service, typename Action, typename StateVariable>
 
64
HDeviceModelValidator::ValidationResult
 
65
    HDeviceModelValidatorPrivate::validateDevice(Device* device)
 
66
{
 
67
    const HDeviceInfo& deviceInfo = device->info();
 
68
 
 
69
    qint32 deviceVersion = deviceInfo.deviceType().version();
 
70
 
 
71
    HServicesSetupData servicesSetupData =
 
72
        m_infoProvider->servicesSetupData(deviceInfo);
 
73
 
 
74
    foreach(const HServiceId& srvId, servicesSetupData.serviceIds())
 
75
    {
 
76
        HServiceSetup serviceSetup = servicesSetupData.get(srvId);
 
77
 
 
78
        if (deviceVersion >= serviceSetup.version())
 
79
        {
 
80
            Service* service = device->serviceById(srvId);
 
81
            if (!service)
 
82
            {
 
83
                if (serviceSetup.inclusionRequirement() == InclusionMandatory)
 
84
                {
 
85
                    m_lastErrorDescription =
 
86
                        QString("Mandatory service [%1] is missing").arg(
 
87
                            srvId.toString());
 
88
 
 
89
                    return HDeviceModelValidator::MandatoryServiceMissing;
 
90
                }
 
91
            }
 
92
            else
 
93
            {
 
94
                HDeviceModelValidator::ValidationResult res =
 
95
                    validateService<Service, Action, StateVariable>(service);
 
96
 
 
97
                if (res != HDeviceModelValidator::ValidationSucceeded)
 
98
                {
 
99
                    return res;
 
100
                }
 
101
            }
 
102
        }
 
103
    }
 
104
 
 
105
    HDevicesSetupData embDevSetupData =
 
106
        m_infoProvider->embedddedDevicesSetupData(deviceInfo);
 
107
 
 
108
    foreach(const HResourceType& devType, embDevSetupData.deviceTypes())
 
109
    {
 
110
        HDeviceSetup deviceSetup = embDevSetupData.get(devType);
 
111
 
 
112
        if (deviceVersion >= deviceSetup.version())
 
113
        {
 
114
            QList<Device*> devices = device->embeddedDevicesByType(devType);
 
115
            if (devices.isEmpty() &&
 
116
                deviceSetup.inclusionRequirement() == InclusionMandatory)
 
117
            {
 
118
                m_lastErrorDescription =
 
119
                    QString("Mandatory device [%1] is missing").arg(
 
120
                        devType.toString());
 
121
 
 
122
                return HDeviceModelValidator::MandatoryDeviceMissing;
 
123
            }
 
124
 
 
125
            foreach(Device* device, devices)
 
126
            {
 
127
                HDeviceModelValidator::ValidationResult res =
 
128
                    validateDevice<Device, Service, Action, StateVariable>(device);
 
129
 
 
130
                if (res != HDeviceModelValidator::ValidationSucceeded)
 
131
                {
 
132
                    return res;
 
133
                }
 
134
            }
 
135
        }
 
136
    }
 
137
 
 
138
    return HDeviceModelValidator::ValidationSucceeded;
 
139
}
 
140
 
 
141
template<typename Service, typename Action, typename StateVariable>
 
142
HDeviceModelValidator::ValidationResult
 
143
    HDeviceModelValidatorPrivate::validateService(Service* service)
 
144
{
 
145
    const HServiceInfo& serviceInfo = service->info();
 
146
    const HDeviceInfo& deviceInfo = service->parentDevice()->info();
 
147
 
 
148
    qint32 serviceVersion = serviceInfo.serviceType().version();
 
149
 
 
150
    HActionsSetupData actionSetupData =
 
151
        m_infoProvider->actionsSetupData(serviceInfo, deviceInfo);
 
152
 
 
153
    // All the action setup elements will be checked. If there exists an action,
 
154
    // but the info provider does not return an action setup information of it,
 
155
    // the action is considered valid.
 
156
 
 
157
    foreach(const QString& actionName, actionSetupData.names())
 
158
    {
 
159
        HActionSetup actionSetup = actionSetupData.get(actionName);
 
160
 
 
161
        if (serviceVersion >= actionSetup.version())
 
162
        {
 
163
            // Validate only if the version of the service is equal or greater
 
164
            // to the version defined in the setup data. If the service version
 
165
            // is smaller then the action was not defined in that version at all,
 
166
            // but that is not an error. This only means that when interacting with
 
167
            // this service version, this action should never be called.
 
168
            //
 
169
            // However, this version check is just an extra sanity check, since a
 
170
            // properly functioning "info provider" should check the version of
 
171
            // the provided service info and return information considering that.
 
172
 
 
173
            const Action* action = service->actions().value(actionName);
 
174
 
 
175
            if (!action)
 
176
            {
 
177
                if (actionSetup.inclusionRequirement() == InclusionMandatory)
 
178
                {
 
179
                    m_lastErrorDescription =
 
180
                        QString("Mandatory action [%1] is missing").arg(
 
181
                            actionName);
 
182
 
 
183
                    return HDeviceModelValidator::MandatoryActionMissing;
 
184
                }
 
185
            }
 
186
            else
 
187
            {
 
188
                HDeviceModelValidator::ValidationResult res =
 
189
                    validateAction(action, actionSetup);
 
190
 
 
191
                if (res != HDeviceModelValidator::ValidationSucceeded)
 
192
                {
 
193
                    m_lastErrorDescription =
 
194
                        QString("Action [%1] failed validation: %2").arg(
 
195
                            actionName, m_lastErrorDescription);
 
196
 
 
197
                    return res;
 
198
                }
 
199
            }
 
200
        }
 
201
    }
 
202
 
 
203
    HStateVariablesSetupData svData =
 
204
        m_infoProvider->stateVariablesSetupData(serviceInfo, deviceInfo);
 
205
 
 
206
    foreach(const QString& svName, svData.names())
 
207
    {
 
208
        HStateVariableInfo svInfo = svData.get(svName);
 
209
 
 
210
        if (serviceVersion >= svInfo.version())
 
211
        {
 
212
            const StateVariable* stateVar =
 
213
                service->stateVariables().value(svName);
 
214
 
 
215
            if (!stateVar)
 
216
            {
 
217
                if (svInfo.inclusionRequirement() == InclusionMandatory)
 
218
                {
 
219
                    m_lastErrorDescription =
 
220
                        QString("Mandatory state variable [%1] is missing").arg(
 
221
                            svName);
 
222
 
 
223
                    return HDeviceModelValidator::MandatoryStateVariableMissing;
 
224
                }
 
225
            }
 
226
            else
 
227
            {
 
228
                HDeviceModelValidator::ValidationResult res =
 
229
                    validateStateVariable(stateVar, svInfo);
 
230
 
 
231
                if (res != HDeviceModelValidator::ValidationSucceeded)
 
232
                {
 
233
                    m_lastErrorDescription =
 
234
                        QString("State Variable [%1] failed validation: %2").arg(
 
235
                            svName, m_lastErrorDescription);
 
236
 
 
237
                    return res;
 
238
                }
 
239
            }
 
240
        }
 
241
    }
 
242
 
 
243
    return HDeviceModelValidator::ValidationSucceeded;
 
244
}
 
245
 
 
246
template<typename Action>
 
247
HDeviceModelValidator::ValidationResult
 
248
    HDeviceModelValidatorPrivate::validateAction(
 
249
        Action* action, const HActionSetup& actionSetup)
 
250
{
 
251
    HActionArguments inArgs = action->info().inputArguments();
 
252
    HActionArguments outArgs = action->info().outputArguments();
 
253
 
 
254
    HActionArguments inArgsSetup = actionSetup.inputArguments();
 
255
    HActionArguments outArgsSetup = actionSetup.outputArguments();
 
256
 
 
257
    foreach(const QString& argName, inArgsSetup.names())
 
258
    {
 
259
        HActionArgument argSetup = inArgsSetup.get(argName);
 
260
        HActionArgument arg = inArgs.get(argName);
 
261
 
 
262
        if (!arg.isValid())
 
263
        {
 
264
            m_lastErrorDescription =
 
265
                QString("Mandatory action argument [%1] is missing").arg(
 
266
                    argName);
 
267
 
 
268
            return HDeviceModelValidator::MandatoryActionArgumentMissing;
 
269
        }
 
270
        else if (arg.dataType() != argSetup.dataType())
 
271
        {
 
272
            m_lastErrorDescription =
 
273
                QString("Invalid data type specified in action argument [%1]").arg(
 
274
                    argName);
 
275
 
 
276
            return HDeviceModelValidator::InvalidArgumentDefinition;
 
277
        }
 
278
    }
 
279
 
 
280
    foreach(const QString& argName, outArgsSetup.names())
 
281
    {
 
282
        HActionArgument argSetup = outArgsSetup.get(argName);
 
283
        HActionArgument arg = outArgs.get(argName);
 
284
 
 
285
        if (!arg.isValid())
 
286
        {
 
287
            m_lastErrorDescription =
 
288
                QString("Mandatory action argument [%1] is missing").arg(
 
289
                    argName);
 
290
 
 
291
            return HDeviceModelValidator::MandatoryActionArgumentMissing;
 
292
        }
 
293
        else if (arg.dataType() != argSetup.dataType())
 
294
        {
 
295
            m_lastErrorDescription =
 
296
                QString("Invalid data type specified in action argument [%1]").arg(
 
297
                    argName);
 
298
 
 
299
            return HDeviceModelValidator::InvalidArgumentDefinition;
 
300
        }
 
301
    }
 
302
 
 
303
    return HDeviceModelValidator::ValidationSucceeded;
 
304
}
 
305
 
 
306
template<typename Action>
 
307
HDeviceModelValidator::ValidationResult
 
308
    HDeviceModelValidatorPrivate::validateAction(Action* action)
 
309
{
 
310
    Q_ASSERT_X(action, "", "Null pointer provided");
 
311
 
 
312
    const HServiceInfo& serviceInfo = action->parentService()->info();
 
313
    const HDeviceInfo& deviceInfo   =
 
314
        action->parentService()->parentDevice()->info();
 
315
 
 
316
    HActionsSetupData actionSetupData =
 
317
        m_infoProvider->actionsSetupData(serviceInfo, deviceInfo);
 
318
 
 
319
    HActionSetup actionSetup = actionSetupData.get(action->info().name());
 
320
 
 
321
    return actionSetup.isValid() ?
 
322
        validateAction(action, actionSetup) :
 
323
        HDeviceModelValidator::ValidationSucceeded;
 
324
}
 
325
 
 
326
template<typename StateVariable>
 
327
HDeviceModelValidator::ValidationResult
 
328
    HDeviceModelValidatorPrivate::validateStateVariable(
 
329
        StateVariable* stateVar, const HStateVariableInfo& svInfo)
 
330
{
 
331
    if (stateVar->info().dataType() != svInfo.dataType())
 
332
    {
 
333
        m_lastErrorDescription =
 
334
            QString("Invalid data type specified. Was [%1], expected [%2].").arg(
 
335
                HUpnpDataTypes::toString(stateVar->info().dataType()),
 
336
                HUpnpDataTypes::toString(svInfo.dataType()));
 
337
 
 
338
        return HDeviceModelValidator::InvalidStateVariableDefinition;
 
339
    }
 
340
 
 
341
    return HDeviceModelValidator::ValidationSucceeded;
 
342
}
 
343
 
 
344
template<typename StateVariable>
 
345
HDeviceModelValidator::ValidationResult
 
346
    HDeviceModelValidatorPrivate::validateStateVariable(
 
347
        StateVariable* stateVar)
 
348
{
 
349
    Q_ASSERT_X(stateVar, "", "Null pointer provided");
 
350
 
 
351
    const HServiceInfo& serviceInfo = stateVar->parentService()->info();
 
352
    const HDeviceInfo& deviceInfo   =
 
353
        stateVar->parentService()->parentDevice()->info();
 
354
 
 
355
    HStateVariablesSetupData svSetupData =
 
356
        m_infoProvider->stateVariablesSetupData(serviceInfo, deviceInfo);
 
357
 
 
358
    HStateVariableInfo svInfo = svSetupData.get(stateVar->info().name());
 
359
 
 
360
    return svInfo.isValid() ?
 
361
        validateStateVariable(stateVar, svInfo) :
 
362
        HDeviceModelValidator::ValidationSucceeded;
 
363
}
 
364
 
 
365
/*******************************************************************************
 
366
 * HDeviceModelValidator
 
367
 ******************************************************************************/
 
368
HDeviceModelValidator::HDeviceModelValidator() :
 
369
    h_ptr(new HDeviceModelValidatorPrivate())
 
370
{
 
371
}
 
372
 
 
373
HDeviceModelValidator::~HDeviceModelValidator()
 
374
{
 
375
    delete h_ptr;
 
376
}
 
377
 
 
378
void HDeviceModelValidator::setInfoProvider(
 
379
    const HDeviceModelInfoProvider& provider)
 
380
{
 
381
    h_ptr->m_infoProvider.reset(provider.clone());
 
382
}
 
383
 
 
384
const HDeviceModelInfoProvider* HDeviceModelValidator::infoProvider() const
 
385
{
 
386
    return h_ptr->m_infoProvider.data();
 
387
}
 
388
 
 
389
HDeviceModelValidator::ValidationResult
 
390
    HDeviceModelValidator::validateDevice(HClientDevice* device)
 
391
{
 
392
    if (!infoProvider())
 
393
    {
 
394
        return InfoProviderMissing;
 
395
    }
 
396
 
 
397
    return h_ptr->validateDevice<
 
398
        HClientDevice, HClientService, HClientAction, HClientStateVariable>(device);
 
399
}
 
400
 
 
401
HDeviceModelValidator::ValidationResult
 
402
    HDeviceModelValidator::validateDevice(HServerDevice* device)
 
403
{
 
404
    if (!infoProvider())
 
405
    {
 
406
        return InfoProviderMissing;
 
407
    }
 
408
 
 
409
    return h_ptr->validateDevice<
 
410
        HServerDevice, HServerService, HServerAction, HServerStateVariable>(device);
 
411
}
 
412
 
 
413
HDeviceModelValidator::ValidationResult
 
414
    HDeviceModelValidator::validateService(HClientService* service)
 
415
{
 
416
    if (!infoProvider())
 
417
    {
 
418
        return InfoProviderMissing;
 
419
    }
 
420
 
 
421
    return h_ptr->validateService<HClientService, HClientAction, HClientStateVariable>(service);
 
422
}
 
423
 
 
424
HDeviceModelValidator::ValidationResult
 
425
    HDeviceModelValidator::validateService(HServerService* service)
 
426
{
 
427
    if (!infoProvider())
 
428
    {
 
429
        return InfoProviderMissing;
 
430
    }
 
431
 
 
432
    return h_ptr->validateService<HServerService, HServerAction, HServerStateVariable>(service);
 
433
}
 
434
 
 
435
HDeviceModelValidator::ValidationResult
 
436
    HDeviceModelValidator::validateAction(HClientAction* action)
 
437
{
 
438
    if (!infoProvider())
 
439
    {
 
440
        return HDeviceModelValidator::InfoProviderMissing;
 
441
    }
 
442
 
 
443
    return h_ptr->validateAction(action);
 
444
}
 
445
 
 
446
HDeviceModelValidator::ValidationResult
 
447
    HDeviceModelValidator::validateAction(HServerAction* action)
 
448
{
 
449
    if (!infoProvider())
 
450
    {
 
451
        return HDeviceModelValidator::InfoProviderMissing;
 
452
    }
 
453
 
 
454
    return h_ptr->validateAction(action);
 
455
}
 
456
 
 
457
HDeviceModelValidator::ValidationResult
 
458
    HDeviceModelValidator::validateStateVariable(HClientStateVariable* stateVar)
 
459
{
 
460
    if (!infoProvider())
 
461
    {
 
462
        return HDeviceModelValidator::InfoProviderMissing;
 
463
    }
 
464
 
 
465
    return h_ptr->validateStateVariable(stateVar);
 
466
}
 
467
 
 
468
HDeviceModelValidator::ValidationResult
 
469
    HDeviceModelValidator::validateStateVariable(HServerStateVariable* stateVar)
 
470
{
 
471
    if (!infoProvider())
 
472
    {
 
473
        return HDeviceModelValidator::InfoProviderMissing;
 
474
    }
 
475
 
 
476
    return h_ptr->validateStateVariable(stateVar);
 
477
}
 
478
 
 
479
QString HDeviceModelValidator::lastErrorDescription() const
 
480
{
 
481
    return h_ptr->m_lastErrorDescription;
 
482
}
 
483
 
 
484
}
 
485
}