[cosmetics] update date in GPL header
[vuplus_xbmc] / xbmc / pvr / recordings / PVRRecording.cpp
1 /*
2  *      Copyright (C) 2012-2013 Team XBMC
3  *      http://www.xbmc.org
4  *
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)
8  *  any later version.
9  *
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.
14  *
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/>.
18  *
19  */
20
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"
30
31 #include "epg/Epg.h"
32
33 using namespace PVR;
34 using namespace EPG;
35
36 CPVRRecording::CPVRRecording()
37 {
38   Reset();
39 }
40
41 CPVRRecording::CPVRRecording(const PVR_RECORDING &recording, unsigned int iClientId)
42 {
43   Reset();
44
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_strIconPath       = recording.strIconPath;
60   m_strThumbnailPath  = recording.strThumbnailPath;
61   m_strFanartPath     = recording.strFanartPath;
62 }
63
64 bool CPVRRecording::operator ==(const CPVRRecording& right) const
65 {
66   return (this == &right) ||
67       (m_strRecordingId     == right.m_strRecordingId &&
68        m_iClientId          == right.m_iClientId &&
69        m_strChannelName     == right.m_strChannelName &&
70        m_recordingTime      == right.m_recordingTime &&
71        m_duration           == right.m_duration &&
72        m_strPlotOutline     == right.m_strPlotOutline &&
73        m_strPlot            == right.m_strPlot &&
74        m_strStreamURL       == right.m_strStreamURL &&
75        m_iPriority          == right.m_iPriority &&
76        m_iLifetime          == right.m_iLifetime &&
77        m_strDirectory       == right.m_strDirectory &&
78        m_strFileNameAndPath == right.m_strFileNameAndPath &&
79        m_strTitle           == right.m_strTitle &&
80        m_strIconPath        == right.m_strIconPath &&
81        m_strThumbnailPath   == right.m_strThumbnailPath &&
82        m_strFanartPath      == right.m_strFanartPath);
83 }
84
85 bool CPVRRecording::operator !=(const CPVRRecording& right) const
86 {
87   return !(*this == right);
88 }
89
90 void CPVRRecording::Reset(void)
91 {
92   m_strRecordingId     = StringUtils::EmptyString;
93   m_iClientId          = 0;
94   m_strChannelName     = StringUtils::EmptyString;
95   m_strDirectory       = StringUtils::EmptyString;
96   m_strStreamURL       = StringUtils::EmptyString;
97   m_iPriority          = -1;
98   m_iLifetime          = -1;
99   m_strFileNameAndPath = StringUtils::EmptyString;
100   m_strIconPath        = StringUtils::EmptyString;
101   m_strThumbnailPath   = StringUtils::EmptyString;
102   m_strFanartPath      = StringUtils::EmptyString;
103   m_bGotMetaData       = false;
104
105   m_recordingTime.Reset();
106   CVideoInfoTag::Reset();
107 }
108
109 int CPVRRecording::GetDuration() const
110 {
111   return (m_duration.GetDays() * 60*60*24 +
112       m_duration.GetHours() * 60*60 +
113       m_duration.GetMinutes() * 60 +
114       m_duration.GetSeconds());
115 }
116
117 bool CPVRRecording::Delete(void)
118 {
119   PVR_ERROR error = g_PVRClients->DeleteRecording(*this);
120   if (error != PVR_ERROR_NO_ERROR)
121   {
122     DisplayError(error);
123     return false;
124   }
125
126   return true;
127 }
128
129 bool CPVRRecording::Rename(const CStdString &strNewName)
130 {
131   m_strTitle.Format("%s", strNewName);
132   PVR_ERROR error = g_PVRClients->RenameRecording(*this);
133   if (error != PVR_ERROR_NO_ERROR)
134   {
135     DisplayError(error);
136     return false;
137   }
138
139   return true;
140 }
141
142 bool CPVRRecording::SetPlayCount(int count)
143 {
144   PVR_ERROR error;
145   m_playCount = count;
146   if (g_PVRClients->SupportsRecordingPlayCount(m_iClientId) &&
147       !g_PVRClients->SetRecordingPlayCount(*this, count, &error))
148   {
149     DisplayError(error);
150     return false;
151   }
152
153   return true;
154 }
155
156 void CPVRRecording::UpdateMetadata(void)
157 {
158   CVideoDatabase db;
159
160   if (!g_PVRClients->SupportsRecordingPlayCount(m_iClientId))
161   {
162     if (!m_bGotMetaData && db.Open())
163     {
164       CFileItem pFileItem(*this);
165       m_playCount = db.GetPlayCount(pFileItem);
166     }
167   }
168
169   if ((g_PVRClients->SupportsLastPlayedPosition(m_iClientId)))
170   {
171     int iPosition = g_PVRClients->GetRecordingLastPlayedPosition(*this);
172     if (iPosition > 0)
173     {
174       m_resumePoint.timeInSeconds      = iPosition;
175       m_resumePoint.totalTimeInSeconds = (double)GetDuration();
176     }
177   }
178   else if (!m_bGotMetaData && db.Open())
179   {
180     db.GetResumeBookMark(m_strFileNameAndPath, m_resumePoint);
181   }
182
183   m_bGotMetaData = true;
184 }
185
186 bool CPVRRecording::IncrementPlayCount()
187 {
188   return SetPlayCount(m_playCount + 1);
189 }
190
191 bool CPVRRecording::SetLastPlayedPosition(int lastplayedposition)
192 {
193   PVR_ERROR error;
194   if (g_PVRClients->SupportsLastPlayedPosition(m_iClientId) &&
195       !g_PVRClients->SetRecordingLastPlayedPosition(*this, lastplayedposition, &error))
196   {
197     DisplayError(error);
198     return false;
199   }
200   return true;
201 }
202
203 int CPVRRecording::GetLastPlayedPosition() const
204 {
205   int rc = -1;
206   if (g_PVRClients->SupportsLastPlayedPosition(m_iClientId))
207   {
208     rc = g_PVRClients->GetRecordingLastPlayedPosition(*this);
209     if (rc < 0)
210       DisplayError(PVR_ERROR_SERVER_ERROR);
211   }
212   return rc;
213 }
214
215 void CPVRRecording::DisplayError(PVR_ERROR err) const
216 {
217   if (err == PVR_ERROR_SERVER_ERROR)
218     CGUIDialogOK::ShowAndGetInput(19033,19111,19110,0); /* print info dialog "Server error!" */
219   else if (err == PVR_ERROR_REJECTED)
220     CGUIDialogOK::ShowAndGetInput(19033,19068,19110,0); /* print info dialog "Couldn't delete recording!" */
221   else
222     CGUIDialogOK::ShowAndGetInput(19033,19147,19110,0); /* print info dialog "Unknown error!" */
223
224   return;
225 }
226
227 void CPVRRecording::Update(const CPVRRecording &tag)
228 {
229   m_strRecordingId    = tag.m_strRecordingId;
230   m_iClientId         = tag.m_iClientId;
231   m_strTitle          = tag.m_strTitle;
232   m_recordingTime     = tag.m_recordingTime;
233   m_duration          = tag.m_duration;
234   m_iPriority         = tag.m_iPriority;
235   m_iLifetime         = tag.m_iLifetime;
236   m_strDirectory      = tag.m_strDirectory;
237   m_strPlot           = tag.m_strPlot;
238   m_strPlotOutline    = tag.m_strPlotOutline;
239   m_strStreamURL      = tag.m_strStreamURL;
240   m_strChannelName    = tag.m_strChannelName;
241   m_genre             = tag.m_genre;
242   m_strIconPath       = tag.m_strIconPath;
243   m_strThumbnailPath  = tag.m_strThumbnailPath;
244   m_strFanartPath     = tag.m_strFanartPath;
245
246   if (g_PVRClients->SupportsRecordingPlayCount(m_iClientId))
247     m_playCount       = tag.m_playCount;
248
249   CStdString strShow;
250   strShow.Format("%s - ", g_localizeStrings.Get(20364).c_str());
251   if (m_strPlotOutline.Left(strShow.size()).Equals(strShow))
252   {
253     CStdString strEpisode = m_strPlotOutline;
254     CStdString strTitle = m_strDirectory;
255     
256     int pos = strTitle.ReverseFind('/');
257     strTitle.erase(0, pos + 1);
258     strEpisode.erase(0, strShow.size());
259     m_strTitle.Format("%s - %s", strTitle.c_str(), strEpisode);
260     pos = strEpisode.Find('-');
261     strEpisode.erase(0, pos + 2);
262     m_strPlotOutline = strEpisode;
263   }
264   UpdatePath();
265 }
266
267 void CPVRRecording::UpdatePath(void)
268 {
269   if (!m_strStreamURL.IsEmpty())
270   {
271     m_strFileNameAndPath = m_strStreamURL;
272   }
273   else
274   {
275     CStdString strTitle(m_strTitle);
276     CStdString strDatetime(m_recordingTime.GetAsSaveString());
277     CStdString strDirectory;
278     CStdString strChannel;
279     strTitle.Replace('/',' ');
280
281     if (!m_strDirectory.IsEmpty())
282       strDirectory.Format("%s/", m_strDirectory.c_str());
283     if (!m_strChannelName.IsEmpty())
284       strChannel.Format(" (%s)", m_strChannelName.c_str());
285     m_strFileNameAndPath.Format("pvr://recordings/%s%s, TV%s, %s.pvr", strDirectory.c_str(), strTitle.c_str(), strChannel.c_str(), strDatetime.c_str());
286   }
287 }
288
289 const CDateTime &CPVRRecording::RecordingTimeAsLocalTime(void) const
290 {
291   static CDateTime tmp;
292   tmp.SetFromUTCDateTime(m_recordingTime);
293
294   return tmp;
295 }
296
297 CStdString CPVRRecording::GetTitleFromURL(const CStdString &url)
298 {
299   CRegExp reg(true);
300   if (reg.RegComp("pvr://recordings/(.*/)*(.*), TV( \\(.*\\))?, "
301       "(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"))
302   {
303     if (reg.RegFind(url.c_str()) >= 0)
304       return reg.GetReplaceString("\\2");
305   }
306   return StringUtils::EmptyString;
307 }
308
309 void CPVRRecording::CopyClientInfo(CVideoInfoTag *target) const
310 {
311   if (!target)
312     return;
313
314   target->m_playCount   = m_playCount;
315   target->m_resumePoint = m_resumePoint;
316 }