-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
/
RepositoryUpdater.cpp
224 lines (191 loc) · 6.56 KB
/
RepositoryUpdater.cpp
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
* Copyright (C) 2015 Team Kodi
* http://kodi.tv
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This Program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XBMC; see the file COPYING. If not, see
* <http://www.gnu.org/licenses/>.
*
*/
#include "RepositoryUpdater.h"
#include "Application.h"
#include "ServiceBroker.h"
#include "addons/AddonInstaller.h"
#include "addons/AddonManager.h"
#include "addons/AddonSystemSettings.h"
#include "dialogs/GUIDialogExtendedProgressBar.h"
#include "dialogs/GUIDialogKaiToast.h"
#include "events/AddonManagementEvent.h"
#include "events/EventLog.h"
#include "guilib/GUIWindowManager.h"
#include "settings/Settings.h"
#include "threads/SingleLock.h"
#include "utils/JobManager.h"
#include "utils/log.h"
#include <algorithm>
#include <iterator>
#include <vector>
namespace ADDON
{
CRepositoryUpdater::CRepositoryUpdater(CAddonMgr& addonMgr) :
m_timer(this),
m_doneEvent(true),
m_addonMgr(addonMgr)
{}
void CRepositoryUpdater::Start()
{
CServiceBroker::GetAddonMgr().Events().Subscribe(this, &CRepositoryUpdater::OnEvent);
ScheduleUpdate();
}
CRepositoryUpdater::~CRepositoryUpdater()
{
CServiceBroker::GetAddonMgr().Events().Unsubscribe(this);
}
void CRepositoryUpdater::OnEvent(const ADDON::AddonEvent& event)
{
if (auto enableEvent = dynamic_cast<const AddonEvents::Enabled*>(&event))
{
AddonPtr addon;
if (CAddonMgr::GetInstance().GetAddon(enableEvent->id, addon, ADDON_REPOSITORY))
ScheduleUpdate();
}
}
void CRepositoryUpdater::OnJobComplete(unsigned int jobID, bool success, CJob* job)
{
CSingleLock lock(m_criticalSection);
m_jobs.erase(std::find(m_jobs.begin(), m_jobs.end(), job));
if (m_jobs.empty())
{
CLog::Log(LOGDEBUG, "CRepositoryUpdater: done.");
m_doneEvent.Set();
VECADDONS updates = CAddonMgr::GetInstance().GetAvailableUpdates();
if (CServiceBroker::GetSettings().GetInt(CSettings::SETTING_ADDONS_AUTOUPDATES) == AUTO_UPDATES_NOTIFY)
{
if (!updates.empty())
{
if (updates.size() == 1)
CGUIDialogKaiToast::QueueNotification(
updates[0]->Icon(), updates[0]->Name(), g_localizeStrings.Get(24068),
TOAST_DISPLAY_TIME, false, TOAST_DISPLAY_TIME);
else
CGUIDialogKaiToast::QueueNotification(
"", g_localizeStrings.Get(24001), g_localizeStrings.Get(24061),
TOAST_DISPLAY_TIME, false, TOAST_DISPLAY_TIME);
for (const auto &addon : updates)
CEventLog::GetInstance().Add(EventPtr(new CAddonManagementEvent(addon, 24068)));
}
}
if (CServiceBroker::GetSettings().GetInt(CSettings::SETTING_ADDONS_AUTOUPDATES) == AUTO_UPDATES_ON)
{
CAddonInstaller::GetInstance().InstallUpdates();
}
ScheduleUpdate();
m_events.Publish(RepositoryUpdated{});
}
}
bool CRepositoryUpdater::CheckForUpdates(bool showProgress)
{
VECADDONS addons;
if (CAddonMgr::GetInstance().GetAddons(addons, ADDON_REPOSITORY) && !addons.empty())
{
CSingleLock lock(m_criticalSection);
for (const auto& addon : addons)
CheckForUpdates(std::static_pointer_cast<ADDON::CRepository>(addon), showProgress);
return true;
}
return false;
}
static void SetProgressIndicator(CRepositoryUpdateJob* job)
{
auto dialog = g_windowManager.GetWindow<CGUIDialogExtendedProgressBar>(WINDOW_DIALOG_EXT_PROGRESS);
if (dialog)
job->SetProgressIndicators(dialog->GetHandle(g_localizeStrings.Get(24092)), nullptr);
}
void CRepositoryUpdater::CheckForUpdates(const ADDON::RepositoryPtr& repo, bool showProgress)
{
CSingleLock lock(m_criticalSection);
auto job = std::find_if(m_jobs.begin(), m_jobs.end(),
[&](CRepositoryUpdateJob* job){ return job->GetAddon()->ID() == repo->ID(); });
if (job == m_jobs.end())
{
auto* job = new CRepositoryUpdateJob(repo);
m_jobs.push_back(job);
m_doneEvent.Reset();
if (showProgress)
SetProgressIndicator(job);
CJobManager::GetInstance().AddJob(job, this, CJob::PRIORITY_LOW);
}
else
{
if (showProgress && !(*job)->HasProgressIndicator())
SetProgressIndicator(*job);
}
}
void CRepositoryUpdater::Await()
{
m_doneEvent.Wait();
}
void CRepositoryUpdater::OnTimeout()
{
//workaround
if (g_windowManager.GetActiveWindow() == WINDOW_FULLSCREEN_VIDEO ||
g_windowManager.GetActiveWindow() == WINDOW_SLIDESHOW)
{
CLog::Log(LOGDEBUG,"CRepositoryUpdater: busy playing. postponing scheduled update");
m_timer.RestartAsync(2 * 60 * 1000);
return;
}
CLog::Log(LOGDEBUG,"CRepositoryUpdater: running scheduled update");
CheckForUpdates();
}
void CRepositoryUpdater::OnSettingChanged(std::shared_ptr<const CSetting> setting)
{
if (setting->GetId() == CSettings::SETTING_ADDONS_AUTOUPDATES)
ScheduleUpdate();
}
CDateTime CRepositoryUpdater::LastUpdated() const
{
VECADDONS repos;
if (!CAddonMgr::GetInstance().GetAddons(repos, ADDON_REPOSITORY) || repos.empty())
return CDateTime();
CAddonDatabase db;
db.Open();
std::vector<CDateTime> updateTimes;
std::transform(repos.begin(), repos.end(), std::back_inserter(updateTimes),
[&](const AddonPtr& repo)
{
auto lastCheck = db.LastChecked(repo->ID());
if (lastCheck.first.IsValid() && lastCheck.second == repo->Version())
return lastCheck.first;
return CDateTime();
});
return *std::min_element(updateTimes.begin(), updateTimes.end());
}
void CRepositoryUpdater::ScheduleUpdate()
{
const CDateTimeSpan interval(0, 24, 0, 0);
CSingleLock lock(m_criticalSection);
m_timer.Stop(true);
if (CServiceBroker::GetSettings().GetInt(CSettings::SETTING_ADDONS_AUTOUPDATES) == AUTO_UPDATES_NEVER)
return;
if (!CAddonMgr::GetInstance().HasAddons(ADDON_REPOSITORY))
return;
auto prev = LastUpdated();
auto next = std::max(CDateTime::GetCurrentDateTime(), prev + interval);
int delta = std::max(1, (next - CDateTime::GetCurrentDateTime()).GetSecondsTotal() * 1000);
CLog::Log(LOGDEBUG,"CRepositoryUpdater: previous update at %s, next at %s",
prev.GetAsLocalizedDateTime().c_str(), next.GetAsLocalizedDateTime().c_str());
if (!m_timer.Start(delta))
CLog::Log(LOGERROR,"CRepositoryUpdater: failed to start timer");
}
}