2009-04-09 18:55:47 +00:00
|
|
|
/*
|
2010-08-01 20:36:13 +00:00
|
|
|
* This file Copyright (C) Mnemosyne LLC
|
2009-04-09 18:55:47 +00:00
|
|
|
*
|
2010-08-01 20:36:13 +00:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
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
|
|
|
*/
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
#include <QApplication>
|
|
|
|
#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>
|
|
|
|
#include <QSet>
|
|
|
|
#include <QStyle>
|
|
|
|
|
|
|
|
#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
|
|
|
|
|
|
|
#include "utils.h"
|
|
|
|
|
2010-07-03 00:25:22 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2010-06-04 01:00:27 +00:00
|
|
|
QString
|
|
|
|
Utils :: remoteFileChooser( QWidget * parent, const QString& title, const QString& myPath, bool dir, bool local )
|
|
|
|
{
|
|
|
|
QString path;
|
|
|
|
|
|
|
|
if( local )
|
|
|
|
{
|
|
|
|
if( dir )
|
|
|
|
path = QFileDialog::getExistingDirectory( parent, title, myPath );
|
|
|
|
else
|
|
|
|
path = QFileDialog::getOpenFileName( parent, title, myPath );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
path = QInputDialog::getText( parent, title, tr( "Enter a location:" ), QLineEdit::Normal, myPath, NULL );
|
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2009-04-09 18:55:47 +00:00
|
|
|
void
|
|
|
|
Utils :: toStderr( const QString& str )
|
|
|
|
{
|
2009-08-08 13:22:38 +00:00
|
|
|
std::cerr << qPrintable(str) << std::endl;
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const QIcon&
|
|
|
|
Utils :: guessMimeIcon( const QString& filename )
|
|
|
|
{
|
|
|
|
enum { DISK, DOCUMENT, PICTURE, VIDEO, ARCHIVE, AUDIO, APP, TYPE_COUNT };
|
|
|
|
static QIcon fallback;
|
|
|
|
static QIcon fileIcons[TYPE_COUNT];
|
|
|
|
static QSet<QString> suffixes[TYPE_COUNT];
|
|
|
|
|
|
|
|
if( fileIcons[0].isNull( ) )
|
|
|
|
{
|
|
|
|
fallback = QApplication::style()->standardIcon( QStyle :: SP_FileIcon );
|
|
|
|
|
2012-12-31 22:43:29 +00:00
|
|
|
suffixes[DISK] << QString::fromLatin1("iso");
|
|
|
|
fileIcons[DISK]= QIcon::fromTheme( QString::fromLatin1("media-optical"), fallback );
|
2011-08-20 05:19:27 +00:00
|
|
|
|
|
|
|
const char * doc_types[] = {
|
|
|
|
"abw", "csv", "doc", "dvi", "htm", "html", "ini", "log", "odp",
|
|
|
|
"ods", "odt", "pdf", "ppt", "ps", "rtf", "tex", "txt", "xml" };
|
|
|
|
for( int i=0, n=sizeof(doc_types)/sizeof(doc_types[0]); i<n; ++i )
|
2012-12-31 22:43:29 +00:00
|
|
|
suffixes[DOCUMENT] << QString::fromLatin1(doc_types[i] );
|
|
|
|
fileIcons[DOCUMENT] = QIcon::fromTheme( QString::fromLatin1("text-x-generic"), fallback );
|
2011-08-20 05:19:27 +00:00
|
|
|
|
|
|
|
const char * pic_types[] = {
|
|
|
|
"bmp", "gif", "jpg", "jpeg", "pcx", "png", "psd", "ras", "tga", "tiff" };
|
|
|
|
for( int i=0, n=sizeof(pic_types)/sizeof(pic_types[0]); i<n; ++i )
|
2012-12-31 22:43:29 +00:00
|
|
|
suffixes[PICTURE] << QString::fromLatin1(pic_types[i]);
|
|
|
|
fileIcons[PICTURE] = QIcon::fromTheme( QString::fromLatin1("image-x-generic"), fallback );
|
2011-08-20 05:19:27 +00:00
|
|
|
|
|
|
|
const char * vid_types[] = {
|
2012-12-31 22:43:29 +00:00
|
|
|
"3gp", "asf", "avi", "mkv", "mov", "mpeg", "mpg", "mp4",
|
2011-08-20 05:19:27 +00:00
|
|
|
"ogm", "ogv", "qt", "rm", "wmv" };
|
|
|
|
for( int i=0, n=sizeof(vid_types)/sizeof(vid_types[0]); i<n; ++i )
|
2012-12-31 22:43:29 +00:00
|
|
|
suffixes[VIDEO] << QString::fromLatin1(vid_types[i]);
|
|
|
|
fileIcons[VIDEO] = QIcon::fromTheme( QString::fromLatin1("video-x-generic"), fallback );
|
2011-08-20 05:19:27 +00:00
|
|
|
|
|
|
|
const char * arc_types[] = {
|
|
|
|
"7z", "ace", "bz2", "cbz", "gz", "gzip", "lzma", "rar", "sft", "tar", "zip" };
|
|
|
|
for( int i=0, n=sizeof(arc_types)/sizeof(arc_types[0]); i<n; ++i )
|
2012-12-31 22:43:29 +00:00
|
|
|
suffixes[ARCHIVE] << QString::fromLatin1(arc_types[i]);
|
|
|
|
fileIcons[ARCHIVE] = QIcon::fromTheme( QString::fromLatin1("package-x-generic"), fallback );
|
2011-08-20 05:19:27 +00:00
|
|
|
|
|
|
|
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" };
|
|
|
|
for( int i=0, n=sizeof(aud_types)/sizeof(aud_types[0]); i<n; ++i )
|
2012-12-31 22:43:29 +00:00
|
|
|
suffixes[AUDIO] << QString::fromLatin1(aud_types[i]);
|
|
|
|
fileIcons[AUDIO] = QIcon::fromTheme( QString::fromLatin1("audio-x-generic"), fallback );
|
2011-08-20 05:19:27 +00:00
|
|
|
|
|
|
|
const char * exe_types[] = { "bat", "cmd", "com", "exe" };
|
|
|
|
for( int i=0, n=sizeof(exe_types)/sizeof(exe_types[0]); i<n; ++i )
|
2012-12-31 22:43:29 +00:00
|
|
|
suffixes[APP] << QString::fromLatin1(exe_types[i]);
|
|
|
|
fileIcons[APP] = QIcon::fromTheme( QString::fromLatin1("application-x-executable"), fallback );
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QString suffix( QFileInfo( filename ).suffix( ).toLower( ) );
|
|
|
|
|
|
|
|
for( int i=0; i<TYPE_COUNT; ++i )
|
|
|
|
if( suffixes[i].contains( suffix ) )
|
|
|
|
return fileIcons[i];
|
|
|
|
|
|
|
|
return fallback;
|
|
|
|
}
|
2012-08-19 16:12:20 +00:00
|
|
|
|
|
|
|
bool
|
|
|
|
Utils :: isValidUtf8 ( const char *s )
|
|
|
|
{
|
|
|
|
int n; // number of bytes in a UTF-8 sequence
|
|
|
|
|
|
|
|
for ( const char *c = s; *c; c += n )
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|