2 * SomePlayer - An alternate music player for Maemo 5
3 * Copyright (C) 2010 Nikolay (somebody) Tischenko <niktischenko@gmail.com>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 #include "managelibraryform.h"
21 #include "ui_managelibraryform.h"
23 #include <QFileDialog>
26 #define DEFAULT_PATH_PREFIX "/home/user/MyDocs/"
28 using namespace SomePlayer::DataObjects;
29 using namespace SomePlayer::Storage;
31 ManageLibraryForm::ManageLibraryForm(Library *library, QWidget *parent) :
33 ui(new Ui::ManageLibraryForm),
37 setAttribute(Qt::WA_Maemo5StackedWindow);
38 setWindowFlags(Qt::Window | windowFlags());
39 _model = new QStandardItemModel(0, 2, this);
40 ui->dirView->setModel(_model);
41 _busy_widget = new BusyWidget(this);
42 ui->mainLayout->addWidget(_busy_widget);
44 connect(ui->addButton, SIGNAL(clicked()), this, SLOT(_add()));
45 connect(ui->deleteButton, SIGNAL(clicked()), this, SLOT(_delete_selected()));
46 connect(ui->updateButton, SIGNAL(clicked()), this, SLOT(_update_selected()));
47 connect(ui->updateAllButton, SIGNAL(clicked()), this, SLOT(_update_all()));
48 connect(ui->updatePlsButton, SIGNAL(clicked()), _library, SLOT(updatePlaylists()));
49 connect(_library, SIGNAL(allCount(int)), _busy_widget, SLOT(setMax(int)));
50 connect(_library, SIGNAL(tick()), _busy_widget, SLOT(tick()));
51 connect(_library, SIGNAL(done()), _busy_widget, SLOT(hide()));
52 connect(_library, SIGNAL(started()), _busy_widget, SLOT(show()));
53 connect(_library, SIGNAL(done()), this, SLOT(refresh()));
54 connect(ui->dirView->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
55 this, SLOT(_process_selection(QItemSelection,QItemSelection)));
57 _icons_theme = config.getValue("ui/iconstheme").toString();
60 ManageLibraryForm::~ManageLibraryForm()
65 void ManageLibraryForm::_add() {
66 QString directory = QFileDialog::getExistingDirectory (this, tr("Select directory"), DEFAULT_PATH_PREFIX, QFileDialog::ShowDirsOnly );
67 if (!directory.isEmpty()) {
68 _library->addDirectory(directory);
72 void ManageLibraryForm::refresh() {
73 QList<QString> directories = _library->getDirectories();
75 int artists_count = _library->getArtistsCount();
76 int albums_count = _library->getAlbumsCount();
77 int tracks_count = _library->getTracksCount();
79 ui->artistsLabel->setText(QString("%1 artists").arg(artists_count));
80 ui->albumsLabel->setText(QString("%1 albums").arg(albums_count));
81 ui->tracksView->setText(QString("%1 tracks").arg(tracks_count));
83 int dcount = directories.count();
84 _model->setRowCount(dcount);
85 for (int i = 0; i < dcount; i++) {
86 _model->setItem(i, 0, new QStandardItem(QIcon(":/icons/"+_icons_theme+"/deselect_all.png"), ""));
87 QString dir = directories.at(i);
88 dir.replace(DEFAULT_PATH_PREFIX, "");
89 _model->setItem(i, 1, new QStandardItem(dir));
91 ui->dirView->setColumnWidth(0, 70);
94 void ManageLibraryForm::updateIcons() {
96 _icons_theme = config.getValue("ui/iconstheme").toString();
97 ui->addButton->setIcon(QIcon(":/icons/"+_icons_theme+"/add.png"));
98 ui->deleteButton->setIcon(QIcon(":/icons/"+_icons_theme+"/delete.png"));
99 ui->updateButton->setIcon(QIcon(":/icons/"+_icons_theme+"/update.png"));
100 ui->updateAllButton->setIcon(QIcon(":/icons/"+_icons_theme+"/update-all.png"));
101 ui->updatePlsButton->setIcon(QIcon(":/icons/"+_icons_theme+"/update-pls.png"));
105 void ManageLibraryForm::_process_selection(QItemSelection selected, QItemSelection deselected) {
106 foreach (QModelIndex id, selected.indexes()) {
107 if (id.column() == 0) {
108 _model->item(id.row())->setIcon(QIcon(":/icons/"+_icons_theme+"/select_all.png"));
110 ui->dirView->selectionModel()->select(id, QItemSelectionModel::Select);
112 foreach (QModelIndex id, deselected.indexes()) {
113 if (id.column() == 0) {
114 _model->item(id.row())->setIcon(QIcon(":/icons/"+_icons_theme+"/deselect_all.png"));
116 ui->dirView->selectionModel()->select(id, QItemSelectionModel::Deselect);
120 void ManageLibraryForm::_delete_selected() {
121 QList<QString> directories;
122 QModelIndexList idx = ui->dirView->selectionModel()->selectedIndexes();
123 foreach (QModelIndex id, idx) {
124 if (id.column() == 1) {
125 QString path = id.data().toString();
126 if (!path.startsWith("/")) {
127 path = DEFAULT_PATH_PREFIX+path;
129 directories.append(path);
132 if (!directories.isEmpty()) {
133 _library->deleteDirectories(directories);
135 ui->dirView->selectionModel()->clearSelection();
137 emit refreshLibrary();
140 void ManageLibraryForm::_update_selected() {
141 QList<QString> directories;
142 QModelIndexList idx = ui->dirView->selectionModel()->selectedIndexes();
143 foreach (QModelIndex id, idx) {
144 if (id.column() == 1) {
145 QString path = id.data().toString();
146 if (!path.startsWith("/")) {
147 path = DEFAULT_PATH_PREFIX+path;
149 directories.append(path);
152 if (!directories.isEmpty()) {
153 _library->updateDirectories(directories);
156 emit refreshLibrary();
159 void ManageLibraryForm::_update_all() {
160 _library->updateAll();
162 emit refreshLibrary();
165 void ManageLibraryForm::updateTranslations() {
166 ui->retranslateUi(this);