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( p_intf,
224 extList->setItemDelegate( itemDelegate );
226 // Extension list look & feeling
227 extList->setAlternatingRowColors( true );
228 extList->setSelectionMode( QAbstractItemView::SingleSelection );
231 ExtensionListModel *model = new ExtensionListModel( extList, p_intf );
232 extList->setModel( model );
235 QDialogButtonBox *buttonsBox = new QDialogButtonBox;
237 // More information button
238 butMoreInfo = new QPushButton( QIcon( ":/menu/info" ),
239 qtr( "More information..." ),
241 CONNECT( butMoreInfo, clicked(), this, moreInformation() );
242 buttonsBox->addButton( butMoreInfo, QDialogButtonBox::ActionRole );
245 ExtensionsManager *EM = ExtensionsManager::getInstance( p_intf );
246 QPushButton *reload = new QPushButton( QIcon( ":/update" ),
247 qtr( "Reload extensions" ),
249 CONNECT( reload, clicked(), EM, reloadExtensions() );
250 CONNECT( reload, clicked(), this, updateButtons() );
251 CONNECT( extList->selectionModel(),
252 selectionChanged( const QItemSelection &, const QItemSelection & ),
255 buttonsBox->addButton( reload, QDialogButtonBox::ResetRole );
257 layout->addWidget( buttonsBox );
261 ExtensionTab::~ExtensionTab()
265 void ExtensionTab::updateButtons()
267 butMoreInfo->setEnabled( extList->selectionModel()->hasSelection() );
270 // Do not close on ESC or ENTER
271 void ExtensionTab::keyPressEvent( QKeyEvent *keyEvent )
273 if( keyEvent->key() == Qt::Key_Return ||
274 keyEvent->key() == Qt::Key_Enter )
280 // Show more information
281 void ExtensionTab::moreInformation()
283 QModelIndex index = extList->selectionModel()->selectedIndexes().first();
285 if( !index.isValid() )
288 ExtensionInfoDialog dlg( index, p_intf, this );
292 /* Safe copy of the extension_t struct */
293 ExtensionListModel::ExtensionCopy::ExtensionCopy( extension_t *p_ext )
295 name = qfu( p_ext->psz_name );
296 description = qfu( p_ext->psz_description );
297 shortdesc = qfu( p_ext->psz_shortdescription );
298 if( description.isEmpty() )
299 description = shortdesc;
300 if( shortdesc.isEmpty() && !description.isEmpty() )
301 shortdesc = description;
302 title = qfu( p_ext->psz_title );
303 author = qfu( p_ext->psz_author );
304 version = qfu( p_ext->psz_version );
305 url = qfu( p_ext->psz_url );
306 icon = loadPixmapFromData( p_ext->p_icondata, p_ext->i_icondata_size );
309 ExtensionListModel::ExtensionCopy::~ExtensionCopy()
314 QVariant ExtensionListModel::ExtensionCopy::data( int role ) const
318 case Qt::DisplayRole:
320 case Qt::DecorationRole:
321 if ( !icon ) return QPixmap( ":/logo/vlc48.png" );
323 case DescriptionRole:
338 /* Extensions list model for the QListView */
340 ExtensionListModel::ExtensionListModel( QListView *view, intf_thread_t *intf )
341 : QAbstractListModel( view ), p_intf( intf )
343 // Connect to ExtensionsManager::extensionsUpdated()
344 ExtensionsManager* EM = ExtensionsManager::getInstance( p_intf );
345 CONNECT( EM, extensionsUpdated(), this, updateList() );
347 // Load extensions now if not already loaded
348 EM->loadExtensions();
351 ExtensionListModel::~ExtensionListModel()
353 // Clear extensions list
354 while( !extensions.isEmpty() )
355 delete extensions.takeLast();
358 void ExtensionListModel::updateList()
362 // Clear extensions list
363 while( !extensions.isEmpty() )
365 ext = extensions.takeLast();
369 // Find new extensions
370 ExtensionsManager *EM = ExtensionsManager::getInstance( p_intf );
371 extensions_manager_t *p_mgr = EM->getManager();
375 vlc_mutex_lock( &p_mgr->lock );
377 FOREACH_ARRAY( p_ext, p_mgr->extensions )
379 ext = new ExtensionCopy( p_ext );
380 extensions.append( ext );
383 vlc_mutex_unlock( &p_mgr->lock );
384 vlc_object_release( p_mgr );
386 emit dataChanged( index( 0 ), index( rowCount() - 1 ) );
389 int ExtensionListModel::rowCount( const QModelIndex& ) const
392 ExtensionsManager *EM = ExtensionsManager::getInstance( p_intf );
393 extensions_manager_t *p_mgr = EM->getManager();
397 vlc_mutex_lock( &p_mgr->lock );
398 count = p_mgr->extensions.i_size;
399 vlc_mutex_unlock( &p_mgr->lock );
400 vlc_object_release( p_mgr );
405 QVariant ExtensionListModel::data( const QModelIndex& index, int role ) const
407 if( !index.isValid() )
410 ExtensionCopy * extension =
411 static_cast<ExtensionCopy *>(index.internalPointer());
413 return extension->data( role );
416 QModelIndex ExtensionListModel::index( int row, int column,
417 const QModelIndex& ) const
420 return QModelIndex();
421 if( row < 0 || row >= extensions.count() )
422 return QModelIndex();
424 return createIndex( row, 0, extensions.at( row ) );
428 /* Extension List Widget Item */
429 ExtensionItemDelegate::ExtensionItemDelegate( intf_thread_t *p_intf,
431 : QStyledItemDelegate( view ), view( view ), p_intf( p_intf )
433 margins = QMargins( 4, 4, 4, 4 );
436 ExtensionItemDelegate::~ExtensionItemDelegate()
440 void ExtensionItemDelegate::paint( QPainter *painter,
441 const QStyleOptionViewItem &option,
442 const QModelIndex &index ) const
444 QStyleOptionViewItemV4 opt = option;
445 initStyleOption( &opt, index );
448 if ( opt.state & QStyle::State_Selected )
449 painter->fillRect( opt.rect, opt.palette.highlight() );
452 QPixmap icon = index.data( Qt::DecorationRole ).value<QPixmap>();
455 painter->drawPixmap( opt.rect.left() + margins.left(),
456 opt.rect.top() + margins.top(),
457 icon.scaled( opt.decorationSize,
459 Qt::SmoothTransformation )
464 painter->setRenderHint( QPainter::TextAntialiasing );
466 if ( opt.state & QStyle::State_Selected )
467 painter->setPen( opt.palette.highlightedText().color() );
469 QFont font( option.font );
470 font.setBold( true );
471 painter->setFont( font );
472 QRect textrect( opt.rect );
473 textrect.adjust( 2 * margins.left() + margins.right() + opt.decorationSize.width(),
476 - margins.bottom() - opt.fontMetrics.height() );
478 painter->drawText( textrect, Qt::AlignLeft,
479 index.data( Qt::DisplayRole ).toString() );
481 font.setBold( false );
482 painter->setFont( font );
483 painter->drawText( textrect.translated( 0, option.fontMetrics.height() ),
485 index.data( ExtensionListModel::DescriptionRole ).toString() );
490 QSize ExtensionItemDelegate::sizeHint( const QStyleOptionViewItem &option,
491 const QModelIndex &index ) const
493 if ( index.isValid() )
495 return QSize( 200, 2 * option.fontMetrics.height()
496 + margins.top() + margins.bottom() );
502 void ExtensionItemDelegate::initStyleOption( QStyleOptionViewItem *option,
503 const QModelIndex &index ) const
505 QStyledItemDelegate::initStyleOption( option, index );
506 option->decorationSize = QSize( option->rect.height(), option->rect.height() );
507 option->decorationSize -= QSize( margins.left() + margins.right(),
508 margins.top() + margins.bottom() );
511 /* "More information" dialog */
513 ExtensionInfoDialog::ExtensionInfoDialog( const QModelIndex &index,
514 intf_thread_t *p_intf,
516 : QVLCDialog( parent, p_intf )
518 // Let's be a modal dialog
519 setWindowModality( Qt::WindowModal );
522 setWindowTitle( qtr( "About" ) + " " + index.data(Qt::DisplayRole).toString() );
525 QGridLayout *layout = new QGridLayout( this );
528 QLabel *icon = new QLabel( this );
529 QPixmap pix = index.data(Qt::DecorationRole).value<QPixmap>();
530 Q_ASSERT( !pix.isNull() );
531 icon->setPixmap( pix );
532 icon->setAlignment( Qt::AlignCenter );
533 icon->setFixedSize( 48, 48 );
534 layout->addWidget( icon, 1, 0, 2, 1 );
537 QLabel *label = new QLabel( index.data(Qt::DisplayRole).toString(), this );
538 QFont font = label->font();
539 font.setBold( true );
540 font.setPointSizeF( font.pointSizeF() * 1.3f );
541 label->setFont( font );
542 layout->addWidget( label, 0, 0, 1, -1 );
545 label = new QLabel( "<b>" + qtr( "Version" ) + ":</b>", this );
546 layout->addWidget( label, 1, 1, 1, 1, Qt::AlignBottom );
547 label = new QLabel( index.data(ExtensionListModel::VersionRole).toString(), this );
548 layout->addWidget( label, 1, 2, 1, 2, Qt::AlignBottom );
551 label = new QLabel( "<b>" + qtr( "Author" ) + ":</b>", this );
552 layout->addWidget( label, 2, 1, 1, 1, Qt::AlignTop );
553 label = new QLabel( index.data(ExtensionListModel::AuthorRole).toString(), this );
554 layout->addWidget( label, 2, 2, 1, 2, Qt::AlignTop );
558 label = new QLabel( this );
559 label->setText( index.data(ExtensionListModel::DescriptionRole).toString() );
560 label->setWordWrap( true );
561 label->setOpenExternalLinks( true );
562 layout->addWidget( label, 4, 0, 1, -1 );
565 label = new QLabel( "<b>" + qtr( "Website" ) + ":</b>", this );
566 layout->addWidget( label, 5, 0, 1, 2 );
567 label = new QLabel( QString("<a href=\"%1\">%2</a>")
568 .arg( index.data(ExtensionListModel::LinkRole).toString() )
569 .arg( index.data(ExtensionListModel::LinkRole).toString() )
571 label->setOpenExternalLinks( true );
572 layout->addWidget( label, 5, 2, 1, -1 );
575 label = new QLabel( "<b>" + qtr( "File" ) + ":</b>", this );
576 layout->addWidget( label, 6, 0, 1, 2 );
578 new QLineEdit( index.data(ExtensionListModel::NameRole).toString(), this );
579 line->setReadOnly( true );
580 layout->addWidget( line, 6, 2, 1, -1 );
583 QDialogButtonBox *group = new QDialogButtonBox( this );
584 QPushButton *closeButton = new QPushButton( qtr( "&Close" ) );
585 group->addButton( closeButton, QDialogButtonBox::RejectRole );
586 BUTTONACT( closeButton, close() );
588 layout->addWidget( group, 7, 0, 1, -1 );
591 layout->setColumnStretch( 2, 1 );
592 layout->setRowStretch( 4, 1 );
593 setMinimumSize( 450, 350 );
596 static QPixmap *loadPixmapFromData( char *data, int size )
598 if( !data || size <= 0 )
600 QPixmap *pixmap = new QPixmap();
601 if( !pixmap->loadFromData( (const uchar*) data, (uint) size ) )