Search
lxdream.org :: lxdream/src/gdlist.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/gdlist.c
changeset 1036:af7b0c5905dd
prev1034:7044e01148f0
next1097:d4807997e450
author nkeynes
date Wed Jun 24 06:06:40 2009 +0000 (12 years ago)
permissions -rw-r--r--
last change Support shell substitutions in config paths
Keep track of last folder in file dialogs
Fix out-of-dateness in GTK path dialog
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     lxdream_set_global_config_list_value( CONFIG_RECENT, gdrom_recent_list ); 
    69 }
    72 void gdrom_list_add_recent_item( const gchar *name )
    73 {
    74     gdrom_recent_list = g_list_prepend( gdrom_recent_list, g_strdup(name) );
    75     if( g_list_length(gdrom_recent_list) > MAX_RECENT_ITEMS ) {
    76         GList *ptr = g_list_nth( gdrom_recent_list, MAX_RECENT_ITEMS );
    77         g_free( ptr->data );
    78         gdrom_recent_list = g_list_remove( gdrom_recent_list, ptr->data );
    79     } else {
    80         gdrom_recent_count ++;
    81     }
    82     gdrom_list_update_config();
    83 }
    85 void gdrom_list_move_to_front( const gchar *name )
    86 {
    87     GList *ptr;
    88     for( ptr = gdrom_recent_list; ptr != NULL; ptr = g_list_next(ptr) ) {
    89         gchar *file = (gchar *)ptr->data;
    90         if( strcmp(file, name) == 0 ) {
    91             gdrom_recent_list = g_list_delete_link( gdrom_recent_list, ptr );
    92             gdrom_recent_list = g_list_prepend( gdrom_recent_list, file );
    93             gdrom_list_update_config();
    94             return;
    95         }
    96     }
    97 }
    99 /**
   100  * Disc-changed callback from the GD-Rom driver. Updates the list accordingly.
   101  */
   102 gboolean gdrom_list_disc_changed( gdrom_disc_t disc, const gchar *disc_name, void *user_data )
   103 {
   104     gboolean list_changed = FALSE;
   105     int posn = 0;
   106     if( disc != NULL ) {
   107         posn = gdrom_list_find( disc_name );
   108         if( posn == -1 ) {
   109             gdrom_list_add_recent_item( disc_name );
   110             posn = FIRST_RECENT_INDEX;
   111             list_changed = TRUE;
   112         } else if( posn > FIRST_RECENT_INDEX ) {
   113             gdrom_list_move_to_front( disc_name );
   114             posn = FIRST_RECENT_INDEX;
   115             list_changed = TRUE;
   116         }
   117     }
   119     lxdream_set_global_config_path_value( CONFIG_GDROM, disc_name );
   120     lxdream_save_config();   
   122     CALL_HOOKS( gdrom_list_change_hook, list_changed, posn );
   123     return TRUE;
   124 }
   126 /**
   127  * Drives-changed callback from the host CD-Rom drivers. Probably not likely to
   128  * happen too often unless you're adding/removing external drives...
   129  */
   130 void gdrom_list_drives_changed( GList *device_list )
   131 {
   132 }
   134 /************ Public interface ***********/
   136 void gdrom_list_init()
   137 {
   138     gdrom_recent_list = lxdream_get_global_config_list_value( CONFIG_RECENT );
   139     register_gdrom_disc_change_hook( gdrom_list_disc_changed, NULL );
   140     gdrom_device_list = cdrom_get_native_devices();
   141     gdrom_device_count = g_list_length(gdrom_device_list);
   142     gdrom_recent_count = g_list_length(gdrom_recent_list);
   144     // Run the hooks in case anyone registered before the list was initialized
   145     CALL_HOOKS( gdrom_list_change_hook, TRUE, gdrom_list_get_selection() );
   146 }
   148 gboolean gdrom_list_set_selection( int posn )
   149 {
   150     if( posn == 0 ) { // Always 'Empty'
   151         gdrom_unmount_disc();
   152         return TRUE;
   153     }
   155     if( posn <= gdrom_device_count ) {
   156         gdrom_device_t device = g_list_nth_data(gdrom_device_list, posn-1);
   157         return gdrom_mount_image(device->name);
   158     }
   160     posn -= FIRST_RECENT_INDEX;
   161     if( posn >= 0 && posn < gdrom_recent_count ) {
   162         gchar *entry = g_list_nth_data(gdrom_recent_list, posn);
   163         return gdrom_mount_image(entry);
   164     }
   166     return FALSE;
   167 }
   169 gint gdrom_list_get_selection( )
   170 {
   171     const char *name = gdrom_get_current_disc_name();
   172     if( name == NULL ) {
   173         return 0;
   174     } else {
   175         return gdrom_list_find(name);
   176     }
   177 }
   179 int gdrom_list_size()
   180 {
   181     return gdrom_device_count + gdrom_recent_count + 2;
   182 }
   184 const gchar *gdrom_list_get_display_name( int posn )
   185 {
   186     if( posn == 0 ) {
   187         return _("Empty");
   188     }
   190     if( posn <= gdrom_device_count ) {
   191         gdrom_device_t device = g_list_nth_data(gdrom_device_list, posn-1);
   192         return device->device_name;
   193     }
   195     if( posn == gdrom_device_count + 1) {
   196         return "";
   197     }
   199     if( posn < 0 || posn > gdrom_list_size() ) {
   200         return NULL;
   201     }
   203     gchar *entry = g_list_nth_data(gdrom_recent_list, posn-FIRST_RECENT_INDEX);
   204     return basename(entry);
   205 }
   207 const gchar *gdrom_list_get_filename( int posn )
   208 {
   209     if( posn == 0 ) {
   210         return _("Empty");
   211     }
   213     if( posn <= gdrom_device_count ) {
   214         gdrom_device_t device = g_list_nth_data(gdrom_device_list, posn-1);
   215         return device->name;
   216     }
   218     if( posn == gdrom_device_count + 1) {
   219         return "";
   220     }
   222     if( posn < 0 || posn > gdrom_list_size() ) {
   223         return NULL;
   224     }
   226     return g_list_nth_data(gdrom_recent_list, posn-FIRST_RECENT_INDEX);
   227 }
.