PluginLoader.cc 88.8 KB
Newer Older
1
/*===========================================================================*\
Jan Möbius's avatar
Jan Möbius committed
2
3
*                                                                            *
*                              OpenFlipper                                   *
Jan Möbius's avatar
Jan Möbius committed
4
*      Copyright (C) 2001-2011 by Computer Graphics Group, RWTH Aachen       *
Jan Möbius's avatar
Jan Möbius committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
*                           www.openflipper.org                              *
*                                                                            *
*--------------------------------------------------------------------------- *
*  This file is part of OpenFlipper.                                         *
*                                                                            *
*  OpenFlipper is free software: you can redistribute it and/or modify       *
*  it under the terms of the GNU Lesser General Public License as            *
*  published by the Free Software Foundation, either version 3 of            *
*  the License, or (at your option) any later version with the               *
*  following exceptions:                                                     *
*                                                                            *
*  If other files instantiate templates or use macros                        *
*  or inline functions from this file, or you compile this file and          *
*  link it with other files to produce an executable, this file does         *
*  not by itself cause the resulting executable to be covered by the         *
*  GNU Lesser General Public License. This exception does not however        *
*  invalidate any other reasons why the executable file might be             *
*  covered by the GNU Lesser General Public License.                         *
*                                                                            *
*  OpenFlipper 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 Lesser General Public License for more details.                       *
*                                                                            *
*  You should have received a copy of the GNU LesserGeneral Public           *
*  License along with OpenFlipper. If not,                                   *
*  see <http://www.gnu.org/licenses/>.                                       *
*                                                                            *
33
34
35
\*===========================================================================*/

/*===========================================================================*\
Jan Möbius's avatar
Jan Möbius committed
36
37
*                                                                            *
*   $Revision$                                                       *
38
*   $LastChangedBy$                                         *
Jan Möbius's avatar
Jan Möbius committed
39
40
*   $Date$                     *
*                                                                            *
41
\*===========================================================================*/
Jan Möbius's avatar
   
Jan Möbius committed
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61




//=============================================================================
//
//  CLASS Core - IMPLEMENTATION of the Plugin Loading
//
//=============================================================================


//== INCLUDES =================================================================

// -------------------- mview
#include "Core.hh"

#include <QMenuBar>
#include <QToolBox>
#include <QMessageBox>
#include <QApplication>
Jan Möbius's avatar
Jan Möbius committed
62
#include <QScrollArea>
Jan Möbius's avatar
   
Jan Möbius committed
63
64
65
66
67
68
69
70
71

#include <QPluginLoader>
#include "OpenFlipper/BasePlugin/BaseInterface.hh"
#include "OpenFlipper/BasePlugin/KeyInterface.hh"
#include "OpenFlipper/BasePlugin/BackupInterface.hh"
#include "OpenFlipper/BasePlugin/LoggingInterface.hh"
#include "OpenFlipper/BasePlugin/MouseInterface.hh"
#include "OpenFlipper/BasePlugin/PickingInterface.hh"
#include "OpenFlipper/BasePlugin/ToolboxInterface.hh"
Dirk Wilden's avatar
Dirk Wilden committed
72
#include "OpenFlipper/BasePlugin/OptionsInterface.hh"
Jan Möbius's avatar
   
Jan Möbius committed
73
74
75
76
#include "OpenFlipper/BasePlugin/ToolbarInterface.hh"
#include "OpenFlipper/BasePlugin/TextureInterface.hh"
#include "OpenFlipper/BasePlugin/MenuInterface.hh"
#include "OpenFlipper/BasePlugin/ContextMenuInterface.hh"
77
#include "OpenFlipper/BasePlugin/ProcessInterface.hh"
Jan Möbius's avatar
   
Jan Möbius committed
78
#include "OpenFlipper/BasePlugin/ViewInterface.hh"
Jan Möbius's avatar
Jan Möbius committed
79
#include "OpenFlipper/BasePlugin/ViewModeInterface.hh"
Jan Möbius's avatar
   
Jan Möbius committed
80
81
82
83
84
85
#include "OpenFlipper/BasePlugin/LoadSaveInterface.hh"
#include "OpenFlipper/BasePlugin/StatusbarInterface.hh"
#include "OpenFlipper/BasePlugin/INIInterface.hh"
#include "OpenFlipper/BasePlugin/FileInterface.hh"
#include "OpenFlipper/BasePlugin/RPCInterface.hh"
#include "OpenFlipper/BasePlugin/ScriptInterface.hh"
86
#include "OpenFlipper/BasePlugin/SecurityInterface.hh"
87
#include "OpenFlipper/BasePlugin/SelectionInterface.hh"
88
#include "OpenFlipper/BasePlugin/TypeInterface.hh"
89
#include "OpenFlipper/BasePlugin/PluginConnectionInterface.hh"
Jan Möbius's avatar
   
Jan Möbius committed
90
91
92
93
94
95
96
97
98

#include "OpenFlipper/INIFile/INIFile.hh"

#include "OpenFlipper/common/GlobalOptions.hh"

#include <QMessageBox>
#include <QtScript/QScriptValueIterator>

#include <ACG/QtWidgets/QtFileDialog.hh>
Dirk Wilden's avatar
Dirk Wilden committed
99
#include "OpenFlipper/widgets/PluginDialog/PluginDialog.hh"
Jan Möbius's avatar
   
Jan Möbius committed
100

Jan Möbius's avatar
Jan Möbius committed
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
class PreloadThread : public QThread
{
  public: 
    
    /** \brief Preload thread constructor
    *
    * This constructor initializes the preloading thread.
    * @param _fileName Filename of the Plugin to be loaded.
    */
    PreloadThread(QString _fileName):
      filename_(_fileName) {      
    }
  
  public:
    
    /** \brief get an instance of the loaded plugin
    *
    * This function returns an instance of the loaded plugin or 0 if something went wrong.
    * This mehod has to be called from the main thread as the plugins have to stay in the
    * core thread of the application.
    */
    QObject* getInstance() { return loader_.instance(); }
    
    /** \brief function to get the filename used in this preloader */
    QString filename() { return filename_; }
    
    /// If an error occured, this function will return the error message
    QString getError() { return loader_.errorString(); }
    
    /** \brief preload function
    *
    * This function is used in the thread to preload a plugin. The name is given in the constructor.
    */
    void run() {
      loader_.setFileName( filename_ );
      loader_.load();
    }
    
  private:
    QString filename_;
    QPluginLoader loader_;
};


Jan Möbius's avatar
   
Jan Möbius committed
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
//== IMPLEMENTATION ==========================================================


bool Core::checkSlot(QObject* _plugin , const char* _slotSignature) {
  const QMetaObject* meta = _plugin->metaObject();
  int id = meta->indexOfSlot( QMetaObject::normalizedSignature( _slotSignature ) );
  return ( id != -1 );
}

bool Core::checkSignal(QObject* _plugin , const char* _signalSignature) {
  const QMetaObject* meta = _plugin->metaObject();
  int id = meta->indexOfSignal( QMetaObject::normalizedSignature( _signalSignature ) );
  return ( id != -1 );
}

 /** Load plugins from dir Plugins. The plugin information gets stored, toolbox and menu entries are created and the required signals and slots are connected.
  */
void Core::loadPlugins()
{
Jan Möbius's avatar
Jan Möbius committed
164
165
166
  
  QString licenseTexts = "";
  
Dirk Wilden's avatar
Dirk Wilden committed
167
  //try to load plugins from new location
168
  QDir tempDir = QDir(OpenFlipper::Options::pluginDir());
Dirk Wilden's avatar
Dirk Wilden committed
169

170
171
172
173
174
  // Possible Plugin extensions
  QStringList filter;
  if ( OpenFlipper::Options::isWindows() )
    filter << "*.dll";
  else if ( OpenFlipper::Options::isDarwin() )
Jan Möbius's avatar
Jan Möbius committed
175
176
    filter << "*.so";
//    filter << "*.dylib";
177
178
179
  else
    filter << "*.so";
  
Jan Möbius's avatar
   
Jan Möbius committed
180
  // Get all files in the Plugin dir
181
  QStringList pluginlist = tempDir.entryList(filter,QDir::Files);
Dirk Wilden's avatar
Dirk Wilden committed
182

183
184
  // Convert local file path to absolute path
  for (int i=0; i < pluginlist.size(); i++) {
185
     pluginlist[i] = tempDir.absoluteFilePath(pluginlist[i]);
Jan Möbius's avatar
   
Jan Möbius committed
186
187
  }

188
  // get all Plugin Names which will not be loaded
189
  QStringList dontLoadPlugins = OpenFlipperSettings().value("PluginControl/DontLoadNames", QStringList()).toStringList();
190
191
192
193
194
195
  
  // Output info about additional plugins
  for ( int i = 0 ; i < dontLoadPlugins.size(); ++i) 
    emit log(LOGOUT,tr("dontLoadPlugins Plugin from ini file: %1").arg( dontLoadPlugins[i] ) );
  
  // get all Plugins which should be loaded in addition to the standard plugins 
196
  QStringList additionalPlugins = OpenFlipperSettings().value("PluginControl/AdditionalPlugins", QStringList()).toStringList();
197
198
  
  // Output info about additional plugins
Jan Möbius's avatar
Jan Möbius committed
199
  for ( int i = 0 ; i < additionalPlugins.size(); ++i) {
200
    emit log(LOGOUT,tr("Additional Plugin from file: %1").arg( additionalPlugins[i] ) );
Jan Möbius's avatar
Jan Möbius committed
201
  }
202
203
204
205
  
  // Prepend the additional Plugins to the plugin list
  pluginlist = additionalPlugins << pluginlist;
  
Jan Möbius's avatar
   
Jan Möbius committed
206
  // Sort plugins to load FilePlugins first
207
  QStringList typePlugins;
Jan Möbius's avatar
   
Jan Möbius committed
208
209
210
211
212
  QStringList filePlugins;
  QStringList textureControl;
  QStringList otherPlugins;
  //plugin Liste sortieren
  for (int i=0; i < pluginlist.size(); i++)
213
214
215
    if (pluginlist[i].contains("Plugin-Type") )
      typePlugins.push_back(pluginlist[i]);
    else if (pluginlist[i].contains("Plugin-File") )
Jan Möbius's avatar
   
Jan Möbius committed
216
217
218
219
220
221
      filePlugins.push_back(pluginlist[i]);
    else if (pluginlist[i].contains("TextureControl"))
      textureControl.push_back(pluginlist[i]);
    else
      otherPlugins.push_back(pluginlist[i]);

222
223
224
225
226
227
  // This is the order in which plugins have to be loaded:
  // First load all type Plugins to register the dataTypes to the core
  // Than load the file plugins to load objects 
  // Next is textureControl to control texture based properties
  // Than load everything else.
  pluginlist = typePlugins << filePlugins << textureControl << otherPlugins;
Jan Möbius's avatar
   
Jan Möbius committed
228

229
230
  for ( int i = 0 ; i < dontLoadPlugins.size(); ++i )
    emit log(LOGWARN,tr("Skipping Plugins :\t %1").arg( dontLoadPlugins[i] ) );
Jan Möbius's avatar
   
Jan Möbius committed
231

232
  emit log(LOGOUT,"================================================================================");
Jan Möbius's avatar
   
Jan Möbius committed
233

Jan Möbius's avatar
Jan Möbius committed
234
235
236
237
238
239
240
  QTime time;
  
  time.start();
  
  std::vector< PreloadThread* > loaderThreads;
  
  // Try to load each file as a plugin in a separate thread (only load them in seperate thread. Instance will be created in main thread)
Jan Möbius's avatar
   
Jan Möbius committed
241
  for ( int i = 0 ; i < pluginlist.size() ; ++i) {
Jan Möbius's avatar
Jan Möbius committed
242
243
244
245
246
247
248
249
250
251
252
253
254
255
    
    // create loader thread
    PreloadThread* load = new PreloadThread(pluginlist[i]);
    
    // remember loader thread
    loaderThreads.push_back(load);
    
    // start loader thread
    load->start();
  }
  
  // Check each thread for a valid plugin
  for ( uint i = 0 ; i < loaderThreads.size() ; ++i) {
    
256
    if ( OpenFlipper::Options::gui() && OpenFlipperSettings().value("Core/Gui/splash",true).toBool() ) {
Jan Möbius's avatar
Jan Möbius committed
257
      splashMessage_ = tr("Loading Plugin %1/%2").arg(i).arg(loaderThreads.size()) ;
Jan Möbius's avatar
   
Jan Möbius committed
258
259
260
      splash_->showMessage( splashMessage_ , Qt::AlignBottom | Qt::AlignLeft , Qt::white);
      QApplication::processEvents();
    }
Jan Möbius's avatar
Jan Möbius committed
261
262
263
264
    
    loaderThreads[i]->wait();
    
    if ( loaderThreads[i]->getInstance() != 0 ) {
Jan Möbius's avatar
Jan Möbius committed
265
266
267
      QString pluginLicenseText  = "";
      loadPlugin(loaderThreads[i]->filename(),true,pluginLicenseText, loaderThreads[i]->getInstance());
      licenseTexts += pluginLicenseText;
Jan Möbius's avatar
Jan Möbius committed
268
269
270
271
272
273
274
    } else {
      emit log(LOGERR,tr("Unable to load Plugin :\t %1").arg( loaderThreads[i]->filename() ) );
      emit log(LOGERR,tr("Error was : ") + loaderThreads[i]->getError() );
      emit log(LOGOUT,"================================================================================");       
    }
    
    delete loaderThreads[i];
Jan Möbius's avatar
   
Jan Möbius committed
275
276
277
  }

  splashMessage_ = "";
Jan Möbius's avatar
Jan Möbius committed
278
279
  
  if ( licenseTexts != "" ) {
Jan Möbius's avatar
Jan Möbius committed
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
    if ( OpenFlipper::Options::gui() ) {
      
      // split for each license block
      QStringList licenseBlocks = licenseTexts.split("==");

      // Cleanup lists to get only the ones containing valid plugins.
      for ( QStringList::iterator it = licenseBlocks.begin(); it != licenseBlocks.end() ; ++it )
        if ( ! it->contains("PluginName") ) {
          licenseBlocks.erase(it);
          it = licenseBlocks.begin();
        }
      
      // sort by the contact mails
      QMap< QString , QString > contacts;
      
      for ( QStringList::iterator it = licenseBlocks.begin(); it != licenseBlocks.end() ; ++it ) {
        QStringList lines = it->split("\n");
        
        lines = lines.filter ( "Contact mail", Qt::CaseInsensitive );
        
        // Corect one found:
        if (lines.size() == 1)  {
          QString mail = lines[0].section(":",-1).simplified();
          QString list = contacts.take(mail);
          list.append(*it);
          contacts.insert(mail,list);
        } else {
          emit log(LOGWARN,tr("Can't extract mail contact from license request"));
        }
          
      }
      
      for ( QMap<QString , QString>::iterator it = contacts.begin() ; it != contacts.end() ; ++it ) {
        
        QStringList request = it.value().split("\n");
        
        // Cleanup lists to get only the relevant part
        for ( QStringList::iterator lit = request.begin(); lit != request.end() ; ++lit ) {
          
          if ( lit->contains("Message:")  ) {
            *lit = lit->section(":",-1).simplified();
          }
          
          if ( lit->contains("Contact mail:")  ) {
            *lit = lit->section(":",-1).simplified();
          }
          
        }
        
Jan Möbius's avatar
Jan Möbius committed
329
        QMessageBox::StandardButton button = QMessageBox::warning ( 0, tr("Plugin License check failed, issuer is: %1").arg( it.key() ),  request.join("\n") + tr("\n\n The text has been copied to your clipboard.\n Open in Mail program?"),QMessageBox::Yes | QMessageBox::No,QMessageBox::Yes  );
Jan Möbius's avatar
Jan Möbius committed
330
        
Jan Möbius's avatar
Jan Möbius committed
331
332
333
334
335
        
        QClipboard *cb = QApplication::clipboard();
        
        // set a text to the Clipboard
        cb->setText(request.join("\n"));
Jan Möbius's avatar
Jan Möbius committed
336
337


338
339
340
341
342
343
344
345
        if ( button == QMessageBox::Yes ) {
           QString url = "mailto:" + it.key();
           url += "?subject=License Request&body=";
#ifdef WIN32
           url += request.join(";;");
#else
           url += request.join("\n");
#endif
Jan Möbius's avatar
Jan Möbius committed
346

347
348
349
           QUrl encodedURL(url, QUrl::TolerantMode);
           QDesktopServices::openUrl(encodedURL);
        }
Jan Möbius's avatar
Jan Möbius committed
350
351
352
353
354
355
        
      }

      
    } else {
      emit log(LOGWARN,tr("Plugin License check failed: "));
Jan Möbius's avatar
Jan Möbius committed
356
357
358
      std::cerr << licenseTexts.toStdString() << std::endl;
    }
  } 
Jan Möbius's avatar
   
Jan Möbius committed
359
360
361

  emit pluginsInitialized();

Jan Möbius's avatar
Jan Möbius committed
362
  emit log(LOGOUT,tr("Loaded %n Plugin(s)","",plugins.size()) );
Jan Möbius's avatar
   
Jan Möbius committed
363
364
365
366
367
368
369
370
371
}

/** @brief slot for loading Plugins
 */
void Core::slotLoadPlugin(){

  if ( OpenFlipper::Options::nogui() )
    return;

372
  // Setup filters for possible plugin extensions
Jan Möbius's avatar
   
Jan Möbius committed
373
374
375
  QString filter;
  if ( OpenFlipper::Options::isWindows() )
    filter = "Plugins (*.dll)";
376
  else if ( OpenFlipper::Options::isDarwin() )
Jan Möbius's avatar
Jan Möbius committed
377
378
    // filter = "Plugins (*.dylib)";
    filter = "Plugins (*.so)";
Jan Möbius's avatar
   
Jan Möbius committed
379
380
381
  else
    filter = "Plugins (*.so)";

382
  // Ask the user to select the file to load
383
  QString filename = ACG::getOpenFileName(coreWidget_,tr("Load Plugin"),filter, OpenFlipperSettings().value("Core/CurrentDir").toString() );
Jan Möbius's avatar
   
Jan Möbius committed
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404

  if (filename.isEmpty())
    return;

  // get the plugin name
  // and check if Plugin is in the dontLoad List
  QPluginLoader loader( filename );
  QObject *plugin = loader.instance();
  QString name;

  // Check if a plugin has been loaded
  if (plugin) {
    // Check if it is a BasePlugin
    BaseInterface* basePlugin = qobject_cast< BaseInterface * >(plugin);
    if ( basePlugin ) {
      name = basePlugin->name();
    }else
      return;
  }else
    return;

405
  // Ask if the plugin is on the block list
406
  QStringList dontLoadPlugins = OpenFlipperSettings().value("PluginControl/DontLoadNames", QStringList()).toStringList();
407
  if (dontLoadPlugins.contains(name)){
Jan Möbius's avatar
   
Jan Möbius committed
408
409
      int ret = QMessageBox::question(0, tr("Plugin Loading Prevention"),
                   tr("OpenFlipper is currently configured to prevent loading this plugin.\n"
410
                      "Do you want to enable this plugin permanently?"),
Jan Möbius's avatar
   
Jan Möbius committed
411
412
                   QMessageBox::Yes | QMessageBox::No,
                   QMessageBox::Yes);
413
414
415
416
417
      if (ret == QMessageBox::Yes) {
        dontLoadPlugins.removeAll(name);
        OpenFlipperSettings().setValue("PluginControl/DontLoadNames",dontLoadPlugins);
      } else
         return;
Jan Möbius's avatar
   
Jan Möbius committed
418
  }
419
420
  
  // check if the plugin is not on the additional plugin list
421
  QStringList additionalPlugins = OpenFlipperSettings().value("PluginControl/AdditionalPlugins", QStringList()).toStringList();
422
423
424
425
426
427
428
429
430
431
432
433
  if (!additionalPlugins.contains(name)){
    int ret = QMessageBox::question(0, tr("Plugin Loading ..."),
                                       tr("Should OpenFlipper load this plugin on next startup?"),
                                    QMessageBox::Yes | QMessageBox::No,
                                    QMessageBox::Yes);
    if (ret == QMessageBox::Yes) {
      additionalPlugins << filename;
      std::cerr << "Added: " << filename.toStdString() << std::endl;
      OpenFlipperSettings().setValue("PluginControl/AdditionalPlugins",additionalPlugins);
    }
  }
  
Jan Möbius's avatar
Jan Möbius committed
434
435
436
437
  QString licenseText = "";
  loadPlugin(filename,false,licenseText);
  
  if ( licenseText != "" ) {
Jan Möbius's avatar
Jan Möbius committed
438
    if ( OpenFlipper::Options::gui() ) {
Jan Möbius's avatar
Jan Möbius committed
439
      QMessageBox::warning ( 0, tr("Plugin License check failed"),  licenseText );
Jan Möbius's avatar
Jan Möbius committed
440
441
442
443
      
      std::cerr << "OpenURL: " << std::endl;
      QDesktopServices::openUrl(QUrl(tr("mailto:contact@openflipper.com?subject=License Request&body=%1").arg(licenseText), QUrl::TolerantMode));
    } else {
Jan Möbius's avatar
Jan Möbius committed
444
445
446
      std::cerr << "Plugin License check failed" << std::endl;
      std::cerr << licenseText.toStdString() << std::endl;
    }
447
  }
Jan Möbius's avatar
   
Jan Möbius committed
448
449
}

Dirk Wilden's avatar
Dirk Wilden committed
450
/** @brief slot for showing loaded Plugins
Jan Möbius's avatar
   
Jan Möbius committed
451
 */
Dirk Wilden's avatar
Dirk Wilden committed
452
void Core::slotShowPlugins(){
Jan Möbius's avatar
   
Jan Möbius committed
453

Dirk Wilden's avatar
Dirk Wilden committed
454
455
456
457
458
459
460
  if ( OpenFlipper::Options::gui() ){

    int ret = 0;

    while (ret == 0){

      PluginDialog* dialog = new PluginDialog(plugins, coreWidget_);
Jan Möbius's avatar
   
Jan Möbius committed
461

Dirk Wilden's avatar
Dirk Wilden committed
462
463
464
465
466
467
468
469
470
471
      //connect signals
      connect(dialog, SIGNAL(unloadPlugin(QString)), this, SLOT(unloadPlugin(QString)));
      connect(dialog, SIGNAL( loadPlugin() ), this, SLOT( slotLoadPlugin() ));

      //if a plugin was deleted/loaded the dialog returns 0 and it needs to be loaded again
      ret = dialog->exec();

      delete dialog;
    }
  }
Jan Möbius's avatar
   
Jan Möbius committed
472
473
474
475
476
477
478
}

/** @brief Unload a Plugin with given name (slot)
 *  @param name plugin name
 */
void Core::unloadPlugin(QString name){
  for (uint i=0; i < plugins.size(); i++)
Dirk Wilden's avatar
Dirk Wilden committed
479
    if (plugins[i].rpcName == name){
Jan Möbius's avatar
   
Jan Möbius committed
480
481
482
483
484
485
      if ( checkSlot( plugins[i].plugin , "exit()" ) )
        QMetaObject::invokeMethod(plugins[i].plugin, "exit",  Qt::DirectConnection);

      //remove toolbox widget
      QString name_nospace = name;
      name_nospace.remove(" ");
Jan Möbius's avatar
Jan Möbius committed
486
487
      if ( coreWidget_->viewModes_[0]->visibleToolboxes.contains(name_nospace) )
        coreWidget_->viewModes_[0]->visibleToolboxes.removeAt(coreWidget_->viewModes_[0]->visibleToolboxes.indexOf(name_nospace));
488
489
490
491
      for ( uint j = 0 ; j < plugins[i].toolboxWidgets.size() ; ++j )
        if (plugins[i].toolboxWidgets[j].second ){
          plugins[i].toolboxWidgets[j].second->setVisible(false);
          delete plugins[i].toolboxWidgets[j].second;
Dirk Wilden's avatar
Dirk Wilden committed
492
493
494

          if( plugins[i].toolboxIcons[j] != 0 )
            delete plugins[i].toolboxIcons[j];
495
        }
Jan Möbius's avatar
   
Jan Möbius committed
496
497
498

      plugins.erase(plugins.begin() + i);

Jan Möbius's avatar
Jan Möbius committed
499
      emit log(LOGOUT,tr("Unloaded Plugin :\t\t %1").arg( name) );
Jan Möbius's avatar
   
Jan Möbius committed
500
501
502
503

      return;
    }

Jan Möbius's avatar
Jan Möbius committed
504
    log(LOGERR, tr("Unable to unload plugin '%1' (plugin wasn't found)").arg(name));
Jan Möbius's avatar
   
Jan Möbius committed
505
506
507
508
509
}

/** @brief Load a Plugin with given filename
 *  @param filename complete path + filename of the plugin
 *  @param silent if 'true': user isn't asked what to do if a plugin is already loaded
Jan Möbius's avatar
Jan Möbius committed
510
 *  @param _licenseErrors String will be epty when no license issues occured, otherwise it contains the required information for obtaining a license
Jan Möbius's avatar
Jan Möbius committed
511
 *  @param _plugin You can provide a preloaded plugin here. If this is 0 the filename will be used to load the plugin.
Jan Möbius's avatar
   
Jan Möbius committed
512
 */
Jan Möbius's avatar
Jan Möbius committed
513
void Core::loadPlugin(QString filename, bool silent, QString& _licenseErrors, QObject* _plugin){
Jan Möbius's avatar
   
Jan Möbius committed
514

Jan Möbius's avatar
Jan Möbius committed
515
516
  _licenseErrors = "";
  
Jan Möbius's avatar
   
Jan Möbius committed
517
518
519
520
521
522
523
524
525
526
527
528
529
530
  // Only load .dll under windows
  if ( OpenFlipper::Options::isWindows() ) {
    QString dllname = filename;
    if ( ! dllname.endsWith( ".dll" ) )
      return;
  }

  // Only load .so under linux
  if ( OpenFlipper::Options::isLinux() ) {
    QString soname = filename;
    if ( ! soname.endsWith( ".so" ) )
      return;
  }

Jan Möbius's avatar
Jan Möbius committed
531
532
  // This will be the reference to our plugin
  QObject *plugin = 0;
Jan Möbius's avatar
   
Jan Möbius committed
533

Jan Möbius's avatar
Jan Möbius committed
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
  // Try to open the file if we did not get a plugin,
  // Otherwise use the supplied plugin pointer
  if ( _plugin == 0 ) {
    QPluginLoader loader( filename );
    plugin = loader.instance();  
    
    if ( !plugin) {
      emit log(LOGERR,tr("Unable to load Plugin :\t %1").arg( filename ) );
      emit log(LOGERR,tr("Error was : ") + loader.errorString() );
      emit log(LOGOUT,"================================================================================");       
    }
    
  } else {
      plugin = _plugin;
  }
  
Jan Möbius's avatar
   
Jan Möbius committed
550
  // Check if a plugin has been loaded
Jan Möbius's avatar
Jan Möbius committed
551
552
  PluginInfo info;
  QString supported;
Jan Möbius's avatar
   
Jan Möbius committed
553

Jan Möbius's avatar
Jan Möbius committed
554
  emit log(LOGOUT,tr("Location : \t %1").arg(filename) );
Jan Möbius's avatar
Jan Möbius committed
555
556
557
558
559
  
  // Check if it is a BasePlugin
  BaseInterface* basePlugin = qobject_cast< BaseInterface * >(plugin);
  if ( basePlugin ) {
    emit log(LOGOUT,tr("Found Plugin : \t %1").arg(basePlugin->name()) );
Dirk Wilden's avatar
Dirk Wilden committed
560
    emit log(LOGOUT,tr("Location : \t %2").arg(filename) );
Jan Möbius's avatar
   
Jan Möbius committed
561

Jan Möbius's avatar
Jan Möbius committed
562
563
564
565
566
    if ( OpenFlipper::Options::gui() && OpenFlipperSettings().value("Core/Gui/splash",true).toBool() ) {
      splashMessage_ = splashMessage_ + " " + basePlugin->name() ;
      splash_->showMessage( splashMessage_ , Qt::AlignBottom | Qt::AlignLeft , Qt::white);
      QApplication::processEvents();
    }
Jan Möbius's avatar
   
Jan Möbius committed
567

Jan Möbius's avatar
Jan Möbius committed
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
    //Check if plugin is already loaded
    for (uint k=0; k < plugins.size(); k++){

        QString name_nospace =  basePlugin->name();
        name_nospace.remove(" ");

        if (plugins[k].name == name_nospace){
          if (silent || OpenFlipper::Options::nogui() ){ //dont load the plugin
            emit log(LOGWARN, tr("\t\t\t Already loaded from %1").arg( plugins[k].path) );
            emit log(LOGOUT,"================================================================================");
            return;
        }else{ //ask the user
          int ret = QMessageBox::question(coreWidget_,
                                          tr("Plugin already loaded"),
                                          tr("A Plugin with the same name was already loaded from %1.\n" 
                                              "You can only load the new plugin if you unload the existing one first.\n\n"
                                              "Do you want to unload the existing plugin first?").arg( plugins[k].path),
                                          QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
          if (ret == QMessageBox::Yes)
            unloadPlugin(plugins[k].name);
          else{
            emit log(LOGWARN, tr("\t\t\t Already loaded from %1.").arg( plugins[k].path));
            emit log(LOGOUT,"================================================================================");
            return;
Jan Möbius's avatar
   
Jan Möbius committed
592
593
          }
        }
Dirk Wilden's avatar
Dirk Wilden committed
594
      }
Jan Möbius's avatar
Jan Möbius committed
595
    }
Jan Möbius's avatar
   
Jan Möbius committed
596

597
    QStringList dontLoadPlugins = OpenFlipperSettings().value("PluginControl/DontLoadNames",QStringList()).toStringList();
Jan Möbius's avatar
Jan Möbius committed
598
599
600
601
602
603
    
    if ( dontLoadPlugins.contains(basePlugin->name(), Qt::CaseInsensitive) ) {
      emit log(LOGWARN,tr("OpenFlipper.ini prevented Plugin %1 from being loaded! ").arg( basePlugin->name() ));
      emit log(LOGOUT,"================================================================================");
      return;
    }
Jan Möbius's avatar
   
Jan Möbius committed
604

Jan Möbius's avatar
Jan Möbius committed
605
606
607
608
    //Check if it is a BasePlugin
    SecurityInterface * securePlugin = qobject_cast< SecurityInterface * >(plugin);
    if ( securePlugin ) {
      emit log(LOGINFO,tr("Plugin uses security interface. Trying to authenticate against plugin ..."));
609

Jan Möbius's avatar
Jan Möbius committed
610
      bool success = false;
611
612
613
614
615
      QMetaObject::invokeMethod(plugin,"authenticate", Q_RETURN_ARG( bool , success ) ) ;
      
      QString message = "";
      QMetaObject::invokeMethod(plugin,"licenseError", Q_RETURN_ARG( QString , message ) ) ;
      _licenseErrors = message;
616

Jan Möbius's avatar
Jan Möbius committed
617
618
619
620
621
      if ( success )
        emit log(LOGINFO,tr("... ok. Loading plugin "));
      else {
        emit log(LOGERR,tr("... failed. Plugin access denied."));
        emit log(LOGOUT,"================================================================================");
Jan Möbius's avatar
Jan Möbius committed
622
623
        
        // Abort here, as the plugin will not do anything else until correct authentication.
Jan Möbius's avatar
Jan Möbius committed
624
        return;
625
      }
Jan Möbius's avatar
Jan Möbius committed
626
    }
627
628


Jan Möbius's avatar
Jan Möbius committed
629
    emit log(LOGOUT,tr("Plugin Desciption :\t %1 ").arg( basePlugin->description()) );
630

Jan Möbius's avatar
Jan Möbius committed
631
    supported = "BaseInterface ";
Jan Möbius's avatar
   
Jan Möbius committed
632

Jan Möbius's avatar
Jan Möbius committed
633
634
635
636
    info.name          = basePlugin->name();
    info.description   = basePlugin->description();
    info.plugin        = plugin;
    info.path          = filename;
Jan Möbius's avatar
   
Jan Möbius committed
637

Jan Möbius's avatar
Jan Möbius committed
638
639
640
641
    if ( checkSlot(plugin,"version()") )
      info.version = basePlugin->version();
    else
      info.version = QString::number(-1);
Jan Möbius's avatar
   
Jan Möbius committed
642

Jan Möbius's avatar
Jan Möbius committed
643
    if ( OpenFlipper::Options::nogui() ) {
Jan Möbius's avatar
   
Jan Möbius committed
644

Jan Möbius's avatar
Jan Möbius committed
645
646
      if ( ! checkSlot( plugin , "noguiSupported()" ) ) {
        emit log(LOGWARN,tr("Running in nogui mode which is unsupported by this plugin, skipping") );
647
        emit log(LOGOUT,"================================================================================");
648
649
650
        return;
      }

Jan Möbius's avatar
Jan Möbius committed
651
    }
652
653


Jan Möbius's avatar
Jan Möbius committed
654
655
656
657
658
659
    // Check for baseInterface of old style!
    if ( checkSignal(plugin,"updated_objects(int)") ) {
      log(LOGERR,tr("Plugin Uses old style updated_objects! Convert to updatedObject!"));
      emit log(LOGOUT,"================================================================================");
      return;
    }
660

Jan Möbius's avatar
Jan Möbius committed
661
662
663
664
665
    if ( checkSignal(plugin,"update_view()") ) {
      log(LOGERR,tr("Plugin Uses old style update_view! Convert to updateView!"));
      emit log(LOGOUT,"================================================================================");
      return;
    }
666

Jan Möbius's avatar
Jan Möbius committed
667
668
    if ( checkSignal(plugin,"updateView()") )
      connect(plugin,SIGNAL(updateView()),this,SLOT(updateView()));
Dirk Wilden's avatar
Dirk Wilden committed
669

670

Jan Möbius's avatar
Jan Möbius committed
671
    if ( checkSignal(plugin,"updatedObject(int)") && checkSignal(plugin,"updatedObject(int,const UpdateType)") ){
Dirk Wilden's avatar
Dirk Wilden committed
672
      
Jan Möbius's avatar
Jan Möbius committed
673
674
675
676
      log(LOGERR,tr("Plugin uses deprecated and(!) new updatedObject. Only new updatedObject will be active."));
      connect(plugin,SIGNAL(updatedObject(int,const UpdateType)),this,SLOT(slotObjectUpdated(int,const UpdateType)), Qt::DirectConnection);
      
    } else {
677

Jan Möbius's avatar
Jan Möbius committed
678
679
680
      if ( checkSignal(plugin,"updatedObject(int)") ){
        log(LOGWARN,tr("Plugin uses deprecated updatedObject."));
        connect(plugin,SIGNAL(updatedObject(int)),this,SLOT(slotObjectUpdated(int)), Qt::DirectConnection);
681
      }