ml_item.cpp 9.79 KB
Newer Older
jetru's avatar
jetru committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/*****************************************************************************
 * ml_item.cpp: the media library's result item
 *****************************************************************************
 * Copyright (C) 2008-2011 the VideoLAN Team and AUTHORS
 * $Id$
 *
 * Authors: Antoine Lejeune <phytos@videolan.org>
 *          Jean-Philippe André <jpeg@videolan.org>
 *          Rémi Duraffort <ivoire@videolan.org>
 *          Adrien Maglo <magsoft@videolan.org>
 *          Srikanth Raju <srikiraju#gmail#com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (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
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU 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.
 *****************************************************************************/

28
29
30
31
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

jetru's avatar
jetru committed
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#ifdef MEDIA_LIBRARY

#include <QDateTime>
#include <QUrl>
#include <QFileInfo>
#include "ml_item.hpp"
#include <assert.h>


/**
 * @brief Compare the attribute 'meta' between medias a and b.
 * @param a
 * @param b
 * @param meta
 * @note If a->meta < b->meta, return -1
 *       If a->meta == b->meta, return 0
 *       If a->meta > b->meta, return +1
 * @note If a->meta == NULL and b->meta != NULL (strings), then b>a
 */
François Cartegnie's avatar
François Cartegnie committed
51
static int compareMeta( ml_media_t *a, ml_media_t *b, ml_select_e meta )
jetru's avatar
jetru committed
52
{
François Cartegnie's avatar
François Cartegnie committed
53
54
55
56
57
58
59
60
    bool i_ret = 0;
#   define scomp(c) i_ret = ((a->c&&b->c&&*a->c&&*b->c) ?\
                     strcasecmp(a->c,b->c) : \
                     (a->c&&*a->c?-1:(b->c&&*b->c?1:0))); break;
#   define icomp(c) i_ret = (a->c<b->c?-1:(a->c==b->c?0:1)); break;
    if ( a == b ) return 0;
    vlc_mutex_lock( &a->lock );
    vlc_mutex_lock( &b->lock );
jetru's avatar
jetru committed
61
62
    switch( meta )
    {
François Cartegnie's avatar
François Cartegnie committed
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
    case ML_ALBUM: scomp( psz_album );
    case ML_ALBUM_ID: icomp( i_album_id );
        //case ML_ARTIST: scomp( psz_artist );
        //case ML_ARTIST_ID: icomp( i_artist_id );
    case ML_COVER: scomp( psz_cover );
    case ML_DURATION: icomp( i_duration );
    case ML_EXTRA: scomp( psz_extra );
    case ML_GENRE: scomp( psz_genre );
    case ML_ID: icomp( i_id );
    case ML_LAST_PLAYED: icomp( i_last_played );
    case ML_ORIGINAL_TITLE: scomp( psz_orig_title );
    case ML_PLAYED_COUNT: icomp( i_played_count );
        // case ML_ROLE:  0;
    case ML_SCORE: icomp( i_score );
    case ML_TITLE: scomp( psz_title );
    case ML_TRACK_NUMBER: icomp( i_track_number );
    case ML_TYPE: icomp( i_type );
    case ML_URI: scomp( psz_uri );
    case ML_VOTE: icomp( i_vote );
    case ML_YEAR: icomp( i_year );
    default:
        break;
jetru's avatar
jetru committed
85
86
87
    }
#   undef scomp
#   undef icomp
François Cartegnie's avatar
François Cartegnie committed
88
89
90
    vlc_mutex_unlock( &a->lock );
    vlc_mutex_unlock( &b->lock );
    return i_ret;
jetru's avatar
jetru committed
91
92
93
94
95
96
97
}


MLItem::MLItem( const MLModel *p_model,
                            intf_thread_t* _p_intf,
                            ml_media_t *p_media,
                            MLItem *p_parent )
98
        : p_intf( _p_intf ), model( p_model )
jetru's avatar
jetru committed
99
{
100
    parentItem = p_parent;
jetru's avatar
jetru committed
101
102
    if( p_media )
        ml_gc_incref( p_media );
103
    media = p_media;
104
    p_ml = ml_Get( _p_intf );
jetru's avatar
jetru committed
105
106
107
108
109
110
111
112
113
114
115
}

MLItem::~MLItem()
{
    // Free private data
    if( this->media )
        ml_gc_decref( this->media );
    if( !children.isEmpty() )
        clearChildren();
}

116
AbstractPLItem* MLItem::child( int row ) const
jetru's avatar
jetru committed
117
118
119
120
121
122
123
124
{
    if( row < 0 || row >= childCount() ) return NULL;
    else return children.at( row );
}

void MLItem::delChild( int row )
{
    if( !childCount() ) return; // assert ?
125
    AbstractPLItem *item =
126
            children.takeAt( ( row!=-1 ) ? row : ( children.count()-1 ) );
jetru's avatar
jetru committed
127
128
129
130
    assert( item );
    delete item;
}

131
132
133
134
135
input_item_t* MLItem::inputItem()
{
    return ml_CreateInputItem( p_ml,  id() );
}

jetru's avatar
jetru committed
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/**
 * @brief Get a QVariant representing the data on a column
 * @param column
 * @return The QVariant may be formed of a int, QString
 *         Use toString() to print it on the screen, except for pixmaps
 */
QVariant MLItem::data( int column ) const
{
    ml_select_e type = model->columnType( column );
    ml_person_t *p_people = NULL, *p_person = NULL;
    QString qsz_return;
#define sreturn(a) if(media->a) return qfu(media->a); break
    switch( type )
    {
        case ML_ALBUM: sreturn( psz_album );
        case ML_ALBUM_ID: return media->i_album_id;
        case ML_ARTIST:
            p_people = ml_GetPersonsFromMedia( p_ml, media, ML_PERSON_ARTIST );
            p_person = p_people;
            while( p_person )
            {
                if( !EMPTY_STR( p_person->psz_name ) )
                {
                    qsz_return.isEmpty() ? qsz_return = qfu( p_person->psz_name ) :
                        qsz_return.append( "," ).append( qfu( p_person->psz_name ) );
                }
                p_person = p_person->p_next;
            }
            ml_FreePeople( p_people );
            return qsz_return;
            break;
        case ML_COVER: sreturn( psz_cover );
        case ML_DURATION:
            return QTime().addSecs( media->i_duration/1000000 ).toString( "HH:mm:ss" );
        case ML_EXTRA: sreturn( psz_extra );
        case ML_GENRE: sreturn( psz_genre );
        case ML_ID: return media->i_id;
        case ML_LAST_PLAYED:
        {
            if( media->i_last_played > 0 )
            {
                QDateTime time( QDate(1970,1,1) );
                return time.addSecs( qint64( media->i_last_played ) );
            }
            else
                return QString();
        }
        case ML_ORIGINAL_TITLE: sreturn( psz_orig_title );
        case ML_PLAYED_COUNT: return media->i_played_count;
        // case ML_ROLE: return qtr( "Role" );
        case ML_SCORE: return media->i_score ? media->i_score : QVariant();
        case ML_TITLE:
        {
189
190
191
            vlc_mutex_lock( &media->lock );
            qsz_return = qfu( media->psz_title );
            vlc_mutex_unlock( &media->lock );
jetru's avatar
jetru committed
192
            /* If no title, return filename */
193
194
            if( ! qsz_return.isEmpty() )
                return qsz_return;
jetru's avatar
jetru committed
195
196
            else
            {
197
                vlc_mutex_lock( &media->lock );
jetru's avatar
jetru committed
198
                QFileInfo p_file = QFileInfo( qfu( media->psz_uri ) );
199
                vlc_mutex_unlock( &media->lock );
jetru's avatar
jetru committed
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
                return p_file.fileName().isEmpty() ? p_file.absoluteFilePath()
                    : p_file.fileName();
            }
        }
        case ML_TRACK_NUMBER: return media->i_track_number ? media->i_track_number : QVariant();
        case ML_TYPE:
        {
            QString txt;
            if( media->i_type & ML_AUDIO )
                txt = "Audio";
            if( media->i_type & ML_VIDEO )
                txt = "Video";
            if( media->i_type & ML_STREAM )
            {
                if( txt.isEmpty() ) txt = "Stream";
                else txt += " stream";
            }
            if( media->i_type & ML_REMOVABLE )
            {
                if( txt.isEmpty() ) txt = "Removable media";
                else txt += " (removable media)";
            }
            if( media->i_type & ML_NODE )
            {
                if( txt.isEmpty() ) txt = "Playlist";
                else txt += " (Playlist)";
            }
            if( txt.isEmpty() )
                txt = qtr( "Unknown" );
            return txt;
        }
        case ML_URI: sreturn( psz_uri );
        case ML_VOTE: return media->i_vote ? media->i_vote : QVariant();
        case ML_YEAR: return media->i_year ? media->i_year : QVariant();
        default: return QVariant();
    }
#   undef sreturn
    return QVariant();
}

bool MLItem::setData( ml_select_e meta, const QVariant &data )
{
#   define setmeta(a) ml_LockMedia(media); free(media->a);                  \
    media->a = strdup( qtu(data.toString()) ); ml_UnlockMedia( media );     \
    goto update;
    switch( meta )
    {
        /* String values */
        case ML_ALBUM: setmeta( psz_album );
        case ML_ARTIST: ml_DeletePersonTypeFromMedia( media, ML_PERSON_ARTIST );
                        ml_CreateAppendPersonAdv( &media->p_people,
                                ML_PERSON_ARTIST, (char*)qtu(data.toString()), 0 );
                        return ml_UpdateSimple( p_ml, ML_MEDIA, NULL, id(),
                                ML_PEOPLE, ML_PERSON_ARTIST, qtu( data.toString() ) ) == VLC_SUCCESS;
        case ML_EXTRA: setmeta( psz_extra );
        case ML_GENRE: setmeta( psz_genre );
        case ML_ORIGINAL_TITLE: setmeta( psz_orig_title );
        case ML_TITLE: setmeta( psz_title );
update:
            return ml_UpdateSimple( p_ml, ML_MEDIA, NULL, id(),
                                    meta, qtu( data.toString() ) ) == VLC_SUCCESS;

        /* Modifiable integers */
        case ML_TRACK_NUMBER:
        case ML_YEAR:
            return ml_UpdateSimple( p_ml, ML_MEDIA, NULL, id(),
                                    meta, data.toInt() ) == VLC_SUCCESS;

        // TODO case ML_VOTE:

        /* Non modifiable meta */
        default:
            return false;
    }
#   undef setmeta
}

int MLItem::id() const
{
    return media->i_id;
}

ml_media_t* MLItem::getMedia() const
{
    return media;
}

QUrl MLItem::getUri() const
{
    if( !media->psz_uri ) return QUrl(); // This should be rootItem
    QString uri = qfu( media->psz_uri );
    if( uri.contains( "://" ) )
        return QUrl( uri );
    else
        return QUrl( "file://" + uri );
}

bool MLItem::operator<( MLItem* item )
{
     int ret = compareMeta( getMedia(), item->getMedia(), ML_ALBUM );
     if( ret == -1 )
         return true;
     else return false;
}
#endif