Skip to content

Commit 2d7beb1

Browse files
committed
Use GLib to decode/encode URIs
1 parent bad0c5b commit 2d7beb1

File tree

6 files changed

+47
-184
lines changed

6 files changed

+47
-184
lines changed

CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ set(SRC_LIST
77
src/mpris-player.c
88
src/xmms2.c
99
src/mpris.c
10-
src/uri.c
1110
src/art.c
1211
)
1312

src/art.c

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include <string.h>
22
#include <strings.h>
33
#include <dirent.h>
4+
#include <stdlib.h>
5+
#include <linux/limits.h>
46

57
#include "art.h"
68

@@ -18,16 +20,22 @@ static const char* ART_NAMES[] = {
1820
/** The length of typical album art names. */
1921
static const size_t ART_NAMES_LENGTH = sizeof(ART_NAMES) / sizeof(char*);
2022

21-
bool find_album_art(char** buffer, size_t path_size) {
22-
char* path = *buffer;
23+
char* find_album_art(const char* filename) {
24+
size_t path_size = strlen(filename) + NAME_MAX;
25+
char* path = calloc(path_size, sizeof(char));
26+
strcpy(path, filename);
2327

2428
// Replace the path with just the dirname.
25-
char* last_slash = strrchr(path, '/');
29+
char* basename_ptr = strrchr(path, '/');
2630

27-
if (last_slash) {
28-
memset(last_slash + 1, '\0', path_size - (last_slash - path) - 1);
31+
if (!basename_ptr) {
32+
return NULL;
2933
}
3034

35+
// Delete the characters after slash to remove the basename.
36+
++basename_ptr;
37+
memset(basename_ptr, '\0', path_size - (basename_ptr - path));
38+
3139
bool match_found = false;
3240

3341
for (size_t index = 0; index < ART_NAMES_LENGTH; ++index) {
@@ -47,13 +55,15 @@ bool find_album_art(char** buffer, size_t path_size) {
4755
}
4856

4957
if (match_found) {
50-
// Update the path to replace the basename with the match.
51-
strcpy(last_slash + 1, entry->d_name);
52-
break;
58+
strcpy(basename_ptr, entry->d_name);
5359
}
5460

5561
closedir(dir);
62+
63+
if (match_found) {
64+
break;
65+
}
5666
}
5767

58-
return match_found;
68+
return match_found ? path : NULL;
5969
}

src/art.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@
66

77
/**
88
* Find album art for file, given a path to an audio file.
9-
*
10-
* If album art is found, the the path will be updated to point to it
11-
* instead.
129
*/
13-
bool find_album_art(char** buffer, size_t path_size);
10+
char* find_album_art(const char* filename);
1411

1512
#endif /* __XMMS2_MPRIS_ART_H__ */

src/uri.c

Lines changed: 0 additions & 122 deletions
This file was deleted.

src/uri.h

Lines changed: 0 additions & 25 deletions
This file was deleted.

src/xmms2-mpris.c

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
#include <linux/limits.h>
44

55
#include <gio/gio.h>
6+
#include <glib.h>
67

78
#include "track-info.h"
89
#include "xmms2.h"
910
#include "mpris.h"
10-
#include "uri.h"
1111
#include "art.h"
1212

1313
/** The global data for the app. */
@@ -134,33 +134,37 @@ void handle_status(PlaybackStatus status) {
134134
}
135135

136136
void handle_track_info(XmmsTrackInfo* info) {
137-
// Create a buffer with enough size to store the
138-
// URI converted to a path, plus a new filename on the end.
139-
size_t buffer_size = strlen(info->url) + NAME_MAX + 1;
140-
char* buffer = calloc(buffer_size, sizeof(char));
141-
char* out_buffer = NULL;
142-
143-
if (
144-
uri_to_path(&buffer, info->url)
145-
&& find_album_art(&buffer, buffer_size)
146-
) {
147-
// Create a buffer with enough size to store percent
148-
// encodings of all of the characters.
149-
size_t out_buffer_size = buffer_size + NAME_MAX * 2;
150-
out_buffer = calloc(out_buffer_size, sizeof(char));
151-
152-
path_to_uri(&out_buffer, buffer);
153-
154-
info->art_url = out_buffer;
137+
char* filename = g_filename_from_uri(info->url, NULL, NULL);
138+
139+
// Replace plusses with spaces.
140+
// XMMS2 encodes spaces in filenames as spaces.
141+
if (filename) {
142+
size_t filename_length = strlen(filename);
143+
144+
for (size_t index = 0; index < filename_length; ++index) {
145+
if (filename[index] == '+') {
146+
filename[index] = ' ';
147+
}
148+
}
155149
}
156150

157-
display_track_info(app.player, info);
151+
char* art_filename = NULL;
152+
char* art_uri = NULL;
158153

159-
free(buffer);
154+
if (filename) {
155+
art_filename = find_album_art(filename);
160156

161-
if (out_buffer) {
162-
free(out_buffer);
157+
if (art_filename) {
158+
art_uri = g_filename_to_uri(art_filename, NULL, NULL);
159+
info->art_url = art_uri;
160+
}
163161
}
162+
163+
display_track_info(app.player, info);
164+
165+
g_free(filename);
166+
free(art_filename);
167+
g_free(art_uri);
164168
}
165169

166170
void handle_playlist_position_change(int32_t position, int32_t length) {

0 commit comments

Comments
 (0)