gui: qt: use float for rate
[vlc.git] / modules / gui / qt / recents.cpp
blob44b700479bd6cc00f41649625803232ce0bbf94b
1 /*****************************************************************************
2 * recents.cpp : Recents MRL (menu)
3 *****************************************************************************
4 * Copyright © 2008-2014 VideoLAN and VLC authors
6 * Authors: Ludovic Fauvet <etix@l0cal.com>
7 * Jean-baptiste Kempf <jb@videolan.org>
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.
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.
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 *****************************************************************************/
24 #include "qt.hpp"
25 #include "recents.hpp"
26 #include "dialogs_provider.hpp"
27 #include "menus.hpp"
28 #include "util/qt_dirs.hpp"
30 #include <QStringList>
31 #include <QRegExp>
32 #include <QSignalMapper>
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
48 #include <vlc_charset.h>
49 #endif
51 #include <vlc_input_item.h>
53 RecentsMRL::RecentsMRL( intf_thread_t *_p_intf ) : p_intf( _p_intf )
55 recents = QStringList();
56 times = QStringList();
58 signalMapper = new QSignalMapper( this );
59 CONNECT( signalMapper,
60 mapped(const QString & ),
61 this,
62 playMRL( const QString & ) );
64 /* Load the filter psz */
65 char* psz_tmp = var_InheritString( p_intf, "qt-recentplay-filter" );
66 if( psz_tmp && *psz_tmp )
67 filter = new QRegExp( psz_tmp, Qt::CaseInsensitive );
68 else
69 filter = NULL;
70 free( psz_tmp );
72 load();
73 isActive = var_InheritBool( p_intf, "qt-recentplay" );
74 if( !isActive ) clear();
77 RecentsMRL::~RecentsMRL()
79 save();
80 delete filter;
83 void RecentsMRL::addRecent( const QString &mrl )
85 if ( !isActive || ( filter && filter->indexIn( mrl ) >= 0 ) )
86 return;
88 #ifdef _WIN32
89 /* Add to the Windows 7 default list in taskbar */
90 char* path = vlc_uri2path( qtu( mrl ) );
91 if( path )
93 wchar_t *wmrl = ToWide( path );
94 SHAddToRecentDocs( SHARD_PATHW, wmrl );
95 free( wmrl );
96 free( path );
98 #endif
100 int i_index = recents.indexOf( mrl );
101 if( 0 <= i_index )
103 /* move to the front */
104 recents.move( i_index, 0 );
105 times.move( i_index, 0 );
107 else
109 recents.prepend( mrl );
110 times.prepend( "-1" );
111 if( recents.count() > RECENTS_LIST_SIZE ) {
112 recents.takeLast();
113 times.takeLast();
116 VLCMenuBar::updateRecents( p_intf );
117 save();
120 void RecentsMRL::clear()
122 if ( recents.isEmpty() )
123 return;
125 recents.clear();
126 times.clear();
127 if( isActive ) VLCMenuBar::updateRecents( p_intf );
128 save();
131 QStringList RecentsMRL::recentList()
133 return recents;
136 void RecentsMRL::load()
138 /* Load from the settings */
139 QStringList list = getSettings()->value( "RecentsMRL/list" ).toStringList();
140 QStringList list2 = getSettings()->value( "RecentsMRL/times" ).toStringList();
142 /* And filter the regexp on the list */
143 for( int i = 0; i < list.count(); ++i )
145 if ( !filter || filter->indexIn( list.at(i) ) == -1 ) {
146 recents.append( list.at(i) );
147 times.append( list2.value(i, "-1" ) );
152 void RecentsMRL::save()
154 getSettings()->setValue( "RecentsMRL/list", recents );
155 getSettings()->setValue( "RecentsMRL/times", times );
158 void RecentsMRL::playMRL( const QString &mrl )
160 Open::openMRL( p_intf, mrl );
163 vlc_tick_t RecentsMRL::time( const QString &mrl )
165 if( !isActive )
166 return -1;
168 int i_index = recents.indexOf( mrl );
169 if( i_index != -1 )
170 return VLC_TICK_FROM_MS(times.value(i_index, "-1").toInt());
171 else
172 return -1;
175 void RecentsMRL::setTime( const QString &mrl, const vlc_tick_t time )
177 int i_index = recents.indexOf( mrl );
178 if( i_index != -1 )
179 times[i_index] = QString::number( MS_FROM_VLC_TICK( time ) );
182 int Open::openMRL( intf_thread_t *p_intf,
183 const QString &mrl,
184 bool b_start )
186 return openMRLwithOptions( p_intf, mrl, NULL, b_start );
189 int Open::openMRLwithOptions( intf_thread_t* p_intf,
190 const QString &mrl,
191 QStringList *options,
192 bool b_start,
193 const char *title)
195 /* Options */
196 const char **ppsz_options = NULL;
197 int i_options = 0;
199 if( options != NULL && options->count() > 0 )
201 ppsz_options = new const char *[options->count()];
202 for( int j = 0; j < options->count(); j++ ) {
203 QString option = colon_unescape( options->at(j) );
204 if( !option.isEmpty() ) {
205 ppsz_options[i_options] = strdup(qtu(option));
206 i_options++;
211 /* Add to playlist */
212 int i_ret = playlist_AddExt( THEPL, qtu(mrl), title, b_start,
213 i_options, ppsz_options, VLC_INPUT_OPTION_TRUSTED );
215 /* Add to recent items, only if played */
216 if( i_ret == VLC_SUCCESS && b_start )
217 RecentsMRL::getInstance( p_intf )->addRecent( mrl );
219 /* Free options */
220 if ( ppsz_options != NULL )
222 for ( int i = 0; i < i_options; ++i )
223 free( (char*)ppsz_options[i] );
224 delete[] ppsz_options;
226 return i_ret;