/
folder.cpp
412 lines (346 loc) · 9.99 KB
/
folder.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/*
* The Doomsday Engine Project -- libdeng2
*
* Copyright © 2009-2013 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* LGPL: http://www.gnu.org/licenses/lgpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 3 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
* General Public License for more details. You should have received a copy of
* the GNU Lesser General Public License along with this program; if not, see:
* http://www.gnu.org/licenses</small>
*/
#include "de/Folder"
#include "de/Feed"
#include "de/FS"
#include "de/NumberValue"
#include "de/Log"
#include "de/Guard"
using namespace de;
Folder::Folder(String const &name) : File(name)
{
setStatus(Status::FOLDER);
// Standard info.
info().add(new Variable("contentSize",
new Accessor(*this, Accessor::CONTENT_SIZE),
Accessor::VARIABLE_MODE));
}
Folder::~Folder()
{
DENG2_GUARD(this);
DENG2_FOR_AUDIENCE2(Deletion, i) i->fileBeingDeleted(*this);
audienceForDeletion().clear();
deindex();
// Empty the contents.
clear();
// Destroy all feeds that remain.
for(Feeds::reverse_iterator i = _feeds.rbegin(); i != _feeds.rend(); ++i)
{
delete *i;
}
}
String Folder::describe() const
{
DENG2_GUARD(this);
String desc = String("folder \"%1\" (with %2 items from %3 feeds")
.arg(name()).arg(_contents.size()).arg(_feeds.size());
if(!_feeds.empty())
{
int n = 0;
DENG2_FOR_EACH_CONST(Feeds, i, _feeds)
{
desc += String("; feed #%1 is %2").arg(++n).arg((*i)->description());
}
}
return desc + ")";
}
void Folder::clear()
{
DENG2_GUARD(this);
if(_contents.empty()) return;
// Destroy all the file objects.
for(Contents::iterator i = _contents.begin(); i != _contents.end(); ++i)
{
i->second->setParent(0);
delete i->second;
}
_contents.clear();
}
void Folder::populate(PopulationBehavior behavior)
{
DENG2_GUARD(this);
LOG_AS("Folder");
// Prune the existing files first.
for(Contents::iterator i = _contents.begin(); i != _contents.end(); )
{
// By default we will NOT prune if there are no feeds attached to the folder.
// In this case the files were probably created manually, so we shouldn't
// touch them.
bool mustPrune = false;
File *file = i->second;
// If the file has a designated feed, ask it about pruning.
if(file->originFeed() && file->originFeed()->prune(*file))
{
LOG_RES_XVERBOSE("Pruning \"%s\"") << file->path();
mustPrune = true;
}
else if(!file->originFeed())
{
// There is no designated feed, ask all feeds of this folder.
// If even one of the feeds thinks that the file is out of date,
// it will be pruned.
for(Feeds::iterator f = _feeds.begin(); f != _feeds.end(); ++f)
{
if((*f)->prune(*file))
{
LOG_RES_XVERBOSE("Pruning ") << file->path();
mustPrune = true;
break;
}
}
}
if(mustPrune)
{
// It needs to go.
_contents.erase(i++);
delete file;
}
else
{
++i;
}
}
// Populate with new/updated ones.
for(Feeds::reverse_iterator i = _feeds.rbegin(); i != _feeds.rend(); ++i)
{
(*i)->populate(*this);
}
if(behavior == PopulateFullTree)
{
// Call populate on subfolders.
for(Contents::iterator i = _contents.begin(); i != _contents.end(); ++i)
{
if(Folder *folder = i->second->maybeAs<Folder>())
{
folder->populate();
}
}
}
}
Folder::Contents const &Folder::contents() const
{
DENG2_GUARD(this);
return _contents;
}
File &Folder::newFile(String const &newPath, FileCreationBehavior behavior)
{
DENG2_GUARD(this);
String path = newPath.fileNamePath();
if(!path.empty())
{
// Locate the folder where the file will be created in.
return locate<Folder>(path).newFile(newPath.fileName(), behavior);
}
verifyWriteAccess();
if(behavior == ReplaceExisting && has(newPath))
{
try
{
removeFile(newPath);
}
catch(Feed::RemoveError const &er)
{
LOG_RES_WARNING("Failed to replace %s: existing file could not be removed.\n")
<< newPath << er.asText();
}
}
// The first feed able to create a file will get the honors.
for(Feeds::iterator i = _feeds.begin(); i != _feeds.end(); ++i)
{
File *file = (*i)->newFile(newPath);
if(file)
{
// Allow writing to the new file.
file->setMode(Write);
add(file);
fileSystem().index(*file);
return *file;
}
}
/// @throw NewFileError All feeds of this folder failed to create a file.
throw NewFileError("Folder::newFile", "Unable to create new file '" + newPath +
"' in " + description());
}
File &Folder::replaceFile(String const &newPath)
{
return newFile(newPath, ReplaceExisting);
}
void Folder::removeFile(String const &removePath)
{
DENG2_GUARD(this);
String path = removePath.fileNamePath();
if(!path.empty())
{
// Locate the folder where the file will be removed.
return locate<Folder>(path).removeFile(removePath.fileName());
}
verifyWriteAccess();
// It should now be in this folder.
File *file = &locate<File>(removePath);
Feed *originFeed = file->originFeed();
// This'll close it and remove it from the index.
delete file;
// The origin feed will remove the original data of the file (e.g., the native file).
if(originFeed)
{
originFeed->removeFile(removePath);
}
}
bool Folder::has(String const &name) const
{
DENG2_GUARD(this);
if(name.isEmpty()) return false;
// Check if we were given a path rather than just a name.
String path = name.fileNamePath();
if(!path.empty())
{
Folder *folder = tryLocate<Folder>(path);
if(folder)
{
return folder->has(name.fileName());
}
return false;
}
return (_contents.find(name.lower()) != _contents.end());
}
File &Folder::add(File *file)
{
DENG2_GUARD(this);
DENG2_ASSERT(file != 0);
if(has(file->name()))
{
/// @throw DuplicateNameError All file names in a folder must be unique.
throw DuplicateNameError("Folder::add", "Folder cannot contain two files with the same name: '" +
file->name() + "'");
}
_contents[file->name().lower()] = file;
file->setParent(this);
return *file;
}
File *Folder::remove(File &file)
{
DENG2_GUARD(this);
for(Contents::iterator i = _contents.begin(); i != _contents.end(); ++i)
{
if(i->second == &file)
{
_contents.erase(i);
break;
}
}
file.setParent(0);
return &file;
}
File *Folder::tryLocateFile(String const &path) const
{
if(path.empty())
{
File *file = const_cast<Folder *>(this);
return file;
}
if(path[0] == '/')
{
// Route back to the root of the file system.
File *file = fileSystem().root().tryLocateFile(path.substr(1));
return file;
}
DENG2_GUARD(this);
// Extract the next component.
String::size_type end = path.indexOf('/');
if(end == String::npos)
{
// No more slashes. What remains is the final component.
Contents::const_iterator found = _contents.find(path.lower());
if(found != _contents.end())
{
File *file = found->second;
return file;
}
return 0;
}
String component = path.substr(0, end);
String remainder = path.substr(end + 1);
// Check for some special cases.
if(component == ".")
{
File *file = tryLocateFile(remainder);
return file;
}
if(component == "..")
{
if(!parent())
{
// Can't go there.
return 0;
}
File *file = parent()->tryLocateFile(remainder);
return file;
}
// Do we have a folder for this?
Contents::const_iterator found = _contents.find(component.lower());
if(found != _contents.end())
{
if(Folder *subFolder = found->second->maybeAs<Folder>())
{
// Continue recursively to the next component.
File *file = subFolder->tryLocateFile(remainder);
return file;
}
}
// Dead end.
return 0;
}
void Folder::attach(Feed *feed)
{
if(feed)
{
DENG2_GUARD(this);
_feeds.push_back(feed);
}
}
Feed *Folder::detach(Feed &feed)
{
DENG2_GUARD(this);
_feeds.remove(&feed);
return &feed;
}
void Folder::setPrimaryFeed(Feed &feed)
{
DENG2_GUARD(this);
_feeds.remove(&feed);
_feeds.push_front(&feed);
}
Folder::Accessor::Accessor(Folder &owner, Property prop) : _owner(owner), _prop(prop)
{}
void Folder::Accessor::update() const
{
DENG2_GUARD(_owner);
// We need to alter the value content.
Accessor *nonConst = const_cast<Accessor *>(this);
switch(_prop)
{
case CONTENT_SIZE:
nonConst->setValue(QString::number(_owner._contents.size()));
break;
}
}
Value *Folder::Accessor::duplicateContent() const
{
return new NumberValue(asNumber());
}