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