pluginsDialog.cpp 9.76 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

16
#include "window.h"
17
#include "plugin.h"
18

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

namespace SCHNApps
{

Pierre Kraemer's avatar
Pierre Kraemer committed
25
PluginsDialog::PluginsDialog(Window* window) :
26
	QDialog(window),
27
	m_window(window),
28 29 30 31 32 33 34 35 36
	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
37 38
	connect(directoryButton, SIGNAL(pressed()), this, SLOT(cb_addPluginsDirectory()));

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

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

Pierre Kraemer's avatar
Pierre Kraemer committed
44
//	restoreState();
45

Pierre Kraemer's avatar
Pierre Kraemer committed
46
	addPluginsDirectory(m_window->getAppPath() + QString("../lib/"));
47

48 49 50
	init = false;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
51
PluginsDialog::~PluginsDialog()
52 53
{}

Pierre Kraemer's avatar
Pierre Kraemer committed
54
//bool PluginsDialog::restoreState()
Pierre Kraemer's avatar
Pierre Kraemer committed
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
//{
//	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;
//}
156

157 158 159 160 161
void PluginsDialog::addPluginsDirectory(const QString& dir)
{
	QDir directory(dir);

	if (!directory.exists())
162
		return;
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

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

	QStringList filters;
	filters << "lib*.so";
	filters << "lib*.dylib";

	QStringList dirFiles;
	dirFiles = directory.entryList(filters, QDir::Files);

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

	foreach(QString pluginPath, dirFiles)
	{
		QFileInfo pfi(pluginPath);
		QString pluginName = pfi.baseName().remove(0, 3);
		PluginInfo pinfo(directory.absoluteFilePath(pluginPath), pluginName);

		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, pluginName);

		m_listedPlugins[item] = pinfo;
	}
}

Pierre Kraemer's avatar
Pierre Kraemer committed
196
void PluginsDialog::cb_addPlugins()
197 198
{
	init = true;
Pierre Kraemer's avatar
Pierre Kraemer committed
199

200 201
	QStringList files = QFileDialog::getOpenFileNames(
		this,
Pierre Kraemer's avatar
Pierre Kraemer committed
202
		"Select one or more plugins",
203
		m_window->getAppPath(),
204 205 206 207 208
		"Plugins (lib*.so lib*.dylib)"
	);

	if (!files.empty())
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
209
		foreach(QString pluginPath, files)
210
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
211 212 213 214
			QFileInfo pfi(pluginPath);
			QString pluginName = pfi.baseName().remove(0, 3);
			PluginInfo pinfo(pluginPath, pluginName);

215 216 217
			QTreeWidgetItem *item = new QTreeWidgetItem(treeWidget, FILE);
			item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
			item->setCheckState(0, Qt::Unchecked);
Pierre Kraemer's avatar
Pierre Kraemer committed
218 219 220
			item->setText(1, pluginName);

			m_listedPlugins[item] = pinfo;
221 222 223 224 225 226
		}
	}

	init = false;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
227
void PluginsDialog::cb_addPluginsDirectory()
228 229
{
	init = true;
Pierre Kraemer's avatar
Pierre Kraemer committed
230

231 232
	QString dir = QFileDialog::getExistingDirectory(
		this,
Pierre Kraemer's avatar
Pierre Kraemer committed
233
		tr("Select a directory"),
Pierre Kraemer's avatar
Pierre Kraemer committed
234
		m_window->getAppPath() + QString("../lib/"),
235 236 237 238
		QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks
	);

	if (!dir.isEmpty())
239
		addPluginsDirectory(dir);
240 241 242 243

	init = false;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
244
void PluginsDialog::cb_removePlugins()
245
{
Pierre Kraemer's avatar
Pierre Kraemer committed
246
	QList<QTreeWidgetItem*> itemList = treeWidget->selectedItems();
247 248 249

	if (!itemList.isEmpty())
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
250
		foreach(QTreeWidgetItem* item, itemList)
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
		{
			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
267 268

					m_listedPlugins.remove(item);
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
				}
			}
			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
301 302 303 304 305 306
					for (int i = 0; i < item->childCount(); ++i)
					{
						fileItem = item->child(i);
						m_listedPlugins.remove(fileItem);
					}

307 308 309 310 311 312 313 314
					item = treeWidget->takeTopLevelItem(treeWidget->indexOfTopLevelItem(item));
					delete item;
				}
			}
		}
	}
}

Pierre Kraemer's avatar
Pierre Kraemer committed
315
void PluginsDialog::cb_togglePlugin(QTreeWidgetItem* item, int column)
316 317 318
{
	if (!init && column == 0)
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
319
		PluginInfo pinfo = m_listedPlugins[item];
320

Pierre Kraemer's avatar
Pierre Kraemer committed
321 322
		QString pluginPath = pinfo.pluginPath;
		QString pluginName = pinfo.pluginName;
323

324 325
		if (item->checkState(0) == Qt::Checked)
		{
326
			const PluginHash& activePlugins = m_window->getPluginsHash();
327

328
			if (activePlugins.contains(pluginName))
329
			{
330 331 332
				init = true;
				item->setCheckState(0, Qt::Unchecked);
				init = false;
333 334 335
				return;
			}

Pierre Kraemer's avatar
Pierre Kraemer committed
336
			Plugin* p = m_window->loadPlugin(pluginPath);
337 338

			if (p == NULL)
339
			{
340 341 342
				init = true;
				item->setCheckState(0, Qt::Unchecked);
				init = false;
343 344 345
			}
		}
		else if (item->checkState(0) == Qt::Unchecked)
346 347 348
		{
			Plugin* p = m_window->getPlugin(pluginName);

Pierre Kraemer's avatar
Pierre Kraemer committed
349
			if(p->isUsed())
350 351 352 353 354 355 356 357 358 359 360 361
			{
				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
362 363
	}
}
364

Pierre Kraemer's avatar
Pierre Kraemer committed
365
//void PluginsDialog::customContextMenu(const QPoint &pos)
Pierre Kraemer's avatar
Pierre Kraemer committed
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
//{
//	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);
//	}
//}
383

Pierre Kraemer's avatar
Pierre Kraemer committed
384
//void PluginsDialog::cb_acceptDialog()
Pierre Kraemer's avatar
Pierre Kraemer committed
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
//{
//	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);
//		}
//	}
//}
401

Pierre Kraemer's avatar
Pierre Kraemer committed
402
//void PluginsDialog::showPluginInfo()
403 404 405 406 407 408
//{
//	QTreeWidgetItem *item = treeWidget->currentItem();
//	QString strUrl = item->text(1);
//
//	System::Info::showPluginInfo(strUrl);
//}
Pierre Kraemer's avatar
Pierre Kraemer committed
409 410 411 412

} // namespace SCHNApps

} // namespace CGoGN