]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs/plugins.cpp
b549db4bebb3ed72c6d4eb7c3fef0b2af9ed2651
[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 == PluginTab::SCORE )
187         return text( col ).toInt() < other.text( col ).toInt();
188     else if ( col == PluginTab::CAPABILITY )
189     {
190         if ( text( PluginTab::CAPABILITY ) == other.text( PluginTab::CAPABILITY ) )
191             return text( PluginTab::NAME ) < other.text( PluginTab::NAME );
192         else
193             return text( PluginTab::CAPABILITY ) < other.text( PluginTab::CAPABILITY );
194     }
195     return text( col ) < other.text( col );
196 }
197
198 /* Extensions tab */
199 ExtensionTab::ExtensionTab( intf_thread_t *p_intf_ )
200         : QVLCFrame( p_intf_ )
201 {
202     // Layout
203     QVBoxLayout *layout = new QVBoxLayout( this );
204
205     QLabel *notice = new QLabel( qtr("Get more extensions from")
206             + QString( " <a href=\"http://addons.videolan.org/\">"
207                        "addons.videolan.org</a>." ) );
208     notice->setOpenExternalLinks( true );
209     layout->addWidget( notice );
210
211     // ListView
212     extList = new QListView( this );
213     CONNECT( extList, activated( const QModelIndex& ),
214              this, moreInformation() );
215     layout->addWidget( extList );
216
217     // List item delegate
218     ExtensionItemDelegate *itemDelegate = new ExtensionItemDelegate( p_intf,
219                                                                      extList );
220     extList->setItemDelegate( itemDelegate );
221
222     // Extension list look & feeling
223     extList->setAlternatingRowColors( true );
224     extList->setSelectionMode( QAbstractItemView::SingleSelection );
225
226     // Model
227     ExtensionListModel *model = new ExtensionListModel( extList, p_intf );
228     extList->setModel( model );
229
230     // Buttons' layout
231     QHBoxLayout *hbox = new QHBoxLayout;
232     hbox->addItem( new QSpacerItem( 1, 1, QSizePolicy::Expanding,
233                                     QSizePolicy::Fixed ) );
234
235     // More information button
236     butMoreInfo = new QPushButton( QIcon( ":/menu/info" ),
237                                    qtr( "More information..." ),
238                                    this );
239     CONNECT( butMoreInfo, clicked(), this, moreInformation() );
240     hbox->addWidget( butMoreInfo );
241
242     // Reload button
243     ExtensionsManager *EM = ExtensionsManager::getInstance( p_intf );
244     QPushButton *reload = new QPushButton( QIcon( ":/update" ),
245                                            qtr( "Reload extensions" ),
246                                            this );
247     CONNECT( reload, clicked(), EM, reloadExtensions() );
248     CONNECT( reload, clicked(), this, updateButtons() );
249     CONNECT( extList->selectionModel(),
250              selectionChanged( const QItemSelection &, const QItemSelection & ),
251              this,
252              updateButtons() );
253     hbox->addWidget( reload );
254
255     // Add buttons hbox
256     layout->addItem( hbox );
257     updateButtons();
258 }
259
260 ExtensionTab::~ExtensionTab()
261 {
262 }
263
264 void ExtensionTab::updateButtons()
265 {
266     butMoreInfo->setEnabled( extList->selectionModel()->hasSelection() );
267 }
268
269 // Do not close on ESC or ENTER
270 void ExtensionTab::keyPressEvent( QKeyEvent *keyEvent )
271 {
272     if( keyEvent->key() == Qt::Key_Return ||
273         keyEvent->key() == Qt::Key_Enter )
274         keyEvent->accept();
275     else
276         keyEvent->ignore();
277 }
278
279 // Show more information
280 void ExtensionTab::moreInformation()
281 {
282     QModelIndex index = extList->selectionModel()->selectedIndexes().first();
283
284     if( !index.isValid() )
285         return;
286
287     ExtensionInfoDialog dlg( index, p_intf, this );
288     dlg.exec();
289 }
290
291 /* Safe copy of the extension_t struct */
292 ExtensionListModel::ExtensionCopy::ExtensionCopy( extension_t *p_ext )
293 {
294     name = qfu( p_ext->psz_name );
295     description = qfu( p_ext->psz_description );
296     shortdesc = qfu( p_ext->psz_shortdescription );
297     if( description.isEmpty() )
298         description = shortdesc;
299     if( shortdesc.isEmpty() && !description.isEmpty() )
300         shortdesc = description;
301     title = qfu( p_ext->psz_title );
302     author = qfu( p_ext->psz_author );
303     version = qfu( p_ext->psz_version );
304     url = qfu( p_ext->psz_url );
305     icon = loadPixmapFromData( p_ext->p_icondata, p_ext->i_icondata_size );
306 }
307
308 ExtensionListModel::ExtensionCopy::~ExtensionCopy()
309 {
310     delete icon;
311 }
312
313 QVariant ExtensionListModel::ExtensionCopy::data( int role ) const
314 {
315     switch( role )
316     {
317     case Qt::DisplayRole:
318         return title;
319     case Qt::DecorationRole:
320         if ( !icon ) return QPixmap( ":/logo/vlc48.png" );
321         return *icon;
322     case DescriptionRole:
323         return shortdesc;
324     case VersionRole:
325         return version;
326     case AuthorRole:
327         return author;
328     case LinkRole:
329         return url;
330     case NameRole:
331         return name;
332     default:
333         return QVariant();
334     }
335 }
336
337 /* Extensions list model for the QListView */
338
339 ExtensionListModel::ExtensionListModel( QListView *view, intf_thread_t *intf )
340         : QAbstractListModel( view ), p_intf( intf )
341 {
342     // Connect to ExtensionsManager::extensionsUpdated()
343     ExtensionsManager* EM = ExtensionsManager::getInstance( p_intf );
344     CONNECT( EM, extensionsUpdated(), this, updateList() );
345
346     // Load extensions now if not already loaded
347     EM->loadExtensions();
348 }
349
350 ExtensionListModel::~ExtensionListModel()
351 {
352     // Clear extensions list
353     while( !extensions.isEmpty() )
354         delete extensions.takeLast();
355 }
356
357 void ExtensionListModel::updateList()
358 {
359     ExtensionCopy *ext;
360
361     // Clear extensions list
362     while( !extensions.isEmpty() )
363     {
364         ext = extensions.takeLast();
365         delete ext;
366     }
367
368     // Find new extensions
369     ExtensionsManager *EM = ExtensionsManager::getInstance( p_intf );
370     extensions_manager_t *p_mgr = EM->getManager();
371     if( !p_mgr )
372         return;
373
374     vlc_mutex_lock( &p_mgr->lock );
375     extension_t *p_ext;
376     FOREACH_ARRAY( p_ext, p_mgr->extensions )
377     {
378         ext = new ExtensionCopy( p_ext );
379         extensions.append( ext );
380     }
381     FOREACH_END()
382     vlc_mutex_unlock( &p_mgr->lock );
383     vlc_object_release( p_mgr );
384
385     emit dataChanged( index( 0 ), index( rowCount() - 1 ) );
386 }
387
388 int ExtensionListModel::rowCount( const QModelIndex& ) const
389 {
390     int count = 0;
391     ExtensionsManager *EM = ExtensionsManager::getInstance( p_intf );
392     extensions_manager_t *p_mgr = EM->getManager();
393     if( !p_mgr )
394         return 0;
395
396     vlc_mutex_lock( &p_mgr->lock );
397     count = p_mgr->extensions.i_size;
398     vlc_mutex_unlock( &p_mgr->lock );
399     vlc_object_release( p_mgr );
400
401     return count;
402 }
403
404 QVariant ExtensionListModel::data( const QModelIndex& index, int role ) const
405 {
406     if( !index.isValid() )
407         return QVariant();
408
409     ExtensionCopy * extension =
410             static_cast<ExtensionCopy *>(index.internalPointer());
411
412     return extension->data( role );
413 }
414
415 QModelIndex ExtensionListModel::index( int row, int column,
416                                        const QModelIndex& ) const
417 {
418     if( column != 0 )
419         return QModelIndex();
420     if( row < 0 || row >= extensions.count() )
421         return QModelIndex();
422
423     return createIndex( row, 0, extensions.at( row ) );
424 }
425
426
427 /* Extension List Widget Item */
428 ExtensionItemDelegate::ExtensionItemDelegate( intf_thread_t *p_intf,
429                                               QListView *view )
430         : QStyledItemDelegate( view ), view( view ), p_intf( p_intf )
431 {
432 }
433
434 ExtensionItemDelegate::~ExtensionItemDelegate()
435 {
436 }
437
438 void ExtensionItemDelegate::paint( QPainter *painter,
439                                    const QStyleOptionViewItem &option,
440                                    const QModelIndex &index ) const
441 {
442     int width = option.rect.width();
443
444     // Pixmap: buffer where to draw
445     QPixmap pix(option.rect.size());
446
447     // Draw background
448     pix.fill( Qt::transparent ); // FIXME
449
450     // ItemView primitive style
451     QApplication::style()->drawPrimitive( QStyle::PE_PanelItemViewItem,
452                                           &option,
453                                           painter );
454
455     // Painter on the pixmap
456     QPainter *pixpaint = new QPainter(&pix);
457
458     // Text font & pen
459     QFont font = painter->font();
460     QPen pen = painter->pen();
461     if( view->selectionModel()->selectedIndexes().contains( index ) )
462     {
463         pen.setBrush( option.palette.highlightedText() );
464     }
465     else
466     {
467         pen.setBrush( option.palette.text() );
468     }
469     pixpaint->setPen( pen );
470     QFontMetrics metrics = option.fontMetrics;
471
472     // Icon
473     QPixmap icon = index.data( Qt::DecorationRole ).value<QPixmap>();
474     if( !icon.isNull() )
475     {
476         pixpaint->drawPixmap( 7, 7, 2*metrics.height(), 2*metrics.height(),
477                               icon );
478     }
479
480     // Title: bold
481     pixpaint->setRenderHint( QPainter::TextAntialiasing );
482     font.setBold( true );
483     pixpaint->setFont( font );
484     pixpaint->drawText( QRect( 17 + 2 * metrics.height(), 7,
485                                width - 40 - 2 * metrics.height(),
486                                metrics.height() ),
487                         Qt::AlignLeft, index.data( Qt::DisplayRole ).toString() );
488
489     // Short description: normal
490     font.setBold( false );
491     pixpaint->setFont( font );
492     pixpaint->drawText( QRect( 17 + 2 * metrics.height(),
493                                7 + metrics.height(), width - 40,
494                                metrics.height() ),
495                         Qt::AlignLeft, index.data( ExtensionListModel::DescriptionRole ).toString() );
496
497     // Flush paint operations
498     delete pixpaint;
499
500     // Draw it on the screen!
501     painter->drawPixmap( option.rect, pix );
502 }
503
504 QSize ExtensionItemDelegate::sizeHint( const QStyleOptionViewItem &option,
505                                        const QModelIndex &index ) const
506 {
507     if (index.isValid() && index.column() == 0)
508     {
509         QFontMetrics metrics = option.fontMetrics;
510         return QSize( 200, 14 + 2 * metrics.height() );
511     }
512     else
513         return QSize();
514 }
515
516 /* "More information" dialog */
517
518 ExtensionInfoDialog::ExtensionInfoDialog( const QModelIndex &index,
519                                           intf_thread_t *p_intf,
520                                           QWidget *parent )
521        : QVLCDialog( parent, p_intf )
522 {
523     // Let's be a modal dialog
524     setWindowModality( Qt::WindowModal );
525
526     // Window title
527     setWindowTitle( qtr( "About" ) + " " + index.data(Qt::DisplayRole).toString() );
528
529     // Layout
530     QGridLayout *layout = new QGridLayout( this );
531
532     // Icon
533     QLabel *icon = new QLabel( this );
534     QPixmap pix = index.data(Qt::DecorationRole).value<QPixmap>();
535     Q_ASSERT( !pix.isNull() );
536     icon->setPixmap( pix );
537     icon->setAlignment( Qt::AlignCenter );
538     icon->setFixedSize( 48, 48 );
539     layout->addWidget( icon, 1, 0, 2, 1 );
540
541     // Title
542     QLabel *label = new QLabel( index.data(Qt::DisplayRole).toString(), this );
543     QFont font = label->font();
544     font.setBold( true );
545     font.setPointSizeF( font.pointSizeF() * 1.3f );
546     label->setFont( font );
547     layout->addWidget( label, 0, 0, 1, -1 );
548
549     // Version
550     label = new QLabel( "<b>" + qtr( "Version" ) + ":</b>", this );
551     layout->addWidget( label, 1, 1, 1, 1, Qt::AlignBottom );
552     label = new QLabel( index.data(ExtensionListModel::VersionRole).toString(), this );
553     layout->addWidget( label, 1, 2, 1, 2, Qt::AlignBottom );
554
555     // Author
556     label = new QLabel( "<b>" + qtr( "Author" ) + ":</b>", this );
557     layout->addWidget( label, 2, 1, 1, 1, Qt::AlignTop );
558     label = new QLabel( index.data(ExtensionListModel::AuthorRole).toString(), this );
559     layout->addWidget( label, 2, 2, 1, 2, Qt::AlignTop );
560
561
562     // Description
563     label = new QLabel( this );
564     label->setText( index.data(ExtensionListModel::DescriptionRole).toString() );
565     label->setWordWrap( true );
566     label->setOpenExternalLinks( true );
567     layout->addWidget( label, 4, 0, 1, -1 );
568
569     // URL
570     label = new QLabel( "<b>" + qtr( "Website" ) + ":</b>", this );
571     layout->addWidget( label, 5, 0, 1, 2 );
572     label = new QLabel( QString("<a href=\"%1\">%2</a>")
573                         .arg( index.data(ExtensionListModel::LinkRole).toString() )
574                         .arg( index.data(ExtensionListModel::LinkRole).toString() )
575                         , this );
576     label->setOpenExternalLinks( true );
577     layout->addWidget( label, 5, 2, 1, -1 );
578
579     // Script file
580     label = new QLabel( "<b>" + qtr( "File" ) + ":</b>", this );
581     layout->addWidget( label, 6, 0, 1, 2 );
582     QLineEdit *line =
583             new QLineEdit( index.data(ExtensionListModel::NameRole).toString(), this );
584     line->setReadOnly( true );
585     layout->addWidget( line, 6, 2, 1, -1 );
586
587     // Close button
588     QDialogButtonBox *group = new QDialogButtonBox( this );
589     QPushButton *closeButton = new QPushButton( qtr( "&Close" ) );
590     group->addButton( closeButton, QDialogButtonBox::RejectRole );
591     BUTTONACT( closeButton, close() );
592
593     layout->addWidget( group, 7, 0, 1, -1 );
594
595     // Fix layout
596     layout->setColumnStretch( 2, 1 );
597     layout->setRowStretch( 4, 1 );
598     setMinimumSize( 450, 350 );
599 }
600
601 static QPixmap *loadPixmapFromData( char *data, int size )
602 {
603     if( !data || size <= 0 )
604         return NULL;
605     QPixmap *pixmap = new QPixmap();
606     if( !pixmap->loadFromData( (const uchar*) data, (uint) size ) )
607     {
608         delete pixmap;
609         return NULL;
610     }
611     return pixmap;
612 }