[3e4acc]: libkwave / MetaData.cpp Maximize Restore History

Download this file

MetaData.cpp    326 lines (279 with data), 10.0 kB

  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
/***************************************************************************
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 <QApplication>
#include <QDateTime>
#include <QMutexLocker>
#include <QUuid>
#include "MetaData.h"
//***************************************************************************
// initializers of the standard property names
const QString Kwave::MetaData::STDPROP_TYPE("STDPROP_TYPE");
const QString Kwave::MetaData::STDPROP_TRACKS("STDPROP_TRACKS");
const QString Kwave::MetaData::STDPROP_START("STDPROP_START");
const QString Kwave::MetaData::STDPROP_END("STDPROP_END");
const QString Kwave::MetaData::STDPROP_POS("STDPROP_POS");
const QString Kwave::MetaData::STDPROP_DESCRIPTION("STDPROP_DESCRIPTION");
//***************************************************************************
Kwave::MetaData::MetaData()
:m_data(0)
{
}
//***************************************************************************
Kwave::MetaData::MetaData(const Kwave::MetaData &other)
:m_data(other.m_data)
{
}
//***************************************************************************
Kwave::MetaData::MetaData(Scope scope)
:m_data(new MetaDataPriv)
{
setScope(scope);
}
//***************************************************************************
Kwave::MetaData::~MetaData()
{
}
//***************************************************************************
void Kwave::MetaData::clear()
{
if (m_data != 0) m_data->m_properties.clear();
}
//***************************************************************************
bool Kwave::MetaData::isNull() const
{
return ((m_data == 0) || (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_START;
list << Kwave::MetaData::STDPROP_END;
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;
}
// start sample index given
if ((scope() & Kwave::MetaData::Range) &&
hasProperty(Kwave::MetaData::STDPROP_START)) {
bool ok = false;
sample_index_t start = static_cast<sample_index_t>(
property(Kwave::MetaData::STDPROP_START).toULongLong(&ok));
if (ok) return start;
}
// 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;
}
// bound to a scope
if (scope() & Kwave::MetaData::Range) {
// end sample index given
if (hasProperty(Kwave::MetaData::STDPROP_END)) {
bool ok = false;
sample_index_t end = static_cast<sample_index_t>(
property(Kwave::MetaData::STDPROP_END).toULongLong(&ok));
if (ok) return end;
}
// fallback: no end => use start
if (hasProperty(Kwave::MetaData::STDPROP_START)) {
bool ok = false;
sample_index_t start = static_cast<sample_index_t>(
property(Kwave::MetaData::STDPROP_START).toULongLong(&ok));
if (ok) return start;
}
}
// fallback: infinite
return SAMPLE_INDEX_MAX;
}
//***************************************************************************
QList<unsigned int> Kwave::MetaData::boundTracks() const
{
QList<unsigned int> tracks;
if (hasProperty(Kwave::MetaData::STDPROP_TRACKS)) {
const QList<QVariant> v_track_list =
property(Kwave::MetaData::STDPROP_TRACKS).toList();
foreach (const QVariant &v, v_track_list) {
bool ok = false;
unsigned int t = v.toUInt(&ok);
if (ok) tracks += t;
}
}
return tracks;
}
//***************************************************************************
void Kwave::MetaData::dump() const
{
QString scope_list;
const Scope s = scope();
if (s == All)
scope_list = "all";
else {
if (s & Signal) scope_list += " signal";
if (s & Track) scope_list += " track";
if (s & Range) scope_list += " range";
if (s & Position) scope_list += " position";
}
qDebug(" scope =%s", scope_list.toLocal8Bit().data());
const QStringList props = keys();
foreach (const QString &p, props) {
QVariant prop = property(p);
const QList<QVariant> v_vals = prop.toList();
QString value;
if (!v_vals.isEmpty()) {
foreach (QVariant v, v_vals)
value += "{'" + v.toString() + "'} ";
} else {
value += "'" + prop.toString() + "'";
}
qDebug(" '%s' = %s",
p.toLocal8Bit().data(),
value.toLocal8Bit().data()
);
}
}
//***************************************************************************
//***************************************************************************
/** 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;
}
//***************************************************************************
//***************************************************************************