diff options
| author | isanae <14251494+isanae@users.noreply.github.com> | 2019-12-18 20:05:47 -0500 |
|---|---|---|
| committer | isanae <14251494+isanae@users.noreply.github.com> | 2020-02-04 03:33:18 -0500 |
| commit | f27a73b123f9a1cc7b25dfb0982047b9cc3c2d34 (patch) | |
| tree | ab0d0db4f089d9cab7dc8364df22dd1546a3d678 /src | |
| parent | 2f07eb51c1a117d60fdda1b986fbd23766a3e8c6 (diff) | |
implemented dump to file
fixed duplicate module notifications not being skipped
Diffstat (limited to 'src')
| -rw-r--r-- | src/datatab.cpp | 57 | ||||
| -rw-r--r-- | src/filetree.cpp | 79 | ||||
| -rw-r--r-- | src/filetree.h | 6 | ||||
| -rw-r--r-- | src/shared/directoryentry.h | 10 |
4 files changed, 93 insertions, 59 deletions
diff --git a/src/datatab.cpp b/src/datatab.cpp index 5f9a17b3..489f0554 100644 --- a/src/datatab.cpp +++ b/src/datatab.cpp @@ -257,70 +257,13 @@ void DataTab::hideFile() void DataTab::unhideFile() { - auto* item = singleSelection(); - if (!item) { - return; - } - - QString oldName = item->data(0, Qt::UserRole).toString(); - QString newName = oldName.left(oldName.length() - ModInfo::s_HiddenExt.length()); - if (QFileInfo(newName).exists()) { - if (QMessageBox::question(m_parent, tr("Replace file?"), tr("There already is a visible version of this file. Replace it?"), - QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) { - if (!QFile(newName).remove()) { - QMessageBox::critical(m_parent, tr("File operation failed"), tr("Failed to remove \"%1\". Maybe you lack the required file permissions?").arg(newName)); - return; - } - } else { - return; - } - } - if (QFile::rename(oldName, newName)) { - emit originModified(item->data(1, Qt::UserRole + 1).toInt()); - refreshDataTreeKeepExpandedNodes(); - } else { - reportError(tr("failed to rename \"%1\" to \"%2\"").arg(QDir::toNativeSeparators(oldName)).arg(QDir::toNativeSeparators(newName))); - } } void DataTab::writeDataToFile( QFile &file, const QString &directory, const DirectoryEntry &directoryEntry) { - for (FileEntry::Ptr current : directoryEntry.getFiles()) { - bool isArchive = false; - int origin = current->getOrigin(isArchive); - if (isArchive) { - // TODO: don't list files from archives. maybe make this an option? - continue; - } - QString fullName = directory + "\\" + ToQString(current->getName()); - file.write(fullName.toUtf8()); - - file.write("\t("); - file.write(ToQString(m_core.directoryStructure()->getOriginByID(origin).getName()).toUtf8()); - file.write(")\r\n"); - } - - // recurse into subdirectories - std::vector<DirectoryEntry*>::const_iterator current, end; - directoryEntry.getSubDirectories(current, end); - for (; current != end; ++current) { - writeDataToFile(file, directory + "\\" + ToQString((*current)->getName()), **current); - } } void DataTab::writeDataToFile() { - QString fileName = QFileDialog::getSaveFileName(m_parent); - if (!fileName.isEmpty()) { - QFile file(fileName); - if (!file.open(QIODevice::WriteOnly)) { - reportError(tr("failed to write to file %1").arg(fileName)); - } - - writeDataToFile(file, "data", *m_core.directoryStructure()); - file.close(); - - MessageDialog::showMessage(tr("%1 written").arg(QDir::toNativeSeparators(fileName)), m_parent); - } } diff --git a/src/filetree.cpp b/src/filetree.cpp index c80024c5..ee6d932d 100644 --- a/src/filetree.cpp +++ b/src/filetree.cpp @@ -1004,8 +1004,85 @@ void FileTree::unhide() toggleVisibility(true); } -void FileTree::dumpToFile() +class DumpFailed {}; + +void FileTree::dumpToFile() const +{ + log::debug("dumping filetree to file"); + + QString file = QFileDialog::getSaveFileName(m_tree->window()); + if (file.isEmpty()) { + log::debug("user cancelled"); + return; + } + + QFile out(file); + + if (!out.open(QIODevice::WriteOnly)) { + QMessageBox::critical( + m_tree->window(), + QObject::tr("Error"), + QObject::tr("Failed to open file '%1': %2") + .arg(file) + .arg(out.errorString())); + + return; + } + + try + { + dumpToFile(out, "Data", *m_core.directoryStructure()); + } + catch(DumpFailed&) + { + // try to remove it silently + if (out.exists()) { + if (!out.remove()) { + log::error("failed to remove '{}', ignoring", file); + } + } + } +} + +void FileTree::dumpToFile( + QFile& out, const QString& parentPath, const DirectoryEntry& entry) const { + entry.forEachFile([&](auto&& file) { + bool isArchive = false; + const int originID = file.getOrigin(isArchive); + + if (isArchive) { + // TODO: don't list files from archives. maybe make this an option? + return true; + } + + const auto& origin = m_core.directoryStructure()->getOriginByID(originID); + const auto originName = QString::fromStdWString(origin.getName()); + + const QString path = + parentPath + "\\" + QString::fromStdWString(file.getName()); + + if (out.write(path.toUtf8() + "\t(" + originName.toUtf8() + ")\r\n") == -1) { + QMessageBox::critical( + m_tree->window(), + QObject::tr("Error"), + QObject::tr("Failed to write to file %1: %2") + .arg(out.fileName()) + .arg(out.errorString())); + + throw DumpFailed(); + } + + return true; + }); + + entry.forEachDirectory([&](auto&& dir) { + const auto newParentPath = + parentPath + "\\" + QString::fromStdWString(dir.getName()); + + dumpToFile(out, newParentPath, dir); + return true; + }); } void FileTree::onContextMenu(const QPoint &pos) diff --git a/src/filetree.h b/src/filetree.h index 05d63f62..08ebdcb7 100644 --- a/src/filetree.h +++ b/src/filetree.h @@ -173,7 +173,7 @@ public: void hide(); void unhide(); - void dumpToFile(); + void dumpToFile() const; signals: void executablesChanged(); @@ -195,6 +195,10 @@ private: void addCommonMenus(QMenu& menu); void toggleVisibility(bool b); + + void dumpToFile( + QFile& out, const QString& parentPath, + const MOShared::DirectoryEntry& entry) const; }; #endif // MODORGANIZER_FILETREE_INCLUDED diff --git a/src/shared/directoryentry.h b/src/shared/directoryentry.h index 8766f0c6..0a857443 100644 --- a/src/shared/directoryentry.h +++ b/src/shared/directoryentry.h @@ -250,6 +250,16 @@ public: }
template <class F>
+ void forEachDirectory(F&& f) const
+ {
+ for (auto&& d : m_SubDirectories) {
+ if (!f(*d)) {
+ break;
+ }
+ }
+ }
+
+ template <class F>
void forEachFile(F&& f) const
{
for (auto&& p : m_Files) {
|
