TTKMusicPlayer  3.7.0.0
TTKMusicPlayer imitates Kugou UI, the music player uses of qmmp core library based on Qt for windows and linux
musicfileutils.cpp
Go to the documentation of this file.
1 #include "musicfileutils.h"
2 #include "musicwidgetheaders.h"
3 #include "musicsettingmanager.h"
4 
5 #include <QDirIterator>
6 
7 QStringList TTK::File::fileListByPath(const QString &dpath, const QStringList &filter, bool recursively)
8 {
9  QDir dir(dpath);
10  if(TTK::Core::isBreakPointEnabled() || !dir.exists())
11  {
12  return {};
13  }
14 
15  const QString &spr = dpath.endsWith(TTK_SEPARATOR) ? QString() : TTK_SEPARATOR;
16 
17  QStringList fileList;
18  for(const QString &path : dir.entryList(filter, QDir::Files | QDir::Hidden))
19  {
20  fileList.append(dpath + spr + path);
21  }
22 
23  if(recursively)
24  {
25  const QFileInfoList &folderList = dir.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot);
26  for(const QFileInfo &fin : qAsConst(folderList))
27  {
28  fileList.append(TTK::File::fileListByPath(fin.absoluteFilePath(), filter, recursively));
29  }
30  }
31  return fileList;
32 }
33 
34 QFileInfoList TTK::File::fileInfoListByPath(const QString &dpath, const QStringList &filter, bool recursively)
35 {
36  QDir dir(dpath);
37  if(TTK::Core::isBreakPointEnabled() || !dir.exists())
38  {
39  return {};
40  }
41 
42  QFileInfoList fileList = dir.entryInfoList(filter, QDir::Files | QDir::Hidden);
43  if(recursively)
44  {
45  const QFileInfoList &folderList = dir.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot);
46  for(const QFileInfo &fin : qAsConst(folderList))
47  {
48  fileList.append(TTK::File::fileInfoListByPath(fin.absoluteFilePath(), filter, recursively));
49  }
50  }
51  return fileList;
52 }
53 
54 bool TTK::File::copyPath(const QString &srcPath, const QString &dstPath, bool overwrite)
55 {
57  {
58  return false;
59  }
60 
61  if(QFileInfo(srcPath).isFile())
62  {
63  if(QFileInfo(dstPath).isFile())
64  {
65  if(overwrite && QFile::exists(dstPath))
66  {
67  QFile::remove(dstPath);
68  }
69  return QFile::copy(srcPath, dstPath);
70  }
71  return false;
72  }
73 
74  QDir dstDir(dstPath);
75  if(!dstDir.exists() && !dstDir.mkpath(dstDir.absolutePath()))
76  {
77  return false;
78  }
79 
80  for(const QFileInfo &fileInfo : QDir(srcPath).entryInfoList())
81  {
82  const QString &fileName = fileInfo.fileName();
83  if(fileName == "." || fileName == "..")
84  {
85  continue;
86  }
87 
88  if(fileInfo.isDir())
89  {
90  if(!TTK::File::copyPath(fileInfo.filePath(), dstDir.filePath(fileName), overwrite))
91  {
92  return false;
93  }
94  }
95  else
96  {
97  if(overwrite && dstDir.exists(fileName))
98  {
99  dstDir.remove(fileName);
100  }
101 
102  if(!QFile::copy(fileInfo.filePath(), dstDir.filePath(fileName)))
103  {
104  return false;
105  }
106  }
107  }
108  return true;
109 }
110 
111 bool TTK::File::removeRecursively(const QString &dir, bool self)
112 {
113  QDir dr(dir);
114  if(TTK::Core::isBreakPointEnabled() || !dr.exists())
115  {
116  return true;
117  }
118 
119  bool success = true;
120  const QString &dirPath = dr.path();
121  // not empty -- we must empty it first
122  QDirIterator di(dirPath, QDir::AllEntries | QDir::Hidden | QDir::System | QDir::NoDotAndDotDot);
123  while(di.hasNext())
124  {
125  di.next();
126  const QFileInfo &fin = di.fileInfo();
127  const QString &filePath = di.filePath();
128 
129  bool ok = false;
130  if(fin.isDir() && !fin.isSymLink())
131  {
132  ok = TTK::File::removeRecursively(filePath, self); // recursive
133  }
134  else
135  {
136  ok = QFile::remove(filePath);
137  if(!ok)
138  {
139  // Read-only files prevent directory deletion on Windows, retry with Write permission.
140  const QFile::Permissions permissions = QFile::permissions(filePath);
141  if(!(permissions & QFile::WriteUser))
142  {
143  ok = QFile::setPermissions(filePath, permissions | QFile::WriteUser) && QFile::remove(filePath);
144  }
145  }
146  }
147 
148  if(!ok)
149  {
150  success = false;
151  }
152  }
153 
154  if(success && self)
155  {
156  success = dr.rmdir(dr.absolutePath());
157  }
158  return success;
159 }
160 
161 QString TTK::File::getExistingDirectory(QWidget *parent)
162 {
163  QString path = G_SETTING_PTR->value(MusicSettingManager::LastFileDialogPath).toString();
164  path = QFileDialog::getExistingDirectory(parent, QObject::tr("Choose a dir to open under"), path);
165  if(!path.isEmpty())
166  {
167  path += TTK_SEPARATOR;
169  }
170  return path;
171 }
172 
173 QString TTK::File::getOpenFileName(QWidget *parent, const QString &filter)
174 {
175  QString path = QFileInfo(G_SETTING_PTR->value(MusicSettingManager::LastFileDialogPath).toString()).absolutePath();
176  path = QFileDialog::getOpenFileName(parent, QObject::tr("Choose a filename to open under"), path, filter);
177  if(!path.isEmpty())
178  {
180  }
181  return path;
182 }
183 
184 QStringList TTK::File::getOpenFileNames(QWidget *parent, const QString &filter)
185 {
186  const QString &path = QFileInfo(G_SETTING_PTR->value(MusicSettingManager::LastFileDialogPath).toString()).absolutePath();
187  const QStringList &files = QFileDialog::getOpenFileNames(parent, QObject::tr("Choose a filename to open under"), path, filter);
188  if(!files.isEmpty())
189  {
190  const QString &v = files.front();
191  if(!v.isEmpty())
192  {
194  }
195  }
196  return files;
197 }
198 
199 QString TTK::File::getSaveFileName(QWidget *parent, const QString &filter)
200 {
201  const QString &title = QObject::tr("Choose a filename to save under");
202  QString path = G_SETTING_PTR->value(MusicSettingManager::LastFileDialogPath).toString();
203 #if defined Q_OS_WIN || defined Q_OS_MAC
204  path = QFileDialog::getSaveFileName(parent, title, path, filter);
205 #else
206  QFileDialog dialog(parent, title, QFileInfo(path).absolutePath(), filter);
207  dialog.setAcceptMode(QFileDialog::AcceptSave);
208  if(parent)
209  {
210  dialog.setWindowModality(Qt::WindowModal);
211  }
212 
213  if(dialog.exec() != QDialog::Accepted)
214  {
215  return {};
216  }
217 
218  const QString &selectFilter = dialog.selectedNameFilter();
219  const QStringList &filters = filter.split(";;");
220 
221  if(!filters.isEmpty())
222  {
223  const QRegExp regx("(?:^\\*\\.(?!.*\\()|\\(\\*\\.)(\\w+)");
224  if(regx.indexIn(selectFilter) != -1)
225  {
226  dialog.setDefaultSuffix(regx.cap(1));
227  }
228  }
229 
230  const QStringList &files = dialog.selectedFiles();
231  if(files.isEmpty())
232  {
233  return {};
234  }
235 
236  path = files.front();
237  const QString &suffix = dialog.defaultSuffix();
238  if(!suffix.isEmpty() && suffix != QFileInfo(path).suffix())
239  {
240  path += "." + suffix;
241  }
242 #endif
243  if(!path.isEmpty())
244  {
246  }
247  return path;
248 }
TTK_MODULE_EXPORT QString getExistingDirectory(QWidget *parent)
TTK_MODULE_EXPORT QString getOpenFileName(QWidget *parent, const QString &filter="Image Files (*.png *.bmp *.jpg)")
TTK_MODULE_EXPORT bool copyPath(const QString &srcPath, const QString &dstPath, bool overwrite)
TTK_MODULE_EXPORT QString getSaveFileName(QWidget *parent, const QString &filter="Image Files (*.png *.bmp *.jpg)")
#define qAsConst
Definition: ttkqtglobal.h:53
TTK_MODULE_EXPORT QString suffix(const QString &name)
TTK_MODULE_EXPORT bool isBreakPointEnabled()
#define TTK_SEPARATOR
Definition: ttkglobal.h:195
TTK_MODULE_EXPORT QFileInfoList fileInfoListByPath(const QString &dpath, const QStringList &filter={}, bool recursively=true)
TTK_MODULE_EXPORT QStringList getOpenFileNames(QWidget *parent, const QString &filter="Image Files (*.png *.bmp *.jpg)")
TTK_MODULE_EXPORT QStringList fileListByPath(const QString &dpath, const QStringList &filter={}, bool recursively=true)
TTK_MODULE_EXPORT bool removeRecursively(const QString &dir, bool self=true)
#define G_SETTING_PTR