~ubuntu-branches/ubuntu/saucy/clementine/saucy

« back to all changes in this revision

Viewing changes to src/core/utilities.cpp

  • Committer: Package Import Robot
  • Author(s): Thomas PIERSON
  • Date: 2012-01-01 20:43:39 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20120101204339-lsb6nndwhfy05sde
Tags: 1.0.1+dfsg-1
New upstream release. (Closes: #653926, #651611, #657391)

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 
18
18
#include "timeconstants.h"
19
19
#include "utilities.h"
20
 
 
21
 
#include <QCoreApplication>
 
20
#include "core/logging.h"
 
21
 
 
22
#include "sha2.h"
 
23
 
 
24
#include <QApplication>
22
25
#include <QDateTime>
23
26
#include <QDesktopServices>
24
27
#include <QDir>
25
28
#include <QIODevice>
 
29
#include <QMouseEvent>
26
30
#include <QStringList>
 
31
#include <QTcpServer>
27
32
#include <QTemporaryFile>
28
33
#include <QUrl>
 
34
#include <QWidget>
 
35
#include <QXmlStreamReader>
29
36
#include <QtDebug>
30
37
#include <QtGlobal>
31
38
 
37
44
 
38
45
#ifdef Q_OS_DARWIN
39
46
#  include "core/mac_startup.h"
 
47
#  include "CoreServices/CoreServices.h"
40
48
#endif
41
49
 
42
50
#include <boost/scoped_array.hpp>
189
197
  QDir dir(source);
190
198
  foreach (const QString& child, dir.entryList(QDir::NoDotAndDotDot | QDir::Dirs)) {
191
199
    if (!CopyRecursive(source + "/" + child, dest_path)) {
192
 
      qWarning() << "Failed to copy dir" << source + "/" + child << "to" << dest_path;
 
200
      qLog(Warning) << "Failed to copy dir" << source + "/" + child << "to" << dest_path;
193
201
      return false;
194
202
    }
195
203
  }
196
204
 
197
205
  foreach (const QString& child, dir.entryList(QDir::NoDotAndDotDot | QDir::Files)) {
198
206
    if (!QFile::copy(source + "/" + child, dest_path + "/" + child)) {
199
 
      qWarning() << "Failed to copy file" << source + "/" + child << "to" << dest_path;
 
207
      qLog(Warning) << "Failed to copy file" << source + "/" + child << "to" << dest_path;
200
208
      return false;
201
209
    }
202
210
  }
262
270
      return GetConfigPath(Path_Root) +
263
271
          QString("/gst-registry-%1-bin").arg(QCoreApplication::applicationVersion());
264
272
 
265
 
    case Path_Scripts:
266
 
      return GetConfigPath(Path_Root) + "/scripts";
267
 
 
268
273
    case Path_DefaultMusicLibrary:
269
274
      #ifdef Q_OS_DARWIN
270
275
        return mac::GetMusicDirectory();
272
277
        return QDir::homePath();
273
278
      #endif
274
279
 
 
280
    case Path_LocalSpotifyBlob:
 
281
      return GetConfigPath(Path_Root) + "/spotifyblob";
 
282
 
275
283
    default:
276
284
      qFatal("%s", Q_FUNC_INFO);
277
285
      return QString::null;
278
286
  }
279
287
}
280
288
 
281
 
void OpenInFileBrowser(const QStringList& filenames) {
 
289
#ifdef Q_OS_DARWIN
 
290
qint32 GetMacVersion() {
 
291
  SInt32 minor_version;
 
292
  Gestalt(gestaltSystemVersionMinor, &minor_version);
 
293
  return minor_version;
 
294
}
 
295
#endif  // Q_OS_DARWIN
 
296
 
 
297
void OpenInFileBrowser(const QList<QUrl>& urls) {
282
298
  QSet<QString> dirs;
283
299
 
284
 
  foreach (const QString& filename, filenames) {
285
 
    // Ignore things that look like URLs
286
 
    if (filename.contains("://"))
287
 
      continue;
288
 
 
289
 
    if (!QFile::exists(filename))
290
 
      continue;
291
 
 
292
 
    const QString directory = QFileInfo(filename).dir().path();
 
300
  foreach (const QUrl& url, urls) {
 
301
    if (url.scheme() != "file") {
 
302
      continue;
 
303
    }
 
304
    QString path = url.toLocalFile();
 
305
 
 
306
    if (!QFile::exists(path))
 
307
      continue;
 
308
 
 
309
    const QString directory = QFileInfo(path).dir().path();
293
310
 
294
311
    if (dirs.contains(directory))
295
312
      continue;
299
316
  }
300
317
}
301
318
 
 
319
QByteArray Hmac(const QByteArray& key, const QByteArray& data, HashFunction method) {
 
320
  const int kBlockSize = 64; // bytes
 
321
  Q_ASSERT(key.length() <= kBlockSize);
 
322
 
 
323
  QByteArray inner_padding(kBlockSize, char(0x36));
 
324
  QByteArray outer_padding(kBlockSize, char(0x5c));
 
325
 
 
326
  for (int i=0 ; i<key.length() ; ++i) {
 
327
    inner_padding[i] = inner_padding[i] ^ key[i];
 
328
    outer_padding[i] = outer_padding[i] ^ key[i];
 
329
  }
 
330
  if (Md5_Algo == method) {
 
331
    return QCryptographicHash::hash(outer_padding +
 
332
                                    QCryptographicHash::hash(inner_padding + data,
 
333
                                                             QCryptographicHash::Md5),
 
334
                                    QCryptographicHash::Md5);
 
335
  } else { // Sha256_Algo, currently default
 
336
    return Sha256(outer_padding + Sha256(inner_padding + data));
 
337
  }
 
338
}
 
339
 
 
340
QByteArray HmacSha256(const QByteArray& key, const QByteArray& data) {
 
341
  return Hmac(key, data, Sha256_Algo);
 
342
}
 
343
 
 
344
QByteArray HmacMd5(const QByteArray& key, const QByteArray& data) {
 
345
  return Hmac(key, data, Md5_Algo);
 
346
}
 
347
 
 
348
QByteArray Sha256(const QByteArray& data) {
 
349
  SHA256_CTX context;
 
350
  SHA256_Init(&context);
 
351
  SHA256_Update(&context, reinterpret_cast<const u_int8_t*>(data.constData()),
 
352
                data.length());
 
353
 
 
354
  QByteArray ret(SHA256_DIGEST_LENGTH, '\0');
 
355
  SHA256_Final(reinterpret_cast<u_int8_t*>(ret.data()), &context);
 
356
 
 
357
  return ret;
 
358
}
 
359
 
 
360
QString PrettySize(const QSize& size) {
 
361
  return QString::number(size.width()) + "x" +
 
362
         QString::number(size.height());
 
363
}
 
364
 
 
365
void ForwardMouseEvent(const QMouseEvent* e, QWidget* target) {
 
366
  QMouseEvent c(e->type(), target->mapFromGlobal(e->globalPos()),
 
367
                e->globalPos(), e->button(), e->buttons(), e->modifiers());
 
368
 
 
369
  QApplication::sendEvent(target, &c);
 
370
}
 
371
 
 
372
bool IsMouseEventInWidget(const QMouseEvent* e, const QWidget* widget) {
 
373
  return widget->rect().contains(widget->mapFromGlobal(e->globalPos()));
 
374
}
 
375
 
 
376
quint16 PickUnusedPort() {
 
377
  forever {
 
378
    const quint16 port = 49152 + qrand() % 16384;
 
379
 
 
380
    QTcpServer server;
 
381
    if (server.listen(QHostAddress::Any, port)) {
 
382
      return port;
 
383
    }
 
384
  }
 
385
}
 
386
 
 
387
void ConsumeCurrentElement(QXmlStreamReader* reader) {
 
388
  int level = 1;
 
389
  while (level != 0 && !reader->atEnd()) {
 
390
    switch (reader->readNext()) {
 
391
      case QXmlStreamReader::StartElement: ++level; break;
 
392
      case QXmlStreamReader::EndElement:   --level; break;
 
393
      default: break;
 
394
    }
 
395
  }
 
396
}
 
397
 
302
398
}  // namespace Utilities
303
399
 
304
400