Browse Source

Convert misc class to a namespace instead.

adaptive-webui-19844
sledgehammer999 12 years ago
parent
commit
7a16146f6f
  1. 18
      src/misc.cpp
  2. 59
      src/misc.h

18
src/misc.cpp

@ -175,7 +175,7 @@ void misc::shutdownComputer(bool sleep) {
if (sleep) if (sleep)
SetSuspendState(false, false, false); SetSuspendState(false, false, false);
else else
InitiateSystemShutdownA(0, tr("qBittorrent will shutdown the computer now because all downloads are complete.").toLocal8Bit().data(), 10, true, false); InitiateSystemShutdownA(0, QCoreApplication::translate("misc", "qBittorrent will shutdown the computer now because all downloads are complete.").toLocal8Bit().data(), 10, true, false);
// Disable shutdown privilege. // Disable shutdown privilege.
tkp.Privileges[0].Attributes = 0; tkp.Privileges[0].Attributes = 0;
@ -233,17 +233,17 @@ int misc::pythonVersion() {
// value must be given in bytes // value must be given in bytes
QString misc::friendlyUnit(qreal val, bool is_speed) { QString misc::friendlyUnit(qreal val, bool is_speed) {
if (val < 0) if (val < 0)
return tr("Unknown", "Unknown (size)"); return QCoreApplication::translate("misc", "Unknown", "Unknown (size)");
int i = 0; int i = 0;
while(val >= 1024. && i++<6) while(val >= 1024. && i++<6)
val /= 1024.; val /= 1024.;
QString ret; QString ret;
if (i == 0) if (i == 0)
ret = QString::number((long)val) + " " + tr(units[0].source, units[0].comment); ret = QString::number((long)val) + " " + QCoreApplication::translate("misc", units[0].source, units[0].comment);
else else
ret = QString::number(val, 'f', 1) + " " + tr(units[i].source, units[i].comment); ret = QString::number(val, 'f', 1) + " " + QCoreApplication::translate("misc", units[i].source, units[i].comment);
if (is_speed) if (is_speed)
ret += tr("/s", "per second"); ret += QCoreApplication::translate("misc", "/s", "per second");
return ret; return ret;
} }
@ -379,21 +379,21 @@ QString misc::userFriendlyDuration(qlonglong seconds) {
return "0"; return "0";
} }
if (seconds < 60) { if (seconds < 60) {
return tr("< 1m", "< 1 minute"); return QCoreApplication::translate("misc", "< 1m", "< 1 minute");
} }
int minutes = seconds / 60; int minutes = seconds / 60;
if (minutes < 60) { if (minutes < 60) {
return tr("%1m","e.g: 10minutes").arg(QString::number(minutes)); return QCoreApplication::translate("misc", "%1m","e.g: 10minutes").arg(QString::number(minutes));
} }
int hours = minutes / 60; int hours = minutes / 60;
minutes = minutes - hours*60; minutes = minutes - hours*60;
if (hours < 24) { if (hours < 24) {
return tr("%1h %2m", "e.g: 3hours 5minutes").arg(QString::number(hours)).arg(QString::number(minutes)); return QCoreApplication::translate("misc", "%1h %2m", "e.g: 3hours 5minutes").arg(QString::number(hours)).arg(QString::number(minutes));
} }
int days = hours / 24; int days = hours / 24;
hours = hours - days * 24; hours = hours - days * 24;
if (days < 100) { if (days < 100) {
return tr("%1d %2h", "e.g: 2days 10hours").arg(QString::number(days)).arg(QString::number(hours)); return QCoreApplication::translate("misc", "%1d %2h", "e.g: 2days 10hours").arg(QString::number(days)).arg(QString::number(hours));
} }
return QString::fromUtf8(""); return QString::fromUtf8("");
} }

59
src/misc.h

@ -43,7 +43,6 @@
#include <QFile> #include <QFile>
#include <QDir> #include <QDir>
#include <QUrl> #include <QUrl>
#include <QCoreApplication>
#ifndef DISABLE_GUI #ifndef DISABLE_GUI
#include <QIcon> #include <QIcon>
#endif #endif
@ -51,79 +50,73 @@
const qlonglong MAX_ETA = 8640000; const qlonglong MAX_ETA = 8640000;
/* Miscellaneaous functions that can be useful */ /* Miscellaneaous functions that can be useful */
class misc namespace misc
{ {
Q_DECLARE_TR_FUNCTIONS(misc) inline QString toQString(const std::string &str) {
private:
misc(); // Forbidden
public:
static inline QString toQString(const std::string &str) {
return QString::fromLocal8Bit(str.c_str()); return QString::fromLocal8Bit(str.c_str());
} }
static inline QString toQString(const char* str) { inline QString toQString(const char* str) {
return QString::fromLocal8Bit(str); return QString::fromLocal8Bit(str);
} }
static inline QString toQStringU(const std::string &str) { inline QString toQStringU(const std::string &str) {
return QString::fromUtf8(str.c_str()); return QString::fromUtf8(str.c_str());
} }
static inline QString toQStringU(const char* str) { inline QString toQStringU(const char* str) {
return QString::fromUtf8(str); return QString::fromUtf8(str);
} }
static inline QString toQString(const libtorrent::sha1_hash &hash) { inline QString toQString(const libtorrent::sha1_hash &hash) {
char out[41]; char out[41];
to_hex((char const*)&hash[0], libtorrent::sha1_hash::size, out); to_hex((char const*)&hash[0], libtorrent::sha1_hash::size, out);
return QString(out); return QString(out);
} }
#ifndef DISABLE_GUI #ifndef DISABLE_GUI
static void shutdownComputer(bool sleep=false); void shutdownComputer(bool sleep=false);
#endif #endif
static QString parseHtmlLinks(const QString &raw_text); QString parseHtmlLinks(const QString &raw_text);
static bool isUrl(const QString &s); bool isUrl(const QString &s);
#ifndef DISABLE_GUI #ifndef DISABLE_GUI
// Get screen center // Get screen center
static QPoint screenCenter(QWidget *win); QPoint screenCenter(QWidget *win);
#endif #endif
static int pythonVersion(); int pythonVersion();
// return best userfriendly storage unit (B, KiB, MiB, GiB, TiB) // return best userfriendly storage unit (B, KiB, MiB, GiB, TiB)
// use Binary prefix standards from IEC 60027-2 // use Binary prefix standards from IEC 60027-2
// see http://en.wikipedia.org/wiki/Kilobyte // see http://en.wikipedia.org/wiki/Kilobyte
// value must be given in bytes // value must be given in bytes
static QString friendlyUnit(qreal val, bool is_speed = false); QString friendlyUnit(qreal val, bool is_speed = false);
static bool isPreviewable(const QString& extension); bool isPreviewable(const QString& extension);
static QString magnetUriToName(const QString& magnet_uri); QString magnetUriToName(const QString& magnet_uri);
static QString magnetUriToHash(const QString& magnet_uri); QString magnetUriToHash(const QString& magnet_uri);
static QList<QUrl> magnetUriToTrackers(const QString& magnet_uri); QList<QUrl> magnetUriToTrackers(const QString& magnet_uri);
static QString bcLinkToMagnet(QString bc_link); QString bcLinkToMagnet(QString bc_link);
// Take a number of seconds and return an user-friendly // Take a number of seconds and return an user-friendly
// time duration like "1d 2h 10m". // time duration like "1d 2h 10m".
static QString userFriendlyDuration(qlonglong seconds); QString userFriendlyDuration(qlonglong seconds);
static QString getUserIDString(); QString getUserIDString();
// Convert functions // Convert functions
static QStringList toStringList(const QList<bool> &l); QStringList toStringList(const QList<bool> &l);
static QList<int> intListfromStringList(const QStringList &l); QList<int> intListfromStringList(const QStringList &l);
static QList<bool> boolListfromStringList(const QStringList &l); QList<bool> boolListfromStringList(const QStringList &l);
#if LIBTORRENT_VERSION_NUM < 001600 #if LIBTORRENT_VERSION_NUM < 001600
static QString toQString(const boost::posix_time::ptime& boostDate); QString toQString(const boost::posix_time::ptime& boostDate);
#else #else
static QString toQString(time_t t); QString toQString(time_t t);
#endif #endif
#ifndef DISABLE_GUI #ifndef DISABLE_GUI
static bool naturalSort(QString left, QString right, bool& result); bool naturalSort(QString left, QString right, bool& result);
#endif #endif
}; }
// Trick to get a portable sleep() function // Trick to get a portable sleep() function
class SleeperThread : public QThread { class SleeperThread : public QThread {

Loading…
Cancel
Save