]> git.sesse.net Git - vlc/blob - modules/gui/qt4/recents.cpp
Qt: add a new class helper to open files
[vlc] / modules / gui / qt4 / recents.cpp
1 /*****************************************************************************
2  * recents.cpp : Recents MRL (menu)
3  *****************************************************************************
4  * Copyright © 2008-2014 VideoLAN and VLC authors
5  * $Id$
6  *
7  * Authors: Ludovic Fauvet <etix@l0cal.com>
8  *          Jean-baptiste Kempf <jb@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * ( at your option ) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 #include "qt4.hpp"
26 #include "recents.hpp"
27 #include "dialogs_provider.hpp"
28 #include "menus.hpp"
29
30 #include <QStringList>
31 #include <QRegExp>
32 #include <QSignalMapper>
33
34 #ifdef _WIN32
35     #include <shlobj.h>
36     /* typedef enum  {
37         SHARD_PIDL              = 0x00000001,
38         SHARD_PATHA             = 0x00000002,
39         SHARD_PATHW             = 0x00000003,
40         SHARD_APPIDINFO         = 0x00000004,
41         SHARD_APPIDINFOIDLIST   = 0x00000005,
42         SHARD_LINK              = 0x00000006,
43         SHARD_APPIDINFOLINK     = 0x00000007,
44         SHARD_SHELLITEM         = 0x00000008 
45     } SHARD; */
46     #define SHARD_PATHW 0x00000003
47
48     #include <vlc_charset.h>
49 #endif
50
51 RecentsMRL::RecentsMRL( intf_thread_t *_p_intf ) : p_intf( _p_intf )
52 {
53     stack = new QStringList;
54
55     signalMapper = new QSignalMapper( this );
56     CONNECT( signalMapper,
57             mapped(const QString & ),
58             DialogsProvider::getInstance( p_intf ),
59             playMRL( const QString & ) );
60
61     /* Load the filter psz */
62     char* psz_tmp = var_InheritString( p_intf, "qt-recentplay-filter" );
63     if( psz_tmp && *psz_tmp )
64         filter = new QRegExp( psz_tmp, Qt::CaseInsensitive );
65     else
66         filter = NULL;
67     free( psz_tmp );
68
69     load();
70     isActive = var_InheritBool( p_intf, "qt-recentplay" );
71     if( !isActive ) clear();
72 }
73
74 RecentsMRL::~RecentsMRL()
75 {
76     delete filter;
77     delete stack;
78 }
79
80 void RecentsMRL::addRecent( const QString &mrl )
81 {
82     if ( !isActive || ( filter && filter->indexIn( mrl ) >= 0 ) )
83         return;
84
85     msg_Dbg( p_intf, "Adding a new MRL to recent ones: %s", qtu( mrl ) );
86
87 #ifdef _WIN32
88     /* Add to the Windows 7 default list in taskbar */
89     char* path = make_path( qtu( mrl ) );
90     if( path )
91     {
92         wchar_t *wmrl = ToWide( path );
93         SHAddToRecentDocs( SHARD_PATHW, wmrl );
94         free( wmrl );
95         free( path );
96     }
97 #endif
98
99     int i_index = stack->indexOf( mrl );
100     if( 0 <= i_index )
101     {
102         /* move to the front */
103         stack->move( i_index, 0 );
104     }
105     else
106     {
107         stack->prepend( mrl );
108         if( stack->count() > RECENTS_LIST_SIZE )
109             stack->takeLast();
110     }
111     VLCMenuBar::updateRecents( p_intf );
112     save();
113 }
114
115 void RecentsMRL::clear()
116 {
117     if ( stack->isEmpty() )
118         return;
119
120     stack->clear();
121     if( isActive ) VLCMenuBar::updateRecents( p_intf );
122     save();
123 }
124
125 QStringList RecentsMRL::recents()
126 {
127     return *stack;
128 }
129
130 void RecentsMRL::load()
131 {
132     /* Load from the settings */
133     QStringList list = getSettings()->value( "RecentsMRL/list" ).toStringList();
134
135     /* And filter the regexp on the list */
136     for( int i = 0; i < list.count(); ++i )
137     {
138         if ( !filter || filter->indexIn( list.at(i) ) == -1 )
139             stack->append( list.at(i) );
140     }
141 }
142
143 void RecentsMRL::save()
144 {
145     getSettings()->setValue( "RecentsMRL/list", *stack );
146 }
147
148 playlist_item_t *RecentsMRL::toPlaylist(int length)
149 {
150     playlist_item_t *p_node_recent = playlist_NodeCreate(THEPL, _("Recently Played"), THEPL->p_root, PLAYLIST_END, PLAYLIST_RO_FLAG, NULL);
151
152     if ( p_node_recent == NULL )  return NULL;
153
154     if (length == 0 || stack->count() < length)
155         length = stack->count();
156
157     for (int i = 0; i < length; i++)
158     {
159         input_item_t *p_input = input_item_New(qtu(stack->at(i)), NULL);
160         playlist_NodeAddInput(THEPL, p_input, p_node_recent, PLAYLIST_APPEND, PLAYLIST_END, false);
161     }
162
163     return p_node_recent;
164 }
165
166 void Open::openMRL( intf_thread_t *p_intf,
167                     const QString &mrl,
168                     bool b_start,
169                     bool b_playlist)
170 {
171     playlist_Add( THEPL, qtu(mrl), NULL,
172                   PLAYLIST_APPEND | (b_start ? PLAYLIST_GO : PLAYLIST_PREPARSE),
173                   PLAYLIST_END,
174                   b_playlist,
175                   pl_Unlocked );
176
177     if( b_start && b_playlist )
178         RecentsMRL::getInstance( p_intf )->addRecent( mrl );
179 }