Merge pull request #4857 from t-nelson/Gotham_13.2_backports
[vuplus_xbmc] / xbmc / playlists / PlayListWPL.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 "PlayListWPL.h"
22 #include "Util.h"
23 #include "utils/XBMCTinyXML.h"
24 #include "settings/AdvancedSettings.h"
25 #include "filesystem/File.h"
26 #include "utils/log.h"
27 #include "utils/URIUtils.h"
28
29 using namespace XFILE;
30 using namespace PLAYLIST;
31 using namespace std;
32
33 /* ------------------------ example wpl playlist file ---------------------------------
34   <?wpl version="1.0"?>
35   <smil>
36       <head>
37           <meta name="Generator" content="Microsoft Windows Media Player -- 10.0.0.3646"/>
38           <author/>
39           <title>Playlist</title>
40       </head>
41       <body>
42           <seq>
43               <media src="E:\MP3\Track1.mp3"/>
44               <media src="E:\MP3\Track2.mp3"/>
45               <media src="E:\MP3\track3.mp3"/>
46           </seq>
47       </body>
48   </smil>
49 ------------------------ end of example wpl playlist file ---------------------------------*/
50 //Note: File is utf-8 encoded by default
51
52 CPlayListWPL::CPlayListWPL(void)
53 {}
54
55 CPlayListWPL::~CPlayListWPL(void)
56 {}
57
58
59 bool CPlayListWPL::LoadData(istream& stream)
60 {
61   CXBMCTinyXML xmlDoc;
62
63   stream >> xmlDoc;
64   if (xmlDoc.Error())
65   {
66     CLog::Log(LOGERROR, "Unable to parse B4S info Error: %s", xmlDoc.ErrorDesc());
67     return false;
68   }
69
70   TiXmlElement* pRootElement = xmlDoc.RootElement();
71   if (!pRootElement ) return false;
72
73   TiXmlElement* pHeadElement = pRootElement->FirstChildElement("head");
74   if (pHeadElement )
75   {
76     TiXmlElement* pTitelElement = pHeadElement->FirstChildElement("title");
77     if (pTitelElement )
78       m_strPlayListName = pTitelElement->Value();
79   }
80
81   TiXmlElement* pBodyElement = pRootElement->FirstChildElement("body");
82   if (!pBodyElement ) return false;
83
84   TiXmlElement* pSeqElement = pBodyElement->FirstChildElement("seq");
85   if (!pSeqElement ) return false;
86
87   TiXmlElement* pMediaElement = pSeqElement->FirstChildElement("media");
88
89   if (!pMediaElement) return false;
90   while (pMediaElement)
91   {
92     CStdString strFileName = pMediaElement->Attribute("src");
93     if (strFileName.size())
94     {
95       strFileName = URIUtils::SubstitutePath(strFileName);
96       CUtil::GetQualifiedFilename(m_strBasePath, strFileName);
97       CStdString strDescription = URIUtils::GetFileName(strFileName);
98       CFileItemPtr newItem(new CFileItem(strDescription));
99       newItem->SetPath(strFileName);
100       Add(newItem);
101     }
102     pMediaElement = pMediaElement->NextSiblingElement();
103   }
104   return true;
105 }
106
107 void CPlayListWPL::Save(const CStdString& strFileName) const
108 {
109   if (!m_vecItems.size()) return ;
110   CStdString strPlaylist = CUtil::MakeLegalPath(strFileName);
111   CFile file;
112   if (!file.OpenForWrite(strPlaylist, true))
113   {
114     CLog::Log(LOGERROR, "Could not save WPL playlist: [%s]", strPlaylist.c_str());
115     return ;
116   }
117   CStdString write;
118   write += StringUtils::Format("<?wpl version=%c1.0%c>\n", 34, 34);
119   write += StringUtils::Format("<smil>\n");
120   write += StringUtils::Format("    <head>\n");
121   write += StringUtils::Format("        <meta name=%cGenerator%c content=%cMicrosoft Windows Media Player -- 10.0.0.3646%c/>\n", 34, 34, 34, 34);
122   write += StringUtils::Format("        <author/>\n");
123   write += StringUtils::Format("        <title>%s</title>\n", m_strPlayListName.c_str());
124   write += StringUtils::Format("    </head>\n");
125   write += StringUtils::Format("    <body>\n");
126   write += StringUtils::Format("        <seq>\n");
127   for (int i = 0; i < (int)m_vecItems.size(); ++i)
128   {
129     CFileItemPtr item = m_vecItems[i];
130     write += StringUtils::Format("            <media src=%c%s%c/>", 34, item->GetPath().c_str(), 34);
131   }
132   write += StringUtils::Format("        </seq>\n");
133   write += StringUtils::Format("    </body>\n");
134   write += StringUtils::Format("</smil>\n");
135   file.Write(write.c_str(), write.size());
136   file.Close();
137 }