Ejemplo n.º 1
0
PreferencesSearchPageWidget::PreferencesSearchPageWidget(QWidget *parent) : QWidget(parent),
	m_ui(new Ui::PreferencesSearchPageWidget)
{
	m_ui->setupUi(this);

	ItemModel *searchEnginesModel(new ItemModel(this));
	searchEnginesModel->setHorizontalHeaderLabels({tr("Name"), tr("Keyword")});
	searchEnginesModel->setHeaderData(0, Qt::Horizontal, 250, HeaderViewWidget::WidthRole);
	searchEnginesModel->setExclusive(true);

	const QString defaultSearchEngine(SettingsManager::getOption(SettingsManager::Search_DefaultSearchEngineOption).toString());
	const QStringList searchEngines(SearchEnginesManager::getSearchEngines());

	for (int i = 0; i < searchEngines.count(); ++i)
	{
		const SearchEnginesManager::SearchEngineDefinition searchEngine(SearchEnginesManager::getSearchEngine(searchEngines.at(i)));

		if (!searchEngine.isValid())
		{
			continue;
		}

		m_searchEngines[searchEngine.identifier] = {false, searchEngine};

		searchEnginesModel->appendRow(createRow(searchEngine, (searchEngine.identifier == defaultSearchEngine)));
	}

	m_ui->searchViewWidget->setModel(searchEnginesModel);
	m_ui->searchViewWidget->setItemDelegateForColumn(0, new SearchEngineTitleDelegate(this));
	m_ui->searchViewWidget->setItemDelegateForColumn(1, new SearchEngineKeywordDelegate(this));
	m_ui->searchViewWidget->setExclusive(true);
	m_ui->searchSuggestionsCheckBox->setChecked(SettingsManager::getOption(SettingsManager::Search_SearchEnginesSuggestionsOption).toBool());

	QMenu *addSearchEngineMenu(new QMenu(m_ui->addSearchButton));
	addSearchEngineMenu->addAction(tr("New…"), this, &PreferencesSearchPageWidget::createSearchEngine);
	addSearchEngineMenu->addAction(tr("File…"), this, &PreferencesSearchPageWidget::importSearchEngine);
	addSearchEngineMenu->addAction(tr("Readd"))->setMenu(new QMenu(m_ui->addSearchButton));

	m_ui->addSearchButton->setMenu(addSearchEngineMenu);
	m_ui->moveDownSearchButton->setIcon(ThemesManager::createIcon(QLatin1String("arrow-down")));
	m_ui->moveUpSearchButton->setIcon(ThemesManager::createIcon(QLatin1String("arrow-up")));

	updateReaddSearchEngineMenu();

	connect(m_ui->searchFilterLineEditWidget, &LineEditWidget::textChanged, m_ui->searchViewWidget, &ItemViewWidget::setFilterString);
	connect(m_ui->searchViewWidget, &ItemViewWidget::canMoveRowDownChanged, m_ui->moveDownSearchButton, &QToolButton::setEnabled);
	connect(m_ui->searchViewWidget, &ItemViewWidget::canMoveRowUpChanged, m_ui->moveUpSearchButton, &QToolButton::setEnabled);
	connect(m_ui->searchViewWidget, &ItemViewWidget::needsActionsUpdate, this, &PreferencesSearchPageWidget::updateSearchEngineActions);
	connect(m_ui->searchViewWidget, &ItemViewWidget::modified, this, &PreferencesSearchPageWidget::settingsModified);
	connect(m_ui->addSearchButton->menu()->actions().at(2)->menu(), &QMenu::triggered, this, &PreferencesSearchPageWidget::readdSearchEngine);
	connect(m_ui->editSearchButton, &QPushButton::clicked, this, &PreferencesSearchPageWidget::editSearchEngine);
	connect(m_ui->updateSearchButton, &QPushButton::clicked, this, &PreferencesSearchPageWidget::updateSearchEngine);
	connect(m_ui->removeSearchButton, &QPushButton::clicked, this, &PreferencesSearchPageWidget::removeSearchEngine);
	connect(m_ui->moveDownSearchButton, &QToolButton::clicked, m_ui->searchViewWidget, &ItemViewWidget::moveDownRow);
	connect(m_ui->moveUpSearchButton, &QToolButton::clicked, m_ui->searchViewWidget, &ItemViewWidget::moveUpRow);
}
Ejemplo n.º 2
0
InputInterpreter::InterpreterResult InputInterpreter::interpret(const QString &text, InterpreterFlags flags)
{
	InterpreterResult result;

	if (text.isEmpty())
	{
		return result;
	}

	if (!flags.testFlag(NoSearchKeywordsFlag))
	{
		const QString keyword(text.section(QLatin1Char(' '), 0, 0));
		const SearchEnginesManager::SearchEngineDefinition searchEngine(SearchEnginesManager::getSearchEngine(keyword, true));

		if (searchEngine.isValid())
		{
			result.searchEngine = searchEngine.identifier;
			result.searchQuery = text.section(QLatin1Char(' '), 1);
			result.type = InterpreterResult::SearchType;

			return result;
		}

		if (keyword == QLatin1String("?"))
		{
			result.searchQuery = text.section(QLatin1Char(' '), 1);
			result.type = InterpreterResult::SearchType;

			return result;
		}
	}

	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)
		{
			result.bookmark = bookmark;
			result.type = InterpreterResult::BookmarkType;

			return result;
		}
	}

	if (!flags.testFlag(NoBookmarkKeywordsFlag))
	{
		BookmarksItem *bookmark(BookmarksManager::getBookmark(text));

		if (bookmark)
		{
			result.bookmark = bookmark;
			result.type = InterpreterResult::BookmarkType;

			return result;
		}
	}

	const QString localPath(Utils::normalizePath(text));

	if (localPath != text)
	{
		result.url = QUrl::fromLocalFile(localPath);
		result.type = InterpreterResult::UrlType;

		return result;
	}

	const QFileInfo fileInformation(text);

	if (fileInformation.exists() && fileInformation.isAbsolute())
	{
		result.url = QUrl::fromLocalFile(fileInformation.canonicalFilePath());
		result.type = InterpreterResult::UrlType;

		return result;
	}

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

	if (!QHostAddress(text).isNull() || (url.isValid() && (url.isLocalFile() || QRegularExpression(QLatin1String("^(\\w+\\:\\S+)|([\\w\\-]+\\.[a-zA-Z]{2,}(/\\S*)?$)")).match(text).hasMatch())))
	{
		result.url = url;
		result.type = InterpreterResult::UrlType;

		return result;
	}

#if QT_VERSION >= 0x050900
	if (!flags.testFlag(NoHostLookupFlag))
	{
		const int lookupTimeout(SettingsManager::getOption(SettingsManager::AddressField_HostLookupTimeoutOption).toInt());

		if (url.isValid() && lookupTimeout > 0)
		{
			QEventLoop eventLoop;
			const int lookupIdentifier(QHostInfo::lookupHost(url.host(), [&](const QHostInfo &information)
			{
				if (information.error() == QHostInfo::NoError)
				{
					result.url = QUrl::fromUserInput(text);
					result.type = InterpreterResult::UrlType;
				}

				eventLoop.quit();
			}));

			QTimer timer;
			timer.setSingleShot(true);

			connect(&timer, &QTimer::timeout, [&]()
			{
				QHostInfo::abortHostLookup(lookupIdentifier);

				eventLoop.quit();
			});

			timer.start(lookupTimeout);

			if (result.type == InterpreterResult::UnknownType)
			{
				eventLoop.exec();
			}

			if (result.type == InterpreterResult::UrlType)
			{
				return result;
			}
		}
	}
#endif

	result.searchQuery = text;
	result.type = InterpreterResult::SearchType;

	return result;
}