1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
|
/*
* Cantata
*
* Copyright (c) 2011-2022 Craig Drummond <craig.p.drummond@gmail.com>
*
* ----
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "librarypage.h"
#include "covers.h"
#include "customactions.h"
#include "models/mpdlibrarymodel.h"
#include "mpd-interface/mpdconnection.h"
#include "mpd-interface/mpdstats.h"
#include "settings.h"
#include "stdactions.h"
#include "support/actioncollection.h"
#include "support/messagebox.h"
#include "widgets/genrecombo.h"
#include "widgets/menubutton.h"
#include <QRandomGenerator>
LibraryPage::LibraryPage(QWidget* p)
: SinglePageWidget(p)
{
genreCombo = new GenreCombo(this);
connect(StdActions::self()->addRandomAlbumToPlayQueueAction, SIGNAL(triggered()), SLOT(addRandomAlbum()));
connect(MPDConnection::self(), SIGNAL(updatingLibrary(time_t)), view, SLOT(updating()));
connect(MPDConnection::self(), SIGNAL(updatedLibrary()), view, SLOT(updated()));
connect(MPDConnection::self(), SIGNAL(updatingDatabase()), view, SLOT(updating()));
connect(MPDConnection::self(), SIGNAL(updatedDatabase()), view, SLOT(updated()));
connect(view, SIGNAL(itemsSelected(bool)), this, SLOT(controlActions()));
connect(view, SIGNAL(doubleClicked(const QModelIndex&)), this, SLOT(itemDoubleClicked(const QModelIndex&)));
view->setModel(MpdLibraryModel::self());
connect(MpdLibraryModel::self(), SIGNAL(modelReset()), this, SLOT(modelReset()));
view->allowCategorized();
// Settings...
Configuration config(metaObject()->className());
view->setMode(ItemView::Mode_DetailedTree);
MpdLibraryModel::self()->load(config);
config.beginGroup(SqlLibraryModel::groupingStr(MpdLibraryModel::self()->topLevel()));
view->load(config);
view->setSearchToolTip(tr("<p>Enter a string to search artist, album, title, etc. To filter based on year, add <i>#year-range</i> to search string - e.g.</p><ul>"
"<li><b><i>#2000</i></b> return tracks from 2000</li>"
"<li><b><i>#1980-1989</i></b> return tracks from the 80's</li>"
"<li><b><i>Blah #2000</i></b> to search for string <i>Blah</i> and only return tracks from 2000</li>"
"</ul></p>"));
showArtistImagesAction = new QAction(tr("Show Artist Images"), this);
showArtistImagesAction->setCheckable(true);
libraryAlbumSortAction = createMenuGroup(tr("Sort Albums"), QList<MenuItem>() << MenuItem(tr("Name"), LibraryDb::AS_AlArYr) << MenuItem(tr("Year"), LibraryDb::AS_YrAlAr),
MpdLibraryModel::self()->libraryAlbumSort(), this, SLOT(libraryAlbumSortChanged()));
albumAlbumSortAction = createMenuGroup(tr("Sort Albums"), QList<MenuItem>() << MenuItem(tr("Album, Artist, Year"), LibraryDb::AS_AlArYr) << MenuItem(tr("Album, Year, Artist"), LibraryDb::AS_AlYrAr) << MenuItem(tr("Artist, Album, Year"), LibraryDb::AS_ArAlYr) << MenuItem(tr("Artist, Year, Album"), LibraryDb::AS_ArYrAl) << MenuItem(tr("Year, Album, Artist"), LibraryDb::AS_YrAlAr) << MenuItem(tr("Year, Artist, Album"), LibraryDb::AS_YrArAl) << MenuItem(tr("Modified Date"), LibraryDb::AS_Modified),
MpdLibraryModel::self()->albumAlbumSort(), this, SLOT(albumAlbumSortChanged()));
MenuButton* menu = new MenuButton(this);
viewAction = createViewMenu(QList<ItemView::Mode>() << ItemView::Mode_BasicTree << ItemView::Mode_SimpleTree
<< ItemView::Mode_DetailedTree << ItemView::Mode_List
<< ItemView::Mode_IconTop << ItemView::Mode_Categorized);
menu->addAction(viewAction);
menu->addAction(createMenuGroup(tr("Group By"), QList<MenuItem>() << MenuItem(tr("Genre"), SqlLibraryModel::T_Genre) << MenuItem(tr("Artist"), SqlLibraryModel::T_Artist) << MenuItem(tr("Album"), SqlLibraryModel::T_Album),
MpdLibraryModel::self()->topLevel(), this, SLOT(groupByChanged())));
genreCombo->setVisible(SqlLibraryModel::T_Genre != MpdLibraryModel::self()->topLevel());
menu->addAction(libraryAlbumSortAction);
menu->addAction(albumAlbumSortAction);
showArtistImagesAction->setChecked(MpdLibraryModel::self()->useArtistImages());
menu->addAction(showArtistImagesAction);
connect(showArtistImagesAction, SIGNAL(toggled(bool)), this, SLOT(showArtistImagesChanged(bool)));
showArtistImagesAction->setVisible(SqlLibraryModel::T_Album != MpdLibraryModel::self()->topLevel() && ItemView::Mode_IconTop != view->viewMode());
albumAlbumSortAction->setVisible(SqlLibraryModel::T_Album == MpdLibraryModel::self()->topLevel());
libraryAlbumSortAction->setVisible(SqlLibraryModel::T_Album != MpdLibraryModel::self()->topLevel());
genreCombo->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred);
init(ReplacePlayQueue | AppendToPlayQueue, QList<QWidget*>() << menu << genreCombo);
connect(genreCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(doSearch()));
view->addAction(StdActions::self()->addToStoredPlaylistAction);
view->addAction(CustomActions::self());
#ifdef TagLib_FOUND
#ifdef ENABLE_DEVICES_SUPPORT
view->addAction(StdActions::self()->copyToDeviceAction);
#endif
view->addAction(StdActions::self()->organiseFilesAction);
view->addAction(StdActions::self()->editTagsAction);
#ifdef ENABLE_REPLAYGAIN_SUPPORT
view->addAction(StdActions::self()->replaygainAction);
#endif
view->addAction(StdActions::self()->setCoverAction);
#ifdef ENABLE_DEVICES_SUPPORT
view->addSeparator();
view->addAction(StdActions::self()->deleteSongsAction);
#endif
#endif// TagLib_FOUND
connect(view, SIGNAL(updateToPlayQueue(QModelIndex, bool)), this, SLOT(updateToPlayQueue(QModelIndex, bool)));
view->setOpenAfterSearch(SqlLibraryModel::T_Album != MpdLibraryModel::self()->topLevel());
view->setInfoText(tr("No music? Looks like your MPD is not configured correctly."));
for (QAction* act : viewAction->menu()->actions()) {
if (ItemView::Mode_Categorized == act->property(constValProp).toInt()) {
act->setVisible(SqlLibraryModel::T_Album == MpdLibraryModel::self()->topLevel());
break;
}
}
}
LibraryPage::~LibraryPage()
{
Configuration config(metaObject()->className());
MpdLibraryModel::self()->save(config);
config.beginGroup(SqlLibraryModel::groupingStr(MpdLibraryModel::self()->topLevel()));
view->save(config);
}
void LibraryPage::clear()
{
MpdLibraryModel::self()->clear();
view->goToTop();
}
QStringList LibraryPage::selectedFiles(bool allowPlaylists) const
{
QModelIndexList selected = view->selectedIndexes();
if (selected.isEmpty()) {
return QStringList();
}
return MpdLibraryModel::self()->filenames(selected, allowPlaylists);
}
QList<Song> LibraryPage::selectedSongs(bool allowPlaylists) const
{
QModelIndexList selected = view->selectedIndexes();
if (selected.isEmpty()) {
return QList<Song>();
}
return MpdLibraryModel::self()->songs(selected, allowPlaylists);
}
Song LibraryPage::coverRequest() const
{
QModelIndexList selected = view->selectedIndexes(false);// Dont need sorted selection here...
if (1 == selected.count()) {
QList<Song> songs = MpdLibraryModel::self()->songs(QModelIndexList() << selected.first(), false);
if (!songs.isEmpty()) {
Song s = songs.at(0);
if (SqlLibraryModel::T_Artist == static_cast<SqlLibraryModel::Item*>(selected.first().internalPointer())->getType()) {
if (s.useComposer()) {
s.setComposerImageRequest();
}
else {
s.setArtistImageRequest();
}
}
return s;
}
}
return Song();
}
#ifdef ENABLE_DEVICES_SUPPORT
void LibraryPage::addSelectionToDevice(const QString& udi)
{
QList<Song> songs = selectedSongs();
if (!songs.isEmpty()) {
emit addToDevice(QString(), udi, songs);
view->clearSelection();
}
}
void LibraryPage::deleteSongs()
{
QList<Song> songs = selectedSongs();
if (!songs.isEmpty()) {
if (MessageBox::Yes == MessageBox::warningYesNo(this, tr("Are you sure you wish to delete the selected songs?\n\nThis cannot be undone."), tr("Delete Songs"), StdGuiItem::del(), StdGuiItem::cancel())) {
emit deleteSongs(QString(), songs);
}
view->clearSelection();
}
}
#endif
void LibraryPage::showSongs(const QList<Song>& songs)
{
// Filter out non-mpd file songs...
QList<Song> sngs;
for (const Song& s : songs) {
if (!s.file.isEmpty() && !s.hasProtocolOrIsAbsolute()) {
sngs.append(s);
}
}
if (sngs.isEmpty()) {
return;
}
view->clearSearchText();
bool first = true;
for (const Song& s : sngs) {
QModelIndex idx = MpdLibraryModel::self()->findSongIndex(s);
if (idx.isValid()) {
if (ItemView::Mode_SimpleTree == view->viewMode() || ItemView::Mode_DetailedTree == view->viewMode() || first) {
view->showIndex(idx, first);
}
if (first) {
first = false;
}
if (ItemView::Mode_SimpleTree != view->viewMode() && ItemView::Mode_DetailedTree != view->viewMode()) {
return;
}
}
}
}
void LibraryPage::showArtist(const QString& artist)
{
view->clearSearchText();
QModelIndex idx = MpdLibraryModel::self()->findArtistIndex(artist);
if (idx.isValid()) {
view->showIndex(idx, true);
if (ItemView::Mode_SimpleTree == view->viewMode() || ItemView::Mode_DetailedTree == view->viewMode()) {
while (idx.isValid()) {
view->setExpanded(idx);
idx = idx.parent();
}
}
}
}
void LibraryPage::showAlbum(const QString& artist, const QString& album)
{
view->clearSearchText();
QModelIndex idx = MpdLibraryModel::self()->findAlbumIndex(artist, album);
if (idx.isValid()) {
view->showIndex(idx, true);
if (ItemView::Mode_SimpleTree == view->viewMode() || ItemView::Mode_DetailedTree == view->viewMode()) {
while (idx.isValid()) {
view->setExpanded(idx);
idx = idx.parent();
}
}
}
}
void LibraryPage::itemDoubleClicked(const QModelIndex&)
{
const QModelIndexList selected = view->selectedIndexes(false);// Dont need sorted selection here...
if (1 != selected.size()) {
return;//doubleclick should only have one selected item
}
SqlLibraryModel::Item* item = static_cast<SqlLibraryModel::Item*>(selected.at(0).internalPointer());
if (SqlLibraryModel::T_Track == item->getType()) {
addSelectionToPlaylist();
}
}
void LibraryPage::setView(int v)
{
SinglePageWidget::setView(v);
showArtistImagesAction->setVisible(SqlLibraryModel::T_Album != MpdLibraryModel::self()->topLevel() && ItemView::Mode_IconTop != view->viewMode());
}
void LibraryPage::modelReset()
{
genreCombo->update(MpdLibraryModel::self()->getGenres());
int count = MpdLibraryModel::self()->trackCount();
view->setMinSearchDebounce(count <= 12500 ? 1000u : count <= 18000 ? 1500u
: 2000u);
}
void LibraryPage::groupByChanged()
{
QAction* act = qobject_cast<QAction*>(sender());
if (!act) {
return;
}
int mode = act->property(constValProp).toInt();
Configuration config(metaObject()->className());
config.beginGroup(SqlLibraryModel::groupingStr(MpdLibraryModel::self()->topLevel()));
view->save(config);
MpdLibraryModel::self()->setTopLevel((SqlLibraryModel::Type)mode);
albumAlbumSortAction->setVisible(SqlLibraryModel::T_Album == MpdLibraryModel::self()->topLevel());
libraryAlbumSortAction->setVisible(SqlLibraryModel::T_Album != MpdLibraryModel::self()->topLevel());
showArtistImagesAction->setVisible(SqlLibraryModel::T_Album != MpdLibraryModel::self()->topLevel() && ItemView::Mode_IconTop != view->viewMode());
genreCombo->setVisible(SqlLibraryModel::T_Genre != MpdLibraryModel::self()->topLevel());
config.endGroup();
config.beginGroup(SqlLibraryModel::groupingStr(MpdLibraryModel::self()->topLevel()));
if (!config.hasEntry(ItemView::constViewModeKey)) {
view->setMode(SqlLibraryModel::T_Album == mode ? ItemView::Mode_IconTop : ItemView::Mode_DetailedTree);
}
view->load(config);
for (QAction* act : viewAction->menu()->actions()) {
int viewMode = act->property(constValProp).toInt();
if (viewMode == view->viewMode()) {
act->setChecked(true);
}
if (ItemView::Mode_Categorized == viewMode) {
act->setVisible(SqlLibraryModel::T_Album == MpdLibraryModel::self()->topLevel());
}
if (ItemView::Mode_IconTop == viewMode) {
act->setVisible(SqlLibraryModel::T_Genre != MpdLibraryModel::self()->topLevel());
}
}
view->setOpenAfterSearch(SqlLibraryModel::T_Album != MpdLibraryModel::self()->topLevel());
}
void LibraryPage::libraryAlbumSortChanged()
{
QAction* act = qobject_cast<QAction*>(sender());
if (act) {
MpdLibraryModel::self()->setLibraryAlbumSort((LibraryDb::AlbumSort)act->property(constValProp).toInt());
}
}
void LibraryPage::albumAlbumSortChanged()
{
QAction* act = qobject_cast<QAction*>(sender());
if (act) {
MpdLibraryModel::self()->setAlbumAlbumSort((LibraryDb::AlbumSort)act->property(constValProp).toInt());
}
}
void LibraryPage::showArtistImagesChanged(bool u)
{
MpdLibraryModel::self()->setUseArtistImages(u);
}
void LibraryPage::updateToPlayQueue(const QModelIndex& idx, bool replace)
{
QStringList files = MpdLibraryModel::self()->filenames(QModelIndexList() << idx, true);
if (!files.isEmpty()) {
emit add(files, replace ? MPDConnection::ReplaceAndplay : MPDConnection::Append, 0, false);
}
}
void LibraryPage::addRandomAlbum()
{
if (!isVisible()) {
return;
}
QStringList genres;
QStringList artists;
QList<SqlLibraryModel::AlbumItem*> albums;
QModelIndexList selected = view->selectedIndexes(false);// Dont need sorted selection here...
for (const QModelIndex& idx : selected) {
SqlLibraryModel::Item* item = static_cast<SqlLibraryModel::Item*>(idx.internalPointer());
switch (item->getType()) {
case SqlLibraryModel::T_Genre:
genres.append(item->getId());
break;
case SqlLibraryModel::T_Artist:
artists.append(item->getId());
break;
case SqlLibraryModel::T_Album:
// Can only have albums selected if set to group by albums
// ...controlActions ensures this!
albums.append(static_cast<SqlLibraryModel::AlbumItem*>(item));
break;
default:
break;
}
}
LibraryDb::Album album;
if (!albums.isEmpty()) {
// We have albums selected, so choose a random one of these...
SqlLibraryModel::AlbumItem* al = albums.at(QRandomGenerator::global()->bounded(albums.size()));
album.artist = al->getArtistId();
album.id = al->getId();
}
else {
// If all items selected, then just choose random of all albums
switch (MpdLibraryModel::self()->topLevel()) {
case SqlLibraryModel::T_Genre:
if (genres.size() == MpdLibraryModel::self()->rowCount(QModelIndex())) {
genres = QStringList();
}
break;
case SqlLibraryModel::T_Artist:
if (artists.size() == MpdLibraryModel::self()->rowCount(QModelIndex())) {
artists = QStringList();
}
break;
case SqlLibraryModel::T_Album:
genres = artists = QStringList();
break;
default:
break;
}
album = MpdLibraryModel::self()->getRandomAlbum(genres, artists);
}
if (album.artist.isEmpty() || album.id.isEmpty()) {
return;
}
QList<Song> songs = MpdLibraryModel::self()->getAlbumTracks(album.artist, album.id);
if (!songs.isEmpty()) {
QStringList files;
for (const Song& s : songs) {
files.append(s.file);
}
emit add(files, /*replace ? MPDConnection::ReplaceAndplay : */ MPDConnection::Append, 0, false);
}
}
void LibraryPage::doSearch()
{
MpdLibraryModel::self()->search(view->searchText(),
genreCombo->isHidden() || genreCombo->currentIndex() <= 0 ? QString() : genreCombo->currentText());
}
void LibraryPage::controlActions()
{
QModelIndexList selected = view->selectedIndexes(false);// Dont need sorted selection here...
bool enable = selected.count() > 0;
CustomActions::self()->setEnabled(enable);
StdActions::self()->enableAddToPlayQueue(enable);
StdActions::self()->addToStoredPlaylistAction->setEnabled(enable);
#ifdef TagLib_FOUND
StdActions::self()->organiseFilesAction->setEnabled(enable && MPDConnection::self()->getDetails().dirReadable);
StdActions::self()->editTagsAction->setEnabled(StdActions::self()->organiseFilesAction->isEnabled());
#ifdef ENABLE_REPLAYGAIN_SUPPORT
StdActions::self()->replaygainAction->setEnabled(StdActions::self()->organiseFilesAction->isEnabled());
#endif
#ifdef ENABLE_DEVICES_SUPPORT
StdActions::self()->deleteSongsAction->setEnabled(StdActions::self()->organiseFilesAction->isEnabled());
StdActions::self()->copyToDeviceAction->setEnabled(StdActions::self()->organiseFilesAction->isEnabled());
#endif
#endif// TagLib_FOUND
if (1 == selected.count()) {
SqlLibraryModel::Item* item = static_cast<SqlLibraryModel::Item*>(selected.at(0).internalPointer());
SqlLibraryModel::Type type = item->getType();
StdActions::self()->setCoverAction->setEnabled((SqlLibraryModel::T_Artist == type /* && !static_cast<MusicLibraryItemArtist *>(item)->isComposer()*/) || SqlLibraryModel::T_Album == type);
}
else {
StdActions::self()->setCoverAction->setEnabled(false);
}
bool allowRandomAlbum = isVisible() && !selected.isEmpty();
if (allowRandomAlbum) {
bool groupingAlbums = SqlLibraryModel::T_Album == MpdLibraryModel::self()->topLevel();
for (const QModelIndex& idx : selected) {
if (SqlLibraryModel::T_Track == static_cast<SqlLibraryModel::Item*>(idx.internalPointer())->getType() || (!groupingAlbums && SqlLibraryModel::T_Album == static_cast<SqlLibraryModel::Item*>(idx.internalPointer())->getType())) {
allowRandomAlbum = false;
break;
}
}
}
StdActions::self()->addRandomAlbumToPlayQueueAction->setVisible(allowRandomAlbum);
}
#include "moc_librarypage.cpp"
|