]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs/plugins.cpp
qt4: rename a couple of methods to be more explicit
[vlc] / modules / gui / qt4 / dialogs / plugins.cpp
1 /*****************************************************************************
2  * plugins.cpp : Plug-ins and extensions listing
3  ****************************************************************************
4  * Copyright (C) 2008-2010 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Jean-Baptiste Kempf <jb (at) videolan.org>
8  *          Jean-Philippe AndrĂ© <jpeg (at) videolan.org>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include "plugins.hpp"
30
31 #include "util/searchlineedit.hpp"
32 #include "extensions_manager.hpp"
33
34 #include <assert.h>
35
36 #include <vlc_modules.h>
37
38 #include <QTreeWidget>
39 #include <QStringList>
40 #include <QTabWidget>
41 #include <QHeaderView>
42 #include <QDialogButtonBox>
43 #include <QLineEdit>
44 #include <QLabel>
45 #include <QVBoxLayout>
46 #include <QComboBox>
47 #include <QHBoxLayout>
48 #include <QVBoxLayout>
49 #include <QSpacerItem>
50 #include <QListView>
51 #include <QPainter>
52 #include <QStyleOptionViewItem>
53 #include <QKeyEvent>
54 #include <QPushButton>
55 #include <QPixmap>
56
57 static QPixmap *loadPixmapFromData( char *, int size );
58
59
60 PluginDialog::PluginDialog( intf_thread_t *_p_intf ) : QVLCFrame( _p_intf )
61 {
62     setWindowTitle( qtr( "Plugins and extensions" ) );
63     setWindowRole( "vlc-plugins" );
64
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 ),
70                   qtr( "Plugins" ) );
71     layout->addWidget( tabs );
72
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 ) );
79 }
80
81 PluginDialog::~PluginDialog()
82 {
83     saveWidgetPosition( "PluginsDialog" );
84 }
85
86 /* Plugins tab */
87
88 PluginTab::PluginTab( intf_thread_t *p_intf_ )
89         : QVLCFrame( p_intf_ )
90 {
91     QGridLayout *layout = new QGridLayout( this );
92
93     /* Main Tree for modules */
94     treePlugins = new QTreeWidget;
95     layout->addWidget( treePlugins, 0, 0, 1, -1 );
96
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 );
103
104     QStringList headerNames;
105     headerNames << qtr("Name") << qtr("Capability" ) << qtr( "Score" );
106     treePlugins->setHeaderLabels( headerNames );
107
108     FillTree();
109
110     /* Set capability column to the correct Size*/
111     treePlugins->resizeColumnToContents( 1 );
112     treePlugins->header()->restoreState(
113             getSettings()->value( "Plugins/Header-State" ).toByteArray() );
114
115     treePlugins->setSortingEnabled( true );
116     treePlugins->sortByColumn( 1, Qt::AscendingOrder );
117
118     QLabel *label = new QLabel( qtr("&Search:"), this );
119     edit = new SearchLineEdit( this );
120     label->setBuddy( edit );
121
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& ) );
126
127     setMinimumSize( 500, 300 );
128     restoreWidgetPosition( "Plugins", QSize( 540, 400 ) );
129 }
130
131 inline void PluginTab::FillTree()
132 {
133     module_t **p_list = module_list_get( NULL );
134     module_t *p_module;
135
136     for( unsigned int i = 0; (p_module = p_list[i] ) != NULL; i++ )
137     {
138         QStringList qs_item;
139         qs_item << qfu( module_get_name( p_module, true ) )
140                 << qfu( module_get_capability( p_module ) )
141                 << QString::number( module_get_score( p_module ) );
142 #ifndef DEBUG
143         if( qs_item.at(1).isEmpty() ) continue;
144 #endif
145
146         QTreeWidgetItem *item = new PluginTreeItem( qs_item );
147         treePlugins->addTopLevelItem( item );
148     }
149     module_list_free( p_list );
150 }
151
152 void PluginTab::search( const QString& qs )
153 {
154     QList<QTreeWidgetItem *> items = treePlugins->findItems( qs, Qt::MatchContains );
155     items += treePlugins->findItems( qs, Qt::MatchContains, 1 );
156
157     QTreeWidgetItem *item = NULL;
158     for( int i = 0; i < treePlugins->topLevelItemCount(); i++ )
159     {
160         item = treePlugins->topLevelItem( i );
161         item->setHidden( !items.contains( item ) );
162     }
163 }
164
165 PluginTab::~PluginTab()
166 {
167     saveWidgetPosition( "Plugins" );
168     getSettings()->setValue( "Plugins/Header-State",
169                              treePlugins->header()->saveState() );
170 }
171
172 void PluginTab::keyPressEvent( QKeyEvent *keyEvent )
173 {
174     if( keyEvent->key() == Qt::Key_Return ||
175         keyEvent->key() == Qt::Key_Enter )
176         keyEvent->accept();
177     else
178         keyEvent->ignore();
179 }
180
181 bool PluginTreeItem::operator< ( const QTreeWidgetItem & other ) const
182 {
183     int col = treeWidget()->sortColumn();
184     if( col == 2 )
185         return text( col ).toInt() < other.text( col ).toInt();
186     return text( col ) < other.text( col );
187 }
188
189 /* Extensions tab */
190 ExtensionTab::ExtensionTab( intf_thread_t *p_intf_ )
191         : QVLCFrame( p_intf_ )
192 {
193     // Layout
194     QVBoxLayout *layout = new QVBoxLayout( this );
195
196     QLabel *notice = new QLabel( qtr("Get more extensions from")
197             + QString( " <a href=\"http://addons.videolan.org/\">"
198                        "addons.videolan.org</a>." ) );
199     notice->setOpenExternalLinks( true );
200     layout->addWidget( notice );
201
202     // ListView
203     extList = new QListView( this );
204     CONNECT( extList, activated( const QModelIndex& ),
205              this, moreInformation() );
206     layout->addWidget( extList );
207
208     // List item delegate
209     ExtensionItemDelegate *itemDelegate = new ExtensionItemDelegate( p_intf,
210                                                                      extList );
211     extList->setItemDelegate( itemDelegate );
212
213     // Extension list look & feeling
214     extList->setAlternatingRowColors( true );
215     extList->setSelectionMode( QAbstractItemView::SingleSelection );
216
217     // Model
218     ExtensionListModel *model = new ExtensionListModel( extList, p_intf );
219     extList->setModel( model );
220
221     // Buttons' layout
222     QHBoxLayout *hbox = new QHBoxLayout;
223     hbox->addItem( new QSpacerItem( 1, 1, QSizePolicy::Expanding,
224                                     QSizePolicy::Fixed ) );
225
226     // More information button
227     butMoreInfo = new QPushButton( QIcon( ":/menu/info" ),
228                                    qtr( "More information..." ),
229                                    this );
230     CONNECT( butMoreInfo, clicked(), this, moreInformation() );
231     hbox->addWidget( butMoreInfo );
232
233     // Reload button
234     ExtensionsManager *EM = ExtensionsManager::getInstance( p_intf );
235     QPushButton *reload = new QPushButton( QIcon( ":/update" ),
236                                            qtr( "Reload extensions" ),
237                                            this );
238     CONNECT( reload, clicked(), EM, reloadExtensions() );
239     CONNECT( reload, clicked(), this, updateButtons() );
240     CONNECT( extList->selectionModel(),
241              selectionChanged( const QItemSelection &, const QItemSelection & ),
242              this,
243              updateButtons() );
244     hbox->addWidget( reload );
245
246     // Add buttons hbox
247     layout->addItem( hbox );
248     updateButtons();
249 }
250
251 ExtensionTab::~ExtensionTab()
252 {
253 }
254
255 void ExtensionTab::updateButtons()
256 {
257     butMoreInfo->setEnabled( extList->selectionModel()->hasSelection() );
258 }
259
260 // Do not close on ESC or ENTER
261 void ExtensionTab::keyPressEvent( QKeyEvent *keyEvent )
262 {
263     if( keyEvent->key() == Qt::Key_Return ||
264         keyEvent->key() == Qt::Key_Enter )
265         keyEvent->accept();
266     else
267         keyEvent->ignore();
268 }
269
270 // Show more information
271 void ExtensionTab::moreInformation()
272 {
273     QModelIndex index = extList->selectionModel()->selectedIndexes().first();
274     ExtensionCopy *ext = (ExtensionCopy*) index.internalPointer();
275     if( !ext )
276         return;
277
278     ExtensionInfoDialog dlg( *ext, p_intf, this );
279     dlg.exec();
280 }
281
282 /* Safe copy of the extension_t struct */
283 class ExtensionCopy
284 {
285 public:
286     ExtensionCopy( extension_t *p_ext )
287     {
288         name = qfu( p_ext->psz_name );
289         description = qfu( p_ext->psz_description );
290         shortdesc = qfu( p_ext->psz_shortdescription );
291         if( description.isEmpty() )
292             description = shortdesc;
293         if( shortdesc.isEmpty() && !description.isEmpty() )
294             shortdesc = description;
295         title = qfu( p_ext->psz_title );
296         author = qfu( p_ext->psz_author );
297         version = qfu( p_ext->psz_version );
298         url = qfu( p_ext->psz_url );
299         icon = loadPixmapFromData( p_ext->p_icondata, p_ext->i_icondata_size );
300     }
301     ~ExtensionCopy() {}
302
303     QString name, title, description, shortdesc, author, version, url;
304     QPixmap *icon;
305 };
306
307 /* Extensions list model for the QListView */
308
309 ExtensionListModel::ExtensionListModel( QListView *view, intf_thread_t *intf )
310         : QAbstractListModel( view ), p_intf( intf )
311 {
312     // Connect to ExtensionsManager::extensionsUpdated()
313     ExtensionsManager* EM = ExtensionsManager::getInstance( p_intf );
314     CONNECT( EM, extensionsUpdated(), this, updateList() );
315
316     // Load extensions now if not already loaded
317     EM->loadExtensions();
318 }
319
320 ExtensionListModel::~ExtensionListModel()
321 {
322     // Clear extensions list
323     while( !extensions.isEmpty() )
324         delete extensions.takeLast();
325 }
326
327 void ExtensionListModel::updateList()
328 {
329     ExtensionCopy *ext;
330
331     // Clear extensions list
332     while( !extensions.isEmpty() )
333     {
334         ext = extensions.takeLast();
335         delete ext;
336     }
337
338     // Find new extensions
339     ExtensionsManager *EM = ExtensionsManager::getInstance( p_intf );
340     extensions_manager_t *p_mgr = EM->getManager();
341     if( !p_mgr )
342         return;
343
344     vlc_mutex_lock( &p_mgr->lock );
345     extension_t *p_ext;
346     FOREACH_ARRAY( p_ext, p_mgr->extensions )
347     {
348         ext = new ExtensionCopy( p_ext );
349         extensions.append( ext );
350     }
351     FOREACH_END()
352     vlc_mutex_unlock( &p_mgr->lock );
353     vlc_object_release( p_mgr );
354
355     emit dataChanged( index( 0 ), index( rowCount() - 1 ) );
356 }
357
358 int ExtensionListModel::rowCount( const QModelIndex& ) const
359 {
360     int count = 0;
361     ExtensionsManager *EM = ExtensionsManager::getInstance( p_intf );
362     extensions_manager_t *p_mgr = EM->getManager();
363     if( !p_mgr )
364         return 0;
365
366     vlc_mutex_lock( &p_mgr->lock );
367     count = p_mgr->extensions.i_size;
368     vlc_mutex_unlock( &p_mgr->lock );
369     vlc_object_release( p_mgr );
370
371     return count;
372 }
373
374 QVariant ExtensionListModel::data( const QModelIndex& index, int role ) const
375 {
376     if( !index.isValid() )
377         return QVariant();
378
379     switch( role )
380     {
381     default:
382         return QVariant();
383     }
384 }
385
386 QModelIndex ExtensionListModel::index( int row, int column,
387                                        const QModelIndex& ) const
388 {
389     if( column != 0 )
390         return QModelIndex();
391     if( row < 0 || row >= extensions.count() )
392         return QModelIndex();
393
394     return createIndex( row, 0, extensions.at( row ) );
395 }
396
397
398 /* Extension List Widget Item */
399 ExtensionItemDelegate::ExtensionItemDelegate( intf_thread_t *p_intf,
400                                               QListView *view )
401         : QStyledItemDelegate( view ), view( view ), p_intf( p_intf )
402 {
403 }
404
405 ExtensionItemDelegate::~ExtensionItemDelegate()
406 {
407 }
408
409 void ExtensionItemDelegate::paint( QPainter *painter,
410                                    const QStyleOptionViewItem &option,
411                                    const QModelIndex &index ) const
412 {
413     ExtensionCopy *ext = ( ExtensionCopy* ) index.internalPointer();
414     assert( ext != NULL );
415
416     int width = option.rect.width();
417
418     // Pixmap: buffer where to draw
419     QPixmap pix(option.rect.size());
420
421     // Draw background
422     pix.fill( Qt::transparent ); // FIXME
423
424     // ItemView primitive style
425     QApplication::style()->drawPrimitive( QStyle::PE_PanelItemViewItem,
426                                           &option,
427                                           painter );
428
429     // Painter on the pixmap
430     QPainter *pixpaint = new QPainter(&pix);
431
432     // Text font & pen
433     QFont font = painter->font();
434     QPen pen = painter->pen();
435     if( view->selectionModel()->selectedIndexes().contains( index ) )
436     {
437         pen.setBrush( option.palette.highlightedText() );
438     }
439     else
440     {
441         pen.setBrush( option.palette.text() );
442     }
443     pixpaint->setPen( pen );
444     QFontMetrics metrics = option.fontMetrics;
445
446     // Icon
447     if( ext->icon != NULL )
448     {
449         pixpaint->drawPixmap( 7, 7, 2*metrics.height(), 2*metrics.height(),
450                               *ext->icon );
451     }
452
453     // Title: bold
454     pixpaint->setRenderHint( QPainter::TextAntialiasing );
455     font.setBold( true );
456     pixpaint->setFont( font );
457     pixpaint->drawText( QRect( 17 + 2 * metrics.height(), 7,
458                                width - 40 - 2 * metrics.height(),
459                                metrics.height() ),
460                         Qt::AlignLeft, ext->title );
461
462     // Short description: normal
463     font.setBold( false );
464     pixpaint->setFont( font );
465     pixpaint->drawText( QRect( 17 + 2 * metrics.height(),
466                                7 + metrics.height(), width - 40,
467                                metrics.height() ),
468                         Qt::AlignLeft, ext->shortdesc );
469
470     // Flush paint operations
471     delete pixpaint;
472
473     // Draw it on the screen!
474     painter->drawPixmap( option.rect, pix );
475 }
476
477 QSize ExtensionItemDelegate::sizeHint( const QStyleOptionViewItem &option,
478                                        const QModelIndex &index ) const
479 {
480     if (index.isValid() && index.column() == 0)
481     {
482         QFontMetrics metrics = option.fontMetrics;
483         return QSize( 200, 14 + 2 * metrics.height() );
484     }
485     else
486         return QSize();
487 }
488
489 /* "More information" dialog */
490
491 ExtensionInfoDialog::ExtensionInfoDialog( const ExtensionCopy& extension,
492                                           intf_thread_t *p_intf,
493                                           QWidget *parent )
494        : QVLCDialog( parent, p_intf )
495 {
496     // Let's be a modal dialog
497     setWindowModality( Qt::WindowModal );
498
499     // Window title
500     setWindowTitle( qtr( "About" ) + " " + extension.title );
501
502     // Layout
503     QGridLayout *layout = new QGridLayout( this );
504
505     // Icon
506     QLabel *icon = new QLabel( this );
507     if( !extension.icon )
508     {
509         QPixmap pix( ":/logo/vlc48.png" );
510         icon->setPixmap( pix );
511     }
512     else
513     {
514         icon->setPixmap( *extension.icon );
515     }
516     icon->setAlignment( Qt::AlignCenter );
517     icon->setFixedSize( 48, 48 );
518     layout->addWidget( icon, 1, 0, 2, 1 );
519
520     // Title
521     QLabel *label = new QLabel( extension.title, this );
522     QFont font = label->font();
523     font.setBold( true );
524     font.setPointSizeF( font.pointSizeF() * 1.3f );
525     label->setFont( font );
526     layout->addWidget( label, 0, 0, 1, -1 );
527
528     // Version
529     label = new QLabel( "<b>" + qtr( "Version" ) + ":</b>", this );
530     layout->addWidget( label, 1, 1, 1, 1, Qt::AlignBottom );
531     label = new QLabel( extension.version, this );
532     layout->addWidget( label, 1, 2, 1, 2, Qt::AlignBottom );
533
534     // Author
535     label = new QLabel( "<b>" + qtr( "Author" ) + ":</b>", this );
536     layout->addWidget( label, 2, 1, 1, 1, Qt::AlignTop );
537     label = new QLabel( extension.author, this );
538     layout->addWidget( label, 2, 2, 1, 2, Qt::AlignTop );
539
540
541     // Description
542     label = new QLabel( this );
543     label->setText( extension.description );
544     label->setWordWrap( true );
545     label->setOpenExternalLinks( true );
546     layout->addWidget( label, 4, 0, 1, -1 );
547
548     // URL
549     label = new QLabel( "<b>" + qtr( "Website" ) + ":</b>", this );
550     layout->addWidget( label, 5, 0, 1, 2 );
551     label = new QLabel( QString("<a href=\"%1\">%2</a>")
552                         .arg( extension.url ).arg( extension.url )
553                         , this );
554     label->setOpenExternalLinks( true );
555     layout->addWidget( label, 5, 2, 1, -1 );
556
557     // Script file
558     label = new QLabel( "<b>" + qtr( "File" ) + ":</b>", this );
559     layout->addWidget( label, 6, 0, 1, 2 );
560     QLineEdit *line = new QLineEdit( extension.name, this );
561     line->setReadOnly( true );
562     layout->addWidget( line, 6, 2, 1, -1 );
563
564     // Close button
565     QDialogButtonBox *group = new QDialogButtonBox( this );
566     QPushButton *closeButton = new QPushButton( qtr( "&Close" ) );
567     group->addButton( closeButton, QDialogButtonBox::RejectRole );
568     BUTTONACT( closeButton, close() );
569
570     layout->addWidget( group, 7, 0, 1, -1 );
571
572     // Fix layout
573     layout->setColumnStretch( 2, 1 );
574     layout->setRowStretch( 4, 1 );
575     setMinimumSize( 450, 350 );
576 }
577
578 static QPixmap *loadPixmapFromData( char *data, int size )
579 {
580     if( !data || size <= 0 )
581         return NULL;
582     QPixmap *pixmap = new QPixmap();
583     if( !pixmap->loadFromData( (const uchar*) data, (uint) size ) )
584     {
585         delete pixmap;
586         return NULL;
587     }
588     return pixmap;
589 }