-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmanager.cpp
236 lines (191 loc) · 6.13 KB
/
manager.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
#include "manager.h"
#include <QStringList>
Master Manager::master;
Xml_Parser Manager::parser;
Setting Manager::setting;
Manager::Manager(QObject *parent) :
QObject(parent)
{
}
Manager::~Manager()
{
}
bool Manager::fileExists(QString path)
{
QFileInfo checkFile(path);
// check if file exists and if yes: Is it really a file and no directory?
if (checkFile.exists() && checkFile.isFile())
{
return true;
}
else
{
return false;
}
}
void Manager::split(const std::wstring& s, char c, std::vector<std::wstring>& v)
{
std::wstring::size_type i = 0;
std::wstring::size_type j = s.find(c);
while (j != std::wstring::npos) {
v.push_back(s.substr(i, j-i));
i = ++j;
j = s.find(c, j);
if (j == std::wstring::npos)
v.push_back(s.substr(i, s.length()));
}
}
void Manager::GetFileListing(std::wstring directory, std::wstring fileFilter, Xml_Parser &parser, bool recursively)
{
if (recursively)
GetFileListing(directory, fileFilter, parser, false);
directory += L"/";
WIN32_FIND_DATA FindFileData;
HANDLE hFind = INVALID_HANDLE_VALUE;
std::wstring filter = directory + (recursively ? L"*" : fileFilter);
const wchar_t * wstr;
wstr = filter.c_str();
LPCWSTR filter_lpcwstr = wstr;
hFind = FindFirstFile(filter_lpcwstr, &FindFileData);
if (hFind == INVALID_HANDLE_VALUE)
{
return;
}
else
{
if (!recursively)
{
std::wstring wstr(FindFileData.cFileName);
std::wstring str( wstr.begin(), wstr.end() );
QString title, artist, album, filename;
int duration;
uint year;
filename = QString::fromStdWString(directory + str);
TagLib::MPEG::File f( reinterpret_cast<const wchar_t*>(filename.constData()) );
TagLib::ID3v2::Tag *tag = f.ID3v2Tag();
TagLib::ID3v2::FrameList albart = tag->frameList("TPE2");
if (!albart.isEmpty())
{
artist = TStringToQString(albart.front()->toString());
}
else
artist = TStringToQString(f.tag()->artist());
artist = artist.simplified();
title = TStringToQString(f.tag()->title());
title = title.simplified();
album = TStringToQString(f.tag()->album());
album = album.simplified();
year = f.tag()->year();
duration = f.audioProperties()->lengthInSeconds();
CheckSongInfo(title, artist, album, filename);
parser.AddToDom(title, artist, album, filename, year);
}
while (FindNextFile(hFind, &FindFileData) != 0)
{
if (!recursively)
{
std::wstring wstr(FindFileData.cFileName);
std::wstring str( wstr.begin(), wstr.end() );
QString title, artist, album, filename;
uint year;
filename = QString::fromStdWString(directory + str);
TagLib::MPEG::File f( reinterpret_cast<const wchar_t*>(filename.constData()) );
title = TStringToQString(f.tag()->title());
title = title.simplified();
TagLib::ID3v2::FrameList albart = f.ID3v2Tag()->frameList("TPE2");
if (!albart.isEmpty())
{
artist = TStringToQString(albart.front()->toString());
}
else
{
artist = TStringToQString(f.tag()->artist());
}
artist = artist.simplified();
album = TStringToQString(f.tag()->album());
album = album.simplified();
year = f.tag()->year();
CheckSongInfo(title, artist, album, filename);
parser.AddToDom(title, artist, album, filename, year);
}
else
{
if ((FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)>0 && FindFileData.cFileName[0]!='.')
{
std::wstring wstr(FindFileData.cFileName);
std::wstring str( wstr.begin(), wstr.end() );
GetFileListing(directory + str, fileFilter, parser);
}
}
}
DWORD dwError = GetLastError();
FindClose(hFind);
if (dwError != ERROR_NO_MORE_FILES)
{
std::cout << "FindNextFile error. Error is "<< dwError << std::endl;
}
}
}
void Manager::GetFiles(std::wstring directory, std::wstring fileFilter, Xml_Parser &parser, bool recursively)
{
std::vector<std::wstring> ex;
split(fileFilter, '|', ex);
for (unsigned int i = 0; i < ex.size(); i++)
{
GetFileListing(directory, ex[i], parser, recursively);
}
}
void Manager::CreationProcessSuccess(bool isSuccessful)
{
if (isSuccessful)
{
if (!QDir("Data").exists())
QDir().mkdir("Data");
parser.SaveChoosenFolder("Data\\Master.xml");
}
}
void Manager::CreateMaster(QStringList str_list)
{
if (master.GetCount() != 0)
master.ClearList();
parser.Init();
for (int i = 0; i < str_list.size(); i++)
{
GetFiles(str_list.at(i).toStdWString(), L"*.mp3|*.wav", parser, true);
}
master.SetList(parser.GetAllSong());
master.SetAlbumList(parser.GetAllAlbums());
}
void Manager::CheckSongInfo(QString &title, QString &artist, QString &album, const QString &path)
{
if (title == "")
{
QStringList name = path.split("/");
title = name.value(name.length() - 1);
name = title.split(".");
title = name.value(0);
}
title = title.simplified();
if (album == "")
{
album = "Unknown";
}
album = album.simplified();
if (artist == "")
{
artist = "Unknown";
}
artist = artist.simplified();
}
bool Manager::LoadSongToMaster()
{
QString XmlPath = "Data\\Master.xml";
if (fileExists(XmlPath))
{
parser.LoadData(XmlPath);
parser.GetSongsInPlaylist(master);
parser.GetAlbumsInPlaylist(master);
return true;
}
return false;
}