2 * Copyright (C) 2016-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.
9 #include "PVRGUIActionsDatabase.h"
12 #include "ServiceBroker.h"
13 #include "dialogs/GUIDialogProgress.h"
14 #include "dialogs/GUIDialogSelect.h"
15 #include "dialogs/GUIDialogYesNo.h"
16 #include "guilib/GUIComponent.h"
17 #include "guilib/GUIWindowManager.h"
18 #include "guilib/LocalizeStrings.h"
19 #include "guilib/WindowIDs.h"
20 #include "messaging/ApplicationMessenger.h"
21 #include "pvr/PVRDatabase.h"
22 #include "pvr/PVRManager.h"
23 #include "pvr/PVRPlaybackState.h"
24 #include "pvr/epg/EpgContainer.h"
25 #include "pvr/epg/EpgDatabase.h"
26 #include "pvr/guilib/PVRGUIActionsParentalControl.h"
27 #include "pvr/recordings/PVRRecordings.h"
28 #include "pvr/recordings/PVRRecordingsPath.h"
29 #include "utils/StringUtils.h"
30 #include "utils/Variant.h"
31 #include "utils/log.h"
32 #include "video/VideoDatabase.h"
41 class CPVRGUIDatabaseResetComponentsSelector
44 CPVRGUIDatabaseResetComponentsSelector() = default;
45 virtual ~CPVRGUIDatabaseResetComponentsSelector() = default;
49 CGUIDialogSelect
* pDlgSelect
=
50 CServiceBroker::GetGUI()->GetWindowManager().GetWindow
<CGUIDialogSelect
>(
51 WINDOW_DIALOG_SELECT
);
54 CLog::LogF(LOGERROR
, "Unable to get WINDOW_DIALOG_SELECT!");
58 CFileItemList options
;
60 const std::shared_ptr
<CFileItem
> itemAll
=
61 std::make_shared
<CFileItem
>(StringUtils::Format(g_localizeStrings
.Get(593))); // All
62 itemAll
->SetPath("all");
65 // if channels are cleared, groups, EPG data and providers must also be cleared
66 const std::shared_ptr
<CFileItem
> itemChannels
=
67 std::make_shared
<CFileItem
>(StringUtils::Format("{}, {}, {}, {}",
68 g_localizeStrings
.Get(19019), // Channels
69 g_localizeStrings
.Get(19146), // Groups
70 g_localizeStrings
.Get(19069), // Guide
71 g_localizeStrings
.Get(19334))); // Providers
72 itemChannels
->SetPath("channels");
73 itemChannels
->Select(true); // preselect this item in dialog
74 options
.Add(itemChannels
);
76 const std::shared_ptr
<CFileItem
> itemGroups
=
77 std::make_shared
<CFileItem
>(g_localizeStrings
.Get(19146)); // Groups
78 itemGroups
->SetPath("groups");
79 options
.Add(itemGroups
);
81 const std::shared_ptr
<CFileItem
> itemGuide
=
82 std::make_shared
<CFileItem
>(g_localizeStrings
.Get(19069)); // Guide
83 itemGuide
->SetPath("guide");
84 options
.Add(itemGuide
);
86 const std::shared_ptr
<CFileItem
> itemProviders
=
87 std::make_shared
<CFileItem
>(g_localizeStrings
.Get(19334)); // Providers
88 itemProviders
->SetPath("providers");
89 options
.Add(itemProviders
);
91 const std::shared_ptr
<CFileItem
> itemReminders
=
92 std::make_shared
<CFileItem
>(g_localizeStrings
.Get(19215)); // Reminders
93 itemReminders
->SetPath("reminders");
94 options
.Add(itemReminders
);
96 const std::shared_ptr
<CFileItem
> itemRecordings
=
97 std::make_shared
<CFileItem
>(g_localizeStrings
.Get(19017)); // Recordings
98 itemRecordings
->SetPath("recordings");
99 options
.Add(itemRecordings
);
101 const std::shared_ptr
<CFileItem
> itemClients
=
102 std::make_shared
<CFileItem
>(g_localizeStrings
.Get(24019)); // PVR clients
103 itemClients
->SetPath("clients");
104 options
.Add(itemClients
);
107 pDlgSelect
->SetHeading(CVariant
{g_localizeStrings
.Get(19185)}); // "Clear data"
108 pDlgSelect
->SetItems(options
);
109 pDlgSelect
->SetMultiSelection(true);
112 if (!pDlgSelect
->IsConfirmed())
115 for (int i
: pDlgSelect
->GetSelectedItems())
117 const std::string path
= options
.Get(i
)->GetPath();
119 m_bResetChannels
|= (path
== "channels" || path
== "all");
120 m_bResetGroups
|= (path
== "groups" || path
== "all");
121 m_bResetGuide
|= (path
== "guide" || path
== "all");
122 m_bResetProviders
|= (path
== "providers" || path
== "all");
123 m_bResetReminders
|= (path
== "reminders" || path
== "all");
124 m_bResetRecordings
|= (path
== "recordings" || path
== "all");
125 m_bResetClients
|= (path
== "clients" || path
== "all");
128 m_bResetGroups
|= m_bResetChannels
;
129 m_bResetGuide
|= m_bResetChannels
;
130 m_bResetProviders
|= m_bResetChannels
;
132 return (m_bResetChannels
|| m_bResetGroups
|| m_bResetGuide
|| m_bResetProviders
||
133 m_bResetReminders
|| m_bResetRecordings
|| m_bResetClients
);
136 bool IsResetChannelsSelected() const { return m_bResetChannels
; }
137 bool IsResetGroupsSelected() const { return m_bResetGroups
; }
138 bool IsResetGuideSelected() const { return m_bResetGuide
; }
139 bool IsResetProvidersSelected() const { return m_bResetProviders
; }
140 bool IsResetRemindersSelected() const { return m_bResetReminders
; }
141 bool IsResetRecordingsSelected() const { return m_bResetRecordings
; }
142 bool IsResetClientsSelected() const { return m_bResetClients
; }
145 bool m_bResetChannels
= false;
146 bool m_bResetGroups
= false;
147 bool m_bResetGuide
= false;
148 bool m_bResetProviders
= false;
149 bool m_bResetReminders
= false;
150 bool m_bResetRecordings
= false;
151 bool m_bResetClients
= false;
154 } // unnamed namespace
156 bool CPVRGUIActionsDatabase::ResetDatabase(bool bResetEPGOnly
)
158 CGUIDialogProgress
* pDlgProgress
=
159 CServiceBroker::GetGUI()->GetWindowManager().GetWindow
<CGUIDialogProgress
>(
160 WINDOW_DIALOG_PROGRESS
);
163 CLog::LogF(LOGERROR
, "Unable to get WINDOW_DIALOG_PROGRESS!");
167 bool bResetChannels
= false;
168 bool bResetGroups
= false;
169 bool bResetGuide
= false;
170 bool bResetProviders
= false;
171 bool bResetReminders
= false;
172 bool bResetRecordings
= false;
173 bool bResetClients
= false;
177 if (!CGUIDialogYesNo::ShowAndGetInput(
178 CVariant
{19098}, // "Warning!"
179 CVariant
{19188})) // "All guide data will be cleared. Are you sure?"
186 if (CServiceBroker::GetPVRManager().Get
<PVR::GUI::Parental
>().CheckParentalPIN() !=
187 ParentalCheckResult::SUCCESS
)
190 CPVRGUIDatabaseResetComponentsSelector selector
;
191 if (!selector
.Select())
194 if (!CGUIDialogYesNo::ShowAndGetInput(
195 CVariant
{19098}, // "Warning!"
196 CVariant
{19186})) // "All selected data will be cleared. ... Are you sure?"
199 bResetChannels
= selector
.IsResetChannelsSelected();
200 bResetGroups
= selector
.IsResetGroupsSelected();
201 bResetGuide
= selector
.IsResetGuideSelected();
202 bResetProviders
= selector
.IsResetProvidersSelected();
203 bResetReminders
= selector
.IsResetRemindersSelected();
204 bResetRecordings
= selector
.IsResetRecordingsSelected();
205 bResetClients
= selector
.IsResetClientsSelected();
208 CDateTime::ResetTimezoneBias();
210 CLog::LogFC(LOGDEBUG
, LOGPVR
, "PVR clearing {} database", bResetEPGOnly
? "EPG" : "PVR and EPG");
212 pDlgProgress
->SetHeading(CVariant
{313}); // "Cleaning database"
213 pDlgProgress
->SetLine(0, CVariant
{g_localizeStrings
.Get(19187)}); // "Clearing all related data."
214 pDlgProgress
->SetLine(1, CVariant
{""});
215 pDlgProgress
->SetLine(2, CVariant
{""});
217 pDlgProgress
->Open();
218 pDlgProgress
->Progress();
220 if (CServiceBroker::GetPVRManager().PlaybackState()->IsPlaying())
222 CLog::Log(LOGINFO
, "PVR is stopping playback for {} database reset",
223 bResetEPGOnly
? "EPG" : "PVR and EPG");
224 CServiceBroker::GetAppMessenger()->SendMsg(TMSG_MEDIA_STOP
);
227 const std::shared_ptr
<CPVRDatabase
> pvrDatabase(CServiceBroker::GetPVRManager().GetTVDatabase());
228 const std::shared_ptr
<CPVREpgDatabase
> epgDatabase(
229 CServiceBroker::GetPVRManager().EpgContainer().GetEpgDatabase());
231 // increase db open refcounts, so they don't get closed during following pvr manager shutdown
235 // stop pvr manager; close both pvr and epg databases
236 CServiceBroker::GetPVRManager().Stop();
238 const int iProgressStepPercentage
=
239 100 / ((2 * bResetChannels
) + bResetGroups
+ bResetGuide
+ bResetProviders
+ bResetReminders
+
240 bResetRecordings
+ bResetClients
+ 1);
241 int iProgressStepsDone
= 0;
245 pDlgProgress
->SetPercentage(iProgressStepPercentage
* ++iProgressStepsDone
);
246 pDlgProgress
->Progress();
248 // delete all providers
249 pvrDatabase
->DeleteProviders();
254 pDlgProgress
->SetPercentage(iProgressStepPercentage
* ++iProgressStepsDone
);
255 pDlgProgress
->Progress();
257 // reset channel's EPG pointers
258 pvrDatabase
->ResetEPG();
260 // delete all entries from the EPG database
261 epgDatabase
->DeleteEpg();
266 pDlgProgress
->SetPercentage(iProgressStepPercentage
* ++iProgressStepsDone
);
267 pDlgProgress
->Progress();
269 // delete all channel groups (including data only available locally, like user defined groups)
270 pvrDatabase
->DeleteChannelGroups();
275 pDlgProgress
->SetPercentage(iProgressStepPercentage
* ++iProgressStepsDone
);
276 pDlgProgress
->Progress();
278 // delete all channels (including data only available locally, like user set icons)
279 pvrDatabase
->DeleteChannels();
284 pDlgProgress
->SetPercentage(iProgressStepPercentage
* ++iProgressStepsDone
);
285 pDlgProgress
->Progress();
287 // delete all timers data (e.g. all reminders, which are only stored locally)
288 pvrDatabase
->DeleteTimers();
293 pDlgProgress
->SetPercentage(iProgressStepPercentage
* ++iProgressStepsDone
);
294 pDlgProgress
->Progress();
296 // delete all clients data (e.g priorities, which are only stored locally)
297 pvrDatabase
->DeleteClients();
300 if (bResetChannels
|| bResetRecordings
)
302 CVideoDatabase videoDatabase
;
304 if (videoDatabase
.Open())
308 pDlgProgress
->SetPercentage(iProgressStepPercentage
* ++iProgressStepsDone
);
309 pDlgProgress
->Progress();
311 // delete all channel's entries (e.g. settings, bookmarks, stream details)
312 videoDatabase
.EraseAllForPath("pvr://channels/");
315 if (bResetRecordings
)
317 pDlgProgress
->SetPercentage(iProgressStepPercentage
* ++iProgressStepsDone
);
318 pDlgProgress
->Progress();
320 // delete all recording's entries (e.g. settings, bookmarks, stream details)
321 videoDatabase
.EraseAllForPath(CPVRRecordingsPath::PATH_RECORDINGS
);
324 videoDatabase
.Close();
328 // decrease db open refcounts; this actually closes dbs because refcounts drops to zero
329 pvrDatabase
->Close();
330 epgDatabase
->Close();
332 CLog::LogFC(LOGDEBUG
, LOGPVR
, "{} database cleared", bResetEPGOnly
? "EPG" : "PVR and EPG");
334 CLog::Log(LOGINFO
, "Restarting the PVR Manager after {} database reset",
335 bResetEPGOnly
? "EPG" : "PVR and EPG");
336 CServiceBroker::GetPVRManager().Start();
338 pDlgProgress
->SetPercentage(100);
339 pDlgProgress
->Close();