Minor bugfixes
[someplayer] / src / managelibraryform.cpp
1 /*
2  * SomePlayer - An alternate music player for Maemo 5
3  * Copyright (C) 2010 Nikolay (somebody) Tischenko <niktischenko@gmail.com>
4  *
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.
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, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18  */
19
20 #include "managelibraryform.h"
21 #include "ui_managelibraryform.h"
22 #include "library.h"
23 #include <QFileDialog>
24 #include <QDebug>
25
26 #define DEFAULT_PATH_PREFIX "/home/user/MyDocs/"
27
28 using namespace SomePlayer::DataObjects;
29 using namespace SomePlayer::Storage;
30
31 ManageLibraryForm::ManageLibraryForm(Library *library, QWidget *parent) :
32                 QWidget(parent),
33                 ui(new Ui::ManageLibraryForm),
34                 _library (library)
35 {
36         ui->setupUi(this);
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);
43         _busy_widget->hide();
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)));
56         Config config;
57         _icons_theme = config.getValue("ui/iconstheme").toString();
58 }
59
60 ManageLibraryForm::~ManageLibraryForm()
61 {
62         delete ui;
63 }
64
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);
69         }
70 }
71
72 void ManageLibraryForm::refresh() {
73         QList<QString> directories = _library->getDirectories();
74         qSort(directories);
75         int artists_count = _library->getArtistsCount();
76         int albums_count = _library->getAlbumsCount();
77         int tracks_count = _library->getTracksCount();
78
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));
82
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));
90         }
91         ui->dirView->setColumnWidth(0, 70);
92 }
93
94 void ManageLibraryForm::updateIcons() {
95         Config config;
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"));
102         refresh();
103 }
104
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"));
109                 }
110                 ui->dirView->selectionModel()->select(id, QItemSelectionModel::Select);
111         }
112         foreach (QModelIndex id, deselected.indexes()) {
113                 if (id.column() == 0) {
114                         _model->item(id.row())->setIcon(QIcon(":/icons/"+_icons_theme+"/deselect_all.png"));
115                 }
116                 ui->dirView->selectionModel()->select(id, QItemSelectionModel::Deselect);
117         }
118 }
119
120 void ManageLibraryForm::_delete_selected() {
121         QList<QString> directories;
122         QModelIndexList idx = ui->dirView->selectionModel()->selectedIndexes();
123         if (idx.count() == 0) {
124                 return;
125         }
126         CONFIRM_ACTION(this, tr("Delete selected directories?"))
127         foreach (QModelIndex id, idx) {
128                 if (id.column() == 1) {
129                         QString path = id.data().toString();
130                         if (!path.startsWith("/")) {
131                                 path = DEFAULT_PATH_PREFIX+path;
132                         }
133                         directories.append(path);
134                 }
135         }
136         if (!directories.isEmpty()) {
137                 _library->deleteDirectories(directories);
138         }
139         ui->dirView->selectionModel()->clearSelection();
140         refresh();
141         emit refreshLibrary();
142 }
143
144 void ManageLibraryForm::_update_selected() {
145         CONFIRM_ACTION(this, tr("Update selected directories? It may takes several minutes"))
146         QList<QString> directories;
147         QModelIndexList idx = ui->dirView->selectionModel()->selectedIndexes();
148         foreach (QModelIndex id, idx) {
149                 if (id.column() == 1) {
150                         QString path = id.data().toString();
151                         if (!path.startsWith("/")) {
152                                 path = DEFAULT_PATH_PREFIX+path;
153                         }
154                         directories.append(path);
155                 }
156         }
157         if (!directories.isEmpty()) {
158                 _library->updateDirectories(directories);
159         }
160         refresh();
161         emit refreshLibrary();
162 }
163
164 void ManageLibraryForm::_update_all() {
165         CONFIRM_ACTION(this, tr("Update all library? It may takes a long time"))
166         _library->updateAll();
167         refresh();
168         emit refreshLibrary();
169 }
170
171 void ManageLibraryForm::updateTranslations() {
172         ui->retranslateUi(this);
173 }