1 /* GenreFetcher.cpp */
2
3 /* Copyright (C) 2011-2020 Michael Lugmair (Lucio Carreras)
4 *
5 * This file is part of sayonara player
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "GenreFetcher.h"
22
23 #include "Components/Library/LocalLibrary.h"
24 #include "Components/Tagging/ChangeNotifier.h"
25 #include "Components/Tagging/UserTaggingOperations.h"
26
27 #include "Database/Connector.h"
28 #include "Database/LibraryDatabase.h"
29
30 #include "Utils/Library/LibraryInfo.h"
31 #include "Utils/MetaData/MetaDataList.h"
32 #include "Utils/Set.h"
33
34 #include <QTimer>
35
36 struct GenreFetcher::Private
37 {
38 LocalLibrary* localLibrary=nullptr;
39 Util::Set<Genre> genres;
40 Util::Set<Genre> additionalGenres; // empty genres that are inserted
41 Tagging::UserOperations* uto=nullptr;
42 };
43
GenreFetcher(QObject * parent)44 GenreFetcher::GenreFetcher(QObject* parent) :
45 QObject(parent)
46 {
47 m = Pimpl::make<Private>();
48
49 Tagging::ChangeNotifier* mcn = Tagging::ChangeNotifier::instance();
50
51 connect(mcn, &Tagging::ChangeNotifier::sigMetadataChanged, this, &GenreFetcher::reloadGenres);
52 connect(mcn, &Tagging::ChangeNotifier::sigMetadataDeleted, this, &GenreFetcher::reloadGenres);
53 }
54
initTagging()55 Tagging::UserOperations* GenreFetcher::initTagging()
56 {
57 if(!m->uto)
58 {
59 m->uto = new Tagging::UserOperations(-1, this);
60 connect(m->uto, &Tagging::UserOperations::sigProgress, this, &GenreFetcher::sigProgress);
61 connect(m->uto, &Tagging::UserOperations::sigFinished, this, &GenreFetcher::sigFinished);
62 }
63
64 return m->uto;
65 }
66
67 GenreFetcher::~GenreFetcher() = default;
68
reloadGenres()69 void GenreFetcher::reloadGenres()
70 {
71 if(!m->localLibrary){
72 return;
73 }
74
75 LibraryId libraryId = m->localLibrary->info().id();
76
77 DB::LibraryDatabase* lib_db = DB::Connector::instance()->libraryDatabase(libraryId, 0);
78 m->genres = lib_db->getAllGenres();
79
80 emit sigGenresFetched();
81 }
82
genres() const83 Util::Set<Genre> GenreFetcher::genres() const
84 {
85 Util::Set<Genre> genres(m->genres);
86 for(const Genre& genre : m->additionalGenres)
87 {
88 genres.insert(genre);
89 }
90
91 return genres;
92 }
93
createGenre(const Genre & genre)94 void GenreFetcher::createGenre(const Genre& genre)
95 {
96 m->additionalGenres << genre;
97 emit sigGenresFetched();
98 }
99
applyGenreToMetadata(const MetaDataList & v_md,const Genre & genre)100 void GenreFetcher::applyGenreToMetadata(const MetaDataList& v_md, const Genre& genre)
101 {
102 Tagging::UserOperations* uto = initTagging();
103 uto->applyGenreToMetadata(v_md, genre);
104 }
105
deleteGenre(const Genre & genre)106 void GenreFetcher::deleteGenre(const Genre& genre)
107 {
108 Tagging::UserOperations* uto = initTagging();
109 uto->deleteGenre(genre);
110 }
111
deleteGenres(const Util::Set<Genre> & genres)112 void GenreFetcher::deleteGenres(const Util::Set<Genre>& genres)
113 {
114 for(const Genre& genre : genres)
115 {
116 deleteGenre(genre);
117 }
118 }
119
renameGenre(const Genre & oldGenre,const Genre & newGenre)120 void GenreFetcher::renameGenre(const Genre& oldGenre, const Genre& newGenre)
121 {
122 Tagging::UserOperations* uto = initTagging();
123 uto->renameGenre(oldGenre, newGenre);
124 }
125
set_local_library(LocalLibrary * local_library)126 void GenreFetcher::set_local_library(LocalLibrary* local_library)
127 {
128 m->localLibrary = local_library;
129 connect(m->localLibrary, &LocalLibrary::sigReloadingLibraryFinished,
130 this, &GenreFetcher::reloadGenres);
131
132 QTimer::singleShot(200, this, SLOT(reloadGenres()));
133 }
134