media_library.c 4.21 KB
Newer Older
1
/*****************************************************************************
Pere Orga's avatar
Pere Orga committed
2
 * media_library.c: libvlc tags tree functions
3
 * Create a tree of the 'tags' of a media_list's medias.
4
 *****************************************************************************
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
5
 * Copyright (C) 2007 VLC authors and VideoLAN
6
 * $Id$
7 8 9
 *
 * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
 *
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
10 11 12
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
13 14 15 16
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
17 18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
19
 *
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
20 21 22
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23
 *****************************************************************************/
24

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
25 26 27 28
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

29
#include <vlc/vlc.h>
30 31 32 33 34 35 36

#include <vlc_common.h>

#include "libvlc_internal.h"

struct libvlc_media_library_t
{
37
    libvlc_event_manager_t   event_manager;
38 39 40 41 42
    libvlc_instance_t *      p_libvlc_instance;
    int                      i_refcount;
    libvlc_media_list_t *    p_mlist;
};

43 44 45 46 47 48 49 50 51 52 53 54 55

/*
 * Private functions
 */

/*
 * Public libvlc functions
 */

/**************************************************************************
 *       new (Public)
 **************************************************************************/
libvlc_media_library_t *
56
libvlc_media_library_new( libvlc_instance_t * p_inst )
57 58 59
{
    libvlc_media_library_t * p_mlib;

60
    p_mlib = malloc(sizeof(libvlc_media_library_t));
61

62
    if( !p_mlib )
63 64
    {
        libvlc_printerr( "Not enough memory" );
65
        return NULL;
66
    }
67

68
    p_mlib->p_libvlc_instance = p_inst;
69 70 71
    p_mlib->i_refcount = 1;
    p_mlib->p_mlist = NULL;

72
    libvlc_event_manager_init( &p_mlib->event_manager, p_mlib );
73
    libvlc_retain( p_inst );
74
    return p_mlib;
75 76 77 78 79 80 81 82 83 84 85 86
}

/**************************************************************************
 *       release (Public)
 **************************************************************************/
void libvlc_media_library_release( libvlc_media_library_t * p_mlib )
{
    p_mlib->i_refcount--;

    if( p_mlib->i_refcount > 0 )
        return;

87
    libvlc_event_manager_destroy( &p_mlib->event_manager );
88
    libvlc_release( p_mlib->p_libvlc_instance );
89
    free( p_mlib );
90 91 92 93 94 95 96
}

/**************************************************************************
 *       retain (Public)
 **************************************************************************/
void libvlc_media_library_retain( libvlc_media_library_t * p_mlib )
{
97
    p_mlib->i_refcount++;
98 99 100 101 102 103 104
}

/**************************************************************************
 *       load (Public)
 *
 * It doesn't yet load the playlists
 **************************************************************************/
105
int libvlc_media_library_load( libvlc_media_library_t * p_mlib )
106
{
107
    char *psz_datadir = config_GetUserDir( VLC_USERDATA_DIR );
108 109
    char * psz_uri;

110
    if( psz_datadir == NULL
111
     || asprintf( &psz_uri, "file/directory://%s" DIR_SEP "ml.xsp",
112
                  psz_datadir ) == -1 )
113 114 115
        psz_uri = NULL;
    free( psz_datadir );

Rémi Duraffort's avatar
Rémi Duraffort committed
116
    if( psz_uri == NULL )
117
    {
118
        libvlc_printerr( "Not enough memory" );
119
        return -1;
120
    }
121

122 123 124
    if( p_mlib->p_mlist )
        libvlc_media_list_release( p_mlib->p_mlist );

125
    p_mlib->p_mlist = libvlc_media_list_new( p_mlib->p_libvlc_instance );
126

127
    int ret = libvlc_media_list_add_file_content( p_mlib->p_mlist, psz_uri );
128
    free( psz_uri );
129
    return ret;
130 131 132 133 134 135
}

/**************************************************************************
 *        media_list (Public)
 **************************************************************************/
libvlc_media_list_t *
136
libvlc_media_library_media_list( libvlc_media_library_t * p_mlib )
137 138 139 140 141
{
    if( p_mlib->p_mlist )
        libvlc_media_list_retain( p_mlib->p_mlist );
    return p_mlib->p_mlist;
}