2010-03-20 22:09:27 +03:00
/*
* Bittorrent Client using Qt4 and libtorrent .
* Copyright ( C ) 2006 Christophe Dumez
*
* 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 .
*
* 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
* along with this program ; if not , write to the Free Software
* Foundation , Inc . , 51 Franklin Street , Fifth Floor , Boston , MA 02110 - 1301 , USA .
*
* 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 .
*
* Contact : chris @ qbittorrent . org
*/
# include "misc.h"
# include <QUrl>
# include <QDir>
# include <QFileInfo>
# include <QDateTime>
# include <QByteArray>
2011-01-06 15:12:07 +03:00
# include <QDebug>
2011-09-11 21:22:54 +04:00
# include <QProcess>
2011-10-02 18:56:00 +04:00
# include <QSettings>
2010-03-20 22:09:27 +03:00
# ifdef DISABLE_GUI
# include <QCoreApplication>
# else
# include <QApplication>
# include <QDesktopWidget>
# endif
# ifdef Q_WS_WIN
2010-06-09 13:48:14 +04:00
# include <windows.h>
2010-12-31 16:36:32 +03:00
# include <PowrProf.h>
2010-06-09 13:48:14 +04:00
const int UNLEN = 256 ;
# else
# include <unistd.h>
# include <sys/types.h>
2010-03-20 22:09:27 +03:00
# endif
# ifdef Q_WS_MAC
# include <CoreServices/CoreServices.h>
2010-08-17 14:50:14 +04:00
# include <Carbon/Carbon.h>
2010-03-20 22:09:27 +03:00
# endif
2011-01-15 11:42:32 +03:00
# ifndef DISABLE_GUI
2011-03-07 23:22:35 +03:00
# if defined(Q_WS_X11) && defined(QT_DBUS_LIB)
2010-08-16 21:35:32 +04:00
# include <QDBusInterface>
# include <QDBusMessage>
# endif
2011-01-15 11:42:32 +03:00
# endif // DISABLE_GUI
2010-08-16 21:35:32 +04:00
2010-11-23 00:55:32 +03:00
using namespace libtorrent ;
2011-01-20 19:45:59 +03:00
static struct { const char * source ; const char * comment ; } units [ ] = {
2011-03-21 22:01:36 +03:00
QT_TRANSLATE_NOOP3 ( " misc " , " B " , " bytes " ) ,
QT_TRANSLATE_NOOP3 ( " misc " , " KiB " , " kibibytes (1024 bytes) " ) ,
QT_TRANSLATE_NOOP3 ( " misc " , " MiB " , " mebibytes (1024 kibibytes) " ) ,
QT_TRANSLATE_NOOP3 ( " misc " , " GiB " , " gibibytes (1024 mibibytes) " ) ,
QT_TRANSLATE_NOOP3 ( " misc " , " TiB " , " tebibytes (1024 gibibytes) " )
} ;
2010-12-02 21:21:45 +03:00
2011-01-15 11:42:32 +03:00
# ifndef DISABLE_GUI
2010-12-31 16:36:32 +03:00
void misc : : shutdownComputer ( bool sleep ) {
2011-03-07 23:22:35 +03:00
# if defined(Q_WS_X11) && defined(QT_DBUS_LIB)
2011-04-05 20:22:16 +04:00
// Use dbus to power off / suspend the system
2012-02-20 21:30:53 +04:00
if ( sleep ) {
2013-12-23 15:41:07 +04:00
// Some recent systems use systemd's logind
QDBusInterface login1Iface ( " org.freedesktop.login1 " , " /org/freedesktop/login1 " ,
" org.freedesktop.login1.Manager " , QDBusConnection : : systemBus ( ) ) ;
if ( login1Iface . isValid ( ) ) {
login1Iface . call ( " Suspend " , false ) ;
return ;
}
// Else, other recent systems use UPower
2011-04-05 20:22:16 +04:00
QDBusInterface upowerIface ( " org.freedesktop.UPower " , " /org/freedesktop/UPower " ,
2011-04-05 20:41:54 +04:00
" org.freedesktop.UPower " , QDBusConnection : : systemBus ( ) ) ;
2012-02-20 21:30:53 +04:00
if ( upowerIface . isValid ( ) ) {
2011-04-05 20:41:54 +04:00
upowerIface . call ( " Suspend " ) ;
return ;
}
// HAL (older systems)
QDBusInterface halIface ( " org.freedesktop.Hal " , " /org/freedesktop/Hal/devices/computer " ,
" org.freedesktop.Hal.Device.SystemPowerManagement " ,
QDBusConnection : : systemBus ( ) ) ;
halIface . call ( " Suspend " , 5 ) ;
2011-04-05 20:22:16 +04:00
} else {
2013-12-23 15:41:07 +04:00
// Some recent systems use systemd's logind
QDBusInterface login1Iface ( " org.freedesktop.login1 " , " /org/freedesktop/login1 " ,
" org.freedesktop.login1.Manager " , QDBusConnection : : systemBus ( ) ) ;
if ( login1Iface . isValid ( ) ) {
login1Iface . call ( " PowerOff " , false ) ;
return ;
}
// Else, other recent systems use ConsoleKit
2011-04-05 20:22:16 +04:00
QDBusInterface consolekitIface ( " org.freedesktop.ConsoleKit " , " /org/freedesktop/ConsoleKit/Manager " ,
" org.freedesktop.ConsoleKit.Manager " , QDBusConnection : : systemBus ( ) ) ;
2012-02-20 21:30:53 +04:00
if ( consolekitIface . isValid ( ) ) {
2011-04-05 20:41:54 +04:00
consolekitIface . call ( " Stop " ) ;
return ;
}
// HAL (older systems)
QDBusInterface halIface ( " org.freedesktop.Hal " , " /org/freedesktop/Hal/devices/computer " ,
" org.freedesktop.Hal.Device.SystemPowerManagement " ,
QDBusConnection : : systemBus ( ) ) ;
halIface . call ( " Shutdown " ) ;
2011-04-05 20:22:16 +04:00
}
2010-12-31 16:36:32 +03:00
# endif
# ifdef Q_WS_MAC
AEEventID EventToSend ;
2012-02-20 21:30:53 +04:00
if ( sleep )
2010-12-31 16:36:32 +03:00
EventToSend = kAESleep ;
else
EventToSend = kAEShutDown ;
2010-08-16 21:42:21 +04:00
AEAddressDesc targetDesc ;
static const ProcessSerialNumber kPSNOfSystemProcess = { 0 , kSystemProcess } ;
AppleEvent eventReply = { typeNull , NULL } ;
AppleEvent appleEventToSend = { typeNull , NULL } ;
OSStatus error = noErr ;
error = AECreateDesc ( typeProcessSerialNumber , & kPSNOfSystemProcess ,
sizeof ( kPSNOfSystemProcess ) , & targetDesc ) ;
if ( error ! = noErr )
{
2010-08-17 14:50:14 +04:00
return ;
2010-08-16 21:42:21 +04:00
}
error = AECreateAppleEvent ( kCoreEventClass , EventToSend , & targetDesc ,
kAutoGenerateReturnID , kAnyTransactionID , & appleEventToSend ) ;
AEDisposeDesc ( & targetDesc ) ;
if ( error ! = noErr )
{
2010-10-25 23:34:42 +04:00
return ;
2010-08-16 21:42:21 +04:00
}
error = AESend ( & appleEventToSend , & eventReply , kAENoReply ,
kAENormalPriority , kAEDefaultTimeout , NULL , NULL ) ;
AEDisposeDesc ( & appleEventToSend ) ;
if ( error ! = noErr )
{
2010-08-17 14:50:14 +04:00
return ;
2010-08-16 21:42:21 +04:00
}
AEDisposeDesc ( & eventReply ) ;
# endif
2010-08-16 21:57:15 +04:00
# ifdef Q_WS_WIN
2010-08-16 22:34:30 +04:00
HANDLE hToken ; // handle to process token
TOKEN_PRIVILEGES tkp ; // pointer to token structure
2012-02-20 21:30:53 +04:00
if ( ! OpenProcessToken ( GetCurrentProcess ( ) , TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY , & hToken ) )
2010-08-16 22:34:30 +04:00
return ;
// Get the LUID for shutdown privilege.
LookupPrivilegeValue ( NULL , SE_SHUTDOWN_NAME ,
& tkp . Privileges [ 0 ] . Luid ) ;
tkp . PrivilegeCount = 1 ; // one privilege to set
tkp . Privileges [ 0 ] . Attributes = SE_PRIVILEGE_ENABLED ;
// Get shutdown privilege for this process.
AdjustTokenPrivileges ( hToken , FALSE , & tkp , 0 ,
( PTOKEN_PRIVILEGES ) NULL , 0 ) ;
// Cannot test the return value of AdjustTokenPrivileges.
if ( GetLastError ( ) ! = ERROR_SUCCESS )
return ;
2011-09-22 21:44:01 +04:00
2012-02-20 21:30:53 +04:00
if ( sleep )
2010-12-31 16:36:32 +03:00
SetSuspendState ( false , false , false ) ;
else
2013-07-27 01:43:58 +04:00
InitiateSystemShutdownA ( 0 , QCoreApplication : : translate ( " misc " , " qBittorrent will shutdown the computer now because all downloads are complete. " ) . toLocal8Bit ( ) . data ( ) , 10 , true , false ) ;
2010-08-16 22:34:30 +04:00
// Disable shutdown privilege.
tkp . Privileges [ 0 ] . Attributes = 0 ;
AdjustTokenPrivileges ( hToken , FALSE , & tkp , 0 ,
( PTOKEN_PRIVILEGES ) NULL , 0 ) ;
2010-08-16 21:57:15 +04:00
# endif
2010-08-16 21:35:32 +04:00
}
2011-01-15 11:48:20 +03:00
# endif // DISABLE_GUI
2010-08-16 21:35:32 +04:00
2010-03-20 22:09:27 +03:00
# ifndef DISABLE_GUI
// Get screen center
QPoint misc : : screenCenter ( QWidget * win ) {
int scrn = 0 ;
const QWidget * w = win - > window ( ) ;
2012-02-20 21:30:53 +04:00
if ( w )
2010-03-20 22:09:27 +03:00
scrn = QApplication : : desktop ( ) - > screenNumber ( w ) ;
2012-02-20 21:30:53 +04:00
else if ( QApplication : : desktop ( ) - > isVirtualDesktop ( ) )
2010-03-20 22:09:27 +03:00
scrn = QApplication : : desktop ( ) - > screenNumber ( QCursor : : pos ( ) ) ;
else
scrn = QApplication : : desktop ( ) - > screenNumber ( win ) ;
QRect desk ( QApplication : : desktop ( ) - > availableGeometry ( scrn ) ) ;
return QPoint ( ( desk . width ( ) - win - > frameGeometry ( ) . width ( ) ) / 2 , ( desk . height ( ) - win - > frameGeometry ( ) . height ( ) ) / 2 ) ;
}
# endif
2011-09-11 21:22:54 +04:00
/**
* Detects the version of python by calling
* " python --version " and parsing the output .
*/
int misc : : pythonVersion ( ) {
static int version = - 1 ;
if ( version < 0 ) {
QProcess python_proc ;
python_proc . start ( " python " , QStringList ( ) < < " --version " , QIODevice : : ReadOnly ) ;
if ( ! python_proc . waitForFinished ( ) ) return - 1 ;
if ( python_proc . exitCode ( ) < 0 ) return - 1 ;
QByteArray output = python_proc . readAllStandardOutput ( ) ;
if ( output . isEmpty ( ) )
output = python_proc . readAllStandardError ( ) ;
const QByteArray version_str = output . split ( ' ' ) . last ( ) ;
qDebug ( ) < < " Python version is: " < < version_str . trimmed ( ) ;
if ( version_str . startsWith ( " 3. " ) )
version = 3 ;
else
version = 2 ;
}
return version ;
}
2010-03-20 22:09:27 +03: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
2012-05-26 21:42:44 +04:00
QString misc : : friendlyUnit ( qreal val , bool is_speed ) {
2012-02-20 21:30:53 +04:00
if ( val < 0 )
2013-07-27 01:43:58 +04:00
return QCoreApplication : : translate ( " misc " , " Unknown " , " Unknown (size) " ) ;
2010-12-02 21:21:45 +03:00
int i = 0 ;
2010-03-20 22:09:27 +03:00
while ( val > = 1024. & & i + + < 6 )
val / = 1024. ;
2012-05-26 21:42:44 +04:00
QString ret ;
2012-02-20 21:30:53 +04:00
if ( i = = 0 )
2013-07-27 01:43:58 +04:00
ret = QString : : number ( ( long ) val ) + " " + QCoreApplication : : translate ( " misc " , units [ 0 ] . source , units [ 0 ] . comment ) ;
2012-05-26 21:42:44 +04:00
else
2013-09-10 18:48:16 +04:00
/* 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 */
ret = QString : : number ( ( int ) ( val * 10 ) / 10.0 , ' f ' , 1 ) + " " + QCoreApplication : : translate ( " misc " , units [ i ] . source , units [ i ] . comment ) ;
2012-05-26 21:42:44 +04:00
if ( is_speed )
2013-07-27 01:43:58 +04:00
ret + = QCoreApplication : : translate ( " misc " , " /s " , " per second " ) ;
2012-05-26 21:42:44 +04:00
return ret ;
2010-03-20 22:09:27 +03:00
}
2012-11-11 21:23:10 +04:00
bool misc : : isPreviewable ( const QString & extension ) {
static QSet < QString > multimedia_extensions ;
if ( multimedia_extensions . empty ( ) ) {
multimedia_extensions . insert ( " 3GP " ) ;
multimedia_extensions . insert ( " AAC " ) ;
multimedia_extensions . insert ( " AC3 " ) ;
multimedia_extensions . insert ( " AIF " ) ;
multimedia_extensions . insert ( " AIFC " ) ;
2012-11-11 21:27:05 +04:00
multimedia_extensions . insert ( " AIFF " ) ;
2012-11-11 21:23:10 +04:00
multimedia_extensions . insert ( " ASF " ) ;
multimedia_extensions . insert ( " AU " ) ;
multimedia_extensions . insert ( " AVI " ) ;
multimedia_extensions . insert ( " FLAC " ) ;
multimedia_extensions . insert ( " FLV " ) ;
multimedia_extensions . insert ( " M3U " ) ;
multimedia_extensions . insert ( " M4A " ) ;
multimedia_extensions . insert ( " M4P " ) ;
multimedia_extensions . insert ( " M4V " ) ;
multimedia_extensions . insert ( " MID " ) ;
multimedia_extensions . insert ( " MKV " ) ;
multimedia_extensions . insert ( " MOV " ) ;
multimedia_extensions . insert ( " MP2 " ) ;
multimedia_extensions . insert ( " MP3 " ) ;
multimedia_extensions . insert ( " MP4 " ) ;
2012-11-11 21:27:05 +04:00
multimedia_extensions . insert ( " MPC " ) ;
2012-11-11 21:23:10 +04:00
multimedia_extensions . insert ( " MPE " ) ;
multimedia_extensions . insert ( " MPEG " ) ;
multimedia_extensions . insert ( " MPG " ) ;
multimedia_extensions . insert ( " MPP " ) ;
multimedia_extensions . insert ( " OGG " ) ;
multimedia_extensions . insert ( " OGM " ) ;
2012-11-11 21:27:05 +04:00
multimedia_extensions . insert ( " OGV " ) ;
2012-11-11 21:23:10 +04:00
multimedia_extensions . insert ( " QT " ) ;
multimedia_extensions . insert ( " RA " ) ;
multimedia_extensions . insert ( " RAM " ) ;
multimedia_extensions . insert ( " RM " ) ;
multimedia_extensions . insert ( " RMV " ) ;
multimedia_extensions . insert ( " RMVB " ) ;
multimedia_extensions . insert ( " SWA " ) ;
multimedia_extensions . insert ( " SWF " ) ;
multimedia_extensions . insert ( " VOB " ) ;
multimedia_extensions . insert ( " WAV " ) ;
multimedia_extensions . insert ( " WMA " ) ;
2012-11-11 21:27:05 +04:00
multimedia_extensions . insert ( " WMV " ) ;
2012-11-11 21:23:10 +04:00
}
if ( extension . isEmpty ( ) )
return false ;
return multimedia_extensions . contains ( extension . toUpper ( ) ) ;
2010-03-20 22:09:27 +03:00
}
2010-07-23 02:19:42 +04:00
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
// Format is now AA/url_encoded_filename/size_bytes/info_hash/ZZ
QStringList parts = QString ( raw_bc ) . split ( " / " ) ;
2012-02-20 21:30:53 +04:00
if ( parts . size ( ) ! = 5 ) return QString : : null ;
2010-07-23 02:19:42 +04:00
QString filename = parts . at ( 1 ) ;
QString hash = parts . at ( 3 ) ;
QString magnet = " magnet:?xt=urn:btih: " + hash ;
magnet + = " &dn= " + filename ;
return magnet ;
}
2012-09-23 12:09:01 +04:00
QString misc : : magnetUriToName ( const QString & magnet_uri ) {
2010-03-20 22:09:27 +03:00
QString name = " " ;
QRegExp regHex ( " dn=([^&]+) " ) ;
const int pos = regHex . indexIn ( magnet_uri ) ;
2012-02-20 21:30:53 +04:00
if ( pos > - 1 ) {
2010-07-23 18:05:53 +04:00
const QString found = regHex . cap ( 1 ) ;
2010-03-20 22:09:27 +03:00
// URL decode
name = QUrl : : fromPercentEncoding ( found . toLocal8Bit ( ) ) . replace ( " + " , " " ) ;
}
return name ;
}
2012-09-23 12:09:01 +04:00
QList < QUrl > misc : : magnetUriToTrackers ( const QString & magnet_uri )
{
QList < QUrl > trackers ;
QRegExp rx ( " tr=([^&]+) " ) ;
int pos = 0 ;
while ( ( pos = rx . indexIn ( magnet_uri , pos ) ) ! = - 1 ) {
const QUrl tracker = QUrl : : fromEncoded ( rx . cap ( 1 ) . toUtf8 ( ) ) ;
qDebug ( ) < < Q_FUNC_INFO < < " Found tracker: " < < tracker . toString ( ) ;
trackers < < tracker ;
pos + = rx . matchedLength ( ) ;
}
return trackers ;
}
QString misc : : magnetUriToHash ( const QString & magnet_uri ) {
2010-03-20 22:09:27 +03:00
QString hash = " " ;
QRegExp regHex ( " urn:btih:([0-9A-Za-z]+) " ) ;
// Hex
int pos = regHex . indexIn ( magnet_uri ) ;
2012-02-20 21:30:53 +04:00
if ( pos > - 1 ) {
2010-07-23 18:05:53 +04:00
const QString found = regHex . cap ( 1 ) ;
2011-06-05 20:08:30 +04:00
qDebug ( ) < < Q_FUNC_INFO < < " regex found: " < < found ;
2012-02-20 21:30:53 +04:00
if ( found . length ( ) = = 40 ) {
2011-06-05 20:08:30 +04:00
const sha1_hash sha1 ( QByteArray : : fromHex ( found . toAscii ( ) ) . constData ( ) ) ;
2010-03-20 22:09:27 +03:00
qDebug ( " magnetUriToHash (Hex): hash: %s " , qPrintable ( misc : : toQString ( sha1 ) ) ) ;
return misc : : toQString ( sha1 ) ;
}
}
// Base 32
QRegExp regBase32 ( " urn:btih:([A-Za-z2-7=]+) " ) ;
pos = regBase32 . indexIn ( magnet_uri ) ;
2012-02-20 21:30:53 +04:00
if ( pos > - 1 ) {
2010-07-23 18:05:53 +04:00
const QString found = regBase32 . cap ( 1 ) ;
2012-02-20 21:30:53 +04:00
if ( found . length ( ) > 20 & & ( found . length ( ) * 5 ) % 40 = = 0 ) {
2010-03-20 22:09:27 +03:00
const sha1_hash sha1 ( base32decode ( regBase32 . cap ( 1 ) . toStdString ( ) ) ) ;
hash = misc : : toQString ( sha1 ) ;
}
}
qDebug ( " magnetUriToHash (base32): hash: %s " , qPrintable ( hash ) ) ;
return hash ;
}
// Take a number of seconds and return an user-friendly
// time duration like "1d 2h 10m".
QString misc : : userFriendlyDuration ( qlonglong seconds ) {
2012-02-20 21:30:53 +04:00
if ( seconds < 0 | | seconds > = MAX_ETA ) {
2010-03-20 22:09:27 +03:00
return QString : : fromUtf8 ( " ∞ " ) ;
}
2012-02-20 21:30:53 +04:00
if ( seconds = = 0 ) {
2010-05-21 15:25:30 +04:00
return " 0 " ;
}
2012-02-20 21:30:53 +04:00
if ( seconds < 60 ) {
2013-07-27 01:43:58 +04:00
return QCoreApplication : : translate ( " misc " , " < 1m " , " < 1 minute " ) ;
2010-03-20 22:09:27 +03:00
}
int minutes = seconds / 60 ;
2012-02-20 21:30:53 +04:00
if ( minutes < 60 ) {
2013-07-27 01:43:58 +04:00
return QCoreApplication : : translate ( " misc " , " %1m " , " e.g: 10minutes " ) . arg ( QString : : number ( minutes ) ) ;
2010-03-20 22:09:27 +03:00
}
int hours = minutes / 60 ;
minutes = minutes - hours * 60 ;
2012-02-20 21:30:53 +04:00
if ( hours < 24 ) {
2013-07-27 01:43:58 +04:00
return QCoreApplication : : translate ( " misc " , " %1h %2m " , " e.g: 3hours 5minutes " ) . arg ( QString : : number ( hours ) ) . arg ( QString : : number ( minutes ) ) ;
2010-03-20 22:09:27 +03:00
}
int days = hours / 24 ;
hours = hours - days * 24 ;
2012-02-20 21:30:53 +04:00
if ( days < 100 ) {
2013-07-27 01:43:58 +04:00
return QCoreApplication : : translate ( " misc " , " %1d %2h " , " e.g: 2days 10hours " ) . arg ( QString : : number ( days ) ) . arg ( QString : : number ( hours ) ) ;
2010-03-20 22:09:27 +03:00
}
return QString : : fromUtf8 ( " ∞ " ) ;
}
2010-06-05 22:59:05 +04:00
2010-06-09 13:48:14 +04:00
QString misc : : getUserIDString ( ) {
QString uid = " 0 " ;
# ifdef Q_WS_WIN
char buffer [ UNLEN + 1 ] = { 0 } ;
DWORD buffer_len = UNLEN + 1 ;
if ( ! GetUserNameA ( buffer , & buffer_len ) )
uid = QString ( buffer ) ;
# else
uid = QString : : number ( getuid ( ) ) ;
# endif
return uid ;
}
2010-06-05 22:59:05 +04:00
QStringList misc : : toStringList ( const QList < bool > & l ) {
QStringList ret ;
2012-02-20 21:30:53 +04:00
foreach ( const bool & b , l ) {
2010-12-02 21:21:45 +03:00
ret < < ( b ? " 1 " : " 0 " ) ;
2010-06-05 22:59:05 +04:00
}
return ret ;
}
QList < int > misc : : intListfromStringList ( const QStringList & l ) {
QList < int > ret ;
2012-02-20 21:30:53 +04:00
foreach ( const QString & s , l ) {
2010-06-05 22:59:05 +04:00
ret < < s . toInt ( ) ;
}
return ret ;
}
QList < bool > misc : : boolListfromStringList ( const QStringList & l ) {
QList < bool > ret ;
2012-02-20 21:30:53 +04:00
foreach ( const QString & s , l ) {
2010-12-02 21:21:45 +03:00
ret < < ( s = = " 1 " ) ;
2010-08-16 21:42:21 +04:00
}
return ret ;
2010-06-05 22:59:05 +04:00
}
2010-10-25 23:34:42 +04:00
2010-12-30 22:12:03 +03:00
bool misc : : isUrl ( const QString & s )
{
const QString scheme = QUrl ( s ) . scheme ( ) ;
QRegExp is_url ( " http[s]?|ftp " , Qt : : CaseInsensitive ) ;
return is_url . exactMatch ( scheme ) ;
}
2011-04-04 21:16:34 +04:00
QString misc : : parseHtmlLinks ( const QString & raw_text )
{
QString result = raw_text ;
QRegExp reURL ( " ( \\ s|^) " //start with whitespace or beginning of line
" ( "
" ( " //case 1 -- URL with scheme
" (http(s?)) \\ :// " //start with scheme
" ([a-zA-Z0-9_-]+ \\ .)+ " // domainpart. at least one of these must exist
" ([a-zA-Z0-9 \\ ?%=&/_ \\ .:#;-]+) " // everything to 1st non-URI char, must be at least one char after the previous dot (cannot use ".*" because it can be too greedy)
" ) "
" | "
" ( " //case 2a -- no scheme, contains common TLD example.com
" ([a-zA-Z0-9_-]+ \\ .)+ " // domainpart. at least one of these must exist
" (?= " // must be followed by TLD
" AERO|aero| " //N.B. assertions are non-capturing
" ARPA|arpa| "
" ASIA|asia| "
" BIZ|biz| "
" CAT|cat| "
" COM|com| "
" COOP|coop| "
" EDU|edu| "
" GOV|gov| "
" INFO|info| "
" INT|int| "
" JOBS|jobs| "
" MIL|mil| "
" MOBI|mobi| "
" MUSEUM|museum| "
" NAME|name| "
" NET|net| "
" ORG|org| "
" PRO|pro| "
" RO|ro| "
" RU|ru| "
" TEL|tel| "
" TRAVEL|travel "
" ) "
" ([a-zA-Z0-9 \\ ?%=&/_ \\ .:#;-]+) " // everything to 1st non-URI char, must be at least one char after the previous dot (cannot use ".*" because it can be too greedy)
" ) "
" | "
" ( " // case 2b no scheme, no TLD, must have at least 2 aphanum strings plus uncommon TLD string --> del.icio.us
2012-02-20 21:56:07 +04:00
" ([a-zA-Z0-9_-]+ \\ .) {2,} " //2 or more domainpart. --> del.icio.
2011-04-04 21:16:34 +04:00
" [a-zA-Z]{2,} " //one ab (2 char or longer) --> us
" ([a-zA-Z0-9 \\ ?%=&/_ \\ .:#;-]*) " // everything to 1st non-URI char, maybe nothing in case of del.icio.us/path
" ) "
" ) "
) ;
// Capture links
result . replace ( reURL , " \\ 1<a href= \" \\ 2 \" > \\ 2</a> " ) ;
// Capture links without scheme
QRegExp reNoScheme ( " <a \\ s+href= \" (?!http(s?) ) ( [ a - zA - Z0 - 9 \ \ ? % = & / _ \ \ . - : # ] + ) \ \ s * \ " > " ) ;
result . replace ( reNoScheme , " <a href= \" http:// \\ 1 \" > " ) ;
return result ;
}
2012-05-15 20:57:31 +04:00
QString misc : : toQString ( time_t t )
{
return QDateTime : : fromTime_t ( t ) . toString ( Qt : : DefaultLocaleLongDate ) ;
}
2013-07-08 18:31:20 +04:00
# ifndef DISABLE_GUI
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
do {
int posL = left . indexOf ( QRegExp ( " [0-9] " ) ) ;
int posR = right . indexOf ( QRegExp ( " [0-9] " ) ) ;
if ( posL = = - 1 | | posR = = - 1 )
break ; // No data
else if ( posL ! = posR )
break ; // Digit positions mismatch
else if ( left . left ( posL ) ! = right . left ( posR ) )
break ; // Strings' subsets before digit do not match
QString temp ;
while ( posL < left . size ( ) ) {
if ( left . at ( posL ) . isDigit ( ) )
temp + = left . at ( posL ) ;
else
break ;
posL + + ;
}
int numL = temp . toInt ( ) ;
temp . clear ( ) ;
while ( posR < right . size ( ) ) {
if ( right . at ( posR ) . isDigit ( ) )
temp + = right . at ( posR ) ;
else
break ;
posR + + ;
}
int numR = temp . toInt ( ) ;
if ( numL ! = numR ) {
result = ( numL < numR ) ;
return true ;
}
// Strings + digits do match and we haven't hit string end
// Do another round
left . remove ( 0 , posL ) ;
right . remove ( 0 , posR ) ;
} while ( true ) ;
return false ;
}
# endif