summaryrefslogtreecommitdiffstats
path: root/xbmc/pvr/guilib/PVRGUIActionsRecordings.cpp
blob: df23f38751ee41ebd12f8c2d93baa2aa6afb22f3 (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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*
 *  Copyright (C) 2016-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.
 */

#include "PVRGUIActionsRecordings.h"

#include "FileItem.h"
#include "ServiceBroker.h"
#include "Util.h"
#include "dialogs/GUIDialogBusy.h"
#include "dialogs/GUIDialogYesNo.h"
#include "filesystem/IDirectory.h"
#include "guilib/GUIComponent.h"
#include "guilib/GUIWindowManager.h"
#include "guilib/WindowIDs.h"
#include "messaging/helpers/DialogOKHelper.h"
#include "pvr/PVRItem.h"
#include "pvr/PVRManager.h"
#include "pvr/addons/PVRClient.h"
#include "pvr/addons/PVRClients.h"
#include "pvr/dialogs/GUIDialogPVRRecordingInfo.h"
#include "pvr/dialogs/GUIDialogPVRRecordingSettings.h"
#include "pvr/recordings/PVRRecording.h"
#include "settings/Settings.h"
#include "threads/IRunnable.h"
#include "utils/Variant.h"
#include "utils/log.h"

#include <memory>
#include <numeric>
#include <string>

using namespace PVR;
using namespace KODI::MESSAGING;

namespace
{
class AsyncRecordingAction : private IRunnable
{
public:
  bool Execute(const CFileItem& item);

protected:
  AsyncRecordingAction() = default;

private:
  // IRunnable implementation
  void Run() override;

  // the worker function
  virtual bool DoRun(const std::shared_ptr<CFileItem>& item) = 0;

  std::shared_ptr<CFileItem> m_item;
  bool m_bSuccess = false;
};

bool AsyncRecordingAction::Execute(const CFileItem& item)
{
  m_item = std::make_shared<CFileItem>(item);
  CGUIDialogBusy::Wait(this, 100, false);
  return m_bSuccess;
}

void AsyncRecordingAction::Run()
{
  m_bSuccess = DoRun(m_item);

  if (m_bSuccess)
    CServiceBroker::GetPVRManager().TriggerRecordingsUpdate();
}

class AsyncRenameRecording : public AsyncRecordingAction
{
public:
  explicit AsyncRenameRecording(const std::string& strNewName) : m_strNewName(strNewName) {}

private:
  bool DoRun(const std::shared_ptr<CFileItem>& item) override
  {
    if (item->IsUsablePVRRecording())
    {
      return item->GetPVRRecordingInfoTag()->Rename(m_strNewName);
    }
    else
    {
      CLog::LogF(LOGERROR, "Cannot rename item '{}': no valid recording tag", item->GetPath());
      return false;
    }
  }
  std::string m_strNewName;
};

class AsyncDeleteRecording : public AsyncRecordingAction
{
public:
  explicit AsyncDeleteRecording(bool bWatchedOnly = false) : m_bWatchedOnly(bWatchedOnly) {}

private:
  bool DoRun(const std::shared_ptr<CFileItem>& item) override
  {
    CFileItemList items;
    if (item->m_bIsFolder)
    {
      CUtil::GetRecursiveListing(item->GetPath(), items, "", XFILE::DIR_FLAG_NO_FILE_INFO);
    }
    else
    {
      items.Add(item);
    }

    return std::accumulate(
        items.cbegin(), items.cend(), true, [this](bool success, const auto& itemToDelete) {
          return (itemToDelete->IsPVRRecording() &&
                  (!m_bWatchedOnly || itemToDelete->GetPVRRecordingInfoTag()->GetPlayCount() > 0) &&
                  !itemToDelete->GetPVRRecordingInfoTag()->Delete())
                     ? false
                     : success;
        });
  }
  bool m_bWatchedOnly = false;
};

class AsyncEmptyRecordingsTrash : public AsyncRecordingAction
{
private:
  bool DoRun(const std::shared_ptr<CFileItem>& item) override
  {
    return CServiceBroker::GetPVRManager().Clients()->DeleteAllRecordingsFromTrash() ==
           PVR_ERROR_NO_ERROR;
  }
};

class AsyncUndeleteRecording : public AsyncRecordingAction
{
private:
  bool DoRun(const std::shared_ptr<CFileItem>& item) override
  {
    if (item->IsDeletedPVRRecording())
    {
      return item->GetPVRRecordingInfoTag()->Undelete();
    }
    else
    {
      CLog::LogF(LOGERROR, "Cannot undelete item '{}': no valid recording tag", item->GetPath());
      return false;
    }
  }
};

class AsyncSetRecordingPlayCount : public AsyncRecordingAction
{
private:
  bool DoRun(const std::shared_ptr<CFileItem>& item) override
  {
    const std::shared_ptr<CPVRClient> client = CServiceBroker::GetPVRManager().GetClient(*item);
    if (client)
    {
      const std::shared_ptr<CPVRRecording> recording = item->GetPVRRecordingInfoTag();
      return client->SetRecordingPlayCount(*recording, recording->GetLocalPlayCount()) ==
             PVR_ERROR_NO_ERROR;
    }
    return false;
  }
};

class AsyncSetRecordingLifetime : public AsyncRecordingAction
{
private:
  bool DoRun(const std::shared_ptr<CFileItem>& item) override
  {
    const std::shared_ptr<CPVRClient> client = CServiceBroker::GetPVRManager().GetClient(*item);
    if (client)
      return client->SetRecordingLifetime(*item->GetPVRRecordingInfoTag()) == PVR_ERROR_NO_ERROR;
    return false;
  }
};

} // unnamed namespace

bool CPVRGUIActionsRecordings::ShowRecordingInfo(const CFileItem& item) const
{
  if (!item.IsPVRRecording())
  {
    CLog::LogF(LOGERROR, "No recording!");
    return false;
  }

  CGUIDialogPVRRecordingInfo* pDlgInfo =
      CServiceBroker::GetGUI()->GetWindowManager().GetWindow<CGUIDialogPVRRecordingInfo>(
          WINDOW_DIALOG_PVR_RECORDING_INFO);
  if (!pDlgInfo)
  {
    CLog::LogF(LOGERROR, "Unable to get WINDOW_DIALOG_PVR_RECORDING_INFO!");
    return false;
  }

  pDlgInfo->SetRecording(item);
  pDlgInfo->Open();
  return true;
}

bool CPVRGUIActionsRecordings::EditRecording(const CFileItem& item) const
{
  const std::shared_ptr<CPVRRecording> recording = CPVRItem(item).GetRecording();
  if (!recording)
  {
    CLog::LogF(LOGERROR, "No recording!");
    return false;
  }

  std::shared_ptr<CPVRRecording> origRecording(new CPVRRecording);
  origRecording->Update(*recording,
                        *CServiceBroker::GetPVRManager().GetClient(recording->ClientID()));

  if (!ShowRecordingSettings(recording))
    return false;

  if (origRecording->m_strTitle != recording->m_strTitle)
  {
    if (!AsyncRenameRecording(recording->m_strTitle).Execute(item))
      CLog::LogF(LOGERROR, "Renaming recording failed!");
  }

  if (origRecording->GetLocalPlayCount() != recording->GetLocalPlayCount())
  {
    if (!AsyncSetRecordingPlayCount().Execute(item))
      CLog::LogF(LOGERROR, "Setting recording playcount failed!");
  }

  if (origRecording->LifeTime() != recording->LifeTime())
  {
    if (!AsyncSetRecordingLifetime().Execute(item))
      CLog::LogF(LOGERROR, "Setting recording lifetime failed!");
  }

  return true;
}

bool CPVRGUIActionsRecordings::CanEditRecording(const CFileItem& item) const
{
  return CGUIDialogPVRRecordingSettings::CanEditRecording(item);
}

bool CPVRGUIActionsRecordings::DeleteRecording(const CFileItem& item) const
{
  if ((!item.IsPVRRecording() && !item.m_bIsFolder) || item.IsParentFolder())
    return false;

  if (!ConfirmDeleteRecording(item))
    return false;

  if (!AsyncDeleteRecording().Execute(item))
  {
    HELPERS::ShowOKDialogText(
        CVariant{257},
        CVariant{
            19111}); // "Error", "PVR backend error. Check the log for more information about this message."
    return false;
  }

  return true;
}

bool CPVRGUIActionsRecordings::ConfirmDeleteRecording(const CFileItem& item) const
{
  return CGUIDialogYesNo::ShowAndGetInput(
      CVariant{122}, // "Confirm delete"
      item.m_bIsFolder
          ? CVariant{19113} // "Delete all recordings in this folder?"
          : item.GetPVRRecordingInfoTag()->IsDeleted()
                ? CVariant{19294}
                // "Remove this deleted recording from trash? This operation cannot be reverted."
                : CVariant{19112}, // "Delete this recording?"
      CVariant{""}, CVariant{item.GetLabel()});
}

bool CPVRGUIActionsRecordings::DeleteWatchedRecordings(const CFileItem& item) const
{
  if (!item.m_bIsFolder || item.IsParentFolder())
    return false;

  if (!ConfirmDeleteWatchedRecordings(item))
    return false;

  if (!AsyncDeleteRecording(true).Execute(item))
  {
    HELPERS::ShowOKDialogText(
        CVariant{257},
        CVariant{
            19111}); // "Error", "PVR backend error. Check the log for more information about this message."
    return false;
  }

  return true;
}

bool CPVRGUIActionsRecordings::ConfirmDeleteWatchedRecordings(const CFileItem& item) const
{
  return CGUIDialogYesNo::ShowAndGetInput(
      CVariant{122}, // "Confirm delete"
      CVariant{19328}, // "Delete all watched recordings in this folder?"
      CVariant{""}, CVariant{item.GetLabel()});
}

bool CPVRGUIActionsRecordings::DeleteAllRecordingsFromTrash() const
{
  if (!ConfirmDeleteAllRecordingsFromTrash())
    return false;

  if (!AsyncEmptyRecordingsTrash().Execute({}))
    return false;

  return true;
}

bool CPVRGUIActionsRecordings::ConfirmDeleteAllRecordingsFromTrash() const
{
  return CGUIDialogYesNo::ShowAndGetInput(
      CVariant{19292}, // "Delete all permanently"
      CVariant{
          19293}); // "Remove all deleted recordings from trash? This operation cannot be reverted."
}

bool CPVRGUIActionsRecordings::UndeleteRecording(const CFileItem& item) const
{
  if (!item.IsDeletedPVRRecording())
    return false;

  if (!AsyncUndeleteRecording().Execute(item))
  {
    HELPERS::ShowOKDialogText(
        CVariant{257},
        CVariant{
            19111}); // "Error", "PVR backend error. Check the log for more information about this message."
    return false;
  }

  return true;
}

bool CPVRGUIActionsRecordings::ShowRecordingSettings(
    const std::shared_ptr<CPVRRecording>& recording) const
{
  CGUIDialogPVRRecordingSettings* pDlgInfo =
      CServiceBroker::GetGUI()->GetWindowManager().GetWindow<CGUIDialogPVRRecordingSettings>(
          WINDOW_DIALOG_PVR_RECORDING_SETTING);
  if (!pDlgInfo)
  {
    CLog::LogF(LOGERROR, "Unable to get WINDOW_DIALOG_PVR_RECORDING_SETTING!");
    return false;
  }

  pDlgInfo->SetRecording(recording);
  pDlgInfo->Open();

  return pDlgInfo->IsConfirmed();
}