Search
lxdream.org :: lxdream/src/gdlist.c
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.
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 <glib/gi18n.h>
    24 #include <libgen.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         gchar *device = (gchar *)ptr->data;
    47         posn++;
    48         if( strcmp(device, 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 = gdrom_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         gchar *entry = g_list_nth_data(gdrom_device_list, posn-1);
   176         gdrom_mount_image(entry);
   177         return TRUE;
   178     }
   180     posn -= FIRST_RECENT_INDEX;
   181     if( posn >= 0 && posn < gdrom_recent_count ) {
   182         gchar *entry = g_list_nth_data(gdrom_recent_list, posn);
   183         gdrom_mount_image(entry);
   184         return TRUE;
   185     }
   187     return FALSE;
   188 }
   190 gint gdrom_list_get_selection( )
   191 {
   192     const char *name = gdrom_get_current_disc_name();
   193     if( name == NULL ) {
   194         return 0;
   195     } else {
   196         return gdrom_list_find(name);
   197     }
   198 }
   200 int gdrom_list_size()
   201 {
   202     return gdrom_device_count + gdrom_recent_count + 2;
   203 }
   205 const gchar *gdrom_list_get_display_name( int posn )
   206 {
   207     if( posn == 0 ) {
   208         return _("Empty");
   209     }
   211     if( posn <= gdrom_device_count ) {
   212         return g_list_nth_data(gdrom_device_list, posn-1);
   213     }
   215     if( posn == gdrom_device_count + 1) {
   216         return "";
   217     }
   219     if( posn < 0 || posn > gdrom_list_size() ) {
   220         return NULL;
   221     }
   223     gchar *entry = g_list_nth_data(gdrom_recent_list, posn-FIRST_RECENT_INDEX);
   224     return basename(entry);
   225 }
.