1 /*
2  *  Copyright (C) 2004,2005 Marc Pavot <marc.pavot@gmail.com>
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
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  */
19 
20 #include "ario-lyrics.h"
21 #include <glib.h>
22 #include <string.h>
23 #include <glib/gi18n.h>
24 #include "ario-util.h"
25 #include "ario-debug.h"
26 
27 static void ario_lyrics_create_ario_lyrics_dir (void);
28 
29 gchar*
ario_lyrics_make_lyrics_path(const gchar * artist,const gchar * title)30 ario_lyrics_make_lyrics_path (const gchar *artist,
31                               const gchar *title)
32 {
33         ARIO_LOG_FUNCTION_START;
34         char *ario_lyrics_path;
35         char *filename;
36 
37         filename = g_strdup_printf ("%s-%s.txt", artist, title);
38 
39         ario_util_sanitize_filename (filename);
40 
41         /* The returned path is ~/.config/ario/lyrics/filename */
42         ario_lyrics_path = g_build_filename (ario_util_config_dir (), "lyrics", filename, NULL);
43         g_free (filename);
44 
45         return ario_lyrics_path;
46 }
47 
48 static void
ario_lyrics_create_ario_lyrics_dir(void)49 ario_lyrics_create_ario_lyrics_dir (void)
50 {
51         ARIO_LOG_FUNCTION_START;
52         gchar *ario_lyrics_dir;
53 
54         ario_lyrics_dir = g_build_filename (ario_util_config_dir (), "lyrics", NULL);
55 
56         /* If the lyrics directory doesn't exist, we create it */
57         if (!ario_util_uri_exists (ario_lyrics_dir))
58                 ario_util_mkdir (ario_lyrics_dir);
59         g_free (ario_lyrics_dir);
60 }
61 
62 void
ario_lyrics_remove_lyrics(const gchar * artist,const gchar * title)63 ario_lyrics_remove_lyrics (const gchar *artist,
64                            const gchar *title)
65 {
66         ARIO_LOG_FUNCTION_START;
67         gchar *ario_lyrics_path;
68 
69         if (!ario_lyrics_lyrics_exists (artist, title))
70                 return;
71 
72         /* Delete the lyrics*/
73         ario_lyrics_path = ario_lyrics_make_lyrics_path (artist, title);
74         if (ario_util_uri_exists (ario_lyrics_path))
75                 ario_util_unlink_uri (ario_lyrics_path);
76         g_free (ario_lyrics_path);
77 }
78 
79 ArioLyrics *
ario_lyrics_get_local_lyrics(const gchar * artist,const gchar * title)80 ario_lyrics_get_local_lyrics (const gchar *artist,
81                               const gchar *title)
82 {
83         ARIO_LOG_FUNCTION_START;
84         ArioLyrics *lyrics = NULL;
85         gchar *ario_lyrics_path;
86         gchar *read_data;
87 
88         if (!ario_lyrics_lyrics_exists (artist, title))
89                 return NULL;
90 
91         ario_lyrics_path = ario_lyrics_make_lyrics_path (artist, title);
92 
93         if (ario_file_get_contents (ario_lyrics_path,
94                                     &read_data, NULL, NULL)) {
95                 lyrics = (ArioLyrics *) g_malloc0 (sizeof (ArioLyrics));
96                 lyrics->lyrics = read_data;
97                 lyrics->artist = g_strdup (artist);
98                 lyrics->title = g_strdup (title);
99         }
100         g_free (ario_lyrics_path);
101 
102         return lyrics;
103 }
104 
105 void
ario_lyrics_free(ArioLyrics * lyrics)106 ario_lyrics_free (ArioLyrics *lyrics)
107 {
108         if (lyrics) {
109                 g_free (lyrics->artist);
110                 g_free (lyrics->title);
111                 g_free (lyrics->lyrics);
112                 g_free (lyrics);
113         }
114 }
115 
116 void
ario_lyrics_candidate_free(ArioLyricsCandidate * candidate)117 ario_lyrics_candidate_free (ArioLyricsCandidate *candidate)
118 {
119         if (candidate) {
120                 g_free (candidate->artist);
121                 g_free (candidate->title);
122                 g_free (candidate->data);
123                 g_free (candidate);
124         }
125 }
126 
127 ArioLyricsCandidate *
ario_lyrics_candidate_copy(const ArioLyricsCandidate * candidate)128 ario_lyrics_candidate_copy (const ArioLyricsCandidate *candidate)
129 {
130         ArioLyricsCandidate *ret;
131 
132         ret = (ArioLyricsCandidate *) g_malloc (sizeof (ArioLyricsCandidate));
133 
134         ret->artist = g_strdup (candidate->artist);
135         ret->title = g_strdup (candidate->title);
136         ret->data = g_strdup (candidate->data);
137         ret->lyrics_provider = candidate->lyrics_provider;
138 
139         return ret;
140 }
141 
142 gboolean
ario_lyrics_save_lyrics(const gchar * artist,const gchar * title,const gchar * lyrics)143 ario_lyrics_save_lyrics (const gchar *artist,
144                          const gchar *title,
145                          const gchar *lyrics)
146 {
147         ARIO_LOG_FUNCTION_START;
148         gboolean ret;
149         gchar *ario_lyrics_path;
150 
151         if (!artist || !title || !lyrics)
152                 return FALSE;
153 
154         /* If the lyrics directory doesn't exist, we create it */
155         ario_lyrics_create_ario_lyrics_dir ();
156 
157         /* The path for the lyrics */
158         ario_lyrics_path = ario_lyrics_make_lyrics_path (artist, title);
159 
160         ret = ario_file_set_contents (ario_lyrics_path,
161                                       lyrics, -1,
162                                       NULL);
163 
164         g_free (ario_lyrics_path);
165 
166         return ret;
167 }
168 
169 gboolean
ario_lyrics_lyrics_exists(const gchar * artist,const gchar * title)170 ario_lyrics_lyrics_exists (const gchar *artist,
171                            const gchar *title)
172 {
173         ARIO_LOG_FUNCTION_START;
174         gchar *ario_lyrics_path;
175         gboolean result;
176 
177         /* The path for the lyrics */
178         ario_lyrics_path = ario_lyrics_make_lyrics_path (artist, title);
179 
180         result = ario_util_uri_exists (ario_lyrics_path);
181 
182         g_free (ario_lyrics_path);
183 
184         return result;
185 }
186 
187 void
ario_lyrics_prepend_infos(ArioLyrics * lyrics)188 ario_lyrics_prepend_infos (ArioLyrics *lyrics)
189 {
190         GString *string;
191         gchar *toprepend;
192 
193         if (!lyrics)
194                 return;
195 
196         if (lyrics->artist && lyrics->title) {
197                 toprepend = g_strdup_printf ("%s - %s\n\n", lyrics->artist, lyrics->title);
198         } else if (lyrics->artist) {
199                 toprepend = g_strdup_printf ("%s\n\n", lyrics->artist);
200         } else if (lyrics->title) {
201                 toprepend = g_strdup_printf ("%s\n\n", lyrics->title);
202         } else {
203                 return;
204         }
205 
206         string = g_string_new (lyrics->lyrics);
207         string = g_string_prepend (string, toprepend);
208         g_free (lyrics->lyrics);
209         lyrics->lyrics = g_string_free (string, FALSE);
210         g_free (toprepend);
211 }
212