summaryrefslogtreecommitdiffstats
path: root/src/app/playlistFile.cpp
blob: 83b1f5d35bf9bc484cc23ce8181e96891e777994 (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
// (C) 2005 Max Howell (max.howell@methylblue.com)
// See COPYING file for licensing information


//TODO error messages that vary depending on if the file is remote or not


#include "codeine.h"
#include "debug.h"
#include <tdeio/netaccess.h>
#include "playlistFile.h"
#include <ntqfile.h>
#include <ntqtextstream.h>
#include <mxcl.library.h>


PlaylistFile::PlaylistFile( const KURL &url )
      : m_url( url )
      , m_isRemoteFile( !url.isLocalFile() )
      , m_isValid( false )
{
   mxcl::WaitCursor allocateOnStack;

   TQString &path = m_path = url.path();

   if( path.endsWith( ".pls", false ) )
      m_type = PLS; else
   if( path.endsWith( ".m3u", false ) )
      m_type = M3U;
   else {
      m_type = Unknown;
      m_error = i18n( "The file is not a playlist" );
      return;
   }

   if( m_isRemoteFile ) {
      path = TQString();
      if( !TDEIO::NetAccess::download( url, path, Codeine::mainWindow() ) ) {
         m_error = i18n( "Codeine could not download the remote playlist: %1" ).arg( url.prettyURL() );
         return;
      }
   }

   TQFile file( path );
   if( file.open( IO_ReadOnly ) ) {
      TQTextStream stream( &file );
      switch( m_type ) {
         case M3U: parseM3uFile( stream ); break;
         case PLS: parsePlsFile( stream ); break;
         default: ;
      }

      if( m_contents.isEmpty() )
         m_error = i18n( "<qt>The playlist, <i>'%1'</i>, could not be interpreted. Perhaps it is empty?" ).arg( path ),
         m_isValid = false;
   }
   else
      m_error = i18n( "Codeine could not open the file: %1" ).arg( path );
}


PlaylistFile::~PlaylistFile()
{
   if( m_isRemoteFile )
      TDEIO::NetAccess::removeTempFile( m_path );
}


void
PlaylistFile::parsePlsFile( TQTextStream &stream )
{
   DEBUG_BLOCK

   for( TQString line = stream.readLine(); !line.isNull(); )
   {
      if( line.startsWith( "File" ) ) {
         const KURL url = line.section( '=', -1 );
         const TQString title = stream.readLine().section( '=', -1 );

         debug() << url << endl << title << endl;

         m_contents += url;
         m_isValid = true;

         return; //TODO continue for all urls
      }
      line = stream.readLine();
   }
}


void
PlaylistFile::parseM3uFile( TQTextStream &stream )
{
   DEBUG_BLOCK

   for( TQString line; !stream.atEnd(); )
   {
      line = stream.readLine();

      if( line.startsWith( "#EXTINF", false ) )
         continue;

      else if( !line.startsWith( "#" ) && !line.isEmpty() )
      {
         KURL url;

         // KURL::isRelativeURL() expects absolute URLs to start with a protocol, so prepend it if missing
         if( line.startsWith( "/" ) )
            line.prepend( "file://" );

         if( KURL::isRelativeURL( line ) )
            url.setPath( m_url.directory() + line );
         else
            url = KURL::fromPathOrURL( line );

         m_contents += url;
         m_isValid = true;

         return;
      }
   }
}