]> git.sesse.net Git - vlc/blob - modules/gui/qt4/recents.cpp
qt4: fix memory leak
[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 #include "util/qt_dirs.hpp"
30
31 #include <QStringList>
32 #include <QRegExp>
33 #include <QSignalMapper>
34
35 #ifdef _WIN32
36     #include <shlobj.h>
37     /* typedef enum  {
38         SHARD_PIDL              = 0x00000001,
39         SHARD_PATHA             = 0x00000002,
40         SHARD_PATHW             = 0x00000003,
41         SHARD_APPIDINFO         = 0x00000004,
42         SHARD_APPIDINFOIDLIST   = 0x00000005,
43         SHARD_LINK              = 0x00000006,
44         SHARD_APPIDINFOLINK     = 0x00000007,
45         SHARD_SHELLITEM         = 0x00000008 
46     } SHARD; */
47     #define SHARD_PATHW 0x00000003
48
49     #include <vlc_charset.h>
50 #endif
51
52 RecentsMRL::RecentsMRL( intf_thread_t *_p_intf ) : p_intf( _p_intf )
53 {
54     recents = QStringList();
55     times = QStringList();
56
57     signalMapper = new QSignalMapper( this );
58     CONNECT( signalMapper,
59             mapped(const QString & ),
60             this,
61             playMRL( const QString & ) );
62
63     /* Load the filter psz */
64     char* psz_tmp = var_InheritString( p_intf, "qt-recentplay-filter" );
65     if( psz_tmp && *psz_tmp )
66         filter = new QRegExp( psz_tmp, Qt::CaseInsensitive );
67     else
68         filter = NULL;
69     free( psz_tmp );
70
71     load();
72     isActive = var_InheritBool( p_intf, "qt-recentplay" );
73     if( !isActive ) clear();
74 }
75
76 RecentsMRL::~RecentsMRL()
77 {
78     save();
79     delete filter;
80 }
81
82 void RecentsMRL::addRecent( const QString &mrl )
83 {
84     if ( !isActive || ( filter && filter->indexIn( mrl ) >= 0 ) )
85         return;
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 = recents.indexOf( mrl );
100     if( 0 <= i_index )
101     {
102         /* move to the front */
103         recents.move( i_index, 0 );
104         times.move( i_index, 0 );
105     }
106     else
107     {
108         recents.prepend( mrl );
109         times.prepend( "-1" );
110         if( recents.count() > RECENTS_LIST_SIZE ) {
111             recents.takeLast();
112             times.takeLast();
113         }
114     }
115     VLCMenuBar::updateRecents( p_intf );
116     save();
117 }
118
119 void RecentsMRL::clear()
120 {
121     if ( recents.isEmpty() )
122         return;
123
124     recents.clear();
125     times.clear();
126     if( isActive ) VLCMenuBar::updateRecents( p_intf );
127     save();
128 }
129
130 QStringList RecentsMRL::recentList()
131 {
132     return recents;
133 }
134
135 void RecentsMRL::load()
136 {
137     /* Load from the settings */
138     QStringList list = getSettings()->value( "RecentsMRL/list" ).toStringList();
139     QStringList list2 = getSettings()->value( "RecentsMRL/times" ).toStringList();
140
141     /* And filter the regexp on the list */
142     for( int i = 0; i < list.count(); ++i )
143     {
144         if ( !filter || filter->indexIn( list.at(i) ) == -1 ) {
145             recents.append( list.at(i) );
146             times.append( list2.value(i, "-1" ) );
147         }
148     }
149 }
150
151 void RecentsMRL::save()
152 {
153     getSettings()->setValue( "RecentsMRL/list", recents );
154     getSettings()->setValue( "RecentsMRL/times", times );
155 }
156
157 playlist_item_t *RecentsMRL::toPlaylist(int length)
158 {
159     playlist_item_t *p_node_recent = playlist_NodeCreate(THEPL, _("Recently Played"), THEPL->p_root, PLAYLIST_END, PLAYLIST_RO_FLAG, NULL);
160
161     if ( p_node_recent == NULL )  return NULL;
162
163     if (length == 0 || recents.count() < length)
164         length = recents.count();
165
166     for (int i = 0; i < length; i++)
167     {
168         input_item_t *p_input = input_item_New(qtu(recents.at(i)), NULL);
169         playlist_NodeAddInput(THEPL, p_input, p_node_recent, PLAYLIST_APPEND, PLAYLIST_END, false);
170     }
171
172     return p_node_recent;
173 }
174
175 void RecentsMRL::playMRL( const QString &mrl )
176 {
177     Open::openMRL( p_intf, mrl );
178 }
179
180 int RecentsMRL::time( const QString &mrl )
181 {
182     if( !isActive )
183         return -1;
184
185     int i_index = recents.indexOf( mrl );
186     if( i_index != -1 )
187         return times.value(i_index, "-1").toInt();
188     else
189         return -1;
190 }
191
192 void RecentsMRL::setTime( const QString &mrl, const int64_t time )
193 {
194     int i_index = recents.indexOf( mrl );
195     if( i_index != -1 )
196         times[i_index] = QString::number( time / 1000 );
197 }
198
199 int Open::openMRL( intf_thread_t *p_intf,
200                     const QString &mrl,
201                     bool b_start,
202                     bool b_playlist)
203 {
204     return openMRLwithOptions( p_intf, mrl, NULL, b_start, b_playlist );
205 }
206
207 int Open::openMRLwithOptions( intf_thread_t* p_intf,
208                      const QString &mrl,
209                      QStringList *options,
210                      bool b_start,
211                      bool b_playlist,
212                      const char *title)
213 {
214     /* Options */
215     const char **ppsz_options = NULL;
216     int i_options = 0;
217
218     if( options != NULL && options->count() > 0 )
219     {
220         ppsz_options = new const char *[options->count()];
221         for( int j = 0; j < options->count(); j++ ) {
222             QString option = colon_unescape( options->at(j) );
223             if( !option.isEmpty() ) {
224                 ppsz_options[j] = qtu(option);
225                 i_options++;
226             }
227         }
228     }
229
230     /* Add to playlist */
231     int i_ret = playlist_AddExt( THEPL,
232                   qtu(mrl), title,
233                   PLAYLIST_APPEND | (b_start ? PLAYLIST_GO : PLAYLIST_PREPARSE),
234                   PLAYLIST_END,
235                   -1,
236                   i_options, ppsz_options, VLC_INPUT_OPTION_TRUSTED,
237                   b_playlist,
238                   pl_Unlocked );
239
240     /* Add to recent items, only if played */
241     if( i_ret == VLC_SUCCESS && b_start && b_playlist )
242         RecentsMRL::getInstance( p_intf )->addRecent( mrl );
243
244     delete[] options;
245     return i_ret;
246 }
247
248