summaryrefslogtreecommitdiffstats
path: root/xbmc/addons/AddonUpdateRules.cpp
blob: 7d9d43a7c6fdd66e1020b52c56fb328cb01f350a (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
/*
 *  Copyright (C) 2005-2020 Team Kodi
 *  This file is part of Kodi - https://kodi.tv
 *
 *  SPDX-License-Identifier: GPL-2.0-or-later
 *  See LICENSES/README.md for more information.
 */

#include "AddonUpdateRules.h"

#include "AddonDatabase.h"
#include "addons/addoninfo/AddonInfo.h"
#include "utils/log.h"

#include <mutex>

using namespace ADDON;

bool CAddonUpdateRules::RefreshRulesMap(const CAddonDatabase& db)
{
  m_updateRules.clear();
  db.GetAddonUpdateRules(m_updateRules);
  return true;
}

bool CAddonUpdateRules::IsAutoUpdateable(const std::string& id) const
{
  std::unique_lock<CCriticalSection> lock(m_critSection);
  return m_updateRules.find(id) == m_updateRules.end();
}

bool CAddonUpdateRules::IsUpdateableByRule(const std::string& id, AddonUpdateRule updateRule) const
{
  std::unique_lock<CCriticalSection> lock(m_critSection);
  const auto& updateRulesEntry = m_updateRules.find(id);
  return (updateRulesEntry == m_updateRules.end() ||
          std::none_of(updateRulesEntry->second.begin(), updateRulesEntry->second.end(),
                       [updateRule](AddonUpdateRule rule) { return rule == updateRule; }));
}

bool CAddonUpdateRules::AddUpdateRuleToList(CAddonDatabase& db,
                                            const std::string& id,
                                            AddonUpdateRule updateRule)
{
  std::unique_lock<CCriticalSection> lock(m_critSection);

  if (!IsUpdateableByRule(id, updateRule))
  {
    return true;
  }

  if (db.AddUpdateRuleForAddon(id, updateRule))
  {
    m_updateRules[id].emplace_back(updateRule);
    return true;
  }
  return false;
}

bool CAddonUpdateRules::RemoveUpdateRuleFromList(CAddonDatabase& db,
                                                 const std::string& id,
                                                 AddonUpdateRule updateRule)
{
  return (updateRule != AddonUpdateRule::ANY && RemoveFromUpdateRuleslist(db, id, updateRule));
}

bool CAddonUpdateRules::RemoveAllUpdateRulesFromList(CAddonDatabase& db, const std::string& id)
{
  return RemoveFromUpdateRuleslist(db, id, AddonUpdateRule::ANY);
}

bool CAddonUpdateRules::RemoveFromUpdateRuleslist(CAddonDatabase& db,
                                                  const std::string& id,
                                                  AddonUpdateRule updateRule)
{
  std::unique_lock<CCriticalSection> lock(m_critSection);

  const auto& updateRulesEntry = m_updateRules.find(id);
  if (updateRulesEntry != m_updateRules.end())
  {
    bool onlySingleRule = (updateRulesEntry->second.size() == 1);

    if (updateRule == AddonUpdateRule::ANY ||
        (onlySingleRule && updateRulesEntry->second.front() == updateRule))
    {
      if (db.RemoveAllUpdateRulesForAddon(id))
      {
        m_updateRules.erase(id);
        return true;
      }
    }
    else if (!onlySingleRule)
    {
      const auto& position =
          std::find(updateRulesEntry->second.begin(), updateRulesEntry->second.end(), updateRule);
      if (position != updateRulesEntry->second.end() && db.RemoveUpdateRuleForAddon(id, updateRule))
      {
        updateRulesEntry->second.erase(position);
        return true;
      }
    }
  }
  return false;
}