summaryrefslogtreecommitdiffstats
path: root/xbmc/pvr/windows/GUIWindowPVRSearch.cpp
blob: f0cd3fedce2a5e9375d1a10075eee0d28f3f9032 (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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
/*
 *  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.
 */

#include "GUIWindowPVRSearch.h"

#include "FileItem.h"
#include "ServiceBroker.h"
#include "dialogs/GUIDialogBusy.h"
#include "dialogs/GUIDialogYesNo.h"
#include "guilib/GUIComponent.h"
#include "guilib/GUIMessage.h"
#include "guilib/GUIWindowManager.h"
#include "guilib/LocalizeStrings.h"
#include "input/actions/Action.h"
#include "input/actions/ActionIDs.h"
#include "messaging/helpers/DialogOKHelper.h"
#include "pvr/PVRItem.h"
#include "pvr/PVRManager.h"
#include "pvr/dialogs/GUIDialogPVRGuideSearch.h"
#include "pvr/epg/Epg.h"
#include "pvr/epg/EpgContainer.h"
#include "pvr/epg/EpgInfoTag.h"
#include "pvr/epg/EpgSearchFilter.h"
#include "pvr/epg/EpgSearchPath.h"
#include "pvr/guilib/PVRGUIActionsEPG.h"
#include "pvr/guilib/PVRGUIActionsTimers.h"
#include "pvr/recordings/PVRRecording.h"
#include "threads/IRunnable.h"
#include "utils/StringUtils.h"
#include "utils/URIUtils.h"
#include "utils/Variant.h"

#include <algorithm>
#include <memory>
#include <vector>

using namespace PVR;
using namespace KODI::MESSAGING;

namespace
{
class AsyncSearchAction : private IRunnable
{
public:
  AsyncSearchAction() = delete;
  AsyncSearchAction(CFileItemList* items, CPVREpgSearchFilter* filter)
    : m_items(items), m_filter(filter)
  {
  }
  bool Execute();

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

  CFileItemList* m_items;
  CPVREpgSearchFilter* m_filter;
};

bool AsyncSearchAction::Execute()
{
  CGUIDialogBusy::Wait(this, 100, false);
  return true;
}

void AsyncSearchAction::Run()
{
  std::vector<std::shared_ptr<CPVREpgInfoTag>> results =
      CServiceBroker::GetPVRManager().EpgContainer().GetTags(m_filter->GetEpgSearchData());
  m_filter->SetEpgSearchDataFiltered();

  // Tags can still contain false positives, for search criteria that cannot be handled via
  // database. So, run extended search filters on what we got from the database.
  for (auto it = results.begin(); it != results.end();)
  {
    it = results.erase(std::remove_if(results.begin(), results.end(),
                                      [this](const std::shared_ptr<CPVREpgInfoTag>& entry) {
                                        return !m_filter->FilterEntry(entry);
                                      }),
                       results.end());
  }

  if (m_filter->ShouldRemoveDuplicates())
    m_filter->RemoveDuplicates(results);

  m_filter->SetLastExecutedDateTime(CDateTime::GetUTCDateTime());

  for (const auto& tag : results)
  {
    m_items->Add(std::make_shared<CFileItem>(tag));
  }
}
} // unnamed namespace

CGUIWindowPVRSearchBase::CGUIWindowPVRSearchBase(bool bRadio, int id, const std::string& xmlFile)
  : CGUIWindowPVRBase(bRadio, id, xmlFile), m_bSearchConfirmed(false)
{
}

CGUIWindowPVRSearchBase::~CGUIWindowPVRSearchBase()
{
}

void CGUIWindowPVRSearchBase::GetContextButtons(int itemNumber, CContextButtons& buttons)
{
  if (itemNumber < 0 || itemNumber >= m_vecItems->Size())
    return;

  const CPVREpgSearchPath path(m_vecItems->GetPath());
  const bool bIsSavedSearchesRoot = (path.IsValid() && path.IsSavedSearchesRoot());
  if (!bIsSavedSearchesRoot)
    buttons.Add(CONTEXT_BUTTON_CLEAR, 19232); // "Clear search results"

  CGUIWindowPVRBase::GetContextButtons(itemNumber, buttons);
}

bool CGUIWindowPVRSearchBase::OnContextButton(int itemNumber, CONTEXT_BUTTON button)
{
  if (itemNumber < 0 || itemNumber >= m_vecItems->Size())
    return false;
  CFileItemPtr pItem = m_vecItems->Get(itemNumber);

  return OnContextButtonClear(pItem.get(), button) ||
         CGUIMediaWindow::OnContextButton(itemNumber, button);
}

void CGUIWindowPVRSearchBase::SetItemToSearch(const CFileItem& item)
{
  if (item.HasEPGSearchFilter())
  {
    SetSearchFilter(item.GetEPGSearchFilter());
  }
  else if (item.IsUsablePVRRecording())
  {
    SetSearchFilter(std::make_shared<CPVREpgSearchFilter>(m_bRadio));
    m_searchfilter->SetSearchPhrase(item.GetPVRRecordingInfoTag()->m_strTitle);
  }
  else
  {
    SetSearchFilter(std::make_shared<CPVREpgSearchFilter>(m_bRadio));

    const std::shared_ptr<CPVREpgInfoTag> epgTag(CPVRItem(item).GetEpgInfoTag());
    if (epgTag && !CServiceBroker::GetPVRManager().IsParentalLocked(epgTag))
      m_searchfilter->SetSearchPhrase(epgTag->Title());
  }

  ExecuteSearch();
}

void CGUIWindowPVRSearchBase::OnPrepareFileItems(CFileItemList& items)
{
  if (m_bSearchConfirmed)
  {
    items.Clear();
  }

  if (items.IsEmpty())
  {
    auto item = std::make_shared<CFileItem>(CPVREpgSearchPath::PATH_SEARCH_DIALOG, false);
    item->SetLabel(g_localizeStrings.Get(
        m_searchfilter == nullptr ? 19335 : 19336)); // "New search..." / "Edit search..."
    item->SetLabelPreformatted(true);
    item->SetSpecialSort(SortSpecialOnTop);
    item->SetArt("icon", "DefaultPVRSearch.png");
    items.Add(item);

    item = std::make_shared<CFileItem>(m_bRadio ? CPVREpgSearchPath::PATH_RADIO_SAVEDSEARCHES
                                                : CPVREpgSearchPath::PATH_TV_SAVEDSEARCHES,
                                       true);
    item->SetLabel(g_localizeStrings.Get(19337)); // "Saved searches"
    item->SetLabelPreformatted(true);
    item->SetSpecialSort(SortSpecialOnTop);
    item->SetArt("icon", "DefaultFolder.png");
    items.Add(item);
  }

  if (m_bSearchConfirmed)
  {
    const int itemCount = items.GetObjectCount();

    AsyncSearchAction(&items, m_searchfilter.get()).Execute();

    if (items.GetObjectCount() == itemCount)
    {
      HELPERS::ShowOKDialogText(CVariant{284}, // "No results found"
                                m_searchfilter->GetSearchTerm());
    }
  }
}

bool CGUIWindowPVRSearchBase::OnAction(const CAction& action)
{
  if (action.GetID() == ACTION_PARENT_DIR || action.GetID() == ACTION_NAV_BACK)
  {
    const CPVREpgSearchPath path(m_vecItems->GetPath());
    if (path.IsValid() && path.IsSavedSearchesRoot())
    {
      // Go to root dir and show previous search results if any
      m_bSearchConfirmed = (m_searchfilter != nullptr);
      GoParentFolder();
      return true;
    }
  }
  return CGUIWindowPVRBase::OnAction(action);
}

bool CGUIWindowPVRSearchBase::OnMessage(CGUIMessage& message)
{
  if (message.GetMessage() == GUI_MSG_CLICKED)
  {
    if (message.GetSenderId() == m_viewControl.GetCurrentControl())
    {
      int iItem = m_viewControl.GetSelectedItem();
      if (iItem >= 0 && iItem < m_vecItems->Size())
      {
        CFileItemPtr pItem = m_vecItems->Get(iItem);

        /* process actions */
        switch (message.GetParam1())
        {
          case ACTION_SHOW_INFO:
          case ACTION_SELECT_ITEM:
          case ACTION_MOUSE_LEFT_CLICK:
          {
            const CPVREpgSearchPath path(pItem->GetPath());
            const bool bIsSavedSearch = (path.IsValid() && path.IsSavedSearch());
            const bool bIsSavedSearchesRoot = (path.IsValid() && path.IsSavedSearchesRoot());

            if (message.GetParam1() != ACTION_SHOW_INFO)
            {
              if (pItem->IsParentFolder())
              {
                // Go to root dir and show previous search results if any
                m_bSearchConfirmed = (m_searchfilter != nullptr);
                break; // handled by base class
              }

              if (bIsSavedSearchesRoot)
              {
                // List saved searches
                m_bSearchConfirmed = false;
                break; // handled by base class
              }

              if (bIsSavedSearch)
              {
                // Execute selected saved search
                SetSearchFilter(pItem->GetEPGSearchFilter());
                ExecuteSearch();
                return true;
              }
            }

            if (bIsSavedSearch)
            {
              OpenDialogSearch(*pItem);
            }
            else if (pItem->GetPath() == CPVREpgSearchPath::PATH_SEARCH_DIALOG)
            {
              OpenDialogSearch(m_searchfilter);
            }
            else
            {
              CServiceBroker::GetPVRManager().Get<PVR::GUI::EPG>().ShowEPGInfo(*pItem);
            }
            return true;
          }

          case ACTION_CONTEXT_MENU:
          case ACTION_MOUSE_RIGHT_CLICK:
            OnPopupMenu(iItem);
            return true;

          case ACTION_RECORD:
            CServiceBroker::GetPVRManager().Get<PVR::GUI::Timers>().ToggleTimer(*pItem);
            return true;
        }
      }
    }
  }
  else if (message.GetMessage() == GUI_MSG_REFRESH_LIST)
  {
    if (static_cast<PVREvent>(message.GetParam1()) == PVREvent::SavedSearchesInvalidated)
    {
      Refresh(true);

      // Refresh triggered by deleted saved search?
      if (m_searchfilter)
      {
        const CPVREpgSearchPath path(m_vecItems->GetPath());
        const bool bIsSavedSearchesRoot = (path.IsValid() && path.IsSavedSearchesRoot());
        if (bIsSavedSearchesRoot)
        {
          const std::string filterPath = m_searchfilter->GetPath();
          bool bFound = false;
          for (const auto& item : *m_vecItems)
          {
            const auto filter = item->GetEPGSearchFilter();
            if (filter && filter->GetPath() == filterPath)
            {
              bFound = true;
              break;
            }
          }
          if (!bFound)
            SetSearchFilter(nullptr);
        }
      }
    }
  }
  else if (message.GetMessage() == GUI_MSG_WINDOW_INIT)
  {
    const CPVREpgSearchPath path(message.GetStringParam(0));
    if (path.IsValid() && path.IsSavedSearch())
    {
      const std::shared_ptr<CPVREpgSearchFilter> filter =
          CServiceBroker::GetPVRManager().EpgContainer().GetSavedSearchById(path.IsRadio(),
                                                                            path.GetId());
      if (filter)
      {
        SetSearchFilter(filter);
        m_bSearchConfirmed = true;
      }
    }
  }

  return CGUIWindowPVRBase::OnMessage(message);
}

bool CGUIWindowPVRSearchBase::Update(const std::string& strDirectory,
                                     bool updateFilterPath /* = true */)
{
  if (m_vecItems->GetObjectCount() > 0)
  {
    const CPVREpgSearchPath path(m_vecItems->GetPath());
    if (path.IsValid() && path.IsSavedSearchesRoot())
    {
      const std::string oldPath = m_vecItems->GetPath();

      const bool bReturn = CGUIWindowPVRBase::Update(strDirectory);

      if (bReturn && oldPath == m_vecItems->GetPath() && m_vecItems->GetObjectCount() == 0)
      {
        // Go to parent folder if we're in a subdir and for instance just deleted the last item
        GoParentFolder();
      }
      return bReturn;
    }
  }
  return CGUIWindowPVRBase::Update(strDirectory);
}

void CGUIWindowPVRSearchBase::UpdateButtons()
{
  CGUIWindowPVRBase::UpdateButtons();

  bool bSavedSearchesRoot = false;
  std::string header;
  const CPVREpgSearchPath path(m_vecItems->GetPath());
  if (path.IsValid() && path.IsSavedSearchesRoot())
  {
    bSavedSearchesRoot = true;
    header = g_localizeStrings.Get(19337); // "Saved searches"
  }

  if (header.empty() && m_searchfilter)
  {
    header = m_searchfilter->GetTitle();
    if (header.empty())
    {
      header = m_searchfilter->GetSearchTerm();
      StringUtils::Trim(header, "\"");
    }
  }
  SET_CONTROL_LABEL(CONTROL_LABEL_HEADER1, header);

  if (!bSavedSearchesRoot && m_searchfilter && m_searchfilter->IsChanged())
    SET_CONTROL_LABEL(CONTROL_LABEL_HEADER2, g_localizeStrings.Get(19342)); // "[not saved]"
  else if (!bSavedSearchesRoot && m_searchfilter)
    SET_CONTROL_LABEL(CONTROL_LABEL_HEADER2, g_localizeStrings.Get(19343)); // "[saved]"
  else
    SET_CONTROL_LABEL(CONTROL_LABEL_HEADER2, "");
}

bool CGUIWindowPVRSearchBase::OnContextButtonClear(CFileItem* item, CONTEXT_BUTTON button)
{
  bool bReturn = false;

  if (button == CONTEXT_BUTTON_CLEAR)
  {
    bReturn = true;

    m_bSearchConfirmed = false;
    SetSearchFilter(nullptr);

    Refresh(true);
  }

  return bReturn;
}

CGUIDialogPVRGuideSearch::Result CGUIWindowPVRSearchBase::OpenDialogSearch(const CFileItem& item)
{
  const auto searchFilter = item.GetEPGSearchFilter();
  if (!searchFilter)
    return CGUIDialogPVRGuideSearch::Result::CANCEL;

  return OpenDialogSearch(searchFilter);
}

CGUIDialogPVRGuideSearch::Result CGUIWindowPVRSearchBase::OpenDialogSearch(
    const std::shared_ptr<CPVREpgSearchFilter>& searchFilter)
{
  CGUIDialogPVRGuideSearch* dlgSearch =
      CServiceBroker::GetGUI()->GetWindowManager().GetWindow<CGUIDialogPVRGuideSearch>(
          WINDOW_DIALOG_PVR_GUIDE_SEARCH);

  if (!dlgSearch)
    return CGUIDialogPVRGuideSearch::Result::CANCEL;

  const std::shared_ptr<CPVREpgSearchFilter> tmpSearchFilter =
      searchFilter != nullptr ? std::make_shared<CPVREpgSearchFilter>(*searchFilter)
                              : std::make_shared<CPVREpgSearchFilter>(m_bRadio);

  dlgSearch->SetFilterData(tmpSearchFilter);

  /* Open dialog window */
  dlgSearch->Open();

  const CGUIDialogPVRGuideSearch::Result result = dlgSearch->GetResult();
  if (result == CGUIDialogPVRGuideSearch::Result::SEARCH)
  {
    SetSearchFilter(tmpSearchFilter);
    ExecuteSearch();
  }
  else if (result == CGUIDialogPVRGuideSearch::Result::SAVE)
  {
    CServiceBroker::GetPVRManager().EpgContainer().PersistSavedSearch(*tmpSearchFilter);
    if (searchFilter)
      searchFilter->SetDatabaseId(tmpSearchFilter->GetDatabaseId());

    const CPVREpgSearchPath path(m_vecItems->GetPath());
    if (path.IsValid() && path.IsSearchRoot())
    {
      SetSearchFilter(tmpSearchFilter);
      ExecuteSearch();
    }
  }

  return result;
}

void CGUIWindowPVRSearchBase::ExecuteSearch()
{
  m_bSearchConfirmed = true;

  const CPVREpgSearchPath path(m_vecItems->GetPath());
  if (path.IsValid() && path.IsSavedSearchesRoot())
  {
    GoParentFolder();
  }
  else if (IsActive())
  {
    Refresh(true);
  }

  // Save if not a transient search
  if (m_searchfilter->GetDatabaseId() != -1)
    CServiceBroker::GetPVRManager().EpgContainer().UpdateSavedSearchLastExecuted(*m_searchfilter);
}

void CGUIWindowPVRSearchBase::SetSearchFilter(
    const std::shared_ptr<CPVREpgSearchFilter>& searchFilter)
{
  if (m_searchfilter && m_searchfilter->IsChanged() &&
      (!searchFilter || m_searchfilter->GetPath() != searchFilter->GetPath()))
  {
    bool bCanceled = false;
    if (!CGUIDialogYesNo::ShowAndGetInput(CVariant{14117}, // "Warning"
                                          CVariant{19341}, // "Save the current search?"
                                          CVariant{},
                                          CVariant{!m_searchfilter->GetTitle().empty()
                                                       ? m_searchfilter->GetTitle()
                                                       : m_searchfilter->GetSearchTerm()},
                                          bCanceled, CVariant{107}, // "Yes"
                                          CVariant{106}, // "No"
                                          CGUIDialogYesNo::NO_TIMEOUT) &&
        !bCanceled)
    {
      std::string title = m_searchfilter->GetTitle();
      if (title.empty())
      {
        title = m_searchfilter->GetSearchTerm();
        if (title.empty())
          title = g_localizeStrings.Get(137); // "Search"
        else
          StringUtils::Trim(title, "\"");

        m_searchfilter->SetTitle(title);
      }
      CServiceBroker::GetPVRManager().EpgContainer().PersistSavedSearch(*m_searchfilter);
    }
  }
  m_searchfilter = searchFilter;
}

std::string CGUIWindowPVRTVSearch::GetRootPath() const
{
  return CPVREpgSearchPath::PATH_TV_SEARCH;
}

std::string CGUIWindowPVRTVSearch::GetStartFolder(const std::string& dir)
{
  return CPVREpgSearchPath::PATH_TV_SEARCH;
}

std::string CGUIWindowPVRTVSearch::GetDirectoryPath()
{
  return URIUtils::PathHasParent(m_vecItems->GetPath(), CPVREpgSearchPath::PATH_TV_SEARCH)
             ? m_vecItems->GetPath()
             : CPVREpgSearchPath::PATH_TV_SEARCH;
}

std::string CGUIWindowPVRRadioSearch::GetRootPath() const
{
  return CPVREpgSearchPath::PATH_RADIO_SEARCH;
}

std::string CGUIWindowPVRRadioSearch::GetStartFolder(const std::string& dir)
{
  return CPVREpgSearchPath::PATH_RADIO_SEARCH;
}

std::string CGUIWindowPVRRadioSearch::GetDirectoryPath()
{
  return URIUtils::PathHasParent(m_vecItems->GetPath(), CPVREpgSearchPath::PATH_RADIO_SEARCH)
             ? m_vecItems->GetPath()
             : CPVREpgSearchPath::PATH_RADIO_SEARCH;
}