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 );
147 void PluginTab::search( const QString& qs )
149 QList<QTreeWidgetItem *> items = treePlugins->findItems( qs, Qt::MatchContains );
150 items += treePlugins->findItems( qs, Qt::MatchContains, 1 );
152 QTreeWidgetItem *item = NULL;
153 for( int i = 0; i < treePlugins->topLevelItemCount(); i++ )
155 item = treePlugins->topLevelItem( i );
156 item->setHidden( !items.contains( item ) );
160 PluginTab::~PluginTab()
162 writeSettings( "Plugins" );
163 getSettings()->setValue( "Plugins/Header-State",
164 treePlugins->header()->saveState() );
167 bool PluginTreeItem::operator< ( const QTreeWidgetItem & other ) const
169 int col = treeWidget()->sortColumn();
171 return text( col ).toInt() < other.text( col ).toInt();
172 return text( col ) < other.text( col );
176 ExtensionTab::ExtensionTab( intf_thread_t *p_intf )
177 : QVLCFrame( p_intf )
180 QVBoxLayout *layout = new QVBoxLayout( this );
183 extList = new QListView( this );
184 CONNECT( extList, activated( const QModelIndex& ),
185 this, moreInformation() );
186 layout->addWidget( extList );
188 // List item delegate
189 ExtensionItemDelegate *itemDelegate = new ExtensionItemDelegate( p_intf,
191 extList->setItemDelegate( itemDelegate );
193 // Extension list look & feeling
194 extList->setAlternatingRowColors( true );
195 extList->setSelectionMode( QAbstractItemView::SingleSelection );
198 ExtensionListModel *model = new ExtensionListModel( extList, p_intf );
199 extList->setModel( model );
202 QHBoxLayout *hbox = new QHBoxLayout;
203 hbox->addItem( new QSpacerItem( 1, 1, QSizePolicy::Expanding,
204 QSizePolicy::Fixed ) );
206 // More information button
207 butMoreInfo = new QPushButton( QIcon( ":/menu/info" ),
208 qtr( "More information..." ),
210 CONNECT( butMoreInfo, clicked(),
211 this, moreInformation() );
212 hbox->addWidget( butMoreInfo );
215 ExtensionsManager *EM = ExtensionsManager::getInstance( p_intf );
216 QPushButton *reload = new QPushButton( QIcon( ":/update" ),
217 qtr( "Reload extensions" ),
219 CONNECT( reload, clicked(),
220 EM, reloadExtensions() );
221 hbox->addWidget( reload );
224 layout->addItem( hbox );
227 ExtensionTab::~ExtensionTab()
231 // Do not close on ESC or ENTER
232 void ExtensionTab::keyPressEvent( QKeyEvent *keyEvent )
237 // Show more information
238 void ExtensionTab::moreInformation()
240 if( !extList->selectionModel() ||
241 extList->selectionModel()->selectedIndexes().isEmpty() )
247 QModelIndex index = extList->selectionModel()->selectedIndexes().first();
248 ExtensionCopy *ext = (ExtensionCopy*) index.internalPointer();
252 ExtensionInfoDialog dlg( *ext, p_intf, this );
256 /* Safe copy of the extension_t struct */
260 ExtensionCopy( extension_t *p_ext )
262 name = qfu( p_ext->psz_name );
263 description = qfu( p_ext->psz_description );
264 shortdesc = qfu( p_ext->psz_shortdescription );
265 if( description.isEmpty() )
266 description = shortdesc;
267 if( shortdesc.isEmpty() && !description.isEmpty() )
268 shortdesc = description;
269 title = qfu( p_ext->psz_title );
270 author = qfu( p_ext->psz_author );
271 version = qfu( p_ext->psz_version );
272 url = qfu( p_ext->psz_url );
276 QString name, title, description, shortdesc, author, version, url;
279 /* Extensions list model for the QListView */
281 ExtensionListModel::ExtensionListModel( QListView *view, intf_thread_t *intf )
282 : QAbstractListModel( view ), p_intf( intf )
284 // Connect to ExtensionsManager::extensionsUpdated()
285 ExtensionsManager* EM = ExtensionsManager::getInstance( p_intf );
286 CONNECT( EM, extensionsUpdated(), this, updateList() );
288 // Load extensions now if not already loaded
289 EM->loadExtensions();
292 ExtensionListModel::~ExtensionListModel()
296 void ExtensionListModel::updateList()
300 // Clear extensions list
301 while( !extensions.isEmpty() )
303 ext = extensions.takeLast();
307 // Find new extensions
308 ExtensionsManager *EM = ExtensionsManager::getInstance( p_intf );
309 extensions_manager_t *p_mgr = EM->getManager();
313 vlc_mutex_lock( &p_mgr->lock );
315 FOREACH_ARRAY( p_ext, p_mgr->extensions )
317 ext = new ExtensionCopy( p_ext );
318 extensions.push_back( ext );
321 vlc_mutex_unlock( &p_mgr->lock );
322 vlc_object_release( p_mgr );
324 emit dataChanged( index( 0 ), index( rowCount() - 1 ) );
327 int ExtensionListModel::rowCount( const QModelIndex& parent ) const
330 ExtensionsManager *EM = ExtensionsManager::getInstance( p_intf );
331 extensions_manager_t *p_mgr = EM->getManager();
335 vlc_mutex_lock( &p_mgr->lock );
336 count = p_mgr->extensions.i_size;
337 vlc_mutex_unlock( &p_mgr->lock );
338 vlc_object_release( p_mgr );
343 QVariant ExtensionListModel::data( const QModelIndex& index, int role ) const
345 if( !index.isValid() )
355 QModelIndex ExtensionListModel::index( int row, int column,
356 const QModelIndex& parent ) const
359 return QModelIndex();
360 if( row < 0 || row >= extensions.size() )
361 return QModelIndex();
363 return createIndex( row, 0, extensions.at( row ) );
367 /* Extension List Widget Item */
368 ExtensionItemDelegate::ExtensionItemDelegate( intf_thread_t *p_intf,
370 : QStyledItemDelegate( view ), view( view ), p_intf( p_intf )
374 ExtensionItemDelegate::~ExtensionItemDelegate()
378 void ExtensionItemDelegate::paint( QPainter *painter,
379 const QStyleOptionViewItem &option,
380 const QModelIndex &index ) const
382 ExtensionCopy *ext = ( ExtensionCopy* ) index.internalPointer();
383 assert( ext != NULL );
385 int width = option.rect.width();
386 int height = option.rect.height();
388 // Pixmap: buffer where to draw
389 QPixmap pix(option.rect.size());
392 pix.fill( Qt::transparent ); // FIXME
394 // ItemView primitive style
395 QApplication::style()->drawPrimitive( QStyle::PE_PanelItemViewItem,
399 // Painter on the pixmap
400 QPainter *pixpaint = new QPainter(&pix);
403 QFont font = painter->font();
404 QPen pen = painter->pen();
405 if( view->selectionModel()->selectedIndexes().contains( index ) )
407 pen.setBrush( option.palette.highlightedText() );
411 pen.setBrush( option.palette.text() );
413 pixpaint->setPen( pen );
414 QFontMetrics metrics = option.fontMetrics;
416 /// @todo Add extension's icon
419 font.setBold( true );
420 pixpaint->setFont( font );
421 pixpaint->drawText( QRect( 10, 7, width - 70, metrics.height() ),
422 Qt::AlignLeft, ext->title );
424 // Short description: normal
425 font.setBold( false );
426 pixpaint->setFont( font );
427 pixpaint->drawText( QRect( 10, 7 + metrics.height(), width - 40,
429 Qt::AlignLeft, ext->shortdesc );
432 font.setItalic( true );
433 pixpaint->setFont( font );
434 pixpaint->drawText( width - 40, 7 + metrics.height(), ext->version );
436 // Flush paint operations
439 // Draw it on the screen!
440 painter->drawPixmap( option.rect, pix );
443 QSize ExtensionItemDelegate::sizeHint( const QStyleOptionViewItem &option,
444 const QModelIndex &index ) const
446 if (index.isValid() && index.column() == 0)
448 QFontMetrics metrics = option.fontMetrics;
449 return QSize( 200, 14 + 2 * metrics.height() );
455 /* "More information" dialog */
457 ExtensionInfoDialog::ExtensionInfoDialog( const ExtensionCopy& extension,
458 intf_thread_t *p_intf,
460 : QVLCDialog( parent, p_intf ),
461 extension( new ExtensionCopy( extension ) )
463 // Let's be a modal dialog
464 setWindowModality( Qt::WindowModal );
467 setWindowTitle( qtr( "About" ) + " " + extension.title );
470 QGridLayout *layout = new QGridLayout( this );
473 /// @todo Use the extension's icon, when extensions will support icons :)
474 QLabel *icon = new QLabel( this );
475 QPixmap pix( ":/logo/vlc48.png" );
476 icon->setPixmap( pix );
477 layout->addWidget( icon, 1, 0, 2, 1 );
480 QLabel *label = new QLabel( extension.title, this );
481 QFont font = label->font();
482 font.setBold( true );
483 font.setPointSizeF( font.pointSizeF() * 1.3f );
484 label->setFont( font );
485 layout->addWidget( label, 0, 0, 1, -1 );
488 label = new QLabel( "<b>" + qtr( "Version" ) + ":</b>", this );
489 layout->addWidget( label, 1, 1, 1, 1, Qt::AlignBottom );
490 label = new QLabel( extension.version, this );
491 layout->addWidget( label, 1, 2, 1, 2, Qt::AlignBottom );
494 label = new QLabel( "<b>" + qtr( "Author" ) + ":</b>", this );
495 layout->addWidget( label, 2, 1, 1, 1, Qt::AlignTop );
496 label = new QLabel( extension.author, this );
497 layout->addWidget( label, 2, 2, 1, 2, Qt::AlignTop );
501 label = new QLabel( this );
502 label->setText( extension.description );
503 label->setWordWrap( true );
504 label->setOpenExternalLinks( true );
505 layout->addWidget( label, 4, 0, 1, -1 );
508 label = new QLabel( "<b>" + qtr( "Website" ) + ":</b>", this );
509 layout->addWidget( label, 5, 0, 1, 2 );
510 QString txt = "<a href=\"";
511 txt += extension.url;
513 txt += extension.url;
515 label = new QLabel( txt, this );
516 label->setText( txt );
517 label->setOpenExternalLinks( true );
518 layout->addWidget( label, 5, 2, 1, -1 );
521 label = new QLabel( "<b>" + qtr( "File" ) + ":</b>", this );
522 layout->addWidget( label, 6, 0, 1, 2 );
523 QLineEdit *line = new QLineEdit( extension.name, this );
524 layout->addWidget( line, 6, 2, 1, -1 );
527 QDialogButtonBox *group = new QDialogButtonBox( QDialogButtonBox::Close,
528 Qt::Horizontal, this );
529 layout->addWidget( group, 7, 0, 1, -1 );
530 connect( group, SIGNAL(clicked(QAbstractButton*)), this, SLOT(close()) );
533 layout->setColumnStretch( 2, 1 );
534 layout->setRowStretch( 4, 1 );
535 setMinimumSize( 450, 350 );
538 ExtensionInfoDialog::~ExtensionInfoDialog()