2009-08-11 17:59:40 -04:00
|
|
|
/****************************************************************
|
|
|
|
* RetroShare is distributed under the following license:
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006 - 2009 RetroShare Team
|
|
|
|
*
|
|
|
|
* 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-02-05 10:29:43 -05:00
|
|
|
#include <QFile>
|
|
|
|
#include <QDir>
|
|
|
|
#include <QRegExp>
|
|
|
|
|
|
|
|
#include <QDebug>
|
|
|
|
|
|
|
|
#include <QWidget> //a strange thing: it compiles without this header, but
|
|
|
|
//then segfaults in some place
|
|
|
|
|
2009-02-09 16:01:41 -05:00
|
|
|
#include <QApplication>
|
|
|
|
|
2009-02-05 10:29:43 -05:00
|
|
|
#include "PluginManager.h"
|
2009-02-09 16:01:41 -05:00
|
|
|
#include "PluginManagerWidget.h"
|
2009-02-05 10:29:43 -05:00
|
|
|
#include "plugins/PluginInterface.h"
|
2009-02-09 16:01:41 -05:00
|
|
|
#include "rshare.h"
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
//=============================================================================
|
|
|
|
//=============================================================================
|
2009-02-05 10:29:43 -05:00
|
|
|
|
|
|
|
PluginManager::PluginManager()
|
|
|
|
{
|
2009-02-09 16:01:41 -05:00
|
|
|
baseFolder = //qApp->applicationDirPath()+"///plugins" ;
|
|
|
|
Rshare::dataDirectory() + "/plugins" ;
|
|
|
|
lastError = "No error.";
|
|
|
|
|
|
|
|
viewWidget = 0;
|
2009-02-05 10:29:43 -05:00
|
|
|
|
2009-02-09 16:01:41 -05:00
|
|
|
// defaultLoad();
|
2009-02-05 10:29:43 -05:00
|
|
|
}
|
2009-02-09 16:01:41 -05:00
|
|
|
|
|
|
|
//=============================================================================
|
2009-02-05 10:29:43 -05:00
|
|
|
|
|
|
|
void
|
2009-02-09 16:01:41 -05:00
|
|
|
PluginManager::defaultLoad( )
|
|
|
|
{
|
|
|
|
qDebug() << " " << "Default load started" ;
|
|
|
|
|
|
|
|
QDir workDir(baseFolder);
|
2009-02-05 10:29:43 -05:00
|
|
|
|
2009-02-09 16:01:41 -05:00
|
|
|
if ( !workDir.exists() )
|
|
|
|
{
|
2014-07-10 14:37:21 -04:00
|
|
|
QString em= tr("base folder %1 doesn't exist, default load failed")
|
2009-02-09 16:01:41 -05:00
|
|
|
.arg( baseFolder );
|
|
|
|
emit errorAppeared( em );
|
|
|
|
return ;
|
2009-02-05 10:29:43 -05:00
|
|
|
}
|
2009-02-09 16:01:41 -05:00
|
|
|
|
2009-02-05 10:29:43 -05:00
|
|
|
//=== get current available plugins =====
|
|
|
|
QStringList currAvailable = workDir.entryList(QDir::Files);
|
2016-01-02 08:19:53 -05:00
|
|
|
#if defined(Q_OS_WIN)
|
2009-02-05 10:29:43 -05:00
|
|
|
QRegExp trx("*.dll") ;
|
2016-01-02 08:19:53 -05:00
|
|
|
#elif defined(__MACH__)
|
|
|
|
QRegExp trx("*.dylib");
|
2009-02-05 10:29:43 -05:00
|
|
|
#else
|
|
|
|
QRegExp trx("*.so");
|
|
|
|
#endif
|
|
|
|
trx.setPatternSyntax(QRegExp::Wildcard );
|
|
|
|
|
|
|
|
currAvailable.filter( trx );
|
|
|
|
|
|
|
|
qDebug() << " " << "can load this plugins: " << currAvailable ;
|
|
|
|
|
|
|
|
//===
|
|
|
|
foreach(QString pluginFileName, currAvailable)
|
|
|
|
{
|
|
|
|
QString fullfn( workDir.absoluteFilePath( pluginFileName ) );
|
2009-02-09 16:01:41 -05:00
|
|
|
QString newName;
|
|
|
|
int ti = readPluginInformation( fullfn, newName);
|
|
|
|
if (! ti )
|
|
|
|
{
|
|
|
|
acceptPlugin(fullfn, newName);
|
|
|
|
}
|
2009-02-05 10:29:43 -05:00
|
|
|
}// foreach(QString pluginFileName, currAvailable)
|
|
|
|
|
|
|
|
qDebug() << " " << "names are " << names;
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
2009-02-09 16:01:41 -05:00
|
|
|
int
|
|
|
|
PluginManager::readPluginInformation(QString fullFileName, QString& pluginName)
|
2009-02-05 10:29:43 -05:00
|
|
|
{
|
|
|
|
qDebug() << " " << "processing file " << fullFileName;
|
|
|
|
|
2009-02-09 16:01:41 -05:00
|
|
|
PluginInterface* plugin = loadPluginInterface(fullFileName) ;
|
|
|
|
pluginName = "Undefined name" ;
|
2009-02-05 10:29:43 -05:00
|
|
|
if (plugin)
|
|
|
|
{
|
2009-02-09 16:01:41 -05:00
|
|
|
pluginName = plugin->pluginName();
|
|
|
|
qDebug() << " " << "got pluginName:" << pluginName;
|
|
|
|
delete plugin;
|
|
|
|
return 0 ;
|
2009-02-05 10:29:43 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-02-09 16:01:41 -05:00
|
|
|
//do not emit anything, cuz error message already was sent
|
|
|
|
//from loadPluginInterface(..)
|
|
|
|
return 1; //this means, some rrror appeared
|
2009-02-05 10:29:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
|
|
|
PluginInterface*
|
2009-02-09 16:01:41 -05:00
|
|
|
PluginManager::loadPluginInterface(QString fileName)
|
2009-02-05 10:29:43 -05:00
|
|
|
{
|
2009-02-09 16:01:41 -05:00
|
|
|
QString errorMessage = "Default Error Message" ;
|
2009-02-05 10:29:43 -05:00
|
|
|
PluginInterface* plugin = 0 ;
|
|
|
|
QPluginLoader* plLoader = new QPluginLoader(fileName);
|
|
|
|
|
|
|
|
QObject *pluginObject = plLoader->instance();
|
|
|
|
if (pluginObject)
|
|
|
|
{
|
|
|
|
//qDebug() << " " << "loaded..." ;
|
2009-02-09 16:01:41 -05:00
|
|
|
plugin = qobject_cast<PluginInterface*> (pluginObject) ;
|
2009-02-05 10:29:43 -05:00
|
|
|
|
|
|
|
if (plugin)
|
|
|
|
{
|
|
|
|
errorMessage = "No error" ;
|
|
|
|
}
|
|
|
|
else
|
2009-02-09 16:01:41 -05:00
|
|
|
{
|
|
|
|
errorMessage = "Cast to 'PluginInterface*' failed";
|
|
|
|
emit errorAppeared( errorMessage );
|
|
|
|
}
|
2009-02-05 10:29:43 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-02-09 16:01:41 -05:00
|
|
|
errorMessage = "Istance wasn't created: " + plLoader->errorString() ;
|
|
|
|
emit errorAppeared( errorMessage );
|
2009-02-05 10:29:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
delete plLoader; // plugin instance will not be deleted with this action
|
|
|
|
return plugin;
|
|
|
|
}
|
|
|
|
|
2009-02-09 16:01:41 -05:00
|
|
|
//=============================================================================
|
2009-02-05 10:29:43 -05:00
|
|
|
|
2009-02-09 16:01:41 -05:00
|
|
|
PluginManager:: ~PluginManager()
|
|
|
|
{
|
2009-02-05 10:29:43 -05:00
|
|
|
}
|
2009-02-09 16:01:41 -05:00
|
|
|
|
2009-02-05 10:29:43 -05:00
|
|
|
//=============================================================================
|
|
|
|
|
2009-02-09 16:01:41 -05:00
|
|
|
void
|
|
|
|
PluginManager::acceptPlugin(QString fileName, QString pluginName)
|
2009-02-05 10:29:43 -05:00
|
|
|
{
|
2009-02-09 16:01:41 -05:00
|
|
|
qDebug() << " " << "accepting plugin " << pluginName;
|
|
|
|
|
|
|
|
names.append(pluginName);
|
|
|
|
fileNames.append(fileName);
|
|
|
|
|
|
|
|
if (viewWidget)
|
|
|
|
viewWidget->registerNewPlugin( pluginName );
|
|
|
|
|
|
|
|
emit newPluginRegistered( pluginName );
|
2009-02-05 10:29:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
2009-02-09 16:01:41 -05:00
|
|
|
QWidget*
|
|
|
|
PluginManager::pluginWidget(QString pluginName)
|
2009-02-05 10:29:43 -05:00
|
|
|
{
|
2009-02-09 16:01:41 -05:00
|
|
|
QWidget* result = 0;
|
|
|
|
int plIndex = names.indexOf( pluginName ) ;
|
|
|
|
if (plIndex >=0 )
|
|
|
|
{
|
|
|
|
//=== load plugin's interface
|
|
|
|
QString fn = fileNames.at(plIndex);
|
|
|
|
PluginInterface* pliface = loadPluginInterface(fn);
|
|
|
|
if (pliface)
|
|
|
|
{
|
|
|
|
//=== now, get a widget
|
|
|
|
result = pliface->pluginWidget() ;
|
|
|
|
if (result)
|
|
|
|
{
|
|
|
|
// all was good,
|
|
|
|
qDebug() << " " << "got plg widget..." ;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-03-07 06:57:10 -05:00
|
|
|
QString em=tr("Error: instance '%1' can't create a widget")
|
2009-02-09 16:01:41 -05:00
|
|
|
.arg( pluginName );
|
|
|
|
emit errorAppeared( em );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// do nothing here...
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-07-10 14:37:21 -04:00
|
|
|
QString em = tr("Error: no plugin with name '%1' found")
|
2009-02-09 16:01:41 -05:00
|
|
|
.arg(pluginName);
|
|
|
|
emit errorAppeared( em );
|
|
|
|
}
|
2009-02-05 10:29:43 -05:00
|
|
|
}
|
2009-02-09 16:01:41 -05:00
|
|
|
|
2009-02-05 10:29:43 -05:00
|
|
|
//=============================================================================
|
2009-02-09 16:01:41 -05:00
|
|
|
|
|
|
|
QWidget*
|
|
|
|
PluginManager::getViewWidget(QWidget* parent )
|
2009-02-05 10:29:43 -05:00
|
|
|
{
|
2009-02-09 16:01:41 -05:00
|
|
|
if (viewWidget)
|
|
|
|
return viewWidget;
|
|
|
|
|
|
|
|
//=== else, create the viewWidget and return it
|
|
|
|
//
|
|
|
|
viewWidget = new PluginManagerWidget();
|
|
|
|
|
|
|
|
foreach(QString pn, names)
|
|
|
|
{
|
|
|
|
qDebug() << " " << "reg new plg " << pn;
|
|
|
|
viewWidget->registerNewPlugin( pn );
|
|
|
|
}
|
|
|
|
|
|
|
|
connect(this , SIGNAL( errorAppeared(QString) ) ,
|
|
|
|
viewWidget, SLOT( acceptErrorMessage( QString)));
|
|
|
|
|
|
|
|
connect(viewWidget, SIGNAL( destroyed() ) ,
|
|
|
|
this , SLOT( viewWidgetDestroyed( )));
|
|
|
|
|
|
|
|
connect(viewWidget, SIGNAL( installPluginRequested(QString)),
|
|
|
|
this , SLOT( installPlugin( QString)));
|
|
|
|
|
|
|
|
connect(viewWidget, SIGNAL( removeRequested( QString ) ),
|
|
|
|
this , SLOT( removePlugin(QString )));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
qDebug() << " PluginManager::getViewWidget done";
|
2009-02-05 10:29:43 -05:00
|
|
|
|
2009-02-09 16:01:41 -05:00
|
|
|
return viewWidget;
|
2009-02-05 10:29:43 -05:00
|
|
|
}
|
2009-02-09 16:01:41 -05:00
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
|
|
|
void
|
|
|
|
PluginManager::viewWidgetDestroyed(QObject* obj )
|
|
|
|
{
|
|
|
|
qDebug() << " PluginManager::viewWidgetDestroyed is here";
|
|
|
|
|
|
|
|
viewWidget = 0;
|
2009-02-05 10:29:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
|
|
|
void
|
2009-02-09 16:01:41 -05:00
|
|
|
PluginManager::removePlugin(QString pluginName)
|
2009-02-05 10:29:43 -05:00
|
|
|
{
|
2009-02-09 16:01:41 -05:00
|
|
|
QWidget* result = 0;
|
|
|
|
int plIndex = names.indexOf( pluginName ) ;
|
|
|
|
if (plIndex >=0 )
|
|
|
|
{
|
|
|
|
QString fn = fileNames.at(plIndex);
|
|
|
|
if (QDir::isRelativePath(fn))
|
|
|
|
fn = QDir(baseFolder).path() + QDir::separator() + fn ;
|
2009-02-05 10:29:43 -05:00
|
|
|
|
2009-02-09 16:01:41 -05:00
|
|
|
QFile fl(fn);
|
|
|
|
if (!fl.remove())
|
|
|
|
{
|
2014-12-17 13:02:52 -05:00
|
|
|
QString em = tr("Error: failed to remove file %1"
|
2017-03-07 06:57:10 -05:00
|
|
|
" (uninstalling plugin '%2')")
|
2009-02-09 16:01:41 -05:00
|
|
|
.arg(fn).arg(pluginName);
|
|
|
|
emit errorAppeared( em);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (viewWidget)
|
|
|
|
viewWidget->removePluginFrame( pluginName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-03-07 06:57:10 -05:00
|
|
|
QString em = tr("Error (uninstall): no plugin with name '%1' found")
|
2009-02-09 16:01:41 -05:00
|
|
|
.arg(pluginName);
|
|
|
|
emit errorAppeared( em );
|
|
|
|
}
|
2009-02-05 10:29:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
|
|
|
void
|
2009-02-09 16:01:41 -05:00
|
|
|
PluginManager::installPlugin(QString fileName)
|
2009-02-05 10:29:43 -05:00
|
|
|
{
|
2009-02-09 16:01:41 -05:00
|
|
|
qDebug() << " " << "PluginManager::installPlugin is here" ;
|
|
|
|
|
|
|
|
if (!QFile::exists( fileName) )
|
|
|
|
{
|
2017-03-07 06:57:10 -05:00
|
|
|
QString em = tr("Error (installation): plugin file %1 doesn't exist")
|
2009-02-09 16:01:41 -05:00
|
|
|
.arg( fileName );
|
|
|
|
|
|
|
|
emit errorAppeared( em );
|
|
|
|
return ;
|
|
|
|
}
|
2009-02-05 10:29:43 -05:00
|
|
|
|
2009-02-09 16:01:41 -05:00
|
|
|
QString pn;
|
|
|
|
if (! readPluginInformation(fileName, pn))
|
2009-02-05 10:29:43 -05:00
|
|
|
{
|
2009-02-09 16:01:41 -05:00
|
|
|
QFile sf( fileName) ;
|
|
|
|
QString newFileName = baseFolder + QDir::separator() +
|
|
|
|
QFileInfo( fileName).fileName();
|
|
|
|
if ( QFile::copy( fileName, newFileName ) )
|
|
|
|
{
|
|
|
|
QString pn;
|
|
|
|
int ti = readPluginInformation( newFileName , pn );
|
|
|
|
if (! ti )
|
|
|
|
{
|
|
|
|
acceptPlugin(newFileName, pn);
|
|
|
|
}
|
|
|
|
} //
|
|
|
|
else
|
|
|
|
{
|
2014-12-17 13:02:52 -05:00
|
|
|
QString em = tr("Error: can't copy %1 to %2")
|
2009-02-09 16:01:41 -05:00
|
|
|
.arg(fileName, newFileName) ;
|
|
|
|
emit errorAppeared( em );
|
2009-02-05 10:29:43 -05:00
|
|
|
}
|
|
|
|
}
|
2009-02-09 16:01:41 -05:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// do nothing here: read plugin information emits its own error
|
|
|
|
}
|
2009-02-05 10:29:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|