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
|
/***************************************************************************
* Copyright (C) 2006-2015 by Ilya Kotov *
* forkotov02@hotmail.ru *
* *
* 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; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************/
#include <qmmp/metadatamanager.h>
#include <QRegExp>
#include "fileloader_p.h"
#include "qmmpuisettings.h"
#include "playlistitem.h"
#include "playlisttrack.h"
FileLoader::FileLoader(QObject *parent) : QThread(parent)
{
m_settings = QmmpUiSettings::instance();
m_finished = false;
}
FileLoader::~FileLoader()
{}
void FileLoader::addFile(const QString &path, PlayListItem *before)
{
bool use_meta = m_settings->useMetadata();
QList <FileInfo *> playList = MetaDataManager::instance()->createPlayList(path, use_meta);
if(before)
{
foreach(FileInfo *info, playList)
emit newTrackToInsert(before, new PlayListTrack(info));
}
else
{
foreach(FileInfo *info, playList)
emit newTrackToAdd(new PlayListTrack(info));
}
qDeleteAll(playList);
}
void FileLoader::addDirectory(const QString& s, PlayListItem *before)
{
QDir dir(s);
dir.setFilter(QDir::Files | QDir::Hidden | QDir::NoSymLinks);
dir.setSorting(QDir::Name);
QFileInfoList l = dir.entryInfoList(m_filters);
foreach(QFileInfo info, l)
{
if(checkRestrictFilters(info) && checkExcludeFilters(info))
addFile(info.absoluteFilePath (), before);
if (m_finished) return;
}
dir.setFilter(QDir::Dirs | QDir::NoDotAndDotDot);
dir.setSorting(QDir::Name);
l.clear();
l = dir.entryInfoList();
if (l.size() > 0)
for (int i = 0; i < l.size(); ++i)
{
QFileInfo fileInfo = l.at(i);
addDirectory(fileInfo.absoluteFilePath (), before);
if (m_finished) return;
}
}
void FileLoader::run()
{
m_finished = false;
while(!m_tasks.isEmpty() && !m_finished)
{
LoaderTask i = m_tasks.dequeue();
PlayListItem *before = i.before;
QString path = i.path;
QFileInfo info(path);
if(info.isDir())
{
addDirectory(path, before);
continue;
}
else if(info.isFile() || path.contains("://"))
{
addFile(path, before);
continue;
}
}
}
void FileLoader::add(const QString &path)
{
add(QStringList() << path);
}
void FileLoader::add(const QStringList &paths)
{
foreach (QString path, paths)
{
LoaderTask task;
task.before = 0;
task.path = path;
m_tasks.append(task);
}
MetaDataManager::instance()->prepareForAnotherThread();
m_filters = MetaDataManager::instance()->nameFilters();
start(QThread::IdlePriority);
}
void FileLoader::insert(PlayListItem *before, const QString &path)
{
insert(before, QStringList() << path);
}
void FileLoader::insert(PlayListItem *before, const QStringList &paths)
{
foreach (QString path, paths)
{
LoaderTask task;
task.before = before;
task.path = path;
m_tasks.append(task);
}
MetaDataManager::instance()->prepareForAnotherThread();
m_filters = MetaDataManager::instance()->nameFilters();
start(QThread::IdlePriority);
}
void FileLoader::finish()
{
m_finished = true;
wait();
m_tasks.clear();
}
bool FileLoader::checkRestrictFilters(const QFileInfo &info)
{
if(m_settings->restrictFilters().isEmpty())
return true;
foreach(QString filter, m_settings->restrictFilters())
{
QRegExp regexp (filter, Qt::CaseInsensitive, QRegExp::Wildcard);
if(regexp.exactMatch(info.absoluteFilePath()))
return true;
}
return false;
}
bool FileLoader::checkExcludeFilters(const QFileInfo &info)
{
if(m_settings->excludeFilters().isEmpty())
return true;
foreach(QString filter, m_settings->excludeFilters())
{
QRegExp regexp (filter, Qt::CaseInsensitive, QRegExp::Wildcard);
if(regexp.exactMatch(info.absoluteFilePath()))
return false;
}
return true;
}
|