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();
186 if( col == PluginTab::SCORE )
187 return text( col ).toInt() < other.text( col ).toInt();
188 else if ( col == PluginTab::CAPABILITY )
190 if ( text( PluginTab::CAPABILITY ) == other.text( PluginTab::CAPABILITY ) )
191 return text( PluginTab::NAME ) < other.text( PluginTab::NAME );
193 return text( PluginTab::CAPABILITY ) < other.text( PluginTab::CAPABILITY );
195 return text( col ) < other.text( col );
199 ExtensionTab::ExtensionTab( intf_thread_t *p_intf_ )
200 : QVLCFrame( p_intf_ )
203 QVBoxLayout *layout = new QVBoxLayout( this );
205 QLabel *notice = new QLabel( qtr("Get more extensions from")
206 + QString( " <a href=\"http://addons.videolan.org/\">"
207 "addons.videolan.org</a>." ) );
208 notice->setOpenExternalLinks( true );
209 layout->addWidget( notice );
212 extList = new QListView( this );
213 CONNECT( extList, activated( const QModelIndex& ),
214 this, moreInformation() );
215 layout->addWidget( extList );
217 // List item delegate
218 ExtensionItemDelegate *itemDelegate = new ExtensionItemDelegate( p_intf,
220 extList->setItemDelegate( itemDelegate );
222 // Extension list look & feeling
223 extList->setAlternatingRowColors( true );
224 extList->setSelectionMode( QAbstractItemView::SingleSelection );
227 ExtensionListModel *model = new ExtensionListModel( extList, p_intf );
228 extList->setModel( model );
231 QDialogButtonBox *buttonsBox = new QDialogButtonBox;
233 // More information button
234 butMoreInfo = new QPushButton( QIcon( ":/menu/info" ),
235 qtr( "More information..." ),
237 CONNECT( butMoreInfo, clicked(), this, moreInformation() );
238 buttonsBox->addButton( butMoreInfo, QDialogButtonBox::ActionRole );
241 ExtensionsManager *EM = ExtensionsManager::getInstance( p_intf );
242 QPushButton *reload = new QPushButton( QIcon( ":/update" ),
243 qtr( "Reload extensions" ),
245 CONNECT( reload, clicked(), EM, reloadExtensions() );
246 CONNECT( reload, clicked(), this, updateButtons() );
247 CONNECT( extList->selectionModel(),
248 selectionChanged( const QItemSelection &, const QItemSelection & ),
251 buttonsBox->addButton( reload, QDialogButtonBox::ResetRole );
253 layout->addWidget( buttonsBox );
257 ExtensionTab::~ExtensionTab()
261 void ExtensionTab::updateButtons()
263 butMoreInfo->setEnabled( extList->selectionModel()->hasSelection() );
266 // Do not close on ESC or ENTER
267 void ExtensionTab::keyPressEvent( QKeyEvent *keyEvent )
269 if( keyEvent->key() == Qt::Key_Return ||
270 keyEvent->key() == Qt::Key_Enter )
276 // Show more information
277 void ExtensionTab::moreInformation()
279 QModelIndex index = extList->selectionModel()->selectedIndexes().first();
281 if( !index.isValid() )
284 ExtensionInfoDialog dlg( index, p_intf, this );
288 /* Safe copy of the extension_t struct */
289 ExtensionListModel::ExtensionCopy::ExtensionCopy( extension_t *p_ext )
291 name = qfu( p_ext->psz_name );
292 description = qfu( p_ext->psz_description );
293 shortdesc = qfu( p_ext->psz_shortdescription );
294 if( description.isEmpty() )
295 description = shortdesc;
296 if( shortdesc.isEmpty() && !description.isEmpty() )
297 shortdesc = description;
298 title = qfu( p_ext->psz_title );
299 author = qfu( p_ext->psz_author );
300 version = qfu( p_ext->psz_version );
301 url = qfu( p_ext->psz_url );
302 icon = loadPixmapFromData( p_ext->p_icondata, p_ext->i_icondata_size );
305 ExtensionListModel::ExtensionCopy::~ExtensionCopy()
310 QVariant ExtensionListModel::ExtensionCopy::data( int role ) const
314 case Qt::DisplayRole:
316 case Qt::DecorationRole:
317 if ( !icon ) return QPixmap( ":/logo/vlc48.png" );
319 case DescriptionRole:
334 /* Extensions list model for the QListView */
336 ExtensionListModel::ExtensionListModel( QListView *view, intf_thread_t *intf )
337 : QAbstractListModel( view ), p_intf( intf )
339 // Connect to ExtensionsManager::extensionsUpdated()
340 ExtensionsManager* EM = ExtensionsManager::getInstance( p_intf );
341 CONNECT( EM, extensionsUpdated(), this, updateList() );
343 // Load extensions now if not already loaded
344 EM->loadExtensions();
347 ExtensionListModel::~ExtensionListModel()
349 // Clear extensions list
350 while( !extensions.isEmpty() )
351 delete extensions.takeLast();
354 void ExtensionListModel::updateList()
358 // Clear extensions list
359 while( !extensions.isEmpty() )
361 ext = extensions.takeLast();
365 // Find new extensions
366 ExtensionsManager *EM = ExtensionsManager::getInstance( p_intf );
367 extensions_manager_t *p_mgr = EM->getManager();
371 vlc_mutex_lock( &p_mgr->lock );
373 FOREACH_ARRAY( p_ext, p_mgr->extensions )
375 ext = new ExtensionCopy( p_ext );
376 extensions.append( ext );
379 vlc_mutex_unlock( &p_mgr->lock );
380 vlc_object_release( p_mgr );
382 emit dataChanged( index( 0 ), index( rowCount() - 1 ) );
385 int ExtensionListModel::rowCount( const QModelIndex& ) const
388 ExtensionsManager *EM = ExtensionsManager::getInstance( p_intf );
389 extensions_manager_t *p_mgr = EM->getManager();
393 vlc_mutex_lock( &p_mgr->lock );
394 count = p_mgr->extensions.i_size;
395 vlc_mutex_unlock( &p_mgr->lock );
396 vlc_object_release( p_mgr );
401 QVariant ExtensionListModel::data( const QModelIndex& index, int role ) const
403 if( !index.isValid() )
406 ExtensionCopy * extension =
407 static_cast<ExtensionCopy *>(index.internalPointer());
409 return extension->data( role );
412 QModelIndex ExtensionListModel::index( int row, int column,
413 const QModelIndex& ) const
416 return QModelIndex();
417 if( row < 0 || row >= extensions.count() )
418 return QModelIndex();
420 return createIndex( row, 0, extensions.at( row ) );
424 /* Extension List Widget Item */
425 ExtensionItemDelegate::ExtensionItemDelegate( intf_thread_t *p_intf,
427 : QStyledItemDelegate( view ), view( view ), p_intf( p_intf )
431 ExtensionItemDelegate::~ExtensionItemDelegate()
435 void ExtensionItemDelegate::paint( QPainter *painter,
436 const QStyleOptionViewItem &option,
437 const QModelIndex &index ) const
439 int width = option.rect.width();
441 // Pixmap: buffer where to draw
442 QPixmap pix(option.rect.size());
445 pix.fill( Qt::transparent ); // FIXME
447 // ItemView primitive style
448 QApplication::style()->drawPrimitive( QStyle::PE_PanelItemViewItem,
452 // Painter on the pixmap
453 QPainter *pixpaint = new QPainter(&pix);
456 QFont font = painter->font();
457 QPen pen = painter->pen();
458 if( view->selectionModel()->selectedIndexes().contains( index ) )
460 pen.setBrush( option.palette.highlightedText() );
464 pen.setBrush( option.palette.text() );
466 pixpaint->setPen( pen );
467 QFontMetrics metrics = option.fontMetrics;
470 QPixmap icon = index.data( Qt::DecorationRole ).value<QPixmap>();
473 pixpaint->drawPixmap( 7, 7, 2*metrics.height(), 2*metrics.height(),
478 pixpaint->setRenderHint( QPainter::TextAntialiasing );
479 font.setBold( true );
480 pixpaint->setFont( font );
481 pixpaint->drawText( QRect( 17 + 2 * metrics.height(), 7,
482 width - 40 - 2 * metrics.height(),
484 Qt::AlignLeft, index.data( Qt::DisplayRole ).toString() );
486 // Short description: normal
487 font.setBold( false );
488 pixpaint->setFont( font );
489 pixpaint->drawText( QRect( 17 + 2 * metrics.height(),
490 7 + metrics.height(), width - 40,
492 Qt::AlignLeft, index.data( ExtensionListModel::DescriptionRole ).toString() );
494 // Flush paint operations
497 // Draw it on the screen!
498 painter->drawPixmap( option.rect, pix );
501 QSize ExtensionItemDelegate::sizeHint( const QStyleOptionViewItem &option,
502 const QModelIndex &index ) const
504 if (index.isValid() && index.column() == 0)
506 QFontMetrics metrics = option.fontMetrics;
507 return QSize( 200, 14 + 2 * metrics.height() );
513 /* "More information" dialog */
515 ExtensionInfoDialog::ExtensionInfoDialog( const QModelIndex &index,
516 intf_thread_t *p_intf,
518 : QVLCDialog( parent, p_intf )
520 // Let's be a modal dialog
521 setWindowModality( Qt::WindowModal );
524 setWindowTitle( qtr( "About" ) + " " + index.data(Qt::DisplayRole).toString() );
527 QGridLayout *layout = new QGridLayout( this );
530 QLabel *icon = new QLabel( this );
531 QPixmap pix = index.data(Qt::DecorationRole).value<QPixmap>();
532 Q_ASSERT( !pix.isNull() );
533 icon->setPixmap( pix );
534 icon->setAlignment( Qt::AlignCenter );
535 icon->setFixedSize( 48, 48 );
536 layout->addWidget( icon, 1, 0, 2, 1 );
539 QLabel *label = new QLabel( index.data(Qt::DisplayRole).toString(), this );
540 QFont font = label->font();
541 font.setBold( true );
542 font.setPointSizeF( font.pointSizeF() * 1.3f );
543 label->setFont( font );
544 layout->addWidget( label, 0, 0, 1, -1 );
547 label = new QLabel( "<b>" + qtr( "Version" ) + ":</b>", this );
548 layout->addWidget( label, 1, 1, 1, 1, Qt::AlignBottom );
549 label = new QLabel( index.data(ExtensionListModel::VersionRole).toString(), this );
550 layout->addWidget( label, 1, 2, 1, 2, Qt::AlignBottom );
553 label = new QLabel( "<b>" + qtr( "Author" ) + ":</b>", this );
554 layout->addWidget( label, 2, 1, 1, 1, Qt::AlignTop );
555 label = new QLabel( index.data(ExtensionListModel::AuthorRole).toString(), this );
556 layout->addWidget( label, 2, 2, 1, 2, Qt::AlignTop );
560 label = new QLabel( this );
561 label->setText( index.data(ExtensionListModel::DescriptionRole).toString() );
562 label->setWordWrap( true );
563 label->setOpenExternalLinks( true );
564 layout->addWidget( label, 4, 0, 1, -1 );
567 label = new QLabel( "<b>" + qtr( "Website" ) + ":</b>", this );
568 layout->addWidget( label, 5, 0, 1, 2 );
569 label = new QLabel( QString("<a href=\"%1\">%2</a>")
570 .arg( index.data(ExtensionListModel::LinkRole).toString() )
571 .arg( index.data(ExtensionListModel::LinkRole).toString() )
573 label->setOpenExternalLinks( true );
574 layout->addWidget( label, 5, 2, 1, -1 );
577 label = new QLabel( "<b>" + qtr( "File" ) + ":</b>", this );
578 layout->addWidget( label, 6, 0, 1, 2 );
580 new QLineEdit( index.data(ExtensionListModel::NameRole).toString(), this );
581 line->setReadOnly( true );
582 layout->addWidget( line, 6, 2, 1, -1 );
585 QDialogButtonBox *group = new QDialogButtonBox( this );
586 QPushButton *closeButton = new QPushButton( qtr( "&Close" ) );
587 group->addButton( closeButton, QDialogButtonBox::RejectRole );
588 BUTTONACT( closeButton, close() );
590 layout->addWidget( group, 7, 0, 1, -1 );
593 layout->setColumnStretch( 2, 1 );
594 layout->setRowStretch( 4, 1 );
595 setMinimumSize( 450, 350 );
598 static QPixmap *loadPixmapFromData( char *data, int size )
600 if( !data || size <= 0 )
602 QPixmap *pixmap = new QPixmap();
603 if( !pixmap->loadFromData( (const uchar*) data, (uint) size ) )