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 "templatedpathresolver.h"
26 
27 #include <QStringList>
28 
29 FileSearchPath::FileSearchPath()
30 {
31  recursive_ = false;
32 }
33 
34 FileSearchPath::FileSearchPath(const QString &path)
35 {
36  path_ = path;
37  recursive_ = false;
38 }
39 
40 QList<FileSearchPath> FileSearchPath::fromStringList(const QStringList &collection)
41 {
42  QList<FileSearchPath> result;
43  for (QString path : collection)
44  {
45  result << path;
46  }
47  return result;
48 }
49 
50 FileSearchPath FileSearchPath::fromVariant(const QVariant &var)
51 {
52  FileSearchPath result;
53  QVariantList list = var.toList();
54  if (list.size() >= 1 && list.size() <= 2)
55  {
56  result.setPath(list[0].toString());
57  if (list.size() == 2)
58  {
59  result.setRecursive(list[1].toBool());
60  }
61  }
62  return result;
63 }
64 
65 QList<FileSearchPath> FileSearchPath::fromVariantList(const QVariantList &collection)
66 {
67  QList<FileSearchPath> result;
68  for (const QVariant &variant : collection)
69  {
70  result << fromVariant(variant);
71  }
72  return result;
73 }
74 
75 bool FileSearchPath::isValid() const
76 {
77  return !path_.isNull();
78 }
79 
80 FileSearchPath FileSearchPath::resolveTemplated(TemplatedPathResolver &resolver,
81  const FileSearchPath &path)
82 {
83  FileSearchPath resolved = path;
84  resolved.path_ = resolver.resolve(resolved.path_);
85  return resolved;
86 }
87 
88 QList<FileSearchPath> FileSearchPath::resolveTemplated(TemplatedPathResolver &resolver,
89  const QList<FileSearchPath> &paths)
90 {
91  QList<FileSearchPath> resolved;
92  for (const FileSearchPath &path : paths)
93  resolved << resolveTemplated(resolver, path);
94  return resolved;
95 }
96 
97 QVariant FileSearchPath::toVariant() const
98 {
99  QVariantList var;
100  var << path_;
101  var << recursive_;
102  return var;
103 }
104 
105 QVariantList FileSearchPath::toVariantList(const QList<FileSearchPath> &collection)
106 {
107  QVariantList result;
108  for (const FileSearchPath &path : collection)
109  {
110  result << path.toVariant();
111  }
112  return result;
113 }
114 
115 void FileSearchPath::setCache(const QMap<QString, QString> &files)
116 {
117  cacheFiles_ = files;
118 }
119 
120 bool FileSearchPath::hasCache()
121 {
122  return !cacheFiles_.isEmpty();
123 }
124 
125 const QMap<QString, QString> &FileSearchPath::getCache()
126 {
127  return cacheFiles_;
128 }