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