-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvvave.cpp
244 lines (194 loc) · 6.13 KB
/
vvave.cpp
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
#include "vvave.h"
#include "db/collectionDB.h"
#include "services/local/taginfo.h"
#include <MauiKit/fileloader.h>
#include <MauiKit/fm.h>
#include <QTimer>
static FMH::MODEL trackInfo(const QUrl &url)
{
TagInfo info(url.toLocalFile());
if (info.isNull())
return FMH::MODEL();
const auto track = info.getTrack();
const auto genre = info.getGenre();
const auto album = BAE::fixString(info.getAlbum());
const auto title = BAE::fixString(info.getTitle()); /* to fix*/
const auto artist = BAE::fixString(info.getArtist());
const auto sourceUrl = FMH::parentDir(url).toString();
const auto duration = info.getDuration();
const auto year = info.getYear();
FMH::MODEL map = {{FMH::MODEL_KEY::URL, url.toString()},
{FMH::MODEL_KEY::TRACK, QString::number(track)},
{FMH::MODEL_KEY::TITLE, title},
{FMH::MODEL_KEY::ARTIST, artist},
{FMH::MODEL_KEY::ALBUM, album},
{FMH::MODEL_KEY::DURATION, QString::number(duration)},
{FMH::MODEL_KEY::GENRE, genre},
{FMH::MODEL_KEY::SOURCE, sourceUrl},
{FMH::MODEL_KEY::FAV, "0"},
{FMH::MODEL_KEY::RELEASEDATE, QString::number(year)}};
BAE::artworkCache(map, FMH::MODEL_KEY::ALBUM);
return map;
}
/*
* Sets upthe app default config paths
* BrainDeamon to get collection information
* YoutubeFetcher ?
*
* */
vvave::vvave(QObject *parent)
: QObject(parent)
, db(CollectionDB::getInstance())
{
qRegisterMetaType<QList<QUrl> *>("QList<QUrl>&");
QDir dirPath(BAE::CachePath.toLocalFile());
if (!dirPath.exists())
dirPath.mkpath(".");
auto tracksTimer = new QTimer(this);
tracksTimer->setSingleShot(true);
tracksTimer->setInterval(1000);
auto albumsTimer = new QTimer(this);
albumsTimer->setSingleShot(true);
albumsTimer->setInterval(1000);
auto artistTimer = new QTimer(this);
artistTimer->setSingleShot(true);
artistTimer->setInterval(1000);
connect(db, &CollectionDB::trackInserted, [this, tracksTimer](QVariantMap) {
m_newTracks++;
tracksTimer->start();
});
connect(db, &CollectionDB::albumInserted, [this, albumsTimer](QVariantMap) {
m_newAlbums++;
albumsTimer->start();
});
connect(db, &CollectionDB::artistInserted, [this, artistTimer](QVariantMap) {
m_newArtist++;
artistTimer->start();
});
connect(db, &CollectionDB::sourceInserted, [this](QVariantMap) {
m_newSources++;
});
connect(tracksTimer, &QTimer::timeout, [this]()
{
if (m_newTracks > 0) {
emit tracksAdded(m_newTracks);
m_newTracks = 0;
}
});
connect(albumsTimer, &QTimer::timeout, [this]()
{
if (m_newAlbums > 0) {
emit albumsAdded(m_newAlbums);
m_newAlbums = 0;
}
});
connect(artistTimer, &QTimer::timeout, [this]()
{
if (m_newArtist > 0) {
emit artistsAdded(m_newArtist);
m_newArtist = 0;
}
});
}
//// PUBLIC SLOTS
//vvave *vvave::qmlAttachedProperties(QObject *object)
//{
// Q_UNUSED(object)
// return vvave::instance();
//}
void vvave::setFetchArtwork(bool fetchArtwork)
{
if (m_fetchArtwork == fetchArtwork)
return;
m_fetchArtwork = fetchArtwork;
emit fetchArtworkChanged(m_fetchArtwork);
}
bool vvave::autoScan() const
{
return m_autoScan;
}
bool vvave::fetchArtwork() const
{
return m_fetchArtwork;
}
void vvave::addSources(const QList<QUrl> &paths)
{
auto urls = QUrl::fromStringList(sources());
QList<QUrl> newUrls;
for (const auto &path : paths) {
if (!urls.contains(path)) {
newUrls << path;
emit sourceAdded(path);
}
}
if (newUrls.isEmpty())
return;
urls << newUrls;
FMStatic::saveSettings("SETTINGS", QVariant::fromValue(QUrl::toStringList(urls)), "SOURCES");
scanDir(urls);
emit sourcesChanged();
}
bool vvave::removeSource(const QString &source)
{
auto urls = this->sources();
if (!urls.contains(source))
return false;
urls.removeOne(source);
FMStatic::saveSettings("SETTINGS", QVariant::fromValue(urls), "SOURCES");
emit sourcesChanged();
if (this->db->removeSource(source)) {
emit this->sourceRemoved(source);
return true;
}
return false;
}
void vvave::scanDir(const QList<QUrl> &paths)
{
auto fileLoader = new FMH::FileLoader();
fileLoader->informer = &trackInfo;
// fileLoader->setBatchCount(50);
connect(fileLoader, &FMH::FileLoader::itemReady, db, &CollectionDB::addTrack);
connect(fileLoader, &FMH::FileLoader::finished, fileLoader, &FMH::FileLoader::deleteLater);
fileLoader->requestPath(paths, true, QStringList() << FMH::FILTER_LIST[FMH::FILTER_TYPE::AUDIO] << "*.m4a");
}
QStringList vvave::sources()
{
return FMStatic::loadSettings("SETTINGS", "SOURCES", QVariant::fromValue(BAE::defaultSources)).toStringList();
}
QVariantList vvave::sourcesModel()
{
QVariantList res;
for (const auto &url : sources())
res << FMH::getDirInfo(url);
return res;
}
void vvave::setAutoScan(bool autoScan)
{
if (m_autoScan == autoScan)
return;
m_autoScan = autoScan;
emit autoScanChanged(m_autoScan);
if (m_autoScan) {
scanDir(QUrl::fromStringList(sources()));
}
}
void vvave::openUrls(const QStringList &urls)
{
if (urls.isEmpty())
return;
QVariantList data;
for (const auto &url : urls) {
auto _url = QUrl::fromUserInput(url);
if (db->check_existance(BAE::TABLEMAP[BAE::TABLE::TRACKS], FMH::MODEL_NAME[FMH::MODEL_KEY::URL], _url.toString())) {
data << FMH::toMap(this->db->getDBData(QStringList() << _url.toString()).first());
} else {
data << FMH::toMap(trackInfo(_url));
}
}
emit this->openFiles(data);
}
QList<QUrl> vvave::folders()
{
const auto sources = CollectionDB::getInstance()->getDBData("select * from sources");
return QUrl::fromStringList(FMH::modelToList(sources, FMH::MODEL_KEY::URL));
}