summaryrefslogtreecommitdiff
path: root/mpris2/tracklistinterfacetest.cpp
blob: 8be1994744a63bdcb9a8bb60b10c982f65f9c84a (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
/*
 * Copyright 2011  Alex Merry <dev@randomguy3.me.uk>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


#include "tracklistinterfacetest.h"

#include <QtDBus>

#define MPRIS2_TRACKLIST_IFACE "org.mpris.MediaTrackList2.TrackList"

using namespace Mpris2;

TrackListInterfaceTest::TrackListInterfaceTest(const QString& service, QObject* parent)
    : InterfaceTest(MPRIS2_TRACKLIST_IFACE, service, parent)
{
}

TrackListInterfaceTest::~TrackListInterfaceTest()
{
}

void TrackListInterfaceTest::connectSignals()
{
    QDBusConnection::sessionBus().connect(
            iface->service(),
            iface->path(),
            iface->interface(),
            "TrackListReplaced", /* signature, */
            this,
            SLOT(_m_trackListReplaced(QList<QDBusObjectPath>,QDBusObjectPath)));
    QDBusConnection::sessionBus().connect(
            iface->service(),
            iface->path(),
            iface->interface(),
            "TrackAdded", /* signature, */
            this,
            SLOT(_m_trackAdded(QVariantMap,QDBusObjectPath)));
    QDBusConnection::sessionBus().connect(
            iface->service(),
            iface->path(),
            iface->interface(),
            "TrackRemoved", /* signature, */
            this,
            SLOT(_m_trackRemoved(QDBusObjectPath)));
    QDBusConnection::sessionBus().connect(
            iface->service(),
            iface->path(),
            iface->interface(),
            "TrackMetadataChanged", /* signature, */
            this,
            SLOT(_m_trackMetadataChanged(QDBusObjectPath,QVariantMap)));
}

void TrackListInterfaceTest::checkUpdatedProperty(const QString& propName)
{
    if (propName == "CanEditTracks") {
        checkPropValid("CanEditTracks", QVariant::Bool);
    } else if (propName == "Tracks") {
        checkTracks();
    }
}

void TrackListInterfaceTest::checkProps(const QVariantMap& oldProps)
{
    checkPropValid("CanEditTracks", QVariant::Bool);
    checkTracks();
}

void TrackListInterfaceTest::checkTracks(const QVariantMap& oldProps)
{
}

static bool compare(const QVariantMap& one, const QVariantMap& other)
{
    if (one.size() != other.size())
        return false;

    QVariantMap::const_iterator it1 = one.begin();
    QVariantMap::const_iterator it2 = other.begin();

    while (it1 != one.end()) {
        if (it1.value().userType() != it2.value().userType())
            return false;
        if (!(it1.value() == it2.value())) {
            if (it1.value().userType() == qMetaTypeId<QDBusObjectPath>()) {
                if (!(it1.value().value<QDBusObjectPath>() == it2.value().value<QDBusObjectPath>()))
                    return false;
            }
        }
        if (qMapLessThanKey(it1.key(), it2.key()) || qMapLessThanKey(it2.key(), it1.key()))
            return false;
        ++it2;
        ++it1;
    }
    return true;
}

void TrackListInterfaceTest::checkMetadata(const QVariantMap& oldProps)
{
    if (!props.contains("Metadata")) {
        emit interfaceError(Property, "Metadata", "Property Metadata is missing");
        return;
    }
    if (props["Metadata"].type() != QVariant::Map &&
        !props.value("Metadata").canConvert<QDBusArgument>())
    {
        const char * gotTypeCh = QDBusMetaType::typeToSignature(props["Metadata"].userType());
        QString gotType = gotTypeCh ? QString::fromAscii(gotTypeCh) : "<unknown>";
        emit interfaceError(Property, "Metadata", "Property Metadata has type " + gotType + ", but should be type a{sv}");
        return;
    }
    QVariantMap metadata;
    if (props["Metadata"].type() == QVariant::Map) {
        metadata = props["Metadata"].toMap();
    } else {
        QDBusArgument arg = props["Metadata"].value<QDBusArgument>();
        arg >> metadata;
        // replace the entry in the properties array
        props["Metadata"] = metadata;
    }

    if (oldProps.contains("Metadata") &&
        oldProps.value("Metadata").canConvert(QVariant::Map))
    {
        QVariantMap oldMetadata = oldProps.value("Metadata").toMap();

        // custom compare fn as we're expecting a QDBusObjectPath entry
        if (!compare(metadata, oldMetadata)) {
            outOfDateProperties["Metadata"] = props["Metadata"];
            props["Metadata"] = oldProps["Metadata"];
            return;
        } else {
            // same as before; don't re-run checks
            return;
        }
    }
    if (metadata.isEmpty()) {
        emit interfaceInfo(Property, "Metadata",
                           "No metadata provided");
        return;
    }

    if (!metadata.contains("mpris:trackid")) {
        emit interfaceError(Property, "Metadata",
                            "No mpris:trackid entry for the current track");
    } else if (metadata.value("mpris:trackid").userType() != qMetaTypeId<QDBusObjectPath>()) {
        emit interfaceError(Property, "Metadata",
                            "mpris:trackid entry was not sent as a D-Bus object path (D-Bus type 'o')");
    } else if (metadata.value("mpris:trackid").value<QDBusObjectPath>().path().isEmpty()) {
        emit interfaceError(Property, "Metadata",
                            "mpris:trackid entry is an empty path");
    }

    checkMetadataEntry(metadata, "mpris:length", QVariant::LongLong);

    if (checkMetadataEntry(metadata, "mpris:artUrl", QVariant::Url)) {
        QString artUrl = metadata.value("mpris:artUrl").toString();
        QUrl asUrl(artUrl, QUrl::StrictMode);
        if (asUrl.scheme() != "file" && asUrl.scheme() != "http" && asUrl.scheme() != "https") {
            emit interfaceInfo(Property, "Metadata",
                                "mpris:artUrl has a scheme (" + asUrl.scheme() + ") which not all clients may recognise");
        } else {
            if (asUrl.scheme() == "file") {
                if (!QFile::exists(asUrl.toLocalFile())) {
                    emit interfaceInfo(Property, "Metadata",
                                        "mpris:artUrl references a file that does not exist");
                }
            }
            // TODO: check network files
        }
    }

    Q_FOREACH( QString key, metadata.keys() ) {
        if (!key.startsWith("xesam:")) {
            if (key != "mpris:trackid" &&
                key != "mpris:length" &&
                key != "mpris:artUrl")
            {
                emit interfaceWarning(Property, "Metadata",
                                      "Unrecognised entry " + key);
            }
        }
    }

    checkMetadataEntry(metadata, "xesam:album", QVariant::String);
    checkMetadataEntry(metadata, "xesam:albumArtist", QVariant::StringList);
    checkMetadataEntry(metadata, "xesam:artist", QVariant::StringList);
    checkMetadataEntry(metadata, "xesam:asText", QVariant::String);
    checkMetadataEntry(metadata, "xesam:audioBpm", QVariant::Int);
    checkMetadataEntry(metadata, "xesam:autoRating", QVariant::Double);
    checkMetadataEntry(metadata, "xesam:comment", QVariant::StringList);
    checkMetadataEntry(metadata, "xesam:composer", QVariant::StringList);
    checkMetadataEntry(metadata, "xesam:contentCreator", QVariant::DateTime);
    checkMetadataEntry(metadata, "xesam:discNumber", QVariant::Int);
    checkMetadataEntry(metadata, "xesam:firstUsed", QVariant::DateTime);
    checkMetadataEntry(metadata, "xesam:genre", QVariant::StringList);
    checkMetadataEntry(metadata, "xesam:lastUsed", QVariant::DateTime);
    checkMetadataEntry(metadata, "xesam:lyricist", QVariant::StringList);
    checkMetadataEntry(metadata, "xesam:title", QVariant::String);
    checkMetadataEntry(metadata, "xesam:trackNumber", QVariant::Int);
    checkMetadataEntry(metadata, "xesam:url", QVariant::Url);
    checkMetadataEntry(metadata, "xesam:useCount", QVariant::Int);
    checkMetadataEntry(metadata, "xesam:userRating", QVariant::Double);
}

bool TrackListInterfaceTest::checkMetadataEntry(const QVariantMap& metadata, const QString& entry, QVariant::Type expType)
{
    if (metadata.contains(entry)) {
        QVariant value = metadata.value(entry);

        bool propertyTypeError = false;
        bool propertyTypeWarning = false;
        QVariant::Type realExpectedType = expType;
        if (expType == QVariant::DateTime || expType == QVariant::Url) {
            realExpectedType = QVariant::String;
        }

        // be lax about integers
        if (realExpectedType == QVariant::Int) {
            if (value.type() == QVariant::UInt ||
                value.type() == QVariant::LongLong ||
                value.type() == QVariant::ULongLong)
            {
                propertyTypeWarning = true;
            } else if (value.type() != QVariant::Int) {
                propertyTypeError = true;
            }
        } else if (realExpectedType == QVariant::UInt || realExpectedType == QVariant::LongLong) {
            if (value.type() == QVariant::ULongLong) {
                propertyTypeWarning = true;
            } else if (value.type() != realExpectedType) {
                propertyTypeError = true;
            }
        } else if (value.type() != realExpectedType) {
            propertyTypeError = true;
        }

        if (propertyTypeError || propertyTypeWarning) {
            const char * gotTypeCh = QDBusMetaType::typeToSignature(value.userType());
            QString gotType = gotTypeCh ? QString::fromAscii(gotTypeCh) : "<unknown>";
            const char * expTypeCh = QDBusMetaType::typeToSignature(realExpectedType);
            QString expType = expTypeCh ? QString::fromAscii(expTypeCh) : "<unknown>";
            if (propertyTypeError) {
                emit interfaceError(Property, "Metadata",
                                    entry + " entry is of type '" + gotType + "' but should have been of type '" + expType + "'");
                return false;
            } else {
                emit interfaceWarning(Property, "Metadata",
                                      entry + " entry is of type '" + gotType + "' but should have been of type '" + expType + "'");
                return true;
            }
        }

        // extra checks for special types
        if (expType == QVariant::DateTime) {
            QDateTime dtValue = QDateTime::fromString(value.toString(), Qt::ISODate);
            if (!dtValue.isValid()) {
                emit interfaceError(Property, "Metadata",
                                    entry + " entry does not contain a valid date/time string (value was " + value.toString() + ")");
                return false;
            }
        } else if (expType == QVariant::Url) {
            if (value.toString().isEmpty()) {
                return false;
            } else {
                QUrl asUrl(value.toString(), QUrl::StrictMode);
                if (!asUrl.isValid()) {
                    emit interfaceError(Property, "Metadata",
                                        entry + " entry is not a valid URL");
                    return false;
                }
            }
        }
        return true;
    }
    return false;
}

void TrackListInterfaceTest::_m_trackListReplaced(const QList<QDBusObjectPath>& trackIds, const QDBusObjectPath& currentPath)
{
}

void TrackListInterfaceTest::_m_trackAdded(const QVariantMap& metadata, const QDBusObjectPath& afterTrack)
{
}

void TrackListInterfaceTest::_m_trackRemoved(const QDBusObjectPath& afterTrack)
{
}

void TrackListInterfaceTest::_m_trackMetadataChanged(const QDBusObjectPath& track, const QVariantMap& metadata)
{
}


/*
void TrackListInterfaceTest::testNext()
{
    QDBusReply<void> reply = iface->call("Next");
    if (!reply.isValid()) {
        emit interfaceError(Method, "Next", "Call to Next failed with error " + reply.error().message());
    } else {
        if (!props["CanGoNext"].toBool()) {
            emit interfaceInfo(Method, "Next", "Next called, but CanGoNext is false, so this should have no effect");
            // TODO: check to see that the track does not change in the next second or so
        } else {
            emit interfaceInfo(Method, "Next", "Next called; the media player should now move to the next track");
            // TODO: check to see if the track changes
            // TODO: check to make sure the PlaybackStatus does not change
        }
    }
}
*/

QList<QVariantMap> TrackListInterfaceTest::testGetTracksMetadata(const QList<QDBusObjectPath>& trackIds)
{
}

void TrackListInterfaceTest::testAddTrack(const QString& uri, const QDBusObjectPath& afterTrack, bool setAsCurrent)
{
}

void TrackListInterfaceTest::testRemoveTrack(const QDBusObjectPath& track)
{
}

void TrackListInterfaceTest::testGoTo(const QDBusObjectPath& track)
{
}