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/customwidgets.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>
56 PluginDialog::PluginDialog( intf_thread_t *_p_intf ) : QVLCFrame( _p_intf )
58 setWindowTitle( qtr( "Plugins and extensions" ) );
59 setWindowRole( "vlc-plugins" );
61 QVBoxLayout *layout = new QVBoxLayout( this );
62 tabs = new QTabWidget( this );
63 tabs->addTab( extensionTab = new ExtensionTab( p_intf ),
64 qtr( "Extensions" ) );
65 tabs->addTab( pluginTab = new PluginTab( p_intf ),
67 layout->addWidget( tabs );
69 QDialogButtonBox *box = new QDialogButtonBox;
70 QPushButton *okButton = new QPushButton( qtr( "&Close" ), this );
71 box->addButton( okButton, QDialogButtonBox::AcceptRole );
72 layout->addWidget( box );
73 BUTTONACT( okButton, close() );
74 readSettings( "PluginsDialog", QSize( 435, 280 ) );
77 PluginDialog::~PluginDialog()
79 writeSettings( "PluginsDialog" );
84 PluginTab::PluginTab( intf_thread_t *p_intf )
87 QGridLayout *layout = new QGridLayout( this );
89 /* Main Tree for modules */
90 treePlugins = new QTreeWidget;
91 layout->addWidget( treePlugins, 0, 0, 1, -1 );
93 /* Users cannot move the columns around but we need to sort */
94 treePlugins->header()->setMovable( false );
95 treePlugins->header()->setSortIndicatorShown( true );
96 // treePlugins->header()->setResizeMode( QHeaderView::ResizeToContents );
97 treePlugins->setAlternatingRowColors( true );
98 treePlugins->setColumnWidth( 0, 200 );
100 QStringList headerNames;
101 headerNames << qtr("Name") << qtr("Capability" ) << qtr( "Score" );
102 treePlugins->setHeaderLabels( headerNames );
106 /* Set capability column to the correct Size*/
107 treePlugins->resizeColumnToContents( 1 );
108 treePlugins->header()->restoreState(
109 getSettings()->value( "Plugins/Header-State" ).toByteArray() );
111 treePlugins->setSortingEnabled( true );
112 treePlugins->sortByColumn( 1, Qt::AscendingOrder );
114 QLabel *label = new QLabel( qtr("&Search:"), this );
115 edit = new SearchLineEdit( this );
116 label->setBuddy( edit );
118 layout->addWidget( label, 1, 0 );
119 layout->addWidget( edit, 1, 1, 1, 1 );
120 CONNECT( edit, textChanged( const QString& ),
121 this, search( const QString& ) );
123 setMinimumSize( 500, 300 );
124 readSettings( "Plugins", QSize( 540, 400 ) );
127 inline void PluginTab::FillTree()
129 module_t **p_list = module_list_get( NULL );
132 for( unsigned int i = 0; (p_module = p_list[i] ) != NULL; i++ )
135 qs_item << qfu( module_get_name( p_module, true ) )
136 << qfu( module_get_capability( p_module ) )
137 << QString::number( module_get_score( p_module ) );
139 if( qs_item.at(1).isEmpty() ) continue;
142 QTreeWidgetItem *item = new PluginTreeItem( qs_item );
143 treePlugins->addTopLevelItem( item );
145 module_list_free( p_list );
148 void PluginTab::search( const QString& qs )
150 QList<QTreeWidgetItem *> items = treePlugins->findItems( qs, Qt::MatchContains );
151 items += treePlugins->findItems( qs, Qt::MatchContains, 1 );
153 QTreeWidgetItem *item = NULL;
154 for( int i = 0; i < treePlugins->topLevelItemCount(); i++ )
156 item = treePlugins->topLevelItem( i );
157 item->setHidden( !items.contains( item ) );
161 PluginTab::~PluginTab()
163 writeSettings( "Plugins" );
164 getSettings()->setValue( "Plugins/Header-State",
165 treePlugins->header()->saveState() );
168 bool PluginTreeItem::operator< ( const QTreeWidgetItem & other ) const
170 int col = treeWidget()->sortColumn();
172 return text( col ).toInt() < other.text( col ).toInt();
173 return text( col ) < other.text( col );
177 ExtensionTab::ExtensionTab( intf_thread_t *p_intf )
178 : QVLCFrame( p_intf )
181 QVBoxLayout *layout = new QVBoxLayout( this );
184 extList = new QListView( this );
185 CONNECT( extList, activated( const QModelIndex& ),
186 this, moreInformation() );
187 layout->addWidget( extList );
189 // List item delegate
190 ExtensionItemDelegate *itemDelegate = new ExtensionItemDelegate( p_intf,
192 extList->setItemDelegate( itemDelegate );
194 // Extension list look & feeling
195 extList->setAlternatingRowColors( true );
196 extList->setSelectionMode( QAbstractItemView::SingleSelection );
199 ExtensionListModel *model = new ExtensionListModel( extList, p_intf );
200 extList->setModel( model );
203 QHBoxLayout *hbox = new QHBoxLayout;
204 hbox->addItem( new QSpacerItem( 1, 1, QSizePolicy::Expanding,
205 QSizePolicy::Fixed ) );
207 // More information button
208 butMoreInfo = new QPushButton( QIcon( ":/menu/info" ),
209 qtr( "More information..." ),
211 CONNECT( butMoreInfo, clicked(),
212 this, moreInformation() );
213 hbox->addWidget( butMoreInfo );
216 ExtensionsManager *EM = ExtensionsManager::getInstance( p_intf );
217 QPushButton *reload = new QPushButton( QIcon( ":/update" ),
218 qtr( "Reload extensions" ),
220 CONNECT( reload, clicked(),
221 EM, reloadExtensions() );
222 hbox->addWidget( reload );
225 layout->addItem( hbox );
228 ExtensionTab::~ExtensionTab()
232 // Do not close on ESC or ENTER
233 void ExtensionTab::keyPressEvent( QKeyEvent *keyEvent )
238 // Show more information
239 void ExtensionTab::moreInformation()
241 if( !extList->selectionModel() ||
242 extList->selectionModel()->selectedIndexes().isEmpty() )
248 QModelIndex index = extList->selectionModel()->selectedIndexes().first();
249 ExtensionCopy *ext = (ExtensionCopy*) index.internalPointer();
253 ExtensionInfoDialog dlg( *ext, p_intf, this );
257 /* Safe copy of the extension_t struct */
261 ExtensionCopy( extension_t *p_ext )
263 name = qfu( p_ext->psz_name );
264 description = qfu( p_ext->psz_description );
265 shortdesc = qfu( p_ext->psz_shortdescription );
266 if( description.isEmpty() )
267 description = shortdesc;
268 if( shortdesc.isEmpty() && !description.isEmpty() )
269 shortdesc = description;
270 title = qfu( p_ext->psz_title );
271 author = qfu( p_ext->psz_author );
272 version = qfu( p_ext->psz_version );
273 url = qfu( p_ext->psz_url );
277 QString name, title, description, shortdesc, author, version, url;
280 /* Extensions list model for the QListView */
282 ExtensionListModel::ExtensionListModel( QListView *view, intf_thread_t *intf )
283 : QAbstractListModel( view ), p_intf( intf )
285 // Connect to ExtensionsManager::extensionsUpdated()
286 ExtensionsManager* EM = ExtensionsManager::getInstance( p_intf );
287 CONNECT( EM, extensionsUpdated(), this, updateList() );
289 // Load extensions now if not already loaded
290 EM->loadExtensions();
293 ExtensionListModel::~ExtensionListModel()
295 // Clear extensions list
296 while( !extensions.isEmpty() )
297 delete extensions.takeLast();
300 void ExtensionListModel::updateList()
304 // Clear extensions list
305 while( !extensions.isEmpty() )
307 ext = extensions.takeLast();
311 // Find new extensions
312 ExtensionsManager *EM = ExtensionsManager::getInstance( p_intf );
313 extensions_manager_t *p_mgr = EM->getManager();
317 vlc_mutex_lock( &p_mgr->lock );
319 FOREACH_ARRAY( p_ext, p_mgr->extensions )
321 ext = new ExtensionCopy( p_ext );
322 extensions.push_back( ext );
325 vlc_mutex_unlock( &p_mgr->lock );
326 vlc_object_release( p_mgr );
328 emit dataChanged( index( 0 ), index( rowCount() - 1 ) );
331 int ExtensionListModel::rowCount( const QModelIndex& parent ) const
334 ExtensionsManager *EM = ExtensionsManager::getInstance( p_intf );
335 extensions_manager_t *p_mgr = EM->getManager();
339 vlc_mutex_lock( &p_mgr->lock );
340 count = p_mgr->extensions.i_size;
341 vlc_mutex_unlock( &p_mgr->lock );
342 vlc_object_release( p_mgr );
347 QVariant ExtensionListModel::data( const QModelIndex& index, int role ) const
349 if( !index.isValid() )
359 QModelIndex ExtensionListModel::index( int row, int column,
360 const QModelIndex& parent ) const
363 return QModelIndex();
364 if( row < 0 || row >= extensions.size() )
365 return QModelIndex();
367 return createIndex( row, 0, extensions.at( row ) );
371 /* Extension List Widget Item */
372 ExtensionItemDelegate::ExtensionItemDelegate( intf_thread_t *p_intf,
374 : QStyledItemDelegate( view ), view( view ), p_intf( p_intf )
378 ExtensionItemDelegate::~ExtensionItemDelegate()
382 void ExtensionItemDelegate::paint( QPainter *painter,
383 const QStyleOptionViewItem &option,
384 const QModelIndex &index ) const
386 ExtensionCopy *ext = ( ExtensionCopy* ) index.internalPointer();
387 assert( ext != NULL );
389 int width = option.rect.width();
390 int height = option.rect.height();
392 // Pixmap: buffer where to draw
393 QPixmap pix(option.rect.size());
396 pix.fill( Qt::transparent ); // FIXME
398 // ItemView primitive style
399 QApplication::style()->drawPrimitive( QStyle::PE_PanelItemViewItem,
403 // Painter on the pixmap
404 QPainter *pixpaint = new QPainter(&pix);
407 QFont font = painter->font();
408 QPen pen = painter->pen();
409 if( view->selectionModel()->selectedIndexes().contains( index ) )
411 pen.setBrush( option.palette.highlightedText() );
415 pen.setBrush( option.palette.text() );
417 pixpaint->setPen( pen );
418 QFontMetrics metrics = option.fontMetrics;
420 /// @todo Add extension's icon
423 font.setBold( true );
424 pixpaint->setFont( font );
425 pixpaint->drawText( QRect( 10, 7, width - 70, metrics.height() ),
426 Qt::AlignLeft, ext->title );
428 // Short description: normal
429 font.setBold( false );
430 pixpaint->setFont( font );
431 pixpaint->drawText( QRect( 10, 7 + metrics.height(), width - 40,
433 Qt::AlignLeft, ext->shortdesc );
435 // Flush paint operations
438 // Draw it on the screen!
439 painter->drawPixmap( option.rect, pix );
442 QSize ExtensionItemDelegate::sizeHint( const QStyleOptionViewItem &option,
443 const QModelIndex &index ) const
445 if (index.isValid() && index.column() == 0)
447 QFontMetrics metrics = option.fontMetrics;
448 return QSize( 200, 14 + 2 * metrics.height() );
454 /* "More information" dialog */
456 ExtensionInfoDialog::ExtensionInfoDialog( const ExtensionCopy& extension,
457 intf_thread_t *p_intf,
459 : QVLCDialog( parent, p_intf ),
460 extension( new ExtensionCopy( extension ) )
462 // Let's be a modal dialog
463 setWindowModality( Qt::WindowModal );
466 setWindowTitle( qtr( "About" ) + " " + extension.title );
469 QGridLayout *layout = new QGridLayout( this );
472 /// @todo Use the extension's icon, when extensions will support icons :)
473 QLabel *icon = new QLabel( this );
474 QPixmap pix( ":/logo/vlc48.png" );
475 icon->setPixmap( pix );
476 layout->addWidget( icon, 1, 0, 2, 1 );
479 QLabel *label = new QLabel( extension.title, this );
480 QFont font = label->font();
481 font.setBold( true );
482 font.setPointSizeF( font.pointSizeF() * 1.3f );
483 label->setFont( font );
484 layout->addWidget( label, 0, 0, 1, -1 );
487 label = new QLabel( "<b>" + qtr( "Version" ) + ":</b>", this );
488 layout->addWidget( label, 1, 1, 1, 1, Qt::AlignBottom );
489 label = new QLabel( extension.version, this );
490 layout->addWidget( label, 1, 2, 1, 2, Qt::AlignBottom );
493 label = new QLabel( "<b>" + qtr( "Author" ) + ":</b>", this );
494 layout->addWidget( label, 2, 1, 1, 1, Qt::AlignTop );
495 label = new QLabel( extension.author, this );
496 layout->addWidget( label, 2, 2, 1, 2, Qt::AlignTop );
500 label = new QLabel( this );
501 label->setText( extension.description );
502 label->setWordWrap( true );
503 label->setOpenExternalLinks( true );
504 layout->addWidget( label, 4, 0, 1, -1 );
507 label = new QLabel( "<b>" + qtr( "Website" ) + ":</b>", this );
508 layout->addWidget( label, 5, 0, 1, 2 );
509 QString txt = "<a href=\"";
510 txt += extension.url;
512 txt += extension.url;
514 label = new QLabel( txt, this );
515 label->setText( txt );
516 label->setOpenExternalLinks( true );
517 layout->addWidget( label, 5, 2, 1, -1 );
520 label = new QLabel( "<b>" + qtr( "File" ) + ":</b>", this );
521 layout->addWidget( label, 6, 0, 1, 2 );
522 QLineEdit *line = new QLineEdit( extension.name, this );
523 layout->addWidget( line, 6, 2, 1, -1 );
526 QDialogButtonBox *group = new QDialogButtonBox( this );
527 QPushButton *closeButton = new QPushButton( qtr( "&Close" ) );
528 group->addButton( closeButton, QDialogButtonBox::AcceptRole );
529 BUTTONACT( closeButton, close() );
531 layout->addWidget( group, 7, 0, 1, -1 );
534 layout->setColumnStretch( 2, 1 );
535 layout->setRowStretch( 4, 1 );
536 setMinimumSize( 450, 350 );
539 ExtensionInfoDialog::~ExtensionInfoDialog()