Esempio n. 1
0
void AddressWidget::handleUserInput(const QString &text)
{
	const BookmarkInformation *bookmark = BookmarksManager::getBookmarkByKeyword(text);

	if (bookmark)
	{
		WindowsManager *windowsManager = SessionsManager::getWindowsManager();

		if (windowsManager)
		{
			windowsManager->open(bookmark);

			return;
		}
	}

	if (text == QString(QLatin1Char('~')) || text.startsWith(QLatin1Char('~') + QDir::separator()))
	{
		const QStringList locations = QStandardPaths::standardLocations(QStandardPaths::HomeLocation);

		if (!locations.isEmpty())
		{
			emit requestedLoadUrl(QUrl(locations.first() + text.mid(1)));

			return;
		}
	}

	const QUrl url = QUrl::fromUserInput(text);

	if (url.isValid() && (url.isLocalFile() || QRegularExpression(QLatin1String("^(\\w+\\:\\S+)|([\\w\\-]+\\.[a-zA-Z]{2,}(/\\S*)?$)")).match(text).hasMatch()))
	{
		emit requestedLoadUrl(url);

		return;
	}

	const QString shortcut = text.section(QLatin1Char(' '), 0, 0);
	const QStringList engines = SearchesManager::getSearchEngines();
	SearchInformation *engine = NULL;

	for (int i = 0; i < engines.count(); ++i)
	{
		engine = SearchesManager::getSearchEngine(engines.at(i));

		if (engine && shortcut == engine->shortcut)
		{
			emit requestedSearch(text.section(QLatin1Char(' '), 1), engine->identifier);

			return;
		}
	}

	const int lookupTimeout = SettingsManager::getValue(QLatin1String("AddressField/HostLookupTimeout")).toInt();

	if (url.isValid() && lookupTimeout > 0)
	{
		if (text == m_lookupQuery)
		{
			return;
		}

		m_lookupQuery = text;

		if (m_lookupTimer != 0)
		{
			QHostInfo::abortHostLookup(m_lookupID);

			killTimer(m_lookupTimer);

			m_lookupTimer = 0;
		}

		m_lookupID = QHostInfo::lookupHost(url.host(), this, SLOT(verifyLookup(QHostInfo)));

		m_lookupTimer = startTimer(lookupTimeout);

		return;
	}

	emit requestedSearch(text, SettingsManager::getValue(QLatin1String("Search/DefaultSearchEngine")).toString());
}
void InputInterpreter::interpret(const QString &text, WindowsManager::OpenHints hints, bool ignoreBookmarks)
{
	if (text.startsWith(QLatin1String("bookmarks:")))
	{
		BookmarksItem *bookmark = (text.startsWith(QLatin1String("bookmarks:/")) ? BookmarksManager::getModel()->getItem(text.mid(11)) : BookmarksManager::getBookmark(text.mid(10).toULongLong()));

		if (bookmark)
		{
			emit requestedOpenBookmark(bookmark, hints);

			deleteLater();

			return;
		}
	}

	if (!ignoreBookmarks)
	{
		BookmarksItem *bookmark = BookmarksManager::getBookmark(text);

		if (bookmark)
		{
			emit requestedOpenBookmark(bookmark, hints);

			deleteLater();

			return;
		}
	}

	if (text == QString(QLatin1Char('~')) || text.startsWith(QLatin1Char('~') + QDir::separator()))
	{
		const QStringList locations = QStandardPaths::standardLocations(QStandardPaths::HomeLocation);

		if (!locations.isEmpty())
		{
			emit requestedOpenUrl(locations.first() + text.mid(1), hints);

			deleteLater();

			return;
		}
	}

	const QFileInfo fileInformation(text);

	if (fileInformation.exists() && fileInformation.isAbsolute())
	{
		emit requestedOpenUrl(QUrl::fromLocalFile(QFileInfo(text).canonicalFilePath()), hints);

		deleteLater();

		return;
	}

	const QUrl url = QUrl::fromUserInput(text);
	const QHostAddress address(text);

	if (!address.isNull() || (url.isValid() && (url.isLocalFile() || QRegularExpression(QLatin1String("^(\\w+\\:\\S+)|([\\w\\-]+\\.[a-zA-Z]{2,}(/\\S*)?$)")).match(text).hasMatch())))
	{
		emit requestedOpenUrl(url, hints);

		deleteLater();

		return;
	}

	const QString keyword = text.section(QLatin1Char(' '), 0, 0);
	const SearchEnginesManager::SearchEngineDefinition searchEngine = SearchEnginesManager::getSearchEngine(keyword, true);

	if (!searchEngine.identifier.isEmpty())
	{
		emit requestedSearch(text.section(QLatin1Char(' '), 1), searchEngine.identifier, hints);

		deleteLater();

		return;
	}

	if (keyword == QLatin1String("?"))
	{
		emit requestedSearch(text.section(QLatin1Char(' '), 1), QString(), hints);

		deleteLater();

		return;
	}

	const int lookupTimeout = SettingsManager::getValue(QLatin1String("AddressField/HostLookupTimeout")).toInt();

	if (url.isValid() && lookupTimeout > 0)
	{
		m_text = text;
		m_hints = hints;

		if (m_timer != 0)
		{
			QHostInfo::abortHostLookup(m_lookup);

			killTimer(m_timer);

			m_timer = 0;
		}

		m_lookup = QHostInfo::lookupHost(url.host(), this, SLOT(verifyLookup(QHostInfo)));
		m_timer = startTimer(lookupTimeout);

		return;
	}

	emit requestedSearch(text, QString(), hints);

	deleteLater();

	return;
}