summaryrefslogtreecommitdiffstats
path: root/amarok/src/dynamicmode.cpp
blob: 6695a27320cbba18888187cc19248b70263fb738 (plain)
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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
189
190
191
192
193
194
195
196
197
198
199
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/***************************************************************************
 * copyright            : (C) 2005 Seb Ruiz <[email protected]>               *
 * copyright            : (C) 2006 Gábor Lehel <[email protected]>       *
 * copyright            : (C) 2006 Bonne Eggleston <[email protected]  *
 **************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

#define DEBUG_PREFIX "DynamicMode"

#include "collectiondb.h" // querybuilder, similar artists
#include "debug.h"
#include "enginecontroller.h" // HACK to get current artist for suggestion retrieval
#include "mountpointmanager.h" // device ids
#include "playlist.h"
#include "playlistbrowser.h"
#include "playlistbrowseritem.h"
#include "playlistselection.h"
#include "playlistwindow.h"
#include "statusbar.h"

#include "dynamicmode.h"

#include <kapplication.h> // random func

#include <tqregexp.h>
#include <tqvaluevector.h>

/////////////////////////////////////////////////////////////////////////////
///    CLASS DynamicMode
////////////////////////////////////////////////////////////////////////////

DynamicMode::DynamicMode( const TQString &name )
    : m_title( name )
    , m_cycle( true )
    , m_upcoming( 20 )
    , m_previous( 5 )
    , m_appendType( RANDOM )
{
}

DynamicMode::~DynamicMode()
{}

void
DynamicMode::deleting()
{
    if( this == Playlist::instance()->dynamicMode() )
        Playlist::instance()->disableDynamicMode();
}

void
DynamicMode::edit()
{
    if( this == Playlist::instance()->dynamicMode() )
        Playlist::instance()->editActiveDynamicMode(); //so the changes get noticed
    else
        ConfigDynamic::editDynamicPlaylist( PlaylistWindow::self(), this );
}

TQStringList DynamicMode::items()   const { return m_items; }

TQString DynamicMode::title()       const { return m_title; }
bool DynamicMode::cycleTracks()   const { return m_cycle; }
int  DynamicMode::upcomingCount() const { return m_upcoming; }
int  DynamicMode::previousCount() const { return m_previous; }
int  DynamicMode::appendType()    const { return m_appendType; }

void DynamicMode::setItems( const TQStringList &list ) { m_items = list;      }
void DynamicMode::setCycleTracks( bool e )            { m_cycle = e;         }
void DynamicMode::setUpcomingCount( int c )           { m_upcoming = c;      }
void DynamicMode::setPreviousCount( int c )           { m_previous = c;      }
void DynamicMode::setAppendType( int type )           { m_appendType = type; }
void DynamicMode::setTitle( const TQString& title )    { m_title = title;     }

void DynamicMode::setDynamicItems( TQPtrList<PlaylistBrowserEntry>& newList )
{
DEBUG_BLOCK

    TQStringList strListEntries;
    PlaylistBrowserEntry* entry;
    TQPtrListIterator<PlaylistBrowserEntry> it( newList );

    while( (entry = it.current()) != 0 )
    {
        ++it;
        strListEntries << entry->text(0);
    }

    setItems( strListEntries );
    PlaylistBrowser::instance()->saveDynamics();

    rebuildCachedItemSet();
}

void DynamicMode::rebuildCachedItemSet()
{
DEBUG_BLOCK

    m_cachedItemSet.clear();

    if( appendType() == RANDOM || appendType() == SUGGESTION )
    {
        QueryBuilder qb;
        qb.setOptions( QueryBuilder::optRandomize | QueryBuilder::optRemoveDuplicates );
        qb.addReturnValue( QueryBuilder::tabSong, QueryBuilder::valURL );

        if( appendType() == SUGGESTION )
        {
            // TODO some clever stuff here for spanning across artists
            TQString artist = EngineController::instance()->bundle().artist();

            if( artist.isEmpty() )
            {
                PlaylistItem *currentItem = Playlist::instance()->currentItem();
                if( currentItem != 0 )
                    artist = currentItem->artist();
            }

            debug() << "seeding from: " << artist << endl;

            TQStringList suggestions = CollectionDB::instance()->similarArtists( artist, 16 );
            // for this artist, choose 4 suggested artists at random, to get further suggestions from
            TQStringList newChosen;
            for( uint suggestCount = 0; suggestCount < 4; ++suggestCount )
            {
                if( suggestions.isEmpty() )
                    break;

                TQString chosen = suggestions[ KApplication::random() % suggestions.count() ];

                debug() << "found similar artist: " << chosen << endl;

                TQStringList newSuggestions = CollectionDB::instance()->similarArtists( chosen, 10 );
                for( uint c = 0; c < 4; ++c ) // choose another 4 artists
                {
                    if( newSuggestions.isEmpty() )
                        break;

                    TQString s = newSuggestions[ KApplication::random() % newSuggestions.count() ];

                    debug() << "found extended similar artist: " << s << endl;
                    newChosen += s;
                    newSuggestions.remove( s );
                }
                suggestions.remove( chosen );
            }
            if ( !newChosen.isEmpty() )
                suggestions += newChosen;
            qb.addMatches( QueryBuilder::tabArtist, suggestions );
        }

        qb.setLimit( 0, CACHE_SIZE );
        debug() << "Using SQL: " << qb.query() << endl;

        TQStringList urls = qb.run();

        foreach( urls ) //we have to run setPath on all raw paths
        {
            KURL current;
            current.setPath( *it );
            m_cachedItemSet += current;
        }
    }

    else
    {
        PlaylistBrowser *pb = PlaylistBrowser::instance();
        TQPtrList<PlaylistBrowserEntry> dynamicEntries = pb->dynamicEntries();
        if( !dynamicEntries.count() )
        {
            Amarok::StatusBar::instance()->longMessage( i18n( "This dynamic playlist has no sources set." ),
                                                        KDE::StatusBar::Sorry );
            return;
        }
        // Create an array of the sizes of each of the playlists
        TQValueVector<int> trackCount(dynamicEntries.count()) ;
        int trackCountTotal = 0;

        for( uint i=0; i < dynamicEntries.count(); i++ ){
          trackCount[i] = 0;

          if ( TQListViewItem *item = dynamicEntries.at( i ) ){
            if( item->rtti() == PlaylistEntry::RTTI )
              trackCount[i] = static_cast<PlaylistEntry *>(item)->tracksURL().count();
            else if( item->rtti() == SmartPlaylist::RTTI  )
              trackCount[i] = static_cast<SmartPlaylist *>(item)->length();

            trackCountTotal += trackCount[i];
          }
        }


        PlaylistBrowserEntry* entry;
        TQPtrListIterator<PlaylistBrowserEntry> it( dynamicEntries );

        //const int itemsPerSource = CACHE_SIZE / dynamicEntries.count() != 0 ? CACHE_SIZE / dynamicEntries.count() : 1;

        int i = 0;
        while( (entry = it.current()) != 0 )
        {
            ++it;
            //trackCountTotal might be 0
            int itemsForThisSource = trackCountTotal ? CACHE_SIZE * trackCount[i] / trackCountTotal : 1;
            if (itemsForThisSource == 0)
              itemsForThisSource = 1; 
            debug() << "this source will return " << itemsForThisSource << " entries" << endl;

            if( entry->rtti() == PlaylistEntry::RTTI )
            {
                KURL::List t = tracksFromStaticPlaylist( static_cast<PlaylistEntry*>(entry), itemsForThisSource);
                m_cachedItemSet += t;
            }

            else if( entry->rtti() == SmartPlaylist::RTTI )
            {
                KURL::List t = tracksFromSmartPlaylist( static_cast<SmartPlaylist*>(entry), itemsForThisSource);
                m_cachedItemSet += t;
            }
            i++;
        }
    }
}

KURL::List DynamicMode::tracksFromStaticPlaylist( PlaylistEntry *item, uint songCount )
{
DEBUG_BLOCK

    KURL::List trackList = item->tracksURL();
    KURL::List returnList;

    for( uint i=0; i < songCount; )
    {
        if( trackList.isEmpty() )
            break;

        KURL::List::Iterator urlIt = trackList.at( KApplication::random() % trackList.count() );
        if( (*urlIt).isValid() )
        {
            returnList << (*urlIt).path();
            ++i;
        }
        trackList.remove( urlIt );
    }

    debug() << "Returning " << returnList.count() << " tracks from " << item->text(0) << endl;

    return returnList;
}

KURL::List DynamicMode::tracksFromSmartPlaylist( SmartPlaylist *item, uint songCount )
{
DEBUG_BLOCK
    if( !item || !songCount )
        return KURL::List();

    bool useDirect = true;
    const bool hasTimeOrder = item->isTimeOrdered();
    debug() << "The smart playlist: " << item->text(0) << ", time order? " << hasTimeOrder << endl;

    TQString sql = item->query();

    // FIXME: All this SQL magic out of collectiondb is not a good thing

    // if there is no ordering, add random ordering
    if ( sql.find( TQString("ORDER BY"), false ) == -1 )
    {
        TQRegExp limit( "(LIMIT.*)?;$" );
        sql.replace( limit, TQString(" ORDER BY %1 LIMIT %2 OFFSET 0;")
                            .tqarg( CollectionDB::instance()->randomFunc() )
                            .tqarg( songCount ) );
    }
    else
    {
        uint limit=0, offset=0;

        TQRegExp limitSearch( "LIMIT.*(\\d+).*OFFSET.*(\\d+)" );
        int findLocation = limitSearch.search( sql, 0 );
        if( findLocation == -1 ) //not found, let's find out the higher limit the hard way
        {
            TQString counting( sql );
            counting.replace( TQRegExp( "SELECT.*FROM" ), "SELECT COUNT(*) FROM" );
            // Postgres' grouping rule doesn't like the following clause
            counting.replace( TQRegExp( "ORDER BY.*" ), "" );
            TQStringList countingResult = CollectionDB::instance()->query( counting );
            limit = countingResult[0].toInt();
        }
        else
        {   // There's a Limit, we have to respect it.
            // capturedTexts() gives us the strings that were matched by each subexpression
            offset = limitSearch.capturedTexts()[2].toInt();
            limit  = limitSearch.capturedTexts()[1].toInt();
        }

        // we must be ordering by some other arbitrary query.
        // we can scrap it, since it won't affect our result
        if( !hasTimeOrder )
        {
            // We can mess with the limits if the smart playlist is not orderd by a time criteria
            // Why? We can have a smart playlist which is ordered by name or by some other quality which
            // is meaningless in dynamic mode
            TQRegExp orderLimit( "(ORDER BY.*)?;$" );

            sql.replace( orderLimit, TQString(" ORDER BY %1 LIMIT %2 OFFSET 0;")
                                        .tqarg( CollectionDB::instance()->randomFunc() )
                                        .tqarg( songCount ) );
        }
        else // time ordered criteria, only mess with the limits
        {
            debug() << "time based criteria used!" << endl;
            if ( limit <= songCount ) // The list is even smaller than the number of songs we want :-(
                songCount = limit;
            else
                // Let's get a random limit, repecting the original one.
                offset += KApplication::random() % (limit - songCount);

            if( findLocation == -1 ) // there is no limit
            {
                TQRegExp queryEnd( ";$" ); // find the end of the query an add a limit
                sql.replace( queryEnd, TQString(" LIMIT %1 OFFSET %2;" ).tqarg( songCount*5 ).tqarg( offset ) );
                useDirect = false;
            }
            else // there is a limit, so find it and replace it
                sql.replace( limitSearch, TQString(" LIMIT %1 OFFSET %2;" ).tqarg( songCount ).tqarg( offset ) );
        }
    }

    // only return the fields that we need
    sql.replace( TQRegExp( "SELECT.*FROM" ), "SELECT tags.url, tags.deviceid FROM" );
    TQStringList queryResult = CollectionDB::instance()->query( sql );
    TQStringList items;

    debug() << "Smart Playlist: adding urls from query: " << sql << endl;
    if ( !item->query().isEmpty() )
        //We have to filter all the un-needed results from query( sql )
        for( uint x=0; x < queryResult.count() ; x += 2 )
            items << MountPointManager::instance()->getAbsolutePath( queryResult[x+1].toInt(), queryResult[x] );
    else
        items = queryResult;


    KURL::List urls;
    foreach( items ) //we have to run setPath on all raw paths
    {
        KURL tmp;
        tmp.setPath( *it );
        urls << tmp;
    }
    KURL::List addMe;

    // we have to randomly select tracks from the returned query since we can't have
    // ORDER BY RAND() for some statements
    if( !useDirect )
    {
        for( uint i=0; i < songCount && urls.count(); i++ )
        {
            KURL::List::iterator newItem = urls.at( KApplication::random() % urls.count() );
            addMe << (*newItem);
            urls.remove( newItem );
        }
    }

    useDirect ?
            debug() << "Returning " << urls.count()  << " tracks from " << item->text(0) << endl:
            debug() << "Returning " << addMe.count() << " tracks from " << item->text(0) << endl;

    return useDirect ? urls : addMe;
}


KURL::List DynamicMode::retrieveTracks( const uint trackCount )
{
DEBUG_BLOCK
    KURL::List retrieval;

    // always rebuild with suggested mode since the artists will be changing
    if( m_cachedItemSet.count() <= trackCount || appendType() == SUGGESTION )
        rebuildCachedItemSet();

    for( uint i=0; i < trackCount; i++ )
    {
        if( m_cachedItemSet.isEmpty() )
            break;
        const int pos = KApplication::random() % m_cachedItemSet.count();
        KURL::List::iterator newItem = m_cachedItemSet.at( pos );
        if( TQFile::exists( (*newItem).path() ) )
            retrieval << (*newItem);
        m_cachedItemSet.remove( newItem );
    }

    return retrieval;
}