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