2 * Copyright (C) 2005-2008 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, write to
17 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
18 * http://www.gnu.org/copyleft/gpl.html
22 #include "MythDirectory.h"
23 #include "MythSession.h"
24 #include "utils/URIUtils.h"
25 #include "DllLibCMyth.h"
26 #include "video/VideoInfoTag.h"
28 #include "settings/GUISettings.h"
29 #include "settings/AdvancedSettings.h"
31 #include "utils/StringUtils.h"
32 #include "guilib/LocalizeStrings.h"
33 #include "utils/log.h"
34 #include "DirectoryCache.h"
35 #include "utils/TimeUtils.h"
39 #include "cmyth/include/cmyth/cmyth.h"
40 #include "cmyth/include/refmem/refmem.h"
43 using namespace XFILE;
46 CMythDirectory::CMythDirectory()
54 CMythDirectory::~CMythDirectory()
59 DIR_CACHE_TYPE CMythDirectory::GetCacheType(const CStdString& strPath) const
62 CStdString fileName = url.GetFileName();
63 URIUtils::RemoveSlashAtEnd(fileName);
66 * Always cache "All Recordings", "Guide" (top folder only), "Movies", and "TV Shows" (including
69 * Entire directory cache for myth:// is invalidated when the root directory is requested to
70 * ensure content is always up-to-date.
72 if (fileName == "recordings"
73 || fileName == "guide"
74 || fileName == "movies"
75 || fileName.Left(7) == "tvshows")
76 return DIR_CACHE_ALWAYS;
78 return DIR_CACHE_ONCE;
81 void CMythDirectory::Release()
85 m_dll->ref_release(m_recorder);
90 CMythSession::ReleaseSession(m_session);
96 bool CMythDirectory::GetGuide(const CStdString& base, CFileItemList &items)
98 cmyth_database_t db = m_session->GetDatabase();
102 cmyth_chanlist_t list = m_dll->mysql_get_chanlist(db);
105 CLog::Log(LOGERROR, "%s - Unable to get list of channels: %s", __FUNCTION__, base.c_str());
110 int count = m_dll->chanlist_get_count(list);
111 for (int i = 0; i < count; i++)
113 cmyth_channel_t channel = m_dll->chanlist_get_item(list, i);
116 if (!m_dll->channel_visible(channel))
118 m_dll->ref_release(channel);
122 int channum = m_dll->channel_channum(channel); // e.g. 3
123 CStdString name = GetValue(m_dll->channel_name(channel)); // e.g. TV3
126 CLog::Log(LOGDEBUG, "%s - Skipping channel number %d as <= 0: %s", __FUNCTION__, channum, name.c_str());
127 m_dll->ref_release(channel);
131 CLog::Log(LOGDEBUG, "%s - Adding channel number %d: %s", __FUNCTION__, channum, name.c_str());
134 number.Format("%d", channum); // CStdString easier for string manipulation than int.
135 url.SetFileName("guide/" + number);
136 CFileItemPtr item(new CFileItem(url.Get(), true));
137 item->m_strTitle = number;
139 item->m_strTitle += " - " + name; // e.g. 3 - TV3
141 CStdString icon = GetValue(m_dll->channel_icon(channel));
144 url.SetFileName("files/channels/" + URIUtils::GetFileName(icon)); // e.g. files/channels/tv3.jpg
145 item->SetThumbnailImage(url.Get());
150 m_dll->ref_release(channel);
154 items.AddSortMethod(SORT_METHOD_LABEL, 551 /* Name */, LABEL_MASKS("", "", "%K", ""));
156 m_dll->ref_release(list);
160 bool CMythDirectory::GetGuideForChannel(const CStdString& base, CFileItemList &items, int channelNumber)
162 cmyth_database_t database = m_session->GetDatabase();
165 CLog::Log(LOGERROR, "%s - Could not get database", __FUNCTION__);
171 time_t end = now + (24 * 60 * 60); // How many seconds of EPG from now we should grab, 24 hours in seconds
173 cmyth_program_t *program = NULL;
174 // TODO: See if there is a way to just get the entries for the chosen channel rather than ALL
175 int count = m_dll->mysql_get_guide(database, &program, now, end);
176 CLog::Log(LOGDEBUG, "%s - %i entries in guide data", __FUNCTION__, count);
180 for (int i = 0; i < count; i++)
182 if (program[i].channum == channelNumber)
184 CFileItemPtr item(new CFileItem("", false)); // No path for guide entries
187 * Set the FileItem meta data.
189 CStdString title = program[i].title; // e.g. Mythbusters
190 CStdString subtitle = program[i].subtitle; // e.g. The Pirate Special
191 CDateTime localstart;
192 if (program[i].starttime)
193 localstart = CTimeUtils::GetLocalTime(program[i].starttime);
194 item->m_strTitle.Format("%s - %s", localstart.GetAsLocalizedTime("HH:mm", false), title); // e.g. 20:30 - Mythbusters
195 if (!subtitle.IsEmpty())
196 item->m_strTitle += " - \"" + subtitle + "\""; // e.g. 20:30 - Mythbusters - "The Pirate Special"
197 item->m_dateTime = localstart;
200 * Set the VideoInfoTag meta data so it matches the FileItem meta data where possible.
202 CVideoInfoTag* tag = item->GetVideoInfoTag();
203 tag->m_strTitle = title;
204 if (!subtitle.IsEmpty())
205 tag->m_strTitle += " - \"" + subtitle + "\""; // e.g. Mythbusters - "The Pirate Special"
206 tag->m_strShowTitle = title;
207 tag->m_strOriginalTitle = title;
208 tag->m_strPlotOutline = subtitle;
209 tag->m_strPlot = program[i].description;
210 // TODO: Strip out the subtitle from the description if it is present at the start?
211 // TODO: Do we need to add the subtitle to the start of the plot if not already as it used to? Seems strange, should be handled by skin?
212 tag->m_genre = StringUtils::Split(program[i].category, g_advancedSettings.m_videoItemSeparator); // e.g. Sports
213 tag->m_strAlbum = program[i].callsign; // e.g. TV3
215 CDateTime start(program[i].starttime);
216 CDateTime end(program[i].endtime);
217 CDateTimeSpan runtime = end - start;
218 tag->m_strRuntime = StringUtils::SecondsToTimeString(runtime.GetSeconds() +
219 runtime.GetMinutes() * 60 +
220 runtime.GetHours() * 3600);
221 tag->m_iSeason = 0; // So XBMC treats the content as an episode and displays tag information.
229 * Items are sorted as added to the list (in ascending date order). Specifying sorting by date can
230 * result in the guide being shown in the wrong order for skins that sort by date in descending
231 * order by default with no option to change to ascending, e.g. Confluence.
233 items.AddSortMethod(SORT_METHOD_NONE, 552 /* Date */, LABEL_MASKS("%K", "%J")); // Still leave the date label
235 m_dll->ref_release(program);
239 bool CMythDirectory::GetRecordings(const CStdString& base, CFileItemList &items, enum FilterType type,
240 const CStdString& filter)
242 cmyth_proglist_t list = m_session->GetAllRecordedPrograms();
245 CLog::Log(LOGERROR, "%s - unable to get list of recordings", __FUNCTION__);
249 int count = m_dll->proglist_get_count(list);
250 for (int i = 0; i < count; i++)
252 cmyth_proginfo_t program = m_dll->proglist_get_item(list, i);
255 if (!IsVisible(program))
257 m_dll->ref_release(program);
263 * The base is the URL used to connect to the master server. The hostname in this may not
264 * appropriate for all items as MythTV supports multiple backends (master + slaves).
266 * The appropriate host for playback is contained in the program information sent back from
267 * the master. The same username and password are used in the URL as for the master.
269 url.SetHostName(GetValue(m_dll->proginfo_host(program)));
271 CStdString path = URIUtils::GetFileName(GetValue(m_dll->proginfo_pathname(program)));
272 CStdString name = GetValue(m_dll->proginfo_title(program));
277 if (!IsMovie(program))
279 m_dll->ref_release(program);
282 url.SetFileName("movies/" + path);
285 if (filter.CompareNoCase(name))
287 m_dll->ref_release(program);
290 url.SetFileName("tvshows/" + name + "/" + path);
293 url.SetFileName("recordings/" + path);
297 CFileItemPtr item(new CFileItem(url.Get(), false));
298 m_session->SetFileItemMetaData(*item, program);
301 * If MOVIES, set the label and specify as pre-formatted so any scraper lookup will use the
302 * label rather than the filename. Don't set as pre-formatted for any other types as this
303 * prevents the display of the title changing depending on what the list is being sorted by.
308 * Adding the production year, if available, to the label for Movies to aid in scraper
311 CStdString label(item->m_strTitle);
312 CStdString prodyear = GetValue(m_dll->proginfo_prodyear(program));
313 if (!prodyear.IsEmpty())
314 label += " (" + prodyear + ")";
315 item->SetLabel(label);
316 item->SetLabelPreformated(true);
320 m_dll->ref_release(program);
323 m_dll->ref_release(list);
326 * Don't sort by name for TV_SHOWS as they all have the same name, so only date sort is useful.
327 * Since the subtitle has been added to the TV Show name, the video sort title sort is used so
328 * the subtitle doesn't influence the sort order and they are sorted by date.
330 if (type != TV_SHOWS)
332 if (g_guiSettings.GetBool("filelists.ignorethewhensorting"))
333 items.AddSortMethod(SORT_METHOD_VIDEO_SORT_TITLE_IGNORE_THE, 551 /* Name */, LABEL_MASKS("%K", "%J"));
335 items.AddSortMethod(SORT_METHOD_VIDEO_SORT_TITLE, 551 /* Name */, LABEL_MASKS("%K", "%J"));
337 items.AddSortMethod(SORT_METHOD_DATE, 552 /* Date */, LABEL_MASKS("%K", "%J"));
343 * \brief Gets a list of folders for recorded TV shows
345 bool CMythDirectory::GetTvShowFolders(const CStdString& base, CFileItemList &items)
347 cmyth_proglist_t list = m_session->GetAllRecordedPrograms();
350 CLog::Log(LOGERROR, "%s - unable to get list of recordings", __FUNCTION__);
354 int count = m_dll->proglist_get_count(list);
355 for (int i = 0; i < count; i++)
357 cmyth_proginfo_t program = m_dll->proglist_get_item(list, i);
360 if (!IsVisible(program))
362 m_dll->ref_release(program);
366 if (!IsTvShow(program))
368 m_dll->ref_release(program);
372 CStdString title = GetValue(m_dll->proginfo_title(program));
373 CStdString path = base + "/" + title + "/";
376 * Only add each TV show once. If the TV show is already in the list, update the date for the
377 * folder to be the date of the last recorded TV show as the programs are returned in the
378 * order they were recorded.
380 if (items.Contains(path))
382 CFileItemPtr item = items.Get(path);
383 item->m_dateTime = GetValue(m_dll->proginfo_rec_start(program));
387 CFileItemPtr item(new CFileItem(path, true));
388 item->m_dateTime = GetValue(m_dll->proginfo_rec_start(program));
389 item->SetLabel(title);
392 m_dll->ref_release(program);
396 m_dll->ref_release(list);
398 if (g_guiSettings.GetBool("filelists.ignorethewhensorting"))
399 items.AddSortMethod(SORT_METHOD_LABEL_IGNORE_THE, 551 /* Name */, LABEL_MASKS("", "", "%L", "%J"));
401 items.AddSortMethod(SORT_METHOD_LABEL, 551 /* Name */, LABEL_MASKS("", "", "%L", "%J"));
402 items.AddSortMethod(SORT_METHOD_DATE, 552 /* Date */, LABEL_MASKS("", "", "%L", "%J"));
407 bool CMythDirectory::GetChannels(const CStdString& base, CFileItemList &items)
409 cmyth_conn_t control = m_session->GetControl();
413 vector<cmyth_proginfo_t> channels;
414 for (unsigned i = 0; i < 16; i++)
416 cmyth_recorder_t recorder = m_dll->conn_get_recorder_from_num(control, i);
420 cmyth_proginfo_t program;
421 program = m_dll->recorder_get_cur_proginfo(recorder);
422 program = m_dll->recorder_get_next_proginfo(recorder, program, BROWSE_DIRECTION_UP);
425 m_dll->ref_release(m_recorder);
429 long startchan = m_dll->proginfo_chan_id(program);
431 while (startchan != currchan)
434 for (j = 0; j < channels.size(); j++)
436 if (m_dll->proginfo_compare(program, channels[j]) == 0)
440 if (j == channels.size())
441 channels.push_back(program);
443 program = m_dll->recorder_get_next_proginfo(recorder, program, BROWSE_DIRECTION_UP);
447 currchan = m_dll->proginfo_chan_id(program);
449 m_dll->ref_release(recorder);
454 * The content of the cmyth_proginfo_t struct retrieved and stored in channels[] above does not
455 * contain the host so the URL cannot be modified to support both master and slave servers.
458 for (unsigned i = 0; i < channels.size(); i++)
460 cmyth_proginfo_t program = channels[i];
462 url.SetFileName("channels/" + GetValue(m_dll->proginfo_chanstr(program)) + ".ts"); // e.g. 3.ts
463 CFileItemPtr item(new CFileItem(url.Get(), false));
464 m_session->SetFileItemMetaData(*item, program);
467 m_dll->ref_release(program);
470 items.AddSortMethod(SORT_METHOD_LABEL, 551 /* Name */, LABEL_MASKS("%K", "%B"));
473 * Video sort title is set to the channel number.
475 if (g_guiSettings.GetBool("filelists.ignorethewhensorting"))
476 items.AddSortMethod(SORT_METHOD_VIDEO_SORT_TITLE_IGNORE_THE, 556 /* Title */, LABEL_MASKS("%K", "%B"));
478 items.AddSortMethod(SORT_METHOD_VIDEO_SORT_TITLE, 556 /* Title */, LABEL_MASKS("%K", "%B"));
483 bool CMythDirectory::GetDirectory(const CStdString& strPath, CFileItemList &items)
485 m_session = CMythSession::AquireSession(strPath);
489 m_dll = m_session->GetLibrary();
493 CStdString base(strPath);
494 URIUtils::RemoveSlashAtEnd(base);
497 CStdString fileName = url.GetFileName();
498 URIUtils::RemoveSlashAtEnd(fileName);
503 * If we can't get the control then we can't connect to the backend. Don't even show any of the
504 * virtual folders as none of them will work. Without this check the "Browse" functionality
505 * when adding a myth:// source is way confusing as it shows folders so it looks like it has
506 * connected successfully when it in fact hasn't.
508 cmyth_conn_t control = m_session->GetControl();
514 item.reset(new CFileItem(base + "/recordings/", true));
515 item->SetLabel(g_localizeStrings.Get(22015)); // All recordings
518 item.reset(new CFileItem(base + "/tvshows/", true));
519 item->SetLabel(g_localizeStrings.Get(20343)); // TV shows
522 item.reset(new CFileItem(base + "/movies/", true));
523 item->SetLabel(g_localizeStrings.Get(20342)); // Movies
526 item.reset(new CFileItem(base + "/channels/", true));
527 item->SetLabel(g_localizeStrings.Get(22018)); // Live channels
530 item.reset(new CFileItem(base + "/guide/", true));
531 item->SetLabel(g_localizeStrings.Get(22020)); // Guide
534 items.AddSortMethod(SORT_METHOD_NONE, 564 /* Type */, LABEL_MASKS("", "", "%L", "")); // No sorting, as added to list.
537 * Clear the directory cache so the cached sub-folders are guaranteed to be accurate.
539 g_directoryCache.ClearSubPaths(base);
543 else if (fileName == "channels")
544 return GetChannels(base, items);
545 else if (fileName == "guide")
546 return GetGuide(base, items);
547 else if (fileName.Left(6) == "guide/")
548 return GetGuideForChannel(base, items, atoi(fileName.Mid(6)));
549 else if (fileName == "movies")
550 return GetRecordings(base, items, MOVIES);
551 else if (fileName == "recordings")
552 return GetRecordings(base, items);
553 else if (fileName == "tvshows")
554 return GetTvShowFolders(base, items);
555 else if (fileName.Left(8) == "tvshows/")
556 return GetRecordings(base, items, TV_SHOWS, fileName.Mid(8));
560 bool CMythDirectory::Exists(const char* strPath)
563 * Return true for any virtual folders that are known to exist. Don't check for explicit
564 * existence using GetDirectory() as most methods will return true with empty content due to the
565 * way they are implemented - by iterating over all programs and filtering out content.
568 CStdString fileName = url.GetFileName();
569 URIUtils::RemoveSlashAtEnd(fileName);
572 || fileName == "channels"
573 || fileName == "guide"
574 || fileName.Left(6) == "guide/"
575 || fileName == "movies"
576 || fileName == "recordings"
577 || fileName == "tvshows"
578 || fileName.Left(8) == "tvshows/")
584 bool CMythDirectory::IsVisible(const cmyth_proginfo_t program)
586 CStdString group = GetValue(m_dll->proginfo_recgroup(program));
587 unsigned long flags = m_dll->proginfo_flags(program);
590 * Ignore programs that were recorded using "LiveTV" or that have been deleted via the
591 * "Auto Expire Instead of Delete Recording" option, which places the recording in the
592 * "Deleted" recording group for x days rather than deleting straight away.
594 * As of 0.24, when a recording is deleted using the Myth Protocol it is marked as "pending delete"
595 * using the program flags mask. It is then scheduled to be physically deleted in a detached
596 * thread. This means that a deleted recording can still appear in the list of all recordings.
597 * Recordings that are "pending delete" will have a program flag mask that matches
598 * FL_DELETEPENDING = 0x00000080.
600 return !(group.Equals("LiveTV") || group.Equals("Deleted") || flags & 0x00000080);
603 bool CMythDirectory::IsMovie(const cmyth_proginfo_t program)
606 * The mythconverg.recordedprogram.programid field (if it exists) is a combination key where the first 2 characters map
607 * to the category_type and the rest is the key. From MythTV/release-0-21-fixes/mythtv/libs/libmythtv/programinfo.cpp
613 * Based on MythTV usage it appears that the programid is only filled in for Movies though. Shame, could have used
614 * it for the other categories as well.
616 * mythconverg.recordedprogram.category_type contains the exact information that is needed. However, category_type
617 * isn't available through the libcmyth API. Since there is a direct correlation between the programid starting
618 * with "MV" and the category_type being "movie" that should work fine.
621 const int iMovieLength = g_advancedSettings.m_iMythMovieLength; // Minutes
622 if (iMovieLength > 0) // Use hack to identify movie based on length (used if EPG is dubious).
623 return GetValue(m_dll->proginfo_programid(program)).Left(2) == "MV"
624 || m_dll->proginfo_length_sec(program) > iMovieLength * 60; // Minutes to seconds
626 return GetValue(m_dll->proginfo_programid(program)).Left(2) == "MV";
629 bool CMythDirectory::IsTvShow(const cmyth_proginfo_t program)
632 * There isn't enough information exposed by libcmyth to distinguish between an episode in a series and a
633 * one off TV show. See comment in IsMovie for more information.
635 * Return anything that isn't a movie as per any advanced setting override. This may result in a
636 * recorded TV Show only being shown in the Movies directory if it's something like a double
639 return !IsMovie(program);
642 bool CMythDirectory::SupportsFileOperations(const CStdString& strPath)
645 CStdString filename = url.GetFileName();
646 URIUtils::RemoveSlashAtEnd(filename);
648 * TV Shows directory has sub-folders so extra check is included so only files get the file
651 return filename.Left(11) == "recordings/" ||
652 filename.Left(7) == "movies/" ||
653 (filename.Left(8) == "tvshows/" && URIUtils::GetExtension(filename) != "");
656 bool CMythDirectory::IsLiveTV(const CStdString& strPath)
659 return url.GetFileName().Left(9) == "channels/";