pluginmanager.cpp 10.7 KB
Newer Older
victor's avatar
 
victor committed
1
#include "pluginmanager.h"
Victor Yacovlev's avatar
Victor Yacovlev committed
2
#include "pluginmanager_impl.h"
victor's avatar
 
victor committed
3 4
#include "kplugin.h"

Alexander A. Maly's avatar
Alexander A. Maly committed
5
#include <QCoreApplication>
Victor Yacovlev's avatar
Victor Yacovlev committed
6 7 8 9 10 11
#if QT_VERSION >= 0x050000
#include <QStandardPaths>
#else
#include <QDesktopServices>
#endif

12 13
namespace ExtensionSystem
{
victor's avatar
 
victor committed
14

Victor Yacovlev's avatar
Victor Yacovlev committed
15 16 17
const QString PluginManager::WorkspacesListKey = "WorkspacesList";
const QString PluginManager::CurrentWorkspaceKey = "CurrentWorkspace";
const QString PluginManager::SkipChooseWorkspaceKey = "SkipChooseWorkspace";
victor's avatar
 
victor committed
18

19

victor's avatar
 
victor committed
20
PluginManager::PluginManager()
21 22
	: QObject()
	, pImpl_(new PluginManagerImpl)
victor's avatar
 
victor committed
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
	pImpl_->globalState = Shared::PluginInterface::GS_Unlocked;
	pImpl_->mySettings = SettingsPtr(new Settings("ExtensionSystem"));

	int unnamedArgumentsIndexBegin = 1;

	QStringList arguments = QCoreApplication::instance()->arguments();

	for (int i = 1; i < arguments.size(); i++) {
		const QString &arg = arguments[i];
		if (arg.startsWith("-")) {
			if (arg != "--debug" && !arg.startsWith("--log=")) {
				pImpl_->namedProgramArguments.push_back(arg);
			}
		} else {
			unnamedArgumentsIndexBegin = i;
			break;
		}
	}

	if (unnamedArgumentsIndexBegin) {
		for (int i = unnamedArgumentsIndexBegin; i < arguments.size(); i++) {
			const QString &arg = arguments[i];
			if (!arg.startsWith("[") && !arg.startsWith("-")) {
				pImpl_->unnamedProgramArguments.push_back(arg);
			}
		}
	}
51 52

#ifdef Q_WS_X11
53 54 55 56
	bool gui = getenv("DISPLAY") != 0;
	if (!gui) {
		return;
	}
57
#endif
victor's avatar
 
victor committed
58 59
}

60 61 62

void PluginManager::setupAdditionalPluginPaths()
{
63
	QString homeLocation;
64
#if QT_VERSION >= 0x050000
65
	homeLocation = QStandardPaths::standardLocations(QStandardPaths::HomeLocation).first();
66
#else
67
	homeLocation = QDesktopServices::storageLocation(QDesktopServices::HomeLocation);
68
#endif
69 70 71 72 73 74 75
	pImpl_->additionalPluginPrefixes = QStringList()
		<< "/usr/"
		<< "/opt/kumir2/"
		<< "/opt/kumir/"
		<< "/usr/local/"
		<< homeLocation + "/.local/"
		;
76 77
}

victor's avatar
 
victor committed
78
PluginManager::~PluginManager()
victor's avatar
 
victor committed
79
{
victor's avatar
 
victor committed
80

victor's avatar
 
victor committed
81 82
}

83 84
bool PluginManager::isPluginLoaded(const QByteArray &name) const
{
85
	return pImpl_->isPluginLoaded(name);
86 87
}

88 89
void PluginManager::destroy()
{
90 91 92 93 94
	PluginManager *pm = instance();
	foreach (KPlugin *o, pm->pImpl_->objects) {
		delete o;
	}
	pm->pImpl_->objects.clear();
95 96
}

Victor Yacovlev's avatar
Victor Yacovlev committed
97
SettingsPtr PluginManager::globalSettings() const
victor's avatar
 
victor committed
98
{
99
	return pImpl_->mySettings;
victor's avatar
 
victor committed
100 101
}

Victor Yacovlev's avatar
Victor Yacovlev committed
102
void PluginManager::updateAllSettings()
victor's avatar
 
victor committed
103
{
104 105 106
	foreach (KPlugin *o, pImpl_->objects) {
		o->updateSettings(QStringList());
	}
victor's avatar
 
victor committed
107 108
}

Victor Yacovlev's avatar
Victor Yacovlev committed
109

110
void PluginManager::switchToDefaultWorkspace(bool workDirOnly)
victor's avatar
 
victor committed
111
{
112 113 114 115
	const QString workDir = pImpl_->mySettings->value(
			CurrentWorkspaceKey,
			QString(QDir::homePath() + "/Kumir/")).toString();
	pImpl_->changeWorkingDirectory(workDir, false, workDirOnly);
victor's avatar
 
victor committed
116 117
}

118
void PluginManager::switchToWorkspace(const QString &path, bool workDirOnly)
victor's avatar
 
victor committed
119
{
120
	pImpl_->changeWorkingDirectory(path, true, workDirOnly);
victor's avatar
 
victor committed
121 122 123
}


victor's avatar
 
victor committed
124 125
void PluginManager::setPluginPath(const QString &path)
{
126
	pImpl_->path = path;
127
#ifdef Q_OS_LINUX
128
	setupAdditionalPluginPaths();
129
#endif
victor's avatar
 
victor committed
130 131 132 133
}

void PluginManager::setSharePath(const QString &path)
{
134
	pImpl_->sharePath = path;
victor's avatar
 
victor committed
135 136 137
}


138 139
QString PluginManager::sharePath() const
{
140
	return pImpl_->sharePath;
141 142
}

victor's avatar
 
victor committed
143

144

victor's avatar
 
victor committed
145

146
QString PluginManager::loadPluginsByTemplate(const QByteArray &templ)
victor's avatar
 
victor committed
147
{
148
	QList<PluginSpec> requests;
149
	QString error = pImpl_->parsePluginsRequest(templ, requests);
150 151 152 153 154 155 156 157 158 159 160
	if (!error.isEmpty()) {
		return error;
	}

	Q_FOREACH (PluginSpec spec, requests) {
		error = pImpl_->loadPlugin(spec, requests);
		if (!error.isEmpty()) {
			return error;
		}
	}
	return "";
victor's avatar
 
victor committed
161 162
}

163 164
QString PluginManager::loadExtraModule(const std::string &canonicalFileName)
{
165 166
	Q_UNUSED(canonicalFileName);
	return "Not implemented yet";
167 168
}

victor's avatar
 
victor committed
169 170
bool PluginManager::isGuiRequired() const
{
171 172 173 174
	if (pImpl_->objects.isEmpty()) {
		return false;
	}

175 176 177 178 179 180
	KPlugin *runtimePlugin = qobject_cast<KPlugin *>(pImpl_->objects.last());
	if (runtimePlugin) {
		return runtimePlugin->isGuiRequired();
	} else {
		return false;
	}
victor's avatar
 
victor committed
181 182
}

183 184
QString PluginManager::initializePlugins()
{
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
	QString error;

	// Initialize direct dependencies of startup module, but not startup itself
	KPlugin *entryPoint = loadedPlugin(pImpl_->mainPluginName);
	const PluginSpec &spec = entryPoint->pluginSpec();
	QList<KPlugin *> directEntryPointDeps;
	Q_FOREACH (const QByteArray &depName, spec.dependencies) {
		Q_FOREACH (KPlugin *anotherPlugin, pImpl_->objects) {
			const PluginSpec &anotherSpec = anotherPlugin->pluginSpec();
			if (anotherSpec.name == depName || anotherSpec.provides.contains(depName)) {
				directEntryPointDeps.append(anotherPlugin);
			}
		}
	}
	Q_FOREACH (KPlugin *dep, directEntryPointDeps) {
		error = pImpl_->initializePlugin(dep);
		if (error.length() > 0) {
			return error;
		}
	}

	// Initialize rest of plugins, but not startup
	Q_FOREACH (KPlugin *plugin, pImpl_->objects) {
		error = pImpl_->initializePlugin(plugin);
		if (error.length() > 0) {
			return error;
		}
	}

	// Initialize startup module itself
	error = pImpl_->initializePlugin(entryPoint);
	if (error.length() > 0) {
		return error;
	}

	return "";
221 222
}

223 224
QString PluginManager::commandLineHelp() const
{
225 226 227 228 229 230 231
	PluginSpec mainSpec;
	Q_FOREACH (KPlugin *plugin, pImpl_->objects) {
		if (plugin->pluginSpec().main) {
			mainSpec = plugin->pluginSpec();
			break;
		}
	}
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
	bool guiMode = mainSpec.gui;
	QString result = tr("Usage:\n");
	QString programName = QCoreApplication::applicationFilePath();
	if (programName.startsWith(QCoreApplication::applicationDirPath())) {
		programName.remove(0, QCoreApplication::applicationDirPath().length() + 1);
	}
	QString shortLine = "  " + programName + " ";
	QString longLine = "  " + programName + " ";
	QStringList details;
	foreach (const KPlugin *plugin, pImpl_->objects) {
		const QList<CommandLineParameter> params = plugin->acceptableCommandLineParameters();
		foreach (const CommandLineParameter &param, params) {
			bool allowThisParam = guiMode ? param.allowInGui_ : true;
			if (allowThisParam && param.longName_.length() > 0) {
				if (!param.valueRequired_) {
					shortLine += "[";
					longLine += "[";
				}
				shortLine += "-" + QString(param.shortName_);
				longLine += "--" + QString(param.longName_);
				if (param.acceptValue_) {
					QString a = "=<";
					if (param.acceptType_ == QVariant::Int) {
						a += tr("int");
					} else if (param.acceptType_ == QVariant::Double) {
						a += tr("real");
					} else if (param.acceptType_ == QVariant::Bool) {
						a += tr("bool");
					} else if (param.acceptType_ == QVariant::Char) {
						a += tr("char");
					} else {
						a += tr("string");
					}
					a += ">";
					shortLine += a;
					longLine += a;
				}
				if (!param.valueRequired_) {
					shortLine += "]";
					longLine += "]";
				}
				shortLine += " ";
				longLine += " ";
				details.push_back(param.toHelpLine());
			}
		}
	}
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302

	if (!pImpl_->objects.isEmpty()) {
		QList<CommandLineParameter> params = pImpl_->objects.last()->acceptableCommandLineParameters();
		foreach (const CommandLineParameter &param, params) {
			if (param.shortDescription_.length() > 0) {
				if (param.valueRequired_) {
					shortLine += param.shortDescription_;
					longLine += param.shortDescription_;
				} else {
					shortLine += "[";
					longLine += "[";
					shortLine += param.shortDescription_.arg(1);
					longLine += param.shortDescription_.arg(1);
					shortLine += "]...[";
					longLine += "]...[";
					shortLine += param.shortDescription_.arg("n");
					longLine += param.shortDescription_.arg("n");
					shortLine += "]";
					longLine += "]";
				}
				shortLine += " ";
				longLine += " ";
				details.push_back(param.toHelpLine());
303
			}
304 305 306 307 308 309 310 311
		}
		if (longLine == shortLine) {
			result += shortLine + "\n";
		} else {
			result += longLine + "\n" + tr("or") + "\n" + shortLine + "\n";
		}
		if (details.size() > 0) {
			result += "\n" + tr("Options") + ":\n" + details.join("\n") + "\n";
312 313
		}
	}
314

315
	return result;
316 317
}

318 319
QFont PluginManager::initialApplicationFont() const
{
320
	return pImpl_->initialApplicationFont;
321 322
}

323 324
QDir PluginManager::findSystemResourcesDir(const QString &subdirShouldExists) const
{
325
	QDir result = QDir(sharePath());
326
#ifdef Q_OS_LINUX
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
	const QString homePath = QString::fromLocal8Bit(::getenv("HOME"));
	const QStringList extraPaths = QStringList()
		<< "/usr/share/kumir2/"
		<< "/usr/local/share/kumir2/"
		<< "/opt/kumir2/share/"
		<< "/opt/kumir/share/"
		<< homePath + "/.local/share/kumir2/"
		<< QDir::currentPath() + "/resources/"
		<< QDir::currentPath() + "/data/"
		;

	Q_FOREACH (const QString &path, extraPaths) {
		QDir candidate(path + subdirShouldExists);
		if (candidate.exists()) {
			return QDir(path);
		}
	}
344
#else
345
	Q_UNUSED(subdirShouldExists);
346
#endif
347
	return result;
348 349
}

350

351
QList<const KPlugin *> PluginManager::loadedConstPlugins(const QByteArray &pattern) const
victor's avatar
 
victor committed
352
{
353 354 355 356 357 358 359 360 361
	QList<const KPlugin *> result;
	const QRegExp rx = QRegExp(pattern, Qt::CaseSensitive, QRegExp::Wildcard);
	Q_FOREACH (const KPlugin *p, pImpl_->objects) {
		const PluginSpec &spec = p->pluginSpec();
		if (pattern.length() == 0 || (pattern.length() > 0 && rx.exactMatch(spec.name))) {
			result.append(p);
		}
	}
	return result;
victor's avatar
 
victor committed
362
}
victor's avatar
 
victor committed
363

364
QList<KPlugin *> PluginManager::loadedPlugins(const QByteArray &pattern)
victor's avatar
Painter  
victor committed
365
{
366 367 368 369 370 371 372 373 374
	QList<KPlugin *> result;
	const QRegExp rx = QRegExp(pattern, Qt::CaseSensitive, QRegExp::Wildcard);
	Q_FOREACH (KPlugin *p, pImpl_->objects) {
		const PluginSpec &spec = p->pluginSpec();
		if (pattern.length() == 0 || (pattern.length() > 0 && rx.exactMatch(spec.name))) {
			result.append(p);
		}
	}
	return result;
victor's avatar
Painter  
victor committed
375 376
}

377
KPlugin *PluginManager::loadedPlugin(const QByteArray &name)
victor's avatar
 
victor committed
378
{
379 380 381 382 383 384 385
	Q_FOREACH (KPlugin *p, pImpl_->objects) {
		const PluginSpec &spec = p->pluginSpec();
		if (spec.name == name) {
			return p;
		}
	}
	return 0;
victor's avatar
 
victor committed
386 387
}

388
KPlugin *PluginManager::startupModule()
victor's avatar
 
victor committed
389
{
390
	return loadedPlugin(pImpl_->mainPluginName);
victor's avatar
 
victor committed
391 392
}

victor's avatar
 
victor committed
393 394
QString PluginManager::start()
{
395 396 397 398
	KPlugin *p = startupModule();
	p->start();
	p->_state = KPlugin::Started;
	return "";
victor's avatar
 
victor committed
399 400 401
}


402
KPlugin *PluginManager::dependentPlugin(const QByteArray &name, const KPlugin *p) const
victor's avatar
 
victor committed
403
{
404 405 406 407 408 409 410 411
	Q_UNUSED(p);
	Q_FOREACH (KPlugin *anotherPlugin, pImpl_->objects) {
		const PluginSpec &anotherSpec = anotherPlugin->pluginSpec();
		if (anotherSpec.name == name || anotherSpec.provides.contains(name)) {
			return anotherPlugin;
		}
	}
	return 0;
victor's avatar
 
victor committed
412 413 414
}


415
PluginManager *PluginManager::instance()
victor's avatar
 
victor committed
416
{
417 418
	static PluginManager INSTANCE;
	return &INSTANCE;
victor's avatar
 
victor committed
419 420
}

421
bool PluginManager::shutdown()
victor's avatar
 
victor committed
422
{
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
	Q_FOREACH (KPlugin *plugin, pImpl_->objects) {
		bool moduleSafeToQuit = plugin->isSafeToQuit();
		if (!moduleSafeToQuit) {
			return false;
		}
	}

	Q_FOREACH (KPlugin *plugin, pImpl_->objects) {
		SettingsPtr s = plugin->mySettings();
		if (s) {
			plugin->saveSession();
			s->flush();
		}
		plugin->stop();
		plugin->_state = KPlugin::Stopped;
	}

	return true;
victor's avatar
 
victor committed
441 442
}

Victor Yacovlev's avatar
Victor Yacovlev committed
443

victor's avatar
 
victor committed
444

victor's avatar
 
victor committed
445 446
void PluginManager::switchGlobalState(GlobalState state)
{
447 448 449 450 451
	for (int i = 0; i < pImpl_->objects.size(); i++) {
		KPlugin *p = pImpl_->objects[i];
		p->changeGlobalState(pImpl_->globalState, state);
	}
	pImpl_->globalState = state;
victor's avatar
 
victor committed
452 453 454 455
}

GlobalState PluginManager::currentGlobalState() const
{
456
	return pImpl_->globalState;
victor's avatar
 
victor committed
457 458
}

victor's avatar
 
victor committed
459
} // namespace ExtensionSystem