[cstdstring] demise Format, replacing with StringUtils::Format
[vuplus_xbmc] / xbmc / filesystem / BlurayDirectory.cpp
1 /*
2  *      Copyright (C) 2005-2013 Team XBMC
3  *      http://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 "BlurayDirectory.h"
22 #include "utils/log.h"
23 #include "utils/URIUtils.h"
24 #include "utils/StringUtils.h"
25 #include "URL.h"
26 #include "DllLibbluray.h"
27 #include "FileItem.h"
28 #include "video/VideoInfoTag.h"
29 #include "guilib/LocalizeStrings.h"
30
31 namespace XFILE
32 {
33
34 #define MAIN_TITLE_LENGTH_PERCENT 70 /** Minumum length of main titles, based on longest title */
35
36 CBlurayDirectory::CBlurayDirectory()
37   : m_dll(NULL)
38   , m_bd(NULL)
39 {
40 }
41
42 CBlurayDirectory::~CBlurayDirectory()
43 {
44   Dispose();
45 }
46
47 void CBlurayDirectory::Dispose()
48 {
49   if(m_bd)
50   {
51     m_dll->bd_close(m_bd);
52     m_bd = NULL;
53   }
54   delete m_dll;
55   m_dll = NULL;
56 }
57
58 CFileItemPtr CBlurayDirectory::GetTitle(const BLURAY_TITLE_INFO* title, const CStdString& label)
59 {
60   CStdString buf;
61   CFileItemPtr item(new CFileItem("", false));
62   CURL path(m_url);
63   buf = StringUtils::Format("BDMV/PLAYLIST/%05d.mpls", title->playlist);
64   path.SetFileName(buf);
65   item->SetPath(path.Get());
66   item->GetVideoInfoTag()->m_duration = (int)(title->duration / 90000);
67   item->GetVideoInfoTag()->m_iTrack = title->playlist;
68   buf = StringUtils::Format(label.c_str(), title->playlist);
69   item->m_strTitle = buf;
70   item->SetLabel(buf);
71   item->m_dwSize = 0;
72   item->SetIconImage("DefaultVideo.png");
73   for(unsigned int i = 0; i < title->clip_count; ++i)
74     item->m_dwSize += title->clips[i].pkt_count * 192;
75
76   return item;
77 }
78
79 void CBlurayDirectory::GetTitles(bool main, CFileItemList &items)
80 {
81   unsigned titles = m_dll->bd_get_titles(m_bd, TITLES_RELEVANT, 0);
82   CStdString buf;
83
84   std::vector<BLURAY_TITLE_INFO*> buffer;
85
86   uint64_t duration = 0;
87
88   for(unsigned i=0; i < titles; i++)
89   {
90     BLURAY_TITLE_INFO *t = m_dll->bd_get_title_info(m_bd, i, 0);
91     if(!t)
92     {
93       CLog::Log(LOGDEBUG, "CBlurayDirectory - unable to get title %d", i);
94       continue;
95     }
96     if(t->duration > duration)
97       duration = t->duration;
98
99     buffer.push_back(t);
100   }
101
102   if(main)
103     duration = duration * MAIN_TITLE_LENGTH_PERCENT / 100;
104   else
105     duration = 0;
106
107   for(std::vector<BLURAY_TITLE_INFO*>::iterator it = buffer.begin(); it != buffer.end(); ++it)
108   {
109     if((*it)->duration < duration)
110       continue;
111     items.Add(GetTitle(*it, main ? g_localizeStrings.Get(25004) /* Main Title */ : g_localizeStrings.Get(25005) /* Title */));
112   }
113
114
115   for(std::vector<BLURAY_TITLE_INFO*>::iterator it = buffer.begin(); it != buffer.end(); ++it)
116     m_dll->bd_free_title_info(*it);
117 }
118
119 void CBlurayDirectory::GetRoot(CFileItemList &items)
120 {
121     GetTitles(true, items);
122
123     CURL path(m_url);
124     CFileItemPtr item;
125
126     path.SetFileName(URIUtils::AddFileToFolder(m_url.GetFileName(), "titles"));
127     item.reset(new CFileItem());
128     item->SetPath(path.Get());
129     item->m_bIsFolder = true;
130     item->SetLabel(g_localizeStrings.Get(25002) /* All titles */);
131     item->SetIconImage("DefaultVideoPlaylists.png");
132     items.Add(item);
133
134     path.SetFileName("BDMV/MovieObject.bdmv");
135     item.reset(new CFileItem());
136     item->SetPath(path.Get());
137     item->m_bIsFolder = false;
138     item->SetLabel(g_localizeStrings.Get(25003) /* Menus */);
139     item->SetIconImage("DefaultProgram.png");
140     items.Add(item);
141 }
142
143 bool CBlurayDirectory::GetDirectory(const CStdString& path, CFileItemList &items)
144 {
145   Dispose();
146   m_url.Parse(path);
147   CStdString root = m_url.GetHostName();
148   CStdString file = m_url.GetFileName();
149   URIUtils::RemoveSlashAtEnd(file);
150   URIUtils::RemoveSlashAtEnd(root);
151
152   m_dll = new DllLibbluray();
153   if (!m_dll->Load())
154   {
155     CLog::Log(LOGERROR, "CBlurayDirectory::GetDirectory - failed to load dll");
156     return false;
157   }
158
159   m_dll->bd_register_dir(DllLibbluray::dir_open);
160   m_dll->bd_register_file(DllLibbluray::file_open);
161   m_dll->bd_set_debug_handler(DllLibbluray::bluray_logger);
162   m_dll->bd_set_debug_mask(DBG_CRIT | DBG_BLURAY | DBG_NAV);
163
164   m_bd = m_dll->bd_open(root.c_str(), NULL);
165
166   if(!m_bd)
167   {
168     CLog::Log(LOGERROR, "CBlurayDirectory::GetDirectory - failed to open %s", root.c_str());
169     return false;
170   }
171
172   if(file == "")
173     GetRoot(items);
174   else if(file == "titles")
175     GetTitles(false, items);
176   else
177     return false;
178
179   items.AddSortMethod(SortByTrackNumber,  554, LABEL_MASKS("%L", "%D", "%L", ""));    // FileName, Duration | Foldername, empty
180   items.AddSortMethod(SortBySize,         553, LABEL_MASKS("%L", "%I", "%L", "%I"));  // FileName, Size | Foldername, Size
181
182   return true;
183 }
184
185
186 } /* namespace XFILE */