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 ***************************************************************************/
31 SLManager::SLManager()
36 createTemporaryCollection();
39 SLManager::SLManager(SLManager
&src
)
44 SongListNode
*srcSL
=src
.list
;
49 i
=createCollection(srcSL
->name
);
50 tmpSL
=getCollection(i
);
52 tmpSL
->copy(*srcSL
->SL
);
55 if (src
.tempsl
!=NULL
) tempsl
=new SongList(*src
.tempsl
);
59 SLManager::~SLManager()
66 if (ptr
->SL
!=NULL
) delete ptr
->SL
;
67 if (ptr
->name
!=NULL
) delete ptr
->name
;
71 if (tempsl
!=NULL
) delete tempsl
;
75 int SLManager::createCollection(const char *name
)
78 if (name
&& nameUsed(name
))
80 printf("Name '%s' is already used\n",name
);
85 list
=new SongListNode
;
93 while (ptr
->next
!=NULL
) ptr
=ptr
->next
;
94 ptr
->next
=new SongListNode
;
103 ptr
->name
=new char[strlen(name
)+1];
104 strcpy(ptr
->name
,name
);
108 ptr
->name
=getNotUsedName();
113 char *SLManager::getNotUsedName(void)
116 trythis
=new char[100];
117 strcpy(trythis
,"No Name");
122 if (nameUsed(trythis
)) sprintf(trythis
,"No Name - %d",++tries
);
129 int SLManager::nameUsed(const char *name
)
132 SongListNode *ptr=list;
134 while ((!used)&&(ptr!=NULL))
136 if (strcmp(ptr->name,name)==0) used=1;
141 if (getCollection(name
)==NULL
) return 0;
145 void SLManager::deleteCollection(int id
)
147 if (list
==NULL
) return;
148 SongListNode
*ptr
=list
;
150 if (id
==1) list
=list
->next
;
154 while ((ptr
!=NULL
)&&(ptr
->id
!=id
))
161 printf("Trying to delete a not used id\n");
164 ptr2
->next
=ptr
->next
;
170 regenerateid(ptr2
,id
);
174 void SLManager::regenerateid(SongListNode
*sl
,int id
)
176 SongListNode
*tmp
=sl
;
186 void SLManager::changeCollectionName(int id
,const char *newname
)
189 if (nameUsed(newname
))
191 printf("Cannot change name, '%s' is already used\n",newname
);
194 SongListNode
*ptr
=list
;
195 while ((ptr
!=NULL
)&&(ptr
->id
!=id
)) ptr
=ptr
->next
;
196 if (ptr
==NULL
) return;
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
;
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
;
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
;
233 void SLManager::loadConfig(const char *filename
)
235 #ifdef GENERAL_DEBUG_MESSAGES
236 printf("Loading collections\n");
238 FILE *fh
=fopen(filename
,"rt");
241 printf("Collections cannot be loaded\n(File %s doesn't exist or can't be opened)\n",filename
);
251 if ((strlen(s
)>0)&&(s
[strlen(s
)-1]==10)) s
[strlen(s
)-1]=0;
258 if (sl
!=NULL
) sl
->setActiveSong(activeid
);
259 int id
=createCollection(&s
[1]);
260 sl
=getCollection(id
);
267 if (sl
!=NULL
) sl
->AddSong((const char *)s
);
271 if (sl
!=NULL
) sl
->setActiveSong(activeid
);
276 void SLManager::saveConfig(const char *filename
)
278 SongListNode
*ptr
=list
;
279 FILE *fh
=fopen(filename
,"wt");
282 printf("Collections couldn't be saved\n");
285 char s
[FILENAME_MAX
];
289 sprintf(s
,"=%s\n",ptr
->name
);
293 sprintf(s
,"%d\n",sl
->getActiveSongID());
297 while (!sl
->iteratorAtEnd())
299 sprintf(s
,"%s\n",sl
->getIteratorName());
303 // ptr->SL->saveList(fh);
312 SongList
*SLManager::createTemporaryCollection(void)
315 tempsl
=new SongList();