| 1 | /* |
| 2 | * This file is part of DisOrder. |
| 3 | * Copyright (C) 2004-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 server/lookup.c |
| 21 | * @brief Server lookups |
| 22 | * |
| 23 | * To improve performance many server lookups are cached. |
| 24 | */ |
| 25 | |
| 26 | #include "disorder-cgi.h" |
| 27 | |
| 28 | /** @brief Cached data */ |
| 29 | static unsigned flags; |
| 30 | |
| 31 | /** @brief Map of hashes to queud data */ |
| 32 | static hash *queuemap; |
| 33 | |
| 34 | struct queue_entry *dcgi_queue; |
| 35 | struct queue_entry *dcgi_playing; |
| 36 | struct queue_entry *dcgi_recent; |
| 37 | |
| 38 | int dcgi_volume_left; |
| 39 | int dcgi_volume_right; |
| 40 | |
| 41 | char **dcgi_new; |
| 42 | int dcgi_nnew; |
| 43 | |
| 44 | rights_type dcgi_rights; |
| 45 | |
| 46 | int dcgi_enabled; |
| 47 | int dcgi_random_enabled; |
| 48 | |
| 49 | static void queuemap_add(struct queue_entry *q) { |
| 50 | if(!queuemap) |
| 51 | queuemap = hash_new(sizeof (struct queue_entry *)); |
| 52 | for(; q; q = q->next) |
| 53 | hash_add(queuemap, q->id, &q, HASH_INSERT_OR_REPLACE); |
| 54 | } |
| 55 | |
| 56 | /** @brief Fetch cachable data */ |
| 57 | void dcgi_lookup(unsigned want) { |
| 58 | unsigned need = want ^ (flags & want); |
| 59 | struct queue_entry *r, *rnext; |
| 60 | char *rs; |
| 61 | |
| 62 | if(!dcgi_client || !need) |
| 63 | return; |
| 64 | if(need & DCGI_QUEUE) { |
| 65 | disorder_queue(dcgi_client, &dcgi_queue); |
| 66 | queuemap_add(dcgi_queue); |
| 67 | } |
| 68 | if(need & DCGI_PLAYING) { |
| 69 | disorder_playing(dcgi_client, &dcgi_playing); |
| 70 | queuemap_add(dcgi_playing); |
| 71 | } |
| 72 | if(need & DCGI_NEW) |
| 73 | disorder_new_tracks(dcgi_client, &dcgi_new, &dcgi_nnew, 0); |
| 74 | if(need & DCGI_RECENT) { |
| 75 | /* we need to reverse the order of the list */ |
| 76 | disorder_recent(dcgi_client, &r); |
| 77 | while(r) { |
| 78 | rnext = r->next; |
| 79 | r->next = dcgi_recent; |
| 80 | dcgi_recent = r; |
| 81 | r = rnext; |
| 82 | } |
| 83 | queuemap_add(dcgi_recent); |
| 84 | } |
| 85 | if(need & DCGI_VOLUME) |
| 86 | disorder_get_volume(dcgi_client, |
| 87 | &dcgi_volume_left, &dcgi_volume_right); |
| 88 | if(need & DCGI_RIGHTS) { |
| 89 | dcgi_rights = RIGHT_READ; /* fail-safe */ |
| 90 | if(!disorder_userinfo(dcgi_client, disorder_user(dcgi_client), |
| 91 | "rights", &rs)) |
| 92 | parse_rights(rs, &dcgi_rights, 1); |
| 93 | } |
| 94 | if(need & DCGI_ENABLED) |
| 95 | disorder_enabled(dcgi_client, &dcgi_enabled); |
| 96 | if(need & DCGI_RANDOM_ENABLED) |
| 97 | disorder_random_enabled(dcgi_client, &dcgi_random_enabled); |
| 98 | flags |= need; |
| 99 | } |
| 100 | |
| 101 | /** @brief Locate a track by ID */ |
| 102 | struct queue_entry *dcgi_findtrack(const char *id) { |
| 103 | struct queue_entry **qq; |
| 104 | |
| 105 | if(queuemap && (qq = hash_find(queuemap, id))) |
| 106 | return *qq; |
| 107 | dcgi_lookup(DCGI_PLAYING); |
| 108 | if(queuemap && (qq = hash_find(queuemap, id))) |
| 109 | return *qq; |
| 110 | dcgi_lookup(DCGI_QUEUE); |
| 111 | if(queuemap && (qq = hash_find(queuemap, id))) |
| 112 | return *qq; |
| 113 | dcgi_lookup(DCGI_RECENT); |
| 114 | if(queuemap && (qq = hash_find(queuemap, id))) |
| 115 | return *qq; |
| 116 | return NULL; |
| 117 | } |
| 118 | |
| 119 | void dcgi_lookup_reset(void) { |
| 120 | /* Forget everything we knew */ |
| 121 | flags = 0; |
| 122 | queuemap = 0; |
| 123 | dcgi_recent = 0; |
| 124 | dcgi_queue = 0; |
| 125 | dcgi_playing = 0; |
| 126 | dcgi_rights = 0; |
| 127 | dcgi_new = 0; |
| 128 | dcgi_nnew = 0; |
| 129 | dcgi_enabled = 0; |
| 130 | dcgi_random_enabled = 0; |
| 131 | dcgi_volume_left = dcgi_volume_right = 0; |
| 132 | } |
| 133 | |
| 134 | |
| 135 | /* |
| 136 | Local Variables: |
| 137 | c-basic-offset:2 |
| 138 | comment-column:40 |
| 139 | fill-column:79 |
| 140 | indent-tabs-mode:nil |
| 141 | End: |
| 142 | */ |