~ubuntu-branches/ubuntu/saucy/clementine/saucy

« back to all changes in this revision

Viewing changes to src/globalsearch/globalsearchsettingspage.cpp

  • Committer: Package Import Robot
  • Author(s): Thomas PIERSON
  • Date: 2012-01-01 20:43:39 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20120101204339-lsb6nndwhfy05sde
Tags: 1.0.1+dfsg-1
New upstream release. (Closes: #653926, #651611, #657391)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This file is part of Clementine.
 
2
   Copyright 2011, David Sansome <me@davidsansome.com>
 
3
 
 
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.
 
8
 
 
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.
 
13
 
 
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/>.
 
16
*/
 
17
 
 
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"
 
24
 
 
25
#include <QSettings>
 
26
 
 
27
GlobalSearchSettingsPage::GlobalSearchSettingsPage(SettingsDialog* dialog)
 
28
  : SettingsPage(dialog),
 
29
    ui_(new Ui::GlobalSearchSettingsPage)
 
30
{
 
31
  ui_->setupUi(this);
 
32
 
 
33
  ui_->sources->header()->setResizeMode(0, QHeaderView::Stretch);
 
34
  ui_->sources->header()->setResizeMode(1, QHeaderView::ResizeToContents);
 
35
 
 
36
  warning_icon_ = IconLoader::Load("dialog-warning");
 
37
 
 
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*)));
 
43
}
 
44
 
 
45
GlobalSearchSettingsPage::~GlobalSearchSettingsPage() {
 
46
}
 
47
 
 
48
static bool CompareProviderId(SearchProvider* left, SearchProvider* right) {
 
49
  return left->id() < right->id();
 
50
}
 
51
 
 
52
void GlobalSearchSettingsPage::Load() {
 
53
  QSettings s;
 
54
  s.beginGroup(GlobalSearch::kSettingsGroup);
 
55
 
 
56
  GlobalSearch* engine = dialog()->global_search();
 
57
  QList<SearchProvider*> providers = engine->providers();
 
58
 
 
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);
 
62
 
 
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);
 
71
        providers.erase(it);
 
72
        break;
 
73
      }
 
74
    }
 
75
  }
 
76
 
 
77
  // Now add any others that are remaining
 
78
  foreach (SearchProvider* provider, providers) {
 
79
    AddProviderItem(engine, provider);
 
80
  }
 
81
 
 
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());
 
87
}
 
88
 
 
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));
 
95
 
 
96
  UpdateLoggedInState(engine, item, true);
 
97
 
 
98
  ui_->sources->invisibleRootItem()->addChild(item);
 
99
}
 
100
 
 
101
void GlobalSearchSettingsPage::UpdateLoggedInState(GlobalSearch* engine,
 
102
                                                   QTreeWidgetItem* item,
 
103
                                                   bool set_checked_state) {
 
104
  SearchProvider* provider = item->data(0, Qt::UserRole).value<SearchProvider*>();
 
105
 
 
106
  const bool enabled = engine->is_provider_enabled(provider);
 
107
  const bool logged_in = provider->IsLoggedIn();
 
108
 
 
109
  Qt::CheckState check_state = logged_in && enabled ? Qt::Checked : Qt::Unchecked;
 
110
  Qt::ItemFlags flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
 
111
  if (logged_in)
 
112
    flags |= Qt::ItemIsUserCheckable;
 
113
 
 
114
  if (set_checked_state)
 
115
    item->setData(0, Qt::CheckStateRole, check_state);
 
116
  item->setFlags(flags);
 
117
 
 
118
  if (logged_in) {
 
119
    item->setIcon(1, QIcon());
 
120
    item->setText(1, QString());
 
121
  } else {
 
122
    item->setIcon(1, warning_icon_);
 
123
    item->setText(1, tr("Not logged in") + "    ");
 
124
  }
 
125
}
 
126
 
 
127
void GlobalSearchSettingsPage::Save() {
 
128
  QSettings s;
 
129
  s.beginGroup(GlobalSearch::kSettingsGroup);
 
130
 
 
131
  QStringList provider_order;
 
132
 
 
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*>();
 
136
 
 
137
    provider_order << provider->id();
 
138
 
 
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);
 
143
    }
 
144
  }
 
145
 
 
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());
 
152
}
 
153
 
 
154
void GlobalSearchSettingsPage::MoveUp() {
 
155
  MoveCurrentItem(-1);
 
156
}
 
157
 
 
158
void GlobalSearchSettingsPage::MoveDown() {
 
159
  MoveCurrentItem(+1);
 
160
}
 
161
 
 
162
void GlobalSearchSettingsPage::MoveCurrentItem(int d) {
 
163
  QTreeWidgetItem* item = ui_->sources->currentItem();
 
164
  if (!item)
 
165
    return;
 
166
 
 
167
  QTreeWidgetItem* root = ui_->sources->invisibleRootItem();
 
168
 
 
169
  const int row = root->indexOfChild(item);
 
170
  const int new_row = qBound(0, row + d, root->childCount());
 
171
 
 
172
  if (row == new_row)
 
173
    return;
 
174
 
 
175
  root->removeChild(item);
 
176
  root->insertChild(new_row, item);
 
177
 
 
178
  ui_->sources->setCurrentItem(item);
 
179
}
 
180
 
 
181
void GlobalSearchSettingsPage::ConfigureProvider() {
 
182
  QTreeWidgetItem* item = ui_->sources->currentItem();
 
183
  if (!item)
 
184
    return;
 
185
 
 
186
  SearchProvider* provider = item->data(0, Qt::UserRole).value<SearchProvider*>();
 
187
  provider->ShowConfig();
 
188
}
 
189
 
 
190
void GlobalSearchSettingsPage::CurrentProviderChanged(QTreeWidgetItem* item) {
 
191
  if (!item)
 
192
    return;
 
193
 
 
194
  QTreeWidgetItem* root = ui_->sources->invisibleRootItem();
 
195
  SearchProvider* provider = item->data(0, Qt::UserRole).value<SearchProvider*>();
 
196
  const int row = root->indexOfChild(item);
 
197
 
 
198
  ui_->up->setEnabled(row != 0);
 
199
  ui_->down->setEnabled(row != root->childCount() - 1);
 
200
  ui_->configure->setEnabled(provider->can_show_config());
 
201
}
 
202
 
 
203
void GlobalSearchSettingsPage::showEvent(QShowEvent* e) {
 
204
  QWidget::showEvent(e);
 
205
 
 
206
  // Update the logged-in state of each item when we come back to this page in
 
207
  // the dialog.
 
208
  for (int i = 0 ; i < ui_->sources->invisibleRootItem()->childCount() ; ++i) {
 
209
    UpdateLoggedInState(dialog()->global_search(),
 
210
                        ui_->sources->invisibleRootItem()->child(i),
 
211
                        false);
 
212
  }
 
213
}