2009-02-01 11:37:07 -05:00
|
|
|
#include <QGroupBox>
|
|
|
|
#include <QFrame>
|
|
|
|
#include <QVBoxLayout>
|
|
|
|
#include <QTabWidget>
|
|
|
|
#include <QLabel>
|
|
|
|
#include <QSizePolicy>
|
2009-02-05 10:29:43 -05:00
|
|
|
#include <QTextEdit>
|
2009-02-01 11:37:07 -05:00
|
|
|
|
|
|
|
#include <QtPlugin>
|
|
|
|
#include <QPluginLoader>
|
|
|
|
#include <QDir>
|
|
|
|
#include <QDebug>
|
2009-02-05 10:29:43 -05:00
|
|
|
#include <QStringList>
|
|
|
|
#include <QRegExp>
|
2009-02-01 11:37:07 -05:00
|
|
|
|
2009-02-05 10:29:43 -05:00
|
|
|
#include <QApplication> // for qApp->....
|
|
|
|
//#include <QUiLoader>
|
|
|
|
//#include <QtScript>
|
2009-02-01 11:37:07 -05:00
|
|
|
#include <QFile>
|
2009-02-05 10:29:43 -05:00
|
|
|
//#include <QIODevice>
|
2009-02-01 11:37:07 -05:00
|
|
|
#include <QMessageBox>
|
2009-02-05 10:29:43 -05:00
|
|
|
#include <QGroupBox>
|
2009-02-01 11:37:07 -05:00
|
|
|
|
|
|
|
#include "PluginsPage.h"
|
2009-02-05 10:29:43 -05:00
|
|
|
#include "PluginManagerWidget.h"
|
|
|
|
#include "PluginManager.h"
|
2009-02-01 11:37:07 -05:00
|
|
|
#include "rshare.h"
|
2009-02-05 10:29:43 -05:00
|
|
|
//#include "plugins/PluginInterface.h"
|
2009-02-01 11:37:07 -05:00
|
|
|
|
|
|
|
//==============================================================================
|
|
|
|
|
|
|
|
PluginsPage::PluginsPage(QWidget *parent )
|
2009-02-05 10:29:43 -05:00
|
|
|
// :QGroupBox(parent)
|
2009-02-01 11:37:07 -05:00
|
|
|
:MainPage(parent)
|
|
|
|
{
|
2009-02-05 10:29:43 -05:00
|
|
|
//=== create some gui elements =====
|
2009-02-01 11:37:07 -05:00
|
|
|
pluginPageLayout = new QVBoxLayout(this);
|
|
|
|
|
2009-02-05 10:29:43 -05:00
|
|
|
// pluginPanel = new QGroupBox(this) ;
|
|
|
|
// pluginPanel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding) ;
|
|
|
|
// this->setTitle("RetroShare plugins");
|
|
|
|
|
|
|
|
pluginTabs = new QTabWidget(this) ;
|
|
|
|
pluginPageLayout->addWidget(pluginTabs);
|
|
|
|
|
|
|
|
pmFrame = new QFrame;
|
|
|
|
pmLay = new QVBoxLayout(pmFrame);
|
|
|
|
|
|
|
|
pluginManagerWidget = new PluginManagerWidget(pmFrame);
|
|
|
|
pmLay->addWidget( pluginManagerWidget );
|
|
|
|
|
|
|
|
pmSpacer = new QSpacerItem(283, 20,
|
|
|
|
QSizePolicy::Expanding, QSizePolicy::Minimum);
|
|
|
|
pmLay->addItem(pmSpacer);
|
|
|
|
|
|
|
|
errlogConsole = new QTextEdit();
|
|
|
|
pmLay->addWidget( errlogConsole );
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pluginTabs->addTab( pmFrame, "Manager" ) ;
|
2009-02-01 11:37:07 -05:00
|
|
|
|
2009-02-05 10:29:43 -05:00
|
|
|
// pluginTabs->addTab( errlogConsole, "Error messages" );
|
2009-02-01 11:37:07 -05:00
|
|
|
|
2009-02-05 10:29:43 -05:00
|
|
|
//=== try to load binary plugins =====
|
|
|
|
pluginManager = new PluginManager();
|
|
|
|
connect( pluginManager, SIGNAL( loadDone(QString, QWidget*) ),
|
|
|
|
this , SLOT( loadDone(QString, QWidget*) ) );
|
|
|
|
connect( pluginManager, SIGNAL(loadFailed(QString, QString)) ,
|
|
|
|
this , SLOT( loadFailed(QString, QString ) ) );
|
|
|
|
connect( pluginManagerWidget, SIGNAL( needToLoadFileWithPlugin(QString)),
|
|
|
|
pluginManager , SLOT( readPluginFromFile(QString) ) );
|
|
|
|
|
|
|
|
connect( pluginManager, SIGNAL( installComplete(QString) ),
|
|
|
|
this , SLOT( installComplete(QString) ) );
|
|
|
|
connect( pluginManager, SIGNAL(installFailed(QString, QString)) ,
|
|
|
|
this , SLOT( installFailed(QString, QString ) ) );
|
|
|
|
|
|
|
|
pluginManager->defaultLoad( Rshare::dataDirectory() + "/plugins" );
|
|
|
|
// a variant for a toy app: qApp->applicationDirPath()+"///plugins" ) ;
|
|
|
|
|
|
|
|
//QStringList lll = pluginManager->availablePlugins();
|
|
|
|
//foreach( QString pn, lll)
|
|
|
|
//{
|
|
|
|
// qDebug() << " " << "----" << pn << "----" ;
|
|
|
|
//}
|
|
|
|
|
|
|
|
/*
|
|
|
|
QDir pluginsDir(qApp->applicationDirPath()+"///plugins");
|
|
|
|
if (pluginsDir.exists())
|
|
|
|
loadBinaryPlugins( pluginsDir );
|
|
|
|
else
|
|
|
|
{
|
|
|
|
qDebug() << " " << "plugins will not be loaded: " << pluginsDir.absolutePath() ;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
// pluginPageLayout->addWidget(pluginPanel);
|
|
|
|
/*
|
|
|
|
pluginPanelLayout = new QVBoxLayout(pluginPanel);
|
|
|
|
|
2009-02-01 11:37:07 -05:00
|
|
|
pluginTabs = new QTabWidget(pluginPanel);
|
|
|
|
pluginPanelLayout->addWidget(pluginTabs);
|
|
|
|
|
|
|
|
QLabel* lbl1 = new QLabel();
|
2009-02-05 10:29:43 -05:00
|
|
|
lbl1->setText("If you see only this tab, it's a bug :( If you see this tub and calculator, it's a bug too");
|
2009-02-01 11:37:07 -05:00
|
|
|
pluginTabs->addTab(lbl1, "LLL");//Rshare::dataDirectory());
|
|
|
|
//"L #1");
|
|
|
|
|
|
|
|
//QLabel* lbl2 = new QLabel();
|
|
|
|
//lbl2->setText("Label #2");
|
|
|
|
//pluginTabs->addTab(lbl2, "L #2; for debugging purposes");
|
|
|
|
|
2009-02-05 10:29:43 -05:00
|
|
|
QDir pluginsDir(Rshare::dataDirectory());
|
2009-02-01 11:37:07 -05:00
|
|
|
|
2009-02-05 10:29:43 -05:00
|
|
|
// this piece of code is magical and untested ;
|
|
|
|
// but on Windows it works
|
|
|
|
#if defined(Q_OS_WIN)
|
|
|
|
if (pluginsDir.dirName().toLower() == "debug" || pluginsDir.dirName().toLower() == "release")
|
|
|
|
pluginsDir.cdUp();
|
|
|
|
#elif defined(Q_OS_MAC)
|
|
|
|
if (pluginsDir.dirName() == "MacOS") {
|
|
|
|
pluginsDir.cdUp();
|
|
|
|
pluginsDir.cdUp();
|
|
|
|
pluginsDir.cdUp();
|
2009-02-02 10:31:34 -05:00
|
|
|
}
|
2009-02-05 10:29:43 -05:00
|
|
|
#endif
|
|
|
|
// eof magick
|
|
|
|
|
|
|
|
pluginsDir.cd("plugins");
|
2009-02-01 11:37:07 -05:00
|
|
|
|
|
|
|
foreach (QString fileName, pluginsDir.entryList(QDir::Files))
|
|
|
|
{
|
|
|
|
qDebug() << " "
|
|
|
|
<< "processing file "
|
|
|
|
<< pluginsDir.absoluteFilePath(fileName);
|
|
|
|
|
|
|
|
QPluginLoader loader(pluginsDir.absoluteFilePath(fileName));
|
|
|
|
QObject *pluginObject = loader.instance();
|
|
|
|
if (pluginObject)
|
|
|
|
{
|
|
|
|
qDebug() << " " << "loaded..." ;
|
|
|
|
PluginInterface* plugin = qobject_cast<PluginInterface*> (pluginObject) ;
|
|
|
|
|
|
|
|
if (plugin)
|
|
|
|
{
|
|
|
|
QString pn = plugin->pluginName();
|
|
|
|
qDebug() << " " << "got description:" << pn;
|
|
|
|
|
|
|
|
QWidget* pw = plugin->pluginWidget();
|
|
|
|
qDebug() << " " << "got widget..." ;
|
|
|
|
|
|
|
|
pluginsDir.absoluteFilePath(fileName);
|
|
|
|
pluginTabs->addTab(pw,pn);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
qDebug() << " " << "cast failed..." ;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// script plugins loading; warning, code is dirty; will be changed later
|
2009-02-05 10:29:43 -05:00
|
|
|
engine = new QScriptEngine;
|
2009-02-01 11:37:07 -05:00
|
|
|
|
2009-02-05 10:29:43 -05:00
|
|
|
pluginsDir.cd("../script_plugins");
|
2009-02-01 11:37:07 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
foreach (QString scriptDirName, pluginsDir.entryList(QDir::Dirs))
|
|
|
|
{
|
|
|
|
qDebug() << " " << "sdn is " << scriptDirName ;
|
|
|
|
if ( (scriptDirName !=".") && (scriptDirName != "..") )
|
|
|
|
{
|
|
|
|
QDir spDir(pluginsDir) ;
|
|
|
|
spDir.cd(scriptDirName);
|
|
|
|
QStringList scfList = spDir.entryList(QDir::Files) ;
|
|
|
|
qDebug() << " "
|
|
|
|
<< "to process files: "
|
|
|
|
<< scfList ;// pluginsDir.absoluteFilePath(fileName);
|
|
|
|
|
|
|
|
int ti;
|
|
|
|
|
|
|
|
QRegExp rx_qs(".*js");
|
|
|
|
|
|
|
|
ti = scfList.indexOf(rx_qs);
|
2009-02-05 10:29:43 -05:00
|
|
|
QFile scriptFile( spDir.absoluteFilePath( scfList.at(ti) ) );
|
|
|
|
scriptFile.open(QIODevice::ReadOnly);
|
|
|
|
engine->evaluate(scriptFile.readAll());
|
|
|
|
scriptFile.close();
|
2009-02-01 11:37:07 -05:00
|
|
|
|
|
|
|
QUiLoader loader;
|
|
|
|
QRegExp rx_ui(".*ui");
|
2009-02-05 10:29:43 -05:00
|
|
|
ti = scfList.indexOf(rx_ui) ;
|
|
|
|
QFile uiFile( spDir.absoluteFilePath( scfList.at(ti) ) );
|
|
|
|
qDebug() << "ui file is " << scfList.at(ti) ;
|
|
|
|
uiFile.open(QIODevice::ReadOnly);
|
|
|
|
|
|
|
|
QWidget *ui = loader.load(&uiFile);
|
|
|
|
uiFile.close();
|
2009-02-02 08:47:18 -05:00
|
|
|
|
2009-02-05 10:29:43 -05:00
|
|
|
QScriptValue ctor = engine->evaluate("Plugin");
|
|
|
|
QScriptValue scriptUi = engine->newQObject(ui, QScriptEngine::ScriptOwnership);
|
|
|
|
QScriptValue calc = ctor.construct(QScriptValueList() << scriptUi);
|
2009-02-01 11:37:07 -05:00
|
|
|
|
|
|
|
if (!ui)
|
|
|
|
qDebug() << "ui is null :(" ;
|
|
|
|
|
|
|
|
//ui->show();
|
|
|
|
pluginTabs->addTab(ui,"Script plugin");
|
|
|
|
}
|
2009-02-05 10:29:43 -05:00
|
|
|
//What has a head like a cat, feet like a cat, a tail like a cat, but isn't a cat?
|
2009-02-01 11:37:07 -05:00
|
|
|
}
|
2009-02-05 10:29:43 -05:00
|
|
|
*/
|
2009-02-01 11:37:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//==============================================================================
|
|
|
|
|
|
|
|
PluginsPage::~PluginsPage()
|
|
|
|
{
|
2009-02-05 10:29:43 -05:00
|
|
|
// remove all pages, exept first (i.e remove all pages with plugins)
|
|
|
|
for( int pi=1; pi<pluginTabs->count(); pi++)
|
|
|
|
{
|
|
|
|
pluginTabs->removeTab(pi); // widgets itself will be deleted
|
|
|
|
//in ~PluginManager()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
|
|
|
void
|
|
|
|
PluginsPage::installComplete(QString pluginName)
|
|
|
|
{
|
|
|
|
PluginFrame* pf = new PluginFrame( pluginManagerWidget, pluginName);
|
|
|
|
|
|
|
|
connect( pluginManager, SIGNAL( loadDone(QString, QWidget*) ),
|
|
|
|
pf , SLOT( successfulLoad(QString, QWidget*) ) );
|
|
|
|
|
|
|
|
connect( pf , SIGNAL( needToLoad(QString) ),
|
|
|
|
pluginManager, SLOT( loadPlugin(QString) ) );
|
|
|
|
|
|
|
|
// connect( pf , SIGNAL( needToUnload(QString) ),
|
|
|
|
// pluginManager, SLOT( unloadPlugin(QString) ) );
|
|
|
|
|
|
|
|
connect( pf , SIGNAL( needToUnload(QString) ),
|
|
|
|
this , SLOT( unloadPlugin(QString) ) );
|
|
|
|
|
|
|
|
pluginManagerWidget->addPluginWidget(pf);
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
|
|
|
void
|
|
|
|
PluginsPage::installFailed(QString pluginFileName, QString errorMessage)
|
|
|
|
{
|
|
|
|
QString tmps = QString("failed to install plugin from %1 (%2)")
|
|
|
|
.arg(pluginFileName)
|
|
|
|
.arg(errorMessage);
|
|
|
|
errlogConsole->append(tmps);
|
|
|
|
}
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
|
|
|
void
|
|
|
|
PluginsPage::loadDone(QString pluginName, QWidget* pluginWidget)
|
|
|
|
{
|
|
|
|
pluginTabs->addTab( pluginWidget, pluginName );
|
|
|
|
}
|
|
|
|
|
|
|
|
//==============================================================================
|
|
|
|
|
|
|
|
void
|
|
|
|
PluginsPage::loadFailed(QString pluginName, QString errorMessage)
|
|
|
|
{
|
|
|
|
QString tmps = QString("failed to load plugin %1 (%2)")
|
|
|
|
.arg(pluginName)
|
|
|
|
.arg(errorMessage);
|
|
|
|
errlogConsole->append(tmps);
|
|
|
|
}
|
|
|
|
|
|
|
|
//==============================================================================
|
|
|
|
|
|
|
|
void
|
|
|
|
PluginsPage::unloadPlugin(QString pluginName)
|
|
|
|
{
|
|
|
|
qDebug() << " " << "PluginsPage::unloadPlugin called for " << pluginName ;
|
|
|
|
for (int tabi=0; tabi< pluginTabs->count(); tabi++)
|
|
|
|
{
|
|
|
|
if( pluginTabs->tabText(tabi) == pluginName)
|
|
|
|
{
|
|
|
|
QWidget* tw= pluginTabs->widget(tabi);
|
|
|
|
pluginTabs->removeTab( tabi);//the plugin widget will not be deleted !!
|
|
|
|
//plugin manager will delete it in
|
|
|
|
//it's unloadPlugin
|
|
|
|
// qDebug() << " " << "in pp " << (int) tw ;
|
|
|
|
// delete tw;
|
2009-02-01 11:37:07 -05:00
|
|
|
|
2009-02-05 10:29:43 -05:00
|
|
|
pluginManager->unloadPlugin( pluginName );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2009-02-01 11:37:07 -05:00
|
|
|
}
|
2009-02-05 10:29:43 -05:00
|
|
|
|
|
|
|
//==============================================================================
|
|
|
|
|
|
|
|
int
|
|
|
|
PluginsPage::loadBinaryPlugins(const QDir directory)
|
|
|
|
{/*
|
|
|
|
//=== find a file with last loaded plugins =====
|
|
|
|
QStringList lastLoaded;
|
|
|
|
QFile llFile( directory.absolutePath()+"last_loaded.txt" ) ;
|
|
|
|
if ( llFile.open(QIODevice::ReadOnly) )
|
|
|
|
{
|
|
|
|
QString tmps;
|
|
|
|
QTextStream stream( &llFile ); // Set the stream to read from myFile
|
|
|
|
tmps = stream.readLine();
|
|
|
|
|
|
|
|
lastLoaded = tmps.split(";");
|
|
|
|
llFile.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
//=== get current available plugins =====
|
|
|
|
QStringList currAvailable = directory.entryList(QDir::Files);
|
|
|
|
#if defined(Q_OS_WIN)
|
|
|
|
QRegExp trx("*.dll")
|
|
|
|
#else
|
|
|
|
QRegExp trx("*.so");
|
|
|
|
#endif
|
|
|
|
trx.setPatternSyntax(QRegExp::Wildcard );
|
|
|
|
|
|
|
|
currAvailable.filter( trx );
|
|
|
|
|
|
|
|
qDebug() << " " << "can load this plugins: " << currAvailable ;
|
|
|
|
|
|
|
|
//=== create widgets for all available; also load which were loaded before
|
|
|
|
foreach(QString pluginFileName, currAvailable)
|
|
|
|
{
|
|
|
|
QString tmps( directory.absoluteFilePath( pluginFileName ) );
|
|
|
|
pluginManagerWidget->addPluginWidget( tmps );
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//==============================================================================
|
|
|
|
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
|
|
|
//==============================================================================
|