| 1 | /* |
| 2 | * This file is part of DisOrder |
| 3 | * Copyright (C) 2008 Richard Kettlewell |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
| 18 | * USA |
| 19 | */ |
| 20 | /** @file disobedience/playlists.c |
| 21 | * @brief Playlist for Disobedience |
| 22 | * |
| 23 | */ |
| 24 | #include "disobedience.h" |
| 25 | |
| 26 | static void playlists_updated(void *v, |
| 27 | const char *err, |
| 28 | int nvec, char **vec); |
| 29 | |
| 30 | /** @brief Current list of playlists or NULL */ |
| 31 | char **playlists; |
| 32 | |
| 33 | /** @brief Count of playlists */ |
| 34 | int nplaylists; |
| 35 | |
| 36 | /** @brief Schedule an update to the list of playlists */ |
| 37 | static void playlists_update(const char attribute((unused)) *event, |
| 38 | void attribute((unused)) *eventdata, |
| 39 | void attribute((unused)) *callbackdata) { |
| 40 | disorder_eclient_playlists(client, playlists_updated, 0); |
| 41 | } |
| 42 | |
| 43 | /** @brief qsort() callback for playlist name comparison */ |
| 44 | static int playlistcmp(const void *ap, const void *bp) { |
| 45 | const char *a = *(char **)ap, *b = *(char **)bp; |
| 46 | const char *ad = strchr(a, '.'), *bd = strchr(b, '.'); |
| 47 | int c; |
| 48 | |
| 49 | /* Group owned playlists by owner */ |
| 50 | if(ad && bd) { |
| 51 | const int adn = ad - a, bdn = bd - b; |
| 52 | if((c = strncmp(a, b, adn < bdn ? adn : bdn))) |
| 53 | return c; |
| 54 | /* Lexical order within playlists of a single owner */ |
| 55 | return strcmp(ad + 1, bd + 1); |
| 56 | } |
| 57 | |
| 58 | /* Owned playlists after shared ones */ |
| 59 | if(ad) { |
| 60 | return 1; |
| 61 | } else if(bd) { |
| 62 | return -1; |
| 63 | } |
| 64 | |
| 65 | /* Lexical order of shared playlists */ |
| 66 | return strcmp(a, b); |
| 67 | } |
| 68 | |
| 69 | /** @brief Called with a new list of playlists */ |
| 70 | static void playlists_updated(void attribute((unused)) *v, |
| 71 | const char *err, |
| 72 | int nvec, char **vec) { |
| 73 | if(err) { |
| 74 | playlists = 0; |
| 75 | nplaylists = -1; |
| 76 | /* Probably means server does not support playlists */ |
| 77 | } else { |
| 78 | playlists = vec; |
| 79 | nplaylists = nvec; |
| 80 | qsort(playlists, nplaylists, sizeof (char *), playlistcmp); |
| 81 | } |
| 82 | /* Tell our consumers */ |
| 83 | event_raise("playlists-updated", 0); |
| 84 | } |
| 85 | |
| 86 | /** @brief Called to activate a playlist */ |
| 87 | static void menu_activate_playlist(GtkMenuItem *menuitem, |
| 88 | gpointer attribute((unused)) user_data) { |
| 89 | GtkLabel *label = GTK_LABEL(GTK_BIN(menuitem)->child); |
| 90 | const char *playlist = gtk_label_get_text(label); |
| 91 | |
| 92 | fprintf(stderr, "activate playlist %s\n", playlist); /* TODO */ |
| 93 | } |
| 94 | |
| 95 | /** @brief Called when the playlists change */ |
| 96 | static void menu_playlists_changed(const char attribute((unused)) *event, |
| 97 | void attribute((unused)) *eventdata, |
| 98 | void attribute((unused)) *callbackdata) { |
| 99 | if(!playlists_menu) |
| 100 | return; /* OMG too soon */ |
| 101 | GtkMenuShell *menu = GTK_MENU_SHELL(playlists_menu); |
| 102 | /* TODO: we could be more sophisticated and only insert/remove widgets as |
| 103 | * needed. For now that's too much effort. */ |
| 104 | while(menu->children) |
| 105 | gtk_container_remove(GTK_CONTAINER(menu), GTK_WIDGET(menu->children->data)); |
| 106 | /* NB nplaylists can be -1 as well as 0 */ |
| 107 | for(int n = 0; n < nplaylists; ++n) { |
| 108 | GtkWidget *w = gtk_menu_item_new_with_label(playlists[n]); |
| 109 | g_signal_connect(w, "activate", G_CALLBACK(menu_activate_playlist), 0); |
| 110 | gtk_widget_show(w); |
| 111 | gtk_menu_shell_append(menu, w); |
| 112 | } |
| 113 | gtk_widget_set_sensitive(playlists_widget, |
| 114 | nplaylists > 0); |
| 115 | } |
| 116 | |
| 117 | void edit_playlists(gpointer attribute((unused)) callback_data, |
| 118 | guint attribute((unused)) callback_action, |
| 119 | GtkWidget attribute((unused)) *menu_item) { |
| 120 | fprintf(stderr, "edit playlists\n"); /* TODO */ |
| 121 | } |
| 122 | |
| 123 | /** @brief Initialize playlist support */ |
| 124 | void playlists_init(void) { |
| 125 | /* We re-get all playlists upon any change... */ |
| 126 | event_register("playlist-created", playlists_update, 0); |
| 127 | event_register("playlist-modified", playlists_update, 0); |
| 128 | event_register("playlist-deleted", playlists_update, 0); |
| 129 | /* ...and on reconnection */ |
| 130 | event_register("log-connected", playlists_update, 0); |
| 131 | /* ...and from time to time */ |
| 132 | event_register("periodic-slow", playlists_update, 0); |
| 133 | /* ...and at startup */ |
| 134 | event_register("playlists-updated", menu_playlists_changed, 0); |
| 135 | playlists_update(0, 0, 0); |
| 136 | } |
| 137 | |
| 138 | /* |
| 139 | Local Variables: |
| 140 | c-basic-offset:2 |
| 141 | comment-column:40 |
| 142 | fill-column:79 |
| 143 | indent-tabs-mode:nil |
| 144 | End: |
| 145 | */ |