Code

conf.c: use g_mkdir for WIN32 compatibility
[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.
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  *
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_status.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"
31 #include "screen_utils.h"
32 #include "ncu.h"
34 #include <assert.h>
35 #include <errno.h>
36 #include <sys/stat.h>
37 #include <sys/wait.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <glib.h>
41 #include <unistd.h>
42 #include <stdio.h>
44 static struct screen_text text;
46 static struct mpd_song *next_song;
47 static bool follow = false;
49 static struct {
50         struct mpd_song *song;
52         char *artist, *title, *plugin_name;
54         struct plugin_cycle *loader;
56         guint loader_timeout;
57 } current;
59 static void
60 screen_lyrics_abort(void)
61 {
62         if (current.loader != NULL) {
63                 plugin_stop(current.loader);
64                 current.loader = NULL;
65         }
67         if (current.loader_timeout != 0) {
68                 g_source_remove(current.loader_timeout);
69                 current.loader_timeout = 0;
70         }
72         if (current.plugin_name != NULL) {
73                 g_free(current.plugin_name);
74                 current.plugin_name = NULL;
75         }
77         if (current.artist != NULL) {
78                 g_free(current.artist);
79                 current.artist = NULL;
80         }
82         if (current.title != NULL) {
83                 g_free(current.title);
84                 current.title = NULL;
85         }
87         if (current.song != NULL) {
88                 mpd_song_free(current.song);
89                 current.song = NULL;
90         }
91 }
93 /**
94  * Repaint and update the screen, if it is currently active.
95  */
96 static void
97 lyrics_repaint_if_active(void)
98 {
99         if (screen_is_visible(&screen_lyrics)) {
100                 screen_text_repaint(&text);
102                 /* XXX repaint the screen title */
103         }
106 static void
107 path_lyr_file(char *path, size_t size,
108                 const char *artist, const char *title)
110         snprintf(path, size, "%s/.lyrics/%s - %s.txt",
111                         getenv("HOME"), artist, title);
114 static bool
115 exists_lyr_file(const char *artist, const char *title)
117         char path[1024];
118         struct stat result;
120         path_lyr_file(path, 1024, artist, title);
122         return (stat(path, &result) == 0);
125 static FILE *
126 create_lyr_file(const char *artist, const char *title)
128         char path[1024];
130         snprintf(path, 1024, "%s/.lyrics",
131                  getenv("HOME"));
132         mkdir(path, S_IRWXU);
134         path_lyr_file(path, 1024, artist, title);
136         return fopen(path, "w");
139 static int
140 store_lyr_hd(void)
142         FILE *lyr_file;
143         unsigned i;
145         lyr_file = create_lyr_file(current.artist, current.title);
146         if (lyr_file == NULL)
147                 return -1;
149         for (i = 0; i < text.lines->len; ++i)
150                 fprintf(lyr_file, "%s\n",
151                         (const char*)g_ptr_array_index(text.lines, i));
153         fclose(lyr_file);
154         return 0;
157 static int
158 delete_lyr_hd(void)
160         char path[1024];
162         if (!exists_lyr_file(current.artist, current.title))
163                 return -1;
165         path_lyr_file(path, 1024, current.artist, current.title);
166         if (unlink(path) != 0)
167                 return -2;
169         return 0;
172 static void
173 screen_lyrics_set(const GString *str)
175         screen_text_set(&text, str);
177         /* paint new data */
179         lyrics_repaint_if_active();
182 static void
183 screen_lyrics_callback(const GString *result, const bool success,
184                        const char *plugin_name, G_GNUC_UNUSED void *data)
186         assert(current.loader != NULL);
188         current.plugin_name = g_strdup(plugin_name);
190         /* Display result, which may be lyrics or error messages */
191         if (result != NULL)
192                 screen_lyrics_set(result);
194         if (success == true) {
195                 if (options.lyrics_autosave &&
196                     !exists_lyr_file(current.artist, current.title))
197                         store_lyr_hd();
198         } else {
199                 /* translators: no lyrics were found for the song */
200                 screen_status_message (_("No lyrics"));
201         }
203         if (current.loader_timeout != 0) {
204                 g_source_remove(current.loader_timeout);
205                 current.loader_timeout = 0;
206         }
208         plugin_stop(current.loader);
209         current.loader = NULL;
212 static gboolean
213 screen_lyrics_timeout_callback(gpointer G_GNUC_UNUSED data)
215         plugin_stop(current.loader);
216         current.loader = NULL;
218         screen_status_printf(_("Lyrics timeout occurred after %d seconds"),
219                              options.lyrics_timeout);
221         current.loader_timeout = 0;
222         return FALSE;
225 static void
226 screen_lyrics_load(const struct mpd_song *song)
228         const char *artist, *title;
230         assert(song != NULL);
232         screen_lyrics_abort();
233         screen_text_clear(&text);
235         artist = mpd_song_get_tag(song, MPD_TAG_ARTIST, 0);
236         title = mpd_song_get_tag(song, MPD_TAG_TITLE, 0);
238         current.song = mpd_song_dup(song);
239         current.artist = g_strdup(artist);
240         current.title = g_strdup(title);
242         current.loader = lyrics_load(current.artist, current.title,
243                                      screen_lyrics_callback, NULL);
245         if (options.lyrics_timeout != 0) {
246                 current.loader_timeout =
247                         g_timeout_add_seconds(options.lyrics_timeout,
248                                               screen_lyrics_timeout_callback,
249                                               NULL);
250         }
253 static void
254 screen_lyrics_reload(void)
256         if (current.loader == NULL && current.artist != NULL &&
257             current.title != NULL) {
258                 current.loader = lyrics_load(current.artist, current.title,
259                                              screen_lyrics_callback, NULL);
260                 screen_text_repaint(&text);
261         }
264 static void
265 lyrics_screen_init(WINDOW *w, int cols, int rows)
267         screen_text_init(&text, w, cols, rows);
270 static void
271 lyrics_resize(int cols, int rows)
273         screen_text_resize(&text, cols, rows);
276 static void
277 lyrics_exit(void)
279         screen_lyrics_abort();
281         screen_text_deinit(&text);
284 static void
285 lyrics_open(struct mpdclient *c)
287         const struct mpd_song *next_song_c =
288                 next_song != NULL ? next_song : c->song;
290         if (next_song_c != NULL &&
291             (current.song == NULL ||
292              strcmp(mpd_song_get_uri(next_song_c),
293                     mpd_song_get_uri(current.song)) != 0))
294                 screen_lyrics_load(next_song_c);
296         if (next_song != NULL) {
297                 mpd_song_free(next_song);
298                 next_song = NULL;
299         }
302 static void
303 lyrics_update(struct mpdclient *c)
305         if (!follow)
306                 return;
308         if (c->song != NULL &&
309             (current.song == NULL ||
310              strcmp(mpd_song_get_uri(c->song),
311                     mpd_song_get_uri(current.song)) != 0))
312                 screen_lyrics_load(c->song);
315 static const char *
316 lyrics_title(char *str, size_t size)
318         if (current.loader != NULL) {
319                 snprintf(str, size, "%s (%s)",
320                          _("Lyrics"),
321                          /* translators: this message is displayed
322                             while data is retrieved */
323                          _("loading..."));
324                 return str;
325         } else if (current.artist != NULL && current.title != NULL &&
326                    !screen_text_is_empty(&text)) {
327                 int n;
328                 n = snprintf(str, size, "%s: %s - %s",
329                              _("Lyrics"),
330                              current.artist, current.title);
332                 if (options.lyrics_show_plugin && current.plugin_name != NULL &&
333                     (unsigned int) n < size - 1)
334                         snprintf(str + n, size - n, " (%s)",
335                                  current.plugin_name);
337                 return str;
338         } else
339                 return _("Lyrics");
342 static void
343 lyrics_paint(void)
345         screen_text_paint(&text);
348 /* save current lyrics to a file and run editor on it */
349 static void
350 lyrics_edit(void)
352         char *editor = options.text_editor;
353         int status;
355         if (editor == NULL) {
356                 screen_status_message(_("Editor not configured"));
357                 return;
358         }
360         if (options.text_editor_ask) {
361                 char *buf = g_strdup_printf(
362                     _("Do you really want to start an editor and edit these lyrics [%s/%s]? "),
363                                             YES, NO);
364                 bool really = screen_get_yesno(buf, false);
365                 g_free(buf);
366                 if (!really) {
367                         screen_status_message(_("Aborted"));
368                         return;
369                 }
370         }
372         if (store_lyr_hd() < 0)
373                 return;
375         ncu_deinit();
377         /* TODO: fork/exec/wait won't work on Windows, but building a command
378            string for system() is too tricky */
379         pid_t pid = fork();
380         if (pid == -1) {
381                 screen_status_printf(("%s (%s)"), _("Can't start editor"), g_strerror(errno));
382         } else if (pid == 0) {
383                 char path[1024];
384                 path_lyr_file(path, sizeof(path), current.artist, current.title);
385                 execlp(editor, editor, path, NULL);
386                 /* exec failed, do what system does */
387                 _exit(127);
388         } else {
389                 int ret;
390                 do {
391                         ret = waitpid(pid, &status, 0);
392                 } while (ret == -1 && errno == EINTR);
393         }
395         ncu_init();
397         /* TODO: hardly portable */
398         if (WIFEXITED(status)) {
399                 if (WEXITSTATUS(status) == 0)
400                         /* update to get the changes */
401                         screen_lyrics_reload();
402                 else if (WEXITSTATUS(status) == 127)
403                         screen_status_message(_("Can't start editor"));
404                 else
405                         screen_status_printf(_("Editor exited unexpectedly (%d)"),
406                                              WEXITSTATUS(status));
407         } else if (WIFSIGNALED(status)) {
408                 screen_status_printf(_("Editor exited unexpectedly (signal %d)"),
409                                      WTERMSIG(status));
410         }
413 static bool
414 lyrics_cmd(struct mpdclient *c, command_t cmd)
416         if (screen_text_cmd(&text, c, cmd))
417                 return true;
419         switch(cmd) {
420         case CMD_INTERRUPT:
421                 if (current.loader != NULL) {
422                         screen_lyrics_abort();
423                         screen_text_clear(&text);
424                 }
425                 return true;
426         case CMD_SAVE_PLAYLIST:
427                 if (current.loader == NULL && current.artist != NULL &&
428                     current.title != NULL && store_lyr_hd() == 0)
429                         /* lyrics for the song were saved on hard disk */
430                         screen_status_message (_("Lyrics saved"));
431                 return true;
432         case CMD_DELETE:
433                 if (current.loader == NULL && current.artist != NULL &&
434                     current.title != NULL) {
435                         switch (delete_lyr_hd()) {
436                         case 0:
437                                 screen_status_message (_("Lyrics deleted"));
438                                 break;
439                         case -1:
440                                 screen_status_message (_("No saved lyrics"));
441                                 break;
442                         }
443                 }
444                 return true;
445         case CMD_LYRICS_UPDATE:
446                 if (c->song != NULL) {
447                         screen_lyrics_load(c->song);
448                         screen_text_repaint(&text);
449                 }
450                 return true;
451         case CMD_EDIT:
452                 lyrics_edit();
453                 return true;
454         case CMD_SELECT:
455                 screen_lyrics_reload();
456                 return true;
458 #ifdef ENABLE_SONG_SCREEN
459         case CMD_SCREEN_SONG:
460                 if (current.song != NULL) {
461                         screen_song_switch(c, current.song);
462                         return true;
463                 }
465                 break;
466 #endif
467         case CMD_SCREEN_SWAP:
468                 screen_swap(c, current.song);
469                 return true;
471         case CMD_LOCATE:
472                 if (current.song != NULL) {
473                         screen_file_goto_song(c, current.song);
474                         return true;
475                 }
477                 return false;
479         default:
480                 break;
481         }
483         return false;
486 const struct screen_functions screen_lyrics = {
487         .init = lyrics_screen_init,
488         .exit = lyrics_exit,
489         .open = lyrics_open,
490         .update = lyrics_update,
491         .close = NULL,
492         .resize = lyrics_resize,
493         .paint = lyrics_paint,
494         .cmd = lyrics_cmd,
495         .get_title = lyrics_title,
496 };
498 void
499 screen_lyrics_switch(struct mpdclient *c, const struct mpd_song *song, bool f)
501         assert(song != NULL);
503         follow = f;
504         next_song = mpd_song_dup(song);
505         screen_switch(&screen_lyrics, c);