Icons for update-all & update-pls
[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, "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         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;
128                         }
129                         directories.append(path);
130                 }
131         }
132         if (!directories.isEmpty()) {
133                 _library->deleteDirectories(directories);
134         }
135         ui->dirView->selectionModel()->clearSelection();
136         refresh();
137         emit refreshLibrary();
138 }
139
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;
148                         }
149                         directories.append(path);
150                 }
151         }
152         if (!directories.isEmpty()) {
153                 _library->updateDirectories(directories);
154         }
155         refresh();
156         emit refreshLibrary();
157 }
158
159 void ManageLibraryForm::_update_all() {
160         _library->updateAll();
161         refresh();
162         emit refreshLibrary();
163 }