2 * Copyright (C) 2005-2020 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
9 #include "AddonUpdateRules.h"
11 #include "AddonDatabase.h"
12 #include "addons/addoninfo/AddonInfo.h"
13 #include "utils/log.h"
18 using namespace ADDON
;
20 bool CAddonUpdateRules::RefreshRulesMap(const CAddonDatabase
& db
)
22 m_updateRules
.clear();
23 db
.GetAddonUpdateRules(m_updateRules
);
27 bool CAddonUpdateRules::IsAutoUpdateable(const std::string
& id
) const
29 std::unique_lock
<CCriticalSection
> lock(m_critSection
);
30 return m_updateRules
.find(id
) == m_updateRules
.end();
33 bool CAddonUpdateRules::IsUpdateableByRule(const std::string
& id
, AddonUpdateRule updateRule
) const
35 std::unique_lock
<CCriticalSection
> lock(m_critSection
);
36 const auto& updateRulesEntry
= m_updateRules
.find(id
);
37 return (updateRulesEntry
== m_updateRules
.end() ||
38 std::none_of(updateRulesEntry
->second
.begin(), updateRulesEntry
->second
.end(),
39 [updateRule
](AddonUpdateRule rule
) { return rule
== updateRule
; }));
42 bool CAddonUpdateRules::AddUpdateRuleToList(CAddonDatabase
& db
,
43 const std::string
& id
,
44 AddonUpdateRule updateRule
)
46 std::unique_lock
<CCriticalSection
> lock(m_critSection
);
48 if (!IsUpdateableByRule(id
, updateRule
))
53 if (db
.AddUpdateRuleForAddon(id
, updateRule
))
55 m_updateRules
[id
].emplace_back(updateRule
);
61 bool CAddonUpdateRules::RemoveUpdateRuleFromList(CAddonDatabase
& db
,
62 const std::string
& id
,
63 AddonUpdateRule updateRule
)
65 return (updateRule
!= AddonUpdateRule::ANY
&& RemoveFromUpdateRuleslist(db
, id
, updateRule
));
68 bool CAddonUpdateRules::RemoveAllUpdateRulesFromList(CAddonDatabase
& db
, const std::string
& id
)
70 return RemoveFromUpdateRuleslist(db
, id
, AddonUpdateRule::ANY
);
73 bool CAddonUpdateRules::RemoveFromUpdateRuleslist(CAddonDatabase
& db
,
74 const std::string
& id
,
75 AddonUpdateRule updateRule
)
77 std::unique_lock
<CCriticalSection
> lock(m_critSection
);
79 const auto& updateRulesEntry
= m_updateRules
.find(id
);
80 if (updateRulesEntry
!= m_updateRules
.end())
82 bool onlySingleRule
= (updateRulesEntry
->second
.size() == 1);
84 if (updateRule
== AddonUpdateRule::ANY
||
85 (onlySingleRule
&& updateRulesEntry
->second
.front() == updateRule
))
87 if (db
.RemoveAllUpdateRulesForAddon(id
))
89 m_updateRules
.erase(id
);
93 else if (!onlySingleRule
)
95 const auto& position
=
96 std::find(updateRulesEntry
->second
.begin(), updateRulesEntry
->second
.end(), updateRule
);
97 if (position
!= updateRulesEntry
->second
.end() && db
.RemoveUpdateRuleForAddon(id
, updateRule
))
99 updateRulesEntry
->second
.erase(position
);