casesensitivefsfileseeker.cpp
1 //------------------------------------------------------------------------------
2 // casesensitivefsfileseeker.cpp
3 //------------------------------------------------------------------------------
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 // 02110-1301 USA
19 //
20 //------------------------------------------------------------------------------
21 // Copyright (C) 2013 "Zalewa" <zalewapl@gmail.com>
22 //------------------------------------------------------------------------------
23 #include "casesensitivefsfileseeker.h"
24 
25 #include "pathfinder/filesearchpath.h"
26 
27 #include <QDir>
28 #include <QFileInfo>
29 #include <QSet>
30 #include <QStringList>
31 
32 CaseSensitiveFSFileSeeker::CaseSensitiveFSFileSeeker(QSharedPointer <QList<FileSearchPath> > paths) :
33  FileSeeker(paths)
34 {
35 }
36 
37 QString CaseSensitiveFSFileSeeker::findFileInPath(const QString &fileName, FileSearchPath &path)
38 {
39  if (!path.hasCache())
40  generatePathCacheAndEditPaths(path);
41  if (path.getCache().contains(fileName.toLower()))
42  return QDir(path.path()).absoluteFilePath(path.getCache().value(fileName.toLower()));
43  return QString();
44 }
45 
46 void CaseSensitiveFSFileSeeker::generatePathCacheAndEditPaths(FileSearchPath &path)
47 {
48  QFileInfoList entriesDirectory = QDir(path.path()).entryInfoList(QDir::AllEntries | QDir::NoDotAndDotDot);
49  QSet<QString> entriesDirAbsolutePath;
50  QMap<QString, QString> entriesFileNames;
51 
52  for (const QFileInfo &entry : entriesDirectory)
53  {
54  if (entry.isFile())
55  entriesFileNames.insert(entry.fileName().toLower(), entry.fileName());
56  else if (entry.isDir())
57  entriesDirAbsolutePath << entry.absoluteFilePath();
58  }
59 
60  if (path.isRecursive())
61  {
62  QList<FileSearchPath> subpaths;
63  for (const QString &entry : entriesDirAbsolutePath)
64  {
65  FileSearchPath subpath = FileSearchPath(entry);
66  subpath.setRecursive(true);
67  subpaths << subpath;
68  }
69  insertSubpathsAfterPath(path, subpaths);
70  path.setRecursive(false);
71  }
72  path.setCache(entriesFileNames);
73 }
74 
75 void CaseSensitiveFSFileSeeker::insertSubpathsAfterPath(const FileSearchPath &path, QList<FileSearchPath> subpaths)
76 {
77  QList<FileSearchPath> postPathBaseEntries;
78  while (!paths->isEmpty() && paths->last().path() != path.path())
79  postPathBaseEntries.prepend(paths->takeLast());
80  *paths.data() << subpaths << postPathBaseEntries;
81 }