1 /*****************************************************************************
2 * plugins.cpp : 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::RejectRole );
76 layout->addWidget( box );
77 BUTTONACT( okButton, close() );
78 restoreWidgetPosition( "PluginsDialog", QSize( 435, 280 ) );
81 PluginDialog::~PluginDialog()
83 saveWidgetPosition( "PluginsDialog" );
88 PluginTab::PluginTab( intf_thread_t *p_intf_ )
89 : QVLCFrame( 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 restoreWidgetPosition( "Plugins", QSize( 540, 400 ) );
131 inline void PluginTab::FillTree()
134 module_t **p_list = module_list_get( &count );
136 for( unsigned int i = 0; i < count; i++ )
138 module_t *p_module = p_list[i];
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 ) );
145 if( qs_item.at(1).isEmpty() ) continue;
148 QTreeWidgetItem *item = new PluginTreeItem( qs_item );
149 treePlugins->addTopLevelItem( item );
151 module_list_free( p_list );
154 void PluginTab::search( const QString& qs )
156 QList<QTreeWidgetItem *> items = treePlugins->findItems( qs, Qt::MatchContains );
157 items += treePlugins->findItems( qs, Qt::MatchContains, 1 );
159 QTreeWidgetItem *item = NULL;
160 for( int i = 0; i < treePlugins->topLevelItemCount(); i++ )
162 item = treePlugins->topLevelItem( i );
163 item->setHidden( !items.contains( item ) );
167 PluginTab::~PluginTab()
169 saveWidgetPosition( "Plugins" );
170 getSettings()->setValue( "Plugins/Header-State",
171 treePlugins->header()->saveState() );
174 void PluginTab::keyPressEvent( QKeyEvent *keyEvent )
176 if( keyEvent->key() == Qt::Key_Return ||
177 keyEvent->key() == Qt::Key_Enter )
183 bool PluginTreeItem::operator< ( const QTreeWidgetItem & other ) const
185 int col = treeWidget()->sortColumn();
187 return text( col ).toInt() < other.text( col ).toInt();
188 return text( col ) < other.text( col );
192 ExtensionTab::ExtensionTab( intf_thread_t *p_intf_ )
193 : QVLCFrame( p_intf_ )
196 QVBoxLayout *layout = new QVBoxLayout( this );
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 );
205 extList = new QListView( this );
206 CONNECT( extList, activated( const QModelIndex& ),
207 this, moreInformation() );
208 layout->addWidget( extList );
210 // List item delegate
211 ExtensionItemDelegate *itemDelegate = new ExtensionItemDelegate( p_intf,
213 extList->setItemDelegate( itemDelegate );
215 // Extension list look & feeling
216 extList->setAlternatingRowColors( true );
217 extList->setSelectionMode( QAbstractItemView::SingleSelection );
220 ExtensionListModel *model = new ExtensionListModel( extList, p_intf );
221 extList->setModel( model );
224 QHBoxLayout *hbox = new QHBoxLayout;
225 hbox->addItem( new QSpacerItem( 1, 1, QSizePolicy::Expanding,
226 QSizePolicy::Fixed ) );
228 // More information button
229 butMoreInfo = new QPushButton( QIcon( ":/menu/info" ),
230 qtr( "More information..." ),
232 CONNECT( butMoreInfo, clicked(), this, moreInformation() );
233 hbox->addWidget( butMoreInfo );
236 ExtensionsManager *EM = ExtensionsManager::getInstance( p_intf );
237 QPushButton *reload = new QPushButton( QIcon( ":/update" ),
238 qtr( "Reload extensions" ),
240 CONNECT( reload, clicked(), EM, reloadExtensions() );
241 CONNECT( reload, clicked(), this, updateButtons() );
242 CONNECT( extList->selectionModel(),
243 selectionChanged( const QItemSelection &, const QItemSelection & ),
246 hbox->addWidget( reload );
249 layout->addItem( hbox );
253 ExtensionTab::~ExtensionTab()
257 void ExtensionTab::updateButtons()
259 butMoreInfo->setEnabled( extList->selectionModel()->hasSelection() );
262 // Do not close on ESC or ENTER
263 void ExtensionTab::keyPressEvent( QKeyEvent *keyEvent )
265 if( keyEvent->key() == Qt::Key_Return ||
266 keyEvent->key() == Qt::Key_Enter )
272 // Show more information
273 void ExtensionTab::moreInformation()
275 QModelIndex index = extList->selectionModel()->selectedIndexes().first();
276 ExtensionCopy *ext = (ExtensionCopy*) index.internalPointer();
280 ExtensionInfoDialog dlg( *ext, p_intf, this );
284 /* Safe copy of the extension_t struct */
288 ExtensionCopy( extension_t *p_ext )
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 );
305 QString name, title, description, shortdesc, author, version, url;
309 /* Extensions list model for the QListView */
311 ExtensionListModel::ExtensionListModel( QListView *view, intf_thread_t *intf )
312 : QAbstractListModel( view ), p_intf( intf )
314 // Connect to ExtensionsManager::extensionsUpdated()
315 ExtensionsManager* EM = ExtensionsManager::getInstance( p_intf );
316 CONNECT( EM, extensionsUpdated(), this, updateList() );
318 // Load extensions now if not already loaded
319 EM->loadExtensions();
322 ExtensionListModel::~ExtensionListModel()
324 // Clear extensions list
325 while( !extensions.isEmpty() )
326 delete extensions.takeLast();
329 void ExtensionListModel::updateList()
333 // Clear extensions list
334 while( !extensions.isEmpty() )
336 ext = extensions.takeLast();
340 // Find new extensions
341 ExtensionsManager *EM = ExtensionsManager::getInstance( p_intf );
342 extensions_manager_t *p_mgr = EM->getManager();
346 vlc_mutex_lock( &p_mgr->lock );
348 FOREACH_ARRAY( p_ext, p_mgr->extensions )
350 ext = new ExtensionCopy( p_ext );
351 extensions.append( ext );
354 vlc_mutex_unlock( &p_mgr->lock );
355 vlc_object_release( p_mgr );
357 emit dataChanged( index( 0 ), index( rowCount() - 1 ) );
360 int ExtensionListModel::rowCount( const QModelIndex& ) const
363 ExtensionsManager *EM = ExtensionsManager::getInstance( p_intf );
364 extensions_manager_t *p_mgr = EM->getManager();
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 );
376 QVariant ExtensionListModel::data( const QModelIndex& index, int role ) const
378 if( !index.isValid() )
388 QModelIndex ExtensionListModel::index( int row, int column,
389 const QModelIndex& ) const
392 return QModelIndex();
393 if( row < 0 || row >= extensions.count() )
394 return QModelIndex();
396 return createIndex( row, 0, extensions.at( row ) );
400 /* Extension List Widget Item */
401 ExtensionItemDelegate::ExtensionItemDelegate( intf_thread_t *p_intf,
403 : QStyledItemDelegate( view ), view( view ), p_intf( p_intf )
407 ExtensionItemDelegate::~ExtensionItemDelegate()
411 void ExtensionItemDelegate::paint( QPainter *painter,
412 const QStyleOptionViewItem &option,
413 const QModelIndex &index ) const
415 ExtensionCopy *ext = ( ExtensionCopy* ) index.internalPointer();
416 assert( ext != NULL );
418 int width = option.rect.width();
420 // Pixmap: buffer where to draw
421 QPixmap pix(option.rect.size());
424 pix.fill( Qt::transparent ); // FIXME
426 // ItemView primitive style
427 QApplication::style()->drawPrimitive( QStyle::PE_PanelItemViewItem,
431 // Painter on the pixmap
432 QPainter *pixpaint = new QPainter(&pix);
435 QFont font = painter->font();
436 QPen pen = painter->pen();
437 if( view->selectionModel()->selectedIndexes().contains( index ) )
439 pen.setBrush( option.palette.highlightedText() );
443 pen.setBrush( option.palette.text() );
445 pixpaint->setPen( pen );
446 QFontMetrics metrics = option.fontMetrics;
449 if( ext->icon != NULL )
451 pixpaint->drawPixmap( 7, 7, 2*metrics.height(), 2*metrics.height(),
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(),
462 Qt::AlignLeft, ext->title );
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,
470 Qt::AlignLeft, ext->shortdesc );
472 // Flush paint operations
475 // Draw it on the screen!
476 painter->drawPixmap( option.rect, pix );
479 QSize ExtensionItemDelegate::sizeHint( const QStyleOptionViewItem &option,
480 const QModelIndex &index ) const
482 if (index.isValid() && index.column() == 0)
484 QFontMetrics metrics = option.fontMetrics;
485 return QSize( 200, 14 + 2 * metrics.height() );
491 /* "More information" dialog */
493 ExtensionInfoDialog::ExtensionInfoDialog( const ExtensionCopy& extension,
494 intf_thread_t *p_intf,
496 : QVLCDialog( parent, p_intf )
498 // Let's be a modal dialog
499 setWindowModality( Qt::WindowModal );
502 setWindowTitle( qtr( "About" ) + " " + extension.title );
505 QGridLayout *layout = new QGridLayout( this );
508 QLabel *icon = new QLabel( this );
509 if( !extension.icon )
511 QPixmap pix( ":/logo/vlc48.png" );
512 icon->setPixmap( pix );
516 icon->setPixmap( *extension.icon );
518 icon->setAlignment( Qt::AlignCenter );
519 icon->setFixedSize( 48, 48 );
520 layout->addWidget( icon, 1, 0, 2, 1 );
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 );
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 );
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 );
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 );
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 )
556 label->setOpenExternalLinks( true );
557 layout->addWidget( label, 5, 2, 1, -1 );
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 );
567 QDialogButtonBox *group = new QDialogButtonBox( this );
568 QPushButton *closeButton = new QPushButton( qtr( "&Close" ) );
569 group->addButton( closeButton, QDialogButtonBox::RejectRole );
570 BUTTONACT( closeButton, close() );
572 layout->addWidget( group, 7, 0, 1, -1 );
575 layout->setColumnStretch( 2, 1 );
576 layout->setRowStretch( 4, 1 );
577 setMinimumSize( 450, 350 );
580 static QPixmap *loadPixmapFromData( char *data, int size )
582 if( !data || size <= 0 )
584 QPixmap *pixmap = new QPixmap();
585 if( !pixmap->loadFromData( (const uchar*) data, (uint) size ) )