Code

automatically save lyrics
[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 const struct mpd_song *next_song;
40 static struct {
41         struct mpd_song *song;
43         char *artist, *title;
45         struct plugin_cycle *loader;
46 } current;
48 static void
49 screen_lyrics_abort(void)
50 {
51         if (current.loader != NULL) {
52                 plugin_stop(current.loader);
53                 current.loader = NULL;
54         }
56         if (current.artist != NULL) {
57                 g_free(current.artist);
58                 current.artist = NULL;
59         }
61         if (current.title != NULL) {
62                 g_free(current.title);
63                 current.artist = NULL;
64         }
66         if (current.song != NULL) {
67                 mpd_freeSong(current.song);
68                 current.song = NULL;
69         }
70 }
72 /**
73  * Repaint and update the screen, if it is currently active.
74  */
75 static void
76 lyrics_repaint_if_active(void)
77 {
78         if (screen_is_visible(&screen_lyrics)) {
79                 screen_text_repaint(&text);
81                 /* XXX repaint the screen title */
82         }
83 }
85 static bool
86 exists_lyr_file(const char *artist, const char *title)
87 {
88         char path[1024];
89         struct stat result;
91         snprintf(path, 1024, "%s/.lyrics/%s - %s.txt",
92                  getenv("HOME"), artist, title);
94         return (stat(path, &result) == 0);
95 }
97 static FILE *
98 create_lyr_file(const char *artist, const char *title)
99 {
100         char path[1024];
102         snprintf(path, 1024, "%s/.lyrics",
103                  getenv("HOME"));
104         mkdir(path, S_IRWXU);
106         snprintf(path, 1024, "%s/.lyrics/%s - %s.txt",
107                  getenv("HOME"), artist, title);
109         return fopen(path, "w");
112 static int
113 store_lyr_hd(void)
115         FILE *lyr_file;
116         unsigned i;
118         lyr_file = create_lyr_file(current.artist, current.title);
119         if (lyr_file == NULL)
120                 return -1;
122         for (i = 0; i < text.lines->len; ++i)
123                 fprintf(lyr_file, "%s\n",
124                         (const char*)g_ptr_array_index(text.lines, i));
126         fclose(lyr_file);
127         return 0;
130 static void
131 screen_lyrics_set(const GString *str)
133         screen_text_set(&text, str);
135         /* paint new data */
137         lyrics_repaint_if_active();
139         if (options.lyrics_autosave &&
140             !exists_lyr_file(current.artist, current.title))
141                 store_lyr_hd();
144 static void
145 screen_lyrics_callback(const GString *result, G_GNUC_UNUSED void *data)
147         assert(current.loader != NULL);
149         if (result != NULL)
150                 screen_lyrics_set(result);
151         else
152                 /* translators: no lyrics were found for the song */
153                 screen_status_message (_("No lyrics"));
155         plugin_stop(current.loader);
156         current.loader = NULL;
159 static void
160 screen_lyrics_load(const struct mpd_song *song)
162         char buffer[MAX_SONGNAME_LENGTH];
164         assert(song != NULL);
166         screen_lyrics_abort();
167         screen_text_clear(&text);
169         current.song = mpd_songDup(song);
171         strfsong(buffer, sizeof(buffer), "%artist%", song);
172         current.artist = g_strdup(buffer);
174         strfsong(buffer, sizeof(buffer), "%title%", song);
175         current.title = g_strdup(buffer);
177         current.loader = lyrics_load(current.artist, current.title,
178                                      screen_lyrics_callback, NULL);
181 static void
182 lyrics_screen_init(WINDOW *w, int cols, int rows)
184         screen_text_init(&text, w, cols, rows);
187 static void
188 lyrics_resize(int cols, int rows)
190         screen_text_resize(&text, cols, rows);
193 static void
194 lyrics_exit(void)
196         screen_lyrics_abort();
198         screen_text_deinit(&text);
201 static void
202 lyrics_open(mpdclient_t *c)
204         if (next_song == NULL)
205                 next_song = c->song;
207         if (next_song != NULL &&
208             (current.song == NULL ||
209              strcmp(next_song->file, current.song->file) != 0))
210                 screen_lyrics_load(next_song);
212         next_song = NULL;
216 static const char *
217 lyrics_title(char *str, size_t size)
219         if (current.loader != NULL) {
220                 snprintf(str, size, "%s (%s)",
221                          _("Lyrics"),
222                          /* translators: this message is displayed
223                             while data is retrieved */
224                          _("loading..."));
225                 return str;
226         } else if (current.artist != NULL && current.title != NULL &&
227                    !screen_text_is_empty(&text)) {
228                 snprintf(str, size, "%s: %s - %s",
229                          _("Lyrics"),
230                          current.artist, current.title);
231                 return str;
232         } else
233                 return _("Lyrics");
236 static void
237 lyrics_paint(void)
239         screen_text_paint(&text);
242 static bool
243 lyrics_cmd(mpdclient_t *c, command_t cmd)
245         if (screen_text_cmd(&text, c, cmd))
246                 return true;
248         switch(cmd) {
249         case CMD_INTERRUPT:
250                 if (current.loader != NULL) {
251                         screen_lyrics_abort();
252                         screen_text_clear(&text);
253                 }
254                 return true;
255         case CMD_SAVE_PLAYLIST:
256                 if (current.loader == NULL && current.artist != NULL &&
257                     current.title != NULL && store_lyr_hd() == 0)
258                         /* lyrics for the song were saved on hard disk */
259                         screen_status_message (_("Lyrics saved"));
260                 return true;
261         case CMD_LYRICS_UPDATE:
262                 if (c->song != NULL) {
263                         screen_lyrics_load(c->song);
264                         screen_text_repaint(&text);
265                 }
266                 return true;
268 #ifdef ENABLE_SONG_SCREEN
269         case CMD_SCREEN_SONG:
270                 if (current.song != NULL) {
271                         screen_song_switch(c, current.song);
272                         return true;
273                 }
275                 break;
276 #endif
277         case CMD_SCREEN_SWAP:
278                 screen_swap(c, current.song);
279                 return true;
281         case CMD_LOCATE:
282                 if (current.song != NULL) {
283                         screen_file_goto_song(c, current.song);
284                         return true;
285                 }
287                 return false;
289         default:
290                 break;
291         }
293         return false;
296 const struct screen_functions screen_lyrics = {
297         .init = lyrics_screen_init,
298         .exit = lyrics_exit,
299         .open = lyrics_open,
300         .close = NULL,
301         .resize = lyrics_resize,
302         .paint = lyrics_paint,
303         .cmd = lyrics_cmd,
304         .get_title = lyrics_title,
305 };
307 void
308 screen_lyrics_switch(struct mpdclient *c, const struct mpd_song *song)
310         assert(song != NULL);
312         next_song = song;
313         screen_switch(&screen_lyrics, c);