Mercurial > mplayer.hg
annotate gui/util/list.c @ 35962:df731f25759e
Add new listMgr command PLAYLIST_ITEM_GET_LAST.
Patch by Hans-Dieter Kosch, hdkosch kabelbw de.
author | ib |
---|---|
date | Mon, 25 Mar 2013 00:44:28 +0000 |
parents | cc6e25e348ee |
children | 319cbb4d0967 |
rev | line source |
---|---|
33741 | 1 /* |
2 * This file is part of MPlayer. | |
3 * | |
4 * MPlayer is free software; you can redistribute it and/or modify | |
5 * it under the terms of the GNU General Public License as published by | |
6 * the Free Software Foundation; either version 2 of the License, or | |
7 * (at your option) any later version. | |
8 * | |
9 * MPlayer is distributed in the hope that it will be useful, | |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 * GNU General Public License for more details. | |
13 * | |
14 * You should have received a copy of the GNU General Public License along | |
15 * with MPlayer; if not, write to the Free Software Foundation, Inc., | |
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
17 */ | |
18 | |
34683 | 19 /** |
20 * @file | |
21 * @brief List management | |
22 */ | |
23 | |
33741 | 24 #include <stdlib.h> |
25 #include <string.h> | |
26 | |
27 #include "list.h" | |
35530 | 28 #include "string.h" |
35525 | 29 #include "gui/app/gui.h" |
33741 | 30 |
35376 | 31 #include "mp_msg.h" |
32 #include "path.h" | |
33 | |
34667 | 34 static plItem *plList; |
34664
4df4d842d5fb
Remove global variable pointing to current playlist item.
ib
parents:
34663
diff
changeset
|
35 static plItem *plCurrent; |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
36 |
34668 | 37 static urlItem *urlList; |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
38 |
34683 | 39 /** |
40 * @brief Manage playlists and URL lists. | |
41 * | |
42 * @param cmd task to be performed | |
43 * @param data list item for the task | |
44 * | |
45 * @return pointer to top of list (GET command), | |
46 * pointer to current list item (ITEM command) or | |
47 * NULL (DELETE or unknown command) | |
48 * | |
35487
58a221f73d75
Enhance PLAYLIST_ITEM_GET_POS to provide total number of items.
ib
parents:
35460
diff
changeset
|
49 * @note PLAYLIST_ITEM_GET_POS returns the position number as pointer |
58a221f73d75
Enhance PLAYLIST_ITEM_GET_POS to provide total number of items.
ib
parents:
35460
diff
changeset
|
50 * (if @a data is NULL the last position number, i.e. number of items), |
35458 | 51 * and position 0 means "not found" |
34683 | 52 */ |
34610 | 53 void *listMgr(int cmd, void *data) |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
54 { |
35458 | 55 unsigned int pos; |
34673 | 56 plItem *pdat = (plItem *)data; |
57 urlItem *udat = (urlItem *)data; | |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
58 |
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
59 switch (cmd) { |
34684 | 60 /* playlist */ |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
61 |
34667 | 62 case PLAYLIST_GET: |
63 | |
64 return plList; | |
65 | |
34681 | 66 case PLAYLIST_ITEM_APPEND: |
34674 | 67 |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
68 if (plList) { |
34673 | 69 plItem *item = plList; |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
70 |
34673 | 71 while (item->next) |
72 item = item->next; | |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
73 |
34673 | 74 item->next = pdat; |
75 pdat->prev = item; | |
76 pdat->next = NULL; | |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
77 } else { |
34674 | 78 pdat->next = pdat->prev = NULL; |
34673 | 79 plCurrent = plList = pdat; |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
80 } |
34674 | 81 |
82 return plCurrent; | |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
83 |
34663 | 84 case PLAYLIST_ITEM_INSERT: |
34675 | 85 |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
86 if (plCurrent) { |
34675 | 87 pdat->next = plCurrent->next; |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
88 |
34673 | 89 if (pdat->next) |
90 pdat->next->prev = pdat; | |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
91 |
34675 | 92 pdat->prev = plCurrent; |
93 plCurrent->next = pdat; | |
94 | |
95 plCurrent = pdat; | |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
96 |
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
97 return plCurrent; |
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
98 } else |
34681 | 99 return listMgr(PLAYLIST_ITEM_APPEND, pdat); |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
100 |
35460 | 101 case PLAYLIST_ITEM_FIND: |
102 | |
103 if (plList) { | |
104 plItem *item = plList; | |
105 | |
106 do { | |
107 if (gstrcmp(item->path, pdat->path) == 0 && gstrcmp(item->name, pdat->name) == 0) | |
108 return item; | |
109 | |
110 item = item->next; | |
111 } while (item); | |
112 } | |
113 | |
114 return NULL; | |
115 | |
34682
c3ab7bd64ab3
Cosmetic: Arrange listMgr() commands in switch statement.
ib
parents:
34681
diff
changeset
|
116 case PLAYLIST_ITEM_SET_CURR: |
34676
207272df4aef
Cosmetic: Insert some blank lines and remove commented code.
ib
parents:
34675
diff
changeset
|
117 |
34682
c3ab7bd64ab3
Cosmetic: Arrange listMgr() commands in switch statement.
ib
parents:
34681
diff
changeset
|
118 plCurrent = pdat; |
c3ab7bd64ab3
Cosmetic: Arrange listMgr() commands in switch statement.
ib
parents:
34681
diff
changeset
|
119 return plCurrent; |
34676
207272df4aef
Cosmetic: Insert some blank lines and remove commented code.
ib
parents:
34675
diff
changeset
|
120 |
34682
c3ab7bd64ab3
Cosmetic: Arrange listMgr() commands in switch statement.
ib
parents:
34681
diff
changeset
|
121 case PLAYLIST_ITEM_GET_CURR: |
c3ab7bd64ab3
Cosmetic: Arrange listMgr() commands in switch statement.
ib
parents:
34681
diff
changeset
|
122 |
c3ab7bd64ab3
Cosmetic: Arrange listMgr() commands in switch statement.
ib
parents:
34681
diff
changeset
|
123 return plCurrent; |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
124 |
35458 | 125 case PLAYLIST_ITEM_GET_POS: |
126 | |
127 pos = 0; | |
128 | |
129 if (plList) { | |
130 unsigned int i = 0; | |
131 plItem *item = plList; | |
132 | |
133 do { | |
134 i++; | |
135 | |
136 if (item == pdat) { | |
137 pos = i; | |
138 break; | |
139 } | |
140 | |
141 item = item->next; | |
142 } while (item); | |
35487
58a221f73d75
Enhance PLAYLIST_ITEM_GET_POS to provide total number of items.
ib
parents:
35460
diff
changeset
|
143 |
58a221f73d75
Enhance PLAYLIST_ITEM_GET_POS to provide total number of items.
ib
parents:
35460
diff
changeset
|
144 if (!pdat) |
58a221f73d75
Enhance PLAYLIST_ITEM_GET_POS to provide total number of items.
ib
parents:
35460
diff
changeset
|
145 pos = i; |
35458 | 146 } |
147 | |
148 return (void *)pos; | |
149 | |
34663 | 150 case PLAYLIST_ITEM_GET_PREV: |
34676
207272df4aef
Cosmetic: Insert some blank lines and remove commented code.
ib
parents:
34675
diff
changeset
|
151 |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
152 if (plCurrent && plCurrent->prev) { |
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
153 plCurrent = plCurrent->prev; |
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
154 return plCurrent; |
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
155 } |
34676
207272df4aef
Cosmetic: Insert some blank lines and remove commented code.
ib
parents:
34675
diff
changeset
|
156 |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
157 return NULL; |
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
158 |
34682
c3ab7bd64ab3
Cosmetic: Arrange listMgr() commands in switch statement.
ib
parents:
34681
diff
changeset
|
159 case PLAYLIST_ITEM_GET_NEXT: |
34676
207272df4aef
Cosmetic: Insert some blank lines and remove commented code.
ib
parents:
34675
diff
changeset
|
160 |
34682
c3ab7bd64ab3
Cosmetic: Arrange listMgr() commands in switch statement.
ib
parents:
34681
diff
changeset
|
161 if (plCurrent && plCurrent->next) { |
c3ab7bd64ab3
Cosmetic: Arrange listMgr() commands in switch statement.
ib
parents:
34681
diff
changeset
|
162 plCurrent = plCurrent->next; |
c3ab7bd64ab3
Cosmetic: Arrange listMgr() commands in switch statement.
ib
parents:
34681
diff
changeset
|
163 return plCurrent; |
c3ab7bd64ab3
Cosmetic: Arrange listMgr() commands in switch statement.
ib
parents:
34681
diff
changeset
|
164 } |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
165 |
34682
c3ab7bd64ab3
Cosmetic: Arrange listMgr() commands in switch statement.
ib
parents:
34681
diff
changeset
|
166 return NULL; |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
167 |
35962 | 168 case PLAYLIST_ITEM_GET_LAST: |
169 | |
170 if (plList) { | |
171 plItem *item = plList; | |
172 | |
173 while (item->next) | |
174 item = item->next; | |
175 | |
176 return item; | |
177 } | |
178 | |
179 return NULL; | |
180 | |
34663 | 181 case PLAYLIST_ITEM_DEL_CURR: |
34677 | 182 |
183 if (plCurrent) { | |
34678 | 184 plItem *curr = plCurrent; |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
185 |
34678 | 186 if (curr->prev) |
187 curr->prev->next = curr->next; | |
188 if (curr->next) | |
189 curr->next->prev = curr->prev; | |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
190 |
34678 | 191 plCurrent = curr->next; |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
192 |
34678 | 193 if (curr == plList) |
194 plList = plCurrent; | |
34677 | 195 |
34678 | 196 free(curr->path); |
197 free(curr->name); | |
198 free(curr); | |
199 } | |
34677 | 200 |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
201 return plCurrent; |
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
202 |
34663 | 203 case PLAYLIST_DELETE: |
34676
207272df4aef
Cosmetic: Insert some blank lines and remove commented code.
ib
parents:
34675
diff
changeset
|
204 |
34602 | 205 while (plList) { |
34673 | 206 plItem *item = plList->next; |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
207 |
34602 | 208 free(plList->path); |
209 free(plList->name); | |
210 free(plList); | |
211 | |
34673 | 212 plList = item; |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
213 } |
34676
207272df4aef
Cosmetic: Insert some blank lines and remove commented code.
ib
parents:
34675
diff
changeset
|
214 |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
215 plCurrent = NULL; |
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
216 return NULL; |
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
217 |
34684 | 218 /* URL list */ |
34668 | 219 |
220 case URLLIST_GET: | |
221 | |
222 return urlList; | |
223 | |
34663 | 224 case URLLIST_ITEM_ADD: |
34679 | 225 |
33748 | 226 if (urlList) { |
34673 | 227 urlItem *item = urlList; |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
228 |
34679 | 229 while (item) { |
230 if (strcmp(udat->url, item->url) == 0) { | |
231 free(udat->url); | |
232 free(udat); | |
233 return NULL; | |
234 } | |
235 | |
236 if (item->next) | |
237 item = item->next; | |
238 else { | |
239 item->next = udat; | |
240 udat->next = NULL; | |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
241 break; |
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
242 } |
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
243 } |
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
244 } else { |
34673 | 245 udat->next = NULL; |
246 urlList = udat; | |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
247 } |
34679 | 248 |
249 return udat; | |
34599 | 250 |
34663 | 251 case URLLIST_DELETE: |
34676
207272df4aef
Cosmetic: Insert some blank lines and remove commented code.
ib
parents:
34675
diff
changeset
|
252 |
34599 | 253 while (urlList) { |
34673 | 254 urlItem *item = urlList->next; |
34601 | 255 |
34599 | 256 free(urlList->url); |
34601 | 257 free(urlList); |
258 | |
34673 | 259 urlList = item; |
34599 | 260 } |
34676
207272df4aef
Cosmetic: Insert some blank lines and remove commented code.
ib
parents:
34675
diff
changeset
|
261 |
34599 | 262 return NULL; |
34680 | 263 |
264 default: | |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
265 |
34680 | 266 return NULL; |
267 } | |
33742
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
268 } |
e1539e14d60f
Move purely list related parts of gtkSet() from interface.c to list.c.
ib
parents:
33741
diff
changeset
|
269 |
33741 | 270 /** |
34622 | 271 * @brief Set list to @a entry. |
272 * | |
273 * @param list pointer to the char pointer list | |
274 * @param entry the new (and only) element of the list | |
275 * | |
276 * @note Actually, a new list will be created and the old list will be freed. | |
33741 | 277 */ |
34610 | 278 void listSet(char ***list, const char *entry) |
33741 | 279 { |
34622 | 280 if (*list) { |
281 char **l = *list; | |
33741 | 282 |
34622 | 283 while (*l) { |
284 free(*l); | |
285 l++; | |
286 } | |
33741 | 287 |
288 free(*list); | |
289 } | |
290 | |
34623 | 291 *list = malloc(2 * sizeof(char *)); |
34622 | 292 |
293 if (*list) { | |
34623 | 294 (*list)[0] = gstrdup(entry); |
295 (*list)[1] = NULL; | |
34622 | 296 } |
33741 | 297 } |
298 | |
299 /** | |
34627 | 300 * @brief Replace the first element in list that starts with @a search. |
301 * | |
302 * @note If no such element is found, @a replace will be appended. | |
303 * | |
304 * @param list pointer to the char pointer list | |
305 * @param search element to search | |
306 * @param replace replacement element | |
33741 | 307 */ |
34610 | 308 void listRepl(char ***list, const char *search, const char *replace) |
33741 | 309 { |
34627 | 310 int i = 0; |
311 char **org = *list; | |
312 | |
313 if (!replace) | |
314 return; | |
33741 | 315 |
316 if (*list) { | |
34627 | 317 size_t len = (search ? strlen(search) : 0); |
318 | |
33741 | 319 for (i = 0; (*list)[i]; i++) { |
34627 | 320 if (gstrncmp((*list)[i], search, len) == 0) { |
33741 | 321 free((*list)[i]); |
34627 | 322 (*list)[i] = strdup(replace); |
33741 | 323 return; |
324 } | |
325 } | |
326 | |
327 *list = realloc(*list, (i + 2) * sizeof(char *)); | |
328 } else | |
329 *list = malloc(2 * sizeof(char *)); | |
330 | |
34627 | 331 if (!*list) { |
332 *list = org; | |
333 return; | |
334 } | |
335 | |
336 (*list)[i] = strdup(replace); | |
33741 | 337 (*list)[i + 1] = NULL; |
338 } | |
35376 | 339 |
340 /** | |
341 * @brief Append or insert a file to the playlist. | |
342 * | |
343 * @param what file to be added | |
344 * @param how command (#PLAYLIST_ITEM_APPEND or #PLAYLIST_ITEM_INSERT) to be performed | |
345 * | |
35493 | 346 * @return #True (ok) or #False (error) |
35376 | 347 */ |
348 int add_to_gui_playlist(const char *what, int how) | |
349 { | |
35378 | 350 const char *file; |
351 char *path; | |
35376 | 352 plItem *item; |
353 | |
35386 | 354 if (!what || !*what || (how != PLAYLIST_ITEM_APPEND && how != PLAYLIST_ITEM_INSERT)) |
35493 | 355 return False; |
35376 | 356 |
35378 | 357 file = mp_basename(what); |
358 path = strdup(what); | |
35376 | 359 |
35378 | 360 if (file > what) |
361 path[file - what - 1] = 0; | |
35376 | 362 else |
35378 | 363 strcpy(path, "."); |
35376 | 364 |
365 item = calloc(1, sizeof(plItem)); | |
366 | |
367 if (!item) | |
35493 | 368 return False; |
35376 | 369 |
35378 | 370 mp_msg(MSGT_GPLAYER, MSGL_DBG2, "[list] adding %s/%s\n", path, file); |
35376 | 371 |
35378 | 372 item->name = strdup(file); |
373 item->path = path; | |
35376 | 374 |
375 listMgr(how, item); | |
376 | |
35493 | 377 return True; |
35376 | 378 } |