Example #1
0
void Backend::tryPlugin (PluginSpec const & spec)
{
	PluginPtr plugin = modules.load (spec);

	errorplugins.tryPlugin (*plugin.get ());
	getplugins.tryPlugin (*plugin.get ());
	setplugins.tryPlugin (*plugin.get ());


	for (auto & elem : plugins)
	{
		if (plugin->getFullName () == elem->getFullName ()) throw PluginAlreadyInserted (plugin->getFullName ());
	}


	plugins.push_back (std::move (plugin));
}
Example #2
0
void TreeViewModel::importConfiguration(const QString& name, const QString& format, QString& file, const QVariantList &mergeStrategies)
{
	Key		root(name.toStdString(), KEY_END);
	KeySet	originalKeys = collectCurrentKeySet();
	KeySet	base = originalKeys.cut(root);
	printWarnings (cerr, root);

	KeySet importedKeys;

	string formatString = format.toStdString();
	string fileString = file.remove("file://").toStdString();

	Modules modules;
	PluginPtr plugin = modules.load(formatString);

	Key errorKey (root);
	errorKey.setString (fileString);

	plugin->get(importedKeys, errorKey);

	stringstream ws;
	stringstream es;
	QString warnings;
	QString errors;

	printWarnings(ws, errorKey);
	warnings = QString::fromStdString(ws.str());
	printError(es, errorKey);
	errors  = QString::fromStdString(es.str());

	if(!errors.isEmpty())
	{
		emit showMessage(tr("Error"), tr("Failed to import configuration from %1 to %2.").arg(file, QString::fromStdString(root.getName())), errors);
		return;
	}

	ThreeWayMerge merger;

	foreach(QVariant s, mergeStrategies)
	{
		MergeConflictStrategy* strategy = getMergeStrategy(s.toString());

		if(strategy)
			merger.addConflictStrategy(strategy);
	}
Example #3
0
int ImportCommand::execute (Cmdline const & cl)
{
	size_t argc = cl.arguments.size ();
	if (argc != 1 && argc != 2 && argc != 3)
	{
		throw invalid_argument ("need 1 to 3 arguments");
	}

	Key root = cl.createKey (0);
	if (!root.isValid ())
	{
		throw invalid_argument ("root key \"" + cl.arguments[0] + "\" is not a valid key name");
	}

	KeySet originalKeys;
	kdb.get (originalKeys, root);
	KeySet base = originalKeys.cut (root);
	printWarnings (cerr, root);

	string format = cl.format;
	if (argc > 1) format = cl.arguments[1];

	string file = "/dev/stdin";
	if (argc > 2 && cl.arguments[2] != "-") file = cl.arguments[2];

	Modules modules;
	PluginPtr plugin = modules.load (format, cl.getPluginsConfig ());

	Key errorKey (root);
	errorKey.setString (file);

	KeySet importedKeys;
	plugin->get (importedKeys, errorKey);
	importedKeys = importedKeys.cut (root);

	printWarnings (cerr, errorKey);
	printError (cerr, errorKey);

	ThreeWayMerge merger;
	MergeHelper helper;

	helper.configureMerger (cl, merger);
	MergeResult result = merger.mergeKeySet (
		MergeTask (BaseMergeKeys (base, root), OurMergeKeys (base, root), TheirMergeKeys (importedKeys, root), root));

	helper.reportResult (cl, result, cout, cerr);

	int ret = -1;
	if (!result.hasConflicts ())
	{
		if (cl.verbose)
		{
			cout << "The merged keyset with strategy " << cl.strategy << " is:" << endl;
			cout << result.getMergedKeys ();
		}

		KeySet resultKeys = result.getMergedKeys ();
		originalKeys.append (resultKeys);
		kdb.set (originalKeys, root);
		ret = 0;
	}

	return ret;
}
Example #4
0
int EditorCommand::execute(Cmdline const& cl)
{
#ifdef _WIN32
	throw EditorNotAvailable();
#endif

	int argc = cl.arguments.size ();
	if (argc < 1)
	{
		throw invalid_argument ("wrong number of arguments, 1 needed");
	}
	Key root = cl.createKey(0);

	KeySet ours;
	KDB kdb;
	kdb.get (ours, root);
	KeySet oursToEdit = ours.cut (root);

	// export it to file
	string format = cl.format;
	if (argc > 1) format = cl.arguments[1];

	Modules modules;
	PluginPtr plugin = modules.load(format);

	tmpFile();
	if (cl.verbose) std::cout << "filename set to " << filename << std::endl;
	Key errorKey(root);
	errorKey.setString(filename);

	if (plugin->set(oursToEdit, errorKey) == -1)
	{
		printWarnings(cerr, errorKey);
		printError(cerr, errorKey);
		return 11;
	}

	printWarnings(cerr, errorKey);


	// start editor
	if (cl.verbose) std::cout << "running editor with " << filename << std::endl;
	if (!cl.editor.empty())
	{
		if (!runEditor (cl.editor, filename))
		{
			std::cerr << "Could not run editor " << cl.editor << std::endl;
			return 12;
		}
	} else {
		if (!runAllEditors(filename))
		{
			std::cerr << "Could not run any editor, please change /sw/elektra/kdb/#0/current/editor" << std::endl;
			return 12;
		}
	}

	// import from the file
	KeySet importedKeys;
	plugin->get(importedKeys, errorKey);
	importedKeys = importedKeys.cut(root);

	printWarnings (cerr, errorKey);
	printError (cerr, errorKey);

	ThreeWayMerge merger;
	MergeHelper helper;

	helper.configureMerger (cl, merger);
	MergeResult result = merger.mergeKeySet (
			MergeTask (BaseMergeKeys (oursToEdit, root), OurMergeKeys (oursToEdit, root),
					TheirMergeKeys (importedKeys, root), root));

	helper.reportResult (cl, result, cout, cerr);

	int ret = 13;
	if (!result.hasConflicts ())
	{
		if (cl.verbose)
		{
			cout << "The merged keyset with strategy " << cl.strategy << " is:" << endl;
			cout << result.getMergedKeys();
		}

		KeySet resultKeys = result.getMergedKeys();
		if (cl.verbose) std::cout << "about to write result keys " << resultKeys << std::endl;
		ours.append(resultKeys);
		kdb.set (ours, root);
		if (cl.verbose) std::cout << "successful, cleaning up " << filename << std::endl;
		unlink(filename.c_str());
		ret = 0;
	}
	else
	{
		std::cout << "Import not successful, please import and remove \"" << filename << '"' << std::endl;
	}

	return ret;
}