aboutsummaryrefslogtreecommitdiff
path: root/xbmc/PlayListXML.cpp
blob: 63829b2058f2b60f115b8369909d6c616332f9c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
 *      Copyright (C) 2005-2008 Team XBMC
 *      http://www.xbmc.org
 *
 *  This Program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *
 *  This Program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with XBMC; see the file COPYING.  If not, write to
 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 *  http://www.gnu.org/copyleft/gpl.html
 *
 */

#include "PlayListXML.h"
#include "FileSystem/File.h"
#include "Util.h"
#include "utils/RegExp.h"
#include "utils/log.h"
#include "XMLUtils.h"
#ifndef _LINUX
#include "cores/dllloader/exports/emu_msvcrt.h"
#endif

#include "Settings.h"
#include "MusicInfoTag.h"

using namespace PLAYLIST;
using namespace XFILE;

/*
 Playlist example (must be stored with .pxml extension):

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<streams>
  <!-- Stream definition. To have multiple streams, just add another <stream>...</stream> set.  !-->
  <stream>
    <!-- Stream URL !-->
    <url>mms://stream02.rambler.ru/eurosport</url>
    <!-- Stream name - used for display !-->
    <name>Евроспорт</name>
    <!-- Stream category - currently only LIVETV is supported !-->
    <category>LIVETV</category>
    <!-- Stream language code !-->
    <lang>RU</lang>
    <!-- Stream channel number - will be used to select stream by channel number !-->
    <channel>1</channel>
    <!-- Stream is password-protected !-->
    <lockpassword>123</lockpassword>
  </stream>

  <stream>
    <url>mms://video.rfn.ru/vesti_24</url>
    <name>Вести 24</name>
    <category>LIVETV</category>
    <lang>RU</lang>
    <channel>2</channel>
  </stream>

</streams>
*/


CPlayListXML::CPlayListXML(void)
{}

CPlayListXML::~CPlayListXML(void)
{}


static inline CStdString GetString( const TiXmlElement* pRootElement, const char *tagName )
{
  CStdString strValue;
  if ( XMLUtils::GetString(pRootElement, tagName, strValue) )
    return strValue;

  return "";
}

bool CPlayListXML::Load( const CStdString& strFileName )
{
  TiXmlDocument xmlDoc;

  m_strPlayListName = CUtil::GetFileName(strFileName);
  CUtil::GetParentPath(strFileName, m_strBasePath);

  Clear();

  // Try to load the file as XML. If it does not load, return an error.
  if ( !xmlDoc.LoadFile( strFileName ) )
  {
    CLog::Log(LOGERROR, "Playlist %s has invalid format/malformed xml", strFileName.c_str());
    return false;
  }

  TiXmlElement *pRootElement = xmlDoc.RootElement();

  // If the stream does not contain "streams", still ok. Not an error.
  if ( !pRootElement || stricmp( pRootElement->Value(), "streams" ) )
  {
    CLog::Log(LOGERROR, "Playlist %s has no <streams> root", strFileName.c_str());
    return false;
  }

  TiXmlElement* pSet = pRootElement->FirstChildElement("stream");

  while ( pSet )
  {
    // Get parameters
    CStdString url = GetString( pSet, "url" );
    CStdString name = GetString( pSet, "name" );
    CStdString category = GetString( pSet, "category" );
    CStdString lang = GetString( pSet, "lang" );
    CStdString channel = GetString( pSet, "channel" );
    CStdString lockpass = GetString( pSet, "lockpassword" );

    // If url is empty, it doesn't make any sense
    if ( !url.IsEmpty() )
    {
       // If the name is empty, use url
       if ( name.IsEmpty() )
         name = url;

       // Append language to the name, and also set as metadata
       if ( !lang.IsEmpty() )
         name += " [" + lang + "]";

       CStdString info = name;
       CFileItemPtr newItem( new CFileItem(info) );
       newItem->m_strPath = url;

       // Set language as metadata
       if ( !lang.IsEmpty() )
         newItem->SetProperty("language", lang );

       // Set category as metadata
       if ( !category.IsEmpty() )
         newItem->SetProperty("category", category );

       // Set channel as extra info and as metadata
       if ( !channel.IsEmpty() )
       {
         newItem->SetProperty("remotechannel", channel );
         newItem->SetExtraInfo( "Channel: " + channel );
       }

       if ( !lockpass.IsEmpty() )
       {
         newItem->m_strLockCode = lockpass;
         newItem->m_iHasLock = 2;
         newItem->m_iLockMode = LOCK_MODE_NUMERIC;
       }

       Add(newItem);
    }
    else
       CLog::Log(LOGERROR, "Playlist entry %s in file %s has missing <url> tag", name.c_str(), strFileName.c_str());

    pSet = pSet->NextSiblingElement("stream");
  }

  return true;
}


void CPlayListXML::Save(const CStdString& strFileName) const
{
  if (!m_vecItems.size()) return ;
  CStdString strPlaylist = CUtil::MakeLegalPath(strFileName);
  CFile file;
  if (!file.OpenForWrite(strPlaylist, true))
  {
    CLog::Log(LOGERROR, "Could not save WPL playlist: [%s]", strPlaylist.c_str());
    return ;
  }
  CStdString write;
  write.AppendFormat("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n");
  write.AppendFormat("<streams>\n");
  for (int i = 0; i < (int)m_vecItems.size(); ++i)
  {
    CFileItemPtr item = m_vecItems[i];
    write.AppendFormat("  <stream>\n" );
    write.AppendFormat("    <url>%s</url>", item->m_strPath.c_str() );
    write.AppendFormat("    <name>%s</name>", item->GetLabel().c_str() );

    if ( !item->GetProperty("language").IsEmpty() )
      write.AppendFormat("    <lang>%s</lang>", item->GetProperty("language").c_str() );

    if ( !item->GetProperty("category").IsEmpty() )
      write.AppendFormat("    <category>%s</category>", item->GetProperty("category").c_str() );

    if ( !item->GetProperty("remotechannel").IsEmpty() )
      write.AppendFormat("    <channel>%s</channel>", item->GetProperty("remotechannel").c_str() );

    if ( item->m_iHasLock > 0 )
      write.AppendFormat("    <lockpassword>%s<lockpassword>", item->m_strLockCode.c_str() );

    write.AppendFormat("  </stream>\n\n" );
  }

  write.AppendFormat("</streams>\n");
  file.Write(write.c_str(), write.size());
  file.Close();
}