kwave  18.07.70
MetaData.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  MetaData.cpp - base class for associated meta data
3  -------------------
4  begin : Sat Jan 23 2010
5  copyright : (C) 2010 by Thomas Eschenbacher
6  email : Thomas.Eschenbacher@gmx.de
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #include "config.h"
19 
20 #include <QApplication>
21 #include <QDateTime>
22 #include <QMutexLocker>
23 #include <QUuid>
24 
25 #include "libkwave/MetaData.h"
26 #include "libkwave/String.h"
27 
28 //***************************************************************************
29 // initializers of the standard property names
30 const QString Kwave::MetaData::STDPROP_TYPE( _("STDPROP_TYPE"));
31 const QString Kwave::MetaData::STDPROP_TRACKS( _("STDPROP_TRACKS"));
32 const QString Kwave::MetaData::STDPROP_START( _("STDPROP_START"));
33 const QString Kwave::MetaData::STDPROP_END( _("STDPROP_END"));
34 const QString Kwave::MetaData::STDPROP_POS( _("STDPROP_POS"));
35 const QString Kwave::MetaData::STDPROP_DESCRIPTION(_("STDPROP_DESCRIPTION"));
36 
37 //***************************************************************************
39  :m_data(Q_NULLPTR)
40 {
41 }
42 
43 //***************************************************************************
45  :m_data(other.m_data)
46 {
47 }
48 
49 //***************************************************************************
51  :m_data(new MetaDataPriv)
52 {
53  setScope(scope);
54 }
55 
56 //***************************************************************************
58 {
59  m_data = Q_NULLPTR;
60 }
61 
62 //***************************************************************************
64 {
65  if (m_data) m_data->m_properties.clear();
66 }
67 
68 //***************************************************************************
70 {
71  return (!m_data || (m_data->m_properties.isEmpty()));
72 }
73 
74 //***************************************************************************
75 QString Kwave::MetaData::id() const
76 {
77  return (m_data) ? m_data->m_id : QString();
78 }
79 
80 //***************************************************************************
82 {
83  return (m_data) ? m_data->m_scope : None;
84 }
85 
86 //***************************************************************************
88 {
89  if (m_data) m_data->m_scope = scope;
90 }
91 
92 //***************************************************************************
93 void Kwave::MetaData::setProperty(const QString &p, const QVariant &value)
94 {
95  if (m_data) {
96  if (value.isValid())
97  m_data->m_properties[p] = value;
98  else
99  m_data->m_properties.remove(p);
100  }
101 }
102 
103 //***************************************************************************
104 bool Kwave::MetaData::hasProperty(const QString &property) const
105 {
106  return (m_data && m_data->m_properties.contains(property));
107 }
108 
109 //***************************************************************************
110 QVariant Kwave::MetaData::property(const QString &p) const
111 {
112  if (m_data && m_data->m_properties.contains(p))
113  return m_data->m_properties[p];
114  else
115  return QVariant();
116 }
117 
118 //***************************************************************************
119 QVariant &Kwave::MetaData::property(const QString &p)
120 {
121  if (m_data && m_data->m_properties.contains(p))
122  return m_data->m_properties[p];
123  else {
124  static QVariant dummy;
125  dummy.clear();
126  return dummy;
127  }
128 }
129 
130 //***************************************************************************
132 {
133  if (!m_data && !other.m_data)
134  return true; // both are null objects
135 
136  if ((!m_data) ^ (!other.m_data))
137  return false; // only one is a null object
138 
139  if (m_data->m_scope != other.m_data->m_scope)
140  return false; // different scope
141 
142  if (m_data->m_properties != other.m_data->m_properties)
143  return false; // properties differ
144 
145  return true; // no mismatch found
146 }
147 
148 //***************************************************************************
149 QStringList Kwave::MetaData::keys() const
150 {
151  return (m_data) ? m_data->m_properties.keys() : QStringList();
152 }
153 
154 //***************************************************************************
156 {
157  QStringList list;
161  return list;
162 }
163 
164 //***************************************************************************
166 {
167  // single position?
168  if ((scope() & Kwave::MetaData::Position) &&
170  bool ok = false;
171  sample_index_t pos =
172  property(Kwave::MetaData::STDPROP_POS).toULongLong(&ok);
173  if (ok) return pos;
174  }
175 
176  // start sample index given
177  if ((scope() & Kwave::MetaData::Range) &&
179  bool ok = false;
180  sample_index_t start = static_cast<sample_index_t>(
181  property(Kwave::MetaData::STDPROP_START).toULongLong(&ok));
182  if (ok) return start;
183  }
184 
185  // fallback: start at zero
186  return 0;
187 }
188 
189 //***************************************************************************
191 {
192  // single position?
193  if ((scope() & Kwave::MetaData::Position) &&
195  bool ok = false;
196  sample_index_t pos =
197  property(Kwave::MetaData::STDPROP_POS).toULongLong(&ok);
198  if (ok) return pos;
199  }
200 
201  // bound to a scope
202  if (scope() & Kwave::MetaData::Range) {
203  // end sample index given
205  bool ok = false;
206  sample_index_t end = static_cast<sample_index_t>(
207  property(Kwave::MetaData::STDPROP_END).toULongLong(&ok));
208  if (ok) return end;
209  }
210 
211  // fallback: no end => use start
213  bool ok = false;
214  sample_index_t start = static_cast<sample_index_t>(
215  property(Kwave::MetaData::STDPROP_START).toULongLong(&ok));
216  if (ok) return start;
217  }
218  }
219 
220  // fallback: infinite
221  return SAMPLE_INDEX_MAX;
222 }
223 
224 //***************************************************************************
225 QList<unsigned int> Kwave::MetaData::boundTracks() const
226 {
227  QList<unsigned int> tracks;
229  const QList<QVariant> v_track_list =
231  foreach (const QVariant &v, v_track_list) {
232  bool ok = false;
233  unsigned int t = v.toUInt(&ok);
234  if (ok) tracks += t;
235  }
236  }
237  return tracks;
238 }
239 
240 //***************************************************************************
242 {
243  QString scope_list;
244  const Scope s = scope();
245  if (s == All)
246  scope_list = _("all");
247  else {
248  if (s & Signal) scope_list += _(" signal");
249  if (s & Track) scope_list += _(" track");
250  if (s & Range) scope_list += _(" range");
251  if (s & Position) scope_list += _(" position");
252  }
253  qDebug(" scope =%s", DBG(scope_list));
254  const QStringList props = keys();
255  foreach (const QString &p, props) {
256  QVariant prop = property(p);
257  const QList<QVariant> v_vals = prop.toList();
258  QString value;
259  if (!v_vals.isEmpty()) {
260  foreach (QVariant v, v_vals)
261  value += _("{") + v.toString() + _("'} ");
262  } else {
263  value += _("'") + prop.toString() + _("'");
264  }
265 
266  qDebug(" '%s' = %s", DBG(p), DBG(value));
267  }
268 }
269 
270 //***************************************************************************
271 //***************************************************************************
272 
275 
278 
279 //***************************************************************************
281  :QSharedData(),
282  m_id(newUid()),
283  m_scope(),
284  m_properties()
285 {
286 }
287 
288 //***************************************************************************
290  :QSharedData(),
291  m_id(other.m_id),
292  m_scope(other.m_scope),
294 {
295 }
296 
297 //***************************************************************************
299 {
300 }
301 
302 //***************************************************************************
304 {
305  // create a new unique ID:
306  // <64 bit number> - <date/time> - <session id> - <pseudo uuid from Qt>
307  QMutexLocker _lock(&m_id_lock);
308 
309  QString uid;
310 
311  uid += QString::number(++m_id_counter, 16);
312  uid += _("-");
313  uid += QDateTime::currentDateTime().toString(Qt::ISODate);
314  uid += _("-");
315  uid += qApp->sessionKey();
316  uid += _("-");
317  uid += QUuid::createUuid().toString();
318 
319  return uid;
320 }
321 
322 //***************************************************************************
323 //***************************************************************************
static QStringList positionBoundPropertyNames()
Definition: MetaData.cpp:155
bool hasProperty(const QString &p) const
Definition: MetaData.cpp:104
static const QString STDPROP_POS
Definition: MetaData.h:53
QStringList keys() const
Definition: MetaData.cpp:149
static const QString STDPROP_TRACKS
Definition: MetaData.h:44
Scope scope() const
Definition: MetaData.cpp:81
bool operator==(const MetaData &other) const
Definition: MetaData.cpp:131
quint64 sample_index_t
Definition: Sample.h:28
virtual void clear()
Definition: MetaData.cpp:63
QString id() const
Definition: MetaData.cpp:75
virtual void dump() const
Definition: MetaData.cpp:241
QSharedDataPointer< MetaDataPriv > m_data
Definition: MetaData.h:250
static const QString STDPROP_TYPE
Definition: MetaData.h:41
virtual bool isNull() const
Definition: MetaData.cpp:69
static const QString STDPROP_START
Definition: MetaData.h:47
static quint64 m_id_counter
Definition: MetaData.h:243
void setProperty(const QString &p, const QVariant &value)
Definition: MetaData.cpp:93
virtual ~MetaData()
Definition: MetaData.cpp:57
void setScope(Scope scope)
Definition: MetaData.cpp:87
#define _(m)
Definition: memcpy.c:66
#define DBG(qs)
Definition: String.h:55
QList< unsigned int > boundTracks() const
Definition: MetaData.cpp:225
sample_index_t lastSample() const
Definition: MetaData.cpp:190
QVariant property(const QString &p) const
Definition: MetaData.cpp:110
static const QString STDPROP_END
Definition: MetaData.h:50
sample_index_t firstSample() const
Definition: MetaData.cpp:165
#define SAMPLE_INDEX_MAX
Definition: Sample.h:31
static const QString STDPROP_DESCRIPTION
Definition: MetaData.h:56