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 #if QT_VERSION >= 0x050000
99 treePlugins->header()->setSectionsMovable( false );
101 treePlugins->header()->setMovable( false );
103 treePlugins->header()->setSortIndicatorShown( true );
104 // treePlugins->header()->setResizeMode( QHeaderView::ResizeToContents );
105 treePlugins->setAlternatingRowColors( true );
106 treePlugins->setColumnWidth( 0, 200 );
108 QStringList headerNames;
109 headerNames << qtr("Name") << qtr("Capability" ) << qtr( "Score" );
110 treePlugins->setHeaderLabels( headerNames );
114 /* Set capability column to the correct Size*/
115 treePlugins->resizeColumnToContents( 1 );
116 treePlugins->header()->restoreState(
117 getSettings()->value( "Plugins/Header-State" ).toByteArray() );
119 treePlugins->setSortingEnabled( true );
120 treePlugins->sortByColumn( 1, Qt::AscendingOrder );
122 QLabel *label = new QLabel( qtr("&Search:"), this );
123 edit = new SearchLineEdit( this );
124 label->setBuddy( edit );
126 layout->addWidget( label, 1, 0 );
127 layout->addWidget( edit, 1, 1, 1, 1 );
128 CONNECT( edit, textChanged( const QString& ),
129 this, search( const QString& ) );
131 setMinimumSize( 500, 300 );
132 restoreWidgetPosition( "Plugins", QSize( 540, 400 ) );
135 inline void PluginTab::FillTree()
138 module_t **p_list = module_list_get( &count );
140 for( unsigned int i = 0; i < count; i++ )
142 module_t *p_module = p_list[i];
145 qs_item << qfu( module_get_name( p_module, true ) )
146 << qfu( module_get_capability( p_module ) )
147 << QString::number( module_get_score( p_module ) );
149 if( qs_item.at(1).isEmpty() ) continue;
152 QTreeWidgetItem *item = new PluginTreeItem( qs_item );
153 treePlugins->addTopLevelItem( item );
155 module_list_free( p_list );
158 void PluginTab::search( const QString& qs )
160 QList<QTreeWidgetItem *> items = treePlugins->findItems( qs, Qt::MatchContains );
161 items += treePlugins->findItems( qs, Qt::MatchContains, 1 );
163 QTreeWidgetItem *item = NULL;
164 for( int i = 0; i < treePlugins->topLevelItemCount(); i++ )
166 item = treePlugins->topLevelItem( i );
167 item->setHidden( !items.contains( item ) );
171 PluginTab::~PluginTab()
173 saveWidgetPosition( "Plugins" );
174 getSettings()->setValue( "Plugins/Header-State",
175 treePlugins->header()->saveState() );
178 void PluginTab::keyPressEvent( QKeyEvent *keyEvent )
180 if( keyEvent->key() == Qt::Key_Return ||
181 keyEvent->key() == Qt::Key_Enter )
187 bool PluginTreeItem::operator< ( const QTreeWidgetItem & other ) const
189 int col = treeWidget()->sortColumn();
190 if( col == PluginTab::SCORE )
191 return text( col ).toInt() < other.text( col ).toInt();
192 else if ( col == PluginTab::CAPABILITY )
194 if ( text( PluginTab::CAPABILITY ) == other.text( PluginTab::CAPABILITY ) )
195 return text( PluginTab::NAME ) < other.text( PluginTab::NAME );
197 return text( PluginTab::CAPABILITY ) < other.text( PluginTab::CAPABILITY );
199 return text( col ) < other.text( col );
203 ExtensionTab::ExtensionTab( intf_thread_t *p_intf_ )
204 : QVLCFrame( p_intf_ )
207 QVBoxLayout *layout = new QVBoxLayout( this );
209 QLabel *notice = new QLabel( qtr("Get more extensions from")
210 + QString( " <a href=\"http://addons.videolan.org/\">"
211 "addons.videolan.org</a>." ) );
212 notice->setOpenExternalLinks( true );
213 layout->addWidget( notice );
216 extList = new QListView( this );
217 CONNECT( extList, activated( const QModelIndex& ),
218 this, moreInformation() );
219 layout->addWidget( extList );
221 // List item delegate
222 ExtensionItemDelegate *itemDelegate = new ExtensionItemDelegate( extList );
223 extList->setItemDelegate( itemDelegate );
225 // Extension list look & feeling
226 extList->setAlternatingRowColors( true );
227 extList->setSelectionMode( QAbstractItemView::SingleSelection );
230 ExtensionListModel *model = new ExtensionListModel( extList, p_intf );
231 extList->setModel( model );
234 QDialogButtonBox *buttonsBox = new QDialogButtonBox;
236 // More information button
237 butMoreInfo = new QPushButton( QIcon( ":/menu/info" ),
238 qtr( "More information..." ),
240 CONNECT( butMoreInfo, clicked(), this, moreInformation() );
241 buttonsBox->addButton( butMoreInfo, QDialogButtonBox::ActionRole );
244 ExtensionsManager *EM = ExtensionsManager::getInstance( p_intf );
245 QPushButton *reload = new QPushButton( QIcon( ":/update" ),
246 qtr( "Reload extensions" ),
248 CONNECT( reload, clicked(), EM, reloadExtensions() );
249 CONNECT( reload, clicked(), this, updateButtons() );
250 CONNECT( extList->selectionModel(),
251 selectionChanged( const QItemSelection &, const QItemSelection & ),
254 buttonsBox->addButton( reload, QDialogButtonBox::ResetRole );
256 layout->addWidget( buttonsBox );
260 ExtensionTab::~ExtensionTab()
264 void ExtensionTab::updateButtons()
266 butMoreInfo->setEnabled( extList->selectionModel()->hasSelection() );
269 // Do not close on ESC or ENTER
270 void ExtensionTab::keyPressEvent( QKeyEvent *keyEvent )
272 if( keyEvent->key() == Qt::Key_Return ||
273 keyEvent->key() == Qt::Key_Enter )
279 // Show more information
280 void ExtensionTab::moreInformation()
282 QModelIndex index = extList->selectionModel()->selectedIndexes().first();
284 if( !index.isValid() )
287 ExtensionInfoDialog dlg( index, p_intf, this );
291 /* Safe copy of the extension_t struct */
292 ExtensionListModel::ExtensionCopy::ExtensionCopy( extension_t *p_ext )
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 );
308 ExtensionListModel::ExtensionCopy::~ExtensionCopy()
313 QVariant ExtensionListModel::ExtensionCopy::data( int role ) const
317 case Qt::DisplayRole:
319 case Qt::DecorationRole:
320 if ( !icon ) return QPixmap( ":/logo/vlc48.png" );
322 case DescriptionRole:
337 /* Extensions list model for the QListView */
339 ExtensionListModel::ExtensionListModel( QObject *parent, intf_thread_t *intf )
340 : QAbstractListModel( parent ), p_intf( intf )
342 // Connect to ExtensionsManager::extensionsUpdated()
343 ExtensionsManager* EM = ExtensionsManager::getInstance( p_intf );
344 CONNECT( EM, extensionsUpdated(), this, updateList() );
346 // Load extensions now if not already loaded
347 EM->loadExtensions();
350 ExtensionListModel::~ExtensionListModel()
352 // Clear extensions list
353 while( !extensions.isEmpty() )
354 delete extensions.takeLast();
357 void ExtensionListModel::updateList()
361 // Clear extensions list
362 while( !extensions.isEmpty() )
364 ext = extensions.takeLast();
368 // Find new extensions
369 ExtensionsManager *EM = ExtensionsManager::getInstance( p_intf );
370 extensions_manager_t *p_mgr = EM->getManager();
374 vlc_mutex_lock( &p_mgr->lock );
376 FOREACH_ARRAY( p_ext, p_mgr->extensions )
378 ext = new ExtensionCopy( p_ext );
379 extensions.append( ext );
382 vlc_mutex_unlock( &p_mgr->lock );
383 vlc_object_release( p_mgr );
385 emit dataChanged( index( 0 ), index( rowCount() - 1 ) );
388 int ExtensionListModel::rowCount( const QModelIndex& ) const
391 ExtensionsManager *EM = ExtensionsManager::getInstance( p_intf );
392 extensions_manager_t *p_mgr = EM->getManager();
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 );
404 QVariant ExtensionListModel::data( const QModelIndex& index, int role ) const
406 if( !index.isValid() )
409 ExtensionCopy * extension =
410 static_cast<ExtensionCopy *>(index.internalPointer());
412 return extension->data( role );
415 QModelIndex ExtensionListModel::index( int row, int column,
416 const QModelIndex& ) const
419 return QModelIndex();
420 if( row < 0 || row >= extensions.count() )
421 return QModelIndex();
423 return createIndex( row, 0, extensions.at( row ) );
427 /* Extension List Widget Item */
428 ExtensionItemDelegate::ExtensionItemDelegate( QObject *parent )
429 : QStyledItemDelegate( parent )
431 margins = QMargins( 4, 4, 4, 4 );
434 ExtensionItemDelegate::~ExtensionItemDelegate()
438 void ExtensionItemDelegate::paint( QPainter *painter,
439 const QStyleOptionViewItem &option,
440 const QModelIndex &index ) const
442 QStyleOptionViewItemV4 opt = option;
443 initStyleOption( &opt, index );
446 if ( opt.state & QStyle::State_Selected )
447 painter->fillRect( opt.rect, opt.palette.highlight() );
450 QPixmap icon = index.data( Qt::DecorationRole ).value<QPixmap>();
453 painter->drawPixmap( opt.rect.left() + margins.left(),
454 opt.rect.top() + margins.top(),
455 icon.scaled( opt.decorationSize,
457 Qt::SmoothTransformation )
462 painter->setRenderHint( QPainter::TextAntialiasing );
464 if ( opt.state & QStyle::State_Selected )
465 painter->setPen( opt.palette.highlightedText().color() );
467 QFont font( option.font );
468 font.setBold( true );
469 painter->setFont( font );
470 QRect textrect( opt.rect );
471 textrect.adjust( 2 * margins.left() + margins.right() + opt.decorationSize.width(),
474 - margins.bottom() - opt.fontMetrics.height() );
476 painter->drawText( textrect, Qt::AlignLeft,
477 index.data( Qt::DisplayRole ).toString() );
479 font.setBold( false );
480 painter->setFont( font );
481 painter->drawText( textrect.translated( 0, option.fontMetrics.height() ),
483 index.data( ExtensionListModel::DescriptionRole ).toString() );
488 QSize ExtensionItemDelegate::sizeHint( const QStyleOptionViewItem &option,
489 const QModelIndex &index ) const
491 if ( index.isValid() )
493 return QSize( 200, 2 * option.fontMetrics.height()
494 + margins.top() + margins.bottom() );
500 void ExtensionItemDelegate::initStyleOption( QStyleOptionViewItem *option,
501 const QModelIndex &index ) const
503 QStyledItemDelegate::initStyleOption( option, index );
504 option->decorationSize = QSize( option->rect.height(), option->rect.height() );
505 option->decorationSize -= QSize( margins.left() + margins.right(),
506 margins.top() + margins.bottom() );
509 /* "More information" dialog */
511 ExtensionInfoDialog::ExtensionInfoDialog( const QModelIndex &index,
512 intf_thread_t *p_intf,
514 : QVLCDialog( parent, p_intf )
516 // Let's be a modal dialog
517 setWindowModality( Qt::WindowModal );
520 setWindowTitle( qtr( "About" ) + " " + index.data(Qt::DisplayRole).toString() );
523 QGridLayout *layout = new QGridLayout( this );
526 QLabel *icon = new QLabel( this );
527 QPixmap pix = index.data(Qt::DecorationRole).value<QPixmap>();
528 Q_ASSERT( !pix.isNull() );
529 icon->setPixmap( pix );
530 icon->setAlignment( Qt::AlignCenter );
531 icon->setFixedSize( 48, 48 );
532 layout->addWidget( icon, 1, 0, 2, 1 );
535 QLabel *label = new QLabel( index.data(Qt::DisplayRole).toString(), this );
536 QFont font = label->font();
537 font.setBold( true );
538 font.setPointSizeF( font.pointSizeF() * 1.3f );
539 label->setFont( font );
540 layout->addWidget( label, 0, 0, 1, -1 );
543 label = new QLabel( "<b>" + qtr( "Version" ) + ":</b>", this );
544 layout->addWidget( label, 1, 1, 1, 1, Qt::AlignBottom );
545 label = new QLabel( index.data(ExtensionListModel::VersionRole).toString(), this );
546 layout->addWidget( label, 1, 2, 1, 2, Qt::AlignBottom );
549 label = new QLabel( "<b>" + qtr( "Author" ) + ":</b>", this );
550 layout->addWidget( label, 2, 1, 1, 1, Qt::AlignTop );
551 label = new QLabel( index.data(ExtensionListModel::AuthorRole).toString(), this );
552 layout->addWidget( label, 2, 2, 1, 2, Qt::AlignTop );
556 label = new QLabel( this );
557 label->setText( index.data(ExtensionListModel::DescriptionRole).toString() );
558 label->setWordWrap( true );
559 label->setOpenExternalLinks( true );
560 layout->addWidget( label, 4, 0, 1, -1 );
563 label = new QLabel( "<b>" + qtr( "Website" ) + ":</b>", this );
564 layout->addWidget( label, 5, 0, 1, 2 );
565 label = new QLabel( QString("<a href=\"%1\">%2</a>")
566 .arg( index.data(ExtensionListModel::LinkRole).toString() )
567 .arg( index.data(ExtensionListModel::LinkRole).toString() )
569 label->setOpenExternalLinks( true );
570 layout->addWidget( label, 5, 2, 1, -1 );
573 label = new QLabel( "<b>" + qtr( "File" ) + ":</b>", this );
574 layout->addWidget( label, 6, 0, 1, 2 );
576 new QLineEdit( index.data(ExtensionListModel::NameRole).toString(), this );
577 line->setReadOnly( true );
578 layout->addWidget( line, 6, 2, 1, -1 );
581 QDialogButtonBox *group = new QDialogButtonBox( this );
582 QPushButton *closeButton = new QPushButton( qtr( "&Close" ) );
583 group->addButton( closeButton, QDialogButtonBox::RejectRole );
584 BUTTONACT( closeButton, close() );
586 layout->addWidget( group, 7, 0, 1, -1 );
589 layout->setColumnStretch( 2, 1 );
590 layout->setRowStretch( 4, 1 );
591 setMinimumSize( 450, 350 );
594 static QPixmap *loadPixmapFromData( char *data, int size )
596 if( !data || size <= 0 )
598 QPixmap *pixmap = new QPixmap();
599 if( !pixmap->loadFromData( (const uchar*) data, (uint) size ) )