2 * Copyright (C) 2012-2013 Team XBMC
5 * This Program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This Program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with XBMC; see the file COPYING. If not, see
17 * <http://www.gnu.org/licenses/>.
21 #include "dialogs/GUIDialogOK.h"
22 #include "pvr/PVRManager.h"
23 #include "settings/AdvancedSettings.h"
24 #include "PVRRecordings.h"
25 #include "pvr/addons/PVRClients.h"
26 #include "utils/StringUtils.h"
27 #include "utils/RegExp.h"
28 #include "utils/StringUtils.h"
29 #include "video/VideoDatabase.h"
36 CPVRRecording::CPVRRecording()
41 CPVRRecording::CPVRRecording(const PVR_RECORDING &recording, unsigned int iClientId)
45 m_strRecordingId = recording.strRecordingId;
46 m_strTitle = recording.strTitle;
47 m_iClientId = iClientId;
48 m_recordingTime = recording.recordingTime + g_advancedSettings.m_iPVRTimeCorrection;
49 m_duration = CDateTimeSpan(0, 0, recording.iDuration / 60, recording.iDuration % 60);
50 m_iPriority = recording.iPriority;
51 m_iLifetime = recording.iLifetime;
52 m_strDirectory = recording.strDirectory;
53 m_strPlot = recording.strPlot;
54 m_strPlotOutline = recording.strPlotOutline;
55 m_strStreamURL = recording.strStreamURL;
56 m_strChannelName = recording.strChannelName;
57 m_genre = StringUtils::Split(CEpg::ConvertGenreIdToString(recording.iGenreType, recording.iGenreSubType), g_advancedSettings.m_videoItemSeparator);
58 m_playCount = recording.iPlayCount;
59 m_resumePoint.timeInSeconds = recording.iLastPlayedPosition;
60 m_resumePoint.totalTimeInSeconds = recording.iDuration;
61 m_strIconPath = recording.strIconPath;
62 m_strThumbnailPath = recording.strThumbnailPath;
63 m_strFanartPath = recording.strFanartPath;
66 bool CPVRRecording::operator ==(const CPVRRecording& right) const
68 return (this == &right) ||
69 (m_strRecordingId == right.m_strRecordingId &&
70 m_iClientId == right.m_iClientId &&
71 m_strChannelName == right.m_strChannelName &&
72 m_recordingTime == right.m_recordingTime &&
73 m_duration == right.m_duration &&
74 m_strPlotOutline == right.m_strPlotOutline &&
75 m_strPlot == right.m_strPlot &&
76 m_strStreamURL == right.m_strStreamURL &&
77 m_iPriority == right.m_iPriority &&
78 m_iLifetime == right.m_iLifetime &&
79 m_strDirectory == right.m_strDirectory &&
80 m_strFileNameAndPath == right.m_strFileNameAndPath &&
81 m_strTitle == right.m_strTitle &&
82 m_strIconPath == right.m_strIconPath &&
83 m_strThumbnailPath == right.m_strThumbnailPath &&
84 m_strFanartPath == right.m_strFanartPath);
87 bool CPVRRecording::operator !=(const CPVRRecording& right) const
89 return !(*this == right);
92 void CPVRRecording::Reset(void)
94 m_strRecordingId = StringUtils::EmptyString;
96 m_strChannelName = StringUtils::EmptyString;
97 m_strDirectory = StringUtils::EmptyString;
98 m_strStreamURL = StringUtils::EmptyString;
101 m_strFileNameAndPath = StringUtils::EmptyString;
102 m_strIconPath = StringUtils::EmptyString;
103 m_strThumbnailPath = StringUtils::EmptyString;
104 m_strFanartPath = StringUtils::EmptyString;
105 m_bGotMetaData = false;
107 m_recordingTime.Reset();
108 CVideoInfoTag::Reset();
111 int CPVRRecording::GetDuration() const
113 return (m_duration.GetDays() * 60*60*24 +
114 m_duration.GetHours() * 60*60 +
115 m_duration.GetMinutes() * 60 +
116 m_duration.GetSeconds());
119 bool CPVRRecording::Delete(void)
121 PVR_ERROR error = g_PVRClients->DeleteRecording(*this);
122 if (error != PVR_ERROR_NO_ERROR)
131 bool CPVRRecording::Rename(const CStdString &strNewName)
133 m_strTitle = StringUtils::Format("%s", strNewName.c_str());
134 PVR_ERROR error = g_PVRClients->RenameRecording(*this);
135 if (error != PVR_ERROR_NO_ERROR)
144 bool CPVRRecording::SetPlayCount(int count)
148 if (g_PVRClients->SupportsRecordingPlayCount(m_iClientId) &&
149 !g_PVRClients->SetRecordingPlayCount(*this, count, &error))
158 void CPVRRecording::UpdateMetadata(void)
162 if (!g_PVRClients->SupportsRecordingPlayCount(m_iClientId))
164 if (!m_bGotMetaData && db.Open())
166 CFileItem pFileItem(*this);
167 m_playCount = db.GetPlayCount(pFileItem);
171 if (!g_PVRClients->SupportsLastPlayedPosition(m_iClientId))
173 if (!m_bGotMetaData && db.Open())
174 db.GetResumeBookMark(m_strFileNameAndPath, m_resumePoint);
177 m_bGotMetaData = true;
180 bool CPVRRecording::IncrementPlayCount()
182 return SetPlayCount(m_playCount + 1);
185 bool CPVRRecording::SetLastPlayedPosition(int lastplayedposition)
190 bookmark.timeInSeconds = lastplayedposition;
191 bookmark.totalTimeInSeconds = (double)GetDuration();
192 m_resumePoint = bookmark;
194 if (g_PVRClients->SupportsLastPlayedPosition(m_iClientId) &&
195 !g_PVRClients->SetRecordingLastPlayedPosition(*this, lastplayedposition, &error))
203 int CPVRRecording::GetLastPlayedPosition() const
206 if (g_PVRClients->SupportsLastPlayedPosition(m_iClientId))
208 rc = g_PVRClients->GetRecordingLastPlayedPosition(*this);
210 DisplayError(PVR_ERROR_SERVER_ERROR);
215 std::vector<PVR_EDL_ENTRY> CPVRRecording::GetEdl() const
217 if (g_PVRClients->SupportsRecordingEdl(m_iClientId))
219 return g_PVRClients->GetRecordingEdl(*this);
221 return std::vector<PVR_EDL_ENTRY>();
224 void CPVRRecording::DisplayError(PVR_ERROR err) const
226 if (err == PVR_ERROR_SERVER_ERROR)
227 CGUIDialogOK::ShowAndGetInput(19033,19111,19110,0); /* print info dialog "Server error!" */
228 else if (err == PVR_ERROR_REJECTED)
229 CGUIDialogOK::ShowAndGetInput(19033,19068,19110,0); /* print info dialog "Couldn't delete recording!" */
231 CGUIDialogOK::ShowAndGetInput(19033,19147,19110,0); /* print info dialog "Unknown error!" */
236 void CPVRRecording::Update(const CPVRRecording &tag)
238 m_strRecordingId = tag.m_strRecordingId;
239 m_iClientId = tag.m_iClientId;
240 m_strTitle = tag.m_strTitle;
241 m_recordingTime = tag.m_recordingTime;
242 m_duration = tag.m_duration;
243 m_iPriority = tag.m_iPriority;
244 m_iLifetime = tag.m_iLifetime;
245 m_strDirectory = tag.m_strDirectory;
246 m_strPlot = tag.m_strPlot;
247 m_strPlotOutline = tag.m_strPlotOutline;
248 m_strStreamURL = tag.m_strStreamURL;
249 m_strChannelName = tag.m_strChannelName;
250 m_genre = tag.m_genre;
251 m_strIconPath = tag.m_strIconPath;
252 m_strThumbnailPath = tag.m_strThumbnailPath;
253 m_strFanartPath = tag.m_strFanartPath;
255 if (g_PVRClients->SupportsRecordingPlayCount(m_iClientId))
256 m_playCount = tag.m_playCount;
258 if (g_PVRClients->SupportsLastPlayedPosition(m_iClientId))
260 m_resumePoint.timeInSeconds = tag.m_resumePoint.timeInSeconds;
261 m_resumePoint.totalTimeInSeconds = tag.m_resumePoint.totalTimeInSeconds;
264 CStdString strShow = StringUtils::Format("%s - ", g_localizeStrings.Get(20364).c_str());
265 if (StringUtils::StartsWithNoCase(m_strPlotOutline, strShow))
267 CStdString strEpisode = m_strPlotOutline;
268 CStdString strTitle = m_strDirectory;
270 size_t pos = strTitle.rfind('/');
271 strTitle.erase(0, pos + 1);
272 strEpisode.erase(0, strShow.size());
273 m_strTitle = StringUtils::Format("%s - %s", strTitle.c_str(), strEpisode.c_str());
274 pos = strEpisode.find('-');
275 strEpisode.erase(0, pos + 2);
276 m_strPlotOutline = strEpisode;
281 void CPVRRecording::UpdatePath(void)
283 if (!m_strStreamURL.empty())
285 m_strFileNameAndPath = m_strStreamURL;
289 CStdString strTitle(m_strTitle);
290 CStdString strDatetime(m_recordingTime.GetAsSaveString());
291 CStdString strDirectory;
292 CStdString strChannel;
293 StringUtils::Replace(strTitle, '/',' ');
295 if (!m_strDirectory.empty())
296 strDirectory = StringUtils::Format("%s/", m_strDirectory.c_str());
297 if (!m_strChannelName.empty())
298 strChannel = StringUtils::Format(" (%s)", m_strChannelName.c_str());
299 m_strFileNameAndPath = StringUtils::Format("pvr://recordings/%s%s, TV%s, %s.pvr", strDirectory.c_str(), strTitle.c_str(), strChannel.c_str(), strDatetime.c_str());
303 const CDateTime &CPVRRecording::RecordingTimeAsLocalTime(void) const
305 static CDateTime tmp;
306 tmp.SetFromUTCDateTime(m_recordingTime);
311 CStdString CPVRRecording::GetTitleFromURL(const CStdString &url)
314 if (reg.RegComp("pvr://recordings/(.*/)*(.*), TV( \\(.*\\))?, "
315 "(19[0-9][0-9]|20[0-9][0-9])[0-9][0-9][0-9][0-9]_[0-9][0-9][0-9][0-9][0-9][0-9].pvr"))
317 if (reg.RegFind(url.c_str()) >= 0)
318 return reg.GetMatch(2);
320 return StringUtils::EmptyString;
323 void CPVRRecording::CopyClientInfo(CVideoInfoTag *target) const
328 target->m_playCount = m_playCount;
329 target->m_resumePoint = m_resumePoint;