wadpathfinder.cpp
1 //------------------------------------------------------------------------------
2 // wadpathfinder.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) 2014 "Zalewa" <zalewapl@gmail.com>
22 //------------------------------------------------------------------------------
23 #include "wadpathfinder.h"
24 
25 #include "configuration/doomseekerconfig.h"
26 #include "pathfinder/filealias.h"
27 #include "pathfinder/pathfinder.h"
28 #include "serverapi/server.h"
29 
30 DClass<WadFindResult>
31 {
32 public:
33  QString alias;
34  QString path;
35 };
36 
37 DPointered(WadFindResult)
38 
40 {
41 }
42 
43 WadFindResult::~WadFindResult()
44 {
45 }
46 
47 const QString &WadFindResult::alias() const
48 {
49  return d->alias;
50 }
51 
52 void WadFindResult::setAlias(const QString &val)
53 {
54  d->alias = val;
55 }
56 
57 bool WadFindResult::isAlias() const
58 {
59  return isValid() && !d->alias.isEmpty();
60 }
61 
62 bool WadFindResult::isValid() const
63 {
64  return !d->path.isEmpty();
65 }
66 
67 const QString &WadFindResult::path() const
68 {
69  return d->path;
70 }
71 
72 void WadFindResult::setPath(const QString &val)
73 {
74  d->path = val;
75 }
77 DClass<WadPathFinder>
78 {
79 public:
80  QList<FileAlias> aliases;
81  PathFinder pathFinder;
82  bool aliasesAllowed;
83 
84  QStringList defaultPaths()
85  {
86  QStringList paths;
87  #ifdef Q_OS_UNIX
88  paths << "/usr/local/share/games/doom/"
89  << "/usr/share/games/doom/";
90  #endif
91  return paths;
92  }
93 };
94 
95 DPointered(WadPathFinder)
96 
98 {
99  d->aliases = gConfig.doomseeker.wadAliases();
100  d->aliasesAllowed = true;
101  d->pathFinder = pathFinder;
102  for (const QString &path : d->defaultPaths())
103  d->pathFinder.addSearchDir(path);
104 }
105 
106 WadPathFinder::~WadPathFinder()
107 {
108 }
109 
110 QStringList WadPathFinder::aliases(const QString &name) const
111 {
112  if (!d->aliasesAllowed)
113  return QStringList();
114  return FileAliasList::aliases(d->aliases, name);
115 }
116 
117 WadFindResult WadPathFinder::find(const QString &name)
118 {
119  {
120  QString path = d->pathFinder.findFile(name);
121  if (!path.isEmpty())
122  {
123  WadFindResult result;
124  result.setPath(path);
125  return result;
126  }
127  }
128  for (const QString &alias : aliases(name))
129  {
130  QString path = d->pathFinder.findFile(alias);
131  if (!path.isEmpty())
132  {
133  WadFindResult result;
134  result.setPath(path);
135  result.setAlias(alias);
136  return result;
137  }
138  }
139  return WadFindResult();
140 }
141 
143 {
144  d->aliasesAllowed = allowed;
145 }
146 
148 WadFindResult findWad(ServerPtr server, const QString &wadName)
149 {
150  PathFinder pathFinder = server->wadPathFinder();
151  WadPathFinder wadFinder(pathFinder);
152  return wadFinder.find(wadName);
153 }