]> git.sesse.net Git - vlc/blob - modules/gui/qt4/recents.cpp
Qt: no need to go through the MainInterface to forward the signals about recentItems.
[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     filter = new QRegExp( psz_tmp, Qt::CaseInsensitive );
50     free( psz_tmp );
51
52     load();
53     if ( !isActive ) clear();
54 }
55
56 RecentsMRL::~RecentsMRL()
57 {
58     delete filter;
59     delete stack;
60 }
61
62 void RecentsMRL::addRecent( const QString &mrl )
63 {
64     if ( !isActive || filter->indexIn( mrl ) >= 0 )
65         return;
66
67     msg_Dbg( p_intf, "Adding a new MRL to recent ones: %s", qtu( mrl ) );
68     int i_index = stack->indexOf( mrl );
69     if( 0 <= i_index )
70     {
71         /* move to the front */
72         stack->move( i_index, 0 );
73     }
74     else
75     {
76         stack->prepend( mrl );
77         if( stack->size() > RECENTS_LIST_SIZE )
78             stack->takeLast();
79     }
80     QVLCMenu::updateRecents( p_intf );
81     save();
82 }
83
84 void RecentsMRL::clear()
85 {
86     if ( stack->isEmpty() )
87         return;
88     stack->clear();
89     QVLCMenu::updateRecents( p_intf );
90     save();
91 }
92
93 QList<QString> RecentsMRL::recents()
94 {
95     return QList<QString>(*stack);
96 }
97
98 void RecentsMRL::load()
99 {
100     QStringList list = getSettings()->value( "RecentsMRL/list" ).toStringList();
101
102     for( int i = 0; i < list.size(); ++i )
103     {
104         if (filter->indexIn( list.at(i) ) == -1)
105             stack->append( list.at(i) );
106     }
107 }
108
109 void RecentsMRL::save()
110 {
111     QStringList list;
112
113     for( int i = 0; i < stack->size(); ++i )
114         list << stack->at(i);
115
116     getSettings()->setValue( "RecentsMRL/list", list );
117 }
118