Search
lxdream.org :: lxdream/src/gdlist.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/gdlist.c
changeset 1296:30ecee61f811
prev1109:700c5ab26a63
author nkeynes
date Sat Jan 26 14:00:48 2013 +1000 (9 years ago)
permissions -rw-r--r--
last change Change glib includes to #include <glib.h> rather than the individual
headers, as recent glib versions are breaking on this
file annotate diff log raw
nkeynes@691
     1
/**
nkeynes@691
     2
 * $Id$
nkeynes@691
     3
 *
nkeynes@691
     4
 * GD-Rom list manager - maintains the list of recently accessed images and
nkeynes@691
     5
 * available devices for the UI + config. 
nkeynes@691
     6
 *
nkeynes@691
     7
 * Copyright (c) 2005 Nathan Keynes.
nkeynes@691
     8
 *
nkeynes@691
     9
 * This program is free software; you can redistribute it and/or modify
nkeynes@691
    10
 * it under the terms of the GNU General Public License as published by
nkeynes@691
    11
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@691
    12
 * (at your option) any later version.
nkeynes@691
    13
 *
nkeynes@691
    14
 * This program is distributed in the hope that it will be useful,
nkeynes@691
    15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@691
    16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@691
    17
 * GNU General Public License for more details.
nkeynes@691
    18
 */
nkeynes@691
    19
nkeynes@691
    20
#include <string.h>
nkeynes@691
    21
#include <stdlib.h>
nkeynes@1296
    22
#include <glib.h>
nkeynes@691
    23
#include <libgen.h>
nkeynes@755
    24
#include "gettext.h"
nkeynes@691
    25
#include "gdrom/gdrom.h"
nkeynes@1097
    26
#include "drivers/cdrom/drive.h"
nkeynes@691
    27
#include "gdlist.h"
nkeynes@691
    28
#include "lxdream.h"
nkeynes@691
    29
#include "config.h"
nkeynes@691
    30
nkeynes@691
    31
#define MAX_RECENT_ITEMS 5
nkeynes@691
    32
nkeynes@691
    33
#define FIRST_RECENT_INDEX (gdrom_device_count+2)
nkeynes@691
    34
nkeynes@691
    35
DEFINE_HOOK(gdrom_list_change_hook, gdrom_list_change_hook_t);
nkeynes@691
    36
nkeynes@691
    37
static GList *gdrom_device_list = NULL;
nkeynes@691
    38
static GList *gdrom_recent_list = NULL;
nkeynes@691
    39
static unsigned int gdrom_device_count = 0, gdrom_recent_count = 0;
nkeynes@691
    40
nkeynes@691
    41
gint gdrom_list_find( const gchar *name )
nkeynes@691
    42
{
nkeynes@691
    43
    gint posn = 0;
nkeynes@691
    44
    GList *ptr;
nkeynes@691
    45
nkeynes@691
    46
    for( ptr = gdrom_device_list; ptr != NULL; ptr = g_list_next(ptr) ) {
nkeynes@1097
    47
        cdrom_drive_t device = (cdrom_drive_t)ptr->data;
nkeynes@691
    48
        posn++;
nkeynes@709
    49
        if( strcmp(device->name, name) == 0 ) {
nkeynes@691
    50
            return posn;
nkeynes@691
    51
        }
nkeynes@691
    52
    }
nkeynes@691
    53
    posn++;
nkeynes@691
    54
    for( ptr = gdrom_recent_list; ptr != NULL; ptr = g_list_next(ptr) ) {
nkeynes@691
    55
        gchar *file = (gchar *)ptr->data;
nkeynes@691
    56
        posn++;
nkeynes@691
    57
        if( strcmp(file, name) == 0 ) {
nkeynes@691
    58
            return posn;
nkeynes@691
    59
        }
nkeynes@691
    60
    }
nkeynes@691
    61
    return -1;
nkeynes@691
    62
}
nkeynes@691
    63
nkeynes@691
    64
/**
nkeynes@691
    65
 * Update the recent list in the lxdream config (but does not save)  
nkeynes@691
    66
 */
nkeynes@691
    67
void gdrom_list_update_config()
nkeynes@691
    68
{
nkeynes@1034
    69
    lxdream_set_global_config_list_value( CONFIG_RECENT, gdrom_recent_list ); 
nkeynes@691
    70
}
nkeynes@691
    71
nkeynes@691
    72
nkeynes@691
    73
void gdrom_list_add_recent_item( const gchar *name )
nkeynes@691
    74
{
nkeynes@691
    75
    gdrom_recent_list = g_list_prepend( gdrom_recent_list, g_strdup(name) );
nkeynes@691
    76
    if( g_list_length(gdrom_recent_list) > MAX_RECENT_ITEMS ) {
nkeynes@691
    77
        GList *ptr = g_list_nth( gdrom_recent_list, MAX_RECENT_ITEMS );
nkeynes@691
    78
        g_free( ptr->data );
nkeynes@691
    79
        gdrom_recent_list = g_list_remove( gdrom_recent_list, ptr->data );
nkeynes@691
    80
    } else {
nkeynes@691
    81
        gdrom_recent_count ++;
nkeynes@691
    82
    }
nkeynes@691
    83
    gdrom_list_update_config();
nkeynes@691
    84
}
nkeynes@691
    85
nkeynes@691
    86
void gdrom_list_move_to_front( const gchar *name )
nkeynes@691
    87
{
nkeynes@691
    88
    GList *ptr;
nkeynes@691
    89
    for( ptr = gdrom_recent_list; ptr != NULL; ptr = g_list_next(ptr) ) {
nkeynes@691
    90
        gchar *file = (gchar *)ptr->data;
nkeynes@691
    91
        if( strcmp(file, name) == 0 ) {
nkeynes@691
    92
            gdrom_recent_list = g_list_delete_link( gdrom_recent_list, ptr );
nkeynes@691
    93
            gdrom_recent_list = g_list_prepend( gdrom_recent_list, file );
nkeynes@691
    94
            gdrom_list_update_config();
nkeynes@691
    95
            return;
nkeynes@691
    96
        }
nkeynes@691
    97
    }
nkeynes@691
    98
}
nkeynes@691
    99
nkeynes@691
   100
/**
nkeynes@691
   101
 * Disc-changed callback from the GD-Rom driver. Updates the list accordingly.
nkeynes@691
   102
 */
nkeynes@1097
   103
gboolean gdrom_list_disc_changed( cdrom_disc_t disc, const gchar *disc_name, void *user_data )
nkeynes@691
   104
{
nkeynes@691
   105
    gboolean list_changed = FALSE;
nkeynes@691
   106
    int posn = 0;
nkeynes@691
   107
    if( disc != NULL ) {
nkeynes@691
   108
        posn = gdrom_list_find( disc_name );
nkeynes@691
   109
        if( posn == -1 ) {
nkeynes@691
   110
            gdrom_list_add_recent_item( disc_name );
nkeynes@691
   111
            posn = FIRST_RECENT_INDEX;
nkeynes@691
   112
            list_changed = TRUE;
nkeynes@691
   113
        } else if( posn > FIRST_RECENT_INDEX ) {
nkeynes@691
   114
            gdrom_list_move_to_front( disc_name );
nkeynes@691
   115
            posn = FIRST_RECENT_INDEX;
nkeynes@691
   116
            list_changed = TRUE;
nkeynes@691
   117
        }
nkeynes@691
   118
    }
nkeynes@736
   119
nkeynes@1036
   120
    lxdream_set_global_config_path_value( CONFIG_GDROM, disc_name );
nkeynes@691
   121
    lxdream_save_config();   
nkeynes@691
   122
nkeynes@691
   123
    CALL_HOOKS( gdrom_list_change_hook, list_changed, posn );
nkeynes@691
   124
    return TRUE;
nkeynes@691
   125
}
nkeynes@691
   126
nkeynes@691
   127
/**
nkeynes@691
   128
 * Drives-changed callback from the host CD-Rom drivers. Probably not likely to
nkeynes@691
   129
 * happen too often unless you're adding/removing external drives...
nkeynes@691
   130
 */
nkeynes@691
   131
void gdrom_list_drives_changed( GList *device_list )
nkeynes@691
   132
{
nkeynes@691
   133
}
nkeynes@691
   134
nkeynes@691
   135
/************ Public interface ***********/
nkeynes@691
   136
nkeynes@691
   137
void gdrom_list_init()
nkeynes@691
   138
{
nkeynes@1034
   139
    gdrom_recent_list = lxdream_get_global_config_list_value( CONFIG_RECENT );
nkeynes@691
   140
    register_gdrom_disc_change_hook( gdrom_list_disc_changed, NULL );
nkeynes@1097
   141
    cdrom_drive_scan();
nkeynes@1097
   142
    gdrom_device_list = cdrom_drive_get_list();
nkeynes@691
   143
    gdrom_device_count = g_list_length(gdrom_device_list);
nkeynes@691
   144
    gdrom_recent_count = g_list_length(gdrom_recent_list);
nkeynes@736
   145
nkeynes@691
   146
    // Run the hooks in case anyone registered before the list was initialized
nkeynes@691
   147
    CALL_HOOKS( gdrom_list_change_hook, TRUE, gdrom_list_get_selection() );
nkeynes@691
   148
}
nkeynes@691
   149
nkeynes@1109
   150
gboolean gdrom_list_set_selection( int posn, ERROR *err )
nkeynes@691
   151
{
nkeynes@691
   152
    if( posn == 0 ) { // Always 'Empty'
nkeynes@691
   153
        gdrom_unmount_disc();
nkeynes@691
   154
        return TRUE;
nkeynes@691
   155
    }
nkeynes@736
   156
nkeynes@691
   157
    if( posn <= gdrom_device_count ) {
nkeynes@1097
   158
        cdrom_drive_t device = g_list_nth_data(gdrom_device_list, posn-1);
nkeynes@1109
   159
        return gdrom_mount_image(device->name, err);
nkeynes@691
   160
    }
nkeynes@736
   161
nkeynes@691
   162
    posn -= FIRST_RECENT_INDEX;
nkeynes@691
   163
    if( posn >= 0 && posn < gdrom_recent_count ) {
nkeynes@691
   164
        gchar *entry = g_list_nth_data(gdrom_recent_list, posn);
nkeynes@1109
   165
        return gdrom_mount_image(entry, err);
nkeynes@691
   166
    }
nkeynes@736
   167
nkeynes@1109
   168
    /* Should never happen */
nkeynes@1109
   169
    SET_ERROR( err, LX_ERR_BUG, "Invalid selection: This is probably a bug" );
nkeynes@691
   170
    return FALSE;
nkeynes@691
   171
}
nkeynes@691
   172
nkeynes@691
   173
gint gdrom_list_get_selection( )
nkeynes@691
   174
{
nkeynes@691
   175
    const char *name = gdrom_get_current_disc_name();
nkeynes@691
   176
    if( name == NULL ) {
nkeynes@691
   177
        return 0;
nkeynes@691
   178
    } else {
nkeynes@691
   179
        return gdrom_list_find(name);
nkeynes@691
   180
    }
nkeynes@691
   181
}
nkeynes@691
   182
nkeynes@691
   183
int gdrom_list_size()
nkeynes@691
   184
{
nkeynes@691
   185
    return gdrom_device_count + gdrom_recent_count + 2;
nkeynes@691
   186
}
nkeynes@691
   187
nkeynes@691
   188
const gchar *gdrom_list_get_display_name( int posn )
nkeynes@691
   189
{
nkeynes@691
   190
    if( posn == 0 ) {
nkeynes@691
   191
        return _("Empty");
nkeynes@691
   192
    }
nkeynes@736
   193
nkeynes@691
   194
    if( posn <= gdrom_device_count ) {
nkeynes@1097
   195
        cdrom_drive_t device = g_list_nth_data(gdrom_device_list, posn-1);
nkeynes@1097
   196
        return device->display_name;
nkeynes@691
   197
    }
nkeynes@736
   198
nkeynes@691
   199
    if( posn == gdrom_device_count + 1) {
nkeynes@691
   200
        return "";
nkeynes@691
   201
    }
nkeynes@736
   202
nkeynes@691
   203
    if( posn < 0 || posn > gdrom_list_size() ) {
nkeynes@691
   204
        return NULL;
nkeynes@691
   205
    }
nkeynes@691
   206
nkeynes@691
   207
    gchar *entry = g_list_nth_data(gdrom_recent_list, posn-FIRST_RECENT_INDEX);
nkeynes@691
   208
    return basename(entry);
nkeynes@691
   209
}
nkeynes@696
   210
nkeynes@696
   211
const gchar *gdrom_list_get_filename( int posn )
nkeynes@696
   212
{
nkeynes@736
   213
    if( posn == 0 ) {
nkeynes@736
   214
        return _("Empty");
nkeynes@736
   215
    }
nkeynes@696
   216
nkeynes@736
   217
    if( posn <= gdrom_device_count ) {
nkeynes@1097
   218
        cdrom_drive_t device = g_list_nth_data(gdrom_device_list, posn-1);
nkeynes@736
   219
        return device->name;
nkeynes@736
   220
    }
nkeynes@736
   221
nkeynes@736
   222
    if( posn == gdrom_device_count + 1) {
nkeynes@736
   223
        return "";
nkeynes@736
   224
    }
nkeynes@736
   225
nkeynes@736
   226
    if( posn < 0 || posn > gdrom_list_size() ) {
nkeynes@736
   227
        return NULL;
nkeynes@736
   228
    }
nkeynes@736
   229
nkeynes@736
   230
    return g_list_nth_data(gdrom_recent_list, posn-FIRST_RECENT_INDEX);
nkeynes@696
   231
}
.