2 * Copyright (C) 2005-2018 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.
11 #include "XBDateTime.h"
12 #include "addons/AddonVersion.h"
13 #include "dbwrappers/Database.h"
26 enum class AddonDisabledReason
;
27 enum class AddonUpdateRule
;
29 class CAddonExtensions
;
30 class CAddonInfoBuilderFromDB
;
33 using AddonInfoPtr
= std::shared_ptr
<CAddonInfo
>;
36 using AddonPtr
= std::shared_ptr
<IAddon
>;
37 using VECADDONS
= std::vector
<AddonPtr
>;
40 * @brief Addon content serializer/deserializer.
42 * Used to save data from the add-on in the database using json format.
43 * The corresponding field in SQL is "addons" for "metadata".
45 * @warning Changes in the json format need a way to update the addon database
46 * for users, otherwise problems may occur when reading the old content.
48 class CAddonDatabaseSerializer
50 CAddonDatabaseSerializer() = delete;
53 static std::string
SerializeMetadata(const CAddonInfo
& addon
);
54 static void DeserializeMetadata(const std::string
& document
, CAddonInfoBuilderFromDB
& builder
);
57 static CVariant
SerializeExtensions(const CAddonExtensions
& addonType
);
58 static void DeserializeExtensions(const CVariant
& document
, CAddonExtensions
& addonType
);
61 class CAddonDatabase
: public CDatabase
65 ~CAddonDatabase() override
;
68 /*! \brief Get an addon with a specific version and repository. */
69 bool GetAddon(const std::string
& addonID
,
70 const ADDON::CAddonVersion
& version
,
71 const std::string
& repoId
,
72 ADDON::AddonPtr
& addon
);
74 /*! Get the addon IDs that have been set to disabled */
75 bool GetDisabled(std::map
<std::string
, ADDON::AddonDisabledReason
>& addons
);
77 /*! Returns all addons in the repositories with id `addonId`. */
78 bool FindByAddonId(const std::string
& addonId
, ADDON::VECADDONS
& addons
) const;
80 bool UpdateRepositoryContent(const std::string
& repositoryId
,
81 const ADDON::CAddonVersion
& version
,
82 const std::string
& checksum
,
83 const std::vector
<AddonInfoPtr
>& addons
);
85 int GetRepoChecksum(const std::string
& id
, std::string
& checksum
);
88 \brief Get addons in repository `id`
89 \param id id of the repository
90 \returns true on success, false on error or if repository have never been synced.
92 bool GetRepositoryContent(const std::string
& id
, ADDON::VECADDONS
& addons
) const;
94 /*! Get addons across all repositories */
95 bool GetRepositoryContent(ADDON::VECADDONS
& addons
) const;
99 /*! \brief last time the repo was checked, or invalid CDateTime if never checked */
100 CDateTime lastCheckedAt
;
101 /*! \brief last version of the repo add-on that was checked, or empty if never checked */
102 ADDON::CAddonVersion lastCheckedVersion
{""};
103 /*! \brief next time the repo should be checked, or invalid CDateTime if unknown */
104 CDateTime nextCheckAt
;
106 RepoUpdateData() = default;
108 RepoUpdateData(const CDateTime
& lastCheckedAt
,
109 const ADDON::CAddonVersion
& lastCheckedVersion
,
110 const CDateTime
& nextCheckAt
)
111 : lastCheckedAt
{lastCheckedAt
},
112 lastCheckedVersion
{lastCheckedVersion
},
113 nextCheckAt
{nextCheckAt
}
119 \brief Set data concerning repository update (last/next date etc.), and create the repo if needed
120 \param id add-on id of the repository
121 \param updateData update data to set
122 \returns id of the repository, or -1 on error.
124 int SetRepoUpdateData(const std::string
& id
, const RepoUpdateData
& updateData
);
127 \brief Retrieve repository update data (last/next date etc.)
128 \param id add-on id of the repo
129 \return update data of the repository
131 RepoUpdateData
GetRepoUpdateData(const std::string
& id
);
133 bool Search(const std::string
& search
, ADDON::VECADDONS
& items
);
136 * \brief Disable an addon.
137 * Sets a flag that this addon has been disabled. If disabled, it is usually still available on
139 * \param addonID id of the addon to disable
140 * \param disabledReason the reason why the addon is being disabled
141 * \return true on success, false on failure
142 * \sa IsAddonDisabled, HasDisabledAddons, EnableAddon
144 bool DisableAddon(const std::string
& addonID
, ADDON::AddonDisabledReason disabledReason
);
146 /*! \brief Enable an addon.
147 * Enables an addon that has previously been disabled
148 * \param addonID id of the addon to enable
149 * \return true on success, false on failure
150 * \sa DisableAddon, IsAddonDisabled, HasDisabledAddons
152 bool EnableAddon(const std::string
& addonID
);
155 * \brief Write dataset with addon-id and rule to the db
156 * \param addonID the addonID
157 * \param updateRule the rule value to be written
158 * \return true on success, false otherwise
160 bool AddUpdateRuleForAddon(const std::string
& addonID
, ADDON::AddonUpdateRule updateRule
);
163 * \brief Remove all rule datasets for an addon-id from the db
164 * \param addonID the addonID
165 * \return true on success, false otherwise
167 bool RemoveAllUpdateRulesForAddon(const std::string
& addonID
);
170 * \brief Remove a single rule dataset for an addon-id from the db
171 * \note specifying AddonUpdateRule::ANY will remove all rules.
172 * use @ref RemoveAllUpdateRulesForAddon() instead
173 * \param addonID the addonID
174 * \param updateRule the rule to remove
175 * \return true on success, false otherwise
177 bool RemoveUpdateRuleForAddon(const std::string
& addonID
, AddonUpdateRule updateRule
);
180 * \brief Retrieve all rule datasets from db and store them into map
181 * \param rulesMap target map
182 * \return true on success, false otherwise
184 bool GetAddonUpdateRules(std::map
<std::string
, std::vector
<AddonUpdateRule
>>& rulesMap
) const;
186 /*! \brief Store an addon's package filename and that file's hash for future verification
187 \param addonID id of the addon we're adding a package for
188 \param packageFileName filename of the package
189 \param hash MD5 checksum of the package
190 \return Whether or not the info successfully made it into the DB.
191 \sa GetPackageHash, RemovePackage
193 bool AddPackage(const std::string
& addonID
,
194 const std::string
& packageFileName
,
195 const std::string
& hash
);
196 /*! \brief Query the MD5 checksum of the given given addon's given package
197 \param addonID id of the addon we're who's package we're querying
198 \param packageFileName filename of the package
199 \param hash return the MD5 checksum of the package
200 \return Whether or not we found a hash for the given addon's given package
201 \sa AddPackage, RemovePackage
203 bool GetPackageHash(const std::string
& addonID
,
204 const std::string
& packageFileName
,
206 /*! \brief Remove a package's info from the database
207 \param packageFileName filename of the package
208 \return Whether or not we succeeded in removing the package
209 \sa AddPackage, GetPackageHash
211 bool RemovePackage(const std::string
& packageFileName
);
213 /*! Clear internal fields that shouldn't be kept around indefinitely */
214 void OnPostUnInstall(const std::string
& addonId
);
216 void SyncInstalled(const std::set
<std::string
>& ids
,
217 const std::set
<std::string
>& system
,
218 const std::set
<std::string
>& optional
);
220 bool SetLastUpdated(const std::string
& addonId
, const CDateTime
& dateTime
);
221 bool SetOrigin(const std::string
& addonId
, const std::string
& origin
);
222 bool SetLastUsed(const std::string
& addonId
, const CDateTime
& dateTime
);
224 void GetInstallData(const ADDON::AddonInfoPtr
& addon
);
226 /*! \brief Add dataset for a new installed addon to the database
227 * \param addon the addon to insert
228 * \param origin the origin it was installed from
229 * \return true on success, false otherwise
231 bool AddInstalledAddon(const std::shared_ptr
<CAddonInfo
>& addon
, const std::string
& origin
);
234 void CreateTables() override
;
235 void CreateAnalytics() override
;
236 void UpdateTables(int version
) override
;
237 int GetMinSchemaVersion() const override
;
238 int GetSchemaVersion() const override
;
239 const char *GetBaseDBName() const override
{ return "Addons"; }
241 bool GetAddon(int id
, ADDON::AddonPtr
& addon
);
242 void DeleteRepository(const std::string
& id
);
243 void DeleteRepositoryContents(const std::string
& id
);
244 int GetRepositoryId(const std::string
& addonId
);
247 }; // namespace ADDON