]> git.sesse.net Git - vlc/blob - modules/gui/qt4/recents.cpp
Recent: don't leak just for fun.
[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 #include "dialogs_provider.hpp"
27
28 #include <QList>
29 #include <QString>
30 #include <QAction>
31 #include <QSettings>
32 #include <QRegExp>
33 #include <QSignalMapper>
34
35 RecentsMRL* RecentsMRL::instance = NULL;
36
37 RecentsMRL::RecentsMRL( intf_thread_t *_p_intf ) : p_intf( _p_intf )
38 {
39     stack = new QList<QString>;
40     signalMapper = new QSignalMapper(this);
41     CONNECT( signalMapper,
42             mapped(const QString & ),
43             DialogsProvider::getInstance( p_intf ),
44             playMRL( const QString & ) );
45
46     isActive = config_GetInt( p_intf, "qt-recentplay" );
47     char* psz_tmp = config_GetPsz( p_intf, "qt-recentplay-filter" );
48     filter = new QRegExp( psz_tmp, Qt::CaseInsensitive );
49     free( psz_tmp );
50
51     load();
52     if ( !isActive ) clear();
53 }
54
55 RecentsMRL::~RecentsMRL()
56 {
57     delete filter;
58     delete stack;
59 }
60
61 void RecentsMRL::addRecent( const QString &mrl )
62 {
63     if ( !isActive || filter->indexIn( mrl ) >= 0 )
64         return;
65
66     msg_Dbg( p_intf, "Adding a new MRL to recent ones: %s", qtu( mrl ) );
67     int i_index = stack->indexOf( mrl );
68     if( 0 <= i_index )
69     {
70         /* move to the front */
71         stack->move( i_index, 0 );
72     }
73     else
74     {
75         stack->prepend( mrl );
76         if( stack->size() > RECENTS_LIST_SIZE )
77             stack->takeLast();
78     }
79     emit updated();
80     save();
81 }
82
83 void RecentsMRL::clear()
84 {
85     if ( stack->isEmpty() )
86         return;
87     stack->clear();
88     emit updated();
89     save();
90 }
91
92 QList<QString> RecentsMRL::recents()
93 {
94     return QList<QString>(*stack);
95 }
96
97 void RecentsMRL::load()
98 {
99     QStringList list = getSettings()->value( "RecentsMRL/list" ).toStringList();
100
101     for( int i = 0; i < list.size(); ++i )
102     {
103         if (filter->indexIn( list.at(i) ) == -1)
104             stack->append( list.at(i) );
105     }
106 }
107
108 void RecentsMRL::save()
109 {
110     QStringList list;
111
112     for( int i = 0; i < stack->size(); ++i )
113         list << stack->at(i);
114
115     getSettings()->setValue( "RecentsMRL/list", list );
116 }
117