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