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"
33 #include "managers/addons_manager.hpp"
34 #include "util/animators.hpp"
38 #include <vlc_modules.h>
40 #include <QTreeWidget>
41 #include <QStringList>
43 #include <QHeaderView>
44 #include <QDialogButtonBox>
47 #include <QVBoxLayout>
49 #include <QHBoxLayout>
50 #include <QVBoxLayout>
51 #include <QSpacerItem>
54 #include <QStyleOptionViewItem>
56 #include <QPushButton>
59 #include <QStylePainter>
60 #include <QGraphicsColorizeEffect>
61 #include <QProgressBar>
65 static QPixmap *loadPixmapFromData( char *, int size );
68 PluginDialog::PluginDialog( intf_thread_t *_p_intf ) : QVLCFrame( _p_intf )
70 setWindowTitle( qtr( "Plugins and extensions" ) );
71 setWindowRole( "vlc-plugins" );
73 QVBoxLayout *layout = new QVBoxLayout( this );
74 tabs = new QTabWidget( this );
75 tabs->addTab( extensionTab = new ExtensionTab( p_intf ),
76 qtr( "Active Extensions" ) );
77 tabs->addTab( pluginTab = new PluginTab( p_intf ),
79 tabs->addTab( addonsTab = new AddonsTab( p_intf ),
80 qtr( "Addons Manager" ) );
81 layout->addWidget( tabs );
83 QDialogButtonBox *box = new QDialogButtonBox;
84 QPushButton *okButton = new QPushButton( qtr( "&Close" ), this );
85 box->addButton( okButton, QDialogButtonBox::RejectRole );
86 layout->addWidget( box );
87 BUTTONACT( okButton, close() );
88 restoreWidgetPosition( "PluginsDialog", QSize( 435, 280 ) );
91 PluginDialog::~PluginDialog()
93 saveWidgetPosition( "PluginsDialog" );
98 PluginTab::PluginTab( intf_thread_t *p_intf_ )
99 : QVLCFrame( p_intf_ )
101 QGridLayout *layout = new QGridLayout( this );
103 /* Main Tree for modules */
104 treePlugins = new QTreeWidget;
105 layout->addWidget( treePlugins, 0, 0, 1, -1 );
107 /* Users cannot move the columns around but we need to sort */
108 #if QT_VERSION >= 0x050000
109 treePlugins->header()->setSectionsMovable( false );
111 treePlugins->header()->setMovable( false );
113 treePlugins->header()->setSortIndicatorShown( true );
114 // treePlugins->header()->setResizeMode( QHeaderView::ResizeToContents );
115 treePlugins->setAlternatingRowColors( true );
116 treePlugins->setColumnWidth( 0, 200 );
118 QStringList headerNames;
119 headerNames << qtr("Name") << qtr("Capability" ) << qtr( "Score" );
120 treePlugins->setHeaderLabels( headerNames );
124 /* Set capability column to the correct Size*/
125 treePlugins->resizeColumnToContents( 1 );
126 treePlugins->header()->restoreState(
127 getSettings()->value( "Plugins/Header-State" ).toByteArray() );
129 treePlugins->setSortingEnabled( true );
130 treePlugins->sortByColumn( 1, Qt::AscendingOrder );
132 QLabel *label = new QLabel( qtr("&Search:"), this );
133 edit = new SearchLineEdit( this );
134 label->setBuddy( edit );
136 layout->addWidget( label, 1, 0 );
137 layout->addWidget( edit, 1, 1, 1, 1 );
138 CONNECT( edit, textChanged( const QString& ),
139 this, search( const QString& ) );
141 setMinimumSize( 500, 300 );
142 restoreWidgetPosition( "Plugins", QSize( 540, 400 ) );
145 inline void PluginTab::FillTree()
148 module_t **p_list = module_list_get( &count );
150 for( unsigned int i = 0; i < count; i++ )
152 module_t *p_module = p_list[i];
155 qs_item << qfu( module_get_name( p_module, true ) )
156 << qfu( module_get_capability( p_module ) )
157 << QString::number( module_get_score( p_module ) );
159 if( qs_item.at(1).isEmpty() ) continue;
162 QTreeWidgetItem *item = new PluginTreeItem( qs_item );
163 treePlugins->addTopLevelItem( item );
165 module_list_free( p_list );
168 void PluginTab::search( const QString& qs )
170 QList<QTreeWidgetItem *> items = treePlugins->findItems( qs, Qt::MatchContains );
171 items += treePlugins->findItems( qs, Qt::MatchContains, 1 );
173 QTreeWidgetItem *item = NULL;
174 for( int i = 0; i < treePlugins->topLevelItemCount(); i++ )
176 item = treePlugins->topLevelItem( i );
177 item->setHidden( !items.contains( item ) );
181 PluginTab::~PluginTab()
183 saveWidgetPosition( "Plugins" );
184 getSettings()->setValue( "Plugins/Header-State",
185 treePlugins->header()->saveState() );
188 void PluginTab::keyPressEvent( QKeyEvent *keyEvent )
190 if( keyEvent->key() == Qt::Key_Return ||
191 keyEvent->key() == Qt::Key_Enter )
197 bool PluginTreeItem::operator< ( const QTreeWidgetItem & other ) const
199 int col = treeWidget()->sortColumn();
200 if( col == PluginTab::SCORE )
201 return text( col ).toInt() < other.text( col ).toInt();
202 else if ( col == PluginTab::CAPABILITY )
204 if ( text( PluginTab::CAPABILITY ) == other.text( PluginTab::CAPABILITY ) )
205 return text( PluginTab::NAME ) < other.text( PluginTab::NAME );
207 return text( PluginTab::CAPABILITY ) < other.text( PluginTab::CAPABILITY );
209 return text( col ) < other.text( col );
213 ExtensionTab::ExtensionTab( intf_thread_t *p_intf_ )
214 : QVLCFrame( p_intf_ )
217 QVBoxLayout *layout = new QVBoxLayout( this );
219 QLabel *notice = new QLabel( qtr("Get more extensions from")
220 + QString( " <a href=\"http://addons.videolan.org/\">"
221 "addons.videolan.org</a>." ) );
222 notice->setOpenExternalLinks( true );
223 layout->addWidget( notice );
226 extList = new QListView( this );
227 CONNECT( extList, activated( const QModelIndex& ),
228 this, moreInformation() );
229 layout->addWidget( extList );
231 // List item delegate
232 ExtensionItemDelegate *itemDelegate = new ExtensionItemDelegate( extList );
233 extList->setItemDelegate( itemDelegate );
235 // Extension list look & feeling
236 extList->setAlternatingRowColors( true );
237 extList->setSelectionMode( QAbstractItemView::SingleSelection );
240 ExtensionListModel *model =
241 new ExtensionListModel( extList, ExtensionsManager::getInstance( p_intf ) );
242 extList->setModel( model );
245 QDialogButtonBox *buttonsBox = new QDialogButtonBox;
247 // More information button
248 butMoreInfo = new QPushButton( QIcon( ":/menu/info" ),
249 qtr( "More information..." ),
251 CONNECT( butMoreInfo, clicked(), this, moreInformation() );
252 buttonsBox->addButton( butMoreInfo, QDialogButtonBox::ActionRole );
255 ExtensionsManager *EM = ExtensionsManager::getInstance( p_intf );
256 QPushButton *reload = new QPushButton( QIcon( ":/update" ),
257 qtr( "Reload extensions" ),
259 CONNECT( reload, clicked(), EM, reloadExtensions() );
260 CONNECT( reload, clicked(), this, updateButtons() );
261 CONNECT( extList->selectionModel(),
262 selectionChanged( const QItemSelection &, const QItemSelection & ),
265 buttonsBox->addButton( reload, QDialogButtonBox::ResetRole );
267 layout->addWidget( buttonsBox );
271 ExtensionTab::~ExtensionTab()
275 void ExtensionTab::updateButtons()
277 butMoreInfo->setEnabled( extList->selectionModel()->hasSelection() );
280 // Do not close on ESC or ENTER
281 void ExtensionTab::keyPressEvent( QKeyEvent *keyEvent )
283 if( keyEvent->key() == Qt::Key_Return ||
284 keyEvent->key() == Qt::Key_Enter )
290 // Show more information
291 void ExtensionTab::moreInformation()
293 QModelIndex index = extList->selectionModel()->selectedIndexes().first();
295 if( !index.isValid() )
298 ExtensionInfoDialog dlg( index, p_intf, this );
303 AddonsTab::AddonsTab( intf_thread_t *p_intf_ ) : QVLCFrame( p_intf_ )
306 QVBoxLayout *layout = new QVBoxLayout( this );
309 QHBoxLayout *filtersLayout = new QHBoxLayout();
311 QLabel *addonsLabel = new QLabel( qtr("Addon type:") );
312 addonsLabel->setSizePolicy( QSizePolicy::Maximum, QSizePolicy::Preferred );
313 filtersLayout->addWidget( addonsLabel );
314 QComboBox *typeCombo = new QComboBox();
315 typeCombo->addItem( qtr("All"), -1 );
316 typeCombo->addItem( qtr("Skins"), ADDON_SKIN2 );
317 typeCombo->addItem( qtr("Playlist parsers"), ADDON_PLAYLIST_PARSER );
318 typeCombo->addItem( qtr("Service Discovery"), ADDON_SERVICE_DISCOVERY );
319 typeCombo->addItem( qtr("Extensions"), ADDON_EXTENSION );
320 CONNECT( typeCombo, currentIndexChanged(int), this, typeChanged( int ) );
321 filtersLayout->addWidget( typeCombo );
323 QCheckBox *installedOnlyBox = new QCheckBox( qtr("Show Installed Only") );
324 filtersLayout->addWidget( installedOnlyBox );
325 CONNECT( installedOnlyBox, stateChanged(int), this, installChecked(int) );
327 layout->addLayout( filtersLayout );
330 helpLabel = new QLabel();
331 layout->addWidget( helpLabel );
334 AddonsManager *AM = AddonsManager::getInstance( p_intf );
337 addonsView = new QListView( this );
338 CONNECT( addonsView, activated( const QModelIndex& ), this, moreInformation() );
339 layout->addWidget( addonsView );
341 // List item delegate
342 AddonItemDelegate *addonsDelegate = new AddonItemDelegate( addonsView );
343 addonsView->setItemDelegate( addonsDelegate );
344 addonsDelegate->setAnimator( new DelegateAnimationHelper( addonsView ) );
345 CONNECT( addonsDelegate, showInfo(), this, moreInformation() );
347 // Extension list look & feeling
348 addonsView->setAlternatingRowColors( true );
349 addonsView->setSelectionMode( QAbstractItemView::SingleSelection );
352 addonsView->setAcceptDrops( true );
353 addonsView->setDefaultDropAction( Qt::CopyAction );
354 addonsView->setDropIndicatorShown( true );
355 addonsView->setDragDropMode( QAbstractItemView::DropOnly );
358 AddonsListModel *model = new AddonsListModel( AM, addonsView );
359 addonsModel = new AddonsSortFilterProxyModel();
360 addonsModel->setDynamicSortFilter( true );
361 addonsModel->setSourceModel( model );
362 addonsModel->setFilterRole( Qt::DisplayRole );
363 addonsView->setModel( addonsModel );
365 CONNECT( addonsView->selectionModel(), currentChanged(QModelIndex,QModelIndex),
366 addonsView, edit(QModelIndex) );
368 CONNECT( AM, addonAdded( addon_entry_t * ),
369 model, addonAdded( addon_entry_t * ) );
370 CONNECT( AM, addonChanged( const addon_entry_t * ),
371 model, addonChanged( const addon_entry_t * ) );
373 QList<QString> frames;
374 frames << ":/util/wait1";
375 frames << ":/util/wait2";
376 frames << ":/util/wait3";
377 frames << ":/util/wait4";
378 spinnerAnimation = new PixmapAnimator( this, frames );
379 CONNECT( spinnerAnimation, pixmapReady( const QPixmap & ),
380 addonsView->viewport(), update() );
381 addonsView->viewport()->installEventFilter( this );
384 AddonsTab::~AddonsTab()
386 delete spinnerAnimation;
389 bool AddonsTab::eventFilter( QObject *obj, QEvent *event )
391 if ( obj != addonsView->viewport() )
394 switch( event->type() )
397 if ( spinnerAnimation->state() == PixmapAnimator::Running )
399 QWidget *viewport = qobject_cast<QWidget *>( obj );
400 QStylePainter painter( viewport );
401 QPixmap *spinner = spinnerAnimation->getPixmap();
402 QPoint point = viewport->geometry().center();
403 point -= QPoint( spinner->size().width() / 2, spinner->size().height() / 2 );
404 painter.drawPixmap( point, *spinner );
405 QString text = qtr("Retrieving addons...");
406 QSize textsize = fontMetrics().size( 0, text );
407 point = viewport->geometry().center();
408 point -= QPoint( textsize.width() / 2, -spinner->size().height() );
409 painter.drawText( point, text );
411 else if ( addonsModel->rowCount() == 0 )
413 QWidget *viewport = qobject_cast<QWidget *>( obj );
414 QStylePainter painter( viewport );
415 QString text = qtr("No addons found");
416 QSize size = fontMetrics().size( 0, text );
417 QPoint point = viewport->geometry().center();
418 point -= QPoint( size.width() / 2, size.height() / 2 );
419 painter.drawText( point, text );
423 if ( addonsView->model()->rowCount() < 1 )
425 AddonsManager *AM = AddonsManager::getInstance( p_intf );
426 CONNECT( AM, discoveryEnded(), spinnerAnimation, stop() );
427 spinnerAnimation->start();
432 case QEvent::DragEnter:
434 QDragEnterEvent *dragEvent = dynamic_cast<QDragEnterEvent *>(event);
435 QList<QUrl> urls = dragEvent->mimeData()->urls();
436 if ( dragEvent->proposedAction() != Qt::CopyAction
438 || urls.first().scheme() != "file"
439 || ! urls.first().path().endsWith(".vlp") )
441 dragEvent->acceptProposedAction();
444 case QEvent::DragMove:
446 QDragMoveEvent *moveEvent = dynamic_cast<QDragMoveEvent *>(event);
447 if ( moveEvent->proposedAction() != Qt::CopyAction )
449 moveEvent->acceptProposedAction();
454 QDropEvent *dropEvent = dynamic_cast<QDropEvent *>(event);
455 if ( dropEvent->proposedAction() != Qt::CopyAction )
457 if ( dropEvent->mimeData()->urls().count() )
459 AddonsManager *AM = AddonsManager::getInstance( p_intf );
460 AM->findDesignatedAddon( dropEvent->mimeData()->urls().first().toString() );
461 dropEvent->acceptProposedAction();
471 void AddonsTab::moreInformation()
473 QModelIndex index = addonsView->selectionModel()->selectedIndexes().first();
474 if( !index.isValid() ) return;
475 AddonInfoDialog dlg( index, p_intf, this );
479 void AddonsTab::typeChanged( int i )
481 QComboBox *combo = qobject_cast<QComboBox *>( sender() );
482 int i_type = combo->itemData( i, Qt::UserRole ).toInt();
483 addonsModel->setTypeFilter( i_type );
488 help = qtr( "Skins customize player's appearance."
489 " You can activate them through preferences." );
491 case ADDON_PLAYLIST_PARSER:
492 help = qtr( "Playlist parsers add new capabilities to read"
493 " internet streams or extract meta data." );
495 case ADDON_SERVICE_DISCOVERY:
496 help = qtr( "Service discoveries adds new sources to your playlist"
497 " such as web radios, video websites, ..." );
499 case ADDON_EXTENSION:
500 help = qtr( "Extensions brings various enhancements."
501 " Check descriptions for more details" );
504 helpLabel->setText("");
507 helpLabel->setTextFormat( Qt::RichText );
508 helpLabel->setText( QString( "<img src=\":/menu/info\"/> %1" ).arg( help ) );
511 void AddonsTab::installChecked( int i )
513 if ( i == Qt::Checked )
514 addonsModel->setStatusFilter( ADDON_INSTALLED );
516 addonsModel->setStatusFilter( 0 );
519 /* Safe copy of the extension_t struct */
520 ExtensionListModel::ExtensionCopy::ExtensionCopy( extension_t *p_ext )
522 name = qfu( p_ext->psz_name );
523 description = qfu( p_ext->psz_description );
524 shortdesc = qfu( p_ext->psz_shortdescription );
525 if( description.isEmpty() )
526 description = shortdesc;
527 if( shortdesc.isEmpty() && !description.isEmpty() )
528 shortdesc = description;
529 title = qfu( p_ext->psz_title );
530 author = qfu( p_ext->psz_author );
531 version = qfu( p_ext->psz_version );
532 url = qfu( p_ext->psz_url );
533 icon = loadPixmapFromData( p_ext->p_icondata, p_ext->i_icondata_size );
536 ExtensionListModel::ExtensionCopy::~ExtensionCopy()
541 QVariant ExtensionListModel::ExtensionCopy::data( int role ) const
545 case Qt::DisplayRole:
547 case Qt::DecorationRole:
548 if ( !icon ) return QPixmap( ":/logo/vlc48.png" );
565 /* Extensions list model for the QListView */
566 ExtensionListModel::ExtensionListModel( QObject *parent )
567 : QAbstractListModel( parent ), EM( NULL )
572 ExtensionListModel::ExtensionListModel( QObject *parent, ExtensionsManager* EM_ )
573 : QAbstractListModel( parent ), EM( EM_ )
575 // Connect to ExtensionsManager::extensionsUpdated()
576 CONNECT( EM, extensionsUpdated(), this, updateList() );
578 // Load extensions now if not already loaded
579 EM->loadExtensions();
582 ExtensionListModel::~ExtensionListModel()
584 // Clear extensions list
585 while( !extensions.isEmpty() )
586 delete extensions.takeLast();
589 void ExtensionListModel::updateList()
593 // Clear extensions list
594 while( !extensions.isEmpty() )
596 ext = extensions.takeLast();
600 // Find new extensions
601 extensions_manager_t *p_mgr = EM->getManager();
605 vlc_mutex_lock( &p_mgr->lock );
607 FOREACH_ARRAY( p_ext, p_mgr->extensions )
609 ext = new ExtensionCopy( p_ext );
610 extensions.append( ext );
613 vlc_mutex_unlock( &p_mgr->lock );
614 vlc_object_release( p_mgr );
616 emit dataChanged( index( 0 ), index( rowCount() - 1 ) );
619 int ExtensionListModel::rowCount( const QModelIndex& ) const
622 extensions_manager_t *p_mgr = EM->getManager();
626 vlc_mutex_lock( &p_mgr->lock );
627 count = p_mgr->extensions.i_size;
628 vlc_mutex_unlock( &p_mgr->lock );
629 vlc_object_release( p_mgr );
634 QVariant ExtensionListModel::data( const QModelIndex& index, int role ) const
636 if( !index.isValid() )
639 ExtensionCopy * extension =
640 static_cast<ExtensionCopy *>(index.internalPointer());
642 return extension->data( role );
645 QModelIndex ExtensionListModel::index( int row, int column,
646 const QModelIndex& ) const
649 return QModelIndex();
650 if( row < 0 || row >= extensions.count() )
651 return QModelIndex();
653 return createIndex( row, 0, extensions.at( row ) );
656 AddonsListModel::Addon::Addon( addon_entry_t *p_entry_ )
659 addon_entry_Hold( p_entry );
662 AddonsListModel::Addon::~Addon()
664 addon_entry_Release( p_entry );
667 bool AddonsListModel::Addon::operator==( const Addon & other ) const
669 //return data( IDRole ) == other.data( IDRole );
670 return p_entry == other.p_entry;
673 bool AddonsListModel::Addon::operator==( const addon_entry_t * p_other ) const
675 return p_entry == p_other;
678 QVariant AddonsListModel::Addon::data( int role ) const
682 vlc_mutex_lock( &p_entry->lock );
685 case Qt::DisplayRole:
687 QString name = qfu( p_entry->psz_name );
688 if ( p_entry->e_state == ADDON_INSTALLED )
689 name.append( QString(" (%1)").arg( qtr("installed") ) );
694 case Qt::DecorationRole:
695 if ( p_entry->psz_image_data )
698 pixmap.loadFromData( QByteArray::fromBase64( QByteArray( p_entry->psz_image_data ) ),
704 else if ( p_entry->e_flags & ADDON_BROKEN )
705 returnval = QPixmap( ":/addons/broken" );
707 returnval = QPixmap( ":/addons/default" );
709 case Qt::ToolTipRole:
711 if ( !( p_entry->e_flags & ADDON_MANAGEABLE ) )
713 returnval = qtr("This addon has been installed manually. VLC can't manage it by itself.");
718 returnval = qfu( p_entry->psz_summary );
720 case DescriptionRole:
721 returnval = qfu( p_entry->psz_description );
724 returnval = QVariant( (int) p_entry->e_type );
727 returnval = QByteArray( (const char *) p_entry->uuid, (int) sizeof( addon_uuid_t ) );
730 returnval = QVariant( (int) p_entry->e_flags );
733 returnval = QVariant( (int) p_entry->e_state );
735 case DownloadsCountRole:
736 returnval = QVariant( (double) p_entry->i_downloads );
739 returnval = QVariant( (double) p_entry->i_score );
742 returnval = QVariant( p_entry->psz_version );
745 returnval = qfu( p_entry->psz_author );
748 returnval = qfu( p_entry->psz_source_uri );
753 FOREACH_ARRAY( addon_file_t *p_file, p_entry->files )
754 list << qfu( p_file->psz_filename );
756 returnval = QVariant( list );
762 vlc_mutex_unlock( &p_entry->lock );
767 AddonsListModel::AddonsListModel( AddonsManager *AM_, QObject *parent )
768 :ExtensionListModel( parent ), AM( AM_ )
773 void AddonsListModel::addonAdded( addon_entry_t *p_entry )
775 beginInsertRows( QModelIndex(), addons.count(), addons.count() );
776 addons << new Addon( p_entry );
777 insertRow( addons.count() - 1 );
781 void AddonsListModel::addonChanged( const addon_entry_t *p_entry )
784 foreach ( const Addon *addon, addons )
786 if ( *addon == p_entry )
788 emit dataChanged( index( row, 0 ), index( row, 0 ) );
795 int AddonsListModel::rowCount( const QModelIndex & ) const
797 return addons.count();
800 Qt::ItemFlags AddonsListModel::flags( const QModelIndex &index ) const
802 Qt::ItemFlags i_flags = ExtensionListModel::flags( index );
803 int i_state = data( index, StateRole ).toInt();
805 if ( i_state == ADDON_UNINSTALLING || i_state == ADDON_INSTALLING )
807 i_flags &= !Qt::ItemIsEnabled;
810 i_flags |= Qt::ItemIsEditable;
815 bool AddonsListModel::setData( const QModelIndex &index, const QVariant &value, int role )
817 /* We NEVER set values directly */
818 if ( role == StateRole )
820 int i_value = value.toInt();
821 if ( i_value == ADDON_INSTALLING )
823 AM->install( data( index, UUIDRole ).toByteArray() );
825 else if ( i_value == ADDON_UNINSTALLING )
827 AM->remove( data( index, UUIDRole ).toByteArray() );
830 else if ( role == StateRole + 1 )
832 emit dataChanged( index, index );
837 QVariant AddonsListModel::data( const QModelIndex& index, int role ) const
839 if( !index.isValid() )
842 return ((Addon *)index.internalPointer())->data( role );
845 QModelIndex AddonsListModel::index( int row, int column,
846 const QModelIndex& ) const
849 return QModelIndex();
850 if( row < 0 || row >= addons.count() )
851 return QModelIndex();
853 return createIndex( row, 0, addons.at( row ) );
857 AddonsSortFilterProxyModel::AddonsSortFilterProxyModel( QObject *parent )
858 : QSortFilterProxyModel( parent )
864 void AddonsSortFilterProxyModel::setTypeFilter( int type )
866 i_type_filter = type;
870 void AddonsSortFilterProxyModel::setStatusFilter( int flags )
872 i_status_filter = flags;
876 bool AddonsSortFilterProxyModel::filterAcceptsRow( int source_row,
877 const QModelIndex &source_parent ) const
879 if ( !QSortFilterProxyModel::filterAcceptsRow( source_row, source_parent ) )
882 QModelIndex item = sourceModel()->index( source_row, 0, source_parent );
884 if ( i_type_filter > -1 &&
885 item.data( AddonsListModel::TypeRole ).toInt() != i_type_filter )
888 if ( i_status_filter > 0 &&
889 ( item.data( AddonsListModel::StateRole ).toInt() & i_status_filter ) != i_status_filter )
895 /* Extension List Widget Item */
896 ExtensionItemDelegate::ExtensionItemDelegate( QObject *parent )
897 : QStyledItemDelegate( parent )
899 margins = QMargins( 4, 4, 4, 4 );
902 ExtensionItemDelegate::~ExtensionItemDelegate()
906 void ExtensionItemDelegate::paint( QPainter *painter,
907 const QStyleOptionViewItem &option,
908 const QModelIndex &index ) const
910 QStyleOptionViewItemV4 opt = option;
911 initStyleOption( &opt, index );
914 if ( opt.state & QStyle::State_Selected )
915 painter->fillRect( opt.rect, opt.palette.highlight() );
918 QPixmap icon = index.data( Qt::DecorationRole ).value<QPixmap>();
921 painter->drawPixmap( opt.rect.left() + margins.left(),
922 opt.rect.top() + margins.top(),
923 icon.scaled( opt.decorationSize,
925 Qt::SmoothTransformation )
930 painter->setRenderHint( QPainter::TextAntialiasing );
932 if ( opt.state & QStyle::State_Selected )
933 painter->setPen( opt.palette.highlightedText().color() );
935 QFont font( option.font );
936 font.setBold( true );
937 painter->setFont( font );
938 QRect textrect( opt.rect );
939 textrect.adjust( 2 * margins.left() + margins.right() + opt.decorationSize.width(),
942 - margins.bottom() - opt.fontMetrics.height() );
944 painter->drawText( textrect, Qt::AlignLeft,
945 index.data( Qt::DisplayRole ).toString() );
947 font.setBold( false );
948 painter->setFont( font );
949 painter->drawText( textrect.translated( 0, option.fontMetrics.height() ),
951 index.data( ExtensionListModel::SummaryRole ).toString() );
956 QSize ExtensionItemDelegate::sizeHint( const QStyleOptionViewItem &option,
957 const QModelIndex &index ) const
959 if ( index.isValid() )
961 return QSize( 200, 2 * option.fontMetrics.height()
962 + margins.top() + margins.bottom() );
968 void ExtensionItemDelegate::initStyleOption( QStyleOptionViewItem *option,
969 const QModelIndex &index ) const
971 QStyledItemDelegate::initStyleOption( option, index );
972 option->decorationSize = QSize( option->rect.height(), option->rect.height() );
973 option->decorationSize -= QSize( margins.left() + margins.right(),
974 margins.top() + margins.bottom() );
977 AddonItemDelegate::AddonItemDelegate( QObject *parent )
978 : ExtensionItemDelegate( parent )
984 AddonItemDelegate::~AddonItemDelegate()
989 void AddonItemDelegate::paint( QPainter *painter,
990 const QStyleOptionViewItem &option,
991 const QModelIndex &index ) const
993 QStyleOptionViewItemV4 newopt = option;
994 int i_state = index.data( AddonsListModel::StateRole ).toInt();
996 ExtensionItemDelegate::paint( painter, newopt, index );
998 initStyleOption( &newopt, index );
1001 painter->setRenderHint( QPainter::TextAntialiasing );
1003 if ( newopt.state & QStyle::State_Selected )
1004 painter->setPen( newopt.palette.highlightedText().color() );
1006 /* Start below text */
1007 QRect textrect( newopt.rect );
1008 textrect.adjust( 2 * margins.left() + margins.right() + newopt.decorationSize.width(),
1011 - margins.bottom() - newopt.fontMetrics.height() );
1012 textrect.translate( 0, newopt.fontMetrics.height() * 2 );
1015 QString version = index.data( AddonsListModel::VersionRole ).toString();
1016 if ( !version.isEmpty() )
1017 painter->drawText( textrect, Qt::AlignLeft, qtr("Version %1").arg( version ) );
1019 textrect.translate( 0, newopt.fontMetrics.height() );
1022 double i_score = index.data( AddonsListModel::ScoreRole ).toDouble();
1026 scoreicon = QPixmap( ":/addons/score" ).scaledToHeight(
1027 newopt.fontMetrics.height(), Qt::SmoothTransformation );
1028 int i_width = ( i_score / 5.0 ) * scoreicon.width();
1029 /* Erase the end (value) of our pixmap with a shadow */
1030 QPainter erasepainter( &scoreicon );
1031 erasepainter.setCompositionMode( QPainter::CompositionMode_SourceIn );
1032 erasepainter.fillRect( QRect( i_width, 0,
1033 scoreicon.width() - i_width, scoreicon.height() ),
1034 newopt.palette.color( QPalette::Dark ) );
1036 painter->drawPixmap( textrect.topLeft(), scoreicon );
1040 int i_downloads = index.data( AddonsListModel::DownloadsCountRole ).toInt();
1042 painter->drawText( textrect.translated( scoreicon.width() + margins.left(), 0 ),
1043 Qt::AlignLeft, qtr("%1 downloads").arg( i_downloads ) );
1049 if ( animator->isRunning() && animator->getIndex() == index )
1051 if ( i_state != ADDON_INSTALLING && i_state != ADDON_UNINSTALLING )
1052 animator->run( false );
1054 /* Create our installation progress overlay */
1056 if ( i_state == ADDON_INSTALLING || i_state == ADDON_UNINSTALLING )
1059 painter->setCompositionMode( QPainter::CompositionMode_SourceOver );
1060 painter->fillRect( newopt.rect, QColor( 255, 255, 255, 128 ) );
1061 if ( animator && index.isValid() )
1063 animator->setIndex( index );
1064 animator->run( true );
1065 QSize adjustment = newopt.rect.size() / 4;
1066 progressbar->setGeometry(
1067 newopt.rect.adjusted( adjustment.width(), adjustment.height(),
1068 -adjustment.width(), -adjustment.height() ) );
1069 painter->drawPixmap( newopt.rect.left() + adjustment.width(),
1070 newopt.rect.top() + adjustment.height(),
1071 QPixmap::grabWidget( progressbar ) );
1078 QSize AddonItemDelegate::sizeHint( const QStyleOptionViewItem &option,
1079 const QModelIndex &index ) const
1081 if ( index.isValid() )
1083 return QSize( 200, 4 * option.fontMetrics.height()
1084 + margins.top() + margins.bottom() );
1090 QWidget *AddonItemDelegate::createEditor( QWidget *parent,
1091 const QStyleOptionViewItem &option,
1092 const QModelIndex &index) const
1095 QWidget *editorWidget = new QWidget( parent );
1096 QPushButton *installButton;
1097 QPushButton *infoButton;
1099 editorWidget->setLayout( new QHBoxLayout() );
1100 editorWidget->layout()->setMargin( 0 );
1102 infoButton = new QPushButton( QIcon( ":/menu/info" ),
1103 qtr( "More information..." ) );
1104 connect( infoButton, SIGNAL(clicked()), this, SIGNAL(showInfo()) );
1105 editorWidget->layout()->addWidget( infoButton );
1107 if ( ADDON_MANAGEABLE &
1108 index.data( AddonsListModel::FlagsRole ).toInt() )
1110 if ( index.data( AddonsListModel::StateRole ).toInt() == ADDON_INSTALLED )
1111 installButton = new QPushButton( QIcon( ":/buttons/playlist/playlist_remove" ),
1112 qtr("&Uninstall"), parent );
1114 installButton = new QPushButton( QIcon( ":/buttons/playlist/playlist_add" ),
1115 qtr("&Install"), parent );
1116 CONNECT( installButton, clicked(), this, editButtonClicked() );
1117 editorWidget->layout()->addWidget( installButton );
1120 editorWidget->setSizePolicy( QSizePolicy::Maximum, QSizePolicy::Preferred );
1122 return editorWidget;
1125 void AddonItemDelegate::updateEditorGeometry( QWidget *editor,
1126 const QStyleOptionViewItem &option,
1127 const QModelIndex &index) const
1130 QSize size = editor->sizeHint();
1131 editor->setGeometry( option.rect.right() - size.width(),
1132 option.rect.top() + ( option.rect.height() - size.height()),
1137 void AddonItemDelegate::setModelData( QWidget *editor, QAbstractItemModel *model,
1138 const QModelIndex &index ) const
1140 model->setData( index, editor->property("Addon::state"), AddonsListModel::StateRole );
1143 void AddonItemDelegate::setEditorData( QWidget *editor, const QModelIndex &index ) const
1145 editor->setProperty("Addon::state", index.data( AddonsListModel::StateRole ) );
1148 void AddonItemDelegate::setAnimator( DelegateAnimationHelper *animator_ )
1152 QProgressBar *progress = new QProgressBar( );
1153 progress->setMinimum( 0 );
1154 progress->setMaximum( 0 );
1155 progress->setTextVisible( false );
1156 progressbar = progress;
1158 animator = animator_;
1161 void AddonItemDelegate::editButtonClicked()
1163 QWidget *editor = qobject_cast<QWidget *>(sender()->parent());
1164 int value = editor->property("Addon::state").toInt();
1165 if ( ( value == ADDON_INSTALLED ) )
1167 editor->setProperty("Addon::state", ADDON_UNINSTALLING );
1170 editor->setProperty("Addon::state", ADDON_INSTALLING );
1171 emit commitData( editor );
1172 emit closeEditor( editor );
1175 /* "More information" dialog */
1177 ExtensionInfoDialog::ExtensionInfoDialog( const QModelIndex &index,
1178 intf_thread_t *p_intf,
1180 : QVLCDialog( parent, p_intf )
1182 // Let's be a modal dialog
1183 setWindowModality( Qt::WindowModal );
1186 setWindowTitle( qtr( "About" ) + " " + index.data(Qt::DisplayRole).toString() );
1189 QGridLayout *layout = new QGridLayout( this );
1192 QLabel *icon = new QLabel( this );
1193 QPixmap pix = index.data(Qt::DecorationRole).value<QPixmap>();
1194 Q_ASSERT( !pix.isNull() );
1195 icon->setPixmap( pix );
1196 icon->setAlignment( Qt::AlignCenter );
1197 icon->setFixedSize( 48, 48 );
1198 layout->addWidget( icon, 1, 0, 2, 1 );
1201 QLabel *label = new QLabel( index.data(Qt::DisplayRole).toString(), this );
1202 QFont font = label->font();
1203 font.setBold( true );
1204 font.setPointSizeF( font.pointSizeF() * 1.3f );
1205 label->setFont( font );
1206 layout->addWidget( label, 0, 0, 1, -1 );
1209 label = new QLabel( "<b>" + qtr( "Version" ) + ":</b>", this );
1210 layout->addWidget( label, 1, 1, 1, 1, Qt::AlignBottom );
1211 label = new QLabel( index.data(ExtensionListModel::VersionRole).toString(), this );
1212 layout->addWidget( label, 1, 2, 1, 2, Qt::AlignBottom );
1215 label = new QLabel( "<b>" + qtr( "Author" ) + ":</b>", this );
1216 layout->addWidget( label, 2, 1, 1, 1, Qt::AlignTop );
1217 label = new QLabel( index.data(ExtensionListModel::AuthorRole).toString(), this );
1218 layout->addWidget( label, 2, 2, 1, 2, Qt::AlignTop );
1222 label = new QLabel( this );
1223 label->setText( index.data(ExtensionListModel::SummaryRole).toString() );
1224 label->setWordWrap( true );
1225 label->setOpenExternalLinks( true );
1226 layout->addWidget( label, 4, 0, 1, -1 );
1229 label = new QLabel( "<b>" + qtr( "Website" ) + ":</b>", this );
1230 layout->addWidget( label, 5, 0, 1, 2 );
1231 label = new QLabel( QString("<a href=\"%1\">%2</a>")
1232 .arg( index.data(ExtensionListModel::LinkRole).toString() )
1233 .arg( index.data(ExtensionListModel::LinkRole).toString() )
1235 label->setOpenExternalLinks( true );
1236 layout->addWidget( label, 5, 2, 1, -1 );
1239 label = new QLabel( "<b>" + qtr( "File" ) + ":</b>", this );
1240 layout->addWidget( label, 6, 0, 1, 2 );
1242 new QLineEdit( index.data(ExtensionListModel::FilenameRole).toString(), this );
1243 line->setReadOnly( true );
1244 layout->addWidget( line, 6, 2, 1, -1 );
1247 QDialogButtonBox *group = new QDialogButtonBox( this );
1248 QPushButton *closeButton = new QPushButton( qtr( "&Close" ) );
1249 group->addButton( closeButton, QDialogButtonBox::RejectRole );
1250 BUTTONACT( closeButton, close() );
1252 layout->addWidget( group, 7, 0, 1, -1 );
1255 layout->setColumnStretch( 2, 1 );
1256 layout->setRowStretch( 4, 1 );
1257 setMinimumSize( 450, 350 );
1261 AddonInfoDialog::AddonInfoDialog( const QModelIndex &index,
1262 intf_thread_t *p_intf, QWidget *parent )
1263 : QVLCDialog( parent, p_intf )
1265 // Let's be a modal dialog
1266 setWindowModality( Qt::WindowModal );
1269 setWindowTitle( qtr( "About" ) + " " + index.data(Qt::DisplayRole).toString() );
1272 QGridLayout *layout = new QGridLayout( this );
1276 QLabel *iconLabel = new QLabel( this );
1277 iconLabel->setFixedSize( 100, 100 );
1278 QPixmap icon = index.data( Qt::DecorationRole ).value<QPixmap>();
1279 icon.scaled( iconLabel->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation );
1280 iconLabel->setPixmap( icon );
1281 iconLabel->setAlignment( Qt::AlignCenter | Qt::AlignTop );
1282 layout->addWidget( iconLabel, 1, 0, 2, 1 );
1285 label = new QLabel( index.data(Qt::DisplayRole).toString(), this );
1286 QFont font = label->font();
1287 font.setBold( true );
1288 font.setPointSizeF( font.pointSizeF() * 1.3f );
1289 label->setFont( font );
1290 layout->addWidget( label, 0, 0, 1, -1 );
1292 // HTML Content on right side
1293 QTextEdit *textContent = new QTextEdit();
1294 textContent->viewport()->setAutoFillBackground( false );
1295 textContent->setAcceptRichText( true );
1296 textContent->setBackgroundRole( QPalette::Window );
1297 textContent->setFrameStyle( QFrame::NoFrame );
1298 textContent->setAutoFillBackground( false );
1299 textContent->setReadOnly( true );
1300 layout->addWidget( textContent, 1, 1, 4, -1 );
1303 QString type = AddonsManager::getAddonType( index.data(AddonsListModel::TypeRole).toInt() );
1304 textContent->append( QString("<b>%1:</b> %2<br/>")
1305 .arg( qtr("Type") ).arg( type ) );
1308 QString version = index.data(ExtensionListModel::VersionRole).toString();
1309 if ( !version.isEmpty() )
1311 textContent->append( QString("<b>%1:</b> %2<br/>")
1312 .arg( qtr("Version") ).arg( version ) );
1316 QString author = index.data(ExtensionListModel::AuthorRole).toString();
1317 if ( !author.isEmpty() )
1319 textContent->append( QString("<b>%1:</b> %2<br/>")
1320 .arg( qtr("Author") ).arg( author ) );
1324 textContent->append( QString("%1<br/>\n")
1325 .arg( index.data(AddonsListModel::SummaryRole).toString() ) );
1328 QString description = index.data(AddonsListModel::DescriptionRole).toString();
1329 if ( !description.isEmpty() )
1331 textContent->append( QString("<hr/>\n%1")
1332 .arg( description.replace("\n", "<br/>") ) );
1336 QString sourceUrl = index.data(ExtensionListModel::LinkRole).toString();
1337 if ( !sourceUrl.isEmpty() )
1339 label = new QLabel( "<b>" + qtr( "Website" ) + ":</b>", this );
1340 layout->addWidget( label, 5, 0, 1, 2 );
1341 label = new QLabel( QString("<a href=\"%1\">%2</a>")
1342 .arg( sourceUrl ).arg( sourceUrl ), this );
1343 label->setOpenExternalLinks( true );
1344 layout->addWidget( label, 5, 2, 1, -1 );
1348 QList<QVariant> list = index.data(ExtensionListModel::FilenameRole).toList();
1349 if ( ! list.empty() )
1351 label = new QLabel( "<b>" + qtr( "Files" ) + ":</b>", this );
1352 layout->addWidget( label, 6, 0, 1, 2 );
1353 QComboBox *filesCombo = new QComboBox();
1354 Q_FOREACH( const QVariant & file, list )
1355 filesCombo->addItem( file.toString() );
1356 layout->addWidget( filesCombo, 6, 2, 1, -1 );
1360 QDialogButtonBox *group = new QDialogButtonBox( this );
1361 QPushButton *closeButton = new QPushButton( qtr( "&Close" ) );
1362 group->addButton( closeButton, QDialogButtonBox::RejectRole );
1363 BUTTONACT( closeButton, close() );
1365 layout->addWidget( group, 7, 0, 1, -1 );
1368 layout->setColumnStretch( 2, 1 );
1369 layout->setRowStretch( 4, 1 );
1370 setMinimumSize( 640, 480 );
1373 static QPixmap *loadPixmapFromData( char *data, int size )
1375 if( !data || size <= 0 )
1377 QPixmap *pixmap = new QPixmap();
1378 if( !pixmap->loadFromData( (const uchar*) data, (uint) size ) )