4 * GD-Rom list manager - maintains the list of recently accessed images and
5 * available devices for the UI + config.
7 * Copyright (c) 2005 Nathan Keynes.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
22 #include <glib/gstrfuncs.h>
25 #include "gdrom/gdrom.h"
26 #include "drivers/cdrom/drive.h"
31 #define MAX_RECENT_ITEMS 5
33 #define FIRST_RECENT_INDEX (gdrom_device_count+2)
35 DEFINE_HOOK(gdrom_list_change_hook, gdrom_list_change_hook_t);
37 static GList *gdrom_device_list = NULL;
38 static GList *gdrom_recent_list = NULL;
39 static unsigned int gdrom_device_count = 0, gdrom_recent_count = 0;
41 gint gdrom_list_find( const gchar *name )
46 for( ptr = gdrom_device_list; ptr != NULL; ptr = g_list_next(ptr) ) {
47 cdrom_drive_t device = (cdrom_drive_t)ptr->data;
49 if( strcmp(device->name, name) == 0 ) {
54 for( ptr = gdrom_recent_list; ptr != NULL; ptr = g_list_next(ptr) ) {
55 gchar *file = (gchar *)ptr->data;
57 if( strcmp(file, name) == 0 ) {
65 * Update the recent list in the lxdream config (but does not save)
67 void gdrom_list_update_config()
69 lxdream_set_global_config_list_value( CONFIG_RECENT, gdrom_recent_list );
73 void gdrom_list_add_recent_item( const gchar *name )
75 gdrom_recent_list = g_list_prepend( gdrom_recent_list, g_strdup(name) );
76 if( g_list_length(gdrom_recent_list) > MAX_RECENT_ITEMS ) {
77 GList *ptr = g_list_nth( gdrom_recent_list, MAX_RECENT_ITEMS );
79 gdrom_recent_list = g_list_remove( gdrom_recent_list, ptr->data );
81 gdrom_recent_count ++;
83 gdrom_list_update_config();
86 void gdrom_list_move_to_front( const gchar *name )
89 for( ptr = gdrom_recent_list; ptr != NULL; ptr = g_list_next(ptr) ) {
90 gchar *file = (gchar *)ptr->data;
91 if( strcmp(file, name) == 0 ) {
92 gdrom_recent_list = g_list_delete_link( gdrom_recent_list, ptr );
93 gdrom_recent_list = g_list_prepend( gdrom_recent_list, file );
94 gdrom_list_update_config();
101 * Disc-changed callback from the GD-Rom driver. Updates the list accordingly.
103 gboolean gdrom_list_disc_changed( cdrom_disc_t disc, const gchar *disc_name, void *user_data )
105 gboolean list_changed = FALSE;
108 posn = gdrom_list_find( disc_name );
110 gdrom_list_add_recent_item( disc_name );
111 posn = FIRST_RECENT_INDEX;
113 } else if( posn > FIRST_RECENT_INDEX ) {
114 gdrom_list_move_to_front( disc_name );
115 posn = FIRST_RECENT_INDEX;
120 lxdream_set_global_config_path_value( CONFIG_GDROM, disc_name );
121 lxdream_save_config();
123 CALL_HOOKS( gdrom_list_change_hook, list_changed, posn );
128 * Drives-changed callback from the host CD-Rom drivers. Probably not likely to
129 * happen too often unless you're adding/removing external drives...
131 void gdrom_list_drives_changed( GList *device_list )
135 /************ Public interface ***********/
137 void gdrom_list_init()
139 gdrom_recent_list = lxdream_get_global_config_list_value( CONFIG_RECENT );
140 register_gdrom_disc_change_hook( gdrom_list_disc_changed, NULL );
142 gdrom_device_list = cdrom_drive_get_list();
143 gdrom_device_count = g_list_length(gdrom_device_list);
144 gdrom_recent_count = g_list_length(gdrom_recent_list);
146 // Run the hooks in case anyone registered before the list was initialized
147 CALL_HOOKS( gdrom_list_change_hook, TRUE, gdrom_list_get_selection() );
150 gboolean gdrom_list_set_selection( int posn, ERROR *err )
152 if( posn == 0 ) { // Always 'Empty'
153 gdrom_unmount_disc();
157 if( posn <= gdrom_device_count ) {
158 cdrom_drive_t device = g_list_nth_data(gdrom_device_list, posn-1);
159 return gdrom_mount_image(device->name, err);
162 posn -= FIRST_RECENT_INDEX;
163 if( posn >= 0 && posn < gdrom_recent_count ) {
164 gchar *entry = g_list_nth_data(gdrom_recent_list, posn);
165 return gdrom_mount_image(entry, err);
168 /* Should never happen */
169 SET_ERROR( err, LX_ERR_BUG, "Invalid selection: This is probably a bug" );
173 gint gdrom_list_get_selection( )
175 const char *name = gdrom_get_current_disc_name();
179 return gdrom_list_find(name);
183 int gdrom_list_size()
185 return gdrom_device_count + gdrom_recent_count + 2;
188 const gchar *gdrom_list_get_display_name( int posn )
194 if( posn <= gdrom_device_count ) {
195 cdrom_drive_t device = g_list_nth_data(gdrom_device_list, posn-1);
196 return device->display_name;
199 if( posn == gdrom_device_count + 1) {
203 if( posn < 0 || posn > gdrom_list_size() ) {
207 gchar *entry = g_list_nth_data(gdrom_recent_list, posn-FIRST_RECENT_INDEX);
208 return basename(entry);
211 const gchar *gdrom_list_get_filename( int posn )
217 if( posn <= gdrom_device_count ) {
218 cdrom_drive_t device = g_list_nth_data(gdrom_device_list, posn-1);
222 if( posn == gdrom_device_count + 1) {
226 if( posn < 0 || posn > gdrom_list_size() ) {
230 return g_list_nth_data(gdrom_recent_list, posn-FIRST_RECENT_INDEX);
.