| 1 |
/* |
| 2 |
* This file is part of the apvlv package |
| 3 |
* Copyright (C) <2008> <Alf> |
| 4 |
* |
| 5 |
* Contact: Alf <[email protected]> |
| 6 |
* |
| 7 |
* This program is free software; you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU General Public License as published by |
| 9 |
* the Free Software Foundation; either version 2 of the License, or |
| 10 |
* (at your option) any later version. |
| 11 |
* |
| 12 |
* This program is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License along |
| 18 |
* with this program; if not, write to the Free Software Foundation, Inc., |
| 19 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 |
* |
| 21 |
*/ |
| 22 |
/* @CPPFILE File.cc |
| 23 |
* |
| 24 |
* Author: Alf <[email protected]> |
| 25 |
*/ |
| 26 |
|
| 27 |
#include <QBuffer> |
| 28 |
#include <QDebug> |
| 29 |
#include <algorithm> |
| 30 |
#include <filesystem> |
| 31 |
#include <iostream> |
| 32 |
#include <stack> |
| 33 |
|
| 34 |
#include "ApvlvFile.h" |
| 35 |
#include "ApvlvFileIndex.h" |
| 36 |
#include "ApvlvUtil.h" |
| 37 |
|
| 38 |
namespace apvlv |
| 39 |
{ |
| 40 |
|
| 41 |
using namespace std; |
| 42 |
|
| 43 |
void |
| 44 |
FileIndex::loadDirectory (const string &path1) |
| 45 |
{ |
| 46 |
auto exts = FileFactory::supportFileExts (); |
| 47 |
|
| 48 |
try |
| 49 |
{ |
| 50 |
for (auto &entry : filesystem::directory_iterator ( |
| 51 |
path1, filesystem::directory_options::follow_directory_symlink)) |
| 52 |
{ |
| 53 |
if (entry.is_directory ()) |
| 54 |
{ |
| 55 |
auto index |
| 56 |
= FileIndex (entry.path ().filename ().string (), 0, |
| 57 |
entry.path ().string (), FileIndexType::DIR); |
| 58 |
index.loadDirectory (entry.path ().string ()); |
| 59 |
auto last = entry.last_write_time (); |
| 60 |
index.mtime = filesystemTimeToMSeconds (last); |
| 61 |
if (!index.mChildrenIndex.empty ()) |
| 62 |
{ |
| 63 |
mChildrenIndex.emplace_back (index); |
| 64 |
} |
| 65 |
} |
| 66 |
else if (entry.file_size () > 0) |
| 67 |
{ |
| 68 |
auto file_ext = filenameExtension (entry.path ().string ()); |
| 69 |
if (find (exts.cbegin (), exts.cend (), file_ext) |
| 70 |
!= exts.cend ()) |
| 71 |
{ |
| 72 |
auto index = FileIndex (entry.path ().filename ().string (), |
| 73 |
0, entry.path ().string (), |
| 74 |
FileIndexType::FILE); |
| 75 |
index.size = static_cast<int64_t> (entry.file_size ()); |
| 76 |
auto last = entry.last_write_time (); |
| 77 |
index.mtime = filesystemTimeToMSeconds (last); |
| 78 |
mChildrenIndex.emplace_back (index); |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
catch (filesystem::filesystem_error &err) |
| 84 |
{ |
| 85 |
qWarning () << "file system error: " << err.what (); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
void |
| 90 |
FileIndex::moveChildChildren (const FileIndex &other_index) |
| 91 |
{ |
| 92 |
Q_ASSERT (type == FileIndexType::FILE); |
| 93 |
Q_ASSERT (other_index.type == FileIndexType::FILE); |
| 94 |
mChildrenIndex = other_index.mChildrenIndex; |
| 95 |
} |
| 96 |
|
| 97 |
void |
| 98 |
FileIndex::removeChild (const FileIndex &child) |
| 99 |
{ |
| 100 |
mChildrenIndex.remove (child); |
| 101 |
} |
| 102 |
|
| 103 |
FileIndex::FileIndex (const string &title, int page, const string &path, |
| 104 |
FileIndexType type) |
| 105 |
{ |
| 106 |
this->title = title; |
| 107 |
this->page = page; |
| 108 |
this->path = path; |
| 109 |
this->type = type; |
| 110 |
} |
| 111 |
|
| 112 |
FileIndex::~FileIndex () = default; |
| 113 |
} |
| 114 |
|
| 115 |
// Local Variables: |
| 116 |
// mode: c++ |
| 117 |
// End: |