-
Notifications
You must be signed in to change notification settings - Fork 0
/
filemanager.h
230 lines (179 loc) · 8.06 KB
/
filemanager.h
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
#ifndef FILEMANAGER_H
#define FILEMANAGER_H
#include <QCryptographicHash>
#include <QObject>
#include <QImage>
#include <QThread>
#include <QFile>
#include <QList>
#include <QUrl>
#include <QString>
#include <QDir>
#include <QDebug>
#include <QFileInfo>
#include <fstream>
#include <fileref.h>
#include <tag.h>
#include <id3v2tag.h>
#include <mpegfile.h>
#include <mp4file.h>
#include <mp4tag.h>
#include <mp4coverart.h>
#include <attachedpictureframe.h>
#include "pix.h"
extern QString path;
class FileManager : public QThread
{
Q_OBJECT
public:
QCryptographicHash *hash3 = new QCryptographicHash(QCryptographicHash::Sha3_224);
QList<QUrl> urls;
QVariantList newLib;
Pix p;
FileManager(QVariantList oldLib,QList<QUrl> uris){
newLib = oldLib;
urls = uris;
}
protected:
void run(){
int total = urls.length();
int completed = 0;
foreach (QUrl uri, urls)
{
QStringList urlParts = uri.path().split( "." );
QString format = urlParts.last();
QString filename = uri.fileName();
QString artist, album, title, genre, songId;
int track, year, duration;
bool artWork;
if(format == "mp3")
{
TagLib::MPEG::File MP3FILE(uri.path().toStdString().c_str());
TagLib::ID3v2::Tag *MP3 = MP3FILE.ID3v2Tag(true);
if(MP3->artist().isEmpty()) artist = "Unknown";
else artist = QString::fromStdWString(MP3->artist().toWString()).remove(QRegExp("[^a-zA-Z\\d\\s]"));
if(MP3->album().isEmpty()) album = "Unknown";
else album = QString::fromStdWString(MP3->album().toWString()).remove(QRegExp("[^a-zA-Z\\d\\s]"));
if(MP3->title().isEmpty()) title = filename;
else title = QString::fromStdWString(MP3->title().toWString()).remove(QRegExp("[^a-zA-Z\\d\\s]"));
track = MP3->track();
year = MP3->year();
duration = MP3FILE.audioProperties()->lengthInSeconds();
genre = QString::fromStdWString(MP3->genre().toWString());
TagLib::ID3v2::FrameList frameList = MP3->frameList("APIC");
if(!frameList.isEmpty()) {
artWork = true;
//Check if artist folder exist
if(!QDir(path+"/Cuarzo Player/Artwork/"+artist).exists()){
QDir().mkdir(path+"/Cuarzo Player/Artwork/"+artist);
}
QFileInfo check_file(path+"/Cuarzo Player/Artwork/"+artist+"/"+album+".jpg");
if (!check_file.exists()) {
TagLib::ID3v2::AttachedPictureFrame *coverImg = static_cast<TagLib::ID3v2::AttachedPictureFrame *>(frameList.front());
QImage coverQImg;
coverQImg.loadFromData((const uchar *) coverImg->picture().data(), coverImg->picture().size());
coverQImg = coverQImg.scaled(256,256,Qt::KeepAspectRatio,Qt::SmoothTransformation);
coverQImg.save(path+"/Cuarzo Player/Artwork/"+artist+"/"+album+".jpg","jpg",100);
}
}
else
{
artWork = false;
}
}
else if (format == "m4a")
{
TagLib::MP4::File M4AFILE(uri.path().toStdString().c_str());
TagLib::MP4::Tag *M4A = M4AFILE.tag();
if(M4A->artist().isEmpty()) artist = "Unknown";
else artist = QString::fromStdWString(M4A->artist().toWString()).remove(QRegExp("[^a-zA-Z\\d\\s]"));
if(M4A->album().isEmpty()) album = "Unknown";
else album = QString::fromStdWString(M4A->album().toWString()).remove(QRegExp("[^a-zA-Z\\d\\s]"));
if(M4A->title().isEmpty()) title = filename;
else title = QString::fromStdWString(M4A->title().toWString()).remove(QRegExp("[^a-zA-Z\\d\\s]"));
track = M4A->track();
year = M4A->year();
duration = M4AFILE.audioProperties()->lengthInSeconds();
genre = QString::fromStdWString(M4A->genre().toWString());
TagLib::MP4::ItemListMap itemsListMap = M4A->itemListMap();
TagLib::MP4::Item coverItem = itemsListMap["covr"];
TagLib::MP4::CoverArtList coverArtList = coverItem.toCoverArtList();
if(!coverArtList.isEmpty()) {
artWork = true;
//Check if artist folder exist
if(!QDir(path+"/Cuarzo Player/Artwork/"+artist).exists()){
QDir().mkdir(path+"/Cuarzo Player/Artwork/"+artist);
}
QFileInfo check_file(path+"/Cuarzo Player/Artwork/"+artist+"/"+album+".jpg");
if (!check_file.exists()) {
QImage coverQImg;
TagLib::MP4::CoverArt coverArt = coverArtList.front();
coverQImg.loadFromData((const uchar *)coverArt.data().data(),coverArt.data().size());
coverQImg = coverQImg.scaled(256,256,Qt::KeepAspectRatio,Qt::SmoothTransformation);
coverQImg.save(path+"/Cuarzo Player/Artwork/"+artist+"/"+album+".jpg","jpg",100);
}
}
else
{
artWork = false;
}
}
//Check if artist folder exist
if(!QDir(path+"/Cuarzo Player/Music/"+artist).exists()){
QDir().mkdir(path+"/Cuarzo Player/Music/"+artist);
}
//Check if album folder exist
if(!QDir(path+"/Cuarzo Player/Music/"+artist+"/"+album).exists()){
QDir().mkdir(path+"/Cuarzo Player/Music/"+artist+"/"+album);
}
//Copy file
QFile originFile( uri.path() );
originFile.open( QIODevice::ReadOnly );
QByteArray songData = originFile.readAll();
hash3->addData(songData);
songId = QString::fromStdString(hash3->result().toStdString());
originFile.close();
QString songFileName = QString::number(track) + " " + title + "." + format;
QString destPath = path+"/Cuarzo Player/Music/"+artist+"/"+album+"/";
QFileInfo check_lib(destPath + songFileName);
bool alreadyExistName = check_lib.exists();
int sufix = 1;
while(alreadyExistName)
{
songFileName = QString::number(track) + " " + title + " " + QString::number(sufix) + "." + format;
QFileInfo check_lib(destPath + songFileName);
alreadyExistName = check_lib.exists();
}
QFile destFile(destPath + songFileName);
destFile.open(QIODevice::WriteOnly);
destFile.write(songData);
destFile.close();
QVariantMap song;
song["id"] = songId;
song["track"] = track;
song["year"] = year;
song["duration"] = duration;
song["artist"] = artist;
song["album"] = album;
song["title"] = title;
song["genre"] = genre;
song["format"] = format;
song["artWork"] = artWork;
song["cloud"] = false;
song["local"] = true;
song["artWorkId"] = "";
song["musicId"] = "";
song["fileName"] = songFileName;
newLib.append(song);
completed++;
float percent = (float)100/(float)total*(float)completed;
songAddProgress((float)percent);
}
songAddEnd(newLib);
deleteLater();
}
signals:
void songAddProgress(int);
void songAddEnd(QVariantList);
};
#endif // FILEMANAGER_H