]> git.sesse.net Git - vlc/blob - modules/gui/qt4/recents.cpp
9109989434fa1b0955e090927b6a3ef906d70192
[vlc] / modules / gui / qt4 / recents.cpp
1 /*****************************************************************************
2  * recents.cpp : Recents MRL (menu)
3  *****************************************************************************
4  * Copyright © 2006-2008 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Ludovic Fauvet <etix@l0cal.com>
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  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24
25 #include "recents.hpp"
26
27 #include <QList>
28 #include <QString>
29 #include <QAction>
30 #include <QSettings>
31 #include <QRegExp>
32 #include <QSignalMapper>
33
34 RecentsMRL* RecentsMRL::instance = NULL;
35
36 RecentsMRL::RecentsMRL( intf_thread_t *_p_intf ) : p_intf( _p_intf )
37 {
38     stack = new QList<QString>;
39     signalMapper = new QSignalMapper(this);
40
41     isActive = config_GetInt( p_intf, "qt-recentplay" );
42     filter = new QRegExp(
43             qfu( config_GetPsz( p_intf, "qt-recentplay-filter" ) ),
44             Qt::CaseInsensitive );
45
46     load();
47     if ( !isActive ) clear();
48 }
49
50 RecentsMRL::~RecentsMRL()
51 {
52     delete stack;
53 }
54
55 void RecentsMRL::addRecent( const QString &mrl )
56 {
57     if ( !isActive || filter->indexIn( mrl ) >= 0 )
58         return;
59
60     msg_Dbg( p_intf, "Adding a new MRL to recent ones: %s", qtu( mrl ) );
61     if( stack->contains( mrl ) )
62     {
63         stack->removeOne( mrl );
64         stack->prepend( mrl );
65     }
66     else
67     {
68         stack->prepend( mrl );
69         if( stack->size() > RECENTS_LIST_SIZE )
70             stack->takeLast();
71     }
72     emit updated();
73     save();
74 }
75
76 void RecentsMRL::clear()
77 {
78     if ( stack->isEmpty() )
79         return;
80     stack->clear();
81     emit updated();
82     save();
83 }
84
85 QList<QString> RecentsMRL::recents()
86 {
87     return QList<QString>(*stack);
88 }
89
90 void RecentsMRL::load()
91 {
92     QStringList list = getSettings()->value( "RecentsMRL/list" ).toStringList();
93
94     for( int i = 0; i < list.size(); ++i )
95     {
96         if (filter->indexIn( list.at(i) ) == -1)
97             stack->append( list.at(i) );
98     }
99 }
100
101 void RecentsMRL::save()
102 {
103     QStringList list;
104
105     for( int i = 0; i < stack->size(); ++i )
106         list << stack->at(i);
107
108     getSettings()->setValue( "RecentsMRL/list", list );
109 }
110