Code

screen: moved status message functions to screen_message.c
[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 "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 bool
91 exists_lyr_file(const char *artist, const char *title)
92 {
93         char path[1024];
94         struct stat result;
96         snprintf(path, 1024, "%s/.lyrics/%s - %s.txt",
97                  getenv("HOME"), artist, title);
99         return (stat(path, &result) == 0);
102 static FILE *
103 create_lyr_file(const char *artist, const char *title)
105         char path[1024];
107         snprintf(path, 1024, "%s/.lyrics",
108                  getenv("HOME"));
109         mkdir(path, S_IRWXU);
111         snprintf(path, 1024, "%s/.lyrics/%s - %s.txt",
112                  getenv("HOME"), artist, title);
114         return fopen(path, "w");
117 static int
118 store_lyr_hd(void)
120         FILE *lyr_file;
121         unsigned i;
123         lyr_file = create_lyr_file(current.artist, current.title);
124         if (lyr_file == NULL)
125                 return -1;
127         for (i = 0; i < text.lines->len; ++i)
128                 fprintf(lyr_file, "%s\n",
129                         (const char*)g_ptr_array_index(text.lines, i));
131         fclose(lyr_file);
132         return 0;
135 static void
136 screen_lyrics_set(const GString *str)
138         screen_text_set(&text, str);
140         /* paint new data */
142         lyrics_repaint_if_active();
144         if (options.lyrics_autosave &&
145             !exists_lyr_file(current.artist, current.title))
146                 store_lyr_hd();
149 static void
150 screen_lyrics_callback(const GString *result, G_GNUC_UNUSED void *data)
152         assert(current.loader != NULL);
154         if (result != NULL)
155                 screen_lyrics_set(result);
156         else
157                 /* translators: no lyrics were found for the song */
158                 screen_status_message (_("No lyrics"));
160         plugin_stop(current.loader);
161         current.loader = NULL;
164 static void
165 screen_lyrics_load(const struct mpd_song *song)
167         const char *artist, *title;
169         assert(song != NULL);
171         screen_lyrics_abort();
172         screen_text_clear(&text);
174         artist = mpd_song_get_tag(song, MPD_TAG_ARTIST, 0);
175         title = mpd_song_get_tag(song, MPD_TAG_TITLE, 0);
177         current.song = mpd_song_dup(song);
178         current.artist = g_strdup(artist);
179         current.title = g_strdup(title);
181         current.loader = lyrics_load(current.artist, current.title,
182                                      screen_lyrics_callback, NULL);
185 static void
186 lyrics_screen_init(WINDOW *w, int cols, int rows)
188         screen_text_init(&text, w, cols, rows);
191 static void
192 lyrics_resize(int cols, int rows)
194         screen_text_resize(&text, cols, rows);
197 static void
198 lyrics_exit(void)
200         screen_lyrics_abort();
202         screen_text_deinit(&text);
205 static void
206 lyrics_open(struct mpdclient *c)
208         const struct mpd_song *next_song_c =
209                 next_song != NULL ? next_song : c->song;
211         if (next_song_c != NULL &&
212             (current.song == NULL ||
213              strcmp(mpd_song_get_uri(next_song_c),
214                     mpd_song_get_uri(current.song)) != 0))
215                 screen_lyrics_load(next_song_c);
217         if (next_song != NULL)
218                 mpd_song_free(next_song);
219         next_song = NULL;
222 static void
223 lyrics_update(struct mpdclient *c)
225         if (!follow)
226                 return;
228         if (c->song != NULL &&
229             (current.song == NULL ||
230              strcmp(mpd_song_get_uri(c->song),
231                     mpd_song_get_uri(current.song)) != 0))
232                 screen_lyrics_load(c->song);
235 static const char *
236 lyrics_title(char *str, size_t size)
238         if (current.loader != NULL) {
239                 snprintf(str, size, "%s (%s)",
240                          _("Lyrics"),
241                          /* translators: this message is displayed
242                             while data is retrieved */
243                          _("loading..."));
244                 return str;
245         } else if (current.artist != NULL && current.title != NULL &&
246                    !screen_text_is_empty(&text)) {
247                 snprintf(str, size, "%s: %s - %s",
248                          _("Lyrics"),
249                          current.artist, current.title);
250                 return str;
251         } else
252                 return _("Lyrics");
255 static void
256 lyrics_paint(void)
258         screen_text_paint(&text);
261 static bool
262 lyrics_cmd(struct mpdclient *c, command_t cmd)
264         if (screen_text_cmd(&text, c, cmd))
265                 return true;
267         switch(cmd) {
268         case CMD_INTERRUPT:
269                 if (current.loader != NULL) {
270                         screen_lyrics_abort();
271                         screen_text_clear(&text);
272                 }
273                 return true;
274         case CMD_SAVE_PLAYLIST:
275                 if (current.loader == NULL && current.artist != NULL &&
276                     current.title != NULL && store_lyr_hd() == 0)
277                         /* lyrics for the song were saved on hard disk */
278                         screen_status_message (_("Lyrics saved"));
279                 return true;
280         case CMD_LYRICS_UPDATE:
281                 if (c->song != NULL) {
282                         screen_lyrics_load(c->song);
283                         screen_text_repaint(&text);
284                 }
285                 return true;
287 #ifdef ENABLE_SONG_SCREEN
288         case CMD_SCREEN_SONG:
289                 if (current.song != NULL) {
290                         screen_song_switch(c, current.song);
291                         return true;
292                 }
294                 break;
295 #endif
296         case CMD_SCREEN_SWAP:
297                 screen_swap(c, current.song);
298                 return true;
300         case CMD_LOCATE:
301                 if (current.song != NULL) {
302                         screen_file_goto_song(c, current.song);
303                         return true;
304                 }
306                 return false;
308         default:
309                 break;
310         }
312         return false;
315 const struct screen_functions screen_lyrics = {
316         .init = lyrics_screen_init,
317         .exit = lyrics_exit,
318         .open = lyrics_open,
319         .update = lyrics_update,
320         .close = NULL,
321         .resize = lyrics_resize,
322         .paint = lyrics_paint,
323         .cmd = lyrics_cmd,
324         .get_title = lyrics_title,
325 };
327 void
328 screen_lyrics_switch(struct mpdclient *c, const struct mpd_song *song, bool f)
330         assert(song != NULL);
332         follow = f;
333         next_song = mpd_song_dup(song);
334         screen_switch(&screen_lyrics, c);