Browse Source

Code optimization

adaptive-webui-19844
Christophe Dumez 14 years ago
parent
commit
287cdc43c1
  1. 55
      src/misc.cpp
  2. 7
      src/misc.h

55
src/misc.cpp

@ -75,6 +75,8 @@ const int UNLEN = 256;
using namespace libtorrent; using namespace libtorrent;
const QString misc::units[5] = {tr("B", "bytes"), tr("KiB", "kibibytes (1024 bytes)"), tr("MiB", "mebibytes (1024 kibibytes)"), tr("GiB", "gibibytes (1024 mibibytes)"), tr("TiB", "tebibytes (1024 gibibytes)")};
QString misc::QDesktopServicesDataLocation() { QString misc::QDesktopServicesDataLocation() {
#ifdef Q_WS_WIN #ifdef Q_WS_WIN
LPWSTR path=new WCHAR[256]; LPWSTR path=new WCHAR[256];
@ -336,25 +338,24 @@ QString misc::truncateRootFolder(libtorrent::torrent_handle h) {
return root_folder; return root_folder;
} }
bool misc::sameFiles(QString path1, QString path2) { bool misc::sameFiles(const QString &path1, const QString &path2) {
QFile f1(path1); QFile f1(path1), f2(path2);
if(!f1.exists()) return false; if(!f1.exists() || !f2.exists()) return false;
QFile f2(path2); if(f1.size() != f2.size()) return false;
if(!f2.exists()) return false; if(!f1.open(QIODevice::ReadOnly)) return false;
QByteArray content1, content2; if(!f2.open(QIODevice::ReadOnly)) {
if(f1.open(QIODevice::ReadOnly)) {
content1 = f1.readAll();
f1.close(); f1.close();
} else {
return false; return false;
} }
if(f2.open(QIODevice::ReadOnly)) { bool same = true;
content1 = f2.readAll(); while(!f1.atEnd() && !f2.atEnd()) {
f2.close(); if(f1.read(5) != f2.read(5)) {
} else { same = false;
return false; break;
}
} }
return content1 == content2; f1.close(); f2.close();
return same;
} }
void misc::copyDir(QString src_path, QString dst_path) { void misc::copyDir(QString src_path, QString dst_path) {
@ -401,11 +402,12 @@ void misc::chmod644(const QDir& folder) {
} }
} }
QString misc::updateLabelInSavePath(const QString& defaultSavePath, QString save_path, const QString old_label, const QString new_label) { QString misc::updateLabelInSavePath(const QString& defaultSavePath, const QString &save_path, const QString &old_label, const QString &new_label) {
if(old_label == new_label) return save_path; if(old_label == new_label) return save_path;
qDebug("UpdateLabelInSavePath(%s, %s, %s)", qPrintable(save_path), qPrintable(old_label), qPrintable(new_label)); qDebug("UpdateLabelInSavePath(%s, %s, %s)", qPrintable(save_path), qPrintable(old_label), qPrintable(new_label));
if(!save_path.startsWith(defaultSavePath)) return save_path; if(!save_path.startsWith(defaultSavePath)) return save_path;
QString new_save_path = save_path.replace(defaultSavePath, ""); QString new_save_path = save_path;
new_save_path.replace(defaultSavePath, "");
QStringList path_parts = new_save_path.split(QDir::separator(), QString::SkipEmptyParts); QStringList path_parts = new_save_path.split(QDir::separator(), QString::SkipEmptyParts);
if(path_parts.empty()) { if(path_parts.empty()) {
if(!new_label.isEmpty()) if(!new_label.isEmpty())
@ -526,13 +528,12 @@ QString misc::cacheLocation() {
QString misc::friendlyUnit(double val) { QString misc::friendlyUnit(double val) {
if(val < 0) if(val < 0)
return tr("Unknown", "Unknown (size)"); return tr("Unknown", "Unknown (size)");
const QString units[5] = {tr("B", "bytes"), tr("KiB", "kibibytes (1024 bytes)"), tr("MiB", "mebibytes (1024 kibibytes)"), tr("GiB", "gibibytes (1024 mibibytes)"), tr("TiB", "tebibytes (1024 gibibytes)")}; int i = 0;
char i = 0;
while(val >= 1024. && i++<6) while(val >= 1024. && i++<6)
val /= 1024.; val /= 1024.;
if(i == 0) if(i == 0)
return QString::number((long)val) + " " + units[0]; return QString::number((long)val) + " " + units[0];
return QString::number(val, 'f', 1) + " " + units[(int)i]; return QString::number(val, 'f', 1) + " " + units[i];
} }
bool misc::isPreviewable(QString extension){ bool misc::isPreviewable(QString extension){
@ -657,10 +658,10 @@ QString misc::boostTimeToQString(const boost::optional<boost::posix_time::ptime>
} catch(std::exception e) { } catch(std::exception e) {
return tr("Unknown"); return tr("Unknown");
} }
time_t t = mktime(&tm); const time_t t = mktime(&tm);
if(t < 0) if(t < 0)
return tr("Unknown"); return tr("Unknown");
QDateTime dt = QDateTime::fromTime_t(t); const QDateTime dt = QDateTime::fromTime_t(t);
if(dt.isNull() || !dt.isValid()) if(dt.isNull() || !dt.isValid())
return tr("Unknown"); return tr("Unknown");
return dt.toString(Qt::DefaultLocaleLongDate); return dt.toString(Qt::DefaultLocaleLongDate);
@ -736,10 +737,7 @@ QString misc::getUserIDString() {
QStringList misc::toStringList(const QList<bool> &l) { QStringList misc::toStringList(const QList<bool> &l) {
QStringList ret; QStringList ret;
foreach(const bool &b, l) { foreach(const bool &b, l) {
if(b) ret << (b ? "1" : "0");
ret << "1";
else
ret << "0";
} }
return ret; return ret;
} }
@ -755,10 +753,7 @@ QList<int> misc::intListfromStringList(const QStringList &l) {
QList<bool> misc::boolListfromStringList(const QStringList &l) { QList<bool> misc::boolListfromStringList(const QStringList &l) {
QList<bool> ret; QList<bool> ret;
foreach(const QString &s, l) { foreach(const QString &s, l) {
if(s == "1") ret << (s=="1");
ret << true;
else
ret << false;
} }
return ret; return ret;
} }

7
src/misc.h

@ -123,9 +123,9 @@ public:
static QString truncateRootFolder(boost::intrusive_ptr<libtorrent::torrent_info> t); static QString truncateRootFolder(boost::intrusive_ptr<libtorrent::torrent_info> t);
static QString truncateRootFolder(libtorrent::torrent_handle h); static QString truncateRootFolder(libtorrent::torrent_handle h);
static QString updateLabelInSavePath(const QString& defaultSavePath, QString save_path, const QString old_label, const QString new_label); static QString updateLabelInSavePath(const QString& defaultSavePath, const QString &save_path, const QString &old_label, const QString &new_label);
static bool sameFiles(QString path1, QString path2); static bool sameFiles(const QString &path1, const QString &path2);
static void copyDir(QString src_path, QString dst_path); static void copyDir(QString src_path, QString dst_path);
// Introduced in v2.1.0 for backward compatibility // Introduced in v2.1.0 for backward compatibility
// Remove after some releases // Remove after some releases
@ -171,6 +171,9 @@ public:
static QList<bool> boolListfromStringList(const QStringList &l); static QList<bool> boolListfromStringList(const QStringList &l);
static bool isValidTorrentFile(const QString &path); static bool isValidTorrentFile(const QString &path);
private:
static const QString units[5];
}; };
// Trick to get a portable sleep() function // Trick to get a portable sleep() function

Loading…
Cancel
Save