Code

screen_lyrics.c: wrapped some duplicate code
[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 void
142 screen_lyrics_set(const GString *str)
144         screen_text_set(&text, str);
146         /* paint new data */
148         lyrics_repaint_if_active();
151 static void
152 screen_lyrics_callback(const GString *result, const bool success,
153                        G_GNUC_UNUSED void *data)
155         assert(current.loader != NULL);
157         /* Display result, which may be lyrics or error messages */
158         if (result != NULL)
159                 screen_lyrics_set(result);
161         if (success == true) {
162                 if (options.lyrics_autosave &&
163                     !exists_lyr_file(current.artist, current.title))
164                         store_lyr_hd();
165         } else {
166                 /* translators: no lyrics were found for the song */
167                 screen_status_message (_("No lyrics"));
168         }
170         plugin_stop(current.loader);
171         current.loader = NULL;
174 static void
175 screen_lyrics_load(const struct mpd_song *song)
177         const char *artist, *title;
179         assert(song != NULL);
181         screen_lyrics_abort();
182         screen_text_clear(&text);
184         artist = mpd_song_get_tag(song, MPD_TAG_ARTIST, 0);
185         title = mpd_song_get_tag(song, MPD_TAG_TITLE, 0);
187         current.song = mpd_song_dup(song);
188         current.artist = g_strdup(artist);
189         current.title = g_strdup(title);
191         current.loader = lyrics_load(current.artist, current.title,
192                                      screen_lyrics_callback, NULL);
195 static void
196 lyrics_screen_init(WINDOW *w, int cols, int rows)
198         screen_text_init(&text, w, cols, rows);
201 static void
202 lyrics_resize(int cols, int rows)
204         screen_text_resize(&text, cols, rows);
207 static void
208 lyrics_exit(void)
210         screen_lyrics_abort();
212         screen_text_deinit(&text);
215 static void
216 lyrics_open(struct mpdclient *c)
218         const struct mpd_song *next_song_c =
219                 next_song != NULL ? next_song : c->song;
221         if (next_song_c != NULL &&
222             (current.song == NULL ||
223              strcmp(mpd_song_get_uri(next_song_c),
224                     mpd_song_get_uri(current.song)) != 0))
225                 screen_lyrics_load(next_song_c);
227         if (next_song != NULL)
228                 mpd_song_free(next_song);
229         next_song = NULL;
232 static void
233 lyrics_update(struct mpdclient *c)
235         if (!follow)
236                 return;
238         if (c->song != NULL &&
239             (current.song == NULL ||
240              strcmp(mpd_song_get_uri(c->song),
241                     mpd_song_get_uri(current.song)) != 0))
242                 screen_lyrics_load(c->song);
245 static const char *
246 lyrics_title(char *str, size_t size)
248         if (current.loader != NULL) {
249                 snprintf(str, size, "%s (%s)",
250                          _("Lyrics"),
251                          /* translators: this message is displayed
252                             while data is retrieved */
253                          _("loading..."));
254                 return str;
255         } else if (current.artist != NULL && current.title != NULL &&
256                    !screen_text_is_empty(&text)) {
257                 snprintf(str, size, "%s: %s - %s",
258                          _("Lyrics"),
259                          current.artist, current.title);
260                 return str;
261         } else
262                 return _("Lyrics");
265 static void
266 lyrics_paint(void)
268         screen_text_paint(&text);
271 static bool
272 lyrics_cmd(struct mpdclient *c, command_t cmd)
274         if (screen_text_cmd(&text, c, cmd))
275                 return true;
277         switch(cmd) {
278         case CMD_INTERRUPT:
279                 if (current.loader != NULL) {
280                         screen_lyrics_abort();
281                         screen_text_clear(&text);
282                 }
283                 return true;
284         case CMD_SAVE_PLAYLIST:
285                 if (current.loader == NULL && current.artist != NULL &&
286                     current.title != NULL && store_lyr_hd() == 0)
287                         /* lyrics for the song were saved on hard disk */
288                         screen_status_message (_("Lyrics saved"));
289                 return true;
290         case CMD_LYRICS_UPDATE:
291                 if (c->song != NULL) {
292                         screen_lyrics_load(c->song);
293                         screen_text_repaint(&text);
294                 }
295                 return true;
297 #ifdef ENABLE_SONG_SCREEN
298         case CMD_SCREEN_SONG:
299                 if (current.song != NULL) {
300                         screen_song_switch(c, current.song);
301                         return true;
302                 }
304                 break;
305 #endif
306         case CMD_SCREEN_SWAP:
307                 screen_swap(c, current.song);
308                 return true;
310         case CMD_LOCATE:
311                 if (current.song != NULL) {
312                         screen_file_goto_song(c, current.song);
313                         return true;
314                 }
316                 return false;
318         default:
319                 break;
320         }
322         return false;
325 const struct screen_functions screen_lyrics = {
326         .init = lyrics_screen_init,
327         .exit = lyrics_exit,
328         .open = lyrics_open,
329         .update = lyrics_update,
330         .close = NULL,
331         .resize = lyrics_resize,
332         .paint = lyrics_paint,
333         .cmd = lyrics_cmd,
334         .get_title = lyrics_title,
335 };
337 void
338 screen_lyrics_switch(struct mpdclient *c, const struct mpd_song *song, bool f)
340         assert(song != NULL);
342         follow = f;
343         next_song = mpd_song_dup(song);
344         screen_switch(&screen_lyrics, c);