Search
lxdream.org :: lxdream/src/gdlist.c :: diff
lxdream 0.9.1
released Jun 29
Download Now
filename src/gdlist.c
changeset 691:ad3356543392
next696:3b224db916e3
author nkeynes
date Thu Jun 19 04:40:37 2008 +0000 (11 years ago)
permissions -rw-r--r--
last change Refactor the gd-rom list management out of the GUI (devices, recent files, etc).
Add gd-rom list to the cocoa UI.
file annotate diff log raw
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/gdlist.c Thu Jun 19 04:40:37 2008 +0000
1.3 @@ -0,0 +1,225 @@
1.4 +/**
1.5 + * $Id$
1.6 + *
1.7 + * GD-Rom list manager - maintains the list of recently accessed images and
1.8 + * available devices for the UI + config.
1.9 + *
1.10 + * Copyright (c) 2005 Nathan Keynes.
1.11 + *
1.12 + * This program is free software; you can redistribute it and/or modify
1.13 + * it under the terms of the GNU General Public License as published by
1.14 + * the Free Software Foundation; either version 2 of the License, or
1.15 + * (at your option) any later version.
1.16 + *
1.17 + * This program is distributed in the hope that it will be useful,
1.18 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.19 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.20 + * GNU General Public License for more details.
1.21 + */
1.22 +
1.23 +#include <string.h>
1.24 +#include <stdlib.h>
1.25 +#include <glib/gstrfuncs.h>
1.26 +#include <glib/gi18n.h>
1.27 +#include <libgen.h>
1.28 +#include "gdrom/gdrom.h"
1.29 +#include "gdlist.h"
1.30 +#include "lxdream.h"
1.31 +#include "config.h"
1.32 +
1.33 +#define MAX_RECENT_ITEMS 5
1.34 +
1.35 +#define FIRST_RECENT_INDEX (gdrom_device_count+2)
1.36 +
1.37 +DEFINE_HOOK(gdrom_list_change_hook, gdrom_list_change_hook_t);
1.38 +
1.39 +static GList *gdrom_device_list = NULL;
1.40 +static GList *gdrom_recent_list = NULL;
1.41 +static unsigned int gdrom_device_count = 0, gdrom_recent_count = 0;
1.42 +
1.43 +gint gdrom_list_find( const gchar *name )
1.44 +{
1.45 + gint posn = 0;
1.46 + GList *ptr;
1.47 +
1.48 + for( ptr = gdrom_device_list; ptr != NULL; ptr = g_list_next(ptr) ) {
1.49 + gchar *device = (gchar *)ptr->data;
1.50 + posn++;
1.51 + if( strcmp(device, name) == 0 ) {
1.52 + return posn;
1.53 + }
1.54 + }
1.55 + posn++;
1.56 + for( ptr = gdrom_recent_list; ptr != NULL; ptr = g_list_next(ptr) ) {
1.57 + gchar *file = (gchar *)ptr->data;
1.58 + posn++;
1.59 + if( strcmp(file, name) == 0 ) {
1.60 + return posn;
1.61 + }
1.62 + }
1.63 + return -1;
1.64 +}
1.65 +
1.66 +/**
1.67 + * Update the recent list in the lxdream config (but does not save)
1.68 + */
1.69 +void gdrom_list_update_config()
1.70 +{
1.71 + GList *ptr;
1.72 + int size = 0;
1.73 + for( ptr = gdrom_recent_list; ptr != NULL; ptr = g_list_next(ptr) ) {
1.74 + size += strlen( (gchar *)ptr->data ) + 1;
1.75 + }
1.76 + char buf[size];
1.77 + strcpy( buf, (gchar *)gdrom_recent_list->data );
1.78 + for( ptr = g_list_next(gdrom_recent_list); ptr != NULL; ptr = g_list_next(ptr) ) {
1.79 + strcat( buf, ":" );
1.80 + strcat( buf, (gchar *)ptr->data );
1.81 + }
1.82 + lxdream_set_global_config_value( CONFIG_RECENT, buf );
1.83 +}
1.84 +
1.85 +
1.86 +void gdrom_list_add_recent_item( const gchar *name )
1.87 +{
1.88 + gdrom_recent_list = g_list_prepend( gdrom_recent_list, g_strdup(name) );
1.89 + if( g_list_length(gdrom_recent_list) > MAX_RECENT_ITEMS ) {
1.90 + GList *ptr = g_list_nth( gdrom_recent_list, MAX_RECENT_ITEMS );
1.91 + g_free( ptr->data );
1.92 + gdrom_recent_list = g_list_remove( gdrom_recent_list, ptr->data );
1.93 + } else {
1.94 + gdrom_recent_count ++;
1.95 + }
1.96 + gdrom_list_update_config();
1.97 +}
1.98 +
1.99 +void gdrom_list_move_to_front( const gchar *name )
1.100 +{
1.101 + GList *ptr;
1.102 + for( ptr = gdrom_recent_list; ptr != NULL; ptr = g_list_next(ptr) ) {
1.103 + gchar *file = (gchar *)ptr->data;
1.104 + if( strcmp(file, name) == 0 ) {
1.105 + gdrom_recent_list = g_list_delete_link( gdrom_recent_list, ptr );
1.106 + gdrom_recent_list = g_list_prepend( gdrom_recent_list, file );
1.107 + gdrom_list_update_config();
1.108 + return;
1.109 + }
1.110 + }
1.111 +}
1.112 +
1.113 +/**
1.114 + * Disc-changed callback from the GD-Rom driver. Updates the list accordingly.
1.115 + */
1.116 +gboolean gdrom_list_disc_changed( gdrom_disc_t disc, const gchar *disc_name, void *user_data )
1.117 +{
1.118 + gboolean list_changed = FALSE;
1.119 + int posn = 0;
1.120 + if( disc != NULL ) {
1.121 + posn = gdrom_list_find( disc_name );
1.122 + if( posn == -1 ) {
1.123 + gdrom_list_add_recent_item( disc_name );
1.124 + posn = FIRST_RECENT_INDEX;
1.125 + list_changed = TRUE;
1.126 + } else if( posn > FIRST_RECENT_INDEX ) {
1.127 + gdrom_list_move_to_front( disc_name );
1.128 + posn = FIRST_RECENT_INDEX;
1.129 + list_changed = TRUE;
1.130 + }
1.131 + }
1.132 +
1.133 + lxdream_set_global_config_value( CONFIG_GDROM, disc_name );
1.134 + lxdream_save_config();
1.135 +
1.136 + CALL_HOOKS( gdrom_list_change_hook, list_changed, posn );
1.137 + return TRUE;
1.138 +}
1.139 +
1.140 +/**
1.141 + * Drives-changed callback from the host CD-Rom drivers. Probably not likely to
1.142 + * happen too often unless you're adding/removing external drives...
1.143 + */
1.144 +void gdrom_list_drives_changed( GList *device_list )
1.145 +{
1.146 +}
1.147 +
1.148 +/************ Public interface ***********/
1.149 +
1.150 +void gdrom_list_init()
1.151 +{
1.152 + const gchar *recent = lxdream_get_config_value( CONFIG_RECENT );
1.153 + register_gdrom_disc_change_hook( gdrom_list_disc_changed, NULL );
1.154 + gdrom_device_list = gdrom_get_native_devices();
1.155 + if( recent != NULL ) {
1.156 + gchar **list = g_strsplit(recent, ":", MAX_RECENT_ITEMS);
1.157 + int i;
1.158 + for( i=0; list[i] != NULL; i++ ) {
1.159 + gdrom_recent_list = g_list_append( gdrom_recent_list, g_strdup(list[i]) );
1.160 + }
1.161 + g_strfreev(list);
1.162 + }
1.163 + gdrom_device_count = g_list_length(gdrom_device_list);
1.164 + gdrom_recent_count = g_list_length(gdrom_recent_list);
1.165 +
1.166 + // Run the hooks in case anyone registered before the list was initialized
1.167 + CALL_HOOKS( gdrom_list_change_hook, TRUE, gdrom_list_get_selection() );
1.168 +}
1.169 +
1.170 +gboolean gdrom_list_set_selection( int posn )
1.171 +{
1.172 + if( posn == 0 ) { // Always 'Empty'
1.173 + gdrom_unmount_disc();
1.174 + return TRUE;
1.175 + }
1.176 +
1.177 + if( posn <= gdrom_device_count ) {
1.178 + gchar *entry = g_list_nth_data(gdrom_device_list, posn-1);
1.179 + gdrom_mount_image(entry);
1.180 + return TRUE;
1.181 + }
1.182 +
1.183 + posn -= FIRST_RECENT_INDEX;
1.184 + if( posn >= 0 && posn < gdrom_recent_count ) {
1.185 + gchar *entry = g_list_nth_data(gdrom_recent_list, posn);
1.186 + gdrom_mount_image(entry);
1.187 + return TRUE;
1.188 + }
1.189 +
1.190 + return FALSE;
1.191 +}
1.192 +
1.193 +gint gdrom_list_get_selection( )
1.194 +{
1.195 + const char *name = gdrom_get_current_disc_name();
1.196 + if( name == NULL ) {
1.197 + return 0;
1.198 + } else {
1.199 + return gdrom_list_find(name);
1.200 + }
1.201 +}
1.202 +
1.203 +int gdrom_list_size()
1.204 +{
1.205 + return gdrom_device_count + gdrom_recent_count + 2;
1.206 +}
1.207 +
1.208 +const gchar *gdrom_list_get_display_name( int posn )
1.209 +{
1.210 + if( posn == 0 ) {
1.211 + return _("Empty");
1.212 + }
1.213 +
1.214 + if( posn <= gdrom_device_count ) {
1.215 + return g_list_nth_data(gdrom_device_list, posn-1);
1.216 + }
1.217 +
1.218 + if( posn == gdrom_device_count + 1) {
1.219 + return "";
1.220 + }
1.221 +
1.222 + if( posn < 0 || posn > gdrom_list_size() ) {
1.223 + return NULL;
1.224 + }
1.225 +
1.226 + gchar *entry = g_list_nth_data(gdrom_recent_list, posn-FIRST_RECENT_INDEX);
1.227 + return basename(entry);
1.228 +}
.