Fairly large overhaul of the JuK codebase to beat out a lot of the Qt 3 stuff.
[kdemultimedia.git] / kmid / songlist.cpp
blobcc2572d8383a3fc85f0fa6124820e16dba4d7606
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 ***************************************************************************/
24 #include "songlist.h"
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
29 SongList::SongList(void)
31 list=NULL;
32 last=NULL;
33 active=NULL;
34 ntotal=0;
37 SongList::SongList(SongList &src)
39 list=last=active=NULL;
40 ntotal=0;
41 src.iteratorStart();
42 while (!src.iteratorAtEnd())
44 AddSong(src.getIteratorName());
45 src.iteratorNext();
47 if (src.active!=NULL) active=getSongid(src.active->id);
50 SongList::~SongList()
52 clean();
53 Song *ptr=list;
54 active=last=NULL;
55 ntotal=0;
57 while (ptr!=NULL)
59 list=ptr->next;
60 delete ptr->name;
61 delete ptr;
62 ptr=list;
67 SongList::Song *SongList::getSongid(int id)
69 Song *ptr=list;
70 while ((ptr!=NULL)&&(ptr->id!=id))
71 ptr=ptr->next;
73 return ptr;
76 int SongList::AddSong(const char *song)
78 if (!song) return 0;
80 if (last==NULL)
82 last=new Song;
83 list=last;
85 else
87 last->next=new Song;
88 last=last->next;
90 last->name=new char[strlen(song)+1];
91 strcpy(last->name,song);
92 last->id= ++ntotal;
93 last->next=NULL;
94 if (active==NULL) active=last;
95 return last->id;
98 void SongList::DelSong(int id)
100 Song *ptr;
102 if (list==NULL) return;
103 if (id==1)
105 if (last->id==1)
107 list=last=active=NULL;
108 ntotal=0;
110 else
112 ptr=list;
113 if (active->id==1) active=list->next;
114 list=list->next;
115 delete ptr->name;
116 delete ptr;
117 ntotal--;
119 regenerateid(list,1);
121 return;
123 Song *ptr_prev=getSongid(id-1);
124 ptr=ptr_prev->next;
125 if (last->id==id) last=ptr_prev;
126 if (active->id==id)
127 if (active->next!=NULL) active=active->next;
128 else active=ptr_prev;
130 ntotal--;
131 ptr_prev->next=ptr->next;
132 delete ptr->name;
133 delete ptr;
134 regenerateid(ptr_prev->next,id);
138 void SongList::regenerateid(Song *song,int id)
140 Song *tmp=song;
141 int i=id;
142 while (tmp!=NULL)
144 tmp->id=i++;
145 tmp=tmp->next;
147 ntotal=i-1;
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;
160 return NULL;
164 void SongList::saveList(FILE *fh)
166 Song *ptr=list;
167 while (ptr!=NULL)
169 fputs(fh,ptr->name);
170 ptr=ptr->next;
175 void SongList::iteratorStart(void)
177 it=list;
180 void SongList::iteratorNext(void)
182 if (it!=NULL) it=it->next;
185 int SongList::getIteratorID(void)
187 if (it==NULL) return -1;
188 return it->id;
191 char *SongList::getIteratorName(void)
193 if (it==NULL) return NULL;
194 return it->name;
198 void SongList::clean(void)
200 Song *tmp=list;
201 active=last=NULL;
202 ntotal=0;
204 while (tmp!=NULL)
206 list=tmp->next;
207 delete [] tmp->name;
208 delete tmp;
209 tmp=list;
213 void SongList::copy(SongList &src)
215 clean();
216 src.iteratorStart();
217 while (!src.iteratorAtEnd())
219 AddSong(src.getIteratorName());
220 src.iteratorNext();
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;
229 if (active==NULL)
231 Song *tmp=list;
232 while (tmp->next!=NULL) tmp=tmp->next;
233 active=tmp;
234 return 0;
236 return 1;
240 void SongList::previous(void)
242 if (list==NULL) {active=NULL;return;};
243 Song *tmp=list;
244 while ((tmp->next!=NULL)&&(tmp->next->id!=active->id)) tmp=tmp->next;
245 if (tmp->next==NULL) {active=list;return;};
246 active=tmp;