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 PluginDialog::PluginDialog( intf_thread_t *_p_intf ) : QVLCFrame( _p_intf )
59 setWindowTitle( qtr( "Plugins and extensions" ) );
60 setWindowRole( "vlc-plugins" );
62 QVBoxLayout *layout = new QVBoxLayout( this );
63 tabs = new QTabWidget( this );
64 tabs->addTab( extensionTab = new ExtensionTab( p_intf ),
65 qtr( "Extensions" ) );
66 tabs->addTab( pluginTab = new PluginTab( p_intf ),
68 layout->addWidget( tabs );
70 QDialogButtonBox *box = new QDialogButtonBox;
71 QPushButton *okButton = new QPushButton( qtr( "&Close" ), this );
72 box->addButton( okButton, QDialogButtonBox::AcceptRole );
73 layout->addWidget( box );
74 BUTTONACT( okButton, close() );
75 readSettings( "PluginsDialog", QSize( 435, 280 ) );
78 PluginDialog::~PluginDialog()
80 writeSettings( "PluginsDialog" );
85 PluginTab::PluginTab( intf_thread_t *p_intf )
88 QGridLayout *layout = new QGridLayout( this );
90 /* Main Tree for modules */
91 treePlugins = new QTreeWidget;
92 layout->addWidget( treePlugins, 0, 0, 1, -1 );
94 /* Users cannot move the columns around but we need to sort */
95 treePlugins->header()->setMovable( false );
96 treePlugins->header()->setSortIndicatorShown( true );
97 // treePlugins->header()->setResizeMode( QHeaderView::ResizeToContents );
98 treePlugins->setAlternatingRowColors( true );
99 treePlugins->setColumnWidth( 0, 200 );
101 QStringList headerNames;
102 headerNames << qtr("Name") << qtr("Capability" ) << qtr( "Score" );
103 treePlugins->setHeaderLabels( headerNames );
107 /* Set capability column to the correct Size*/
108 treePlugins->resizeColumnToContents( 1 );
109 treePlugins->header()->restoreState(
110 getSettings()->value( "Plugins/Header-State" ).toByteArray() );
112 treePlugins->setSortingEnabled( true );
113 treePlugins->sortByColumn( 1, Qt::AscendingOrder );
115 QLabel *label = new QLabel( qtr("&Search:"), this );
116 edit = new SearchLineEdit( this );
117 label->setBuddy( edit );
119 layout->addWidget( label, 1, 0 );
120 layout->addWidget( edit, 1, 1, 1, 1 );
121 CONNECT( edit, textChanged( const QString& ),
122 this, search( const QString& ) );
124 setMinimumSize( 500, 300 );
125 readSettings( "Plugins", QSize( 540, 400 ) );
128 inline void PluginTab::FillTree()
130 module_t **p_list = module_list_get( NULL );
133 for( unsigned int i = 0; (p_module = p_list[i] ) != NULL; i++ )
136 qs_item << qfu( module_get_name( p_module, true ) )
137 << qfu( module_get_capability( p_module ) )
138 << QString::number( module_get_score( p_module ) );
140 if( qs_item.at(1).isEmpty() ) continue;
143 QTreeWidgetItem *item = new PluginTreeItem( qs_item );
144 treePlugins->addTopLevelItem( item );
146 module_list_free( p_list );
149 void PluginTab::search( const QString& qs )
151 QList<QTreeWidgetItem *> items = treePlugins->findItems( qs, Qt::MatchContains );
152 items += treePlugins->findItems( qs, Qt::MatchContains, 1 );
154 QTreeWidgetItem *item = NULL;
155 for( int i = 0; i < treePlugins->topLevelItemCount(); i++ )
157 item = treePlugins->topLevelItem( i );
158 item->setHidden( !items.contains( item ) );
162 PluginTab::~PluginTab()
164 writeSettings( "Plugins" );
165 getSettings()->setValue( "Plugins/Header-State",
166 treePlugins->header()->saveState() );
169 bool PluginTreeItem::operator< ( const QTreeWidgetItem & other ) const
171 int col = treeWidget()->sortColumn();
173 return text( col ).toInt() < other.text( col ).toInt();
174 return text( col ) < other.text( col );
178 ExtensionTab::ExtensionTab( intf_thread_t *p_intf )
179 : QVLCFrame( p_intf )
182 QVBoxLayout *layout = new QVBoxLayout( this );
185 extList = new QListView( this );
186 CONNECT( extList, activated( const QModelIndex& ),
187 this, moreInformation() );
188 layout->addWidget( extList );
190 // List item delegate
191 ExtensionItemDelegate *itemDelegate = new ExtensionItemDelegate( p_intf,
193 extList->setItemDelegate( itemDelegate );
195 // Extension list look & feeling
196 extList->setAlternatingRowColors( true );
197 extList->setSelectionMode( QAbstractItemView::SingleSelection );
200 ExtensionListModel *model = new ExtensionListModel( extList, p_intf );
201 extList->setModel( model );
204 QHBoxLayout *hbox = new QHBoxLayout;
205 hbox->addItem( new QSpacerItem( 1, 1, QSizePolicy::Expanding,
206 QSizePolicy::Fixed ) );
208 // More information button
209 butMoreInfo = new QPushButton( QIcon( ":/menu/info" ),
210 qtr( "More information..." ),
212 CONNECT( butMoreInfo, clicked(),
213 this, moreInformation() );
214 hbox->addWidget( butMoreInfo );
217 ExtensionsManager *EM = ExtensionsManager::getInstance( p_intf );
218 QPushButton *reload = new QPushButton( QIcon( ":/update" ),
219 qtr( "Reload extensions" ),
221 CONNECT( reload, clicked(),
222 EM, reloadExtensions() );
223 hbox->addWidget( reload );
226 layout->addItem( hbox );
229 ExtensionTab::~ExtensionTab()
233 // Do not close on ESC or ENTER
234 void ExtensionTab::keyPressEvent( QKeyEvent *keyEvent )
239 // Show more information
240 void ExtensionTab::moreInformation()
242 if( !extList->selectionModel() ||
243 extList->selectionModel()->selectedIndexes().isEmpty() )
249 QModelIndex index = extList->selectionModel()->selectedIndexes().first();
250 ExtensionCopy *ext = (ExtensionCopy*) index.internalPointer();
254 ExtensionInfoDialog dlg( *ext, p_intf, this );
258 /* Safe copy of the extension_t struct */
262 ExtensionCopy( extension_t *p_ext )
264 name = qfu( p_ext->psz_name );
265 description = qfu( p_ext->psz_description );
266 shortdesc = qfu( p_ext->psz_shortdescription );
267 if( description.isEmpty() )
268 description = shortdesc;
269 if( shortdesc.isEmpty() && !description.isEmpty() )
270 shortdesc = description;
271 title = qfu( p_ext->psz_title );
272 author = qfu( p_ext->psz_author );
273 version = qfu( p_ext->psz_version );
274 url = qfu( p_ext->psz_url );
278 QString name, title, description, shortdesc, author, version, url;
281 /* Extensions list model for the QListView */
283 ExtensionListModel::ExtensionListModel( QListView *view, intf_thread_t *intf )
284 : QAbstractListModel( view ), p_intf( intf )
286 // Connect to ExtensionsManager::extensionsUpdated()
287 ExtensionsManager* EM = ExtensionsManager::getInstance( p_intf );
288 CONNECT( EM, extensionsUpdated(), this, updateList() );
290 // Load extensions now if not already loaded
291 EM->loadExtensions();
294 ExtensionListModel::~ExtensionListModel()
296 // Clear extensions list
297 while( !extensions.isEmpty() )
298 delete extensions.takeLast();
301 void ExtensionListModel::updateList()
305 // Clear extensions list
306 while( !extensions.isEmpty() )
308 ext = extensions.takeLast();
312 // Find new extensions
313 ExtensionsManager *EM = ExtensionsManager::getInstance( p_intf );
314 extensions_manager_t *p_mgr = EM->getManager();
318 vlc_mutex_lock( &p_mgr->lock );
320 FOREACH_ARRAY( p_ext, p_mgr->extensions )
322 ext = new ExtensionCopy( p_ext );
323 extensions.push_back( ext );
326 vlc_mutex_unlock( &p_mgr->lock );
327 vlc_object_release( p_mgr );
329 emit dataChanged( index( 0 ), index( rowCount() - 1 ) );
332 int ExtensionListModel::rowCount( const QModelIndex& parent ) const
335 ExtensionsManager *EM = ExtensionsManager::getInstance( p_intf );
336 extensions_manager_t *p_mgr = EM->getManager();
340 vlc_mutex_lock( &p_mgr->lock );
341 count = p_mgr->extensions.i_size;
342 vlc_mutex_unlock( &p_mgr->lock );
343 vlc_object_release( p_mgr );
348 QVariant ExtensionListModel::data( const QModelIndex& index, int role ) const
350 if( !index.isValid() )
360 QModelIndex ExtensionListModel::index( int row, int column,
361 const QModelIndex& parent ) const
364 return QModelIndex();
365 if( row < 0 || row >= extensions.size() )
366 return QModelIndex();
368 return createIndex( row, 0, extensions.at( row ) );
372 /* Extension List Widget Item */
373 ExtensionItemDelegate::ExtensionItemDelegate( intf_thread_t *p_intf,
375 : QStyledItemDelegate( view ), view( view ), p_intf( p_intf )
379 ExtensionItemDelegate::~ExtensionItemDelegate()
383 void ExtensionItemDelegate::paint( QPainter *painter,
384 const QStyleOptionViewItem &option,
385 const QModelIndex &index ) const
387 ExtensionCopy *ext = ( ExtensionCopy* ) index.internalPointer();
388 assert( ext != NULL );
390 int width = option.rect.width();
391 int height = option.rect.height();
393 // Pixmap: buffer where to draw
394 QPixmap pix(option.rect.size());
397 pix.fill( Qt::transparent ); // FIXME
399 // ItemView primitive style
400 QApplication::style()->drawPrimitive( QStyle::PE_PanelItemViewItem,
404 // Painter on the pixmap
405 QPainter *pixpaint = new QPainter(&pix);
408 QFont font = painter->font();
409 QPen pen = painter->pen();
410 if( view->selectionModel()->selectedIndexes().contains( index ) )
412 pen.setBrush( option.palette.highlightedText() );
416 pen.setBrush( option.palette.text() );
418 pixpaint->setPen( pen );
419 QFontMetrics metrics = option.fontMetrics;
421 /// @todo Add extension's icon
424 pixpaint->setRenderHint( QPainter::TextAntialiasing );
425 font.setBold( true );
426 pixpaint->setFont( font );
427 pixpaint->drawText( QRect( 10, 7, width - 70, metrics.height() ),
428 Qt::AlignLeft, ext->title );
430 // Short description: normal
431 font.setBold( false );
432 pixpaint->setFont( font );
433 pixpaint->drawText( QRect( 10, 7 + metrics.height(), width - 40,
435 Qt::AlignLeft, ext->shortdesc );
437 // Flush paint operations
440 // Draw it on the screen!
441 painter->drawPixmap( option.rect, pix );
444 QSize ExtensionItemDelegate::sizeHint( const QStyleOptionViewItem &option,
445 const QModelIndex &index ) const
447 if (index.isValid() && index.column() == 0)
449 QFontMetrics metrics = option.fontMetrics;
450 return QSize( 200, 14 + 2 * metrics.height() );
456 /* "More information" dialog */
458 ExtensionInfoDialog::ExtensionInfoDialog( const ExtensionCopy& extension,
459 intf_thread_t *p_intf,
461 : QVLCDialog( parent, p_intf ),
462 extension( new ExtensionCopy( extension ) )
464 // Let's be a modal dialog
465 setWindowModality( Qt::WindowModal );
468 setWindowTitle( qtr( "About" ) + " " + extension.title );
471 QGridLayout *layout = new QGridLayout( this );
474 /// @todo Use the extension's icon, when extensions will support icons :)
475 QLabel *icon = new QLabel( this );
476 QPixmap pix( ":/logo/vlc48.png" );
477 icon->setPixmap( pix );
478 layout->addWidget( icon, 1, 0, 2, 1 );
481 QLabel *label = new QLabel( extension.title, this );
482 QFont font = label->font();
483 font.setBold( true );
484 font.setPointSizeF( font.pointSizeF() * 1.3f );
485 label->setFont( font );
486 layout->addWidget( label, 0, 0, 1, -1 );
489 label = new QLabel( "<b>" + qtr( "Version" ) + ":</b>", this );
490 layout->addWidget( label, 1, 1, 1, 1, Qt::AlignBottom );
491 label = new QLabel( extension.version, this );
492 layout->addWidget( label, 1, 2, 1, 2, Qt::AlignBottom );
495 label = new QLabel( "<b>" + qtr( "Author" ) + ":</b>", this );
496 layout->addWidget( label, 2, 1, 1, 1, Qt::AlignTop );
497 label = new QLabel( extension.author, this );
498 layout->addWidget( label, 2, 2, 1, 2, Qt::AlignTop );
502 label = new QLabel( this );
503 label->setText( extension.description );
504 label->setWordWrap( true );
505 label->setOpenExternalLinks( true );
506 layout->addWidget( label, 4, 0, 1, -1 );
509 label = new QLabel( "<b>" + qtr( "Website" ) + ":</b>", this );
510 layout->addWidget( label, 5, 0, 1, 2 );
511 QString txt = "<a href=\"";
512 txt += extension.url;
514 txt += extension.url;
516 label = new QLabel( txt, this );
517 label->setText( txt );
518 label->setOpenExternalLinks( true );
519 layout->addWidget( label, 5, 2, 1, -1 );
522 label = new QLabel( "<b>" + qtr( "File" ) + ":</b>", this );
523 layout->addWidget( label, 6, 0, 1, 2 );
524 QLineEdit *line = new QLineEdit( extension.name, this );
525 layout->addWidget( line, 6, 2, 1, -1 );
528 QDialogButtonBox *group = new QDialogButtonBox( this );
529 QPushButton *closeButton = new QPushButton( qtr( "&Close" ) );
530 group->addButton( closeButton, QDialogButtonBox::AcceptRole );
531 BUTTONACT( closeButton, close() );
533 layout->addWidget( group, 7, 0, 1, -1 );
536 layout->setColumnStretch( 2, 1 );
537 layout->setRowStretch( 4, 1 );
538 setMinimumSize( 450, 350 );
541 ExtensionInfoDialog::~ExtensionInfoDialog()