1 2 3 4 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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 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 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
|
/*****************************************************************************
* $CAMITK_LICENCE_BEGIN$
*
* CamiTK - Computer Assisted Medical Intervention ToolKit
* (c) 2001-2014 UJF-Grenoble 1, CNRS, TIMC-IMAG UMR 5525 (GMCAO)
*
* Visit http://camitk.imag.fr for more information
*
* This file is part of CamiTK.
*
* CamiTK is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3
* only, as published by the Free Software Foundation.
*
* CamiTK 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 version 3 for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* version 3 along with CamiTK. If not, see <http://www.gnu.org/licenses/>.
*
* $CAMITK_LICENCE_END$
****************************************************************************/
// -- Core stuff
#include "ExtensionManager.h"
#include "Core.h"
#include "Log.h"
#include "Application.h"
#include "Action.h"
// -- QT stuff
#include <QDir>
#include <QtGui>
#include <QSettings>
#include <QApplication>
namespace camitk {
// -------------------- autoload --------------------
void ExtensionManager::autoload() {
// add the private lib dir to the current application environment's PATH variable
// This modifies the current PATH variable in order for the OS to find the private
// libraries compiled as shared objects/dll/dylib needed by some extensions
QDir privateLibDir(Core::getGlobalInstallDir());
if (privateLibDir.cd("lib/"+QString(Core::shortVersion))) {
QByteArray privateLibDirPath = privateLibDir.canonicalPath().toUtf8();
#ifdef WIN32
// update path for windows
QByteArray path = qgetenv("PATH");
path.append(";");
path.append(privateLibDirPath);
qputenv("PATH",path);
#endif
// update the application / qt plugins library path
Application::instance()->addLibraryPath(privateLibDirPath);
}
autoload(COMPONENT);
autoload(ACTION);
}
void ExtensionManager::autoload(ExtensionManager::ExtensionType type) {
QStringList extensionDir;
switch(type) {
case ACTION:
extensionDir = Core::getActionDirectories();
break;
case COMPONENT:
extensionDir = Core::getComponentDirectories();
break;
default:
CAMITK_DEBUG("ExtensionManager", "autoload of unknown type", "type should be either ACTION or COMPONENT, other extensions are not implemented yet");
break;
}
//-- remove duplicates
QStringList extensionFileNames;
QStringList extensionAbsoluteFileNames;
QMap<QString, QDir> extensionUniqueDir;
foreach(QString dirName, extensionDir)
{
QDir dir(dirName);
QStringList pluginFileNames = getPluginFileNames(dir);
foreach(QString pluginFile, pluginFileNames)
{
QString pluginAbsoluteFileName = dir.absoluteFilePath(pluginFile);
if (!extensionFileNames.contains(pluginFile))
{
extensionFileNames.append(pluginFile);
extensionUniqueDir.insert(pluginFile,dir);
extensionAbsoluteFileNames.append(pluginAbsoluteFileName);
}
else
{
CAMITK_INFO("ExtensionManager", "autoload", "Warning: duplicate extension: " << pluginFile.toStdString() << " found in " << dir.absolutePath().toStdString() << ".\nUsing extension in " << extensionUniqueDir.value(pluginFile).absolutePath().toStdString() << " instead (higher priority)");
}
}
}
//-- load all extensions, try five times to avoid error for internal dependencies
// if there is more than 5 dependency levels, then you have two choices:
// - consider simplifying your component
// - increase maxNumberOfTries
int maxNumberOfTries = 5;
int tryNr = 0;
do {
QMutableListIterator<QString> it(extensionAbsoluteFileNames);
while (it.hasNext()) {
QString extFileName = it.next();
if (loadExtension(type,extFileName)) {
// this one is loaded, remove it from the list to load
it.remove();
}
}
tryNr++;
} while (tryNr < maxNumberOfTries && extensionAbsoluteFileNames.size() > 0);
//-- manage loading errors
if (extensionAbsoluteFileNames.size() > 0) {
// get the messages from Qt
QStringList errorStrings;
foreach(QString fileName, extensionAbsoluteFileNames) {
QPluginLoader pluginLoader(fileName);
QObject *plugin = pluginLoader.instance();
if (!plugin) {
errorStrings << QString("Plugin " + fileName + ", error: " + pluginLoader.errorString());
}
else {
errorStrings << QString("Plugin " + fileName + ", no error (you need to decrease your number of dependency level, or contact the CamiTK SDK developer team)");
}
}
QMessageBox::warning(NULL, "ExtensionManager Opening Error...",
"AutoLoad plugin failed after " + QString::number(tryNr) + " tries for the following extension(s):<ul><li>"
+ errorStrings.join("</li><li>")
+ "</li></ul>"
+ "List of library paths:<ul><li>"
+ Application::instance()->libraryPaths().join("</li><li>")
+ "</li></ul>"
+ "List of path:<ul><li>"
#ifdef WIN32
+ QString(qgetenv("PATH")).split(":").join("</li><li>")
#else
+ QString(qgetenv("LD_LIBRARY_PATH")).split(":").join("</li><li>")
#endif
+ "</li></ul>");
}
//-- load user extensions
QSettings & settings = Application::getSettings();
settings.beginGroup ( "UserExtensions" );
QStringList userRegisteredExtensions;
switch(type) {
case ACTION:
userRegisteredExtensions = settings.value("actions", QVariant(QStringList())).toStringList();
break;
case COMPONENT:
userRegisteredExtensions = settings.value("components", QVariant(QStringList())).toStringList();
break;
default:
break;
}
settings.endGroup();
foreach(QString userRegisteredExtensionFile, userRegisteredExtensions) {
loadExtension(type, userRegisteredExtensionFile);
}
}
// -------------------- loadExtension --------------------
bool ExtensionManager::loadExtension(ExtensionManager::ExtensionType type, QString fileName) {
bool returnValue = false;
QPluginLoader pluginLoader(fileName);
QObject *extension = pluginLoader.instance();
if (extension) {
switch(type) {
case ACTION: {
ActionExtension *ext = qobject_cast<ActionExtension *> (extension);
if (ext) {
//-- register the filename
getActionExtensionMap().insert(fileName, ext);
// initialize all actions
ext->init();
//-- register all actions
Application::registerAllActions(ext);
returnValue = true;
}
}
break;
case COMPONENT: {
ComponentExtension *cp = qobject_cast<ComponentExtension *> (extension);
if (cp) {
cp->setLocation(fileName);
//-- insert the ComponentExtension plugin in the application wide list
if (cp->hasDataDirectory()) {
getDataDirectoryComponentExtensionMap().insert(cp->getName(), cp);
}
else {
// (cannot do that in the constructor because the virtual symbol table seems to be confused!)
foreach(QString ext, cp->getFileExtensions()) {
getComponentExtensionMap().insert(ext, cp);
}
}
returnValue = true;
}
}
break;
default:
CAMITK_DEBUG("ExtensionManager", "loadExtension of unknown type", "type should be either ACTION or COMPONENT, other extensions are not implemented yet");
break;
}
}
else {
pluginLoader.unload(); // to make sure we could try again later
// try to load the missing shared object directly from private dir (give absolute path)
QRegExp libname("\\((lib.*): cannot open shared object file");
if (libname.indexIn(pluginLoader.errorString(), 0)!= -1) {
QString privateLibToLoad=Core::getGlobalInstallDir() + "/lib/" + QString(Core::shortVersion) + "/" + libname.cap(1);
QLibrary privateLib(privateLibToLoad);
privateLib.load();
}
}
return returnValue;
}
// -------------------- getInstallationString --------------------
QString ExtensionManager::getInstallationString(QString file) {
QString whichInstallDir;
QDir dir(QFileInfo(file).absolutePath());
// go up three levels (one for the extension name, one for the camitk short version name, one for the lib name)
dir.cdUp();
dir.cdUp();
dir.cdUp();
QString absolutePath = dir.absolutePath();
if (absolutePath == Core::getGlobalInstallDir()) {
whichInstallDir = "[G]";
}
else if (absolutePath == Core::getUserInstallDir()) {
whichInstallDir = "[L]";
}
else if (absolutePath == Core::getCurrentWorkingDir()) {
whichInstallDir = "[W]";
}
else {
whichInstallDir = "[U]";
}
return whichInstallDir;
}
// -------------------- getComponentExtensionMap --------------------
QMap< QString, ComponentExtension* > & ExtensionManager::getComponentExtensionMap() {
static QMap<QString, ComponentExtension*> componentExtensionMap;
return componentExtensionMap;
}
// -------------------- getDataDirectoryComponentExtensionMap --------------------
QMap< QString, ComponentExtension* > & ExtensionManager::getDataDirectoryComponentExtensionMap() {
static QMap<QString, ComponentExtension*> dataDirectoryComponentExtensionMap;
return dataDirectoryComponentExtensionMap;
}
// -------------------- getComponentExtension --------------------
const ComponentExtension * ExtensionManager::getComponentExtension(QString extOrName) {
ComponentExtension *cp = getComponentExtensionMap().value(extOrName);
if (!cp) {
cp = getDataDirectoryComponentExtensionMap().value(extOrName);
if (!cp) {
// look for the name in getComponentExtensionMap()
QMapIterator<QString, ComponentExtension *> it(getComponentExtensionMap());
while (it.hasNext() && !cp) {
it.next();
if (it.value()->getName() == extOrName)
cp = it.value();
}
}
}
return cp;
}
// -------------------- getComponentExtensions --------------------
const QMap<QString, ComponentExtension*> & ExtensionManager::getComponentExtensions() {
return getComponentExtensionMap();
}
// -------------------- getDataDirectoryComponents --------------------
const QMap<QString, ComponentExtension*> & ExtensionManager::getDataDirectoryComponents() {
return getDataDirectoryComponentExtensionMap();
}
// -------------------- getFileExtensions --------------------
QStringList ExtensionManager::getFileExtensions() {
return ExtensionManager::getComponentExtensions().keys();
}
// -------------------- getDataDirectoryExtNames --------------------
QStringList ExtensionManager::getDataDirectoryExtNames() {
return ExtensionManager::getDataDirectoryComponents().keys();
}
// -------------------- getActionExtensionMap --------------------
QMap<QString, ActionExtension*> & ExtensionManager::getActionExtensionMap() {
static QMap<QString, ActionExtension*> actionExtensionMap;
return actionExtensionMap;
}
// -------------------- getActionExtensions --------------------
const QMap< QString, ActionExtension* >& ExtensionManager::getActionExtensions() {
return getActionExtensionMap();
}
// -------------------- unloadAllActionExtensions --------------------
void ExtensionManager::unloadAllActionExtensions() {
QList<QString> allExtensions = getActionExtensionMap().keys();
while (!allExtensions.isEmpty())
unloadActionExtension(allExtensions.takeFirst());
}
// -------------------- registerFileExtension --------------------
void ExtensionManager::registerFileExtension(QString fileExtension) {
// we get the application name and its binary path for the association
QString appName = QApplication::applicationName(); // used for windows registry
QString appFilePath = QApplication::applicationFilePath(); // needed for associating file for opening command
#ifdef WIN32
// WINDOWS ONLY
appFilePath.replace("/", "\\");
// Associate the file extension with the current application for opening
// Store that information in the windows registery
QSettings* registry = new QSettings("HKEY_CURRENT_USER\\Software\\Classes\\camitk-" + appName + "." + fileExtension, QSettings::NativeFormat);
registry->setValue("Default", "CamiTK Image file");
registry = new QSettings("HKEY_CURRENT_USER\\Software\\Classes\\camitk-" + appName + "." + fileExtension + "\\DefaultIcon", QSettings::NativeFormat);
registry->setValue("Default", "\"" + appFilePath + "\", 0");
registry = new QSettings("HKEY_CURRENT_USER\\Software\\Classes\\camitk-" + appName + "." + fileExtension + "\\shell\\Open\\command", QSettings::NativeFormat);
registry->setValue("Default", "\"" + appFilePath + "\" %1");
registry = new QSettings("HKEY_CURRENT_USER\\Software\\Classes\\." + fileExtension + "\\ShellNew", QSettings::NativeFormat);
registry->setValue("Default", "");
registry = new QSettings("HKEY_CURRENT_USER\\Software\\Classes\\." + fileExtension, QSettings::NativeFormat);
registry->setValue("Default", "camitk-" + appName + "." + fileExtension);
#endif
// TODO : associate file opening on Linux & MacOS
}
// -------------------- promptRegisterFileExtensions --------------------
bool ExtensionManager::promptRegisterFileExtensions(QStringList fileExtensions)
{
// Prompt the user if he wishes those file extension to be associated with this application for default file opening.
QMessageBox msgBox;
msgBox.setWindowTitle(tr("Associate new file extensions for opening."));
msgBox.setText(tr("New component extension(s) allow(s) this application to handle new file type(s) : \n\n")
+ fileExtensions.join(", ") + "\n\n"
+ tr("Do you want this/these file type(s) to be opened by default with this application")
+ " (" + QApplication::applicationName() + ") ?\n");
msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
msgBox.setDefaultButton(QMessageBox::Yes);
return (msgBox.exec() == QMessageBox::Yes);
}
// -------------------- getExtensionFilter --------------------
QStringList ExtensionManager::getExtensionFilter() {
// try to load all Components in the directory
QStringList pluginFilter;
// linux: .so, windows: .dll, macOs: .dylib
pluginFilter << "*.so." + QString(Core::soVersion) << "*.dylib";
if (Core::isDebugBuild())
pluginFilter << "*" + QString(Core::debugPostfix) +".dll";
else
pluginFilter << "*.dll";
return pluginFilter;
}
// -------------------- getPluginFileNames --------------------
QStringList ExtensionManager::getPluginFileNames(QDir extensionsDir) {
// loop to load component plugin, taking into account internal dependencies (i.e. dependency between
// one component and another one.
QStringList pluginFileNames = extensionsDir.entryList(getExtensionFilter(), QDir::Files);
#ifdef WIN32
// Get the MSVC debug dlls
QStringList pluginFileNamesDebugMSVC = pluginFileNames.filter(QRegExp(".*"+QString(Core::debugPostfix)+".dll"));
if (Core::isDebugBuild()) {
return pluginFileNamesDebugMSVC;
}
else {
// remove debug dll one by one
foreach(QString debugDLL, pluginFileNamesDebugMSVC) {
pluginFileNames.removeAll(debugDLL);
}
return pluginFileNames;
}
#endif
return pluginFileNames;
}
// -------------------- unloadActionExtension --------------------
bool ExtensionManager::unloadActionExtension(QString fileName) {
if (getActionExtensionMap().contains(fileName)) {
ActionExtension *ext = getActionExtensionMap().value(fileName);
//-- unregister all actions
foreach(Action *action, ext->getActions()) {
getActionExtensionMap().remove(action->getName());
}
//-- unregister extension
getActionExtensionMap().remove(fileName);
//-- delete extensions (and all its actions)
delete ext;
return true;
}
else
return false;
}
// -------------------- unloadComponentExtension --------------------
bool ExtensionManager::unloadComponentExtension(QString extOrName) {
bool unloaded = false;
// remove from the application wide list
ComponentExtension * cp = getComponentExtensionMap().take(extOrName);
if (cp != NULL) {
// remove all the other extensions
QMutableMapIterator<QString, ComponentExtension *> it(getComponentExtensionMap());
while (it.hasNext()) {
it.next();
if (it.value() == cp) {
getComponentExtensionMap().take(it.key());
}
}
// open the plugin
QPluginLoader pluginLoader(cp->getLocation());
// delete the pointer
delete cp;
// try to unload
unloaded = pluginLoader.unload();
}
else {
cp = getDataDirectoryComponentExtensionMap().take(extOrName);
if (cp != NULL) {
// open the plugin
QPluginLoader pluginLoader(cp->getLocation());
// delete the pointer
delete cp;
// try to unload
unloaded = pluginLoader.unload();
}
else {
// look for the name in getComponentExtensionMap()
QMutableMapIterator<QString, ComponentExtension *> it(getComponentExtensionMap());
while (it.hasNext()) {
it.next();
if (it.value()->getName() == extOrName)
return unloadComponentExtension(it.value()->getName());
}
}
}
return unloaded;
}
// ----------------------------------------------------------------
//
// OBSOLETE PUBLIC METHODS
//
// TODO CAMITK_DEPRECATED. This section list all the methods marked as deprecated.
// They are to be removed in CamiTK 4.0
// ----------------------------------------------------------------
// -------------------- loadComponentExtension --------------------
bool ExtensionManager::loadComponentExtension(QString fileName) {
return loadExtension(COMPONENT,fileName);
}
// -------------------- loadActionExtension --------------------
bool ExtensionManager::loadActionExtension(QString fileName) {
return loadExtension(ACTION,fileName);
}
// -------------------- autoloadComponentExtensions --------------------
void ExtensionManager::autoloadComponentExtensions() {
autoload(COMPONENT);
}
// -------------------- autoloadActionExtensions --------------------
void ExtensionManager::autoloadActionExtensions() {
autoload(ACTION);
}
}
|