filesearchpath.cpp
1 //------------------------------------------------------------------------------
2 // filesearchpath.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 "filesearchpath.h"
24 
25 #include "strings.hpp"
26 #include "templatedpathresolver.h"
27 
28 #include <QDir>
29 #include <QStringList>
30 
31 FileSearchPath::FileSearchPath()
32 {
33  recursive_ = false;
34 }
35 
36 FileSearchPath::FileSearchPath(const QString &path)
37 {
38  path_ = path;
39  recursive_ = false;
40 }
41 
42 QList<FileSearchPath> FileSearchPath::fromStringList(const QStringList &collection)
43 {
44  QList<FileSearchPath> result;
45  for (QString path : collection)
46  {
47  result << path;
48  }
49  return result;
50 }
51 
52 FileSearchPath FileSearchPath::fromVariant(const QVariant &var)
53 {
54  FileSearchPath result;
55  QVariantList list = var.toList();
56  if (list.size() >= 1 && list.size() <= 2)
57  {
58  result.setPath(list[0].toString());
59  if (list.size() == 2)
60  {
61  result.setRecursive(list[1].toBool());
62  }
63  }
64  return result;
65 }
66 
67 QList<FileSearchPath> FileSearchPath::fromVariantList(const QVariantList &collection)
68 {
69  QList<FileSearchPath> result;
70  for (const QVariant &variant : collection)
71  {
72  result << fromVariant(variant);
73  }
74  return result;
75 }
76 
77 bool FileSearchPath::isValid() const
78 {
79  return !path_.isNull();
80 }
81 
82 FileSearchPath FileSearchPath::resolveTemplated(TemplatedPathResolver &resolver,
83  const FileSearchPath &path)
84 {
85  FileSearchPath resolved = path;
86  resolved.path_ = resolver.resolve(resolved.path_);
87  return resolved;
88 }
89 
90 QList<FileSearchPath> FileSearchPath::resolveTemplated(TemplatedPathResolver &resolver,
91  const QList<FileSearchPath> &paths)
92 {
93  QList<FileSearchPath> resolved;
94  for (const FileSearchPath &path : paths)
95  resolved << resolveTemplated(resolver, path);
96  return resolved;
97 }
98 
99 QVariant FileSearchPath::toVariant() const
100 {
101  QVariantList var;
102  var << path_;
103  var << recursive_;
104  return var;
105 }
106 
107 QVariantList FileSearchPath::toVariantList(const QList<FileSearchPath> &collection)
108 {
109  QVariantList result;
110  for (const FileSearchPath &path : collection)
111  {
112  result << path.toVariant();
113  }
114  return result;
115 }
116 
117 void FileSearchPath::setCache(const QMap<QString, QString> &files)
118 {
119  cacheFiles_ = files;
120 }
121 
122 bool FileSearchPath::hasCache()
123 {
124  return !cacheFiles_.isEmpty();
125 }
126 
127 const QMap<QString, QString> &FileSearchPath::getCache()
128 {
129  return cacheFiles_;
130 }
131 
132 bool FileSearchPath::contains(const QString &other_) const
133 {
134  QString self = QDir::cleanPath(this->path_);
135  QString other = QDir::cleanPath(other_);
136  self = Strings::trimr(self, "/");
137  other = Strings::trimr(other, "/");
138 
139  #ifdef Q_OS_WIN
140  self = self.toLower();
141  other = other.toLower();
142  #endif
143 
144  return self == other || (this->recursive_ && other.startsWith(self + '/'));
145 }
146 
147 void FileSearchPath::merge(QList<FileSearchPath> &paths)
148 {
149  for (int idx1 = 0; idx1 < paths.length(); ++idx1)
150  {
151  for (int idx2 = idx1 + 1; idx2 < paths.length(); ++idx2)
152  {
153  FileSearchPath &path1 = paths[idx1];
154  FileSearchPath &path2 = paths[idx2];
155 
156  if (path2.contains(path1.path()))
157  {
158  std::swap(path1, path2);
159  }
160 
161  if (path1.contains(path2.path()))
162  {
163  path1.setRecursive(path1.isRecursive() || path2.isRecursive());
164  paths.removeAt(idx2);
165  --idx2;
166  }
167  }
168  }
169 }