summaryrefslogtreecommitdiffstats
path: root/xbmc/pvr/providers/PVRProviders.h
blob: 8196d3b6d220a0a0a7940ec12c3d1a17ae2b6526 (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
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
 *  Copyright (C) 2012-2018 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.
 */

#pragma once

#include "threads/CriticalSection.h"

#include <memory>
#include <vector>

namespace PVR
{
class CPVRClient;
class CPVRProvider;

enum class ProviderUpdateMode;

class CPVRProvidersContainer
{
public:
  /*!
     * @brief Add a provider to this container or update the provider if already present in this container.
     * @param The provider
     * @return True, if the update was successful. False, otherwise.
     */
  bool UpdateFromClient(const std::shared_ptr<CPVRProvider>& provider);

  /*!
     * @brief Get the provider denoted by given client id and unique client provider id.
     * @param iClientId The client id.
     * @param iUniqueId The client provider id.
     * @return the provider if found, null otherwise.
     */
  std::shared_ptr<CPVRProvider> GetByClient(int iClientId, int iUniqueId) const;

  /*!
     * Get all providers in this container
     * @return The list of all providers
     */
  std::vector<std::shared_ptr<CPVRProvider>> GetProvidersList() const;

  /*!
     * Get the number of providers in this container
     * @return The total number of providers
     */
  std::size_t GetNumProviders() const;

protected:
  void InsertEntry(const std::shared_ptr<CPVRProvider>& newProvider, ProviderUpdateMode updateMode);

  mutable CCriticalSection m_critSection;
  int m_iLastId = 0;
  std::vector<std::shared_ptr<CPVRProvider>> m_providers;
};

class CPVRProviders : public CPVRProvidersContainer
{
public:
  CPVRProviders() = default;
  ~CPVRProviders() = default;

  /**
   * @brief Update all providers from PVR database and from given clients.
   * @param clients The PVR clients data should be loaded for. Leave empty for all clients.
   * @return True on success, false otherwise.
     */
  bool Update(const std::vector<std::shared_ptr<CPVRClient>>& clients);

  /**
     * @brief unload all providers.
     */
  void Unload();

  /**
   * @brief Update data with providers from the given clients, sync with local data.
   * @param clients The clients to fetch data from. Leave empty to fetch data from all created clients.
   * @return True on success, false otherwise.
     */
  bool UpdateFromClients(const std::vector<std::shared_ptr<CPVRClient>>& clients);

  /**
   * @brief Load all local providers from PVR database.
   * @param clients The PVR clients data should be loaded for. Leave empty for all clients.
   * @return True on success, false otherwise.
     */
  bool LoadFromDatabase(const std::vector<std::shared_ptr<CPVRClient>>& clients);

  /*!
     * @brief Check if the entry exists in the container, if it does update it otherwise add it
     * @param newProvider The provider entry to update/add in/to the container
     * @param updateMode update as Client (respect User set values) or DB (update all values)
     * @return The provider if updated or added, otherwise an empty object (nullptr)
     */
  std::shared_ptr<CPVRProvider> CheckAndAddEntry(const std::shared_ptr<CPVRProvider>& newProvider,
                                                 ProviderUpdateMode updateMode);

  /*!
     * @brief Check if the entry exists in the container, if it does update it and persist
     *        it in the DB otherwise add it and persist it in the DB.
     * @param newProvider The provider entry to update/add in/to the container and DB
     * @param updateMode update as Client (respect User set values) or DB (update all values)
     * @return The provider if updated or added, otherwise an empty object (nullptr)
     */
  std::shared_ptr<CPVRProvider> CheckAndPersistEntry(
      const std::shared_ptr<CPVRProvider>& newProvider, ProviderUpdateMode updateMode);

  /**
     * @brief Persist user changes to the current state of the providers in the DB.
     */
  bool PersistUserChanges(const std::vector<std::shared_ptr<CPVRProvider>>& providers);

  /*!
     * @brief Get a provider given it's database ID
     * @param iProviderId The ID to find
     * @return The provider, or an empty one when not found
     */
  std::shared_ptr<CPVRProvider> GetById(int iProviderId) const;

  /*!
    * @brief Erase stale texture db entries and image files.
    * @return number of cleaned up images.
    */
  int CleanupCachedImages();

private:
  void RemoveEntry(const std::shared_ptr<CPVRProvider>& provider);
  bool UpdateDefaultEntries(const CPVRProvidersContainer& newProviders);
  bool UpdateClientEntries(const CPVRProvidersContainer& newProviders,
                           const std::vector<int>& failedClients,
                           const std::vector<int>& disabledClients);

  bool m_bIsUpdating = false;
};
} // namespace PVR