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
|
/***************************************************************************
MetaData.cpp - base class for associated meta data
-------------------
begin : Sat Jan 23 2010
copyright : (C) 2010 by Thomas Eschenbacher
email : Thomas.Eschenbacher@gmx.de
***************************************************************************/
/***************************************************************************
* *
* 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. *
* *
***************************************************************************/
#include "config.h"
#include <new>
#include <QApplication>
#include <QDateTime>
#include <QMetaType>
#include <QMutexLocker>
#include <QUuid>
#include "libkwave/MetaData.h"
#include "libkwave/String.h"
//***************************************************************************
// initializers of the standard property names
const QString Kwave::MetaData::STDPROP_TYPE( _("STDPROP_TYPE"));
const QString Kwave::MetaData::STDPROP_POS( _("STDPROP_POS"));
const QString Kwave::MetaData::STDPROP_DESCRIPTION(_("STDPROP_DESCRIPTION"));
//***************************************************************************
Kwave::MetaData::MetaData()
:m_data(nullptr)
{
}
//***************************************************************************
Kwave::MetaData::MetaData(const Kwave::MetaData &other)
:m_data(other.m_data)
{
}
//***************************************************************************
Kwave::MetaData::MetaData(Scope scope)
:m_data(new(std::nothrow) MetaDataPriv)
{
setScope(scope);
}
//***************************************************************************
Kwave::MetaData::~MetaData()
{
m_data = nullptr;
}
//***************************************************************************
void Kwave::MetaData::clear()
{
if (m_data) m_data->m_properties.clear();
}
//***************************************************************************
bool Kwave::MetaData::isNull() const
{
return (!m_data || (m_data->m_properties.isEmpty()));
}
//***************************************************************************
QString Kwave::MetaData::id() const
{
return (m_data) ? m_data->m_id : QString();
}
//***************************************************************************
Kwave::MetaData::Scope Kwave::MetaData::scope() const
{
return (m_data) ? m_data->m_scope : None;
}
//***************************************************************************
void Kwave::MetaData::setScope(Kwave::MetaData::Scope scope)
{
if (m_data) m_data->m_scope = scope;
}
//***************************************************************************
void Kwave::MetaData::setProperty(const QString &p, const QVariant &value)
{
if (m_data) {
if (value.isValid())
m_data->m_properties[p] = value;
else
m_data->m_properties.remove(p);
}
}
//***************************************************************************
bool Kwave::MetaData::hasProperty(const QString &property) const
{
return (m_data && m_data->m_properties.contains(property));
}
//***************************************************************************
QVariant Kwave::MetaData::property(const QString &p) const
{
if (m_data && m_data->m_properties.contains(p))
return m_data->m_properties[p];
else
return QVariant();
}
//***************************************************************************
QVariant &Kwave::MetaData::property(const QString &p)
{
if (m_data && m_data->m_properties.contains(p))
return m_data->m_properties[p];
else {
static QVariant dummy;
dummy.clear();
return dummy;
}
}
//***************************************************************************
bool Kwave::MetaData::operator == (const Kwave::MetaData &other) const
{
if (!m_data && !other.m_data)
return true; // both are null objects
if ((!m_data) ^ (!other.m_data))
return false; // only one is a null object
if (m_data->m_scope != other.m_data->m_scope)
return false; // different scope
if (m_data->m_properties != other.m_data->m_properties)
return false; // properties differ
return true; // no mismatch found
}
//***************************************************************************
QStringList Kwave::MetaData::keys() const
{
return (m_data) ? m_data->m_properties.keys() : QStringList();
}
//***************************************************************************
QStringList Kwave::MetaData::positionBoundPropertyNames()
{
QStringList list;
list << Kwave::MetaData::STDPROP_POS;
return list;
}
//***************************************************************************
sample_index_t Kwave::MetaData::firstSample() const
{
// single position?
if ((scope() & Kwave::MetaData::Position) &&
hasProperty(Kwave::MetaData::STDPROP_POS)) {
bool ok = false;
sample_index_t pos =
property(Kwave::MetaData::STDPROP_POS).toULongLong(&ok);
if (ok) return pos;
}
// fallback: start at zero
return 0;
}
//***************************************************************************
sample_index_t Kwave::MetaData::lastSample() const
{
// single position?
if ((scope() & Kwave::MetaData::Position) &&
hasProperty(Kwave::MetaData::STDPROP_POS)) {
bool ok = false;
sample_index_t pos =
property(Kwave::MetaData::STDPROP_POS).toULongLong(&ok);
if (ok) return pos;
}
// fallback: infinite
return SAMPLE_INDEX_MAX;
}
//***************************************************************************
static inline QString shortened(const QString &str)
{
if (str.length() < 1024)
return _("'") + str + _("'");
else
return _("'") + str.left(1024) + _("'...");
}
//***************************************************************************
void Kwave::MetaData::dump() const
{
QString scope_list;
const Scope s = scope();
if (s & Signal) scope_list += _(" signal");
if (s & Position) scope_list += _(" position");
qDebug(" scope =%s", DBG(scope_list));
const QStringList props = keys();
foreach (const QString &name, props) {
QVariant val = property(name);
QStringList list = val.toStringList();
if (list.size() > 1)
{
int index = 0;
foreach (const QString &v, list)
{
if (index > 32) {
qDebug(" ..."); // emergency break
break;
}
qDebug(" '%s[%2d]' = %s", DBG(name), index++,
DBG(shortened(v)));
}
}
else
qDebug(" '%s' = %s", DBG(name), DBG(shortened(val.toString())));
}
}
//***************************************************************************
//***************************************************************************
/** static initializer: counter for unique id generation */
quint64 Kwave::MetaData::MetaDataPriv::m_id_counter = 0;
/** static initializer: mutex for protecting the id generator */
QMutex Kwave::MetaData::MetaDataPriv::m_id_lock;
//***************************************************************************
Kwave::MetaData::MetaDataPriv::MetaDataPriv()
:QSharedData(),
m_id(newUid()),
m_scope(),
m_properties()
{
}
//***************************************************************************
Kwave::MetaData::MetaDataPriv::MetaDataPriv(const MetaDataPriv &other)
:QSharedData(),
m_id(other.m_id),
m_scope(other.m_scope),
m_properties(other.m_properties)
{
}
//***************************************************************************
Kwave::MetaData::MetaDataPriv::~MetaDataPriv()
{
}
//***************************************************************************
QString Kwave::MetaData::MetaDataPriv::newUid()
{
// create a new unique ID:
// <64 bit number> - <date/time> - <session id> - <pseudo uuid from Qt>
QMutexLocker _lock(&m_id_lock);
QString uid;
uid += QString::number(++m_id_counter, 16);
uid += _("-");
uid += QDateTime::currentDateTime().toString(Qt::ISODate);
uid += _("-");
uid += qApp->sessionKey();
uid += _("-");
uid += QUuid::createUuid().toString();
return uid;
}
//***************************************************************************
//***************************************************************************
|