summaryrefslogtreecommitdiff
path: root/metadatamodel.cpp
blob: 387747b6c8c2099fefd63bb430395a36048e96ee (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
/*
 * Copyright 2008  Alex Merry <alex.merry@kdemail.net>
 *
 * 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 "metadatamodel.h"
#include <QDBusArgument>
#include <qdebug.h>

static QString formatTimeNs(qlonglong time)
{
    qlonglong secs = time / 1000000;
    qlonglong mins = secs / 60;
    secs = secs % 60;
    return QString::number(time) + "ns ("
            + QString::number(mins) + ":"
            + QString::number(secs).rightJustified(2, '0') + ")";
}

MetadataModel::MetadataModel(QObject* parent)
    : QAbstractTableModel(parent)
{
}
MetadataModel::MetadataModel(const QVariantMap& metadata, QObject* parent)
    : QAbstractTableModel(parent),
      m_metadata(metadata),
      m_keys(metadata.keys())
{
}

int MetadataModel::rowCount(const QModelIndex& parent) const
{
    Q_UNUSED(parent)

    return m_metadata.count();
}

int MetadataModel::columnCount(const QModelIndex& parent) const
{
    Q_UNUSED(parent)

    return 2;
}

QVariant MetadataModel::data(const QModelIndex& index, int role) const
{
    if (index.isValid() && index.column() < 2 && index.row() < rowCount()) {
        if (role == Qt::DisplayRole) {
            QString key = m_keys[index.row()];
            if (index.column() == 0) {
                return key;
            } else {
                if (m_metadata[key].canConvert<QDBusArgument>()) {
                    qDebug() << "Unmarshalled type in metadata map for entry" << key;
                    return QVariant();
                } else if (m_metadata[key].canConvert<QDBusObjectPath>()) {
                    return m_metadata[key].value<QDBusObjectPath>().path();
                } else {
                    if (key == "mpris:length" && m_metadata[key].canConvert(QVariant::LongLong)) {
                        return formatTimeNs(m_metadata[key].toLongLong());
                    } else {
                        return m_metadata[key];
                    }
                }
            }
        }
    }
    return QVariant();
}

void MetadataModel::setMetadata(const QVariantMap& metadata)
{
    m_metadata = metadata;
    m_keys = m_metadata.keys();
    reset();
}

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