/
plugin.c
323 lines (279 loc) · 10 KB
/
plugin.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/* gmpc-shufpl (GMPC plugin)
* Copyright (C) 2006-2009 Qball Cow <qball@sarine.nl>
* Project homepage: http://gmpcwiki.sarine.nl/
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <gtk/gtk.h>
#include <gmpc/plugin.h>
#include <glade/glade.h>
#include <libmpd/libmpd-internal.h>
#include <gmpc/misc.h>
void shufpl_set_enabled(int enabled);
int shufpl_get_enabled(void);
int shufpl_tool_menu(GtkWidget *menu);
extern GtkTreeModel *playlist;
/* Allow gmpc to check the version the plugin is compiled against */
int plugin_api_version = PLUGIN_API_VERSION;
/**
* Define the plugin structure
*/
gmpcPlugin plugin = {
/* name */
.name = "Playlist shuffler",
/* version */
.version = {0,20,0},
/* type */
.plugin_type = GMPC_PLUGIN_NO_GUI,
/** enable/disable */
.get_enabled = shufpl_get_enabled,
.set_enabled = shufpl_set_enabled,
.tool_menu_integration = shufpl_tool_menu
};
GList *items = NULL;
GtkListStore *tag_list = NULL;
typedef struct{
GtkWidget *combo;
GtkWidget *box;
GtkWidget *button;
}Item;
int shufpl_get_enabled(void)
{
return cfg_get_single_value_as_int_with_default(config, "shufpl", "enable", TRUE);
}
void shufpl_set_enabled(int enabled)
{
cfg_set_single_value_as_int(config, "shufpl", "enable", enabled);
pl3_tool_menu_update();
}
/********************************************************
* This code is copy-pasted from playlistsort-plugin,
* which I tottally use for my own
********************************************************/
int playlistsort_sort(gpointer aa, gpointer bb, gpointer data)
{
MpdData_real *a = *(MpdData_real **)aa;
MpdData_real *b = *(MpdData_real **)bb;
int retv =0;
int i;
int *field = ((gint *)(data));
/** Filter */
if(!a && !b) return 0;
if(!a) return -1;
if(!b) return 1;
for(i=0;field[i] != -1;i++)
{
int do_int = 0;
char *sa= NULL, *sb = NULL;
switch(field[i])
{
case MPD_TAG_ITEM_TITLE:
sa = a->song->title;
sb = b->song->title;
break;
case MPD_TAG_ITEM_ARTIST:
sa = a->song->artist;
sb = b->song->artist;
break;
case MPD_TAG_ITEM_ALBUM:
sa = a->song->album;
sb = b->song->album;
break;
case MPD_TAG_ITEM_GENRE:
sa = a->song->genre;
sb = b->song->genre;
break;
case MPD_TAG_ITEM_TRACK:
sa = a->song->track;
sb = b->song->track;
do_int = 1;
break;
case MPD_TAG_ITEM_NAME:
sa = a->song->name;
sb = b->song->name;
break;
case MPD_TAG_ITEM_COMPOSER:
sa = a->song->composer;
sb = b->song->composer;
break;
case MPD_TAG_ITEM_PERFORMER:
sa = a->song->performer;
sb = b->song->performer;
break;
case MPD_TAG_ITEM_COMMENT:
sa = a->song->comment;
sb = b->song->comment;
break;
case MPD_TAG_ITEM_DISC:
sa = a->song->disc;
sb = b->song->disc;
break;
case MPD_TAG_ITEM_DATE:
sa = a->song->date;
sb = b->song->date;
break;
case MPD_TAG_ITEM_FILENAME:
sa = a->song->file;
sb = b->song->file;
break;
default:
g_assert(FALSE);
return 0;
}
if(sa && sb){
if(do_int == 0)
{
gchar *aa,*bb;
aa = g_utf8_strdown(sa, -1);
bb = g_utf8_strdown(sb, -1);
retv = g_utf8_collate(aa,bb);
g_free(aa);
g_free(bb);
}else{
gint64 aa,bb;
aa = g_ascii_strtoll(sa, NULL, 10);
bb = g_ascii_strtoll(sb, NULL, 10);
retv = (gint)aa-bb;
}
}
else if(sa == sb) retv = 0;
else if (sa == NULL) retv = -1;
else retv = 1;
if(retv != 0)
{
return retv;
}
}
return 0;
}
/***************************************************************
* This function is differ from it's original to meet my needs
***************************************************************/
void playlistsort_start_field()
{
GList *node, *list=NULL;
GtkTreeIter iter;
int i;
int fields[4];
GList *a;
MpdData *data2,*data = mpd_playlist_get_changes(connection, -1);
fields[0] = MPD_TAG_ITEM_ALBUM;
fields[1] = MPD_TAG_ITEM_DISC;
fields[2] = MPD_TAG_ITEM_TRACK;
fields[3] = -1;
data2 = misc_sort_mpddata(data,(GCompareDataFunc)playlistsort_sort,(gpointer)fields);
i=0;
for(data = mpd_data_get_first(data); data;data = mpd_data_get_next(data)){
mpd_playlist_move_id(connection, data->song->id, i);
i++;
}
mpd_playlist_queue_commit(connection);
}
/********************************************************
* Shuffling by track
********************************************************/
void shufpl_bytrack()
{
mpd_playlist_shuffle(connection);
}
/********************************************************
* Shuffling by album
********************************************************/
gint id = 0;
void shufpl_submit_album(gpointer data, gpointer user_data){
mpd_playlist_move_id(connection, ((mpd_Song *)data)->id, id++);
}
void shufpl_commit_albums(gpointer data, gpointer user_data){
g_ptr_array_foreach((GPtrArray *)data, shufpl_submit_album, NULL);
}
void shufpl_album_free(gpointer data){
g_ptr_array_free((GPtrArray*) data, TRUE);
}
void shufpl_byalbum(){
// Before shuffling we need to sort queue by album-disk-track
// 'cos after that order of songs in album will be correct.
playlistsort_start_field();
/// P.S. Of course, I tried to sort each album just before commiting
/// but for some unknown reason that just don't work.
/// May be you can figure out why and correct that?
MpdData * data = mpd_playlist_get_changes(connection, -1); // List with songs in current playlist
GData * albums_list = NULL; // Stores pointers to all founded albums, accessible by album_names
g_datalist_init(&albums_list);
GPtrArray * albums_arr = g_ptr_array_new(); // Stores pointers to all founded albums, accessible by indexes
GPtrArray * shuffled_albums_arr = g_ptr_array_new(); // Stores result of shuffling albums from albums_arr
GPtrArray * album; // Stores tracks of a album
gchar * album_name = NULL;
// Getting list of albums in current playlist
for(data = mpd_data_get_first(data); data;data = mpd_data_get_next(data)){
g_free(album_name);
album_name = g_utf8_strdown(data->song->album, -1);
if (!(album = g_datalist_get_data(&albums_list, album_name))) // if we have no such album in list yet
{
album = g_ptr_array_new();
g_ptr_array_add(album, data->song);
g_ptr_array_add(albums_arr, album);
g_datalist_set_data(&albums_list, album_name, album);
} else {
g_ptr_array_add(album, data->song);
}
}
// Current album should be on top of queue
mpd_Song * song = mpd_playlist_get_current_song(connection);
if (song)
album = g_datalist_get_data(&albums_list, g_utf8_strdown(song->album, -1));
if(album){
g_ptr_array_add(shuffled_albums_arr, album);
g_ptr_array_remove_fast(albums_arr, album);
}
// Other albums should be shuffled
gint i, from, len = albums_arr->len;
for (i = 0; i<len; i++){
from = g_random_int_range(0, albums_arr->len);
g_ptr_array_add(shuffled_albums_arr, g_ptr_array_index(albums_arr, from));
g_ptr_array_remove_index_fast(albums_arr, from);
}
// Submitting tracks
id = 0;
g_ptr_array_foreach(shuffled_albums_arr, shufpl_commit_albums, NULL);
mpd_playlist_queue_commit(connection);
// Freeing memory
g_ptr_array_set_free_func(shuffled_albums_arr, shufpl_album_free);
g_ptr_array_free(shuffled_albums_arr, TRUE);
g_datalist_clear(&albums_list);
mpd_data_free(data);
}
/// TODO in v. 0.3.0:
/********************************************************
* Shuffling by artist
********************************************************/
void shufpl_byartist(){}
int shufpl_tool_menu(GtkWidget *menu)
{
GtkWidget *item;
GtkWidget *submenu;
if(!shufpl_get_enabled()) return;
submenu = gtk_menu_new();
GtkWidget *track, *album, *artist;
track = gtk_menu_item_new_with_label("by track");
album = gtk_menu_item_new_with_label("by album");
//artist = gtk_menu_item_new_with_label("by artist");
gtk_menu_append(GTK_MENU(submenu), track);
gtk_menu_append(GTK_MENU(submenu), album);
//gtk_menu_append(GTK_MENU(submenu), artist);
g_signal_connect(G_OBJECT(track), "activate", G_CALLBACK(shufpl_bytrack), NULL);
g_signal_connect(G_OBJECT(album), "activate", G_CALLBACK(shufpl_byalbum), NULL);
//g_signal_connect(G_OBJECT(artist), "activate", G_CALLBACK(shufpl_byartist), NULL);
item = gtk_menu_item_new_with_label("Shuffle playlist");
gtk_menu_item_set_submenu(item, submenu);
gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
return 1;
}