Fairly large overhaul of the JuK codebase to beat out a lot of the Qt 3 stuff.
[kdemultimedia.git] / kmid / slman.cpp
blob0ca14613a7cf4386e7e417215ff6485d9522d6b3
1 /**************************************************************************
3 slman.cc - SongList Manager, which holds a set of collections (SongLists)
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 "slman.h"
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include "version.h"
29 #include <klocale.h>
31 SLManager::SLManager()
33 list=NULL;
34 ntotal=0;
35 tempsl=NULL;
36 createTemporaryCollection();
39 SLManager::SLManager(SLManager &src)
41 list=NULL;
42 ntotal=0;
43 tempsl=NULL;
44 SongListNode *srcSL=src.list;
45 SongList *tmpSL;
46 int i;
47 while (srcSL!=NULL)
49 i=createCollection(srcSL->name);
50 tmpSL=getCollection(i);
51 if (tmpSL!=NULL)
52 tmpSL->copy(*srcSL->SL);
53 srcSL=srcSL->next;
55 if (src.tempsl!=NULL) tempsl=new SongList(*src.tempsl);
56 else tempsl=NULL;
59 SLManager::~SLManager()
61 SongListNode *ptr;
62 ptr=list;
63 while (ptr!=NULL)
65 list=ptr->next;
66 if (ptr->SL!=NULL) delete ptr->SL;
67 if (ptr->name!=NULL) delete ptr->name;
68 delete ptr;
69 ptr=list;
71 if (tempsl!=NULL) delete tempsl;
72 ntotal=0;
75 int SLManager::createCollection(const char *name)
77 SongListNode *ptr;
78 if (name && nameUsed(name))
80 printf("Name '%s' is already used\n",name);
81 return -1;
83 if (list==NULL)
85 list=new SongListNode;
86 list->id=1;
87 ntotal=1;
88 ptr=list;
90 else
92 ptr=list;
93 while (ptr->next!=NULL) ptr=ptr->next;
94 ptr->next=new SongListNode;
95 ptr=ptr->next;
97 ptr->id= ++ntotal;
99 ptr->SL=new SongList;
100 ptr->next=NULL;
101 if (name!=NULL)
103 ptr->name=new char[strlen(name)+1];
104 strcpy(ptr->name,name);
106 else
108 ptr->name=getNotUsedName();
110 return ptr->id;
113 char *SLManager::getNotUsedName(void)
115 char *trythis;
116 trythis=new char[100];
117 strcpy(trythis,"No Name");
118 int tries=1;
119 int success=0;
120 while (!success)
122 if (nameUsed(trythis)) sprintf(trythis,"No Name - %d",++tries);
123 else
124 success=1;
126 return trythis;
129 int SLManager::nameUsed(const char *name)
132 SongListNode *ptr=list;
133 int used=0;
134 while ((!used)&&(ptr!=NULL))
136 if (strcmp(ptr->name,name)==0) used=1;
137 ptr=ptr->next;
139 return used;
141 if (getCollection(name)==NULL) return 0;
142 return 1;
145 void SLManager::deleteCollection(int id)
147 if (list==NULL) return;
148 SongListNode *ptr=list;
149 SongListNode *ptr2;
150 if (id==1) list=list->next;
151 else
153 ptr2=list;
154 while ((ptr!=NULL)&&(ptr->id!=id))
156 ptr2=ptr;
157 ptr=ptr->next;
159 if (ptr==NULL)
161 printf("Trying to delete a not used id\n");
162 return;
164 ptr2->next=ptr->next;
166 ptr2=ptr->next;
167 delete ptr->SL;
168 delete ptr->name;
169 delete ptr;
170 regenerateid(ptr2,id);
174 void SLManager::regenerateid(SongListNode *sl,int id)
176 SongListNode *tmp=sl;
177 int i=id;
178 while (tmp!=NULL)
180 tmp->id=i++;
181 tmp=tmp->next;
183 ntotal=i-1;
186 void SLManager::changeCollectionName(int id,const char *newname)
188 if (id<1) return;
189 if (nameUsed(newname))
191 printf("Cannot change name, '%s' is already used\n",newname);
192 return;
194 SongListNode *ptr=list;
195 while ((ptr!=NULL)&&(ptr->id!=id)) ptr=ptr->next;
196 if (ptr==NULL) return;
198 delete ptr->name;
199 ptr->name=new char[strlen(newname)+1];
200 strcpy(ptr->name,newname);
203 SongList *SLManager::getCollection(int id)
205 if (id==0) return tempsl;
207 SongListNode *ptr=list;
208 while ((ptr!=NULL)&&(ptr->id!=id)) ptr=ptr->next;
210 if (ptr==NULL) return NULL;
211 return ptr->SL;
214 SongList *SLManager::getCollection(const char *name)
216 SongListNode *ptr=list;
217 while ((ptr!=NULL)&&(strcmp(ptr->name,name)!=0)) ptr=ptr->next;
219 if (ptr==NULL) return NULL;
220 return ptr->SL;
223 const char *SLManager::getCollectionName(int id)
225 if (id==0) return I18N_NOOP("Temporary Collection");
226 SongListNode *ptr=list;
227 while ((ptr!=NULL)&&(ptr->id!=id)) ptr=ptr->next;
229 if (ptr==NULL) return NULL;
230 return ptr->name;
233 void SLManager::loadConfig(const char *filename)
235 #ifdef GENERAL_DEBUG_MESSAGES
236 printf("Loading collections\n");
237 #endif
238 FILE *fh=fopen(filename,"rt");
239 if (fh==NULL)
241 printf("Collections cannot be loaded\n(File %s doesn't exist or can't be opened)\n",filename);
242 return;
244 char s[300];
245 SongList *sl=NULL;
246 int activeid=0;
247 while (!feof(fh))
249 s[0] = 0;
250 fgets(s,299,fh);
251 if ((strlen(s)>0)&&(s[strlen(s)-1]==10)) s[strlen(s)-1]=0;
252 switch (s[0])
254 case (0) : break;
255 case (10) : break;
256 case ('=') :
258 if (sl!=NULL) sl->setActiveSong(activeid);
259 int id=createCollection(&s[1]);
260 sl=getCollection(id);
261 fgets(s,299,fh);
262 activeid=atoi(s);
264 break;
265 default :
267 if (sl!=NULL) sl->AddSong((const char *)s);
271 if (sl!=NULL) sl->setActiveSong(activeid);
273 fclose(fh);
276 void SLManager::saveConfig(const char *filename)
278 SongListNode *ptr=list;
279 FILE *fh=fopen(filename,"wt");
280 if (fh==NULL)
282 printf("Collections couldn't be saved\n");
283 return;
285 char s[FILENAME_MAX];
286 SongList *sl;
287 while (ptr!=NULL)
289 sprintf(s,"=%s\n",ptr->name);
290 fputs(s,fh);
292 sl=ptr->SL;
293 sprintf(s,"%d\n",sl->getActiveSongID());
294 fputs(s,fh);
296 sl->iteratorStart();
297 while (!sl->iteratorAtEnd())
299 sprintf(s,"%s\n",sl->getIteratorName());
300 fputs(s,fh);
301 sl->iteratorNext();
303 // ptr->SL->saveList(fh);
304 fputs("\n",fh);
306 ptr=ptr->next;
308 fclose(fh);
312 SongList *SLManager::createTemporaryCollection(void)
314 if (tempsl==NULL)
315 tempsl=new SongList();
316 else
317 tempsl->clean();
319 return tempsl;