Browse Source

Follow project coding style. Issue #2192.

adaptive-webui-19844
sledgehammer999 10 years ago
parent
commit
cbbf14b0f8
  1. 100
      src/misc.cpp
  2. 17
      src/torrentimportdlg.cpp

100
src/misc.cpp

@ -89,30 +89,36 @@ static struct { const char *source; const char *comment; } units[] = {
QT_TRANSLATE_NOOP3("misc", "TiB", "tebibytes (1024 gibibytes)") QT_TRANSLATE_NOOP3("misc", "TiB", "tebibytes (1024 gibibytes)")
}; };
QString misc::toQString(const std::string &str) { QString misc::toQString(const std::string &str)
{
return QString::fromLocal8Bit(str.c_str()); return QString::fromLocal8Bit(str.c_str());
} }
QString misc::toQString(const char* str) { QString misc::toQString(const char* str)
{
return QString::fromLocal8Bit(str); return QString::fromLocal8Bit(str);
} }
QString misc::toQStringU(const std::string &str) { QString misc::toQStringU(const std::string &str)
{
return QString::fromUtf8(str.c_str()); return QString::fromUtf8(str.c_str());
} }
QString misc::toQStringU(const char* str) { QString misc::toQStringU(const char* str)
{
return QString::fromUtf8(str); return QString::fromUtf8(str);
} }
QString misc::toQString(const libtorrent::sha1_hash &hash) { QString misc::toQString(const libtorrent::sha1_hash &hash)
{
char out[41]; char out[41];
libtorrent::to_hex((char const*)&hash[0], libtorrent::sha1_hash::size, out); libtorrent::to_hex((char const*)&hash[0], libtorrent::sha1_hash::size, out);
return QString(out); return QString(out);
} }
#ifndef DISABLE_GUI #ifndef DISABLE_GUI
void misc::shutdownComputer(shutDownAction action) { void misc::shutdownComputer(shutDownAction action)
{
#if (defined(Q_OS_UNIX) && !defined(Q_OS_MAC)) && defined(QT_DBUS_LIB) #if (defined(Q_OS_UNIX) && !defined(Q_OS_MAC)) && defined(QT_DBUS_LIB)
// Use dbus to power off / suspend the system // Use dbus to power off / suspend the system
if (action != SHUTDOWN_COMPUTER) { if (action != SHUTDOWN_COMPUTER) {
@ -144,7 +150,8 @@ void misc::shutdownComputer(shutDownAction action) {
halIface.call("Suspend", 5); halIface.call("Suspend", 5);
else else
halIface.call("Hibernate"); halIface.call("Hibernate");
} else { }
else {
// Some recent systems use systemd's logind // Some recent systems use systemd's logind
QDBusInterface login1Iface("org.freedesktop.login1", "/org/freedesktop/login1", QDBusInterface login1Iface("org.freedesktop.login1", "/org/freedesktop/login1",
"org.freedesktop.login1.Manager", QDBusConnection::systemBus()); "org.freedesktop.login1.Manager", QDBusConnection::systemBus());
@ -183,27 +190,21 @@ void misc::shutdownComputer(shutDownAction action) {
sizeof(kPSNOfSystemProcess), &targetDesc); sizeof(kPSNOfSystemProcess), &targetDesc);
if (error != noErr) if (error != noErr)
{
return; return;
}
error = AECreateAppleEvent(kCoreEventClass, EventToSend, &targetDesc, error = AECreateAppleEvent(kCoreEventClass, EventToSend, &targetDesc,
kAutoGenerateReturnID, kAnyTransactionID, &appleEventToSend); kAutoGenerateReturnID, kAnyTransactionID, &appleEventToSend);
AEDisposeDesc(&targetDesc); AEDisposeDesc(&targetDesc);
if (error != noErr) if (error != noErr)
{
return; return;
}
error = AESend(&appleEventToSend, &eventReply, kAENoReply, error = AESend(&appleEventToSend, &eventReply, kAENoReply,
kAENormalPriority, kAEDefaultTimeout, NULL, NULL); kAENormalPriority, kAEDefaultTimeout, NULL, NULL);
AEDisposeDesc(&appleEventToSend); AEDisposeDesc(&appleEventToSend);
if (error != noErr) if (error != noErr)
{
return; return;
}
AEDisposeDesc(&eventReply); AEDisposeDesc(&eventReply);
#endif #endif
@ -246,7 +247,8 @@ void misc::shutdownComputer(shutDownAction action) {
#ifndef DISABLE_GUI #ifndef DISABLE_GUI
// Get screen center // Get screen center
QPoint misc::screenCenter(QWidget *win) { QPoint misc::screenCenter(QWidget *win)
{
int scrn = 0; int scrn = 0;
const QWidget *w = win->window(); const QWidget *w = win->window();
@ -266,7 +268,8 @@ QPoint misc::screenCenter(QWidget *win) {
* Detects the version of python by calling * Detects the version of python by calling
* "python --version" and parsing the output. * "python --version" and parsing the output.
*/ */
int misc::pythonVersion() { int misc::pythonVersion()
{
static int version = -1; static int version = -1;
if (version < 0) { if (version < 0) {
QProcess python_proc; QProcess python_proc;
@ -291,7 +294,8 @@ int misc::pythonVersion() {
// 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
// to send numbers instead of strings with suffixes // to send numbers instead of strings with suffixes
QString misc::friendlyUnit(qreal val, bool is_speed) { QString misc::friendlyUnit(qreal val, bool is_speed)
{
if (val < 0) if (val < 0)
return QCoreApplication::translate("misc", "Unknown", "Unknown (size)"); return QCoreApplication::translate("misc", "Unknown", "Unknown (size)");
int i = 0; int i = 0;
@ -307,7 +311,8 @@ QString misc::friendlyUnit(qreal val, bool is_speed) {
return ret; return ret;
} }
bool misc::isPreviewable(const QString& extension) { bool misc::isPreviewable(const QString& extension)
{
static QSet<QString> multimedia_extensions; static QSet<QString> multimedia_extensions;
if (multimedia_extensions.empty()) { if (multimedia_extensions.empty()) {
multimedia_extensions.insert("3GP"); multimedia_extensions.insert("3GP");
@ -359,7 +364,8 @@ bool misc::isPreviewable(const QString& extension) {
return multimedia_extensions.contains(extension.toUpper()); return multimedia_extensions.contains(extension.toUpper());
} }
QString misc::bcLinkToMagnet(QString bc_link) { QString misc::bcLinkToMagnet(QString bc_link)
{
QByteArray raw_bc = bc_link.toUtf8(); QByteArray raw_bc = bc_link.toUtf8();
raw_bc = raw_bc.mid(8); // skip bc://bt/ raw_bc = raw_bc.mid(8); // skip bc://bt/
raw_bc = QByteArray::fromBase64(raw_bc); // Decode base64 raw_bc = QByteArray::fromBase64(raw_bc); // Decode base64
@ -373,7 +379,8 @@ QString misc::bcLinkToMagnet(QString bc_link) {
return magnet; return magnet;
} }
QString misc::magnetUriToName(const QString& magnet_uri) { QString misc::magnetUriToName(const QString& magnet_uri)
{
QString name = ""; QString name = "";
QRegExp regHex("dn=([^&]+)"); QRegExp regHex("dn=([^&]+)");
const int pos = regHex.indexIn(magnet_uri); const int pos = regHex.indexIn(magnet_uri);
@ -401,7 +408,8 @@ QList<QUrl> misc::magnetUriToTrackers(const QString& magnet_uri)
return trackers; return trackers;
} }
QString misc::magnetUriToHash(const QString& magnet_uri) { QString misc::magnetUriToHash(const QString& magnet_uri)
{
QString hash = ""; QString hash = "";
QRegExp regHex("urn:btih:([0-9A-Za-z]+)"); QRegExp regHex("urn:btih:([0-9A-Za-z]+)");
// Hex // Hex
@ -431,34 +439,30 @@ QString misc::magnetUriToHash(const QString& magnet_uri) {
// 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".
QString misc::userFriendlyDuration(qlonglong seconds) { QString misc::userFriendlyDuration(qlonglong seconds)
if (seconds < 0 || seconds >= MAX_ETA) { {
if (seconds < 0 || seconds >= MAX_ETA)
return QString::fromUtf8(""); return QString::fromUtf8("");
} if (seconds == 0)
if (seconds == 0) {
return "0"; return "0";
} if (seconds < 60)
if (seconds < 60) {
return QCoreApplication::translate("misc", "< 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 QCoreApplication::translate("misc", "%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 QCoreApplication::translate("misc", "%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 QCoreApplication::translate("misc", "%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("");
} }
QString misc::getUserIDString() { QString misc::getUserIDString()
{
QString uid = "0"; QString uid = "0";
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
char buffer[UNLEN + 1] = {0}; char buffer[UNLEN + 1] = {0};
@ -471,27 +475,27 @@ QString misc::getUserIDString() {
return uid; return uid;
} }
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)
ret << (b ? "1" : "0"); ret << (b ? "1" : "0");
}
return ret; return ret;
} }
QList<int> misc::intListfromStringList(const QStringList &l) { QList<int> misc::intListfromStringList(const QStringList &l)
{
QList<int> ret; QList<int> ret;
foreach (const QString &s, l) { foreach (const QString &s, l)
ret << s.toInt(); ret << s.toInt();
}
return ret; return ret;
} }
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)
ret << (s=="1"); ret << (s=="1");
}
return ret; return ret;
} }
@ -569,8 +573,8 @@ QString misc::toQString(time_t t)
} }
#ifndef DISABLE_GUI #ifndef DISABLE_GUI
bool misc::naturalSort(QString left, QString right, bool &result) { // uses lessThan comparison bool misc::naturalSort(QString left, QString right, bool &result) // uses lessThan comparison
// Return value indicates if functions was successful { // Return value indicates if functions was successful
// result argument will contain actual comparison result if function was successful // result argument will contain actual comparison result if function was successful
int posL = 0; int posL = 0;
int posR = 0; int posR = 0;
@ -631,7 +635,8 @@ bool misc::naturalSort(QString left, QString right, bool &result) { // uses less
#endif #endif
// to send numbers instead of strings with suffixes // to send numbers instead of strings with suffixes
QString misc::accurateDoubleToString(const double &n, const int &precision) { QString misc::accurateDoubleToString(const double &n, const int &precision)
{
/* HACK because QString rounds up. Eg QString::number(0.999*100.0, 'f' ,1) == 99.9 /* HACK because QString rounds up. Eg QString::number(0.999*100.0, 'f' ,1) == 99.9
** but QString::number(0.9999*100.0, 'f' ,1) == 100.0 The problem manifests when ** but QString::number(0.9999*100.0, 'f' ,1) == 100.0 The problem manifests when
** the number has more digits after the decimal than we want AND the digit after ** the number has more digits after the decimal than we want AND the digit after
@ -679,6 +684,7 @@ namespace {
}; };
} }
void misc::msleep(unsigned long msecs) { void misc::msleep(unsigned long msecs)
{
SleeperThread::msleep(msecs); SleeperThread::msleep(msecs);
} }

17
src/torrentimportdlg.cpp

@ -65,12 +65,11 @@ void TorrentImportDlg::on_browseTorrentBtn_clicked()
const QString default_dir = Preferences::instance()->getMainLastDir(); const QString default_dir = Preferences::instance()->getMainLastDir();
// Ask for a torrent file // Ask for a torrent file
m_torrentPath = QFileDialog::getOpenFileName(this, tr("Torrent file to import"), default_dir, tr("Torrent files") + QString(" (*.torrent)")); m_torrentPath = QFileDialog::getOpenFileName(this, tr("Torrent file to import"), default_dir, tr("Torrent files") + QString(" (*.torrent)"));
if (!m_torrentPath.isEmpty()) { if (!m_torrentPath.isEmpty())
loadTorrent(m_torrentPath); loadTorrent(m_torrentPath);
} else { else
ui->lineTorrent->clear(); ui->lineTorrent->clear();
} }
}
void TorrentImportDlg::on_browseContentBtn_clicked() void TorrentImportDlg::on_browseContentBtn_clicked()
{ {
@ -109,7 +108,8 @@ void TorrentImportDlg::on_browseContentBtn_clicked()
if (t->file_at(0).size == file_size) { if (t->file_at(0).size == file_size) {
qDebug("The file size matches, allowing fast seeding..."); qDebug("The file size matches, allowing fast seeding...");
ui->checkSkipCheck->setEnabled(true); ui->checkSkipCheck->setEnabled(true);
} else { }
else {
qDebug("The file size does not match, forbidding fast seeding..."); qDebug("The file size does not match, forbidding fast seeding...");
ui->checkSkipCheck->setChecked(false); ui->checkSkipCheck->setChecked(false);
ui->checkSkipCheck->setEnabled(false); ui->checkSkipCheck->setEnabled(false);
@ -124,7 +124,8 @@ void TorrentImportDlg::on_browseContentBtn_clicked()
m_filesPath.replace(0, new_parts.join("/")); m_filesPath.replace(0, new_parts.join("/"));
} }
m_contentPath = parts.join("/"); m_contentPath = parts.join("/");
} else { }
else {
// multiple files torrent // multiple files torrent
m_contentPath = QFileDialog::getExistingDirectory(this, tr("Please point to the location of the torrent: %1").arg(misc::toQStringU(t->name())), m_contentPath = QFileDialog::getExistingDirectory(this, tr("Please point to the location of the torrent: %1").arg(misc::toQStringU(t->name())),
default_dir); default_dir);
@ -155,7 +156,8 @@ void TorrentImportDlg::on_browseContentBtn_clicked()
qDebug("The file size does not match, forbidding fast seeding..."); qDebug("The file size does not match, forbidding fast seeding...");
ui->checkSkipCheck->setChecked(false); ui->checkSkipCheck->setChecked(false);
ui->checkSkipCheck->setEnabled(false); ui->checkSkipCheck->setEnabled(false);
} else { }
else {
qDebug("The file size matches, allowing fast seeding..."); qDebug("The file size matches, allowing fast seeding...");
ui->checkSkipCheck->setEnabled(true); ui->checkSkipCheck->setEnabled(true);
} }
@ -242,10 +244,9 @@ void TorrentImportDlg::initializeFilesPath()
{ {
m_filesPath.clear(); m_filesPath.clear();
// Loads files path in the torrent // Loads files path in the torrent
for (int i=0; i<t->num_files(); ++i) { for (int i = 0; i<t->num_files(); ++i)
m_filesPath << fsutils::fromNativePath(misc::toQStringU(t->file_at(i).path)); m_filesPath << fsutils::fromNativePath(misc::toQStringU(t->file_at(i).path));
} }
}
bool TorrentImportDlg::fileRenamed() const bool TorrentImportDlg::fileRenamed() const
{ {

Loading…
Cancel
Save