summaryrefslogtreecommitdiff
path: root/mg_playlist.c
blob: a63519e4b36e7f2bd49a72ef58a767d3a7848166 (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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*! 
 * \file   mg_playlist.c
 * \brief  defines functions to be executed on playlists for the vdr muggle plugin
 *
 * \version $Revision: 1.6 $
 * \date    $Date$
 * \author  Ralf Klueber, Lars von Wedel, Andreas Kellner
 * \author  Responsible author: $Author$
 *
 * This file implements the class mgPlaylist which maintains a playlist
 * and supports editing (e.g. adding or moving tracks), navigating it
 * (e.g. obtaining arbitrary items or accessing them sequentially) or
 * making it persistent in some database.
 */

#include <stdio.h>
#include "mg_playlist.h"
#include "mg_tools.h"

#include <vector>
#include <iostream>


/* ==== constructors ==== */

mgPlaylist::mgPlaylist()
{
  m_current_idx = -1;

  char *buffer;
  asprintf( &buffer, "Playlist-%ld", random() );
  
  m_listname = buffer;
}

mgPlaylist::mgPlaylist(std::string listname)
{
  m_current_idx = -1;
  m_listname = listname;
}
     
/* ==== destructor ==== */

mgPlaylist::~mgPlaylist()
{
}

void mgPlaylist::toggleShuffle()
{
}

void mgPlaylist::toggleLoop()
{
}

void mgPlaylist::initialize()
{
  m_current_idx = 0;
}

/* ==== add/remove tracks ==== */

/* adds a song at the end of the playlist */
void mgPlaylist::append(mgContentItem* item)
{
  m_list.push_back(item);
}

/* append a list of tracks at the end of the playlist */
void mgPlaylist::appendList( std::vector<mgContentItem*> *tracks )
{
  std::vector<mgContentItem*>::iterator iter;
  
  mgDebug( 3, "Adding %d tracks to the playlist", tracks->size() ); 
  
  for( iter = tracks->begin(); iter != tracks->end(); iter++ )
    {
      m_list.push_back(*iter);
    }
  
  // TODO: why is this vector cleared? shouldn't the caller take care of this? LVW
  tracks->clear();
}

/* add a song after 'position' */
void mgPlaylist::insert( mgContentItem* item, unsigned int position )
{
  if( position >= m_list.size() )
    {
      m_list.push_back(item);
    }
  else
    {
      m_list.insert( m_list.begin() + position, item );
    }
}

bool mgPlaylist::remove( unsigned int pos )
{
  bool result = false;

  if( pos != m_current_idx )
    {
      result = mgTracklist::remove( pos );

      if( result && pos < m_current_idx )
	{
	  m_current_idx --;
	}
    }

  return result;
}

void mgPlaylist::clear()
{
  // TODO: who takes care of memory allocation/deallocation of mgItems?
  
  std::vector<mgContentItem*>::iterator iter;
  
  for( iter = m_list.begin(); iter != m_list.end(); iter++ )
    { // delete each item in the list
      delete *iter;
    }
  
  // finally clear the list itself
  m_list.clear();

  // reset index
  m_current_idx = 0;
}

void mgPlaylist::move( int from, int to )
{
  std::vector<mgContentItem*>::iterator from_iter = m_list.begin() + from;  
  std::vector<mgContentItem*>::iterator to_iter   = m_list.begin() + to;

  m_list.insert( to_iter, *from_iter);
  m_list.erase( from_iter );

  if( from < m_current_idx )
    {
      m_current_idx--;
    }
  if( to < m_current_idx )
    {
      m_current_idx++;
    }
}

/*====  access tracks ====*/
std::string mgPlaylist::getListname() 
{ 
  return m_listname; 
}

void mgPlaylist::setListname(std::string name)
{ 
  m_listname = name;
}

// returns the count of items in the list
int mgPlaylist::count()
{ 
  return m_list.size();
}

// returns the current item of the list
mgContentItem* mgPlaylist::getCurrent()
{
  mgContentItem *result;

  if( 0 <= m_current_idx && m_current_idx < m_list.size() )
    {
      result = *( m_list.begin() + m_current_idx );
    }
  else
    {
      result = &(mgContentItem::UNDEFINED);
    }

  return result;
}

// returns the nth track from the playlist
bool mgPlaylist::gotoPosition(unsigned int position)
{
  bool result = false;

  if( position < m_list.size() )
    {
      m_current_idx = position;
      result = true;
    }

  return result;
}

// proceeds to the next item
bool mgPlaylist::skipFwd()
{
  bool result = false;

  if( m_current_idx + 1 < m_list.size() ) // unless loop mode
    {
      m_current_idx ++;
      result = true;
    }

  // if we are already at the end -- just do nothing unless in loop mode
  return result;
}

// goes back to the previous item
bool mgPlaylist::skipBack()
{
  bool result = false;

  if( m_current_idx > 0 )
    {
      m_current_idx --;
      result = true;
    }

  // if we are at the beginning -- just do nothing unless in loop mode
  return result;
}

// get next track, do not update data structures
mgContentItem* mgPlaylist::sneakNext()
{
  if( m_current_idx + 1 <= m_list.size() ) // unless loop mode
    {
      return *(m_list.begin() + m_current_idx + 1);
    }
  else
    {
      return &(mgContentItem::UNDEFINED);
    }
}

bool mgPlaylist::exportM3U( std::string m3u_file )
{
  std::vector<mgContentItem*>::iterator iter;
  bool result = true;

  // open a file for writing
  FILE *listfile = fopen( m3u_file.c_str(), "w" );

  if( !listfile )
    {
      return false;
    }

  fprintf( listfile, "#EXTM3U" );

  for( iter = m_list.begin(); iter != m_list.end(); iter++ )
    { //  each item in the list
      fprintf( listfile, "#EXTINF:0,%s\n", (*iter)->getLabel().c_str() );
      fprintf( listfile, "%s", (*iter)->getSourceFile().c_str() );
    }

  fclose( listfile );

  return result;
}