summaryrefslogtreecommitdiffstats
path: root/src/publishsubscribe/contextkitlayer.cpp
blob: 79ca09843813eb7e4c008aef69ea8d65c42a4038 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the Qt Mobility Components.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "contextkitlayer_p.h"
#include <contextpropertyinfo.h>

#include <QCoreApplication>
#include <QMetaType>
#include <QChar>
#include <QtDBus/QDBusMetaType>
#include <QtDBus/QDBusArgument>

// dbus types
Q_DECLARE_METATYPE(float)

QDBusArgument &operator<<(QDBusArgument &argument, const float &f)
{
    argument.beginStructure();
    argument << (double)f;
    argument.endStructure();
    return argument;
}

const QDBusArgument &operator>>(const QDBusArgument &argument, float &f)
{
    double d;
    argument.beginStructure();
    argument >> d;
    argument.endStructure();
    f = (float)d;
    return argument;
}

Q_DECLARE_METATYPE(QChar)

QDBusArgument &operator<<(QDBusArgument &argument, const QChar &ch)
{
    argument.beginStructure();
    argument << ch.unicode();
    argument.endStructure();
    return argument;
}

const QDBusArgument &operator>>(const QDBusArgument &argument, QChar &ch)
{
    ushort sh;
    argument.beginStructure();
    argument >> sh;
    argument.endStructure();
    ch = QChar(sh);
    return argument;
}

QTM_BEGIN_NAMESPACE

using namespace QValueSpace;

ContextKitPath::ContextKitPath()
{
}

ContextKitPath::ContextKitPath(QString path)
{
    initFromPath(path);
}

void ContextKitPath::initFromPath(QString path)
{
    bool hasSlash = path.contains(QLatin1Char('/'));
    bool hasDot = path.contains(QLatin1Char('.'));
    bool startingSlash = path.startsWith(QLatin1Char('/'));

    if (startingSlash)
        path = path.mid(1);

    if (path == "" || path == "/") {
        m_type = SlashPath;
    } else if ((hasSlash && !hasDot) || (hasSlash && startingSlash)) {
        parts = path.split(QLatin1Char('/'));
        m_type = SlashPath;
    } else if (hasDot && !hasSlash) {
        parts = path.split(QLatin1Char('.'));
        m_type = DotPath;
    } else {
        m_type = SlashPath;
        parts.append(path);
    }
}

ContextKitPath::ContextKitPath(QString path, PathType type)
{
    initFromPath(path);
    m_type = type;
}

ContextKitPath::ContextKitPath(const ContextKitPath &other)
{
    parts = other.parts;
    m_type = other.m_type;
}

ContextKitPath &ContextKitPath::operator=(const ContextKitPath &other)
{
    parts = other.parts;
    m_type = other.m_type;
    return *this;
}

bool ContextKitPath::isRegistered() const
{
    ContextPropertyInfo pi(toNative());
    return pi.declared();
}

bool ContextKitPath::isProvided() const
{
    ContextPropertyInfo pi(toNative());
    return pi.provided();
}

ContextKitPath ContextKitPath::operator+(const ContextKitPath &other) const
{
    ContextKitPath p(*this);
    foreach (QString pt, other.parts)
        p.parts.append(pt);
    return p;
}

ContextKitPath ContextKitPath::operator+(const QString &str) const
{
    ContextKitPath s(str);
    return *this + s;
}

ContextKitPath ContextKitPath::operator-(const ContextKitPath &other) const
{
    if (other.parts.size() > parts.size()) return ContextKitPath();
    if (other.parts.size() == 0) return ContextKitPath(*this);
    if (parts.size() == 0) return ContextKitPath();

    ContextKitPath p(*this);
    ContextKitPath q(other);
    while (p.parts.size() > 0 && q.parts.size() > 0 &&
           p.parts.first() == q.parts.first()) {
        p.parts.removeFirst();
        q.parts.removeFirst();
    }
    return p;
}

QString ContextKitPath::toNative() const
{
    if (m_type == SlashPath)
        return "/" + parts.join("/");
    else if (m_type == DotPath)
        return parts.join(".");
    else
        return QString();
}

QString ContextKitPath::toQtPath() const
{
    return "/" + parts.join("/");
}

bool ContextKitPath::operator==(const ContextKitPath &other) const
{
    if (parts.size() != other.parts.size())
        return false;
    if (m_type != other.m_type)
        return false;

    for (int i = 0; i < parts.size(); i++)
        if (other.parts.at(i) != parts.at(i))
            return false;

    return true;
}

bool ContextKitPath::includes(ContextKitPath &other) const
{
    // can't include things smaller than you
    if (other.parts.size() < parts.size())
        return false;

    // can't include paths of different type
    if (other.m_type != m_type)
        return false;

    // root path includes all others
    if (parts.size() == 0)
        return true;

    for (int i = 0; i < parts.size(); i++)
        if (other.parts.at(i) != parts.at(i))
            return false;

    return true;
}

// borrowed from qt/corelib/io/qsettings_mac
static QString comify(const QString &organization)
{
    for (int i = organization.size() - 1; i >= 0; --i) {
        QChar ch = organization.at(i);
        if (ch == QLatin1Char('.') || ch == QChar(0x3002) || ch == QChar(0xff0e)
                || ch == QChar(0xff61)) {
            QString suffix = organization.mid(i + 1).toLower();
            if (suffix.size() == 2 || suffix == QLatin1String("com")
                    || suffix == QLatin1String("org") || suffix == QLatin1String("net")
                    || suffix == QLatin1String("edu") || suffix == QLatin1String("gov")
                    || suffix == QLatin1String("mil") || suffix == QLatin1String("biz")
                    || suffix == QLatin1String("info") || suffix == QLatin1String("name")
                    || suffix == QLatin1String("pro") || suffix == QLatin1String("aero")
                    || suffix == QLatin1String("coop") || suffix == QLatin1String("museum")) {
                QString result = organization;
                result.replace(QLatin1Char('/'), QLatin1Char(' '));
                return result;
            }
            break;
        }
        int uc = ch.unicode();
        if ((uc < 'a' || uc > 'z') && (uc < 'A' || uc > 'Z'))
            break;
    }

    QString domain;
    for (int i = 0; i < organization.size(); ++i) {
        QChar ch = organization.at(i);
        int uc = ch.unicode();
        if ((uc >= 'a' && uc <= 'z') || (uc >= '0' && uc <= '9')) {
            domain += ch;
        } else if (uc >= 'A' && uc <= 'Z') {
            domain += ch.toLower();
        } else {
           domain += QLatin1Char(' ');
        }
    }
    domain = domain.simplified();
    domain.replace(QLatin1Char(' '), QLatin1Char('-'));
    if (!domain.isEmpty())
        domain.append(QLatin1String(".com"));
    return domain;
}

void ContextKitHandle::insertRead(const ContextKitPath &path)
{
    if (!readProps.contains(path.toQtPath())) {
        ContextProperty *prop = new ContextProperty(path.toNative());
        readProps.insert(path.toQtPath(), prop);
    }
}

void ContextKitHandle::updateSubtrees()
{
    QList<QString> keys = ContextRegistryInfo::instance()->listKeys();
    foreach (const QString &k, keys) {
        ContextKitPath p(k);
        if (path.includes(p))
            insertRead(p);
    }
}

ContextKitHandle::ContextKitHandle(ContextKitHandle *parent, const QString &path,
                                   QValueSpace::LayerOptions opts)
{
    QString javaPackageName;
    int curPos = 0;
    int nextDot;

    QString domainName = comify(QCoreApplication::organizationDomain());
    if (domainName.isEmpty()) {
        domainName = QLatin1String("unknown-organization.nokia.com");
        qWarning("No organization name specified, registering on DBUS with "
                 "'com.nokia.unknown-organization'");
    }

    while ((nextDot = domainName.indexOf(QLatin1Char('.'), curPos)) != -1) {
        javaPackageName.prepend(domainName.mid(curPos, nextDot - curPos));
        javaPackageName.prepend(QLatin1Char('.'));
        curPos = nextDot + 1;
    }
    javaPackageName.prepend(domainName.mid(curPos));
    javaPackageName = javaPackageName.toLower();
    if (curPos == 0)
        javaPackageName.prepend(QLatin1String("com."));

    javaPackageName += QLatin1Char('.');
    QString app = QCoreApplication::applicationName();
    if (app.size() < 1 || app.at(0) == QLatin1Char(' ')) {
        javaPackageName += "unknown-application";
        qWarning("No application name specified, registering on DBUS as "
                 "'unknown-application'");
    } else {
        app.replace(QRegExp("[^0-9a-zA-Z]"), "");
        javaPackageName += app;
    }

    service = new ContextProvider::Service(QDBusConnection::SessionBus,
                                           javaPackageName, false);

    subscribed = true;

    if (opts & QValueSpace::PermanentLayer)
        this->path = ContextKitPath(path, ContextKitPath::DotPath);
    else
        this->path = ContextKitPath(path, ContextKitPath::SlashPath);

    if (parent)
        this->path = parent->path + this->path;

    updateSubtrees();
    connect(ContextRegistryInfo::instance(), SIGNAL(changed()),
            this, SLOT(updateSubtrees()));
}

ContextKitHandle::~ContextKitHandle()
{
    service->stop();
    foreach (ContextProperty *prop, readProps.values()) {
        prop->unsubscribe();
        delete prop;
    }
    foreach (ContextProvider::Property *prop, writeProps.values())
        delete prop;
    readProps.clear();
    writeProps.clear();
    service->start();
    delete service;
}

bool ContextKitHandle::value(const QString &path, QVariant *data)
{
    ContextKitPath p = this->path;
    if (!path.isEmpty()) p = p + path;

    if (!p.isRegistered() || !p.isProvided())
        return false;

    ContextProperty *prop = readProps.value(p.toQtPath());
    if (prop) {
        if (!subscribed)
            prop->subscribe();

        prop->waitForSubscription();
        *data = prop->value();

        if (!subscribed)
            prop->unsubscribe();

        return true;
    } else {
        return false;
    }
}

bool ContextKitHandle::setValue(const QString &path, const QVariant &data)
{
    ContextKitPath p = this->path;
    if (!path.isEmpty()) p = p + path;

    if (!p.isRegistered())
        return false;

    ContextProvider::Property *prop = writeProps.value(p.toQtPath());
    if (!prop)
        prop = insertWrite(p);

    prop->setValue(data);
    return true;
}

bool ContextKitHandle::unsetValue(const QString &path)
{
    ContextKitPath p = this->path;
    if (!path.isEmpty()) p = p + path;

    if (!p.isRegistered())
        return false;

    ContextProvider::Property *prop = writeProps.value(p.toQtPath());
    if (!prop)
        prop = insertWrite(p);

    prop->unsetValue();
    return true;
}

ContextProvider::Property *ContextKitHandle::insertWrite(const ContextKitPath &p)
{
    if (!writeProps.contains(p.toQtPath())) {
        ContextProvider::Property *prop;
        QString pth = p.toNative();

        service->stop();

        prop = new ContextProvider::Property(*service, pth);
        writeProps.insert(p.toQtPath(), prop);
        connect(prop, SIGNAL(firstSubscriberAppeared(QString)),
                this, SLOT(on_firstAppeared(QString)));
        connect(prop, SIGNAL(lastSubscriberDisappeared(QString)),
                this, SLOT(on_lastDisappeared(QString)));

        service->start();

        return prop;
    }
    return NULL;
}

void ContextKitHandle::on_firstAppeared(QString key)
{
    ContextKitPath p(key, path.type());
    QString relPath = (p - path).toQtPath().mid(1);
    emit interestChanged(relPath, true);
}

void ContextKitHandle::on_lastDisappeared(QString key)
{
    ContextKitPath p(key, path.type());
    QString relPath = (p - path).toQtPath().mid(1);
    emit interestChanged(relPath, false);
}

void ContextKitHandle::subscribe()
{
    foreach (ContextProperty *p, readProps.values()) {
        p->subscribe();
        connect(p, SIGNAL(valueChanged()),
                this, SIGNAL(valueChanged()));
    }
    subscribed = true;
}

void ContextKitHandle::unsubscribe()
{
    foreach (ContextProperty *p, readProps.values()) {
        p->unsubscribe();
        disconnect(p, SIGNAL(valueChanged()),
                   this, SIGNAL(valueChanged()));
    }
    subscribed = false;
}

QSet<QString> ContextKitHandle::children()
{
    QSet<QString> kids;

    foreach (const QString &qp, readProps.keys()) {
        ContextKitPath pth(qp);
        pth = pth - this->path;
        if (pth.size() > 0)
            kids.insert(pth.at(0));
    }
    return kids;
}

ContextKitLayer::ContextKitLayer()
{
    qDBusRegisterMetaType<float>();
    qDBusRegisterMetaType<QChar>();
}

ContextKitLayer::~ContextKitLayer()
{
}

ContextKitHandle *ContextKitLayer::handleToCKHandle(Handle handle)
{
    ContextKitHandle *ckh = NULL;
    if (handle != InvalidHandle)
        ckh = reinterpret_cast<ContextKitHandle*>(handle);
    return ckh;
}

void ContextKitLayer::sync()
{
    QCoreApplication::processEvents();
}

bool ContextKitLayer::startup(Type)
{
    return true;
}

QAbstractValueSpaceLayer::Handle ContextKitLayer::item(Handle parent,
                                                       const QString &subPath)
{
    ContextKitHandle *parentHandle = handleToCKHandle(parent);
    ContextKitHandle *h = new ContextKitHandle(parentHandle, subPath,
                                               this->layerOptions());
    return reinterpret_cast<Handle>(h);
}

void ContextKitLayer::removeHandle(Handle handle)
{
    ContextKitHandle *h = handleToCKHandle(handle);
    if (h)
        delete h;
}

void ContextKitLayer::setProperty(Handle handle, Properties properties)
{
    ContextKitHandle *h = handleToCKHandle(handle);
    if (!h)
        return;

    if (properties & Publish)
        connect(h, SIGNAL(valueChanged()),
                this, SLOT(contextHandleChanged()));
    else
        disconnect(h, SIGNAL(valueChanged()),
                   this, SLOT(contextHandleChanged()));
}

void ContextKitLayer::contextHandleChanged()
{
    emit handleChanged(reinterpret_cast<Handle>(sender()));
}

void ContextKitLayer::addWatch(QValueSpacePublisher *pub, Handle handle)
{
    ContextKitHandle *h = handleToCKHandle(handle);
    connect(h, SIGNAL(interestChanged(QString,bool)),
            pub, SIGNAL(interestChanged(QString,bool)));
}

void ContextKitLayer::removeWatches(QValueSpacePublisher *pub, Handle handle)
{
    ContextKitHandle *h = handleToCKHandle(handle);
    disconnect(h, SIGNAL(interestChanged(QString,bool)),
               pub, SIGNAL(interestChanged(QString,bool)));
}

bool ContextKitLayer::value(Handle handle, QVariant *data)
{
    ContextKitHandle *h = handleToCKHandle(handle);
    if (h)
        return h->value("", data);
    else
        return false;
}

bool ContextKitLayer::value(Handle handle, const QString &subPath, QVariant *data)
{
    ContextKitHandle *h = handleToCKHandle(handle);
    if (h)
        return h->value(subPath, data);
    else
        return false;
}

bool ContextKitLayer::setValue(QValueSpacePublisher *, Handle handle,
                               const QString &path, const QVariant &value)
{
    ContextKitHandle *h = handleToCKHandle(handle);
    if (h)
        return h->setValue(path, value);
    else
        return false;
}

bool ContextKitLayer::removeValue(QValueSpacePublisher *, Handle handle,
                                  const QString &path)
{
    ContextKitHandle *h = handleToCKHandle(handle);
    if (h)
        return h->unsetValue(path);
    else
        return false;
}

bool ContextKitLayer::removeSubTree(QValueSpacePublisher *vsp, Handle handle)
{
    ContextKitHandle *h = handleToCKHandle(handle);
    if (!h)
        return false;

    // TODO: fix this up
    foreach (QString kid, h->children())
        if (!h->unsetValue(kid)) return false;

    return true;
}

bool ContextKitLayer::notifyInterest(Handle handle, bool interested)
{
    ContextKitHandle *h = handleToCKHandle(handle);

    if (interested)
        h->subscribe();
    else
        h->unsubscribe();
    return true;
}

QSet<QString> ContextKitLayer::children(Handle handle)
{
    ContextKitHandle *h = handleToCKHandle(handle);
    return h->children();
}

QVALUESPACE_AUTO_INSTALL_LAYER(ContextKitNonCoreLayer);
Q_GLOBAL_STATIC(ContextKitNonCoreLayer, contextKitNonCoreLayer);
ContextKitNonCoreLayer *ContextKitNonCoreLayer::instance()
{
    return contextKitNonCoreLayer();
}

QValueSpace::LayerOptions ContextKitNonCoreLayer::layerOptions() const
{
    return WritableLayer | TransientLayer;
}

QString ContextKitNonCoreLayer::name()
{
    return "ContextKit Non-Core Layer";
}

unsigned int ContextKitNonCoreLayer::order()
{
    return 1;
}

QUuid ContextKitNonCoreLayer::id()
{
    return QVALUESPACE_CONTEXTKITNONCORE_LAYER;
}

QVALUESPACE_AUTO_INSTALL_LAYER(ContextKitCoreLayer);
Q_GLOBAL_STATIC(ContextKitCoreLayer, contextKitCoreLayer);
ContextKitCoreLayer *ContextKitCoreLayer::instance()
{
    return contextKitCoreLayer();
}

QValueSpace::LayerOptions ContextKitCoreLayer::layerOptions() const
{
    return WritableLayer | PermanentLayer;
}

QString ContextKitCoreLayer::name()
{
    return "ContextKit Core Layer";
}

unsigned int ContextKitCoreLayer::order()
{
    return 0;
}

QUuid ContextKitCoreLayer::id()
{
    return QVALUESPACE_CONTEXTKITCORE_LAYER;
}


#include "moc_contextkitlayer_p.cpp"

QTM_END_NAMESPACE