mirror of
https://github.com/d47081/qBittorrent.git
synced 2025-01-11 15:27:54 +00:00
Follow project coding style. Issue #2192.
This commit is contained in:
parent
060d3fc5b9
commit
cbbf14b0f8
100
src/misc.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)")
|
||||
};
|
||||
|
||||
QString misc::toQString(const std::string &str) {
|
||||
QString misc::toQString(const std::string &str)
|
||||
{
|
||||
return QString::fromLocal8Bit(str.c_str());
|
||||
}
|
||||
|
||||
QString misc::toQString(const char* str) {
|
||||
QString misc::toQString(const char* 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());
|
||||
}
|
||||
|
||||
QString misc::toQStringU(const char* str) {
|
||||
QString misc::toQStringU(const char* str)
|
||||
{
|
||||
return QString::fromUtf8(str);
|
||||
}
|
||||
|
||||
QString misc::toQString(const libtorrent::sha1_hash &hash) {
|
||||
QString misc::toQString(const libtorrent::sha1_hash &hash)
|
||||
{
|
||||
char out[41];
|
||||
libtorrent::to_hex((char const*)&hash[0], libtorrent::sha1_hash::size, out);
|
||||
return QString(out);
|
||||
}
|
||||
|
||||
#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)
|
||||
// Use dbus to power off / suspend the system
|
||||
if (action != SHUTDOWN_COMPUTER) {
|
||||
@ -144,7 +150,8 @@ void misc::shutdownComputer(shutDownAction action) {
|
||||
halIface.call("Suspend", 5);
|
||||
else
|
||||
halIface.call("Hibernate");
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
// Some recent systems use systemd's logind
|
||||
QDBusInterface login1Iface("org.freedesktop.login1", "/org/freedesktop/login1",
|
||||
"org.freedesktop.login1.Manager", QDBusConnection::systemBus());
|
||||
@ -183,27 +190,21 @@ void misc::shutdownComputer(shutDownAction action) {
|
||||
sizeof(kPSNOfSystemProcess), &targetDesc);
|
||||
|
||||
if (error != noErr)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
error = AECreateAppleEvent(kCoreEventClass, EventToSend, &targetDesc,
|
||||
kAutoGenerateReturnID, kAnyTransactionID, &appleEventToSend);
|
||||
|
||||
AEDisposeDesc(&targetDesc);
|
||||
if (error != noErr)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
error = AESend(&appleEventToSend, &eventReply, kAENoReply,
|
||||
kAENormalPriority, kAEDefaultTimeout, NULL, NULL);
|
||||
|
||||
AEDisposeDesc(&appleEventToSend);
|
||||
if (error != noErr)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
AEDisposeDesc(&eventReply);
|
||||
#endif
|
||||
@ -246,7 +247,8 @@ void misc::shutdownComputer(shutDownAction action) {
|
||||
|
||||
#ifndef DISABLE_GUI
|
||||
// Get screen center
|
||||
QPoint misc::screenCenter(QWidget *win) {
|
||||
QPoint misc::screenCenter(QWidget *win)
|
||||
{
|
||||
int scrn = 0;
|
||||
const QWidget *w = win->window();
|
||||
|
||||
@ -266,7 +268,8 @@ QPoint misc::screenCenter(QWidget *win) {
|
||||
* Detects the version of python by calling
|
||||
* "python --version" and parsing the output.
|
||||
*/
|
||||
int misc::pythonVersion() {
|
||||
int misc::pythonVersion()
|
||||
{
|
||||
static int version = -1;
|
||||
if (version < 0) {
|
||||
QProcess python_proc;
|
||||
@ -291,7 +294,8 @@ int misc::pythonVersion() {
|
||||
// see http://en.wikipedia.org/wiki/Kilobyte
|
||||
// value must be given in bytes
|
||||
// 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)
|
||||
return QCoreApplication::translate("misc", "Unknown", "Unknown (size)");
|
||||
int i = 0;
|
||||
@ -307,7 +311,8 @@ QString misc::friendlyUnit(qreal val, bool is_speed) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool misc::isPreviewable(const QString& extension) {
|
||||
bool misc::isPreviewable(const QString& extension)
|
||||
{
|
||||
static QSet<QString> multimedia_extensions;
|
||||
if (multimedia_extensions.empty()) {
|
||||
multimedia_extensions.insert("3GP");
|
||||
@ -359,7 +364,8 @@ bool misc::isPreviewable(const QString& extension) {
|
||||
return multimedia_extensions.contains(extension.toUpper());
|
||||
}
|
||||
|
||||
QString misc::bcLinkToMagnet(QString bc_link) {
|
||||
QString misc::bcLinkToMagnet(QString bc_link)
|
||||
{
|
||||
QByteArray raw_bc = bc_link.toUtf8();
|
||||
raw_bc = raw_bc.mid(8); // skip bc://bt/
|
||||
raw_bc = QByteArray::fromBase64(raw_bc); // Decode base64
|
||||
@ -373,7 +379,8 @@ QString misc::bcLinkToMagnet(QString bc_link) {
|
||||
return magnet;
|
||||
}
|
||||
|
||||
QString misc::magnetUriToName(const QString& magnet_uri) {
|
||||
QString misc::magnetUriToName(const QString& magnet_uri)
|
||||
{
|
||||
QString name = "";
|
||||
QRegExp regHex("dn=([^&]+)");
|
||||
const int pos = regHex.indexIn(magnet_uri);
|
||||
@ -401,7 +408,8 @@ QList<QUrl> misc::magnetUriToTrackers(const QString& magnet_uri)
|
||||
return trackers;
|
||||
}
|
||||
|
||||
QString misc::magnetUriToHash(const QString& magnet_uri) {
|
||||
QString misc::magnetUriToHash(const QString& magnet_uri)
|
||||
{
|
||||
QString hash = "";
|
||||
QRegExp regHex("urn:btih:([0-9A-Za-z]+)");
|
||||
// Hex
|
||||
@ -431,34 +439,30 @@ QString misc::magnetUriToHash(const QString& magnet_uri) {
|
||||
|
||||
// Take a number of seconds and return an user-friendly
|
||||
// time duration like "1d 2h 10m".
|
||||
QString misc::userFriendlyDuration(qlonglong seconds) {
|
||||
if (seconds < 0 || seconds >= MAX_ETA) {
|
||||
QString misc::userFriendlyDuration(qlonglong seconds)
|
||||
{
|
||||
if (seconds < 0 || seconds >= MAX_ETA)
|
||||
return QString::fromUtf8("∞");
|
||||
}
|
||||
if (seconds == 0) {
|
||||
if (seconds == 0)
|
||||
return "0";
|
||||
}
|
||||
if (seconds < 60) {
|
||||
if (seconds < 60)
|
||||
return QCoreApplication::translate("misc", "< 1m", "< 1 minute");
|
||||
}
|
||||
int minutes = seconds / 60;
|
||||
if (minutes < 60) {
|
||||
if (minutes < 60)
|
||||
return QCoreApplication::translate("misc", "%1m","e.g: 10minutes").arg(QString::number(minutes));
|
||||
}
|
||||
int hours = minutes / 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));
|
||||
}
|
||||
int days = hours / 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 QString::fromUtf8("∞");
|
||||
}
|
||||
|
||||
QString misc::getUserIDString() {
|
||||
QString misc::getUserIDString()
|
||||
{
|
||||
QString uid = "0";
|
||||
#ifdef Q_OS_WIN
|
||||
char buffer[UNLEN + 1] = {0};
|
||||
@ -471,27 +475,27 @@ QString misc::getUserIDString() {
|
||||
return uid;
|
||||
}
|
||||
|
||||
QStringList misc::toStringList(const QList<bool> &l) {
|
||||
QStringList misc::toStringList(const QList<bool> &l)
|
||||
{
|
||||
QStringList ret;
|
||||
foreach (const bool &b, l) {
|
||||
foreach (const bool &b, l)
|
||||
ret << (b ? "1" : "0");
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
QList<int> misc::intListfromStringList(const QStringList &l) {
|
||||
QList<int> misc::intListfromStringList(const QStringList &l)
|
||||
{
|
||||
QList<int> ret;
|
||||
foreach (const QString &s, l) {
|
||||
foreach (const QString &s, l)
|
||||
ret << s.toInt();
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
QList<bool> misc::boolListfromStringList(const QStringList &l) {
|
||||
QList<bool> misc::boolListfromStringList(const QStringList &l)
|
||||
{
|
||||
QList<bool> ret;
|
||||
foreach (const QString &s, l) {
|
||||
foreach (const QString &s, l)
|
||||
ret << (s=="1");
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
@ -569,8 +573,8 @@ QString misc::toQString(time_t t)
|
||||
}
|
||||
|
||||
#ifndef DISABLE_GUI
|
||||
bool misc::naturalSort(QString left, QString right, bool &result) { // uses lessThan comparison
|
||||
// Return value indicates if functions was successful
|
||||
bool misc::naturalSort(QString left, QString right, bool &result) // uses lessThan comparison
|
||||
{ // Return value indicates if functions was successful
|
||||
// result argument will contain actual comparison result if function was successful
|
||||
int posL = 0;
|
||||
int posR = 0;
|
||||
@ -631,7 +635,8 @@ bool misc::naturalSort(QString left, QString right, bool &result) { // uses less
|
||||
#endif
|
||||
|
||||
// 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
|
||||
** 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
|
||||
@ -679,6 +684,7 @@ namespace {
|
||||
};
|
||||
}
|
||||
|
||||
void misc::msleep(unsigned long msecs) {
|
||||
void misc::msleep(unsigned long msecs)
|
||||
{
|
||||
SleeperThread::msleep(msecs);
|
||||
}
|
||||
|
@ -65,12 +65,11 @@ void TorrentImportDlg::on_browseTorrentBtn_clicked()
|
||||
const QString default_dir = Preferences::instance()->getMainLastDir();
|
||||
// Ask for a torrent file
|
||||
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);
|
||||
} else {
|
||||
else
|
||||
ui->lineTorrent->clear();
|
||||
}
|
||||
}
|
||||
|
||||
void TorrentImportDlg::on_browseContentBtn_clicked()
|
||||
{
|
||||
@ -109,7 +108,8 @@ void TorrentImportDlg::on_browseContentBtn_clicked()
|
||||
if (t->file_at(0).size == file_size) {
|
||||
qDebug("The file size matches, allowing fast seeding...");
|
||||
ui->checkSkipCheck->setEnabled(true);
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
qDebug("The file size does not match, forbidding fast seeding...");
|
||||
ui->checkSkipCheck->setChecked(false);
|
||||
ui->checkSkipCheck->setEnabled(false);
|
||||
@ -124,7 +124,8 @@ void TorrentImportDlg::on_browseContentBtn_clicked()
|
||||
m_filesPath.replace(0, new_parts.join("/"));
|
||||
}
|
||||
m_contentPath = parts.join("/");
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
// multiple files torrent
|
||||
m_contentPath = QFileDialog::getExistingDirectory(this, tr("Please point to the location of the torrent: %1").arg(misc::toQStringU(t->name())),
|
||||
default_dir);
|
||||
@ -155,7 +156,8 @@ void TorrentImportDlg::on_browseContentBtn_clicked()
|
||||
qDebug("The file size does not match, forbidding fast seeding...");
|
||||
ui->checkSkipCheck->setChecked(false);
|
||||
ui->checkSkipCheck->setEnabled(false);
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
qDebug("The file size matches, allowing fast seeding...");
|
||||
ui->checkSkipCheck->setEnabled(true);
|
||||
}
|
||||
@ -242,10 +244,9 @@ void TorrentImportDlg::initializeFilesPath()
|
||||
{
|
||||
m_filesPath.clear();
|
||||
// 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));
|
||||
}
|
||||
}
|
||||
|
||||
bool TorrentImportDlg::fileRenamed() const
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user