2009-04-09 18:55:47 +00:00
|
|
|
/*
|
2015-06-10 21:27:11 +00:00
|
|
|
* This file Copyright (C) 2009-2015 Mnemosyne LLC
|
2009-04-09 18:55:47 +00:00
|
|
|
*
|
2014-12-21 23:49:39 +00:00
|
|
|
* It may be used under the GNU GPL versions 2 or 3
|
2014-01-19 01:09:44 +00:00
|
|
|
* or any future license endorsed by Mnemosyne LLC.
|
2009-04-09 18:55:47 +00:00
|
|
|
*
|
2009-05-31 19:33:48 +00:00
|
|
|
* $Id$
|
2009-04-09 18:55:47 +00:00
|
|
|
*/
|
|
|
|
|
2014-07-04 00:00:07 +00:00
|
|
|
#ifdef _WIN32
|
2013-09-08 18:53:11 +00:00
|
|
|
#include <windows.h>
|
|
|
|
#include <shellapi.h>
|
|
|
|
#endif
|
|
|
|
|
2009-04-09 18:55:47 +00:00
|
|
|
#include <QApplication>
|
2015-06-10 21:27:11 +00:00
|
|
|
#include <QColor>
|
2009-04-09 18:55:47 +00:00
|
|
|
#include <QDataStream>
|
|
|
|
#include <QFile>
|
2010-06-04 01:00:27 +00:00
|
|
|
#include <QFileDialog>
|
2009-04-09 18:55:47 +00:00
|
|
|
#include <QFileInfo>
|
2010-06-04 01:00:27 +00:00
|
|
|
#include <QInputDialog>
|
2009-04-09 18:55:47 +00:00
|
|
|
#include <QObject>
|
2013-09-08 18:53:11 +00:00
|
|
|
#include <QPixmapCache>
|
2009-04-09 18:55:47 +00:00
|
|
|
#include <QSet>
|
|
|
|
#include <QStyle>
|
|
|
|
|
2015-01-01 02:28:18 +00:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
|
|
|
|
#include <QMimeDatabase>
|
|
|
|
#include <QMimeType>
|
|
|
|
#endif
|
|
|
|
|
2009-04-09 18:55:47 +00:00
|
|
|
#include <libtransmission/transmission.h>
|
2010-06-22 04:34:16 +00:00
|
|
|
#include <libtransmission/utils.h> // tr_formatter
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2015-06-10 21:27:11 +00:00
|
|
|
#include "Utils.h"
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2010-07-03 00:25:22 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2014-07-04 00:00:07 +00:00
|
|
|
#if defined(_WIN32) && QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
|
2013-09-08 18:53:11 +00:00
|
|
|
// Should be in QtWinExtras soon, but for now let's import it manually
|
|
|
|
extern QPixmap qt_pixmapFromWinHICON(HICON icon);
|
|
|
|
#endif
|
|
|
|
|
2014-07-04 00:00:07 +00:00
|
|
|
#ifdef _WIN32
|
2013-09-08 18:53:11 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
void
|
|
|
|
addAssociatedFileIcon (const QFileInfo& fileInfo, UINT iconSize, QIcon& icon)
|
|
|
|
{
|
2013-09-14 22:45:04 +00:00
|
|
|
QString const pixmapCacheKey = QLatin1String ("tr_file_ext_")
|
|
|
|
+ QString::number (iconSize)
|
2015-01-29 22:10:00 +00:00
|
|
|
+ QLatin1Char ('_')
|
2013-09-14 22:45:04 +00:00
|
|
|
+ fileInfo.suffix ();
|
|
|
|
|
2013-09-08 18:53:11 +00:00
|
|
|
QPixmap pixmap;
|
|
|
|
if (!QPixmapCache::find (pixmapCacheKey, &pixmap))
|
|
|
|
{
|
|
|
|
const QString filename = fileInfo.fileName ();
|
|
|
|
|
|
|
|
SHFILEINFO shellFileInfo;
|
2014-12-27 14:07:14 +00:00
|
|
|
if (::SHGetFileInfoW (reinterpret_cast<const wchar_t*> (filename.utf16 ()), FILE_ATTRIBUTE_NORMAL,
|
2013-09-08 18:53:11 +00:00
|
|
|
&shellFileInfo, sizeof(shellFileInfo),
|
|
|
|
SHGFI_ICON | iconSize | SHGFI_USEFILEATTRIBUTES) != 0)
|
|
|
|
{
|
|
|
|
if (shellFileInfo.hIcon != NULL)
|
|
|
|
{
|
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
|
|
|
|
pixmap = qt_pixmapFromWinHICON (shellFileInfo.hIcon);
|
|
|
|
#else
|
|
|
|
pixmap = QPixmap::fromWinHICON (shellFileInfo.hIcon);
|
|
|
|
#endif
|
|
|
|
::DestroyIcon (shellFileInfo.hIcon);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QPixmapCache::insert (pixmapCacheKey, pixmap);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!pixmap.isNull ())
|
|
|
|
icon.addPixmap (pixmap);
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
#endif
|
|
|
|
|
|
|
|
QIcon
|
2014-12-12 23:05:10 +00:00
|
|
|
Utils::guessMimeIcon (const QString& filename)
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2015-01-01 02:28:18 +00:00
|
|
|
static const QIcon fallback = qApp->style ()->standardIcon (QStyle::SP_FileIcon);
|
|
|
|
|
2014-07-04 00:00:07 +00:00
|
|
|
#ifdef _WIN32
|
2015-01-01 02:28:18 +00:00
|
|
|
|
2013-09-08 18:53:11 +00:00
|
|
|
QIcon icon;
|
|
|
|
|
|
|
|
if (!filename.isEmpty ())
|
|
|
|
{
|
|
|
|
const QFileInfo fileInfo (filename);
|
|
|
|
|
|
|
|
addAssociatedFileIcon (fileInfo, SHGFI_SMALLICON, icon);
|
|
|
|
addAssociatedFileIcon (fileInfo, 0, icon);
|
|
|
|
addAssociatedFileIcon (fileInfo, SHGFI_LARGEICON, icon);
|
|
|
|
}
|
|
|
|
|
2015-01-01 02:28:18 +00:00
|
|
|
if (!icon.isNull ())
|
|
|
|
return icon;
|
|
|
|
|
|
|
|
#elif QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
|
|
|
|
|
|
|
|
QMimeDatabase mimeDb;
|
|
|
|
QMimeType mimeType = mimeDb.mimeTypeForFile (filename, QMimeDatabase::MatchExtension);
|
|
|
|
if (mimeType.isValid ())
|
|
|
|
return QIcon::fromTheme (mimeType.iconName (), QIcon::fromTheme (mimeType.genericIconName (), fallback));
|
2013-09-08 18:53:11 +00:00
|
|
|
|
|
|
|
#else
|
2015-01-01 02:28:18 +00:00
|
|
|
|
2013-09-14 22:45:04 +00:00
|
|
|
enum { DISK, DOCUMENT, PICTURE, VIDEO, ARCHIVE, AUDIO, APP, TYPE_COUNT };
|
|
|
|
static QIcon fileIcons[TYPE_COUNT];
|
|
|
|
static QSet<QString> suffixes[TYPE_COUNT];
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2013-09-14 22:45:04 +00:00
|
|
|
if (fileIcons[0].isNull ())
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2013-09-14 22:45:04 +00:00
|
|
|
suffixes[DISK] << QString::fromLatin1("iso");
|
|
|
|
fileIcons[DISK]= QIcon::fromTheme (QString::fromLatin1("media-optical"), fallback);
|
|
|
|
|
|
|
|
const char * doc_types[] = {
|
|
|
|
"abw", "csv", "doc", "dvi", "htm", "html", "ini", "log", "odp",
|
|
|
|
"ods", "odt", "pdf", "ppt", "ps", "rtf", "tex", "txt", "xml" };
|
2015-04-18 14:41:06 +00:00
|
|
|
for (const char * t: doc_types)
|
|
|
|
suffixes[DOCUMENT] << QString::fromLatin1(t);
|
2013-09-14 22:45:04 +00:00
|
|
|
fileIcons[DOCUMENT] = QIcon::fromTheme (QString::fromLatin1("text-x-generic"), fallback);
|
|
|
|
|
|
|
|
const char * pic_types[] = {
|
|
|
|
"bmp", "gif", "jpg", "jpeg", "pcx", "png", "psd", "ras", "tga", "tiff" };
|
2015-04-18 14:41:06 +00:00
|
|
|
for (const char * t: pic_types)
|
|
|
|
suffixes[PICTURE] << QString::fromLatin1(t);
|
2013-09-14 22:45:04 +00:00
|
|
|
fileIcons[PICTURE] = QIcon::fromTheme (QString::fromLatin1("image-x-generic"), fallback);
|
|
|
|
|
|
|
|
const char * vid_types[] = {
|
|
|
|
"3gp", "asf", "avi", "mkv", "mov", "mpeg", "mpg", "mp4",
|
|
|
|
"ogm", "ogv", "qt", "rm", "wmv" };
|
2015-04-18 14:41:06 +00:00
|
|
|
for (const char * t: vid_types)
|
|
|
|
suffixes[VIDEO] << QString::fromLatin1(t);
|
2013-09-14 22:45:04 +00:00
|
|
|
fileIcons[VIDEO] = QIcon::fromTheme (QString::fromLatin1("video-x-generic"), fallback);
|
|
|
|
|
|
|
|
const char * arc_types[] = {
|
|
|
|
"7z", "ace", "bz2", "cbz", "gz", "gzip", "lzma", "rar", "sft", "tar", "zip" };
|
2015-04-18 14:41:06 +00:00
|
|
|
for (const char * t: arc_types)
|
|
|
|
suffixes[ARCHIVE] << QString::fromLatin1(t);
|
2013-09-14 22:45:04 +00:00
|
|
|
fileIcons[ARCHIVE] = QIcon::fromTheme (QString::fromLatin1("package-x-generic"), fallback);
|
|
|
|
|
|
|
|
const char * aud_types[] = {
|
|
|
|
"aac", "ac3", "aiff", "ape", "au", "flac", "m3u", "m4a", "mid", "midi", "mp2",
|
|
|
|
"mp3", "mpc", "nsf", "oga", "ogg", "ra", "ram", "shn", "voc", "wav", "wma" };
|
2015-04-18 14:41:06 +00:00
|
|
|
for (const char * t: aud_types)
|
|
|
|
suffixes[AUDIO] << QString::fromLatin1(t);
|
2013-09-14 22:45:04 +00:00
|
|
|
fileIcons[AUDIO] = QIcon::fromTheme (QString::fromLatin1("audio-x-generic"), fallback);
|
|
|
|
|
|
|
|
const char * exe_types[] = { "bat", "cmd", "com", "exe" };
|
2015-04-18 14:41:06 +00:00
|
|
|
for (const char * t: exe_types)
|
|
|
|
suffixes[APP] << QString::fromLatin1(t);
|
2013-09-14 22:45:04 +00:00
|
|
|
fileIcons[APP] = QIcon::fromTheme (QString::fromLatin1("application-x-executable"), fallback);
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2013-09-14 22:45:04 +00:00
|
|
|
QString suffix (QFileInfo (filename).suffix ().toLower ());
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2013-09-14 22:45:04 +00:00
|
|
|
for (int i=0; i<TYPE_COUNT; ++i)
|
|
|
|
if (suffixes[i].contains (suffix))
|
|
|
|
return fileIcons[i];
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2013-09-08 18:53:11 +00:00
|
|
|
#endif
|
2015-01-01 02:28:18 +00:00
|
|
|
|
|
|
|
return fallback;
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
2012-08-19 16:12:20 +00:00
|
|
|
|
|
|
|
bool
|
2014-12-12 23:05:10 +00:00
|
|
|
Utils::isValidUtf8 (const char * s)
|
2012-08-19 16:12:20 +00:00
|
|
|
{
|
2013-09-14 22:45:04 +00:00
|
|
|
int n; // number of bytes in a UTF-8 sequence
|
2012-08-19 16:12:20 +00:00
|
|
|
|
2013-09-14 22:45:04 +00:00
|
|
|
for (const char *c = s; *c; c += n)
|
2012-08-19 16:12:20 +00:00
|
|
|
{
|
2013-09-14 22:45:04 +00:00
|
|
|
if ((*c & 0x80) == 0x00) n = 1; // ASCII
|
|
|
|
else if ((*c & 0xc0) == 0x80) return false; // not valid
|
|
|
|
else if ((*c & 0xe0) == 0xc0) n = 2;
|
|
|
|
else if ((*c & 0xf0) == 0xe0) n = 3;
|
|
|
|
else if ((*c & 0xf8) == 0xf0) n = 4;
|
|
|
|
else if ((*c & 0xfc) == 0xf8) n = 5;
|
|
|
|
else if ((*c & 0xfe) == 0xfc) n = 6;
|
|
|
|
else return false;
|
|
|
|
for (int m = 1; m < n; m++)
|
|
|
|
if ((c[m] & 0xc0) != 0x80)
|
|
|
|
return false;
|
2015-01-02 11:15:31 +00:00
|
|
|
}
|
2013-09-14 22:45:04 +00:00
|
|
|
|
|
|
|
return true;
|
2012-08-19 16:12:20 +00:00
|
|
|
}
|
2013-09-08 19:59:47 +00:00
|
|
|
|
|
|
|
QString
|
2014-12-12 23:05:10 +00:00
|
|
|
Utils::removeTrailingDirSeparator (const QString& path)
|
2013-09-08 19:59:47 +00:00
|
|
|
{
|
2014-12-31 21:00:34 +00:00
|
|
|
const QFileInfo pathInfo (path);
|
|
|
|
return pathInfo.fileName ().isEmpty () ? pathInfo.absolutePath () : pathInfo.absoluteFilePath ();
|
2013-09-08 19:59:47 +00:00
|
|
|
}
|
2015-06-10 21:27:11 +00:00
|
|
|
|
|
|
|
QColor
|
|
|
|
Utils::getFadedColor (const QColor& color)
|
|
|
|
{
|
|
|
|
QColor fadedColor (color);
|
|
|
|
fadedColor.setAlpha (128);
|
|
|
|
return fadedColor;
|
|
|
|
}
|