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