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 void PluginTab::keyPressEvent( QKeyEvent *keyEvent )
174 if( keyEvent->key() == Qt::Key_Return ||
175 keyEvent->key() == Qt::Key_Enter )
181 bool PluginTreeItem::operator< ( const QTreeWidgetItem & other ) const
183 int col = treeWidget()->sortColumn();
185 return text( col ).toInt() < other.text( col ).toInt();
186 return text( col ) < other.text( col );
190 ExtensionTab::ExtensionTab( intf_thread_t *p_intf )
191 : QVLCFrame( p_intf )
194 QVBoxLayout *layout = new QVBoxLayout( this );
197 extList = new QListView( this );
198 CONNECT( extList, activated( const QModelIndex& ),
199 this, moreInformation() );
200 layout->addWidget( extList );
202 // List item delegate
203 ExtensionItemDelegate *itemDelegate = new ExtensionItemDelegate( p_intf,
205 extList->setItemDelegate( itemDelegate );
207 // Extension list look & feeling
208 extList->setAlternatingRowColors( true );
209 extList->setSelectionMode( QAbstractItemView::SingleSelection );
212 ExtensionListModel *model = new ExtensionListModel( extList, p_intf );
213 extList->setModel( model );
216 QHBoxLayout *hbox = new QHBoxLayout;
217 hbox->addItem( new QSpacerItem( 1, 1, QSizePolicy::Expanding,
218 QSizePolicy::Fixed ) );
220 // More information button
221 butMoreInfo = new QPushButton( QIcon( ":/menu/info" ),
222 qtr( "More information..." ),
224 CONNECT( butMoreInfo, clicked(),
225 this, moreInformation() );
226 hbox->addWidget( butMoreInfo );
229 ExtensionsManager *EM = ExtensionsManager::getInstance( p_intf );
230 QPushButton *reload = new QPushButton( QIcon( ":/update" ),
231 qtr( "Reload extensions" ),
233 CONNECT( reload, clicked(),
234 EM, reloadExtensions() );
235 hbox->addWidget( reload );
238 layout->addItem( hbox );
241 ExtensionTab::~ExtensionTab()
245 // Do not close on ESC or ENTER
246 void ExtensionTab::keyPressEvent( QKeyEvent *keyEvent )
248 if( keyEvent->key() == Qt::Key_Return ||
249 keyEvent->key() == Qt::Key_Enter )
255 // Show more information
256 void ExtensionTab::moreInformation()
258 if( !extList->selectionModel() ||
259 extList->selectionModel()->selectedIndexes().isEmpty() )
265 QModelIndex index = extList->selectionModel()->selectedIndexes().first();
266 ExtensionCopy *ext = (ExtensionCopy*) index.internalPointer();
270 ExtensionInfoDialog dlg( *ext, p_intf, this );
274 /* Safe copy of the extension_t struct */
278 ExtensionCopy( extension_t *p_ext )
280 name = qfu( p_ext->psz_name );
281 description = qfu( p_ext->psz_description );
282 shortdesc = qfu( p_ext->psz_shortdescription );
283 if( description.isEmpty() )
284 description = shortdesc;
285 if( shortdesc.isEmpty() && !description.isEmpty() )
286 shortdesc = description;
287 title = qfu( p_ext->psz_title );
288 author = qfu( p_ext->psz_author );
289 version = qfu( p_ext->psz_version );
290 url = qfu( p_ext->psz_url );
291 icon = loadPixmapFromData( p_ext->p_icondata, p_ext->i_icondata_size );
295 QString name, title, description, shortdesc, author, version, url;
299 /* Extensions list model for the QListView */
301 ExtensionListModel::ExtensionListModel( QListView *view, intf_thread_t *intf )
302 : QAbstractListModel( view ), p_intf( intf )
304 // Connect to ExtensionsManager::extensionsUpdated()
305 ExtensionsManager* EM = ExtensionsManager::getInstance( p_intf );
306 CONNECT( EM, extensionsUpdated(), this, updateList() );
308 // Load extensions now if not already loaded
309 EM->loadExtensions();
312 ExtensionListModel::~ExtensionListModel()
314 // Clear extensions list
315 while( !extensions.isEmpty() )
316 delete extensions.takeLast();
319 void ExtensionListModel::updateList()
323 // Clear extensions list
324 while( !extensions.isEmpty() )
326 ext = extensions.takeLast();
330 // Find new extensions
331 ExtensionsManager *EM = ExtensionsManager::getInstance( p_intf );
332 extensions_manager_t *p_mgr = EM->getManager();
336 vlc_mutex_lock( &p_mgr->lock );
338 FOREACH_ARRAY( p_ext, p_mgr->extensions )
340 ext = new ExtensionCopy( p_ext );
341 extensions.append( ext );
344 vlc_mutex_unlock( &p_mgr->lock );
345 vlc_object_release( p_mgr );
347 emit dataChanged( index( 0 ), index( rowCount() - 1 ) );
350 int ExtensionListModel::rowCount( const QModelIndex& ) const
353 ExtensionsManager *EM = ExtensionsManager::getInstance( p_intf );
354 extensions_manager_t *p_mgr = EM->getManager();
358 vlc_mutex_lock( &p_mgr->lock );
359 count = p_mgr->extensions.i_size;
360 vlc_mutex_unlock( &p_mgr->lock );
361 vlc_object_release( p_mgr );
366 QVariant ExtensionListModel::data( const QModelIndex& index, int role ) const
368 if( !index.isValid() )
378 QModelIndex ExtensionListModel::index( int row, int column,
379 const QModelIndex& ) const
382 return QModelIndex();
383 if( row < 0 || row >= extensions.count() )
384 return QModelIndex();
386 return createIndex( row, 0, extensions.at( row ) );
390 /* Extension List Widget Item */
391 ExtensionItemDelegate::ExtensionItemDelegate( intf_thread_t *p_intf,
393 : QStyledItemDelegate( view ), view( view ), p_intf( p_intf )
397 ExtensionItemDelegate::~ExtensionItemDelegate()
401 void ExtensionItemDelegate::paint( QPainter *painter,
402 const QStyleOptionViewItem &option,
403 const QModelIndex &index ) const
405 ExtensionCopy *ext = ( ExtensionCopy* ) index.internalPointer();
406 assert( ext != NULL );
408 int width = option.rect.width();
410 // Pixmap: buffer where to draw
411 QPixmap pix(option.rect.size());
414 pix.fill( Qt::transparent ); // FIXME
416 // ItemView primitive style
417 QApplication::style()->drawPrimitive( QStyle::PE_PanelItemViewItem,
421 // Painter on the pixmap
422 QPainter *pixpaint = new QPainter(&pix);
425 QFont font = painter->font();
426 QPen pen = painter->pen();
427 if( view->selectionModel()->selectedIndexes().contains( index ) )
429 pen.setBrush( option.palette.highlightedText() );
433 pen.setBrush( option.palette.text() );
435 pixpaint->setPen( pen );
436 QFontMetrics metrics = option.fontMetrics;
439 if( ext->icon != NULL )
441 pixpaint->drawPixmap( 7, 7, 2*metrics.height(), 2*metrics.height(),
446 pixpaint->setRenderHint( QPainter::TextAntialiasing );
447 font.setBold( true );
448 pixpaint->setFont( font );
449 pixpaint->drawText( QRect( 17 + 2 * metrics.height(), 7,
450 width - 40 - 2 * metrics.height(),
452 Qt::AlignLeft, ext->title );
454 // Short description: normal
455 font.setBold( false );
456 pixpaint->setFont( font );
457 pixpaint->drawText( QRect( 17 + 2 * metrics.height(),
458 7 + metrics.height(), width - 40,
460 Qt::AlignLeft, ext->shortdesc );
462 // Flush paint operations
465 // Draw it on the screen!
466 painter->drawPixmap( option.rect, pix );
469 QSize ExtensionItemDelegate::sizeHint( const QStyleOptionViewItem &option,
470 const QModelIndex &index ) const
472 if (index.isValid() && index.column() == 0)
474 QFontMetrics metrics = option.fontMetrics;
475 return QSize( 200, 14 + 2 * metrics.height() );
481 /* "More information" dialog */
483 ExtensionInfoDialog::ExtensionInfoDialog( const ExtensionCopy& extension,
484 intf_thread_t *p_intf,
486 : QVLCDialog( parent, p_intf ),
487 extension( new ExtensionCopy( extension ) )
489 // Let's be a modal dialog
490 setWindowModality( Qt::WindowModal );
493 setWindowTitle( qtr( "About" ) + " " + extension.title );
496 QGridLayout *layout = new QGridLayout( this );
499 QLabel *icon = new QLabel( this );
500 if( !extension.icon )
502 QPixmap pix( ":/logo/vlc48.png" );
503 icon->setPixmap( pix );
507 icon->setPixmap( *extension.icon );
509 icon->setAlignment( Qt::AlignCenter );
510 icon->setFixedSize( 48, 48 );
511 layout->addWidget( icon, 1, 0, 2, 1 );
514 QLabel *label = new QLabel( extension.title, this );
515 QFont font = label->font();
516 font.setBold( true );
517 font.setPointSizeF( font.pointSizeF() * 1.3f );
518 label->setFont( font );
519 layout->addWidget( label, 0, 0, 1, -1 );
522 label = new QLabel( "<b>" + qtr( "Version" ) + ":</b>", this );
523 layout->addWidget( label, 1, 1, 1, 1, Qt::AlignBottom );
524 label = new QLabel( extension.version, this );
525 layout->addWidget( label, 1, 2, 1, 2, Qt::AlignBottom );
528 label = new QLabel( "<b>" + qtr( "Author" ) + ":</b>", this );
529 layout->addWidget( label, 2, 1, 1, 1, Qt::AlignTop );
530 label = new QLabel( extension.author, this );
531 layout->addWidget( label, 2, 2, 1, 2, Qt::AlignTop );
535 label = new QLabel( this );
536 label->setText( extension.description );
537 label->setWordWrap( true );
538 label->setOpenExternalLinks( true );
539 layout->addWidget( label, 4, 0, 1, -1 );
542 label = new QLabel( "<b>" + qtr( "Website" ) + ":</b>", this );
543 layout->addWidget( label, 5, 0, 1, 2 );
544 QString txt = "<a href=\"";
545 txt += extension.url;
547 txt += extension.url;
549 label = new QLabel( txt, this );
550 label->setText( txt );
551 label->setOpenExternalLinks( true );
552 layout->addWidget( label, 5, 2, 1, -1 );
555 label = new QLabel( "<b>" + qtr( "File" ) + ":</b>", this );
556 layout->addWidget( label, 6, 0, 1, 2 );
557 QLineEdit *line = new QLineEdit( extension.name, this );
558 layout->addWidget( line, 6, 2, 1, -1 );
561 QDialogButtonBox *group = new QDialogButtonBox( this );
562 QPushButton *closeButton = new QPushButton( qtr( "&Close" ) );
563 group->addButton( closeButton, QDialogButtonBox::AcceptRole );
564 BUTTONACT( closeButton, close() );
566 layout->addWidget( group, 7, 0, 1, -1 );
569 layout->setColumnStretch( 2, 1 );
570 layout->setRowStretch( 4, 1 );
571 setMinimumSize( 450, 350 );
574 ExtensionInfoDialog::~ExtensionInfoDialog()
579 static QPixmap *loadPixmapFromData( char *data, int size )
581 if( !data || size <= 0 )
583 QPixmap *pixmap = new QPixmap();
584 if( !pixmap->loadFromData( (const uchar*) data, (uint) size ) )