1 /*************************************************************************/
2 /* Copyright (C) 2011-2014 matias <mati86dl@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 3 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, see <http://www.gnu.org/licenses/>. */
16 /*************************************************************************/
17 
18 #if defined(GETTEXT_PACKAGE)
19 #include <glib/gi18n-lib.h>
20 #else
21 #include <glib/gi18n.h>
22 #endif
23 
24 #include <glyr/glyr.h>
25 
26 #include "pragha-song-info-plugin.h"
27 #include "pragha-song-info-dialog.h"
28 #include "pragha-song-info-pane.h"
29 
30 #include "src/pragha-simple-async.h"
31 #include "src/pragha-utils.h"
32 
33 typedef struct {
34 	PraghaSongInfoPlugin *plugin;
35 	GlyrQuery             query;
36 	GlyrMemCache         *head;
37 } glyr_struct;
38 
39 static void
glyr_finished_successfully(glyr_struct * glyr_info)40 glyr_finished_successfully (glyr_struct *glyr_info)
41 {
42 	PraghaApplication *pragha;
43 	GtkWidget *window;
44 	gchar *title_header = NULL, *subtitle_header = NULL;
45 
46 	pragha = pragha_songinfo_plugin_get_application (glyr_info->plugin);
47 
48 	switch (glyr_info->head->type) {
49 		case GLYR_TYPE_LYRICS:
50 			window = pragha_application_get_window (pragha);
51 			title_header =  g_strdup_printf(_("Lyrics thanks to %s"), glyr_info->head->prov);
52 			subtitle_header = g_markup_printf_escaped (_("%s <small><span weight=\"light\">by</span></small> %s"), glyr_info->query.title, glyr_info->query.artist);
53 
54 			pragha_show_related_text_info_dialog (window, title_header, subtitle_header, glyr_info->head->data);
55 			break;
56 		case GLYR_TYPE_ARTIST_BIO:
57 			window = pragha_application_get_window (pragha);
58 			title_header =  g_strdup_printf(_("Artist info"));
59 			subtitle_header = g_strdup_printf(_("%s <small><span weight=\"light\">thanks to</span></small> %s"), glyr_info->query.artist, glyr_info->head->prov);
60 
61 			pragha_show_related_text_info_dialog (window, title_header, subtitle_header, glyr_info->head->data);
62 			break;
63 		case GLYR_TYPE_COVERART:
64 		default:
65 			break;
66 	}
67 
68 	g_free(title_header);
69 	g_free(subtitle_header);
70 
71 	glyr_free_list(glyr_info->head);
72 }
73 
74 static void
glyr_finished_incorrectly(glyr_struct * glyr_info)75 glyr_finished_incorrectly(glyr_struct *glyr_info)
76 {
77 	PraghaStatusbar *statusbar = pragha_statusbar_get ();
78 
79 	switch (glyr_info->query.type) {
80 		case GLYR_GET_LYRICS:
81 			pragha_statusbar_set_misc_text (statusbar, _("Lyrics not found."));
82 			break;
83 		case GLYR_GET_ARTIST_BIO:
84 			pragha_statusbar_set_misc_text (statusbar, _("Artist information not found."));
85 			break;
86 		case GLYR_GET_COVERART:
87 		default:
88 			break;
89 	}
90 	g_object_unref (statusbar);
91 }
92 
93 /*
94  * Final threads
95  */
96 
97 static gboolean
glyr_finished_thread_update(gpointer data)98 glyr_finished_thread_update (gpointer data)
99 {
100 	PraghaApplication *pragha;
101 	GtkWidget *window;
102 
103 	glyr_struct *glyr_info = data;
104 
105 	pragha = pragha_songinfo_plugin_get_application (glyr_info->plugin);
106 	window = pragha_application_get_window (pragha);
107 	remove_watch_cursor (window);
108 
109 	if(glyr_info->head != NULL)
110 		glyr_finished_successfully (glyr_info);
111 	else
112 		glyr_finished_incorrectly (glyr_info);
113 
114 	glyr_query_destroy (&glyr_info->query);
115 	g_slice_free (glyr_struct, glyr_info);
116 
117 	return FALSE;
118 }
119 
120 /* Get artist bio or lyric on a thread. */
121 
122 static gpointer
get_related_info_idle_func(gpointer data)123 get_related_info_idle_func (gpointer data)
124 {
125 	GlyrMemCache *head;
126 	GLYR_ERROR error;
127 
128 	glyr_struct *glyr_info = data;
129 
130 	head = glyr_get (&glyr_info->query, &error, NULL);
131 
132 	glyr_info->head = head;
133 
134 	return glyr_info;
135 }
136 
137 void
pragha_songinfo_plugin_get_info_to_dialog(PraghaSongInfoPlugin * plugin,GLYR_GET_TYPE type,const gchar * artist,const gchar * title)138 pragha_songinfo_plugin_get_info_to_dialog (PraghaSongInfoPlugin *plugin,
139                                            GLYR_GET_TYPE        type,
140                                            const gchar          *artist,
141                                            const gchar          *title)
142 {
143 	PraghaApplication *pragha;
144 	GtkWidget *window;
145 	GlyrDatabase *cache_db;
146 	glyr_struct *glyr_info;
147 
148 	glyr_info = g_slice_new0 (glyr_struct);
149 
150 	glyr_query_init (&glyr_info->query);
151 	glyr_opt_type (&glyr_info->query, type);
152 
153 	switch (type) {
154 		case GLYR_GET_ARTIST_BIO:
155 			glyr_opt_artist(&glyr_info->query, artist);
156 
157 			glyr_opt_lang (&glyr_info->query, "auto");
158 			glyr_opt_lang_aware_only (&glyr_info->query, TRUE);
159 			break;
160 		case GLYR_GET_LYRICS:
161 			glyr_opt_artist(&glyr_info->query, artist);
162 			glyr_opt_title(&glyr_info->query, title);
163 			break;
164 		default:
165 			break;
166 	}
167 
168 	cache_db = pragha_songinfo_plugin_get_cache (plugin);
169 
170 	glyr_opt_lookup_db (&glyr_info->query, cache_db);
171 	glyr_opt_db_autowrite (&glyr_info->query, TRUE);
172 
173 	glyr_info->plugin = plugin;
174 
175 	pragha = pragha_songinfo_plugin_get_application (plugin);
176 	window = pragha_application_get_window (pragha);
177 	set_watch_cursor (window);
178 
179 	pragha_async_launch (get_related_info_idle_func,
180 	                     glyr_finished_thread_update,
181 	                     glyr_info);
182 }
183 
184