2007-08-31 16:06:31 +04: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 .
*
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-08-31 16:06:31 +04:00
* Contact : chris @ qbittorrent . org
*/
2009-11-20 10:48:44 +03:00
# include "engineselectdlg.h"
# include "downloadthread.h"
2007-08-31 16:06:31 +04:00
# include "misc.h"
2009-03-31 01:38:56 +04:00
# include "ico.h"
2009-11-20 11:20:05 +03:00
# include "pluginsource.h"
2007-08-31 16:06:31 +04:00
# include <QProcess>
# include <QHeaderView>
# include <QSettings>
# include <QMenu>
# include <QMessageBox>
# include <QFileDialog>
2007-09-02 13:01:22 +04:00
# include <QDropEvent>
2007-09-11 23:31:21 +04:00
# include <QInputDialog>
2009-11-04 18:04:11 +03:00
# include <QTemporaryFile>
2007-08-31 16:06:31 +04:00
2009-11-17 20:20:25 +03:00
enum EngineColumns { ENGINE_NAME , ENGINE_URL , ENGINE_STATE , ENGINE_ID } ;
2007-08-31 16:06:31 +04:00
2009-08-25 06:31:36 +04:00
engineSelectDlg : : engineSelectDlg ( QWidget * parent , SupportedEngines * supported_engines ) : QDialog ( parent ) , supported_engines ( supported_engines ) {
2007-08-31 16:06:31 +04:00
setupUi ( this ) ;
setAttribute ( Qt : : WA_DeleteOnClose ) ;
pluginsTree - > header ( ) - > resizeSection ( 0 , 170 ) ;
pluginsTree - > header ( ) - > resizeSection ( 1 , 220 ) ;
2007-09-03 21:45:16 +04:00
pluginsTree - > hideColumn ( ENGINE_ID ) ;
2009-03-08 13:28:58 +03:00
actionEnable - > setIcon ( QIcon ( QString : : fromUtf8 ( " :/Icons/oxygen/button_ok.png " ) ) ) ;
actionDisable - > setIcon ( QIcon ( QString : : fromUtf8 ( " :/Icons/oxygen/button_cancel.png " ) ) ) ;
2009-08-21 11:40:57 +04:00
actionUninstall - > setIcon ( QIcon ( QString : : fromUtf8 ( " :/Icons/oxygen/list-remove.png " ) ) ) ;
2007-08-31 16:06:31 +04:00
connect ( actionEnable , SIGNAL ( triggered ( ) ) , this , SLOT ( enableSelection ( ) ) ) ;
connect ( actionDisable , SIGNAL ( triggered ( ) ) , this , SLOT ( disableSelection ( ) ) ) ;
connect ( pluginsTree , SIGNAL ( customContextMenuRequested ( const QPoint & ) ) , this , SLOT ( displayContextMenu ( const QPoint & ) ) ) ;
downloader = new downloadThread ( this ) ;
connect ( downloader , SIGNAL ( downloadFinished ( QString , QString ) ) , this , SLOT ( processDownloadedFile ( QString , QString ) ) ) ;
connect ( downloader , SIGNAL ( downloadFailure ( QString , QString ) ) , this , SLOT ( handleDownloadFailure ( QString , QString ) ) ) ;
2009-08-25 06:31:36 +04:00
loadSupportedSearchEngines ( ) ;
connect ( supported_engines , SIGNAL ( newSupportedEngine ( QString ) ) , this , SLOT ( addNewEngine ( QString ) ) ) ;
2007-08-31 16:06:31 +04:00
connect ( pluginsTree , SIGNAL ( itemDoubleClicked ( QTreeWidgetItem * , int ) ) , this , SLOT ( toggleEngineState ( QTreeWidgetItem * , int ) ) ) ;
show ( ) ;
}
engineSelectDlg : : ~ engineSelectDlg ( ) {
qDebug ( " Destroying engineSelectDlg " ) ;
emit enginesChanged ( ) ;
qDebug ( " Before deleting downloader " ) ;
delete downloader ;
qDebug ( " Engine plugins dialog destroyed " ) ;
}
2007-09-02 13:01:22 +04:00
void engineSelectDlg : : dropEvent ( QDropEvent * event ) {
event - > acceptProposedAction ( ) ;
QStringList files = event - > mimeData ( ) - > text ( ) . split ( QString : : fromUtf8 ( " \n " ) ) ;
QString file ;
foreach ( file , files ) {
2009-07-12 12:13:00 +04:00
qDebug ( " dropped %s " , file . toLocal8Bit ( ) . data ( ) ) ;
2007-09-02 13:01:22 +04:00
file = file . replace ( " file:// " , " " ) ;
2007-09-08 21:07:29 +04:00
if ( file . startsWith ( " http:// " , Qt : : CaseInsensitive ) | | file . startsWith ( " https:// " , Qt : : CaseInsensitive ) | | file . startsWith ( " ftp:// " , Qt : : CaseInsensitive ) ) {
2007-09-02 13:01:22 +04:00
downloader - > downloadUrl ( file ) ;
continue ;
}
2007-09-08 21:07:29 +04:00
if ( file . endsWith ( " .py " , Qt : : CaseInsensitive ) ) {
2007-09-04 02:05:40 +04:00
QString plugin_name = file . split ( QDir : : separator ( ) ) . last ( ) ;
plugin_name . replace ( " .py " , " " ) ;
installPlugin ( file , plugin_name ) ;
}
2007-09-02 13:01:22 +04:00
}
}
// Decode if we accept drag 'n drop or not
void engineSelectDlg : : dragEnterEvent ( QDragEnterEvent * event ) {
QString mime ;
foreach ( mime , event - > mimeData ( ) - > formats ( ) ) {
2009-07-12 12:13:00 +04:00
qDebug ( " mimeData: %s " , mime . toLocal8Bit ( ) . data ( ) ) ;
2007-09-02 13:01:22 +04:00
}
2007-09-02 17:06:42 +04:00
if ( event - > mimeData ( ) - > hasFormat ( QString : : fromUtf8 ( " text/plain " ) ) | | event - > mimeData ( ) - > hasFormat ( QString : : fromUtf8 ( " text/uri-list " ) ) ) {
2007-09-02 13:01:22 +04:00
event - > acceptProposedAction ( ) ;
}
}
2007-08-31 16:06:31 +04:00
void engineSelectDlg : : on_updateButton_clicked ( ) {
// Download version file from primary server
2009-03-31 01:38:56 +04:00
downloader - > downloadUrl ( " http://www.dchris.eu/search_engine2/versions.txt " ) ;
2007-08-31 16:06:31 +04:00
}
void engineSelectDlg : : toggleEngineState ( QTreeWidgetItem * item , int ) {
2009-08-25 06:31:36 +04:00
SupportedEngine * engine = supported_engines - > value ( item - > text ( ENGINE_ID ) ) ;
engine - > setEnabled ( ! engine - > isEnabled ( ) ) ;
if ( engine - > isEnabled ( ) ) {
item - > setText ( ENGINE_STATE , tr ( " Yes " ) ) ;
setRowColor ( pluginsTree - > indexOfTopLevelItem ( item ) , " green " ) ;
} else {
item - > setText ( ENGINE_STATE , tr ( " No " ) ) ;
setRowColor ( pluginsTree - > indexOfTopLevelItem ( item ) , " red " ) ;
2007-08-31 16:06:31 +04:00
}
}
2009-08-25 06:31:36 +04:00
void engineSelectDlg : : displayContextMenu ( const QPoint & ) {
2007-08-31 16:06:31 +04:00
QMenu myContextMenu ( this ) ;
QModelIndex index ;
// Enable/disable pause/start action given the DL state
QList < QTreeWidgetItem * > items = pluginsTree - > selectedItems ( ) ;
bool has_enable = false , has_disable = false ;
QTreeWidgetItem * item ;
foreach ( item , items ) {
2007-09-03 21:45:16 +04:00
QString id = item - > text ( ENGINE_ID ) ;
2009-08-25 06:31:36 +04:00
if ( supported_engines - > value ( id ) - > isEnabled ( ) and ! has_disable ) {
2007-08-31 16:06:31 +04:00
myContextMenu . addAction ( actionDisable ) ;
has_disable = true ;
}
2009-08-25 06:31:36 +04:00
if ( ! supported_engines - > value ( id ) - > isEnabled ( ) and ! has_enable ) {
2007-08-31 16:06:31 +04:00
myContextMenu . addAction ( actionEnable ) ;
has_enable = true ;
}
if ( has_enable & & has_disable ) break ;
}
myContextMenu . addSeparator ( ) ;
myContextMenu . addAction ( actionUninstall ) ;
2009-08-25 06:31:36 +04:00
myContextMenu . exec ( QCursor : : pos ( ) ) ;
2007-08-31 16:06:31 +04:00
}
void engineSelectDlg : : on_closeButton_clicked ( ) {
close ( ) ;
}
void engineSelectDlg : : on_actionUninstall_triggered ( ) {
QList < QTreeWidgetItem * > items = pluginsTree - > selectedItems ( ) ;
QTreeWidgetItem * item ;
bool change = false ;
bool error = false ;
foreach ( item , items ) {
int index = pluginsTree - > indexOfTopLevelItem ( item ) ;
Q_ASSERT ( index ! = - 1 ) ;
2007-09-03 21:45:16 +04:00
QString id = item - > text ( ENGINE_ID ) ;
if ( QFile : : exists ( " :/search_engine/engines/ " + id + " .py " ) ) {
2007-08-31 16:06:31 +04:00
error = true ;
// Disable it instead
2009-08-25 06:31:36 +04:00
supported_engines - > value ( id ) - > setEnabled ( false ) ;
item - > setText ( ENGINE_STATE , tr ( " No " ) ) ;
2007-08-31 16:06:31 +04:00
setRowColor ( index , " red " ) ;
continue ;
} else {
// Proceed with uninstall
// remove it from hard drive
2007-09-03 21:45:16 +04:00
QDir enginesFolder ( misc : : qBittorrentPath ( ) + " search_engine " + QDir : : separator ( ) + " engines " ) ;
QStringList filters ;
filters < < id + " .* " ;
QStringList files = enginesFolder . entryList ( filters , QDir : : Files , QDir : : Unsorted ) ;
QString file ;
foreach ( file , files ) {
enginesFolder . remove ( file ) ;
2007-09-02 11:58:25 +04:00
}
2009-08-25 06:31:36 +04:00
// Remove it from supported engines
delete supported_engines - > take ( id ) ;
2007-09-03 21:45:16 +04:00
delete item ;
2007-08-31 16:06:31 +04:00
change = true ;
}
}
if ( error )
QMessageBox : : warning ( 0 , tr ( " Uninstall warning " ) , tr ( " Some plugins could not be uninstalled because they are included in qBittorrent. \n Only the ones you added yourself can be uninstalled. \n However, those plugins were disabled. " ) ) ;
else
2007-09-01 14:23:39 +04:00
QMessageBox : : information ( 0 , tr ( " Uninstall success " ) , tr ( " All selected plugins were uninstalled successfully " ) ) ;
2007-08-31 16:06:31 +04:00
}
void engineSelectDlg : : enableSelection ( ) {
QList < QTreeWidgetItem * > items = pluginsTree - > selectedItems ( ) ;
QTreeWidgetItem * item ;
foreach ( item , items ) {
int index = pluginsTree - > indexOfTopLevelItem ( item ) ;
Q_ASSERT ( index ! = - 1 ) ;
2007-09-03 21:45:16 +04:00
QString id = item - > text ( ENGINE_ID ) ;
2009-08-25 06:31:36 +04:00
supported_engines - > value ( id ) - > setEnabled ( true ) ;
item - > setText ( ENGINE_STATE , tr ( " Yes " ) ) ;
2007-08-31 16:06:31 +04:00
setRowColor ( index , " green " ) ;
}
}
void engineSelectDlg : : disableSelection ( ) {
QList < QTreeWidgetItem * > items = pluginsTree - > selectedItems ( ) ;
QTreeWidgetItem * item ;
foreach ( item , items ) {
int index = pluginsTree - > indexOfTopLevelItem ( item ) ;
Q_ASSERT ( index ! = - 1 ) ;
2007-09-03 21:45:16 +04:00
QString id = item - > text ( ENGINE_ID ) ;
2009-08-25 06:31:36 +04:00
supported_engines - > value ( id ) - > setEnabled ( false ) ;
item - > setText ( ENGINE_STATE , tr ( " No " ) ) ;
2007-08-31 16:06:31 +04:00
setRowColor ( index , " red " ) ;
}
}
// Set the color of a row in data model
void engineSelectDlg : : setRowColor ( int row , QString color ) {
QTreeWidgetItem * item = pluginsTree - > topLevelItem ( row ) ;
2009-08-25 06:31:36 +04:00
for ( int i = 0 ; i < pluginsTree - > columnCount ( ) ; + + i ) {
2007-08-31 16:06:31 +04:00
item - > setData ( i , Qt : : ForegroundRole , QVariant ( QColor ( color ) ) ) ;
}
}
QList < QTreeWidgetItem * > engineSelectDlg : : findItemsWithUrl ( QString url ) {
QList < QTreeWidgetItem * > res ;
for ( int i = 0 ; i < pluginsTree - > topLevelItemCount ( ) ; + + i ) {
QTreeWidgetItem * item = pluginsTree - > topLevelItem ( i ) ;
2007-09-08 21:07:29 +04:00
if ( url . startsWith ( item - > text ( ENGINE_URL ) , Qt : : CaseInsensitive ) )
2007-08-31 16:06:31 +04:00
res < < item ;
}
return res ;
}
2007-09-04 02:05:40 +04:00
QTreeWidgetItem * engineSelectDlg : : findItemWithID ( QString id ) {
QList < QTreeWidgetItem * > res ;
for ( int i = 0 ; i < pluginsTree - > topLevelItemCount ( ) ; + + i ) {
QTreeWidgetItem * item = pluginsTree - > topLevelItem ( i ) ;
if ( id = = item - > text ( ENGINE_ID ) )
return item ;
}
return 0 ;
}
2007-09-01 14:23:39 +04:00
bool engineSelectDlg : : isUpdateNeeded ( QString plugin_name , float new_version ) const {
2007-08-31 16:06:31 +04:00
float old_version = misc : : getPluginVersion ( misc : : qBittorrentPath ( ) + " search_engine " + QDir : : separator ( ) + " engines " + QDir : : separator ( ) + plugin_name + " .py " ) ;
2007-09-04 02:05:40 +04:00
qDebug ( " IsUpdate needed? tobeinstalled: %.2f, alreadyinstalled: %.2f " , new_version , old_version ) ;
2007-08-31 16:06:31 +04:00
return ( new_version > old_version ) ;
}
2007-09-04 02:05:40 +04:00
void engineSelectDlg : : installPlugin ( QString path , QString plugin_name ) {
2009-07-12 12:13:00 +04:00
qDebug ( " Asked to install plugin at %s " , path . toLocal8Bit ( ) . data ( ) ) ;
2007-09-02 13:01:22 +04:00
float new_version = misc : : getPluginVersion ( path ) ;
2007-09-04 02:05:40 +04:00
qDebug ( " Version to be installed: %.2f " , new_version ) ;
2007-09-02 13:01:22 +04:00
if ( ! isUpdateNeeded ( plugin_name , new_version ) ) {
2007-09-04 02:05:40 +04:00
qDebug ( " Apparently update it not needed, we have a more recent version " ) ;
2009-07-12 12:13:00 +04:00
QMessageBox : : information ( this , tr ( " Search plugin install " ) + " -- " + tr ( " qBittorrent " ) , tr ( " A more recent version of %1 search engine plugin is already installed. " , " %1 is the name of the search engine " ) . arg ( plugin_name . toLocal8Bit ( ) . data ( ) ) ) ;
2007-09-02 13:01:22 +04:00
return ;
}
2009-08-25 06:31:36 +04:00
// Process with install
2007-09-02 13:01:22 +04:00
QString dest_path = misc : : qBittorrentPath ( ) + " search_engine " + QDir : : separator ( ) + " engines " + QDir : : separator ( ) + plugin_name + " .py " ;
bool update = false ;
if ( QFile : : exists ( dest_path ) ) {
2009-08-25 06:31:36 +04:00
// Backup in case install fails
2007-09-02 13:01:22 +04:00
QFile : : copy ( dest_path , dest_path + " .bak " ) ;
QFile : : remove ( dest_path ) ;
update = true ;
}
2009-08-25 06:31:36 +04:00
// Copy the plugin
2007-09-02 13:01:22 +04:00
QFile : : copy ( path , dest_path ) ;
2009-08-25 06:31:36 +04:00
// Update supported plugins
supported_engines - > update ( ) ;
// Check if this was correctly installed
if ( ! supported_engines - > contains ( plugin_name ) ) {
2007-09-02 13:01:22 +04:00
if ( update ) {
2009-08-25 06:31:36 +04:00
// Remove broken file
2007-09-02 13:01:22 +04:00
QFile : : remove ( dest_path ) ;
2009-08-25 06:31:36 +04:00
// restore backup
2007-09-02 13:01:22 +04:00
QFile : : copy ( dest_path + " .bak " , dest_path ) ;
QFile : : remove ( dest_path + " .bak " ) ;
2009-07-12 12:13:00 +04:00
QMessageBox : : warning ( this , tr ( " Search plugin install " ) + " -- " + tr ( " qBittorrent " ) , tr ( " %1 search engine plugin could not be updated, keeping old version. " , " %1 is the name of the search engine " ) . arg ( plugin_name . toLocal8Bit ( ) . data ( ) ) ) ;
2007-09-02 13:01:22 +04:00
return ;
} else {
2009-08-25 06:31:36 +04:00
// Remove broken file
2007-09-02 13:01:22 +04:00
QFile : : remove ( dest_path ) ;
2009-07-12 12:13:00 +04:00
QMessageBox : : warning ( this , tr ( " Search plugin install " ) + " -- " + tr ( " qBittorrent " ) , tr ( " %1 search engine plugin could not be installed. " , " %1 is the name of the search engine " ) . arg ( plugin_name . toLocal8Bit ( ) . data ( ) ) ) ;
2007-09-02 13:01:22 +04:00
return ;
2007-09-01 14:23:39 +04:00
}
2007-09-02 13:01:22 +04:00
}
2009-08-25 06:31:36 +04:00
// Install was successful, remove backup
2007-09-02 13:01:22 +04:00
if ( update ) {
QFile : : remove ( dest_path + " .bak " ) ;
}
if ( update ) {
2009-07-12 12:13:00 +04:00
QMessageBox : : information ( this , tr ( " Search plugin install " ) + " -- " + tr ( " qBittorrent " ) , tr ( " %1 search engine plugin was successfully updated. " , " %1 is the name of the search engine " ) . arg ( plugin_name . toLocal8Bit ( ) . data ( ) ) ) ;
2007-09-02 13:01:22 +04:00
return ;
} else {
2009-07-12 12:13:00 +04:00
QMessageBox : : information ( this , tr ( " Search plugin install " ) + " -- " + tr ( " qBittorrent " ) , tr ( " %1 search engine plugin was successfully installed. " , " %1 is the name of the search engine " ) . arg ( plugin_name . toLocal8Bit ( ) . data ( ) ) ) ;
2007-09-02 13:01:22 +04:00
return ;
}
}
2009-08-25 06:31:36 +04:00
void engineSelectDlg : : loadSupportedSearchEngines ( ) {
// Some clean up first
pluginsTree - > clear ( ) ;
foreach ( QString name , supported_engines - > keys ( ) ) {
addNewEngine ( name ) ;
}
2007-09-11 23:31:21 +04:00
}
2009-08-25 06:31:36 +04:00
void engineSelectDlg : : addNewEngine ( QString engine_name ) {
QTreeWidgetItem * item = new QTreeWidgetItem ( pluginsTree ) ;
SupportedEngine * engine = supported_engines - > value ( engine_name ) ;
item - > setText ( ENGINE_NAME , engine - > getFullName ( ) ) ;
item - > setText ( ENGINE_URL , engine - > getUrl ( ) ) ;
item - > setText ( ENGINE_ID , engine - > getName ( ) ) ;
if ( engine - > isEnabled ( ) ) {
item - > setText ( ENGINE_STATE , tr ( " Yes " ) ) ;
setRowColor ( pluginsTree - > indexOfTopLevelItem ( item ) , " green " ) ;
} else {
item - > setText ( ENGINE_STATE , tr ( " No " ) ) ;
setRowColor ( pluginsTree - > indexOfTopLevelItem ( item ) , " red " ) ;
}
// Handle icon
QString iconPath = misc : : qBittorrentPath ( ) + " search_engine " + QDir : : separator ( ) + " engines " + QDir : : separator ( ) + engine - > getName ( ) + " .png " ;
if ( QFile : : exists ( iconPath ) ) {
// Good, we already have the icon
item - > setData ( ENGINE_NAME , Qt : : DecorationRole , QVariant ( QIcon ( iconPath ) ) ) ;
} else {
iconPath = misc : : qBittorrentPath ( ) + " search_engine " + QDir : : separator ( ) + " engines " + QDir : : separator ( ) + engine - > getName ( ) + " .ico " ;
if ( QFile : : exists ( iconPath ) ) { // ICO support
item - > setData ( ENGINE_NAME , Qt : : DecorationRole , QVariant ( QIcon ( iconPath ) ) ) ;
} else {
// Icon is missing, we must download it
downloader - > downloadUrl ( engine - > getUrl ( ) + " /favicon.ico " ) ;
}
}
2007-09-11 23:31:21 +04:00
}
2009-08-25 06:31:36 +04:00
void engineSelectDlg : : on_installButton_clicked ( ) {
pluginSourceDlg * dlg = new pluginSourceDlg ( this ) ;
connect ( dlg , SIGNAL ( askForLocalFile ( ) ) , this , SLOT ( askForLocalPlugin ( ) ) ) ;
connect ( dlg , SIGNAL ( askForUrl ( ) ) , this , SLOT ( askForPluginUrl ( ) ) ) ;
}
void engineSelectDlg : : askForPluginUrl ( ) {
bool ok ;
QString url = QInputDialog : : getText ( this , tr ( " New search engine plugin URL " ) ,
tr ( " URL: " ) , QLineEdit : : Normal ,
" http:// " , & ok ) ;
if ( ok & & ! url . isEmpty ( ) )
downloader - > downloadUrl ( url ) ;
}
void engineSelectDlg : : askForLocalPlugin ( ) {
QStringList pathsList = QFileDialog : : getOpenFileNames ( 0 ,
tr ( " Select search plugins " ) , QDir : : homePath ( ) ,
tr ( " qBittorrent search plugins " ) + QString : : fromUtf8 ( " (*.py) " ) ) ;
QString path ;
foreach ( path , pathsList ) {
if ( path . endsWith ( " .py " , Qt : : CaseInsensitive ) ) {
QString plugin_name = path . split ( QDir : : separator ( ) ) . last ( ) ;
plugin_name . replace ( " .py " , " " , Qt : : CaseInsensitive ) ;
installPlugin ( path , plugin_name ) ;
}
}
2007-08-31 16:06:31 +04:00
}
2009-08-25 06:31:36 +04:00
bool engineSelectDlg : : parseVersionsFile ( QString versions_file , QString updateServer ) {
qDebug ( " Checking if update is needed " ) ;
bool file_correct = false ;
QFile versions ( versions_file ) ;
if ( ! versions . open ( QIODevice : : ReadOnly | QIODevice : : Text ) ) {
qDebug ( " * Error: Could not read versions.txt file " ) ;
return false ;
2007-08-31 16:06:31 +04:00
}
2009-08-25 06:31:36 +04:00
bool updated = false ;
while ( ! versions . atEnd ( ) ) {
QByteArray line = versions . readLine ( ) ;
line . replace ( " \n " , " " ) ;
line = line . trimmed ( ) ;
if ( line . isEmpty ( ) ) continue ;
if ( line . startsWith ( " # " ) ) continue ;
QList < QByteArray > list = line . split ( ' ' ) ;
if ( list . size ( ) ! = 2 ) continue ;
QString plugin_name = QString ( list . first ( ) ) ;
if ( ! plugin_name . endsWith ( " : " ) ) continue ;
plugin_name . chop ( 1 ) ; // remove trailing ':'
bool ok ;
float version = list . last ( ) . toFloat ( & ok ) ;
qDebug ( " read line %s: %.2f " , plugin_name . toLocal8Bit ( ) . data ( ) , version ) ;
if ( ! ok ) continue ;
file_correct = true ;
if ( isUpdateNeeded ( plugin_name , version ) ) {
qDebug ( " Plugin: %s is outdated " , plugin_name . toLocal8Bit ( ) . data ( ) ) ;
// Downloading update
downloader - > downloadUrl ( updateServer + plugin_name + " .pyqBT " ) ; // Actually this is really a .py
downloader - > downloadUrl ( updateServer + plugin_name + " .png " ) ;
updated = true ;
} else {
qDebug ( " Plugin: %s is up to date " , plugin_name . toLocal8Bit ( ) . data ( ) ) ;
}
}
// Close file
versions . close ( ) ;
// Clean up tmp file
QFile : : remove ( versions_file ) ;
if ( file_correct & & ! updated ) {
QMessageBox : : information ( this , tr ( " Search plugin update " ) + " -- " + tr ( " qBittorrent " ) , tr ( " All your plugins are already up to date. " ) ) ;
}
return file_correct ;
2007-08-31 16:06:31 +04:00
}
2009-08-25 06:31:36 +04:00
void engineSelectDlg : : processDownloadedFile ( QString url , QString filePath ) {
qDebug ( " engineSelectDlg received %s " , url . toLocal8Bit ( ) . data ( ) ) ;
if ( url . endsWith ( " favicon.ico " , Qt : : CaseInsensitive ) ) {
// Icon downloaded
QImage fileIcon ;
if ( fileIcon . load ( filePath ) ) {
QList < QTreeWidgetItem * > items = findItemsWithUrl ( url ) ;
QTreeWidgetItem * item ;
foreach ( item , items ) {
QString id = item - > text ( ENGINE_ID ) ;
QString iconPath ;
QFile icon ( filePath ) ;
icon . open ( QIODevice : : ReadOnly ) ;
if ( ICOHandler : : canRead ( & icon ) )
iconPath = misc : : qBittorrentPath ( ) + " search_engine " + QDir : : separator ( ) + " engines " + QDir : : separator ( ) + id + " .ico " ;
else
iconPath = misc : : qBittorrentPath ( ) + " search_engine " + QDir : : separator ( ) + " engines " + QDir : : separator ( ) + id + " .png " ;
QFile : : copy ( filePath , iconPath ) ;
item - > setData ( ENGINE_NAME , Qt : : DecorationRole , QVariant ( QIcon ( iconPath ) ) ) ;
}
2007-08-31 16:06:31 +04:00
}
2009-08-25 06:31:36 +04:00
// Delete tmp file
QFile : : remove ( filePath ) ;
return ;
2007-08-31 16:06:31 +04:00
}
2009-08-25 06:31:36 +04:00
if ( url = = " http://www.dchris.eu/search_engine2/versions.txt " ) {
if ( ! parseVersionsFile ( filePath , " http://www.dchris.eu/search_engine2/ " ) ) {
qDebug ( " Primary update server failed, try secondary " ) ;
downloader - > downloadUrl ( " http://hydr0g3n.free.fr/search_engine2/versions.txt " ) ;
}
QFile : : remove ( filePath ) ;
return ;
}
if ( url = = " http://hydr0g3n.free.fr/search_engine2/versions.txt " ) {
if ( ! parseVersionsFile ( filePath , " http://hydr0g3n.free.fr/search_engine2/ " ) ) {
QMessageBox : : warning ( this , tr ( " Search plugin update " ) + " -- " + tr ( " qBittorrent " ) , tr ( " Sorry, update server is temporarily unavailable. " ) ) ;
}
QFile : : remove ( filePath ) ;
return ;
}
if ( url . endsWith ( " .pyqBT " , Qt : : CaseInsensitive ) | | url . endsWith ( " .py " , Qt : : CaseInsensitive ) ) {
QString plugin_name = url . split ( ' / ' ) . last ( ) ;
plugin_name . replace ( " .pyqBT " , " " ) ;
plugin_name . replace ( " .py " , " " ) ;
installPlugin ( filePath , plugin_name ) ;
QFile : : remove ( filePath ) ;
return ;
}
2007-08-31 16:06:31 +04:00
}
2009-08-25 06:31:36 +04:00
void engineSelectDlg : : handleDownloadFailure ( QString url , QString reason ) {
if ( url . endsWith ( " favicon.ico " , Qt : : CaseInsensitive ) ) {
qDebug ( " Could not download favicon: %s, reason: %s " , url . toLocal8Bit ( ) . data ( ) , reason . toLocal8Bit ( ) . data ( ) ) ;
return ;
}
if ( url = = " http://www.dchris.eu/search_engine2/versions.txt " ) {
// Primary update server failed, try secondary
2007-08-31 18:08:47 +04:00
qDebug ( " Primary update server failed, try secondary " ) ;
2009-03-31 01:38:56 +04:00
downloader - > downloadUrl ( " http://hydr0g3n.free.fr/search_engine2/versions.txt " ) ;
2009-08-25 06:31:36 +04:00
return ;
2007-08-31 16:06:31 +04:00
}
2009-08-25 06:31:36 +04:00
if ( url = = " http://hydr0g3n.free.fr/search_engine2/versions.txt " ) {
2007-08-31 17:35:55 +04:00
QMessageBox : : warning ( this , tr ( " Search plugin update " ) + " -- " + tr ( " qBittorrent " ) , tr ( " Sorry, update server is temporarily unavailable. " ) ) ;
2009-08-25 06:31:36 +04:00
return ;
}
if ( url . endsWith ( " .pyqBT " , Qt : : CaseInsensitive ) | | url . endsWith ( " .py " , Qt : : CaseInsensitive ) ) {
// a plugin update download has been failed
QString plugin_name = url . split ( ' / ' ) . last ( ) ;
plugin_name . replace ( " .pyqBT " , " " , Qt : : CaseInsensitive ) ;
plugin_name . replace ( " .py " , " " , Qt : : CaseInsensitive ) ;
QMessageBox : : warning ( this , tr ( " Search plugin update " ) + " -- " + tr ( " qBittorrent " ) , tr ( " Sorry, %1 search plugin install failed. " , " %1 is the name of the search engine " ) . arg ( plugin_name . toLocal8Bit ( ) . data ( ) ) ) ;
2007-08-31 16:06:31 +04:00
}
2007-09-04 02:05:40 +04:00
}