File: dynamic_list.c

package info (click to toggle)
gnomp3 0.1.7-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 488 kB
  • ctags: 342
  • sloc: ansic: 3,285; makefile: 74; awk: 33
file content (140 lines) | stat: -rw-r--r-- 3,616 bytes parent folder | download | duplicates (3)
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
/**
 ** dynamic_list.c - generation/loading of lists based on statistics and 
 **                  other phenomena
 **
 ** Copyright (C) 2000 Matthew Pratt <mattpratt@yahoo.com>
 **
 ** This software is licensed under the terms of the GNU General 
 ** Public License (GPL). Please see the file LICENSE for details.
 **/ 

#include <string.h>

#include "gnomp3.h"
#include "dynamic_list.h"
#include "mp3list.h"
#include "playlist.h"
#include "xmms_play.h"

/******************************************************************************
 * The dynamic lists genertors of gnomp3 reside here. There exists a framework
 * for easily adding generators simply by adding to this file, instead of
 * having to play with the playlist loading/saving/displaying code.
 *
 * To add a new generator:
 *   Write the generator function. Add an entry to the dynamic_lists array 
 * which conatins the name of your generator and a pointer to the generator
 * function.
 *****************************************************************************/

/*
 * Generate a playlist of a fixed size of randoms songs from all those 
 * available
 */
void dynamic_list_random()
{
    int i;
    int len = g_list_length(mp3list);
    GList *ptr;

    for(i = 0; i < gnomp3.dynamic_list_size; i++){
	ptr = g_list_nth( mp3list, 1 + (int) ((float)len * rand()/(RAND_MAX+1.0)));
	playlist_add_song( ptr->data);
    }
}

/*
 * Load the current playlist from XMMS if available.
 */
void dynamic_list_xmms()
{   
    if(!gnomp3.tight_xmms || !gnomp3.use_xmms)
	return;

    xmms_play_list_load();
}

/*
 * Gets the newsest songs.
 */
int time_cmp(gconstpointer a, gconstpointer b){
    const MP3 *m1 = a, *m2 = b;
    if(m1->file_time > m2->file_time)return -1;
    if(m2->file_time > m1->file_time)return 1;
    return 0;
}

void dynamic_list_newest()
{
    GList *ptr;
    GList *sorted = NULL;
    int i;

    for(ptr = mp3list; ptr; ptr = ptr->next){
	sorted = g_list_insert_sorted(sorted, ptr->data, time_cmp);
    }

    for(ptr = sorted, i = 0; i < gnomp3.dynamic_list_size && ptr; i++){
	playlist_add_song( ptr->data);
	ptr = ptr->next;
    }
}

struct dynamic_list dynamic_lists[] = {
    {"Dynamic list: RANDOM", dynamic_list_random},
    {"Dynamic list: NEWEST", dynamic_list_newest},
#ifdef ENABLE_XMMS
    {"Current XMMS playlist", dynamic_list_xmms},
#endif 
    {NULL,NULL}
};

/******************************************************************************
 * The following functions are the framework for displaying/activating the
 * above list generation functions. They are integrated into the playlist
 * loading/saving/displaying infrastructure of gnomp3.
 *****************************************************************************/

/*
 * Add each name in the array of dynamic lists to given list.
 */
GList *dynamic_list_add_names(GList *list)
{
    int i;
    for(i = 0; dynamic_lists[i].name; i++){
	list = g_list_append(list, dynamic_lists[i].name);
    }
    return list;
}

/*
 * Search the list array of dynamic lists for an entry with its name macthing
 * that given. Return a pointer to the entry if found, else NULL.
 */
struct dynamic_list *dynamic_list_find(char *name)
{
    int i;
    for(i = 0; dynamic_lists[i].name; i++){
	if(!strcmp(dynamic_lists[i].name, name))
	    return &dynamic_lists[i];
    }
    return NULL;
}

/*
 * Activate the dynamic list with the given name.
 */
int dynamic_list_load(char *name)
{
    struct dynamic_list *list;

    list = dynamic_list_find(name);
    if(!list)
	return FALSE;

    strcpy(gnomp3.playlist_loaded, list->name);
    list->load_callback();
    return TRUE;
}