1 /*****************************************************************************
2 * plugins.hpp : Plug-ins and extensions listing
3 ****************************************************************************
4 * Copyright (C) 2008-2010 the VideoLAN team
7 * Authors: Jean-Baptiste Kempf <jb (at) videolan.org>
8 * Jean-Philippe André <jpeg (at) videolan.org>
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.
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.
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 *****************************************************************************/
29 #include "plugins.hpp"
31 #include "util/searchlineedit.hpp"
32 #include "extensions_manager.hpp"
36 #include <vlc_modules.h>
38 #include <QTreeWidget>
39 #include <QStringList>
41 #include <QHeaderView>
42 #include <QDialogButtonBox>
45 #include <QVBoxLayout>
47 #include <QHBoxLayout>
48 #include <QVBoxLayout>
49 #include <QSpacerItem>
52 #include <QStyleOptionViewItem>
54 #include <QPushButton>
57 static QPixmap *loadPixmapFromData( char *, int size );
60 PluginDialog::PluginDialog( intf_thread_t *_p_intf ) : QVLCFrame( _p_intf )
62 setWindowTitle( qtr( "Plugins and extensions" ) );
63 setWindowRole( "vlc-plugins" );
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 ),
71 layout->addWidget( tabs );
73 QDialogButtonBox *box = new QDialogButtonBox;
74 QPushButton *okButton = new QPushButton( qtr( "&Close" ), this );
75 box->addButton( okButton, QDialogButtonBox::AcceptRole );
76 layout->addWidget( box );
77 BUTTONACT( okButton, close() );
78 readSettings( "PluginsDialog", QSize( 435, 280 ) );
81 PluginDialog::~PluginDialog()
83 writeSettings( "PluginsDialog" );
88 PluginTab::PluginTab( intf_thread_t *p_intf )
91 QGridLayout *layout = new QGridLayout( this );
93 /* Main Tree for modules */
94 treePlugins = new QTreeWidget;
95 layout->addWidget( treePlugins, 0, 0, 1, -1 );
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 );
104 QStringList headerNames;
105 headerNames << qtr("Name") << qtr("Capability" ) << qtr( "Score" );
106 treePlugins->setHeaderLabels( headerNames );
110 /* Set capability column to the correct Size*/
111 treePlugins->resizeColumnToContents( 1 );
112 treePlugins->header()->restoreState(
113 getSettings()->value( "Plugins/Header-State" ).toByteArray() );
115 treePlugins->setSortingEnabled( true );
116 treePlugins->sortByColumn( 1, Qt::AscendingOrder );
118 QLabel *label = new QLabel( qtr("&Search:"), this );
119 edit = new SearchLineEdit( this );
120 label->setBuddy( edit );
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& ) );
127 setMinimumSize( 500, 300 );
128 readSettings( "Plugins", QSize( 540, 400 ) );
131 inline void PluginTab::FillTree()
133 module_t **p_list = module_list_get( NULL );
136 for( unsigned int i = 0; (p_module = p_list[i] ) != NULL; i++ )
139 qs_item << qfu( module_get_name( p_module, true ) )
140 << qfu( module_get_capability( p_module ) )
141 << QString::number( module_get_score( p_module ) );
143 if( qs_item.at(1).isEmpty() ) continue;
146 QTreeWidgetItem *item = new PluginTreeItem( qs_item );
147 treePlugins->addTopLevelItem( item );
149 module_list_free( p_list );
152 void PluginTab::search( const QString& qs )
154 QList<QTreeWidgetItem *> items = treePlugins->findItems( qs, Qt::MatchContains );
155 items += treePlugins->findItems( qs, Qt::MatchContains, 1 );
157 QTreeWidgetItem *item = NULL;
158 for( int i = 0; i < treePlugins->topLevelItemCount(); i++ )
160 item = treePlugins->topLevelItem( i );
161 item->setHidden( !items.contains( item ) );
165 PluginTab::~PluginTab()
167 writeSettings( "Plugins" );
168 getSettings()->setValue( "Plugins/Header-State",
169 treePlugins->header()->saveState() );
172 bool PluginTreeItem::operator< ( const QTreeWidgetItem & other ) const
174 int col = treeWidget()->sortColumn();
176 return text( col ).toInt() < other.text( col ).toInt();
177 return text( col ) < other.text( col );
181 ExtensionTab::ExtensionTab( intf_thread_t *p_intf )
182 : QVLCFrame( p_intf )
185 QVBoxLayout *layout = new QVBoxLayout( this );
188 extList = new QListView( this );
189 CONNECT( extList, activated( const QModelIndex& ),
190 this, moreInformation() );
191 layout->addWidget( extList );
193 // List item delegate
194 ExtensionItemDelegate *itemDelegate = new ExtensionItemDelegate( p_intf,
196 extList->setItemDelegate( itemDelegate );
198 // Extension list look & feeling
199 extList->setAlternatingRowColors( true );
200 extList->setSelectionMode( QAbstractItemView::SingleSelection );
203 ExtensionListModel *model = new ExtensionListModel( extList, p_intf );
204 extList->setModel( model );
207 QHBoxLayout *hbox = new QHBoxLayout;
208 hbox->addItem( new QSpacerItem( 1, 1, QSizePolicy::Expanding,
209 QSizePolicy::Fixed ) );
211 // More information button
212 butMoreInfo = new QPushButton( QIcon( ":/menu/info" ),
213 qtr( "More information..." ),
215 CONNECT( butMoreInfo, clicked(),
216 this, moreInformation() );
217 hbox->addWidget( butMoreInfo );
220 ExtensionsManager *EM = ExtensionsManager::getInstance( p_intf );
221 QPushButton *reload = new QPushButton( QIcon( ":/update" ),
222 qtr( "Reload extensions" ),
224 CONNECT( reload, clicked(),
225 EM, reloadExtensions() );
226 hbox->addWidget( reload );
229 layout->addItem( hbox );
232 ExtensionTab::~ExtensionTab()
236 // Do not close on ESC or ENTER
237 void ExtensionTab::keyPressEvent( QKeyEvent *keyEvent )
242 // Show more information
243 void ExtensionTab::moreInformation()
245 if( !extList->selectionModel() ||
246 extList->selectionModel()->selectedIndexes().isEmpty() )
252 QModelIndex index = extList->selectionModel()->selectedIndexes().first();
253 ExtensionCopy *ext = (ExtensionCopy*) index.internalPointer();
257 ExtensionInfoDialog dlg( *ext, p_intf, this );
261 /* Safe copy of the extension_t struct */
265 ExtensionCopy( extension_t *p_ext )
267 name = qfu( p_ext->psz_name );
268 description = qfu( p_ext->psz_description );
269 shortdesc = qfu( p_ext->psz_shortdescription );
270 if( description.isEmpty() )
271 description = shortdesc;
272 if( shortdesc.isEmpty() && !description.isEmpty() )
273 shortdesc = description;
274 title = qfu( p_ext->psz_title );
275 author = qfu( p_ext->psz_author );
276 version = qfu( p_ext->psz_version );
277 url = qfu( p_ext->psz_url );
278 icon = loadPixmapFromData( p_ext->p_icondata, p_ext->i_icondata_size );
282 QString name, title, description, shortdesc, author, version, url;
286 /* Extensions list model for the QListView */
288 ExtensionListModel::ExtensionListModel( QListView *view, intf_thread_t *intf )
289 : QAbstractListModel( view ), p_intf( intf )
291 // Connect to ExtensionsManager::extensionsUpdated()
292 ExtensionsManager* EM = ExtensionsManager::getInstance( p_intf );
293 CONNECT( EM, extensionsUpdated(), this, updateList() );
295 // Load extensions now if not already loaded
296 EM->loadExtensions();
299 ExtensionListModel::~ExtensionListModel()
301 // Clear extensions list
302 while( !extensions.isEmpty() )
303 delete extensions.takeLast();
306 void ExtensionListModel::updateList()
310 // Clear extensions list
311 while( !extensions.isEmpty() )
313 ext = extensions.takeLast();
317 // Find new extensions
318 ExtensionsManager *EM = ExtensionsManager::getInstance( p_intf );
319 extensions_manager_t *p_mgr = EM->getManager();
323 vlc_mutex_lock( &p_mgr->lock );
325 FOREACH_ARRAY( p_ext, p_mgr->extensions )
327 ext = new ExtensionCopy( p_ext );
328 extensions.push_back( ext );
331 vlc_mutex_unlock( &p_mgr->lock );
332 vlc_object_release( p_mgr );
334 emit dataChanged( index( 0 ), index( rowCount() - 1 ) );
337 int ExtensionListModel::rowCount( const QModelIndex& ) const
340 ExtensionsManager *EM = ExtensionsManager::getInstance( p_intf );
341 extensions_manager_t *p_mgr = EM->getManager();
345 vlc_mutex_lock( &p_mgr->lock );
346 count = p_mgr->extensions.i_size;
347 vlc_mutex_unlock( &p_mgr->lock );
348 vlc_object_release( p_mgr );
353 QVariant ExtensionListModel::data( const QModelIndex& index, int role ) const
355 if( !index.isValid() )
365 QModelIndex ExtensionListModel::index( int row, int column,
366 const QModelIndex& ) const
369 return QModelIndex();
370 if( row < 0 || row >= extensions.size() )
371 return QModelIndex();
373 return createIndex( row, 0, extensions.at( row ) );
377 /* Extension List Widget Item */
378 ExtensionItemDelegate::ExtensionItemDelegate( intf_thread_t *p_intf,
380 : QStyledItemDelegate( view ), view( view ), p_intf( p_intf )
384 ExtensionItemDelegate::~ExtensionItemDelegate()
388 void ExtensionItemDelegate::paint( QPainter *painter,
389 const QStyleOptionViewItem &option,
390 const QModelIndex &index ) const
392 ExtensionCopy *ext = ( ExtensionCopy* ) index.internalPointer();
393 assert( ext != NULL );
395 int width = option.rect.width();
397 // Pixmap: buffer where to draw
398 QPixmap pix(option.rect.size());
401 pix.fill( Qt::transparent ); // FIXME
403 // ItemView primitive style
404 QApplication::style()->drawPrimitive( QStyle::PE_PanelItemViewItem,
408 // Painter on the pixmap
409 QPainter *pixpaint = new QPainter(&pix);
412 QFont font = painter->font();
413 QPen pen = painter->pen();
414 if( view->selectionModel()->selectedIndexes().contains( index ) )
416 pen.setBrush( option.palette.highlightedText() );
420 pen.setBrush( option.palette.text() );
422 pixpaint->setPen( pen );
423 QFontMetrics metrics = option.fontMetrics;
426 if( ext->icon != NULL )
428 pixpaint->drawPixmap( 7, 7, 2*metrics.height(), 2*metrics.height(),
433 pixpaint->setRenderHint( QPainter::TextAntialiasing );
434 font.setBold( true );
435 pixpaint->setFont( font );
436 pixpaint->drawText( QRect( 17 + 2 * metrics.height(), 7,
437 width - 40 - 2 * metrics.height(),
439 Qt::AlignLeft, ext->title );
441 // Short description: normal
442 font.setBold( false );
443 pixpaint->setFont( font );
444 pixpaint->drawText( QRect( 17 + 2 * metrics.height(),
445 7 + metrics.height(), width - 40,
447 Qt::AlignLeft, ext->shortdesc );
449 // Flush paint operations
452 // Draw it on the screen!
453 painter->drawPixmap( option.rect, pix );
456 QSize ExtensionItemDelegate::sizeHint( const QStyleOptionViewItem &option,
457 const QModelIndex &index ) const
459 if (index.isValid() && index.column() == 0)
461 QFontMetrics metrics = option.fontMetrics;
462 return QSize( 200, 14 + 2 * metrics.height() );
468 /* "More information" dialog */
470 ExtensionInfoDialog::ExtensionInfoDialog( const ExtensionCopy& extension,
471 intf_thread_t *p_intf,
473 : QVLCDialog( parent, p_intf ),
474 extension( new ExtensionCopy( extension ) )
476 // Let's be a modal dialog
477 setWindowModality( Qt::WindowModal );
480 setWindowTitle( qtr( "About" ) + " " + extension.title );
483 QGridLayout *layout = new QGridLayout( this );
486 QLabel *icon = new QLabel( this );
487 if( !extension.icon )
489 QPixmap pix( ":/logo/vlc48.png" );
490 icon->setPixmap( pix );
494 icon->setPixmap( *extension.icon );
496 icon->setAlignment( Qt::AlignCenter );
497 icon->setFixedSize( 48, 48 );
498 layout->addWidget( icon, 1, 0, 2, 1 );
501 QLabel *label = new QLabel( extension.title, this );
502 QFont font = label->font();
503 font.setBold( true );
504 font.setPointSizeF( font.pointSizeF() * 1.3f );
505 label->setFont( font );
506 layout->addWidget( label, 0, 0, 1, -1 );
509 label = new QLabel( "<b>" + qtr( "Version" ) + ":</b>", this );
510 layout->addWidget( label, 1, 1, 1, 1, Qt::AlignBottom );
511 label = new QLabel( extension.version, this );
512 layout->addWidget( label, 1, 2, 1, 2, Qt::AlignBottom );
515 label = new QLabel( "<b>" + qtr( "Author" ) + ":</b>", this );
516 layout->addWidget( label, 2, 1, 1, 1, Qt::AlignTop );
517 label = new QLabel( extension.author, this );
518 layout->addWidget( label, 2, 2, 1, 2, Qt::AlignTop );
522 label = new QLabel( this );
523 label->setText( extension.description );
524 label->setWordWrap( true );
525 label->setOpenExternalLinks( true );
526 layout->addWidget( label, 4, 0, 1, -1 );
529 label = new QLabel( "<b>" + qtr( "Website" ) + ":</b>", this );
530 layout->addWidget( label, 5, 0, 1, 2 );
531 QString txt = "<a href=\"";
532 txt += extension.url;
534 txt += extension.url;
536 label = new QLabel( txt, this );
537 label->setText( txt );
538 label->setOpenExternalLinks( true );
539 layout->addWidget( label, 5, 2, 1, -1 );
542 label = new QLabel( "<b>" + qtr( "File" ) + ":</b>", this );
543 layout->addWidget( label, 6, 0, 1, 2 );
544 QLineEdit *line = new QLineEdit( extension.name, this );
545 layout->addWidget( line, 6, 2, 1, -1 );
548 QDialogButtonBox *group = new QDialogButtonBox( this );
549 QPushButton *closeButton = new QPushButton( qtr( "&Close" ) );
550 group->addButton( closeButton, QDialogButtonBox::AcceptRole );
551 BUTTONACT( closeButton, close() );
553 layout->addWidget( group, 7, 0, 1, -1 );
556 layout->setColumnStretch( 2, 1 );
557 layout->setRowStretch( 4, 1 );
558 setMinimumSize( 450, 350 );
561 ExtensionInfoDialog::~ExtensionInfoDialog()
566 static QPixmap *loadPixmapFromData( char *data, int size )
568 if( !data || size <= 0 )
570 QPixmap *pixmap = new QPixmap();
571 if( !pixmap->loadFromData( (const uchar*) data, (uint) size ) )