2010-03-23 23:11:46 +00:00
|
|
|
/* This file is part of Clementine.
|
2010-11-20 13:27:10 +00:00
|
|
|
Copyright 2010, David Sansome <me@davidsansome.com>
|
2010-03-23 23:11:46 +00:00
|
|
|
|
|
|
|
Clementine 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 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Clementine 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 Clementine. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2010-05-10 21:50:31 +00:00
|
|
|
#ifndef UTILITIES_H
|
|
|
|
#define UTILITIES_H
|
2010-03-23 14:07:05 +00:00
|
|
|
|
2010-10-24 23:46:05 +00:00
|
|
|
#include <QColor>
|
2010-10-17 20:53:15 +00:00
|
|
|
#include <QLocale>
|
2011-09-01 23:45:47 +02:00
|
|
|
#include <QCryptographicHash>
|
2011-07-26 13:02:59 +01:00
|
|
|
#include <QSize>
|
2010-05-10 21:50:31 +00:00
|
|
|
#include <QString>
|
2011-10-26 14:54:24 +02:00
|
|
|
#include <QUrl>
|
2010-03-23 14:07:05 +00:00
|
|
|
|
2010-08-23 19:13:27 +00:00
|
|
|
#include <boost/scoped_array.hpp>
|
|
|
|
|
2010-08-10 19:42:43 +00:00
|
|
|
class QIODevice;
|
2011-10-15 20:48:48 +01:00
|
|
|
class QMouseEvent;
|
2012-01-07 21:51:02 +00:00
|
|
|
class QXmlStreamReader;
|
2012-01-19 15:33:16 -08:00
|
|
|
struct QMetaObject;
|
2010-08-10 19:42:43 +00:00
|
|
|
|
2010-05-10 21:50:31 +00:00
|
|
|
namespace Utilities {
|
|
|
|
QString PrettyTime(int seconds);
|
2011-02-21 20:59:30 +00:00
|
|
|
QString PrettyTimeDelta(int seconds);
|
2011-02-13 18:34:30 +00:00
|
|
|
QString PrettyTimeNanosec(qint64 nanoseconds);
|
2010-07-03 21:05:55 +00:00
|
|
|
QString PrettySize(quint64 bytes);
|
2011-07-26 13:02:59 +01:00
|
|
|
QString PrettySize(const QSize& size);
|
2010-06-12 21:20:53 +00:00
|
|
|
QString WordyTime(quint64 seconds);
|
2011-02-13 18:34:30 +00:00
|
|
|
QString WordyTimeNanosec(qint64 nanoseconds);
|
2010-10-17 20:53:15 +00:00
|
|
|
QString Ago(int seconds_since_epoch, const QLocale& locale);
|
2012-08-27 12:24:28 +01:00
|
|
|
QString PrettyFutureDate(const QDate& date);
|
2010-07-29 22:16:12 +00:00
|
|
|
|
2010-10-24 23:46:05 +00:00
|
|
|
QString ColorToRgba(const QColor& color);
|
|
|
|
|
2010-07-29 22:16:12 +00:00
|
|
|
quint64 FileSystemCapacity(const QString& path);
|
|
|
|
quint64 FileSystemFreeSpace(const QString& path);
|
2010-08-01 14:13:27 +00:00
|
|
|
|
2011-01-28 21:43:10 +00:00
|
|
|
QString MakeTempDir(const QString template_name = QString());
|
2013-08-01 19:13:43 +02:00
|
|
|
QString GetTemporaryFileName();
|
2010-08-01 14:13:27 +00:00
|
|
|
void RemoveRecursive(const QString& path);
|
2011-01-18 23:10:22 +00:00
|
|
|
bool CopyRecursive(const QString& source, const QString& destination);
|
2010-08-10 19:42:43 +00:00
|
|
|
bool Copy(QIODevice* source, QIODevice* destination);
|
2010-12-09 12:34:08 +00:00
|
|
|
|
2011-10-26 14:54:24 +02:00
|
|
|
void OpenInFileBrowser(const QList<QUrl>& filenames);
|
2011-03-17 19:52:21 +00:00
|
|
|
|
2011-09-01 23:45:47 +02:00
|
|
|
enum HashFunction {
|
|
|
|
Md5_Algo,
|
|
|
|
Sha256_Algo,
|
2012-11-27 18:35:06 +01:00
|
|
|
Sha1_Algo,
|
2011-09-01 23:45:47 +02:00
|
|
|
};
|
|
|
|
QByteArray Hmac(const QByteArray& key, const QByteArray& data, HashFunction algo);
|
|
|
|
QByteArray HmacMd5(const QByteArray& key, const QByteArray& data);
|
2011-07-23 19:33:00 +01:00
|
|
|
QByteArray HmacSha256(const QByteArray& key, const QByteArray& data);
|
2012-11-27 18:35:06 +01:00
|
|
|
QByteArray HmacSha1(const QByteArray& key, const QByteArray& data);
|
2011-07-23 19:33:00 +01:00
|
|
|
QByteArray Sha256(const QByteArray& data);
|
|
|
|
|
2010-12-09 12:34:08 +00:00
|
|
|
|
2011-11-28 18:11:09 +00:00
|
|
|
// Picks an unused ephemeral port number. Doesn't hold the port open so
|
|
|
|
// there's the obvious race condition
|
|
|
|
quint16 PickUnusedPort();
|
|
|
|
|
|
|
|
|
2011-10-15 20:48:48 +01:00
|
|
|
// Forwards a mouse event to a different widget, remapping the event's widget
|
|
|
|
// coordinates relative to those of the target widget.
|
|
|
|
void ForwardMouseEvent(const QMouseEvent* e, QWidget* target);
|
|
|
|
|
|
|
|
// Checks if the mouse event was inside the widget's rectangle.
|
|
|
|
bool IsMouseEventInWidget(const QMouseEvent* e, const QWidget* widget);
|
|
|
|
|
2012-01-07 21:51:02 +00:00
|
|
|
// Reads all children of the current element, and returns with the stream
|
|
|
|
// reader either on the EndElement for the current element, or the end of the
|
|
|
|
// file - whichever came first.
|
|
|
|
void ConsumeCurrentElement(QXmlStreamReader* reader);
|
|
|
|
|
2012-03-04 20:47:58 +00:00
|
|
|
// Advances the stream reader until it finds an element with the given name.
|
|
|
|
// Returns false if the end of the document was reached before finding a
|
|
|
|
// matching element.
|
|
|
|
bool ParseUntilElement(QXmlStreamReader* reader, const QString& name);
|
|
|
|
|
|
|
|
// Parses a string containing an RFC822 time and date.
|
|
|
|
QDateTime ParseRFC822DateTime(const QString& text);
|
|
|
|
|
2012-03-05 18:15:45 +00:00
|
|
|
// Replaces some HTML entities with their normal characters.
|
|
|
|
QString DecodeHtmlEntities(const QString& text);
|
|
|
|
|
2012-01-16 14:22:30 +00:00
|
|
|
// Shortcut for getting a Qt-aware enum value as a string.
|
|
|
|
// Pass in the QMetaObject of the class that owns the enum, the string name of
|
|
|
|
// the enum and a valid value from that enum.
|
|
|
|
const char* EnumToString(const QMetaObject& meta, const char* name, int value);
|
|
|
|
|
2012-03-04 20:47:58 +00:00
|
|
|
QStringList Prepend(const QString& text, const QStringList& list);
|
|
|
|
QStringList Updateify(const QStringList& list);
|
|
|
|
|
2012-01-07 21:51:02 +00:00
|
|
|
|
2010-12-09 12:34:08 +00:00
|
|
|
enum ConfigPath {
|
2010-12-09 13:06:00 +00:00
|
|
|
Path_Root,
|
|
|
|
Path_AlbumCovers,
|
|
|
|
Path_NetworkCache,
|
|
|
|
Path_GstreamerRegistry,
|
2010-12-14 15:00:46 +00:00
|
|
|
Path_DefaultMusicLibrary,
|
2011-04-29 19:44:51 +00:00
|
|
|
Path_LocalSpotifyBlob,
|
2012-05-25 17:18:07 +01:00
|
|
|
Path_MoodbarCache,
|
2012-11-06 15:38:15 +01:00
|
|
|
Path_CacheRoot,
|
2010-12-09 12:34:08 +00:00
|
|
|
};
|
|
|
|
QString GetConfigPath(ConfigPath config);
|
2011-11-09 13:59:25 +01:00
|
|
|
|
|
|
|
// Returns the minor version of OS X (ie. 6 for Snow Leopard, 7 for Lion).
|
|
|
|
qint32 GetMacVersion();
|
2012-02-26 15:05:46 +00:00
|
|
|
|
|
|
|
// Borrowed from schedutils
|
|
|
|
enum IoPriority {
|
|
|
|
IOPRIO_CLASS_NONE = 0,
|
|
|
|
IOPRIO_CLASS_RT,
|
|
|
|
IOPRIO_CLASS_BE,
|
|
|
|
IOPRIO_CLASS_IDLE,
|
|
|
|
};
|
|
|
|
enum {
|
|
|
|
IOPRIO_WHO_PROCESS = 1,
|
|
|
|
IOPRIO_WHO_PGRP,
|
|
|
|
IOPRIO_WHO_USER,
|
|
|
|
};
|
|
|
|
static const int IOPRIO_CLASS_SHIFT = 13;
|
|
|
|
|
|
|
|
int SetThreadIOPriority(IoPriority priority);
|
|
|
|
int GetThreadId();
|
2012-03-09 15:25:49 +00:00
|
|
|
|
|
|
|
// Returns true if this machine has a battery.
|
|
|
|
bool IsLaptop();
|
2013-07-27 15:27:08 +10:00
|
|
|
|
|
|
|
QString SystemLanguageName();
|
2010-05-10 21:50:31 +00:00
|
|
|
}
|
|
|
|
|
2010-08-23 19:13:27 +00:00
|
|
|
class ScopedWCharArray {
|
|
|
|
public:
|
|
|
|
ScopedWCharArray(const QString& str);
|
|
|
|
|
|
|
|
QString ToString() const { return QString::fromWCharArray(data_.get()); }
|
|
|
|
|
|
|
|
wchar_t* get() const { return data_.get(); }
|
|
|
|
operator wchar_t*() const { return get(); }
|
|
|
|
|
|
|
|
int characters() const { return chars_; }
|
2010-08-28 13:52:29 +00:00
|
|
|
int bytes() const { return (chars_ + 1) * sizeof(wchar_t); }
|
2010-08-23 19:13:27 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
Q_DISABLE_COPY(ScopedWCharArray);
|
|
|
|
|
|
|
|
int chars_;
|
|
|
|
boost::scoped_array<wchar_t> data_;
|
|
|
|
};
|
|
|
|
|
2010-05-10 21:50:31 +00:00
|
|
|
#endif // UTILITIES_H
|