gui/artwork: Split out album art functions into a new file

I think this is cleaner, and it should make it easier to maintain this
code.

Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2016-07-20 12:02:47 -04:00
parent 8893cdb58b
commit 83724c5f6f
7 changed files with 96 additions and 73 deletions

View File

@ -1,4 +1,5 @@
6.4.16:
- Split album art code into a new file
- Fix PKGBUILD dependencies
6.4.16-rc:

78
gui/artwork.c Normal file
View File

@ -0,0 +1,78 @@
/*
* Copyright 2016 (c) Anna Schumaker.
*/
#include <core/audio.h>
#include <gui/builder.h>
#include <glib/gi18n.h>
static struct album *__artwork_cur_album = NULL;
static GdkPixbuf *__artwork_get_pixbuf(struct track *track)
{
gchar *path = album_artwork_path(track->tr_album);
GdkPixbuf *pix;
int height;
if (!path)
return NULL;
height = gui_builder_widget_height("o_position") +
gui_builder_widget_height("o_tags");
pix = gdk_pixbuf_new_from_file_at_scale(path, -1, height, true, NULL);
g_free(path);
return pix;
}
void gui_artwork_set_cover(void)
{
GtkImage *cover = GTK_IMAGE(gui_builder_widget("o_cover"));
struct track *track = audio_cur_track();
GdkPixbuf *pix;
if (!track || track->tr_album == __artwork_cur_album)
return;
pix = __artwork_get_pixbuf(track);
if (pix) {
gtk_image_set_from_pixbuf(cover, pix);
g_object_unref(G_OBJECT(pix));
} else
gtk_image_set_from_icon_name(cover, "image-missing", GTK_ICON_SIZE_DIALOG);
gtk_widget_set_sensitive(GTK_WIDGET(cover), pix != NULL);
__artwork_cur_album = pix ? track->tr_album : NULL;
}
void __artwork_select_cover(GtkButton *button)
{
struct track *track = audio_cur_track();
GtkFileFilter *filter;
GtkWidget *dialog;
gchar *path;
if (!track)
return;
filter = gtk_file_filter_new();
dialog = gtk_file_chooser_dialog_new("Choose an image",
GTK_WINDOW(gui_builder_widget("o_window")),
GTK_FILE_CHOOSER_ACTION_OPEN,
_("_Cancel"), GTK_RESPONSE_CANCEL,
_("_Open"), GTK_RESPONSE_ACCEPT,
NULL);
gtk_file_filter_add_mime_type(filter, "image/*");
gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(dialog), filter);
gtk_file_chooser_set_preview_widget_active(GTK_FILE_CHOOSER(dialog), true);
if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
path = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
album_artwork_import(track->tr_album, path);
gui_artwork_set_cover();
g_free(path);
}
gtk_widget_destroy(dialog);
g_object_unref(filter);
}

View File

@ -5,13 +5,11 @@
#include <core/collection.h>
#include <core/playlist.h>
#include <core/string.h>
#include <gui/artwork.h>
#include <gui/audio.h>
#include <gui/builder.h>
#include <gui/idle.h>
#include <gui/view.h>
#include <glib/gi18n.h>
static bool __audio_have_cover = false;
static inline void __audio_set_label(const gchar *label, const gchar *size,
const gchar *text)
@ -29,71 +27,6 @@ static inline void __audio_set_time_label(const gchar *label, unsigned int time)
g_free(str);
}
static GdkPixbuf *__audio_get_pixbuf(struct track *track)
{
gchar *path = album_artwork_path(track->tr_album);
GdkPixbuf *pix;
int height;
if (!path)
return NULL;
height = gui_builder_widget_height("o_position") +
gui_builder_widget_height("o_tags");
pix = gdk_pixbuf_new_from_file_at_scale(path, -1, height, true, NULL);
g_free(path);
return pix;
}
static void __audio_set_cover(void)
{
GtkImage *cover = GTK_IMAGE(gui_builder_widget("o_cover"));
GdkPixbuf *pix = __audio_get_pixbuf(audio_cur_track());
if (pix) {
gtk_image_set_from_pixbuf(cover, pix);
g_object_unref(G_OBJECT(pix));
} else
gtk_image_set_from_icon_name(cover, "image-missing", GTK_ICON_SIZE_DIALOG);
gtk_widget_set_sensitive(GTK_WIDGET(cover), pix != NULL);
__audio_have_cover = (pix != NULL);
}
void __audio_select_cover(GtkButton *button)
{
struct track *track = audio_cur_track();
GtkFileFilter *filter;
GtkWidget *dialog;
gchar *path;
if (!track)
return;
filter = gtk_file_filter_new();
dialog = gtk_file_chooser_dialog_new("Choose an image",
GTK_WINDOW(gui_builder_widget("o_window")),
GTK_FILE_CHOOSER_ACTION_OPEN,
_("_Cancel"), GTK_RESPONSE_CANCEL,
_("_Open"), GTK_RESPONSE_ACCEPT,
NULL);
gtk_file_filter_add_mime_type(filter, "image/*");
gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(dialog), filter);
gtk_file_chooser_set_preview_widget_active(GTK_FILE_CHOOSER(dialog), true);
if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
path = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
album_artwork_import(track->tr_album, path);
__audio_set_cover();
g_free(path);
}
gtk_widget_destroy(dialog);
g_object_unref(filter);
}
static void __audio_load(struct track *track)
{
__audio_set_label("o_title", "xx-large", track->tr_title);
@ -107,7 +40,7 @@ static void __audio_load(struct track *track)
playlist_has("Favorites", track));
gui_view_scroll();
__audio_set_cover();
gui_artwork_set_cover();
}
static void __audio_change_state(GstState state)
@ -163,8 +96,7 @@ static int __audio_timeout(gpointer data)
gtk_adjustment_set_value(progress, audio_position() / GST_SECOND);
__audio_set_time_label("o_position", audio_position() / GST_SECOND);
if (audio_cur_track() && !__audio_have_cover)
__audio_set_cover();
gui_artwork_set_cover();
return G_SOURCE_CONTINUE;
}

10
include/gui/artwork.h Normal file
View File

@ -0,0 +1,10 @@
/*
* Copyright 2016 (c) Anna Schumaker.
*/
#ifndef OCARINA_GUI_ARTWORK_H
#define OCARINA_GUI_ARTWORK_H
/* Called to set artwork for a track. */
void gui_artwork_set_cover(void);
#endif /* OCARINA_GUI_ARTWORK_H */

View File

@ -10,7 +10,8 @@ void __audio_favorite() {}
void __audio_hide() {}
void __audio_pause_changed() {}
void __audio_seek() {}
void __audio_select_cover() {}
void _artwork_select_cover() {}
#endif
#ifdef TEST_NEED_SIDEBAR

View File

@ -461,7 +461,7 @@
<property name="receives_default">True</property>
<property name="image">o_cover</property>
<property name="relief">none</property>
<signal name="clicked" handler="__audio_select_cover" swapped="no"/>
<signal name="clicked" handler="__artwork_select_cover" swapped="no"/>
</object>
<packing>
<property name="left_attach">1</property>

View File

@ -34,6 +34,7 @@ res += [ GuiTest("idle") ]
res += [ GuiTest("sidebar") ]
res += [ GuiTest("collection") ]
res += [ GuiTest("playlist") ]
gui_objs += [ env.Object("../../gui/artwork.c") ]
res += [ GuiTest("audio") ]
ignore.close()