pluginsDialog.cpp 10.4 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer committed
1
#include "dialogs/pluginsDialog.h"
2

3
//#include <libxml2/libxml/tree.h>
4
#include <QFile>
5 6
#include <QFileInfo>
#include <QDir>
7
#include <QFileDialog>
8 9 10
#include <QDomDocument>
#include <QDomElement>
#include <QDomNode>
11 12
#include <QDomText>
#include <QMessageBox>
Pierre Kraemer's avatar
Pierre Kraemer committed
13
//#include <QRegExp>
14
//#include <QTextBrowser>
15

Pierre Kraemer's avatar
Pierre Kraemer committed
16
#include "system.h"
17
#include "window.h"
18
#include "plugin.h"
19

Pierre Kraemer's avatar
Pierre Kraemer committed
20 21 22 23 24 25
namespace CGoGN
{

namespace SCHNApps
{

Pierre Kraemer's avatar
Pierre Kraemer committed
26
PluginsDialog::PluginsDialog(Window* window) :
27
	QDialog(window),
28
	m_window(window),
29 30 31 32 33 34 35 36 37
	init(true)
{
	this->setupUi(this);

	treeWidget->setContextMenuPolicy(Qt::CustomContextMenu);
	treeWidget->setSelectionMode(QAbstractItemView::SingleSelection);

	connect(addButton, SIGNAL(pressed()), this, SLOT(cb_addPlugins()));
	connect(removeButton, SIGNAL(pressed()), this, SLOT(cb_removePlugins()));
Pierre Kraemer's avatar
Pierre Kraemer committed
38 39 40
	connect(directoryButton, SIGNAL(pressed()), this, SLOT(cb_addPluginsDirectory()));

	connect(treeWidget, SIGNAL(itemChanged(QTreeWidgetItem *, int)), this, SLOT(cb_togglePlugin(QTreeWidgetItem *, int)));
Pierre Kraemer's avatar
Pierre Kraemer committed
41
//	connect(treeWidget, SIGNAL(customContextMenuRequested(const QPoint &)), this, SLOT(customContextMenu(const QPoint &)));
42

Pierre Kraemer's avatar
Pierre Kraemer committed
43
//	connect(this, SIGNAL(accepted()), this, SLOT(cb_acceptDialog()));
44

Pierre Kraemer's avatar
Pierre Kraemer committed
45
//	restoreState();
46 47 48 49 50 51 52

	if (System::Error::code != System::Error::SUCCESS)
		System::Error::showError(this);

	init = false;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
53
PluginsDialog::~PluginsDialog()
54 55
{}

Pierre Kraemer's avatar
Pierre Kraemer committed
56
//bool PluginsDialog::restoreState()
Pierre Kraemer's avatar
Pierre Kraemer committed
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
//{
//	QFile xmlFile(System::app_path.toStdString().c_str() + QString("/state_save.xml"));
//
//	if (!xmlFile.exists())
//	{
//		System::Error::code = System::Error::NO_PLUGIN_PATH_FILE;
//		return false;
//	}
//
//	if (!xmlFile.open(QIODevice::ReadOnly))
//	{
//		System::Error::code = System::Error::ERROR_OPEN_PLUGIN_FILE;
//		return false;
//	}
//
//	QDomDocument doc;
//
//	if (!doc.setContent(&xmlFile))
//	{
//		System::Error::code = System::Error::BAD_PLUGIN_PATH_FILE;
//		xmlFile.close();
//		return false;
//	}
//
//	xmlFile.close();
//
//	QDomElement root = doc.documentElement();
//	QDomElement plugins_node = root.firstChildElement("PLUGINS");
//
//	const PluginHash& activePlugins = m_window->getPluginsHash();
//
//	if (!plugins_node.isNull())
//	{
//		QDomElement plugins_subNode = plugins_node.firstChildElement();
//
//		while (!plugins_subNode.isNull())
//		{
//			if (plugins_subNode.tagName() == "DIR")
//			{
//				QString pluginDirPath = plugins_subNode.attribute("path", "./plugins");
//				QFileInfo fi(pluginDirPath);
//
//				if (fi.exists() && fi.isDir())
//				{
//					QDir pluginDir(pluginDirPath);
//
//					QTreeWidgetItem *dirItem = new QTreeWidgetItem(treeWidget, DIR);
//					dirItem->setText(1, pluginDir.path());
//
//					QStringList filters, dirFiles;
//					filters << "lib*.so";
//					filters << "lib*.dylib";
//
//					dirFiles = pluginDir.entryList(filters, QDir::Files);
//					foreach(QString fileName, dirFiles)
//					{
//						QFileInfo pfi(fileName);
//						QString pluginName = pfi.baseName().remove(0, 3);
//
//						QTreeWidgetItem *item = new QTreeWidgetItem(dirItem, FILE_DIR);
//						item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
//
//						if (activePlugins.contains(pluginName))
//							item->setCheckState(0, Qt::Checked);
//						else
//							item->setCheckState(0, Qt::Unchecked);
//
//						item->setText(1, pluginDir.absoluteFilePath(fileName));
//					}
//				}
//			}
//			else if (plugins_subNode.tagName() == "FILE")
//			{
//				QString pluginPath = plugins_subNode.attribute("path");
//
//				if (!pluginPath.isEmpty())
//				{
//					QFileInfo fi(pluginPath);
//
//					if (fi.exists() && pluginPath.left(3) == "lib" && (fi.suffix() == "so" || fi.suffix() == "dylib"))
//					{
//						QString pluginName = fi.baseName().remove(0, 3);
//						QTreeWidgetItem *item =  new QTreeWidgetItem(treeWidget, FILE);
//						item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
//
//						if (activePlugins.contains(pluginName))
//							item->setCheckState(0, Qt::Checked);
//						else
//							item->setCheckState(0, Qt::Unchecked);
//
//						item->setText(1, pluginPath);
//					}
//				}
//			}
//
//			plugins_subNode = plugins_subNode.nextSiblingElement();
//		}
//	}
//
//	return true;
//}
158

Pierre Kraemer's avatar
Pierre Kraemer committed
159
void PluginsDialog::cb_addPlugins()
160 161
{
	init = true;
Pierre Kraemer's avatar
Pierre Kraemer committed
162

163 164
	QStringList files = QFileDialog::getOpenFileNames(
		this,
Pierre Kraemer's avatar
Pierre Kraemer committed
165
		"Select one or more plugins",
166
		m_window->getAppPath() + QString("/../Plugins/"),
167 168 169 170 171
		"Plugins (lib*.so lib*.dylib)"
	);

	if (!files.empty())
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
172
		foreach(QString pluginPath, files)
173
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
174 175 176 177
			QFileInfo pfi(pluginPath);
			QString pluginName = pfi.baseName().remove(0, 3);
			PluginInfo pinfo(pluginPath, pluginName);

178 179 180
			QTreeWidgetItem *item = new QTreeWidgetItem(treeWidget, FILE);
			item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
			item->setCheckState(0, Qt::Unchecked);
Pierre Kraemer's avatar
Pierre Kraemer committed
181 182 183
			item->setText(1, pluginName);

			m_listedPlugins[item] = pinfo;
184 185 186 187 188 189 190 191 192
		}
	}

	if (System::Error::code != System::Error::SUCCESS)
		System::Error::showError(this);

	init = false;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
193
void PluginsDialog::cb_addPluginsDirectory()
194 195
{
	init = true;
Pierre Kraemer's avatar
Pierre Kraemer committed
196

197 198
	QString dir = QFileDialog::getExistingDirectory(
		this,
Pierre Kraemer's avatar
Pierre Kraemer committed
199
		tr("Select a directory"),
200
		m_window->getAppPath() + QString("/../Plugins/"),
201 202 203 204
		QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks
	);

	if (!dir.isEmpty())
Pierre Kraemer's avatar
Pierre Kraemer committed
205 206 207 208 209 210 211 212 213
	{
		QDir directory(dir);

		if (!directory.exists())
			System::Error::code = System::Error::BAD_PLUGIN_PATH_IN_FILE_f(directory.absolutePath());

		QTreeWidgetItem *dirItem = new QTreeWidgetItem(treeWidget, DIR);
		dirItem->setText(1, directory.path());

Pierre Kraemer's avatar
Pierre Kraemer committed
214
		QStringList filters;
Pierre Kraemer's avatar
Pierre Kraemer committed
215 216 217
		filters << "lib*.so";
		filters << "lib*.dylib";

Pierre Kraemer's avatar
Pierre Kraemer committed
218
		QStringList dirFiles;
Pierre Kraemer's avatar
Pierre Kraemer committed
219 220
		dirFiles = directory.entryList(filters, QDir::Files);

221 222
		const PluginHash& activePlugins = m_window->getPluginsHash();

Pierre Kraemer's avatar
Pierre Kraemer committed
223
		foreach(QString pluginPath, dirFiles)
Pierre Kraemer's avatar
Pierre Kraemer committed
224
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
225
			QFileInfo pfi(pluginPath);
Pierre Kraemer's avatar
Pierre Kraemer committed
226
			QString pluginName = pfi.baseName().remove(0, 3);
Pierre Kraemer's avatar
Pierre Kraemer committed
227
			PluginInfo pinfo(directory.absoluteFilePath(pluginPath), pluginName);
Pierre Kraemer's avatar
Pierre Kraemer committed
228 229 230 231

			QTreeWidgetItem *item = new QTreeWidgetItem(dirItem, FILE_DIR);
			item->setFlags(item->flags() | Qt::ItemIsUserCheckable);

232
			if (activePlugins.contains(pluginName))
Pierre Kraemer's avatar
Pierre Kraemer committed
233 234 235 236
				item->setCheckState(0, Qt::Checked);
			else
				item->setCheckState(0, Qt::Unchecked);

Pierre Kraemer's avatar
Pierre Kraemer committed
237 238 239
			item->setText(1, pluginName);

			m_listedPlugins[item] = pinfo;
Pierre Kraemer's avatar
Pierre Kraemer committed
240 241 242 243 244
		}

		if (dirFiles.isEmpty())
			System::Error::code = System::Error::NO_PLUGIN_IN_DIR_f(directory.absolutePath());
	}
245 246 247 248 249 250 251

	if (System::Error::code != System::Error::SUCCESS)
		System::Error::showError(this);

	init = false;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
252
void PluginsDialog::cb_removePlugins()
253
{
Pierre Kraemer's avatar
Pierre Kraemer committed
254
	QList<QTreeWidgetItem*> itemList = treeWidget->selectedItems();
255 256 257

	if (!itemList.isEmpty())
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
258
		foreach(QTreeWidgetItem* item, itemList)
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
		{
			if (item->type() == FILE)
			{
				if (item->checkState(0) == Qt::Checked)
				{
					QMessageBox msgBox;
					msgBox.setText("Le plugin\n"
					               "\t" + item->text(1) +
					               " doit être désactivé avant de pouvoir être\n"
					               "supprimé.");
					msgBox.exec();
				}
				else
				{
					item = treeWidget->takeTopLevelItem(treeWidget->indexOfTopLevelItem(item));
					delete item;
Pierre Kraemer's avatar
Pierre Kraemer committed
275 276

					m_listedPlugins.remove(item);
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
				}
			}
			else if (item->type() == FILE_DIR)
			{
				QMessageBox msgBox;
				msgBox.setText("Le plugin\n"
				               "\t" + item->text(1) +
				               " fait partit d'un paquet/directory.\n"
				               "Il ne peut être supprimé séparément.");
				msgBox.exec();
			}
			else if (item->type() == DIR)
			{
				bool isAnyPluginActive = false;
				QTreeWidgetItem *fileItem;

				for (int i = 0; (i < item->childCount() && !isAnyPluginActive); ++i)
				{
					fileItem = item->child(i);
					if (fileItem->checkState(0) == Qt::Checked)
						isAnyPluginActive = true;
				}

				if (isAnyPluginActive)
				{
					QMessageBox msgBox;
					msgBox.setText("Un ou plusieurs plugins du dossier sont actifs\n"
					               "Veuillez désactiver tous les plugins du paquet avant de supprimer celui-ci.");
					msgBox.exec();
				}
				else
				{
Pierre Kraemer's avatar
Pierre Kraemer committed
309 310 311 312 313 314
					for (int i = 0; i < item->childCount(); ++i)
					{
						fileItem = item->child(i);
						m_listedPlugins.remove(fileItem);
					}

315 316 317 318 319 320 321 322 323 324 325
					item = treeWidget->takeTopLevelItem(treeWidget->indexOfTopLevelItem(item));
					delete item;
				}
			}
		}
	}

	if (System::Error::code != System::Error::SUCCESS)
		System::Error::showError(this);
}

Pierre Kraemer's avatar
Pierre Kraemer committed
326
void PluginsDialog::cb_togglePlugin(QTreeWidgetItem *item, int column)
327 328 329
{
	if (!init && column == 0)
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
330
		PluginInfo pinfo = m_listedPlugins[item];
331

Pierre Kraemer's avatar
Pierre Kraemer committed
332 333
		QString pluginPath = pinfo.pluginPath;
		QString pluginName = pinfo.pluginName;
334

335 336
		if (item->checkState(0) == Qt::Checked)
		{
337
			const PluginHash& activePlugins = m_window->getPluginsHash();
338

339
			if (activePlugins.contains(pluginName))
340 341 342
			{
				System::Error::code = System::Error::PLUGIN_EXISTS_f(pluginName);
				System::Error::showError(this);
343 344 345
				init = true;
				item->setCheckState(0, Qt::Unchecked);
				init = false;
346 347 348
				return;
			}

Pierre Kraemer's avatar
Pierre Kraemer committed
349
			Plugin* p = m_window->loadPlugin(pluginPath);
350 351

			if (p == NULL)
352
			{
353 354 355
				init = true;
				item->setCheckState(0, Qt::Unchecked);
				init = false;
356 357 358
			}
		}
		else if (item->checkState(0) == Qt::Unchecked)
359 360 361
		{
			Plugin* p = m_window->getPlugin(pluginName);

Pierre Kraemer's avatar
Pierre Kraemer committed
362
			if(p->isUsed())
363 364 365 366 367 368 369 370 371 372 373 374
			{
				QMessageBox::warning(this, tr("Warning"), "Plugin is currently used");
				init = true;
				item->setCheckState(0, Qt::Checked);
				init = false;
				return;
			}
			else
			{
				m_window->unloadPlugin(pluginName);
			}
		}
Pierre Kraemer's avatar
Pierre Kraemer committed
375 376
	}
}
377

Pierre Kraemer's avatar
Pierre Kraemer committed
378
//void PluginsDialog::customContextMenu(const QPoint &pos)
Pierre Kraemer's avatar
Pierre Kraemer committed
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
//{
//	QPoint globalPos = treeWidget->mapToGlobal(pos);
//
//	QTreeWidgetItem *item = treeWidget->itemAt(pos);
//
//	if (item && (item->type() == FILE || item->type() == FILE_DIR))
//	{
//		item->setSelected(true);
//
//		QMenu myMenu("Plus...", this);
//		QAction pluginInfo("Informations sur le plugin", this);
//		myMenu.addAction(&pluginInfo);
//		connect(&pluginInfo, SIGNAL(triggered()), this, SLOT(showPluginInfo()));
//
//		myMenu.exec(globalPos);
//	}
//}
396

Pierre Kraemer's avatar
Pierre Kraemer committed
397
//void PluginsDialog::cb_acceptDialog()
Pierre Kraemer's avatar
Pierre Kraemer committed
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
//{
//	QStringList paths;
//
//	int t = treeWidget->topLevelItemCount();
//
//	for (int i = 0; i < t; ++i)
//	{
//		QTreeWidgetItem *item = treeWidget->topLevelItem(i);
//
//		if (item->type() == FILE || item->type() == DIR)
//		{
//			QString path = item->text(1);
//			paths.push_back(path);
//		}
//	}
//
414 415
//	if (!System::StateHandler::savePluginsInfo(m_window, activePlugins, paths))
//		System::Error::showError();
Pierre Kraemer's avatar
Pierre Kraemer committed
416
//}
417

Pierre Kraemer's avatar
Pierre Kraemer committed
418
//void PluginsDialog::showPluginInfo()
419 420 421 422 423 424
//{
//	QTreeWidgetItem *item = treeWidget->currentItem();
//	QString strUrl = item->text(1);
//
//	System::Info::showPluginInfo(strUrl);
//}
Pierre Kraemer's avatar
Pierre Kraemer committed
425 426 427 428

} // namespace SCHNApps

} // namespace CGoGN