2011-10-29 12:24:12 +02:00
|
|
|
/* ============================================================
|
|
|
|
* QupZilla - WebKit based browser
|
|
|
|
* Copyright (C) 2010-2011 David Rosca
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
* ============================================================ */
|
2011-10-21 23:26:34 +02:00
|
|
|
#include "searchenginesdialog.h"
|
|
|
|
#include "ui_searchenginesdialog.h"
|
|
|
|
#include "editsearchengine.h"
|
|
|
|
#include "searchenginesmanager.h"
|
|
|
|
#include "mainapplication.h"
|
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
SearchEnginesDialog::SearchEnginesDialog(QWidget* parent)
|
2011-10-21 23:26:34 +02:00
|
|
|
: QDialog(parent)
|
|
|
|
, ui(new Ui::SearchEnginesDialog)
|
|
|
|
{
|
|
|
|
setAttribute(Qt::WA_DeleteOnClose);
|
|
|
|
ui->setupUi(this);
|
|
|
|
|
|
|
|
m_manager = mApp->searchEnginesManager();
|
|
|
|
|
|
|
|
connect(ui->add, SIGNAL(clicked()), this, SLOT(addEngine()));
|
|
|
|
connect(ui->remove, SIGNAL(clicked()), this, SLOT(removeEngine()));
|
|
|
|
connect(ui->edit, SIGNAL(clicked()), this, SLOT(editEngine()));
|
|
|
|
connect(ui->defaults, SIGNAL(clicked()), this, SLOT(defaults()));
|
2011-11-06 17:01:23 +01:00
|
|
|
connect(ui->treeWidget, SIGNAL(itemDoubleClicked(QTreeWidgetItem*, int)), this, SLOT(editEngine()));
|
2011-10-21 23:26:34 +02:00
|
|
|
|
|
|
|
reloadEngines();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SearchEnginesDialog::addEngine()
|
|
|
|
{
|
|
|
|
EditSearchEngine dialog(tr("Add Search Engine"), this);
|
|
|
|
dialog.hideIconLabels();
|
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
if (dialog.exec() != QDialog::Accepted) {
|
2011-10-21 23:26:34 +02:00
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-10-21 23:26:34 +02:00
|
|
|
|
|
|
|
SearchEngine engine;
|
|
|
|
engine.name = dialog.name();
|
|
|
|
engine.url = dialog.url();
|
|
|
|
engine.shortcut = dialog.shortcut();
|
|
|
|
engine.icon = SearchEnginesManager::iconForSearchEngine(dialog.url());
|
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
if (engine.name.isEmpty() || engine.url.isEmpty()) {
|
2011-10-21 23:26:34 +02:00
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-10-21 23:26:34 +02:00
|
|
|
|
|
|
|
QTreeWidgetItem* item = new QTreeWidgetItem();
|
|
|
|
QVariant v;
|
|
|
|
v.setValue<SearchEngine>(engine);
|
|
|
|
item->setData(0, Qt::UserRole, v);
|
|
|
|
|
|
|
|
item->setText(0, engine.name);
|
|
|
|
item->setIcon(0, engine.icon);
|
|
|
|
item->setText(1, engine.shortcut);
|
|
|
|
|
|
|
|
ui->treeWidget->addTopLevelItem(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SearchEnginesDialog::removeEngine()
|
|
|
|
{
|
|
|
|
QTreeWidgetItem* item = ui->treeWidget->currentItem();
|
2011-11-06 17:01:23 +01:00
|
|
|
if (!item || ui->treeWidget->topLevelItemCount() == 1) {
|
2011-10-21 23:26:34 +02:00
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-10-21 23:26:34 +02:00
|
|
|
|
|
|
|
delete item;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SearchEnginesDialog::editEngine()
|
|
|
|
{
|
|
|
|
QTreeWidgetItem* item = ui->treeWidget->currentItem();
|
2011-11-06 17:01:23 +01:00
|
|
|
if (!item) {
|
2011-10-21 23:26:34 +02:00
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-10-21 23:26:34 +02:00
|
|
|
|
|
|
|
SearchEngine engine = item->data(0, Qt::UserRole).value<SearchEngine>();
|
|
|
|
|
|
|
|
EditSearchEngine dialog(tr("Edit Search Engine"), this);
|
|
|
|
|
|
|
|
dialog.setName(engine.name);
|
|
|
|
dialog.setUrl(engine.url);
|
|
|
|
dialog.setShortcut(engine.shortcut);
|
|
|
|
dialog.setIcon(engine.icon);
|
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
if (dialog.exec() != QDialog::Accepted) {
|
2011-10-21 23:26:34 +02:00
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-10-21 23:26:34 +02:00
|
|
|
|
|
|
|
engine.name = dialog.name();
|
|
|
|
engine.url = dialog.url();
|
|
|
|
engine.shortcut = dialog.shortcut();
|
|
|
|
engine.icon = dialog.icon();
|
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
if (engine.name.isEmpty() || engine.url.isEmpty()) {
|
2011-10-21 23:26:34 +02:00
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-10-21 23:26:34 +02:00
|
|
|
|
|
|
|
QVariant v;
|
|
|
|
v.setValue<SearchEngine>(engine);
|
|
|
|
item->setData(0, Qt::UserRole, v);
|
|
|
|
|
|
|
|
item->setText(0, engine.name);
|
|
|
|
item->setIcon(0, engine.icon);
|
|
|
|
item->setText(1, engine.shortcut);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SearchEnginesDialog::defaults()
|
|
|
|
{
|
|
|
|
m_manager->restoreDefaults();
|
|
|
|
reloadEngines();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SearchEnginesDialog::reloadEngines()
|
|
|
|
{
|
|
|
|
ui->treeWidget->clear();
|
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
foreach(SearchEngine en, m_manager->allEngines()) {
|
2011-10-21 23:26:34 +02:00
|
|
|
QTreeWidgetItem* item = new QTreeWidgetItem();
|
|
|
|
item->setIcon(0, en.icon);
|
|
|
|
item->setText(0, en.name);
|
|
|
|
item->setText(1, en.shortcut);
|
|
|
|
QVariant v;
|
|
|
|
v.setValue<SearchEngine>(en);
|
|
|
|
item->setData(0, Qt::UserRole, v);
|
|
|
|
|
|
|
|
ui->treeWidget->addTopLevelItem(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SearchEnginesDialog::accept()
|
|
|
|
{
|
2011-11-06 17:01:23 +01:00
|
|
|
if (ui->treeWidget->topLevelItemCount() < 1) {
|
2011-10-21 23:26:34 +02:00
|
|
|
return;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-10-21 23:26:34 +02:00
|
|
|
|
|
|
|
QList<SearchEngine> allEngines;
|
|
|
|
|
|
|
|
for (int i = 0; i < ui->treeWidget->topLevelItemCount(); i++) {
|
|
|
|
QTreeWidgetItem* item = ui->treeWidget->topLevelItem(i);
|
2011-11-06 17:01:23 +01:00
|
|
|
if (!item) {
|
2011-10-21 23:26:34 +02:00
|
|
|
continue;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-10-21 23:26:34 +02:00
|
|
|
|
|
|
|
SearchEngine engine = item->data(0, Qt::UserRole).value<SearchEngine>();
|
|
|
|
allEngines.append(engine);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_manager->setAllEngines(allEngines);
|
|
|
|
|
|
|
|
QDialog::accept();
|
|
|
|
}
|
|
|
|
|
|
|
|
SearchEnginesDialog::~SearchEnginesDialog()
|
|
|
|
{
|
|
|
|
delete ui;
|
|
|
|
}
|