]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs/fingerprintdialog.cpp
Qt: ExtensionsModel: do DI
[vlc] / modules / gui / qt4 / dialogs / fingerprintdialog.cpp
1 /*****************************************************************************
2  * fingerprintdialog.cpp: Fingerprinter Dialog
3  *****************************************************************************
4  * Copyright (C) 2012 VLC authors and VideoLAN
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation; either version 2.1 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19  *****************************************************************************/
20
21 #include "fingerprintdialog.hpp"
22 #include "ui/fingerprintdialog.h"
23
24 #include "adapters/chromaprint.hpp"
25 #include <vlc_url.h>
26
27 #include <QLabel>
28 #include <QListWidgetItem>
29
30 FingerprintDialog::FingerprintDialog(QWidget *parent, intf_thread_t *p_intf,
31                                      input_item_t *p_item ) :
32     QDialog(parent),
33     ui(new Ui::FingerprintDialog), p_r( NULL )
34 {
35     ui->setupUi(this);
36
37     ui->stackedWidget->setCurrentWidget( ui->wait );
38
39     ui->buttonBox->addButton( "&Close",
40                               QDialogButtonBox::RejectRole );
41
42     ui->buttonsBox->addButton( "&Apply this identity to the file",
43                                 QDialogButtonBox::AcceptRole );
44
45     ui->buttonsBox->addButton( "&Discard all identities",
46                                 QDialogButtonBox::RejectRole );
47
48     CONNECT( ui->buttonsBox, accepted(), this, applyIdentity() );
49     CONNECT( ui->buttonBox, rejected(), this, close() );
50     CONNECT( ui->buttonsBox, rejected(), this, close() );
51
52     t = new Chromaprint( p_intf );
53     if ( t )
54     {
55         CONNECT( t, finished(), this, handleResults() );
56         t->enqueue( p_item );
57     }
58 }
59
60 void FingerprintDialog::applyIdentity()
61 {
62     Q_ASSERT( p_r );
63     if ( ui->recordsList->currentIndex().isValid() )
64         t->apply( p_r, ui->recordsList->currentIndex().row() );
65     emit metaApplied( p_r->p_item );
66     close();
67 }
68
69 void FingerprintDialog::handleResults()
70 {
71     p_r = t->fetchResults();
72
73     if ( ! p_r )
74     {
75         ui->stackedWidget->setCurrentWidget( ui->error );
76         return;
77     }
78
79     if ( vlc_array_count( & p_r->results.metas_array ) == 0 )
80     {
81         fingerprint_request_Delete( p_r );
82         p_r = NULL;
83         ui->stackedWidget->setCurrentWidget( ui->error );
84         return;
85     }
86
87     ui->stackedWidget->setCurrentWidget( ui->results );
88
89     for ( int i=0; i< vlc_array_count( & p_r->results.metas_array ) ; i++ )
90     {
91         vlc_meta_t *p_meta =
92                 (vlc_meta_t *) vlc_array_item_at_index( & p_r->results.metas_array, i );
93         QListWidgetItem *item = new QListWidgetItem();
94         ui->recordsList->addItem( item );
95         QString mb_id( vlc_meta_GetExtra( p_meta, "musicbrainz-id" ) );
96         QLabel *label = new QLabel(
97                     QString( "<h3 style=\"margin: 0\"><a style=\"text-decoration:none\" href=\"%1\">%2</a></h3>"
98                              "<span style=\"padding-left:20px\">%3</span>" )
99                     .arg( QString( "http://mb.videolan.org/recording/%1" ).arg( mb_id ) )
100                     .arg( qfu( vlc_meta_Get( p_meta, vlc_meta_Title ) ) )
101                     .arg( qfu( vlc_meta_Get( p_meta, vlc_meta_Artist ) ) )
102         );
103         label->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Preferred );
104         label->setOpenExternalLinks( true );
105         item->setSizeHint( label->sizeHint() );
106         ui->recordsList->setItemWidget( item, label );
107     }
108     ui->recordsList->setCurrentIndex( ui->recordsList->model()->index( 0, 0 ) );
109 }
110
111 FingerprintDialog::~FingerprintDialog()
112 {
113     if ( t ) delete t;
114     if ( p_r ) fingerprint_request_Delete( p_r );
115     delete ui;
116 }