aboutsummaryrefslogtreecommitdiff
path: root/xbmc/video/jobs/VideoLibraryMarkWatchedJob.cpp
blob: 486bd7a7277c88127cf249f35bc248095ad1f3cc (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
/*
 *  Copyright (C) 2014-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 "VideoLibraryMarkWatchedJob.h"

#include "FileItem.h"
#include "ServiceBroker.h"
#include "Util.h"
#include "filesystem/Directory.h"
#ifdef HAS_UPNP
#include "network/upnp/UPnP.h"
#endif
#include "profiles/ProfileManager.h"
#include "pvr/PVRManager.h"
#include "pvr/recordings/PVRRecordings.h"
#include "settings/SettingsComponent.h"
#include "utils/URIUtils.h"
#include "video/VideoDatabase.h"

#include <memory>
#include <vector>

CVideoLibraryMarkWatchedJob::CVideoLibraryMarkWatchedJob(const std::shared_ptr<CFileItem>& item,
                                                         bool mark)
  : m_item(item), m_mark(mark)
{ }

CVideoLibraryMarkWatchedJob::~CVideoLibraryMarkWatchedJob() = default;

bool CVideoLibraryMarkWatchedJob::operator==(const CJob* job) const
{
  if (strcmp(job->GetType(), GetType()) != 0)
    return false;

  const CVideoLibraryMarkWatchedJob* markJob = dynamic_cast<const CVideoLibraryMarkWatchedJob*>(job);
  if (markJob == NULL)
    return false;

  return m_item->IsSamePath(markJob->m_item.get()) && markJob->m_mark == m_mark;
}

bool CVideoLibraryMarkWatchedJob::Work(CVideoDatabase &db)
{
  const std::shared_ptr<CProfileManager> profileManager = CServiceBroker::GetSettingsComponent()->GetProfileManager();

  if (!profileManager->GetCurrentProfile().canWriteDatabases())
    return false;

  CFileItemList items;
  items.Add(std::make_shared<CFileItem>(*m_item));

  if (m_item->m_bIsFolder)
    CUtil::GetRecursiveListing(m_item->GetPath(), items, "", XFILE::DIR_FLAG_NO_FILE_INFO);

  std::vector<CFileItemPtr> markItems;
  for (int i = 0; i < items.Size(); i++)
  {
    CFileItemPtr item = items.Get(i);
    if (item->HasVideoInfoTag() && m_mark == (item->GetVideoInfoTag()->GetPlayCount() > 0))
      continue;

#ifdef HAS_UPNP
    if (URIUtils::IsUPnP(item->GetPath()) && UPNP::CUPnP::MarkWatched(*item, m_mark))
      continue;
#endif

    if (item->HasPVRRecordingInfoTag() &&
        CServiceBroker::GetPVRManager().Recordings()->MarkWatched(item->GetPVRRecordingInfoTag(), m_mark))
    {
      CDateTime newLastPlayed;
      if (m_mark)
        newLastPlayed = db.IncrementPlayCount(*item);
      else
        newLastPlayed = db.SetPlayCount(*item, 0);

      if (newLastPlayed.IsValid())
        item->GetVideoInfoTag()->m_lastPlayed = newLastPlayed;

      continue;
    }

    markItems.push_back(item);
  }

  if (markItems.empty())
    return true;

  db.BeginTransaction();

  for (std::vector<CFileItemPtr>::const_iterator iter = markItems.begin(); iter != markItems.end(); ++iter)
  {
    const CFileItemPtr& item = *iter;

    std::string path(item->GetPath());
    if (item->HasVideoInfoTag() && !item->GetVideoInfoTag()->GetPath().empty())
      path = item->GetVideoInfoTag()->GetPath();

    // With both mark as watched and unwatched we want the resume bookmarks to be reset
    db.ClearBookMarksOfFile(path, CBookmark::RESUME);

    CDateTime newLastPlayed;
    if (m_mark)
      newLastPlayed = db.IncrementPlayCount(*item);
    else
      newLastPlayed = db.SetPlayCount(*item, 0);

    if (newLastPlayed.IsValid() && item->HasVideoInfoTag())
      item->GetVideoInfoTag()->m_lastPlayed = newLastPlayed;
  }

  db.CommitTransaction();
  db.Close();

  return true;
}