Code

apply whitespace cosmetics to the license comments
[ncmpc.git] / src / filelist.c
index 2ce928cd4f73d7388bfb675a42d90b2896d41d3c..5138fdbcee86f25d65eb8e0941eb43a6ef2dc172 100644 (file)
@@ -1,21 +1,21 @@
 /* ncmpc (Ncurses MPD Client)
- * (c) 2004-2009 The Music Player Daemon Project
+ * (c) 2004-2010 The Music Player Daemon Project
  * Project homepage: http://musicpd.org
+ *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version.
-
+ *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
-
+ *
  * You should have received a copy of the GNU General Public License along
  * with this program; if not, write to the Free Software Foundation, Inc.,
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
-*/
+ */
 
 #include "filelist.h"
 
@@ -66,28 +66,6 @@ filelist_append(struct filelist *filelist, struct mpd_entity *entity)
        return entry;
 }
 
-struct filelist_entry *
-filelist_prepend(struct filelist *filelist, struct mpd_entity *entity)
-{
-       struct filelist_entry *entry = filelist_append(filelist, entity);
-
-       /* this is very slow, but we should optimize screen_artist.c
-          later so that this function can be removed, so I'm not in
-          the mood to implement something better here */
-
-       if (!filelist_is_empty(filelist)) {
-               guint i;
-
-               for (i = filelist_length(filelist) - 1; i > 0; --i)
-                       g_ptr_array_index(filelist->entries, i) =
-                               filelist_get(filelist, i - 1);
-
-               g_ptr_array_index(filelist->entries, 0) = entry;
-       }
-
-       return entry;
-}
-
 void
 filelist_move(struct filelist *filelist, struct filelist *from)
 {
@@ -110,6 +88,35 @@ filelist_compare_indirect(gconstpointer ap, gconstpointer bp, gpointer data)
        return compare_func(a, b);
 }
 
+gint
+compare_filelist_entry_path(gconstpointer filelist_entry1,
+                           gconstpointer filelist_entry2)
+{
+       const struct mpd_entity *e1, *e2;
+       int n = 0;
+
+       e1 = ((const struct filelist_entry *)filelist_entry1)->entity;
+       e2 = ((const struct filelist_entry *)filelist_entry2)->entity;
+
+       if (e1 != NULL && e2 != NULL &&
+           mpd_entity_get_type(e1) == mpd_entity_get_type(e2)) {
+               switch (mpd_entity_get_type(e1)) {
+               case MPD_ENTITY_TYPE_UNKNOWN:
+                       break;
+               case MPD_ENTITY_TYPE_DIRECTORY:
+                       n = g_utf8_collate(mpd_directory_get_path(mpd_entity_get_directory(e1)),
+                                          mpd_directory_get_path(mpd_entity_get_directory(e2)));
+                       break;
+               case MPD_ENTITY_TYPE_SONG:
+                       break;
+               case MPD_ENTITY_TYPE_PLAYLIST:
+                       n = g_utf8_collate(mpd_playlist_get_path(mpd_entity_get_playlist(e1)),
+                                          mpd_playlist_get_path(mpd_entity_get_playlist(e2)));
+               }
+       }
+       return n;
+}
+
 /* Sorts the whole filelist, at the moment used by filelist_search */
 void
 filelist_sort_all(struct filelist *filelist, GCompareFunc compare_func)
@@ -132,15 +139,11 @@ filelist_sort_dir_play(struct filelist *filelist, GCompareFunc compare_func)
 
        if (filelist->entries->len < 2)
                return;
+
+       /* If the first entry is NULL, skip it, because NULL stands for "[..]" */
        iter = ((struct filelist_entry*) g_ptr_array_index(filelist->entries, 0))->entity;
-       /* This can only happen at the beginning of the filelist,
-        * because NULL stands for "[..]" */
-       if (iter == NULL) {
-               iter = ((struct filelist_entry*) g_ptr_array_index(filelist->entries, 1))->entity;
-               first = 1;
-       }
-       else
-               first = 0;
+       first = (iter == NULL)? 1 : 0;
+
        /* find the last directory entry */
        for (last = first+1; last < filelist->entries->len; last++) {
                iter = ((struct filelist_entry*) g_ptr_array_index(filelist->entries, last))->entity;
@@ -167,6 +170,26 @@ filelist_sort_dir_play(struct filelist *filelist, GCompareFunc compare_func)
                                filelist_compare_indirect, compare_func);
 }
 
+void
+filelist_no_duplicates(struct filelist *filelist)
+{
+       for (int i = filelist_length(filelist) - 1; i >= 0; --i) {
+               struct filelist_entry *entry = filelist_get(filelist, i);
+               const struct mpd_song *song;
+
+               if (entry->entity == NULL ||
+                   mpd_entity_get_type(entry->entity) != MPD_ENTITY_TYPE_SONG)
+                       continue;
+
+               song = mpd_entity_get_song(entry->entity);
+               if (filelist_find_song(filelist, song) < i) {
+                       g_ptr_array_remove_index(filelist->entries, i);
+                       mpd_entity_free(entry->entity);
+                       g_slice_free(struct filelist_entry, entry);
+               }
+       }
+}
+
 static bool
 same_song(const struct mpd_song *a, const struct mpd_song *b)
 {