summaryrefslogtreecommitdiff
path: root/src/shared/filesorigin.cpp
diff options
context:
space:
mode:
authorisanae <14251494+isanae@users.noreply.github.com>2020-02-15 13:24:56 -0500
committerisanae <14251494+isanae@users.noreply.github.com>2020-02-18 17:25:04 -0500
commitb1cf498924e461556c8f2fe961172685d92bb03f (patch)
treef51694c37c17c87a8965eb9e8fab459d6f63d5ed /src/shared/filesorigin.cpp
parent88ef0530001d43be8f18fac43a280169b45db852 (diff)
split directoryentry
made classes noncopyable, fixed a few unintended copies
Diffstat (limited to 'src/shared/filesorigin.cpp')
-rw-r--r--src/shared/filesorigin.cpp137
1 files changed, 137 insertions, 0 deletions
diff --git a/src/shared/filesorigin.cpp b/src/shared/filesorigin.cpp
new file mode 100644
index 00000000..47ae4b94
--- /dev/null
+++ b/src/shared/filesorigin.cpp
@@ -0,0 +1,137 @@
+#include "filesorigin.h"
+#include "originconnection.h"
+#include "fileregister.h"
+#include "fileentry.h"
+
+namespace MOShared
+{
+
+std::wstring tail(const std::wstring &source, const size_t count)
+{
+ if (count >= source.length()) {
+ return source;
+ }
+
+ return source.substr(source.length() - count);
+}
+
+
+FilesOrigin::FilesOrigin()
+ : m_ID(0), m_Disabled(false), m_Name(), m_Path(), m_Priority(0)
+{
+}
+
+/*FilesOrigin::FilesOrigin(const FilesOrigin &reference)
+ : m_ID(reference.m_ID)
+ , m_Disabled(reference.m_Disabled)
+ , m_Name(reference.m_Name)
+ , m_Path(reference.m_Path)
+ , m_Priority(reference.m_Priority)
+ , m_FileRegister(reference.m_FileRegister)
+ , m_OriginConnection(reference.m_OriginConnection)
+{
+}*/
+
+FilesOrigin::FilesOrigin(
+ int ID, const std::wstring &name, const std::wstring &path, int priority,
+ boost::shared_ptr<MOShared::FileRegister> fileRegister,
+ boost::shared_ptr<MOShared::OriginConnection> originConnection) :
+ m_ID(ID), m_Disabled(false), m_Name(name), m_Path(path),
+ m_Priority(priority), m_FileRegister(fileRegister),
+ m_OriginConnection(originConnection)
+{
+}
+
+void FilesOrigin::setPriority(int priority)
+{
+ m_OriginConnection.lock()->changePriorityLookup(m_Priority, priority);
+
+ m_Priority = priority;
+}
+
+void FilesOrigin::setName(const std::wstring &name)
+{
+ m_OriginConnection.lock()->changeNameLookup(m_Name, name);
+
+ // change path too
+ if (tail(m_Path, m_Name.length()) == m_Name) {
+ m_Path = m_Path.substr(0, m_Path.length() - m_Name.length()).append(name);
+ }
+
+ m_Name = name;
+}
+
+std::vector<FileEntryPtr> FilesOrigin::getFiles() const
+{
+ std::vector<FileEntryPtr> result;
+
+ {
+ std::scoped_lock lock(m_Mutex);
+
+ for (FileIndex fileIdx : m_Files) {
+ if (FileEntryPtr p = m_FileRegister.lock()->getFile(fileIdx)) {
+ result.push_back(p);
+ }
+ }
+ }
+
+ return result;
+}
+
+FileEntryPtr FilesOrigin::findFile(FileIndex index) const
+{
+ return m_FileRegister.lock()->getFile(index);
+}
+
+void FilesOrigin::enable(bool enabled)
+{
+ DirectoryStats dummy;
+ enable(enabled, dummy);
+}
+
+void FilesOrigin::enable(bool enabled, DirectoryStats& stats)
+{
+ if (!enabled) {
+ ++stats.originsNeededEnabled;
+
+ std::set<FileIndex> copy;
+
+ {
+ std::scoped_lock lock(m_Mutex);
+ copy = m_Files;
+ m_Files.clear();
+ }
+
+ m_FileRegister.lock()->removeOriginMulti(copy, m_ID);
+ }
+
+ m_Disabled = !enabled;
+}
+
+void FilesOrigin::removeFile(FileIndex index)
+{
+ std::scoped_lock lock(m_Mutex);
+
+ auto iter = m_Files.find(index);
+
+ if (iter != m_Files.end()) {
+ m_Files.erase(iter);
+ }
+}
+
+bool FilesOrigin::containsArchive(std::wstring archiveName)
+{
+ std::scoped_lock lock(m_Mutex);
+
+ for (FileIndex fileIdx : m_Files) {
+ if (FileEntryPtr p = m_FileRegister.lock()->getFile(fileIdx)) {
+ if (p->isFromArchive(archiveName)) {
+ return true;
+ }
+ }
+ }
+
+ return false;
+}
+
+} // namespace