Update copyright notices
[gmpc-random-playlist.git] / src / main.c
blob5dc04c85d66041ff99dbc8793751efd38fd81274
1 /* gmpc-random-playlist (GMPC plugin)
2 * Copyright (C) 2006-2009 Qball Cow <qball@sarine.nl>
3 * Project homepage: http://gmpcwiki.sarine.nl/
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include <stdio.h>
21 #include <string.h>
25 #include <glade/glade.h>
26 #include <gmpc/plugin.h>
27 #include <libmpd/debug_printf.h>
28 #include <config.h>
30 /* External pointer + function, there internal from gmpc */
31 extern GladeXML *pl3_xml;
32 /** in gmpc */
33 void pl3_option_menu_activate();
35 static int get_enabled() {
36 return cfg_get_single_value_as_int_with_default(config,"random-playlist", "enabled",TRUE);
38 static void set_enabled(int enabled) {
39 cfg_set_single_value_as_int(config,"random-playlist", "enabled", enabled);
40 pl3_option_menu_activate();
42 static void rp_start()
44 MpdData *data = mpd_database_get_complete(connection);
45 GRand *rand = g_rand_new();
46 int value, pos = cfg_get_single_value_as_int_with_default(config, "random-playlist", "addfact",20)*100;
47 mpd_playlist_clear(connection);
48 for(;data;data = mpd_data_get_next(data))
50 value = g_rand_int_range(rand, 0,10000);
51 /** add 20% */
52 if(value < pos)
54 mpd_playlist_queue_add(connection, data->song->file);
57 mpd_playlist_queue_commit(connection);
58 if(cfg_get_single_value_as_int_with_default(config, "random-playlist", "shuffle",TRUE))
60 mpd_playlist_shuffle(connection);
62 g_rand_free(rand);
66 static int rp_right_mouse_menu(GtkWidget *menu, int type, GtkWidget *tree, GdkEventButton *event)
68 gmpcPlugin *plug = plugin_get_from_id(type);
69 if(!cfg_get_single_value_as_int_with_default(config, "random-playlist", "enabled", TRUE)) {
70 return 0;
72 debug_printf(DEBUG_INFO,"Random playlist right mouse clicked");
73 if(!strcmp(plug->name, "Current Playlist Browser"))
75 GtkWidget *item;
76 item = gtk_image_menu_item_new_with_label("Generate Random Playlist");
77 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item),
78 gtk_image_new_from_stock(GTK_STOCK_ADD, GTK_ICON_SIZE_MENU));
79 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
80 g_signal_connect(G_OBJECT(item), "activate", G_CALLBACK(rp_start), NULL);
81 return 1;
83 return 0;
88 gmpcPlBrowserPlugin rp_gpb = {
89 .cat_right_mouse_menu = rp_right_mouse_menu
93 gmpcPlugin plugin = {
94 .name = "Random Playlist",
95 .version = {PLUGIN_MAJOR_VERSION,PLUGIN_MINOR_VERSION,PLUGIN_MICRO_VERSION},
96 .plugin_type = GMPC_PLUGIN_PL_BROWSER,
97 .browser = &rp_gpb, /* browser intergration */
98 .get_enabled = get_enabled,
99 .set_enabled = set_enabled
101 int plugin_api_version = PLUGIN_API_VERSION;