Code

use libmpdclient2
[ncmpc.git] / src / screen_lyrics.c
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2009 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 <sys/stat.h>
21 #include "i18n.h"
22 #include "options.h"
23 #include "mpdclient.h"
24 #include "command.h"
25 #include "screen.h"
26 #include "strfsong.h"
27 #include "lyrics.h"
28 #include "screen_text.h"
30 #include <stdlib.h>
31 #include <string.h>
32 #include <glib.h>
33 #include <unistd.h>
34 #include <stdio.h>
36 static struct screen_text text;
38 static struct mpd_song *next_song;
39 static bool follow = false;
41 static struct {
42         struct mpd_song *song;
44         char *artist, *title;
46         struct plugin_cycle *loader;
47 } current;
49 static void
50 screen_lyrics_abort(void)
51 {
52         if (current.loader != NULL) {
53                 plugin_stop(current.loader);
54                 current.loader = NULL;
55         }
57         if (current.artist != NULL) {
58                 g_free(current.artist);
59                 current.artist = NULL;
60         }
62         if (current.title != NULL) {
63                 g_free(current.title);
64                 current.artist = NULL;
65         }
67         if (current.song != NULL) {
68                 mpd_song_free(current.song);
69                 current.song = NULL;
70         }
71 }
73 /**
74  * Repaint and update the screen, if it is currently active.
75  */
76 static void
77 lyrics_repaint_if_active(void)
78 {
79         if (screen_is_visible(&screen_lyrics)) {
80                 screen_text_repaint(&text);
82                 /* XXX repaint the screen title */
83         }
84 }
86 static bool
87 exists_lyr_file(const char *artist, const char *title)
88 {
89         char path[1024];
90         struct stat result;
92         snprintf(path, 1024, "%s/.lyrics/%s - %s.txt",
93                  getenv("HOME"), artist, title);
95         return (stat(path, &result) == 0);
96 }
98 static FILE *
99 create_lyr_file(const char *artist, const char *title)
101         char path[1024];
103         snprintf(path, 1024, "%s/.lyrics",
104                  getenv("HOME"));
105         mkdir(path, S_IRWXU);
107         snprintf(path, 1024, "%s/.lyrics/%s - %s.txt",
108                  getenv("HOME"), artist, title);
110         return fopen(path, "w");
113 static int
114 store_lyr_hd(void)
116         FILE *lyr_file;
117         unsigned i;
119         lyr_file = create_lyr_file(current.artist, current.title);
120         if (lyr_file == NULL)
121                 return -1;
123         for (i = 0; i < text.lines->len; ++i)
124                 fprintf(lyr_file, "%s\n",
125                         (const char*)g_ptr_array_index(text.lines, i));
127         fclose(lyr_file);
128         return 0;
131 static void
132 screen_lyrics_set(const GString *str)
134         screen_text_set(&text, str);
136         /* paint new data */
138         lyrics_repaint_if_active();
140         if (options.lyrics_autosave &&
141             !exists_lyr_file(current.artist, current.title))
142                 store_lyr_hd();
145 static void
146 screen_lyrics_callback(const GString *result, G_GNUC_UNUSED void *data)
148         assert(current.loader != NULL);
150         if (result != NULL)
151                 screen_lyrics_set(result);
152         else
153                 /* translators: no lyrics were found for the song */
154                 screen_status_message (_("No lyrics"));
156         plugin_stop(current.loader);
157         current.loader = NULL;
160 static void
161 screen_lyrics_load(const struct mpd_song *song)
163         char buffer[MAX_SONGNAME_LENGTH];
165         assert(song != NULL);
167         screen_lyrics_abort();
168         screen_text_clear(&text);
170         current.song = mpd_song_dup(song);
172         strfsong(buffer, sizeof(buffer), "%artist%", song);
173         current.artist = g_strdup(buffer);
175         strfsong(buffer, sizeof(buffer), "%title%", song);
176         current.title = g_strdup(buffer);
178         current.loader = lyrics_load(current.artist, current.title,
179                                      screen_lyrics_callback, NULL);
182 static void
183 lyrics_screen_init(WINDOW *w, int cols, int rows)
185         screen_text_init(&text, w, cols, rows);
188 static void
189 lyrics_resize(int cols, int rows)
191         screen_text_resize(&text, cols, rows);
194 static void
195 lyrics_exit(void)
197         screen_lyrics_abort();
199         screen_text_deinit(&text);
202 static void
203 lyrics_open(mpdclient_t *c)
205         if (next_song == NULL)
206                 next_song = c->song;
208         if (next_song != NULL &&
209             (current.song == NULL ||
210              strcmp(mpd_song_get_uri(next_song),
211                     mpd_song_get_uri(current.song)) != 0))
212                 screen_lyrics_load(next_song);
214         if (next_song != c->song)
215                 mpd_song_free(next_song);
216         next_song = NULL;
219 static void
220 lyrics_update(mpdclient_t *c)
222         if (!follow)
223                 return;
225         next_song = c->song;
227         if (next_song != NULL &&
228             (current.song == NULL ||
229              strcmp(mpd_song_get_uri(next_song),
230                     mpd_song_get_uri(current.song)) != 0))
231                 screen_lyrics_load(next_song);
233         next_song = NULL;
236 static const char *
237 lyrics_title(char *str, size_t size)
239         if (current.loader != NULL) {
240                 snprintf(str, size, "%s (%s)",
241                          _("Lyrics"),
242                          /* translators: this message is displayed
243                             while data is retrieved */
244                          _("loading..."));
245                 return str;
246         } else if (current.artist != NULL && current.title != NULL &&
247                    !screen_text_is_empty(&text)) {
248                 snprintf(str, size, "%s: %s - %s",
249                          _("Lyrics"),
250                          current.artist, current.title);
251                 return str;
252         } else
253                 return _("Lyrics");
256 static void
257 lyrics_paint(void)
259         screen_text_paint(&text);
262 static bool
263 lyrics_cmd(mpdclient_t *c, command_t cmd)
265         if (screen_text_cmd(&text, c, cmd))
266                 return true;
268         switch(cmd) {
269         case CMD_INTERRUPT:
270                 if (current.loader != NULL) {
271                         screen_lyrics_abort();
272                         screen_text_clear(&text);
273                 }
274                 return true;
275         case CMD_SAVE_PLAYLIST:
276                 if (current.loader == NULL && current.artist != NULL &&
277                     current.title != NULL && store_lyr_hd() == 0)
278                         /* lyrics for the song were saved on hard disk */
279                         screen_status_message (_("Lyrics saved"));
280                 return true;
281         case CMD_LYRICS_UPDATE:
282                 if (c->song != NULL) {
283                         screen_lyrics_load(c->song);
284                         screen_text_repaint(&text);
285                 }
286                 return true;
288 #ifdef ENABLE_SONG_SCREEN
289         case CMD_SCREEN_SONG:
290                 if (current.song != NULL) {
291                         screen_song_switch(c, current.song);
292                         return true;
293                 }
295                 break;
296 #endif
297         case CMD_SCREEN_SWAP:
298                 screen_swap(c, current.song);
299                 return true;
301         case CMD_LOCATE:
302                 if (current.song != NULL) {
303                         screen_file_goto_song(c, current.song);
304                         return true;
305                 }
307                 return false;
309         default:
310                 break;
311         }
313         return false;
316 const struct screen_functions screen_lyrics = {
317         .init = lyrics_screen_init,
318         .exit = lyrics_exit,
319         .open = lyrics_open,
320         .update = lyrics_update,
321         .close = NULL,
322         .resize = lyrics_resize,
323         .paint = lyrics_paint,
324         .cmd = lyrics_cmd,
325         .get_title = lyrics_title,
326 };
328 void
329 screen_lyrics_switch(struct mpdclient *c, const struct mpd_song *song, bool f)
331         assert(song != NULL);
333         follow = f;
334         next_song = mpd_song_dup(song);
335         screen_switch(&screen_lyrics, c);