1 /**************************************************************************
2 * Otter Browser: Web browser controlled by the user, not vice-versa.
3 * Copyright (C) 2013 - 2017 Michal Dutkiewicz aka Emdek <michal@emdek.pl>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 **************************************************************************/
19 
20 #include "OpenBookmarkDialog.h"
21 #include "../core/BookmarksManager.h"
22 #include "../core/BookmarksModel.h"
23 #include "../core/SessionsManager.h"
24 
25 #include "ui_OpenBookmarkDialog.h"
26 
27 #include <QtCore/QStringListModel>
28 
29 namespace Otter
30 {
31 
OpenBookmarkDialog(ActionExecutor::Object executor,QWidget * parent)32 OpenBookmarkDialog::OpenBookmarkDialog(ActionExecutor::Object executor, QWidget *parent) : Dialog(parent),
33 	m_completer(nullptr),
34 	m_executor(executor),
35 	m_ui(new Ui::OpenBookmarkDialog)
36 {
37 	m_ui->setupUi(this);
38 
39 	m_completer = new QCompleter(new QStringListModel(BookmarksManager::getKeywords()), m_ui->lineEditWidget);
40 	m_completer->setCaseSensitivity(Qt::CaseSensitive);
41 	m_completer->setCompletionMode(QCompleter::InlineCompletion);
42 	m_completer->setFilterMode(Qt::MatchStartsWith);
43 
44 	connect(this, &OpenBookmarkDialog::accepted, this, &OpenBookmarkDialog::openBookmark);
45 	connect(m_ui->lineEditWidget, &LineEditWidget::textEdited, this, &OpenBookmarkDialog::setCompletion);
46 }
47 
~OpenBookmarkDialog()48 OpenBookmarkDialog::~OpenBookmarkDialog()
49 {
50 	delete m_ui;
51 }
52 
changeEvent(QEvent * event)53 void OpenBookmarkDialog::changeEvent(QEvent *event)
54 {
55 	QDialog::changeEvent(event);
56 
57 	if (event->type() == QEvent::LanguageChange)
58 	{
59 		m_ui->retranslateUi(this);
60 	}
61 }
62 
openBookmark()63 void OpenBookmarkDialog::openBookmark()
64 {
65 	const BookmarksModel::Bookmark *bookmark(BookmarksManager::getBookmark(m_ui->lineEditWidget->text()));
66 
67 	if (bookmark && m_executor.isValid())
68 	{
69 		m_executor.triggerAction(ActionsManager::OpenBookmarkAction, {{QLatin1String("bookmark"), bookmark->getIdentifier()}, {QLatin1String("hints"), QVariant(SessionsManager::calculateOpenHints(SessionsManager::DefaultOpen))}});
70 	}
71 }
72 
setCompletion(const QString & text)73 void OpenBookmarkDialog::setCompletion(const QString &text)
74 {
75 	m_completer->setCompletionPrefix(text);
76 
77 	if (m_completer->completionCount() == 1)
78 	{
79 		const BookmarksModel::Bookmark *bookmark(BookmarksManager::getBookmark(m_completer->currentCompletion()));
80 
81 		if (bookmark && m_executor.isValid())
82 		{
83 			m_executor.triggerAction(ActionsManager::OpenBookmarkAction, {{QLatin1String("bookmark"), bookmark->getIdentifier()}, {QLatin1String("hints"), QVariant(SessionsManager::calculateOpenHints(SessionsManager::DefaultOpen))}});
84 		}
85 
86 		close();
87 	}
88 }
89 
90 }
91