Code

added comments for translators
[ncmpc.git] / src / screen_lyrics.c
1 /*
2  * (c) 2006 by Kalle Wallin <kaw@linux.se>
3  * Copyright (C) 2008 Max Kellermann <max@duempel.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.
9  *
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.
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  *
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 void
86 screen_lyrics_set(const GString *str)
87 {
88         screen_text_set(&text, str);
90         /* paint new data */
92         lyrics_repaint_if_active();
93 }
95 static void
96 screen_lyrics_callback(const GString *result, G_GNUC_UNUSED void *data)
97 {
98         assert(current.loader != NULL);
100         if (result != NULL)
101                 screen_lyrics_set(result);
102         else
103                 /* translators: no lyrics were found for the song */
104                 screen_status_message (_("No lyrics"));
106         plugin_stop(current.loader);
107         current.loader = NULL;
110 static void
111 screen_lyrics_load(const struct mpd_song *song)
113         char buffer[MAX_SONGNAME_LENGTH];
115         assert(song != NULL);
117         screen_lyrics_abort();
118         screen_text_clear(&text);
120         current.song = mpd_songDup(song);
122         strfsong(buffer, sizeof(buffer), "%artist%", song);
123         current.artist = g_strdup(buffer);
125         strfsong(buffer, sizeof(buffer), "%title%", song);
126         current.title = g_strdup(buffer);
128         current.loader = lyrics_load(current.artist, current.title,
129                                      screen_lyrics_callback, NULL);
132 static FILE *create_lyr_file(const char *artist, const char *title)
134         char path[1024];
136         snprintf(path, 1024, "%s/.lyrics",
137                  getenv("HOME"));
138         mkdir(path, S_IRWXU);
140         snprintf(path, 1024, "%s/.lyrics/%s - %s.txt",
141                  getenv("HOME"), artist, title);
143         return fopen(path, "w");
146 static int store_lyr_hd(void)
148         FILE *lyr_file;
149         unsigned i;
151         lyr_file = create_lyr_file(current.artist, current.title);
152         if (lyr_file == NULL)
153                 return -1;
155         for (i = 0; i < text.lines->len; ++i)
156                 fprintf(lyr_file, "%s\n",
157                         (const char*)g_ptr_array_index(text.lines, i));
159         fclose(lyr_file);
160         return 0;
163 static void
164 lyrics_screen_init(WINDOW *w, int cols, int rows)
166         screen_text_init(&text, w, cols, rows);
169 static void
170 lyrics_resize(int cols, int rows)
172         screen_text_resize(&text, cols, rows);
175 static void
176 lyrics_exit(void)
178         screen_lyrics_abort();
180         screen_text_deinit(&text);
183 static void
184 lyrics_open(mpdclient_t *c)
186         if (next_song == NULL)
187                 next_song = c->song;
189         if (next_song != NULL &&
190             (current.song == NULL ||
191              strcmp(next_song->file, current.song->file) != 0))
192                 screen_lyrics_load(next_song);
194         next_song = NULL;
198 static const char *
199 lyrics_title(char *str, size_t size)
201         if (current.loader != NULL) {
202                 snprintf(str, size, "%s (%s)",
203                          _("Lyrics"),
204                          /* translators: this message is displayed
205                             while data is retrieved */
206                          _("loading..."));
207                 return str;
208         } else if (current.artist != NULL && current.title != NULL &&
209                    !screen_text_is_empty(&text)) {
210                 snprintf(str, size, "%s: %s - %s",
211                          _("Lyrics"),
212                          current.artist, current.title);
213                 return str;
214         } else
215                 return _("Lyrics");
218 static void
219 lyrics_paint(void)
221         screen_text_paint(&text);
224 static bool
225 lyrics_cmd(mpdclient_t *c, command_t cmd)
227         if (screen_text_cmd(&text, c, cmd))
228                 return true;
230         switch(cmd) {
231         case CMD_INTERRUPT:
232                 if (current.loader != NULL) {
233                         screen_lyrics_abort();
234                         screen_text_clear(&text);
235                 }
236                 return true;
237         case CMD_ADD:
238                 if (current.loader == NULL && current.artist != NULL &&
239                     current.title != NULL && store_lyr_hd() == 0)
240                         /* lyrics for the song were saved on hard disk */
241                         screen_status_message (_("Lyrics saved"));
242                 return true;
243         case CMD_LYRICS_UPDATE:
244                 if (c->song != NULL) {
245                         screen_lyrics_load(c->song);
246                         screen_text_repaint(&text);
247                 }
248                 return true;
250 #ifdef ENABLE_SONG_SCREEN
251         case CMD_VIEW:
252                 if (current.song != NULL) {
253                         screen_song_switch(c, current.song);
254                         return true;
255                 }
257                 break;
258 #endif
260         case CMD_LOCATE:
261                 if (current.song != NULL) {
262                         screen_file_goto_song(c, current.song);
263                         return true;
264                 }
266                 return false;
268         default:
269                 break;
270         }
272         return false;
275 const struct screen_functions screen_lyrics = {
276         .init = lyrics_screen_init,
277         .exit = lyrics_exit,
278         .open = lyrics_open,
279         .close = NULL,
280         .resize = lyrics_resize,
281         .paint = lyrics_paint,
282         .cmd = lyrics_cmd,
283         .get_title = lyrics_title,
284 };
286 void
287 screen_lyrics_switch(struct mpdclient *c, const struct mpd_song *song)
289         assert(song != NULL);
291         next_song = song;
292         screen_switch(&screen_lyrics, c);