1
/* This file is part of Clementine.
2
Copyright 2011, David Sansome <me@davidsansome.com>
4
Clementine is free software: you can redistribute it and/or modify
5
it under the terms of the GNU General Public License as published by
6
the Free Software Foundation, either version 3 of the License, or
7
(at your option) any later version.
9
Clementine is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
GNU General Public License for more details.
14
You should have received a copy of the GNU General Public License
15
along with Clementine. If not, see <http://www.gnu.org/licenses/>.
18
#include "globalsearch.h"
19
#include "globalsearchsettingspage.h"
20
#include "core/logging.h"
21
#include "ui/iconloader.h"
22
#include "ui/settingsdialog.h"
23
#include "ui_globalsearchsettingspage.h"
27
GlobalSearchSettingsPage::GlobalSearchSettingsPage(SettingsDialog* dialog)
28
: SettingsPage(dialog),
29
ui_(new Ui::GlobalSearchSettingsPage)
33
ui_->sources->header()->setResizeMode(0, QHeaderView::Stretch);
34
ui_->sources->header()->setResizeMode(1, QHeaderView::ResizeToContents);
36
warning_icon_ = IconLoader::Load("dialog-warning");
38
connect(ui_->up, SIGNAL(clicked()), SLOT(MoveUp()));
39
connect(ui_->down, SIGNAL(clicked()), SLOT(MoveDown()));
40
connect(ui_->configure, SIGNAL(clicked()), SLOT(ConfigureProvider()));
41
connect(ui_->sources, SIGNAL(currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)),
42
SLOT(CurrentProviderChanged(QTreeWidgetItem*)));
45
GlobalSearchSettingsPage::~GlobalSearchSettingsPage() {
48
static bool CompareProviderId(SearchProvider* left, SearchProvider* right) {
49
return left->id() < right->id();
52
void GlobalSearchSettingsPage::Load() {
54
s.beginGroup(GlobalSearch::kSettingsGroup);
56
GlobalSearch* engine = dialog()->global_search();
57
QList<SearchProvider*> providers = engine->providers();
59
// Sort the list of providers alphabetically (by id) initially, so any that
60
// aren't in the provider_order list will take this order.
61
qSort(providers.begin(), providers.end(), CompareProviderId);
63
// Add the ones in the configured list first
64
ui_->sources->clear();
65
foreach (const QString& id, s.value("provider_order", QStringList() << "library").toStringList()) {
66
// Find a matching provider for this id
67
for (QList<SearchProvider*>::iterator it = providers.begin();
68
it != providers.end() ; ++it) {
69
if ((*it)->id() == id) {
70
AddProviderItem(engine, *it);
77
// Now add any others that are remaining
78
foreach (SearchProvider* provider, providers) {
79
AddProviderItem(engine, provider);
82
ui_->show_globalsearch->setChecked(s.value("show_globalsearch", true).toBool());
83
ui_->hide_others->setChecked(s.value("hide_others", false).toBool());
84
ui_->combine->setChecked(s.value("combine_identical_results", true).toBool());
85
ui_->tooltip->setChecked(s.value("tooltip", true).toBool());
86
ui_->tooltip_help->setChecked(s.value("tooltip_help", true).toBool());
89
void GlobalSearchSettingsPage::AddProviderItem(GlobalSearch* engine,
90
SearchProvider* provider) {
91
QTreeWidgetItem* item = new QTreeWidgetItem;
92
item->setText(0, provider->name());
93
item->setIcon(0, provider->icon());
94
item->setData(0, Qt::UserRole, QVariant::fromValue(provider));
96
UpdateLoggedInState(engine, item, true);
98
ui_->sources->invisibleRootItem()->addChild(item);
101
void GlobalSearchSettingsPage::UpdateLoggedInState(GlobalSearch* engine,
102
QTreeWidgetItem* item,
103
bool set_checked_state) {
104
SearchProvider* provider = item->data(0, Qt::UserRole).value<SearchProvider*>();
106
const bool enabled = engine->is_provider_enabled(provider);
107
const bool logged_in = provider->IsLoggedIn();
109
Qt::CheckState check_state = logged_in && enabled ? Qt::Checked : Qt::Unchecked;
110
Qt::ItemFlags flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
112
flags |= Qt::ItemIsUserCheckable;
114
if (set_checked_state)
115
item->setData(0, Qt::CheckStateRole, check_state);
116
item->setFlags(flags);
119
item->setIcon(1, QIcon());
120
item->setText(1, QString());
122
item->setIcon(1, warning_icon_);
123
item->setText(1, tr("Not logged in") + " ");
127
void GlobalSearchSettingsPage::Save() {
129
s.beginGroup(GlobalSearch::kSettingsGroup);
131
QStringList provider_order;
133
for (int i=0 ; i<ui_->sources->invisibleRootItem()->childCount() ; ++i) {
134
const QTreeWidgetItem* item = ui_->sources->invisibleRootItem()->child(i);
135
const SearchProvider* provider = item->data(0, Qt::UserRole).value<SearchProvider*>();
137
provider_order << provider->id();
139
// Only save the enabled state for this provider if it's logged in.
140
if (item->flags() & Qt::ItemIsUserCheckable) {
141
s.setValue("enabled_" + provider->id(),
142
item->data(0, Qt::CheckStateRole).toInt() == Qt::Checked);
146
s.setValue("provider_order", provider_order);
147
s.setValue("show_globalsearch", ui_->show_globalsearch->isChecked());
148
s.setValue("hide_others", ui_->hide_others->isChecked() && ui_->show_globalsearch->isChecked());
149
s.setValue("combine_identical_results", ui_->combine->isChecked());
150
s.setValue("tooltip", ui_->tooltip->isChecked());
151
s.setValue("tooltip_help", ui_->tooltip_help->isChecked());
154
void GlobalSearchSettingsPage::MoveUp() {
158
void GlobalSearchSettingsPage::MoveDown() {
162
void GlobalSearchSettingsPage::MoveCurrentItem(int d) {
163
QTreeWidgetItem* item = ui_->sources->currentItem();
167
QTreeWidgetItem* root = ui_->sources->invisibleRootItem();
169
const int row = root->indexOfChild(item);
170
const int new_row = qBound(0, row + d, root->childCount());
175
root->removeChild(item);
176
root->insertChild(new_row, item);
178
ui_->sources->setCurrentItem(item);
181
void GlobalSearchSettingsPage::ConfigureProvider() {
182
QTreeWidgetItem* item = ui_->sources->currentItem();
186
SearchProvider* provider = item->data(0, Qt::UserRole).value<SearchProvider*>();
187
provider->ShowConfig();
190
void GlobalSearchSettingsPage::CurrentProviderChanged(QTreeWidgetItem* item) {
194
QTreeWidgetItem* root = ui_->sources->invisibleRootItem();
195
SearchProvider* provider = item->data(0, Qt::UserRole).value<SearchProvider*>();
196
const int row = root->indexOfChild(item);
198
ui_->up->setEnabled(row != 0);
199
ui_->down->setEnabled(row != root->childCount() - 1);
200
ui_->configure->setEnabled(provider->can_show_config());
203
void GlobalSearchSettingsPage::showEvent(QShowEvent* e) {
204
QWidget::showEvent(e);
206
// Update the logged-in state of each item when we come back to this page in
208
for (int i = 0 ; i < ui_->sources->invisibleRootItem()->childCount() ; ++i) {
209
UpdateLoggedInState(dialog()->global_search(),
210
ui_->sources->invisibleRootItem()->child(i),