2006-09-30 20:02:39 +04:00
|
|
|
/*
|
|
|
|
* Bittorrent Client using Qt4 and libtorrent.
|
2007-07-14 18:31:59 +04:00
|
|
|
* Copyright (C) 2006 Christophe Dumez
|
2006-09-30 20:02:39 +04:00
|
|
|
*
|
2007-07-14 18:31:59 +04:00
|
|
|
* This program 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 2
|
|
|
|
* of the License, or (at your option) any later version.
|
2006-09-30 20:02:39 +04:00
|
|
|
*
|
|
|
|
* This program 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
|
2007-07-14 18:31:59 +04:00
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
2009-04-05 21:00:55 +04:00
|
|
|
* In addition, as a special exception, the copyright holders give permission to
|
|
|
|
* link this program with the OpenSSL project's "OpenSSL" library (or with
|
|
|
|
* modified versions of it that use the same license as the "OpenSSL" library),
|
|
|
|
* and distribute the linked executables. You must obey the GNU General Public
|
|
|
|
* License in all respects for all of the code used other than "OpenSSL". If you
|
|
|
|
* modify file(s), you may extend this exception to your version of the file(s),
|
|
|
|
* but you are not obligated to do so. If you do not wish to do so, delete this
|
|
|
|
* exception statement from your version.
|
|
|
|
*
|
2007-07-14 18:31:59 +04:00
|
|
|
* Contact : chris@qbittorrent.org
|
2006-09-30 20:02:39 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef MISC_H
|
|
|
|
#define MISC_H
|
|
|
|
|
|
|
|
#include <sstream>
|
|
|
|
#include <QString>
|
2010-08-13 18:02:19 +04:00
|
|
|
#include <QStringList>
|
2006-09-30 20:02:39 +04:00
|
|
|
#include <QThread>
|
2009-11-12 10:42:21 +03:00
|
|
|
#include <ctime>
|
|
|
|
#include <boost/date_time/posix_time/posix_time_types.hpp>
|
|
|
|
#include <boost/date_time/posix_time/conversion.hpp>
|
2010-03-20 22:09:27 +03:00
|
|
|
#include <QPoint>
|
2010-07-24 23:22:55 +04:00
|
|
|
#include <QFile>
|
2010-10-02 00:51:08 +04:00
|
|
|
#include <QDir>
|
2009-08-17 17:17:52 +04:00
|
|
|
|
2007-07-27 18:43:42 +04:00
|
|
|
#include <libtorrent/torrent_info.hpp>
|
2010-04-06 13:17:33 +04:00
|
|
|
#include <libtorrent/torrent_handle.hpp>
|
|
|
|
|
2010-05-21 15:46:12 +04:00
|
|
|
const qlonglong MAX_ETA = 8640000;
|
|
|
|
|
2007-07-27 18:43:42 +04:00
|
|
|
using namespace libtorrent;
|
|
|
|
|
2006-09-30 20:02:39 +04:00
|
|
|
/* Miscellaneaous functions that can be useful */
|
|
|
|
class misc : public QObject{
|
2009-08-17 09:14:03 +04:00
|
|
|
Q_OBJECT
|
2006-09-30 20:02:39 +04:00
|
|
|
|
2009-08-17 09:14:03 +04:00
|
|
|
public:
|
2010-03-03 20:27:25 +03:00
|
|
|
static inline QString toQString(std::string str) {
|
|
|
|
return QString::fromLocal8Bit(str.c_str());
|
2009-08-17 09:14:03 +04:00
|
|
|
}
|
2006-09-30 20:02:39 +04:00
|
|
|
|
2010-03-03 20:27:25 +03:00
|
|
|
static inline QString toQString(char* str) {
|
|
|
|
return QString::fromLocal8Bit(str);
|
2009-08-17 09:14:03 +04:00
|
|
|
}
|
2007-08-20 10:29:18 +04:00
|
|
|
|
2010-06-04 00:08:19 +04:00
|
|
|
static inline QString toQStringU(std::string str) {
|
|
|
|
return QString::fromUtf8(str.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline QString toQStringU(char* str) {
|
|
|
|
return QString::fromUtf8(str);
|
|
|
|
}
|
|
|
|
|
2010-03-03 20:27:25 +03:00
|
|
|
static inline QString toQString(sha1_hash hash) {
|
2009-08-17 09:14:03 +04:00
|
|
|
std::ostringstream o;
|
2010-03-12 00:59:25 +03:00
|
|
|
o << hash;
|
|
|
|
return QString(o.str().c_str());
|
2009-08-17 09:14:03 +04:00
|
|
|
}
|
2007-08-20 10:29:18 +04:00
|
|
|
|
2010-10-17 18:46:01 +04:00
|
|
|
static inline sha1_hash toSha1Hash(QString hash) {
|
|
|
|
return sha1_hash(qPrintable(hash));
|
|
|
|
}
|
|
|
|
|
2010-10-02 00:51:08 +04:00
|
|
|
static void chmod644(const QDir& folder);
|
|
|
|
|
2010-08-13 18:02:19 +04:00
|
|
|
static inline QString removeLastPathPart(QString path) {
|
|
|
|
if(path.isEmpty()) return path;
|
|
|
|
path = path.replace("\\", "/");
|
|
|
|
QStringList tmp = path.split("/");
|
|
|
|
tmp.removeLast();
|
|
|
|
return tmp.join("/");
|
|
|
|
}
|
|
|
|
|
2010-03-03 20:27:25 +03:00
|
|
|
static inline sha1_hash QStringToSha1(const QString& s) {
|
2010-06-06 14:10:45 +04:00
|
|
|
std::string str(s.toLocal8Bit().data());
|
|
|
|
std::istringstream i(str);
|
2010-03-20 22:09:27 +03:00
|
|
|
sha1_hash x;
|
|
|
|
i>>x;
|
|
|
|
return x;
|
2010-01-02 14:22:44 +03:00
|
|
|
}
|
|
|
|
|
2010-10-23 20:21:56 +04:00
|
|
|
static inline QString file_extension(const QString &filename) {
|
|
|
|
QString extension;
|
|
|
|
if(filename.contains(".")) {
|
|
|
|
extension = filename.mid(filename.lastIndexOf(".")+1);
|
|
|
|
}
|
|
|
|
return extension;
|
|
|
|
}
|
|
|
|
|
2010-08-16 21:35:32 +04:00
|
|
|
static void shutdownComputer();
|
|
|
|
|
2010-07-24 23:22:55 +04:00
|
|
|
static bool safeRemove(QString file_path) {
|
|
|
|
QFile MyFile(file_path);
|
|
|
|
if(!MyFile.exists()) return true;
|
|
|
|
// Make sure the permissions are ok
|
|
|
|
MyFile.setPermissions(MyFile.permissions()|QFile::ReadOwner|QFile::WriteOwner|QFile::ReadUser|QFile::WriteUser);
|
|
|
|
// Actually remove the file
|
|
|
|
return MyFile.remove();
|
|
|
|
}
|
|
|
|
|
2010-04-06 13:17:33 +04:00
|
|
|
static QString truncateRootFolder(boost::intrusive_ptr<torrent_info> t);
|
|
|
|
static QString truncateRootFolder(torrent_handle h);
|
|
|
|
|
2010-04-10 18:17:26 +04:00
|
|
|
static QString updateLabelInSavePath(const QString& defaultSavePath, QString save_path, const QString old_label, const QString new_label);
|
|
|
|
|
2010-03-20 22:09:27 +03:00
|
|
|
static bool sameFiles(QString path1, QString path2);
|
|
|
|
static void copyDir(QString src_path, QString dst_path);
|
|
|
|
// Introduced in v2.1.0 for backward compatibility
|
2010-01-02 14:22:44 +03:00
|
|
|
// Remove after some releases
|
2010-03-20 22:09:27 +03:00
|
|
|
static void moveToXDGFolders();
|
|
|
|
static QString toValidFileSystemName(QString filename);
|
|
|
|
static bool isValidFileSystemName(QString filename);
|
2010-01-03 01:20:37 +03:00
|
|
|
|
2010-03-20 22:09:27 +03:00
|
|
|
/* Ported from Qt4 to drop dependency on QtGui */
|
|
|
|
static QString QDesktopServicesDataLocation();
|
|
|
|
static QString QDesktopServicesCacheLocation();
|
2010-03-03 22:04:34 +03:00
|
|
|
/* End of Qt4 code */
|
|
|
|
|
2010-01-24 15:07:07 +03:00
|
|
|
#ifndef DISABLE_GUI
|
2010-01-22 01:23:13 +03:00
|
|
|
// Get screen center
|
2010-03-20 22:09:27 +03:00
|
|
|
static QPoint screenCenter(QWidget *win);
|
2010-01-24 15:07:07 +03:00
|
|
|
#endif
|
2010-03-20 22:09:27 +03:00
|
|
|
static QString searchEngineLocation();
|
|
|
|
static QString BTBackupLocation();
|
|
|
|
static QString cacheLocation();
|
|
|
|
static long long freeDiskSpaceOnPath(QString path);
|
2009-08-17 09:14:03 +04:00
|
|
|
// return best userfriendly storage unit (B, KiB, MiB, GiB, TiB)
|
|
|
|
// use Binary prefix standards from IEC 60027-2
|
|
|
|
// see http://en.wikipedia.org/wiki/Kilobyte
|
|
|
|
// value must be given in bytes
|
2010-03-20 22:09:27 +03:00
|
|
|
static QString friendlyUnit(double val);
|
|
|
|
static bool isPreviewable(QString extension);
|
|
|
|
static bool removeEmptyTree(QString path);
|
|
|
|
static QString magnetUriToName(QString magnet_uri);
|
|
|
|
static QString magnetUriToHash(QString magnet_uri);
|
2010-07-23 02:19:42 +04:00
|
|
|
static QString bcLinkToMagnet(QString bc_link);
|
2010-03-20 22:09:27 +03:00
|
|
|
static QString boostTimeToQString(const boost::optional<boost::posix_time::ptime> boostDate);
|
2009-12-22 13:29:39 +03:00
|
|
|
// Replace ~ in path
|
2010-03-20 22:09:27 +03:00
|
|
|
static QString expandPath(QString path);
|
2009-08-17 09:14:03 +04:00
|
|
|
// Take a number of seconds and return an user-friendly
|
|
|
|
// time duration like "1d 2h 10m".
|
2010-03-20 22:09:27 +03:00
|
|
|
static QString userFriendlyDuration(qlonglong seconds);
|
2010-06-09 13:48:14 +04:00
|
|
|
static QString getUserIDString();
|
2010-06-05 22:59:05 +04:00
|
|
|
|
|
|
|
// Convert functions
|
|
|
|
static QStringList toStringList(const QList<bool> &l);
|
|
|
|
static QList<int> intListfromStringList(const QStringList &l);
|
|
|
|
static QList<bool> boolListfromStringList(const QStringList &l);
|
2006-09-30 20:02:39 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
// Trick to get a portable sleep() function
|
2010-03-20 22:09:27 +03:00
|
|
|
class SleeperThread : public QThread {
|
2009-08-17 09:14:03 +04:00
|
|
|
public:
|
|
|
|
static void msleep(unsigned long msecs)
|
|
|
|
{
|
|
|
|
QThread::msleep(msecs);
|
|
|
|
}
|
2006-09-30 20:02:39 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|