1 /**************************************************************************
3 songlist.cc - class SongList, which holds a list of songs (collection)
4 Copyright (C) 1997,98 Antonio Larrosa Jimenez
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 Send comments and bug fixes to larrosa@kde.org
21 or to Antonio Larrosa, Rio Arnoya, 10 5B, 29006 Malaga, Spain
23 ***************************************************************************/
29 SongList::SongList(void)
37 SongList::SongList(SongList
&src
)
39 list
=last
=active
=NULL
;
42 while (!src
.iteratorAtEnd())
44 AddSong(src
.getIteratorName());
47 if (src
.active
!=NULL
) active
=getSongid(src
.active
->id
);
67 SongList::Song
*SongList::getSongid(int id
)
70 while ((ptr
!=NULL
)&&(ptr
->id
!=id
))
76 int SongList::AddSong(const char *song
)
90 last
->name
=new char[strlen(song
)+1];
91 strcpy(last
->name
,song
);
94 if (active
==NULL
) active
=last
;
98 void SongList::DelSong(int id
)
102 if (list
==NULL
) return;
107 list
=last
=active
=NULL
;
113 if (active
->id
==1) active
=list
->next
;
119 regenerateid(list
,1);
123 Song
*ptr_prev
=getSongid(id
-1);
125 if (last
->id
==id
) last
=ptr_prev
;
127 if (active
->next
!=NULL
) active
=active
->next
;
128 else active
=ptr_prev
;
131 ptr_prev
->next
=ptr
->next
;
134 regenerateid(ptr_prev
->next
,id
);
138 void SongList::regenerateid(Song
*song
,int id
)
150 void SongList::setActiveSong(int id
)
152 Song
*tmp
=getSongid(id
);
153 if (tmp
!=NULL
) active
=tmp
;
156 char *SongList::getName(int id
)
158 Song
*tmp
=getSongid(id
);
159 if (tmp
!=NULL
) return tmp
->name
;
164 void SongList::saveList(FILE *fh)
175 void SongList::iteratorStart(void)
180 void SongList::iteratorNext(void)
182 if (it
!=NULL
) it
=it
->next
;
185 int SongList::getIteratorID(void)
187 if (it
==NULL
) return -1;
191 char *SongList::getIteratorName(void)
193 if (it
==NULL
) return NULL
;
198 void SongList::clean(void)
213 void SongList::copy(SongList
&src
)
217 while (!src
.iteratorAtEnd())
219 AddSong(src
.getIteratorName());
222 if (src
.active
!=NULL
) active
=getSongid(src
.active
->id
);
225 int SongList::next(void)
227 if (list
==NULL
) {active
=NULL
;return 0;};
228 if (active
!=NULL
) active
=active
->next
;
232 while (tmp
->next
!=NULL
) tmp
=tmp
->next
;
240 void SongList::previous(void)
242 if (list
==NULL
) {active
=NULL
;return;};
244 while ((tmp
->next
!=NULL
)&&(tmp
->next
->id
!=active
->id
)) tmp
=tmp
->next
;
245 if (tmp
->next
==NULL
) {active
=list
;return;};