summaryrefslogtreecommitdiff
path: root/src/shared/filesorigin.cpp
blob: 7476b6c53380e6e9c3ae9ac611ee0e7b959f5437 (plain)
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
#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(
  OriginID 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