2008-11-17 20:42:17 -05:00
/****************************************************************
* This file is distributed under the following license :
*
* Copyright ( c ) 2008 , defnax
* 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 .
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
# ifndef MISC_H
# define MISC_H
# include <stdexcept>
# include <QObject>
# include <QPair>
# include <QThread>
2014-05-29 10:49:45 -04:00
# include <QFileDialog>
2008-11-17 20:42:17 -05:00
2010-12-02 19:54:40 -05:00
# include "gui/settings/rsharesettings.h"
2008-11-17 20:42:17 -05:00
/* Miscellaneaous functions that can be useful */
2010-12-02 19:54:40 -05:00
class misc : public QObject
{
Q_OBJECT
2008-11-17 20:42:17 -05:00
2010-12-02 19:54:40 -05:00
public :
2008-11-17 20:42:17 -05:00
// Convert any type of variable to C++ String
// convert=true will convert -1 to 0
2012-03-28 14:02:49 -04:00
// template <class T> static std::string toString(const T& x, bool convert=false) {
// std::ostringstream o;
// if(!(o<<x)) {
// throw std::runtime_error("::toString()");
// }
// if(o.str() == "-1" && convert)
// return "0";
// return o.str();
// }
2008-11-17 20:42:17 -05:00
2012-03-28 14:02:49 -04:00
// template <class T> static QString toQString(const T& x, bool convert=false) {
// std::ostringstream o;
// if(!(o<<x)) {
// throw std::runtime_error("::toString()");
// }
// if(o.str() == "-1" && convert)
// return QString::fromUtf8("0");
// return QString::fromUtf8(o.str().c_str());
// }
2008-11-17 20:42:17 -05:00
2012-03-28 14:02:49 -04:00
// template <class T> static QByteArray toQByteArray(const T& x, bool convert=false) {
// std::ostringstream o;
// if(!(o<<x)) {
// throw std::runtime_error("::toString()");
// }
// if(o.str() == "-1" && convert)
// return "0";
// return QByteArray(o.str().c_str());
// }
2008-11-17 20:42:17 -05:00
// Convert C++ string to any type of variable
2012-03-28 14:02:49 -04:00
// template <class T> static T fromString(const std::string& s) {
// T x;
// std::istringstream i(s);
// if(!(i>>x)) {
// throw std::runtime_error("::fromString()");
// }
// return x;
// }
2008-11-17 20:42:17 -05:00
// template <class T> static T fromQString::fromUtf8(const QString& s) {
// T x;
// std::istringstream i((const char*)s.toUtf8());
// if(!(i>>x)) {
// throw std::runtime_error("::fromString()");
// }
// return x;
// }
//
// template <class T> static T fromQByteArray(const QByteArray& s) {
// T x;
// std::istringstream i((const char*)s);
// if(!(i>>x)) {
// throw std::runtime_error("::fromString()");
// }
// return x;
// }
// 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-12-02 19:54:40 -05:00
static QString friendlyUnit ( float val ) ;
2008-11-17 20:42:17 -05:00
2010-12-02 19:54:40 -05:00
static bool isPreviewable ( QString extension ) ;
2008-11-17 20:42:17 -05:00
2013-11-09 17:52:26 -05:00
static QString fingerPrintStyleSplit ( const QString & in ) ;
2008-11-17 20:42:17 -05:00
// return qBittorrent config path
2010-12-02 19:54:40 -05:00
static QString qBittorrentPath ( ) ;
2008-11-17 20:42:17 -05:00
2010-12-02 19:54:40 -05:00
static QString findFileInDir ( QString dir_path , QString fileName ) ;
2008-11-17 20:42:17 -05:00
// Insertion sort, used instead of bubble sort because it is
// approx. 5 times faster.
2010-12-02 19:54:40 -05:00
// template <class T> static void insertSort(QList<QPair<int, T> > &list, const QPair<int, T>& value, Qt::SortOrder sortOrder) {
// int i = 0;
// if(sortOrder == Qt::AscendingOrder) {
// while(i < list.size() and value.second > list.at(i).second) {
// ++i;
// }
// }else{
// while(i < list.size() and value.second < list.at(i).second) {
// ++i;
// }
// }
// list.insert(i, value);
// }
// template <class T> static void insertSort2(QList<QPair<int, T> > &list, const QPair<int, T>& value, Qt::SortOrder sortOrder) {
// int i = 0;
// if(sortOrder == Qt::AscendingOrder) {
// while(i < list.size() and value.first > list.at(i).first) {
// ++i;
// }
// }else{
// while(i < list.size() and value.first < list.at(i).first) {
// ++i;
// }
// }
// list.insert(i, value);
// }
2008-11-17 20:42:17 -05:00
// Can't use template class for QString because >,< use unicode code for sorting
// which is not what a human would expect when sorting strings.
2010-12-02 19:54:40 -05:00
static void insertSortString ( QList < QPair < int , QString > > & list , QPair < int , QString > value , Qt : : SortOrder sortOrder ) ;
2008-11-17 20:42:17 -05:00
2010-12-02 19:54:40 -05:00
static float getPluginVersion ( QString filePath ) ;
2008-11-17 20:42:17 -05:00
// Take a number of seconds and return an user-friendly
// time duration like "1d 2h 10m".
2010-12-02 19:54:40 -05:00
static QString userFriendlyDuration ( qlonglong seconds ) ;
2010-11-08 07:25:49 -05:00
2016-11-14 15:58:58 -05:00
// Computes the time shift between now and the given time, and prints it in a friendly way, accounting for possible negative shifts (time from the future!)
static QString timeRelativeToNow ( uint32_t mtime ) ;
2010-12-02 19:54:40 -05:00
static QString userFriendlyUnit ( double count , unsigned int decimal , double factor = 1000 ) ;
2010-11-09 14:57:05 -05:00
2010-12-02 19:54:40 -05:00
static QString removeNewLine ( const QString & text ) ;
static QString removeNewLine ( const std : : string & text ) ;
static QString removeNewLine ( const std : : wstring & text ) ;
2010-11-09 14:57:05 -05:00
2011-08-12 16:27:05 -04:00
static bool getOpenAvatarPicture ( QWidget * parent , QByteArray & image_data ) ;
static QPixmap getOpenThumbnailedPicture ( QWidget * parent , const QString & caption , int width , int height ) ;
2014-05-29 10:49:45 -04:00
static bool getOpenFileName ( QWidget * parent , RshareSettings : : enumLastDir type
, const QString & caption , const QString & filter
, QString & file , QFileDialog : : Options options = 0 ) ;
static bool getOpenFileNames ( QWidget * parent , RshareSettings : : enumLastDir type
, const QString & caption , const QString & filter
, QStringList & files , QFileDialog : : Options options = 0 ) ;
static bool getSaveFileName ( QWidget * parent , RshareSettings : : enumLastDir type
, const QString & caption , const QString & filter
, QString & file , QString * selectedFilter = NULL
, QFileDialog : : Options options = 0 ) ;
2008-11-17 20:42:17 -05:00
} ;
// Trick to get a portable sleep() function
class SleeperThread : public QThread {
public :
static void msleep ( unsigned long msecs )
{
QThread : : msleep ( msecs ) ;
}
} ;
2017-02-27 16:29:01 -05:00
template < class T > class SignalsBlocker
{
public :
SignalsBlocker ( T * blocked ) : blocked ( blocked ) , previous ( blocked - > blockSignals ( true ) ) { }
~ SignalsBlocker ( ) { blocked - > blockSignals ( previous ) ; }
T * operator - > ( ) { return blocked ; }
private :
T * blocked ;
bool previous ;
} ;
template < class T > inline SignalsBlocker < T > whileBlocking ( T * blocked ) { return SignalsBlocker < T > ( blocked ) ; }
2008-11-17 20:42:17 -05:00
# endif