Search
lxdream.org :: lxdream/src/gdlist.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/gdlist.c
changeset 755:ab873907b00e
prev736:a02d1475ccfd
next1034:7044e01148f0
author nkeynes
date Wed Jul 30 03:00:40 2008 +0000 (11 years ago)
permissions -rw-r--r--
last change Add debian changelog file
view annotate diff log raw
     1 /**
     2  * $Id$
     3  *
     4  * GD-Rom list manager - maintains the list of recently accessed images and
     5  * available devices for the UI + config. 
     6  *
     7  * Copyright (c) 2005 Nathan Keynes.
     8  *
     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.
    13  *
    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.
    18  */
    20 #include <string.h>
    21 #include <stdlib.h>
    22 #include <glib/gstrfuncs.h>
    23 #include <libgen.h>
    24 #include "gettext.h"
    25 #include "gdrom/gdrom.h"
    26 #include "gdlist.h"
    27 #include "lxdream.h"
    28 #include "config.h"
    30 #define MAX_RECENT_ITEMS 5
    32 #define FIRST_RECENT_INDEX (gdrom_device_count+2)
    34 DEFINE_HOOK(gdrom_list_change_hook, gdrom_list_change_hook_t);
    36 static GList *gdrom_device_list = NULL;
    37 static GList *gdrom_recent_list = NULL;
    38 static unsigned int gdrom_device_count = 0, gdrom_recent_count = 0;
    40 gint gdrom_list_find( const gchar *name )
    41 {
    42     gint posn = 0;
    43     GList *ptr;
    45     for( ptr = gdrom_device_list; ptr != NULL; ptr = g_list_next(ptr) ) {
    46         gdrom_device_t device = (gdrom_device_t)ptr->data;
    47         posn++;
    48         if( strcmp(device->name, name) == 0 ) {
    49             return posn;
    50         }
    51     }
    52     posn++;
    53     for( ptr = gdrom_recent_list; ptr != NULL; ptr = g_list_next(ptr) ) {
    54         gchar *file = (gchar *)ptr->data;
    55         posn++;
    56         if( strcmp(file, name) == 0 ) {
    57             return posn;
    58         }
    59     }
    60     return -1;
    61 }
    63 /**
    64  * Update the recent list in the lxdream config (but does not save)  
    65  */
    66 void gdrom_list_update_config()
    67 {
    68     GList *ptr;
    69     int size = 0;
    70     for( ptr = gdrom_recent_list; ptr != NULL; ptr = g_list_next(ptr) ) {
    71         size += strlen( (gchar *)ptr->data ) + 1;
    72     }
    73     char buf[size];
    74     strcpy( buf, (gchar *)gdrom_recent_list->data );
    75     for( ptr = g_list_next(gdrom_recent_list); ptr != NULL; ptr = g_list_next(ptr) ) {
    76         strcat( buf, ":" );
    77         strcat( buf, (gchar *)ptr->data );
    78     }
    79     lxdream_set_global_config_value( CONFIG_RECENT, buf );
    80 }
    83 void gdrom_list_add_recent_item( const gchar *name )
    84 {
    85     gdrom_recent_list = g_list_prepend( gdrom_recent_list, g_strdup(name) );
    86     if( g_list_length(gdrom_recent_list) > MAX_RECENT_ITEMS ) {
    87         GList *ptr = g_list_nth( gdrom_recent_list, MAX_RECENT_ITEMS );
    88         g_free( ptr->data );
    89         gdrom_recent_list = g_list_remove( gdrom_recent_list, ptr->data );
    90     } else {
    91         gdrom_recent_count ++;
    92     }
    93     gdrom_list_update_config();
    94 }
    96 void gdrom_list_move_to_front( const gchar *name )
    97 {
    98     GList *ptr;
    99     for( ptr = gdrom_recent_list; ptr != NULL; ptr = g_list_next(ptr) ) {
   100         gchar *file = (gchar *)ptr->data;
   101         if( strcmp(file, name) == 0 ) {
   102             gdrom_recent_list = g_list_delete_link( gdrom_recent_list, ptr );
   103             gdrom_recent_list = g_list_prepend( gdrom_recent_list, file );
   104             gdrom_list_update_config();
   105             return;
   106         }
   107     }
   108 }
   110 /**
   111  * Disc-changed callback from the GD-Rom driver. Updates the list accordingly.
   112  */
   113 gboolean gdrom_list_disc_changed( gdrom_disc_t disc, const gchar *disc_name, void *user_data )
   114 {
   115     gboolean list_changed = FALSE;
   116     int posn = 0;
   117     if( disc != NULL ) {
   118         posn = gdrom_list_find( disc_name );
   119         if( posn == -1 ) {
   120             gdrom_list_add_recent_item( disc_name );
   121             posn = FIRST_RECENT_INDEX;
   122             list_changed = TRUE;
   123         } else if( posn > FIRST_RECENT_INDEX ) {
   124             gdrom_list_move_to_front( disc_name );
   125             posn = FIRST_RECENT_INDEX;
   126             list_changed = TRUE;
   127         }
   128     }
   130     lxdream_set_global_config_value( CONFIG_GDROM, disc_name );
   131     lxdream_save_config();   
   133     CALL_HOOKS( gdrom_list_change_hook, list_changed, posn );
   134     return TRUE;
   135 }
   137 /**
   138  * Drives-changed callback from the host CD-Rom drivers. Probably not likely to
   139  * happen too often unless you're adding/removing external drives...
   140  */
   141 void gdrom_list_drives_changed( GList *device_list )
   142 {
   143 }
   145 /************ Public interface ***********/
   147 void gdrom_list_init()
   148 {
   149     const gchar *recent = lxdream_get_config_value( CONFIG_RECENT );
   150     register_gdrom_disc_change_hook( gdrom_list_disc_changed, NULL );
   151     gdrom_device_list = cdrom_get_native_devices();
   152     if( recent != NULL ) {
   153         gchar **list = g_strsplit(recent, ":", MAX_RECENT_ITEMS);
   154         int i;
   155         for( i=0; list[i] != NULL; i++ ) {
   156             gdrom_recent_list = g_list_append( gdrom_recent_list, g_strdup(list[i]) );
   157         }
   158         g_strfreev(list);
   159     }
   160     gdrom_device_count = g_list_length(gdrom_device_list);
   161     gdrom_recent_count = g_list_length(gdrom_recent_list);
   163     // Run the hooks in case anyone registered before the list was initialized
   164     CALL_HOOKS( gdrom_list_change_hook, TRUE, gdrom_list_get_selection() );
   165 }
   167 gboolean gdrom_list_set_selection( int posn )
   168 {
   169     if( posn == 0 ) { // Always 'Empty'
   170         gdrom_unmount_disc();
   171         return TRUE;
   172     }
   174     if( posn <= gdrom_device_count ) {
   175         gdrom_device_t device = g_list_nth_data(gdrom_device_list, posn-1);
   176         return gdrom_mount_image(device->name);
   177     }
   179     posn -= FIRST_RECENT_INDEX;
   180     if( posn >= 0 && posn < gdrom_recent_count ) {
   181         gchar *entry = g_list_nth_data(gdrom_recent_list, posn);
   182         return gdrom_mount_image(entry);
   183     }
   185     return FALSE;
   186 }
   188 gint gdrom_list_get_selection( )
   189 {
   190     const char *name = gdrom_get_current_disc_name();
   191     if( name == NULL ) {
   192         return 0;
   193     } else {
   194         return gdrom_list_find(name);
   195     }
   196 }
   198 int gdrom_list_size()
   199 {
   200     return gdrom_device_count + gdrom_recent_count + 2;
   201 }
   203 const gchar *gdrom_list_get_display_name( int posn )
   204 {
   205     if( posn == 0 ) {
   206         return _("Empty");
   207     }
   209     if( posn <= gdrom_device_count ) {
   210         gdrom_device_t device = g_list_nth_data(gdrom_device_list, posn-1);
   211         return device->device_name;
   212     }
   214     if( posn == gdrom_device_count + 1) {
   215         return "";
   216     }
   218     if( posn < 0 || posn > gdrom_list_size() ) {
   219         return NULL;
   220     }
   222     gchar *entry = g_list_nth_data(gdrom_recent_list, posn-FIRST_RECENT_INDEX);
   223     return basename(entry);
   224 }
   226 const gchar *gdrom_list_get_filename( int posn )
   227 {
   228     if( posn == 0 ) {
   229         return _("Empty");
   230     }
   232     if( posn <= gdrom_device_count ) {
   233         gdrom_device_t device = g_list_nth_data(gdrom_device_list, posn-1);
   234         return device->name;
   235     }
   237     if( posn == gdrom_device_count + 1) {
   238         return "";
   239     }
   241     if( posn < 0 || posn > gdrom_list_size() ) {
   242         return NULL;
   243     }
   245     return g_list_nth_data(gdrom_recent_list, posn-FIRST_RECENT_INDEX);
   246 }
.