]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs/plugins.cpp
qt4: fix memleak.
[vlc] / modules / gui / qt4 / dialogs / plugins.cpp
1 /*****************************************************************************
2  * plugins.hpp : Plug-ins and extensions listing
3  ****************************************************************************
4  * Copyright (C) 2008-2010 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Jean-Baptiste Kempf <jb (at) videolan.org>
8  *          Jean-Philippe AndrĂ© <jpeg (at) 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 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include "plugins.hpp"
30
31 #include "util/customwidgets.hpp"
32 #include "extensions_manager.hpp"
33
34 #include <assert.h>
35
36 //#include <vlc_modules.h>
37
38 #include <QTreeWidget>
39 #include <QStringList>
40 #include <QTabWidget>
41 #include <QHeaderView>
42 #include <QDialogButtonBox>
43 #include <QLineEdit>
44 #include <QLabel>
45 #include <QVBoxLayout>
46 #include <QComboBox>
47 #include <QHBoxLayout>
48 #include <QVBoxLayout>
49 #include <QSpacerItem>
50 #include <QListView>
51 #include <QPainter>
52 #include <QStyleOptionViewItem>
53 #include <QKeyEvent>
54
55
56 PluginDialog::PluginDialog( intf_thread_t *_p_intf ) : QVLCFrame( _p_intf )
57 {
58     setWindowTitle( qtr( "Plugins and extensions" ) );
59     setWindowRole( "vlc-plugins" );
60
61     QVBoxLayout *layout = new QVBoxLayout( this );
62     tabs = new QTabWidget( this );
63     tabs->addTab( extensionTab = new ExtensionTab( p_intf ),
64                   qtr( "Extensions" ) );
65     tabs->addTab( pluginTab = new PluginTab( p_intf ),
66                   qtr( "Plugins" ) );
67     layout->addWidget( tabs );
68
69     QDialogButtonBox *box = new QDialogButtonBox;
70     QPushButton *okButton = new QPushButton( qtr( "&Close" ), this );
71     box->addButton( okButton, QDialogButtonBox::AcceptRole );
72     layout->addWidget( box );
73     BUTTONACT( okButton, close() );
74     readSettings( "PluginsDialog", QSize( 435, 280 ) );
75 }
76
77 PluginDialog::~PluginDialog()
78 {
79     writeSettings( "PluginsDialog" );
80 }
81
82 /* Plugins tab */
83
84 PluginTab::PluginTab( intf_thread_t *p_intf )
85         : QVLCFrame( p_intf )
86 {
87     QGridLayout *layout = new QGridLayout( this );
88
89     /* Main Tree for modules */
90     treePlugins = new QTreeWidget;
91     layout->addWidget( treePlugins, 0, 0, 1, -1 );
92
93     /* Users cannot move the columns around but we need to sort */
94     treePlugins->header()->setMovable( false );
95     treePlugins->header()->setSortIndicatorShown( true );
96     //    treePlugins->header()->setResizeMode( QHeaderView::ResizeToContents );
97     treePlugins->setAlternatingRowColors( true );
98     treePlugins->setColumnWidth( 0, 200 );
99
100     QStringList headerNames;
101     headerNames << qtr("Name") << qtr("Capability" ) << qtr( "Score" );
102     treePlugins->setHeaderLabels( headerNames );
103
104     FillTree();
105
106     /* Set capability column to the correct Size*/
107     treePlugins->resizeColumnToContents( 1 );
108     treePlugins->header()->restoreState(
109             getSettings()->value( "Plugins/Header-State" ).toByteArray() );
110
111     treePlugins->setSortingEnabled( true );
112     treePlugins->sortByColumn( 1, Qt::AscendingOrder );
113
114     QLabel *label = new QLabel( qtr("&Search:"), this );
115     edit = new SearchLineEdit( this );
116     label->setBuddy( edit );
117
118     layout->addWidget( label, 1, 0 );
119     layout->addWidget( edit, 1, 1, 1, 1 );
120     CONNECT( edit, textChanged( const QString& ),
121             this, search( const QString& ) );
122
123     setMinimumSize( 500, 300 );
124     readSettings( "Plugins", QSize( 540, 400 ) );
125 }
126
127 inline void PluginTab::FillTree()
128 {
129     module_t **p_list = module_list_get( NULL );
130     module_t *p_module;
131
132     for( unsigned int i = 0; (p_module = p_list[i] ) != NULL; i++ )
133     {
134         QStringList qs_item;
135         qs_item << qfu( module_get_name( p_module, true ) )
136                 << qfu( module_get_capability( p_module ) )
137                 << QString::number( module_get_score( p_module ) );
138 #ifndef DEBUG
139         if( qs_item.at(1).isEmpty() ) continue;
140 #endif
141
142         QTreeWidgetItem *item = new PluginTreeItem( qs_item );
143         treePlugins->addTopLevelItem( item );
144     }
145     module_list_free( p_list );
146 }
147
148 void PluginTab::search( const QString& qs )
149 {
150     QList<QTreeWidgetItem *> items = treePlugins->findItems( qs, Qt::MatchContains );
151     items += treePlugins->findItems( qs, Qt::MatchContains, 1 );
152
153     QTreeWidgetItem *item = NULL;
154     for( int i = 0; i < treePlugins->topLevelItemCount(); i++ )
155     {
156         item = treePlugins->topLevelItem( i );
157         item->setHidden( !items.contains( item ) );
158     }
159 }
160
161 PluginTab::~PluginTab()
162 {
163     writeSettings( "Plugins" );
164     getSettings()->setValue( "Plugins/Header-State",
165                              treePlugins->header()->saveState() );
166 }
167
168 bool PluginTreeItem::operator< ( const QTreeWidgetItem & other ) const
169 {
170     int col = treeWidget()->sortColumn();
171     if( col == 2 )
172         return text( col ).toInt() < other.text( col ).toInt();
173     return text( col ) < other.text( col );
174 }
175
176 /* Extensions tab */
177 ExtensionTab::ExtensionTab( intf_thread_t *p_intf )
178         : QVLCFrame( p_intf )
179 {
180     // Layout
181     QVBoxLayout *layout = new QVBoxLayout( this );
182
183     // ListView
184     extList = new QListView( this );
185     CONNECT( extList, activated( const QModelIndex& ),
186              this, moreInformation() );
187     layout->addWidget( extList );
188
189     // List item delegate
190     ExtensionItemDelegate *itemDelegate = new ExtensionItemDelegate( p_intf,
191                                                                      extList );
192     extList->setItemDelegate( itemDelegate );
193
194     // Extension list look & feeling
195     extList->setAlternatingRowColors( true );
196     extList->setSelectionMode( QAbstractItemView::SingleSelection );
197
198     // Model
199     ExtensionListModel *model = new ExtensionListModel( extList, p_intf );
200     extList->setModel( model );
201
202     // Buttons' layout
203     QHBoxLayout *hbox = new QHBoxLayout;
204     hbox->addItem( new QSpacerItem( 1, 1, QSizePolicy::Expanding,
205                                     QSizePolicy::Fixed ) );
206
207     // More information button
208     butMoreInfo = new QPushButton( QIcon( ":/menu/info" ),
209                                    qtr( "More information..." ),
210                                    this );
211     CONNECT( butMoreInfo, clicked(),
212              this, moreInformation() );
213     hbox->addWidget( butMoreInfo );
214
215     // Reload button
216     ExtensionsManager *EM = ExtensionsManager::getInstance( p_intf );
217     QPushButton *reload = new QPushButton( QIcon( ":/update" ),
218                                            qtr( "Reload extensions" ),
219                                            this );
220     CONNECT( reload, clicked(),
221              EM, reloadExtensions() );
222     hbox->addWidget( reload );
223
224     // Add buttons hbox
225     layout->addItem( hbox );
226 }
227
228 ExtensionTab::~ExtensionTab()
229 {
230 }
231
232 // Do not close on ESC or ENTER
233 void ExtensionTab::keyPressEvent( QKeyEvent *keyEvent )
234 {
235     keyEvent->ignore();
236 }
237
238 // Show more information
239 void ExtensionTab::moreInformation()
240 {
241     if( !extList->selectionModel() ||
242         extList->selectionModel()->selectedIndexes().isEmpty() )
243
244     {
245         return;
246     }
247
248     QModelIndex index = extList->selectionModel()->selectedIndexes().first();
249     ExtensionCopy *ext = (ExtensionCopy*) index.internalPointer();
250     if( !ext )
251         return;
252
253     ExtensionInfoDialog dlg( *ext, p_intf, this );
254     dlg.exec();
255 }
256
257 /* Safe copy of the extension_t struct */
258 class ExtensionCopy
259 {
260 public:
261     ExtensionCopy( extension_t *p_ext )
262     {
263         name = qfu( p_ext->psz_name );
264         description = qfu( p_ext->psz_description );
265         shortdesc = qfu( p_ext->psz_shortdescription );
266         if( description.isEmpty() )
267             description = shortdesc;
268         if( shortdesc.isEmpty() && !description.isEmpty() )
269             shortdesc = description;
270         title = qfu( p_ext->psz_title );
271         author = qfu( p_ext->psz_author );
272         version = qfu( p_ext->psz_version );
273         url = qfu( p_ext->psz_url );
274     }
275     ~ExtensionCopy() {}
276
277     QString name, title, description, shortdesc, author, version, url;
278 };
279
280 /* Extensions list model for the QListView */
281
282 ExtensionListModel::ExtensionListModel( QListView *view, intf_thread_t *intf )
283         : QAbstractListModel( view ), p_intf( intf )
284 {
285     // Connect to ExtensionsManager::extensionsUpdated()
286     ExtensionsManager* EM = ExtensionsManager::getInstance( p_intf );
287     CONNECT( EM, extensionsUpdated(), this, updateList() );
288
289     // Load extensions now if not already loaded
290     EM->loadExtensions();
291 }
292
293 ExtensionListModel::~ExtensionListModel()
294 {
295 }
296
297 void ExtensionListModel::updateList()
298 {
299     ExtensionCopy *ext;
300
301     // Clear extensions list
302     while( !extensions.isEmpty() )
303     {
304         ext = extensions.takeLast();
305         delete ext;
306     }
307
308     // Find new extensions
309     ExtensionsManager *EM = ExtensionsManager::getInstance( p_intf );
310     extensions_manager_t *p_mgr = EM->getManager();
311     if( !p_mgr )
312         return;
313
314     vlc_mutex_lock( &p_mgr->lock );
315     extension_t *p_ext;
316     FOREACH_ARRAY( p_ext, p_mgr->extensions )
317     {
318         ext = new ExtensionCopy( p_ext );
319         extensions.push_back( ext );
320     }
321     FOREACH_END()
322     vlc_mutex_unlock( &p_mgr->lock );
323     vlc_object_release( p_mgr );
324
325     emit dataChanged( index( 0 ), index( rowCount() - 1 ) );
326 }
327
328 int ExtensionListModel::rowCount( const QModelIndex& parent ) const
329 {
330     int count = 0;
331     ExtensionsManager *EM = ExtensionsManager::getInstance( p_intf );
332     extensions_manager_t *p_mgr = EM->getManager();
333     if( !p_mgr )
334         return 0;
335
336     vlc_mutex_lock( &p_mgr->lock );
337     count = p_mgr->extensions.i_size;
338     vlc_mutex_unlock( &p_mgr->lock );
339     vlc_object_release( p_mgr );
340
341     return count;
342 }
343
344 QVariant ExtensionListModel::data( const QModelIndex& index, int role ) const
345 {
346     if( !index.isValid() )
347         return QVariant();
348
349     switch( role )
350     {
351     default:
352         return QVariant();
353     }
354 }
355
356 QModelIndex ExtensionListModel::index( int row, int column,
357                                        const QModelIndex& parent ) const
358 {
359     if( column != 0 )
360         return QModelIndex();
361     if( row < 0 || row >= extensions.size() )
362         return QModelIndex();
363
364     return createIndex( row, 0, extensions.at( row ) );
365 }
366
367
368 /* Extension List Widget Item */
369 ExtensionItemDelegate::ExtensionItemDelegate( intf_thread_t *p_intf,
370                                               QListView *view )
371         : QStyledItemDelegate( view ), view( view ), p_intf( p_intf )
372 {
373 }
374
375 ExtensionItemDelegate::~ExtensionItemDelegate()
376 {
377 }
378
379 void ExtensionItemDelegate::paint( QPainter *painter,
380                                    const QStyleOptionViewItem &option,
381                                    const QModelIndex &index ) const
382 {
383     ExtensionCopy *ext = ( ExtensionCopy* ) index.internalPointer();
384     assert( ext != NULL );
385
386     int width = option.rect.width();
387     int height = option.rect.height();
388
389     // Pixmap: buffer where to draw
390     QPixmap pix(option.rect.size());
391
392     // Draw background
393     pix.fill( Qt::transparent ); // FIXME
394
395     // ItemView primitive style
396     QApplication::style()->drawPrimitive( QStyle::PE_PanelItemViewItem,
397                                           &option,
398                                           painter );
399
400     // Painter on the pixmap
401     QPainter *pixpaint = new QPainter(&pix);
402
403     // Text font & pen
404     QFont font = painter->font();
405     QPen pen = painter->pen();
406     if( view->selectionModel()->selectedIndexes().contains( index ) )
407     {
408         pen.setBrush( option.palette.highlightedText() );
409     }
410     else
411     {
412         pen.setBrush( option.palette.text() );
413     }
414     pixpaint->setPen( pen );
415     QFontMetrics metrics = option.fontMetrics;
416
417     /// @todo Add extension's icon
418
419     // Title: bold
420     font.setBold( true );
421     pixpaint->setFont( font );
422     pixpaint->drawText( QRect( 10, 7, width - 70, metrics.height() ),
423                         Qt::AlignLeft, ext->title );
424
425     // Short description: normal
426     font.setBold( false );
427     pixpaint->setFont( font );
428     pixpaint->drawText( QRect( 10, 7 + metrics.height(), width - 40,
429                                metrics.height() ),
430                         Qt::AlignLeft, ext->shortdesc );
431
432     // Version: italic
433     font.setItalic( true );
434     pixpaint->setFont( font );
435     pixpaint->drawText( width - 40, 7 + metrics.height(), ext->version );
436
437     // Flush paint operations
438     delete pixpaint;
439
440     // Draw it on the screen!
441     painter->drawPixmap( option.rect, pix );
442 }
443
444 QSize ExtensionItemDelegate::sizeHint( const QStyleOptionViewItem &option,
445                                        const QModelIndex &index ) const
446 {
447     if (index.isValid() && index.column() == 0)
448     {
449         QFontMetrics metrics = option.fontMetrics;
450         return QSize( 200, 14 + 2 * metrics.height() );
451     }
452     else
453         return QSize();
454 }
455
456 /* "More information" dialog */
457
458 ExtensionInfoDialog::ExtensionInfoDialog( const ExtensionCopy& extension,
459                                           intf_thread_t *p_intf,
460                                           QWidget *parent )
461        : QVLCDialog( parent, p_intf ),
462          extension( new ExtensionCopy( extension ) )
463 {
464     // Let's be a modal dialog
465     setWindowModality( Qt::WindowModal );
466
467     // Window title
468     setWindowTitle( qtr( "About" ) + " " + extension.title );
469
470     // Layout
471     QGridLayout *layout = new QGridLayout( this );
472
473     // Icon
474     /// @todo Use the extension's icon, when extensions will support icons :)
475     QLabel *icon = new QLabel( this );
476     QPixmap pix( ":/logo/vlc48.png" );
477     icon->setPixmap( pix );
478     layout->addWidget( icon, 1, 0, 2, 1 );
479
480     // Title
481     QLabel *label = new QLabel( extension.title, this );
482     QFont font = label->font();
483     font.setBold( true );
484     font.setPointSizeF( font.pointSizeF() * 1.3f );
485     label->setFont( font );
486     layout->addWidget( label, 0, 0, 1, -1 );
487
488     // Version
489     label = new QLabel( "<b>" + qtr( "Version" ) + ":</b>", this );
490     layout->addWidget( label, 1, 1, 1, 1, Qt::AlignBottom );
491     label = new QLabel( extension.version, this );
492     layout->addWidget( label, 1, 2, 1, 2, Qt::AlignBottom );
493
494     // Author
495     label = new QLabel( "<b>" + qtr( "Author" ) + ":</b>", this );
496     layout->addWidget( label, 2, 1, 1, 1, Qt::AlignTop );
497     label = new QLabel( extension.author, this );
498     layout->addWidget( label, 2, 2, 1, 2, Qt::AlignTop );
499
500
501     // Description
502     label = new QLabel( this );
503     label->setText( extension.description );
504     label->setWordWrap( true );
505     label->setOpenExternalLinks( true );
506     layout->addWidget( label, 4, 0, 1, -1 );
507
508     // URL
509     label = new QLabel( "<b>" + qtr( "Website" ) + ":</b>", this );
510     layout->addWidget( label, 5, 0, 1, 2 );
511     QString txt = "<a href=\"";
512     txt += extension.url;
513     txt += "\">";
514     txt += extension.url;
515     txt += "</a>";
516     label = new QLabel( txt, this );
517     label->setText( txt );
518     label->setOpenExternalLinks( true );
519     layout->addWidget( label, 5, 2, 1, -1 );
520
521     // Script file
522     label = new QLabel( "<b>" + qtr( "File" ) + ":</b>", this );
523     layout->addWidget( label, 6, 0, 1, 2 );
524     QLineEdit *line = new QLineEdit( extension.name, this );
525     layout->addWidget( line, 6, 2, 1, -1 );
526
527     // Close button
528     QDialogButtonBox *group = new QDialogButtonBox( this );
529     QPushButton *closeButton = new QPushButton( qtr( "&Close" ) );
530     group->addButton( closeButton, QDialogButtonBox::AcceptRole );
531     BUTTONACT( closeButton, close() );
532
533     layout->addWidget( group, 7, 0, 1, -1 );
534
535     // Fix layout
536     layout->setColumnStretch( 2, 1 );
537     layout->setRowStretch( 4, 1 );
538     setMinimumSize( 450, 350 );
539 }
540
541 ExtensionInfoDialog::~ExtensionInfoDialog()
542 {
543     delete extension;
544 }