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