Code

screen_lyrics: some cleanup
[ncmpc.git] / src / screen_lyrics.c
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2010 The Music Player Daemon Project
3  * Project homepage: http://musicpd.org
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.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
20 #include "screen_lyrics.h"
21 #include "screen_interface.h"
22 #include "screen_message.h"
23 #include "screen_file.h"
24 #include "screen_song.h"
25 #include "i18n.h"
26 #include "options.h"
27 #include "mpdclient.h"
28 #include "screen.h"
29 #include "lyrics.h"
30 #include "screen_text.h"
32 #include <assert.h>
33 #include <sys/stat.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <glib.h>
37 #include <unistd.h>
38 #include <stdio.h>
40 static struct screen_text text;
42 static struct mpd_song *next_song;
43 static bool follow = false;
45 static struct {
46         struct mpd_song *song;
48         char *artist, *title;
50         struct plugin_cycle *loader;
51 } current;
53 static void
54 screen_lyrics_abort(void)
55 {
56         if (current.loader != NULL) {
57                 plugin_stop(current.loader);
58                 current.loader = NULL;
59         }
61         if (current.artist != NULL) {
62                 g_free(current.artist);
63                 current.artist = NULL;
64         }
66         if (current.title != NULL) {
67                 g_free(current.title);
68                 current.artist = NULL;
69         }
71         if (current.song != NULL) {
72                 mpd_song_free(current.song);
73                 current.song = NULL;
74         }
75 }
77 /**
78  * Repaint and update the screen, if it is currently active.
79  */
80 static void
81 lyrics_repaint_if_active(void)
82 {
83         if (screen_is_visible(&screen_lyrics)) {
84                 screen_text_repaint(&text);
86                 /* XXX repaint the screen title */
87         }
88 }
90 static void
91 path_lyr_file(char *path, size_t size,
92                 const char *artist, const char *title)
93 {
94         snprintf(path, size, "%s/.lyrics/%s - %s.txt",
95                         getenv("HOME"), artist, title);
96 }
98 static bool
99 exists_lyr_file(const char *artist, const char *title)
101         char path[1024];
102         struct stat result;
104         path_lyr_file(path, 1024, artist, title);
106         return (stat(path, &result) == 0);
109 static FILE *
110 create_lyr_file(const char *artist, const char *title)
112         char path[1024];
114         snprintf(path, 1024, "%s/.lyrics",
115                  getenv("HOME"));
116         mkdir(path, S_IRWXU);
118         path_lyr_file(path, 1024, artist, title);
120         return fopen(path, "w");
123 static int
124 store_lyr_hd(void)
126         FILE *lyr_file;
127         unsigned i;
129         lyr_file = create_lyr_file(current.artist, current.title);
130         if (lyr_file == NULL)
131                 return -1;
133         for (i = 0; i < text.lines->len; ++i)
134                 fprintf(lyr_file, "%s\n",
135                         (const char*)g_ptr_array_index(text.lines, i));
137         fclose(lyr_file);
138         return 0;
141 static int
142 delete_lyr_hd(void)
144         char path[1024];
146         if (!exists_lyr_file(current.artist, current.title))
147                 return -1;
149         path_lyr_file(path, 1024, current.artist, current.title);
150         if (unlink(path) != 0)
151                 return -2;
153         return 0;
156 static void
157 screen_lyrics_set(const GString *str)
159         screen_text_set(&text, str);
161         /* paint new data */
163         lyrics_repaint_if_active();
166 static void
167 screen_lyrics_callback(const GString *result, const bool success,
168                        G_GNUC_UNUSED void *data)
170         assert(current.loader != NULL);
172         /* Display result, which may be lyrics or error messages */
173         if (result != NULL)
174                 screen_lyrics_set(result);
176         if (success == true) {
177                 if (options.lyrics_autosave &&
178                     !exists_lyr_file(current.artist, current.title))
179                         store_lyr_hd();
180         } else {
181                 /* translators: no lyrics were found for the song */
182                 screen_status_message (_("No lyrics"));
183         }
185         plugin_stop(current.loader);
186         current.loader = NULL;
189 static void
190 screen_lyrics_load(const struct mpd_song *song)
192         const char *artist, *title;
194         assert(song != NULL);
196         screen_lyrics_abort();
197         screen_text_clear(&text);
199         artist = mpd_song_get_tag(song, MPD_TAG_ARTIST, 0);
200         title = mpd_song_get_tag(song, MPD_TAG_TITLE, 0);
202         current.song = mpd_song_dup(song);
203         current.artist = g_strdup(artist);
204         current.title = g_strdup(title);
206         current.loader = lyrics_load(current.artist, current.title,
207                                      screen_lyrics_callback, NULL);
210 static void
211 lyrics_screen_init(WINDOW *w, int cols, int rows)
213         screen_text_init(&text, w, cols, rows);
216 static void
217 lyrics_resize(int cols, int rows)
219         screen_text_resize(&text, cols, rows);
222 static void
223 lyrics_exit(void)
225         screen_lyrics_abort();
227         screen_text_deinit(&text);
230 static void
231 lyrics_open(struct mpdclient *c)
233         const struct mpd_song *next_song_c =
234                 next_song != NULL ? next_song : c->song;
236         if (next_song_c != NULL &&
237             (current.song == NULL ||
238              strcmp(mpd_song_get_uri(next_song_c),
239                     mpd_song_get_uri(current.song)) != 0))
240                 screen_lyrics_load(next_song_c);
242         if (next_song != NULL)
243                 mpd_song_free(next_song);
244         next_song = NULL;
247 static void
248 lyrics_update(struct mpdclient *c)
250         if (!follow)
251                 return;
253         if (c->song != NULL &&
254             (current.song == NULL ||
255              strcmp(mpd_song_get_uri(c->song),
256                     mpd_song_get_uri(current.song)) != 0))
257                 screen_lyrics_load(c->song);
260 static const char *
261 lyrics_title(char *str, size_t size)
263         if (current.loader != NULL) {
264                 snprintf(str, size, "%s (%s)",
265                          _("Lyrics"),
266                          /* translators: this message is displayed
267                             while data is retrieved */
268                          _("loading..."));
269                 return str;
270         } else if (current.artist != NULL && current.title != NULL &&
271                    !screen_text_is_empty(&text)) {
272                 snprintf(str, size, "%s: %s - %s",
273                          _("Lyrics"),
274                          current.artist, current.title);
275                 return str;
276         } else
277                 return _("Lyrics");
280 static void
281 lyrics_paint(void)
283         screen_text_paint(&text);
286 static bool
287 lyrics_cmd(struct mpdclient *c, command_t cmd)
289         if (screen_text_cmd(&text, c, cmd))
290                 return true;
292         switch(cmd) {
293         case CMD_INTERRUPT:
294                 if (current.loader != NULL) {
295                         screen_lyrics_abort();
296                         screen_text_clear(&text);
297                 }
298                 return true;
299         case CMD_SAVE_PLAYLIST:
300                 if (current.loader == NULL && current.artist != NULL &&
301                     current.title != NULL && store_lyr_hd() == 0)
302                         /* lyrics for the song were saved on hard disk */
303                         screen_status_message (_("Lyrics saved"));
304                 return true;
305         case CMD_DELETE:
306                 if (current.loader == NULL && current.artist != NULL &&
307                     current.title != NULL) {
308                         switch (delete_lyr_hd()) {
309                         case 0:
310                                 screen_status_message (_("Lyrics deleted"));
311                                 break;
312                         case -1:
313                                 screen_status_message (_("No saved lyrics"));
314                                 break;
315                         }
316                 }
317                 return true;
318         case CMD_LYRICS_UPDATE:
319                 if (c->song != NULL) {
320                         screen_lyrics_load(c->song);
321                         screen_text_repaint(&text);
322                 }
323                 return true;
325 #ifdef ENABLE_SONG_SCREEN
326         case CMD_SCREEN_SONG:
327                 if (current.song != NULL) {
328                         screen_song_switch(c, current.song);
329                         return true;
330                 }
332                 break;
333 #endif
334         case CMD_SCREEN_SWAP:
335                 screen_swap(c, current.song);
336                 return true;
338         case CMD_LOCATE:
339                 if (current.song != NULL) {
340                         screen_file_goto_song(c, current.song);
341                         return true;
342                 }
344                 return false;
346         default:
347                 break;
348         }
350         return false;
353 const struct screen_functions screen_lyrics = {
354         .init = lyrics_screen_init,
355         .exit = lyrics_exit,
356         .open = lyrics_open,
357         .update = lyrics_update,
358         .close = NULL,
359         .resize = lyrics_resize,
360         .paint = lyrics_paint,
361         .cmd = lyrics_cmd,
362         .get_title = lyrics_title,
363 };
365 void
366 screen_lyrics_switch(struct mpdclient *c, const struct mpd_song *song, bool f)
368         assert(song != NULL);
370         follow = f;
371         next_song = mpd_song_dup(song);
372         screen_switch(&screen_lyrics, c);