void AddressCompletionModel::setFilter(const QString &filter)
{
	if (m_filter.isEmpty() && !filter.isEmpty())
	{
		m_types = UnknownCompletionType;

		if (SettingsManager::getValue(QLatin1String("AddressField/SuggestBookmarks")).toBool())
		{
			m_types |= BookmarksCompletionType;
		}

		if (SettingsManager::getValue(QLatin1String("AddressField/SuggestHistory")).toBool())
		{
			m_types |= HistoryCompletionType;
		}

		if (SettingsManager::getValue(QLatin1String("AddressField/SuggestSearch")).toBool())
		{
			m_types |= SearchSuggestionsCompletionType;

			m_defaultSearchEngine = SearchEnginesManager::getSearchEngine();
		}

		if (SettingsManager::getValue(QLatin1String("AddressField/SuggestSpecialPages")).toBool())
		{
			m_types |= SpecialPagesCompletionType;
		}

		if (SettingsManager::getValue(QLatin1String("AddressField/SuggestLocalFiles")).toBool())
		{
			m_types |= LocalPathSuggestionsCompletionType;
		}
	}

	m_filter = filter;
	m_showCompletionCategories = SettingsManager::getValue(QLatin1String("AddressField/ShowCompletionCategories")).toBool();

	if (m_filter.isEmpty())
	{
		if (m_updateTimer != 0)
		{
			killTimer(m_updateTimer);

			m_updateTimer = 0;
		}

		beginResetModel();

		m_completions.clear();

		endResetModel();

		emit completionReady(QString());
	}
	else if (m_updateTimer == 0)
	{
		m_updateTimer = startTimer(50);
	}
}
Ejemplo n.º 2
0
		optionChanged(SettingsManager::AddressField_ShowFeedsIconOption, SettingsManager::getValue(SettingsManager::AddressField_ShowFeedsIconOption));
		optionChanged(SettingsManager::AddressField_ShowUrlIconOption, SettingsManager::getValue(SettingsManager::AddressField_ShowUrlIconOption));

		m_lineEdit->setPlaceholderText(tr("Enter address or search…"));

		connect(SettingsManager::getInstance(), SIGNAL(valueChanged(int,QVariant)), this, SLOT(optionChanged(int,QVariant)));

		if (toolBar->getIdentifier() != ToolBarsManager::NavigationBar)
		{
			connect(toolBar, SIGNAL(windowChanged(Window*)), this, SLOT(setWindow(Window*)));
		}
	}

	connect(this, SIGNAL(activated(QString)), this, SLOT(openUrl(QString)));
	connect(m_lineEdit, SIGNAL(textDropped(QString)), this, SLOT(handleUserInput(QString)));
	connect(m_completionModel, SIGNAL(completionReady(QString)), this, SLOT(setCompletion(QString)));
	connect(BookmarksManager::getModel(), SIGNAL(modelModified()), this, SLOT(updateBookmark()));
	connect(HistoryManager::getTypedHistoryModel(), SIGNAL(modelModified()), this, SLOT(updateLineEdit()));
}

void AddressWidget::changeEvent(QEvent *event)
{
	ComboBoxWidget::changeEvent(event);

	switch (event->type())
	{
		case QEvent::LanguageChange:
			if (!m_isUsingSimpleMode)
			{
				m_lineEdit->setPlaceholderText(tr("Enter address or search…"));
			}
void AddressCompletionModel::timerEvent(QTimerEvent *event)
{
	if (event->timerId() == m_updateTimer)
	{
		killTimer(m_updateTimer);

		m_updateTimer = 0;

		if (m_filter.isEmpty())
		{
			return;
		}

		QList<CompletionEntry> completions;

		if (m_types.testFlag(SearchSuggestionsCompletionType))
		{
			CompletionEntry completionEntry(QUrl(), m_defaultSearchEngine.title, QString(), Utils::getIcon(QLatin1String("edit-find")), SearchSuggestionType);
			completionEntry.text = m_filter;

			completions.append(completionEntry);
		}

		if (m_types.testFlag(BookmarksCompletionType))
		{
			const QList<BookmarksModel::BookmarkMatch> bookmarks = BookmarksManager::findBookmarks(m_filter);

			for (int i = 0; i < bookmarks.count(); ++i)
			{
				CompletionEntry completionEntry(bookmarks.at(i).bookmark->data(BookmarksModel::UrlRole).toUrl(), bookmarks.at(i).bookmark->data(BookmarksModel::TitleRole).toString(), bookmarks.at(i).match, bookmarks.at(i).bookmark->data(Qt::DecorationRole).value<QIcon>(), BookmarkType);
				completionEntry.keyword = bookmarks.at(i).bookmark->data(BookmarksModel::KeywordRole).toString();

				if (completionEntry.keyword.startsWith(m_filter))
				{
					completionEntry.match = completionEntry.keyword;
				}

				completions.append(completionEntry);
			}
		}

		if (m_types.testFlag(HistoryCompletionType))
		{
			const QList<HistoryModel::HistoryEntryMatch> entries = HistoryManager::findEntries(m_filter);

			for (int i = 0; i < entries.count(); ++i)
			{
				completions.append(CompletionEntry(entries.at(i).entry->data(HistoryModel::UrlRole).toUrl(), entries.at(i).entry->data(HistoryModel::TitleRole).toString(), entries.at(i).match, entries.at(i).entry->data(Qt::DecorationRole).value<QIcon>(), (entries.at(i).isTypedIn ? TypedInHistoryType : HistoryType)));
			}
		}

		if (m_types.testFlag(SpecialPageCompletionType))
		{
			const QStringList specialPages = AddonsManager::getSpecialPages();

			for (int i = 0; i < specialPages.count(); ++i)
			{
				const AddonsManager::SpecialPageInformation information = AddonsManager::getSpecialPage(specialPages.at(i));

				if (information.url.toString().startsWith(m_filter))
				{
					completions.append(CompletionEntry(information.url, information.getTitle(), QString(), information.icon, SpecialPageType));
				}
			}
		}

		beginResetModel();

		m_completions = completions;

		endResetModel();

		emit completionReady(m_filter);
	}
}
void AddressCompletionModel::timerEvent(QTimerEvent *event)
{
	if (event->timerId() == m_updateTimer)
	{
		killTimer(m_updateTimer);

		m_updateTimer = 0;

		if (m_filter.isEmpty())
		{
			return;
		}

		QList<CompletionEntry> completions;

		if (m_types.testFlag(SearchSuggestionsCompletionType))
		{
			const QString keyword(m_filter.section(QLatin1Char(' '), 0, 0));
			const SearchEnginesManager::SearchEngineDefinition searchEngine(SearchEnginesManager::getSearchEngine(keyword, true));
			QString title(m_defaultSearchEngine.title);
			QString text(m_filter);
			QIcon icon(m_defaultSearchEngine.icon);

			if (!searchEngine.identifier.isEmpty())
			{
				title = searchEngine.title;
				text = m_filter.section(QLatin1Char(' '), 1, -1);
				icon = searchEngine.icon;
			}
			else if (keyword == QLatin1String("?"))
			{
				text = m_filter.section(QLatin1Char(' '), 1, -1);
			}

			if (icon.isNull())
			{
				icon = ThemesManager::getIcon(QLatin1String("edit-find"));
			}

			if (m_showCompletionCategories)
			{
				completions.append(CompletionEntry(QUrl(), tr("Search with %1").arg(title), QString(), QIcon(), HeaderType));

				title = QString();
			}

			CompletionEntry completionEntry(QUrl(), title, QString(), icon, SearchSuggestionType);
			completionEntry.text = text;

			completions.append(completionEntry);
		}

		if (m_types.testFlag(BookmarksCompletionType))
		{
			const QList<BookmarksModel::BookmarkMatch> bookmarks(BookmarksManager::findBookmarks(m_filter));

			if (m_showCompletionCategories && !bookmarks.isEmpty())
			{
				completions.append(CompletionEntry(QUrl(), tr("Bookmarks"), QString(), QIcon(), HeaderType));
			}

			for (int i = 0; i < bookmarks.count(); ++i)
			{
				CompletionEntry completionEntry(bookmarks.at(i).bookmark->data(BookmarksModel::UrlRole).toUrl(), bookmarks.at(i).bookmark->data(BookmarksModel::TitleRole).toString(), bookmarks.at(i).match, bookmarks.at(i).bookmark->data(Qt::DecorationRole).value<QIcon>(), BookmarkType);
				completionEntry.keyword = bookmarks.at(i).bookmark->data(BookmarksModel::KeywordRole).toString();

				if (completionEntry.keyword.startsWith(m_filter))
				{
					completionEntry.match = completionEntry.keyword;
				}

				completions.append(completionEntry);
			}
		}

		if (m_types.testFlag(LocalPathSuggestionsCompletionType) && m_filter.contains(QDir::separator()))
		{
			const QString directory(m_filter.section(QDir::separator(), 0, -2) + QDir::separator());
			const QString prefix(m_filter.section(QDir::separator(), -1, -1));
			const QList<QFileInfo> entries(QDir(Utils::normalizePath(directory)).entryInfoList(QDir::AllEntries | QDir::NoDotAndDotDot));
			const QFileIconProvider iconProvider;
			bool wasAdded(!m_showCompletionCategories);

			for (int i = 0; i < entries.count(); ++i)
			{
				if (entries.at(i).fileName().startsWith(prefix, Qt::CaseInsensitive))
				{
					const QString path(directory + entries.at(i).fileName());
					const QMimeType type(QMimeDatabase().mimeTypeForFile(entries.at(i), QMimeDatabase::MatchExtension));

					if (!wasAdded)
					{
						completions.append(CompletionEntry(QUrl(), tr("Local files"), QString(), QIcon(), HeaderType));

						wasAdded = true;
					}

					completions.append(CompletionEntry(path, path, QString(), QIcon::fromTheme(type.iconName(), iconProvider.icon(entries.at(i))), LocalPathType));
				}
			}
		}

		if (m_types.testFlag(HistoryCompletionType))
		{
			const QList<HistoryModel::HistoryEntryMatch> entries(HistoryManager::findEntries(m_filter));

			if (m_showCompletionCategories && !entries.isEmpty())
			{
				completions.append(CompletionEntry(QUrl(), tr("History"), QString(), QIcon(), HeaderType));
			}

			for (int i = 0; i < entries.count(); ++i)
			{
				completions.append(CompletionEntry(entries.at(i).entry->data(HistoryModel::UrlRole).toUrl(), entries.at(i).entry->data(HistoryModel::TitleRole).toString(), entries.at(i).match, entries.at(i).entry->data(Qt::DecorationRole).value<QIcon>(), (entries.at(i).isTypedIn ? TypedInHistoryType : HistoryType)));
			}
		}

		if (m_types.testFlag(SpecialPagesCompletionType))
		{
			const QStringList specialPages(AddonsManager::getSpecialPages());
			bool wasAdded(!m_showCompletionCategories);

			for (int i = 0; i < specialPages.count(); ++i)
			{
				const AddonsManager::SpecialPageInformation information(AddonsManager::getSpecialPage(specialPages.at(i)));

				if (information.url.toString().startsWith(m_filter))
				{
					if (!wasAdded)
					{
						completions.append(CompletionEntry(QUrl(), tr("Special pages"), QString(), QIcon(), HeaderType));

						wasAdded = true;
					}

					completions.append(CompletionEntry(information.url, information.getTitle(), QString(), information.icon, SpecialPageType));
				}
			}
		}

		beginResetModel();

		m_completions = completions;

		endResetModel();

		emit completionReady(m_filter);
	}
}