summaryrefslogtreecommitdiff
path: root/mpris2/rootinterfacetest.cpp
blob: eddcf5f557f498d4231b340230eabc53efc38d9c (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
/*
 * 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 "rootinterfacetest.h"

#include <QDBusInterface>
#include <QDBusMessage>
#include <QDBusReply>
#include <QDir>
#include <QSettings>
#include <QString>
#include <QTextStream>

#define MPRIS2_ROOT_IFACE "org.mpris.MediaPlayer2"

using namespace Mpris2;

RootInterfaceTest::RootInterfaceTest(const QString& service, QObject* parent)
    : InterfaceTest(MPRIS2_ROOT_IFACE, service, parent)
{
    // http://standards.freedesktop.org/basedir-spec/0.6/
    QStringList dataPaths;
    QByteArray xdgDataHomeVar = qgetenv("XDG_DATA_HOME");
    if (!xdgDataHomeVar.isEmpty()) {
        dataPaths << QFile::decodeName(xdgDataHomeVar);
    } else {
        dataPaths << QDir::homePath() + "/.local/share";
    }
    QByteArray xdgDataDirsVar = qgetenv("XDG_DATA_DIRS");
    if (xdgDataDirsVar.isEmpty()) {
        xdgDataDirsVar = "/usr/local/share:/usr/share";
    }
    Q_FOREACH(const QByteArray& dir, xdgDataDirsVar.split(':')) {
        dataPaths << QFile::decodeName(dir);
    }
    QDir::setSearchPaths("xdgdata", dataPaths);

    // http://www.iana.org/assignments/media-types/index.html
    m_rootMimetypes << "application" << "audio" << "example"
                    << "image" << "message" << "model"
                    << "multipart" << "text" << "video";
    QFile mimeTypesFile("/etc/mime.types");
    if (!mimeTypesFile.open(QIODevice::ReadOnly)) {
        qWarning("Cannot open /etc/mime.types; full mimetype checking disabled");
    } else {
        QTextStream mimeStream(&mimeTypesFile);
        // assume /etc/mime.types doesn't have ridiculous lines
        QString line = mimeStream.readLine();
        QRegExp wsRegExp("\\s");
        while (!line.isNull()) {
            line = line.trimmed();
            if (!line.isEmpty() && !line.startsWith('#')) {
                int wsPos = line.indexOf(wsRegExp);
                if (wsPos > 0) {
                    line = line.left(wsPos);
                }
                m_mimeTypes.insert(line);
            }
            line = mimeStream.readLine();
        }
    }
}

RootInterfaceTest::~RootInterfaceTest()
{
}

void RootInterfaceTest::checkPropertyIdentity(const QVariantMap& oldProps)
{
    if (checkNonEmptyStringPropValid("Identity", oldProps)) {
        QString identity = props.value("Identity").toString();
        if (("org.mpris.MediaPlayer2." + identity) == iface->service()) {
            emit interfaceWarning(Property, "Identity", "Identity is the same as the service name (one is user-readable, the other is the 'internal' name)");
        }
    }
}

void RootInterfaceTest::checkPropertyDesktopEntry(const QVariantMap& oldProps)
{
    if (checkNonEmptyStringPropValid("DesktopEntry", oldProps)) {
        QString desktopEntry = props.value("DesktopEntry").toString();
        QFile file("xdgdata:applications/" + desktopEntry + ".desktop");
        if (!file.exists()) {
            file.setFileName("xdgdata:applications/" + desktopEntry.replace('-', '/') + ".desktop");
            if (!file.exists()) {
                emit interfaceWarning(Property, "DesktopEntry", "Could not find the desktop file");
                return;
            }
        }

        QSettings desktopFile(file.fileName(), QSettings::IniFormat);
        desktopFile.beginGroup("Desktop Entry");
        QString generalName = desktopFile.value("Name").toString();
        if (generalName.isEmpty()) {
            emit interfaceWarning(Property, "DesktopEntry", "Failed to read the Name entry of " + desktopFile.fileName());
        } else if (generalName != props.value("Identity").toString()) {
            // TODO: translated names?  http://standards.freedesktop.org/desktop-entry-spec/1.1/ar01s04.html
            emit interfaceWarning(Property, "DesktopEntry", "The Name entry of " + desktopFile.fileName() + " (" + generalName + ") is different from Identity");
        }
    }
}

void RootInterfaceTest::checkPropertySupportedUriSchemes(const QVariantMap& oldProps)
{
    if (checkPropValid("SupportedUriSchemes", QVariant::StringList, oldProps)) {
        QStringList uriSchemes = props.value("SupportedUriSchemes").toStringList();
        if (uriSchemes.isEmpty()) {
            if (!props.value("SupportedMimeTypes").toStringList().isEmpty()) {
                emit interfaceWarning(Property, "SupportedUriSchemes",
                                      "The media player lists supported mimetypes, but claims not to support any URI schemes");
            }
            return;
        }

        if (!uriSchemes.contains("file")) {
            emit interfaceWarning(Property, "SupportedUriSchemes", "\"file\" is not listed as a supported URI scheme (this is unusual)");
        }

        // TODO: check the uri schemes against a list of common/registered ones?

        QMap<QString,int> seenCount;
        Q_FOREACH (const QString& scheme, uriSchemes) {
            ++seenCount[scheme];
        }
        QMap<QString,int>::ConstIterator it = seenCount.constBegin();
        for (; it != seenCount.constEnd(); ++it) {
            if (it.value() > 1) {
                emit interfaceWarning(Property, "SupportedUriSchemes",
                                    "\"" + it.key() + "\" appeared " + QString::number(it.value()) + " times");
            }
        }
    }
}

void RootInterfaceTest::checkPropertySupportedMimeTypes(const QVariantMap& oldProps)
{
    if (checkPropValid("SupportedMimeTypes", QVariant::StringList, oldProps)) {
        QStringList mimeTypes = props.value("SupportedMimeTypes").toStringList();
        if (mimeTypes.isEmpty()) {
            if (!props.value("SupportedUriSchemes").toStringList().isEmpty()) {
                emit interfaceWarning(Property, "SupportedMimeTypes",
                                      "The media player lists supported URI schemes, but claims not to support any mimetypes");
            }
            return;
        }

        QMap<QString,int> seenCount;
        Q_FOREACH (const QString& mimeType, mimeTypes) {
            ++seenCount[mimeType];
            if (!m_checkedMimeTypes.contains(mimeType) && !m_mimeTypes.contains(mimeType)) {
                int slashIndex = mimeType.indexOf('/');
                if (slashIndex < 1) {
                    emit interfaceError(Property, "SupportedMimeTypes", "\"" + mimeType + "\" is not a valid mimetype");
                } else {
                    QString rootType = mimeType.left(slashIndex);
                    QString subType = mimeType.mid(slashIndex + 1);
                    if (!m_rootMimetypes.contains(rootType)) {
                        emit interfaceError(Property, "SupportedMimeTypes",
                                            "\"" + mimeType + "\" is not a valid mimetype (\""
                                            + rootType + "\" is not a valid content type)");
                    } else if (!m_mimeTypes.isEmpty() && !subType.startsWith("x-", Qt::CaseInsensitive)) {
                        emit interfaceWarning(Property, "SupportedMimeTypes",
                                            "\"" + mimeType + "\" is not a recognized mimetype");
                    }
                }
                m_checkedMimeTypes.insert(mimeType);
            }
        }

        QMap<QString,int>::ConstIterator it = seenCount.constBegin();
        for (; it != seenCount.constEnd(); ++it) {
            if (it.value() > 1) {
                emit interfaceWarning(Property, "SupportedMimeTypes",
                                    "\"" + it.key() + "\" appeared " + QString::number(it.value()) + " times");
            }
        }
    }
}

void RootInterfaceTest::checkProps(const QVariantMap& oldProps)
{
    checkPropValid("CanQuit", QVariant::Bool, oldProps);
    checkPropValid("CanRaise", QVariant::Bool, oldProps);
    checkPropValid("HasTrackList", QVariant::Bool, oldProps);
    if (props.contains("Fullscreen") || props.contains("CanSetFullscreen")) {
        if (!props.contains("Fullscreen")) {
            emit interfaceError(Property, "Fullscreen", "If you provide CanSetFullscreen, you must also provide Fullscreen");
        } else {
            checkPropValid("Fullscreen", QVariant::Bool, oldProps);
        }
        if (!props.contains("CanSetFullscreen")) {
            emit interfaceError(Property, "CanSetFullscreen", "If you provide Fullscreen, you must also provide CanSetFullscreen");
        } else {
            checkPropValid("CanSetFullscreen", QVariant::Bool, oldProps);
        }
    }
    checkPropertyIdentity(oldProps);
    checkPropertyDesktopEntry(oldProps);
    checkPropertySupportedUriSchemes(oldProps);
    checkPropertySupportedMimeTypes(oldProps);
}

void RootInterfaceTest::checkUpdatedProperty(const QString& propName)
{
    if (propName == "CanQuit") {
        checkPropValid("CanQuit", QVariant::Bool);
    } else if (propName == "CanRaise") {
        checkPropValid("CanRaise", QVariant::Bool);
    } else if (propName == "CanSetFullscreen") {
        checkPropValid("CanSetFullscreen", QVariant::Bool);
    } else if (propName == "Fullscreen") {
        checkPropValid("Fullscreen", QVariant::Bool);
    } else if (propName == "HasTrackList") {
        checkPropValid("HasTrackList", QVariant::Bool);
    } else if (propName == "Identity") {
        checkPropertyIdentity();
    } else if (propName == "DesktopEntry") {
        checkPropertyDesktopEntry();
    } else if (propName == "SupportedUriSchemes") {
        checkPropertySupportedUriSchemes();
    } else if (propName == "SupportedMimeTypes") {
        checkPropertySupportedMimeTypes();
    }
}

void RootInterfaceTest::testQuit()
{
    QDBusReply<void> reply = iface->call("Quit");
    if (!reply.isValid() && props["CanQuit"].toBool()) {
        emit interfaceError(Method, "Quit", "CanQuit is true, but call to Quit failed with error " + reply.error().message());
    } else if (reply.isValid()) {
        if (!props["CanQuit"].toBool()) {
            emit interfaceInfo(Method, "Quit", "Call to Quit successful, even though CanQuit is false");
        } else {
            emit interfaceInfo(Method, "Quit", "Call to Quit successful; the media player should now quit");
            // TODO: check to see if it has gone away in a few seconds
        }
    }
}

void RootInterfaceTest::testRaise()
{
    QDBusReply<void> reply = iface->call("Raise");
    if (!reply.isValid() && props["CanRaise"].toBool()) {
        emit interfaceError(Method, "Raise", "CanRaise is true, but call to Raise failed with error " + reply.error().message());
    } else if (reply.isValid()) {
        if (!props["CanRaise"].toBool()) {
            emit interfaceInfo(Method, "Raise", "Call to Raise successful, even though CanRaise is false");
        } else {
            emit interfaceInfo(Method, "Raise", "Call to Raise successful; the media player should now be raised");
        }
    }
}

void RootInterfaceTest::testSetFullscreen(bool value)
{
    setProp("Fullscreen", QDBusVariant(QVariant(value)),
            props["CanSetFullscreen"].toBool()
            ? PropDisallowErrors
            : PropAllowErrors);
}

void RootInterfaceTest::checkConsistency(const QVariantMap& oldProps)
{
}

// vim:et:sw=4:sts=4