void ActionsManager::loadProfiles()
{
	QHash<QAction*, QStringList>::iterator macrosIterator;

	for (macrosIterator = m_applicationMacros.begin(); macrosIterator != m_applicationMacros.end(); ++macrosIterator)
	{
		const QString indentifier = macrosIterator.key()->objectName();

		if (m_applicationActions.contains(indentifier))
		{
			m_applicationActions[indentifier]->deleteLater();

			m_applicationActions.remove(indentifier);
		}
	}

	m_applicationMacros.clear();
	m_profileShortcuts.clear();

	QList<QKeySequence> shortcutsInUse;
	const QStringList shortcutsProfiles = SettingsManager::getValue(QLatin1String("Browser/KeyboardShortcutsProfilesOrder")).toStringList();

	for (int i = 0; i < shortcutsProfiles.count(); ++i)
	{
		const QString path = SessionsManager::getProfilePath() + QLatin1String("/keyboard/") + shortcutsProfiles.at(i) + QLatin1String(".ini");
		const QSettings profile((QFile::exists(path) ? path : QLatin1String(":/keyboard/") + shortcutsProfiles.at(i) + QLatin1String(".ini")), QSettings::IniFormat);
		const QStringList actions = profile.childGroups();

		for (int j = 0; j < actions.count(); ++j)
		{
			const QStringList rawShortcuts = profile.value(actions.at(j) + QLatin1String("/shortcuts"), QString()).toString().split(QLatin1Char(' '), QString::SkipEmptyParts);
			QList<QKeySequence> actionShortcuts;

			for (int k = 0; k < rawShortcuts.count(); ++k)
			{
				const QKeySequence shortcut = ((rawShortcuts.at(k) == QLatin1String("native")) ? m_nativeShortcuts.value(actions.at(j), QKeySequence()) : QKeySequence(rawShortcuts.at(k)));

				if (!shortcut.isEmpty() && !shortcutsInUse.contains(shortcut))
				{
					actionShortcuts.append(shortcut);
					shortcutsInUse.append(shortcut);
				}
			}

			if (!actionShortcuts.isEmpty())
			{
				m_profileShortcuts[actions.at(j)] = actionShortcuts;
			}
		}
	}

	QHash<QString, QAction*>::iterator actionsIterator;

	for (actionsIterator = m_applicationActions.begin(); actionsIterator != m_applicationActions.end(); ++actionsIterator)
	{
		if (m_profileShortcuts.contains(actionsIterator.key()))
		{
			actionsIterator.value()->setShortcuts(m_profileShortcuts[actionsIterator.key()]);
		}
		else
		{
			actionsIterator.value()->setShortcut(QKeySequence());
		}
	}

	const QStringList macrosProfiles = SettingsManager::getValue(QLatin1String("Browser/ActionMacrosProfilesOrder")).toStringList();

	for (int i = 0; i < macrosProfiles.count(); ++i)
	{
		const QString path = SessionsManager::getProfilePath() + QLatin1String("/macros/") + macrosProfiles.at(i) + QLatin1String(".ini");
		const QSettings profile((QFile::exists(path) ? path : QLatin1String(":/macros/") + macrosProfiles.at(i) + QLatin1String(".ini")), QSettings::IniFormat);
		const QStringList macros = profile.childGroups();

		for (int j = 0; j < macros.count(); ++j)
		{
			const QStringList actions = profile.value(macros.at(j) + QLatin1String("/actions"), QString()).toStringList();

			if (m_applicationActions.contains(macros.at(j)) || actions.isEmpty())
			{
				continue;
			}

			QAction *action = new QAction(profile.value(macros.at(j) + QLatin1String("/title"), QString()).toString(), SessionsManager::getActiveWindow());
			action->setObjectName(macros.at(j));

			connect(action, SIGNAL(triggered()), m_instance, SLOT(triggerMacro()));

			m_applicationActions[macros.at(j)] = action;
			m_applicationMacros[action] = actions;

			const QStringList rawShortcuts = profile.value(macros.at(j) + QLatin1String("/shortcuts"), QString()).toString().split(QLatin1Char(' '), QString::SkipEmptyParts);
			QList<QKeySequence> actionShortcuts;

			for (int k = 0; k < rawShortcuts.count(); ++k)
			{
				const QKeySequence shortcut(rawShortcuts.at(k));

				if (!shortcut.isEmpty() && !shortcutsInUse.contains(shortcut))
				{
					actionShortcuts.append(shortcut);
					shortcutsInUse.append(shortcut);
				}
			}

			if (!actionShortcuts.isEmpty())
			{
				m_profileShortcuts[macros.at(j)] = actionShortcuts;

				action->setShortcuts(actionShortcuts);
				action->setShortcutContext(Qt::ApplicationShortcut);
			}
		}
	}

	QList<QObject*> windows = m_windowActions.keys();

	for (int i = 0; i < windows.count(); ++i)
	{
		setupWindowActions(windows.at(i));
	}
}
Exemple #2
0
void ShortcutsManager::loadProfiles()
{
	qDeleteAll(m_macros.keys());

	m_macros.clear();
	m_shortcuts.clear();

	QList<QKeySequence> shortcutsInUse;
	const QStringList shortcutsProfiles = SettingsManager::getValue(QLatin1String("Browser/KeyboardShortcutsProfilesOrder")).toStringList();

	for (int i = 0; i < shortcutsProfiles.count(); ++i)
	{
		const QString path = SessionsManager::getProfilePath() + QLatin1String("/keyboard/") + shortcutsProfiles.at(i) + QLatin1String(".ini");
		const QSettings profile((QFile::exists(path) ? path : QLatin1String(":/keyboard/") + shortcutsProfiles.at(i) + QLatin1String(".ini")), QSettings::IniFormat);
		const QStringList actions = profile.childGroups();

		for (int j = 0; j < actions.count(); ++j)
		{
			const QStringList rawShortcuts = profile.value(actions.at(j) + QLatin1String("/shortcuts"), QString()).toString().split(QLatin1Char(' '), QString::SkipEmptyParts);
			QList<QKeySequence> actionShortcuts;

			for (int k = 0; k < rawShortcuts.count(); ++k)
			{
				const QKeySequence shortcut = ((rawShortcuts.at(k) == QLatin1String("native")) ? m_nativeShortcuts.value(actions.at(j), QKeySequence()) : QKeySequence(rawShortcuts.at(k)));

				if (!shortcut.isEmpty() && !shortcutsInUse.contains(shortcut))
				{
					actionShortcuts.append(shortcut);
					shortcutsInUse.append(shortcut);
				}
			}

			if (!actionShortcuts.isEmpty())
			{
				m_shortcuts[actions.at(j)] = actionShortcuts;
			}
		}
	}

	const QStringList macrosProfiles = SettingsManager::getValue(QLatin1String("Browser/ActionMacrosProfilesOrder")).toStringList();

	for (int i = 0; i < macrosProfiles.count(); ++i)
	{
		const QString path = SessionsManager::getProfilePath() + QLatin1String("/macros/") + macrosProfiles.at(i) + QLatin1String(".ini");
		const QSettings profile((QFile::exists(path) ? path : QLatin1String(":/macros/") + macrosProfiles.at(i) + QLatin1String(".ini")), QSettings::IniFormat);
		const QStringList macros = profile.childGroups();

		for (int j = 0; j < macros.count(); ++j)
		{
			const QStringList actions = profile.value(macros.at(j) + QLatin1String("/actions"), QString()).toStringList();

///FIXME check if name is unique
			if (actions.isEmpty())
			{
				continue;
			}

			QAction *action = new QAction(QIcon(), profile.value(macros.at(j) + QLatin1String("/title"), QString()).toString(), SessionsManager::getActiveWindow());
			action->setObjectName(macros.at(j));

			connect(action, SIGNAL(triggered()), m_instance, SLOT(triggerMacro()));

			m_macros[action] = actions;

			const QStringList rawShortcuts = profile.value(macros.at(j) + QLatin1String("/shortcuts"), QString()).toString().split(QLatin1Char(' '), QString::SkipEmptyParts);
			QList<QKeySequence> actionShortcuts;

			for (int k = 0; k < rawShortcuts.count(); ++k)
			{
				const QKeySequence shortcut(rawShortcuts.at(k));

				if (!shortcut.isEmpty() && !shortcutsInUse.contains(shortcut))
				{
					actionShortcuts.append(shortcut);
					shortcutsInUse.append(shortcut);
				}
			}

			if (!actionShortcuts.isEmpty())
			{
				m_shortcuts[macros.at(j)] = actionShortcuts;

				action->setShortcuts(actionShortcuts);
				action->setShortcutContext(Qt::ApplicationShortcut);
			}
		}
	}

	emit m_instance->shortcutsChanged();
}