~ubuntu-branches/ubuntu/trusty/tomahawk/trusty-proposed

« back to all changes in this revision

Viewing changes to src/libtomahawk/filemetadata/MetadataEditor.cpp

  • Committer: Package Import Robot
  • Author(s): Harald Sitter
  • Date: 2013-03-07 21:50:13 UTC
  • Revision ID: package-import@ubuntu.com-20130307215013-6gdjkdds7i9uenvs
Tags: upstream-0.6.0+dfsg
ImportĀ upstreamĀ versionĀ 0.6.0+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
 
2
 *
 
3
 *   Copyright 2012, Christopher Reichert <creichert07@gmail.com>
 
4
 *   Copyright 2012, Christian Muehlhaeuser <muesli@tomahawk-player.org>
 
5
 *
 
6
 *   Tomahawk is free software: you can redistribute it and/or modify
 
7
 *   it under the terms of the GNU General Public License as published by
 
8
 *   the Free Software Foundation, either version 3 of the License, or
 
9
 *   (at your option) any later version.
 
10
 *
 
11
 *   Tomahawk 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 General Public License for more details.
 
15
 *
 
16
 *   You should have received a copy of the GNU General Public License
 
17
 *   along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
 
18
 */
 
19
 
 
20
#include "MetadataEditor.h"
 
21
#include "ui_MetadataEditor.h"
 
22
 
 
23
#include <QtGui/QDialog>
 
24
#include <QtGui/QDialogButtonBox>
 
25
#include <QtCore/QFileInfo>
 
26
#include <QtCore/QFile>
 
27
 
 
28
#include "Source.h"
 
29
#include "Result.h"
 
30
#include "Artist.h"
 
31
#include "Album.h"
 
32
#include "Typedefs.h"
 
33
#include "ScanManager.h"
 
34
#include "PlaylistInterface.h"
 
35
#include "AlbumPlaylistInterface.h"
 
36
 
 
37
#include "taglib/fileref.h"
 
38
#include "filemetadata/taghandlers/tag.h"
 
39
#include "utils/TomahawkUtils.h"
 
40
#include "utils/Closure.h"
 
41
#include "utils/Logger.h"
 
42
 
 
43
 
 
44
MetadataEditor::MetadataEditor( const Tomahawk::query_ptr& query, const Tomahawk::playlistinterface_ptr& interface, QWidget* parent )
 
45
    : QDialog( parent )
 
46
{
 
47
    init( interface );
 
48
 
 
49
    loadQuery( query );
 
50
}
 
51
 
 
52
 
 
53
MetadataEditor::MetadataEditor( const Tomahawk::result_ptr& result, const Tomahawk::playlistinterface_ptr& interface, QWidget* parent )
 
54
    : QDialog( parent )
 
55
{
 
56
    init( interface );
 
57
 
 
58
    loadResult( result );
 
59
}
 
60
 
 
61
 
 
62
void
 
63
MetadataEditor::init( const Tomahawk::playlistinterface_ptr& interface )
 
64
{
 
65
    ui = new Ui::MetadataEditor();
 
66
    ui->setupUi( this );
 
67
 
 
68
    setAttribute( Qt::WA_DeleteOnClose );
 
69
 
 
70
    m_interface = interface;
 
71
    m_index = 0;
 
72
    m_editable = false;
 
73
 
 
74
    NewClosure( ui->buttonBox, SIGNAL( accepted() ), this, SLOT( writeMetadata( bool ) ), true )->setAutoDelete( false );
 
75
 
 
76
    connect( ui->buttonBox, SIGNAL( rejected() ), SLOT( close() ) );
 
77
    connect( ui->forwardPushButton, SIGNAL( clicked() ), SLOT( loadNextQuery() ) );
 
78
    connect( ui->previousPushButton, SIGNAL( clicked() ), SLOT( loadPreviousQuery() ) );
 
79
}
 
80
 
 
81
 
 
82
void
 
83
MetadataEditor::writeMetadata( bool closeDlg )
 
84
{
 
85
    if ( m_result )
 
86
    {
 
87
        QFileInfo fi( QUrl( m_result->url() ).toLocalFile() );
 
88
 
 
89
        bool changed = false;
 
90
        QByteArray fileName = QFile::encodeName( fi.canonicalFilePath() );
 
91
        const char *encodedName = fileName.constData();
 
92
 
 
93
        TagLib::FileRef f( encodedName );
 
94
        Tomahawk::Tag* tag = Tomahawk::Tag::fromFile( f );
 
95
 
 
96
        if ( title() != m_result->track() )
 
97
        {
 
98
            tDebug() << Q_FUNC_INFO << "Track changed" << title() << m_result->track();
 
99
 
 
100
            tag->setTitle( title() );
 
101
            m_result->setTrack( title() );
 
102
 
 
103
            changed = true;
 
104
        }
 
105
 
 
106
        Tomahawk::artist_ptr newArtist = Tomahawk::Artist::get( artist(), true );
 
107
        if ( newArtist != m_result->artist() )
 
108
        {
 
109
            tDebug() << Q_FUNC_INFO << "Artist changed" << artist() << m_result->artist();
 
110
 
 
111
            tag->setArtist( artist() );
 
112
            m_result->setArtist( newArtist );
 
113
 
 
114
            changed = true;
 
115
        }
 
116
 
 
117
        Tomahawk::album_ptr newAlbum = Tomahawk::Album::get( newArtist, album(), true );
 
118
        if ( newAlbum != m_result->album() )
 
119
        {
 
120
            tDebug() << Q_FUNC_INFO << "Album changed" << album() << newAlbum->id() << m_result->album()->name() << m_result->album()->id() << newAlbum.data() << m_result->album().data();
 
121
            if ( newAlbum->id() != m_result->album()->id() )
 
122
            {
 
123
                tag->setAlbum( album() );
 
124
                m_result->setAlbum( newAlbum );
 
125
 
 
126
                changed = true;
 
127
            }
 
128
            else
 
129
                Q_ASSERT( false );
 
130
        }
 
131
 
 
132
        // FIXME: Ugly workaround for the min value of 0
 
133
        if ( albumPos() != 0 && albumPos() != (int)m_result->albumpos() )
 
134
        {
 
135
            tag->setTrack( albumPos() );
 
136
            m_result->setAlbumPos( albumPos() );
 
137
 
 
138
            tDebug() << Q_FUNC_INFO << "Albumpos changed";
 
139
            changed = true;
 
140
        }
 
141
 
 
142
        // FIXME: Ugly workaround for the min value of 1900
 
143
        if ( year() != 1900 && year() != m_result->year() )
 
144
        {
 
145
            tag->setYear( year() );
 
146
            m_result->setYear( year() );
 
147
 
 
148
            tDebug() << Q_FUNC_INFO << "Year changed";
 
149
            changed = true;
 
150
        }
 
151
 
 
152
        if ( changed )
 
153
        {
 
154
            f.save();
 
155
 
 
156
            m_editFiles.append( fileName );
 
157
            m_result->doneEditing();
 
158
 
 
159
            tDebug() << Q_FUNC_INFO << m_result->toString();
 
160
            tDebug() << Q_FUNC_INFO << m_result->toQuery()->toString();
 
161
        }
 
162
    }
 
163
 
 
164
    if ( closeDlg )
 
165
    {
 
166
        if ( m_editFiles.count() )
 
167
            ScanManager::instance()->runFileScan( m_editFiles, false );
 
168
 
 
169
        close();
 
170
    }
 
171
}
 
172
 
 
173
 
 
174
void
 
175
MetadataEditor::loadQuery( const Tomahawk::query_ptr& query )
 
176
{
 
177
    if ( query.isNull() )
 
178
        return;
 
179
 
 
180
    if ( query->numResults() )
 
181
    {
 
182
        loadResult( query->results().first() );
 
183
        return;
 
184
    }
 
185
 
 
186
    m_result = Tomahawk::result_ptr();
 
187
    m_query = query;
 
188
    setEditable( false );
 
189
 
 
190
    setTitle( query->displayQuery()->track() );
 
191
    setArtist( query->displayQuery()->artist() );
 
192
    setAlbum( query->displayQuery()->album() );
 
193
    setAlbumPos( query->displayQuery()->albumpos() );
 
194
    setDuration( query->displayQuery()->duration() );
 
195
    setYear( 0 );
 
196
    setBitrate( 0 );
 
197
 
 
198
    setFileName( QString() );
 
199
    setFileSize( 0 );
 
200
 
 
201
    setWindowTitle( query->track() );
 
202
 
 
203
    if ( m_interface )
 
204
    {
 
205
        m_index = m_interface->indexOfQuery( query );
 
206
 
 
207
        if ( m_index >= 0 )
 
208
            enablePushButtons();
 
209
    }
 
210
}
 
211
 
 
212
 
 
213
void
 
214
MetadataEditor::loadResult( const Tomahawk::result_ptr& result )
 
215
{
 
216
    if ( result.isNull() )
 
217
        return;
 
218
 
 
219
    m_result = result;
 
220
    setEditable( result->collection() && result->collection()->source()->isLocal() );
 
221
 
 
222
    setTitle( result->track() );
 
223
    setArtist( result->artist()->name() );
 
224
    setAlbum( result->album()->name() );
 
225
    setAlbumPos( result->albumpos() );
 
226
    setDuration( result->duration() );
 
227
    setYear( result->year() );
 
228
    setBitrate( result->bitrate() );
 
229
 
 
230
    if ( result->collection() && result->collection()->source()->isLocal() )
 
231
    {
 
232
        QFileInfo fi( QUrl( m_result->url() ).toLocalFile() );
 
233
        setFileName( fi.absoluteFilePath() );
 
234
        setFileSize( TomahawkUtils::filesizeToString( fi.size() ) );
 
235
    }
 
236
 
 
237
    setWindowTitle( result->track() );
 
238
 
 
239
    if ( m_interface )
 
240
    {
 
241
        m_index = m_interface->indexOfResult( result );
 
242
 
 
243
        if ( m_index >= 0 )
 
244
            enablePushButtons();
 
245
    }
 
246
}
 
247
 
 
248
 
 
249
void
 
250
MetadataEditor::enablePushButtons()
 
251
{
 
252
    if ( m_interface->siblingIndex( 1, m_index ) > 0 )
 
253
        ui->forwardPushButton->setEnabled( true );
 
254
    else
 
255
        ui->forwardPushButton->setEnabled( false );
 
256
 
 
257
    if ( m_interface->siblingIndex( -1, m_index ) > 0 )
 
258
        ui->previousPushButton->setEnabled( true );
 
259
    else
 
260
        ui->previousPushButton->setEnabled( false );
 
261
}
 
262
 
 
263
 
 
264
void
 
265
MetadataEditor::loadNextQuery()
 
266
{
 
267
    writeMetadata();
 
268
 
 
269
    if ( m_interface->siblingIndex( 1, m_index ) > 0 )
 
270
    {
 
271
        m_index = m_interface->siblingIndex( 1, m_index );
 
272
        loadQuery( m_interface->queryAt( m_index ) );
 
273
    }
 
274
}
 
275
 
 
276
 
 
277
void
 
278
MetadataEditor::loadPreviousQuery()
 
279
{
 
280
    writeMetadata();
 
281
 
 
282
    if ( m_interface->siblingIndex( -1, m_index ) > 0 )
 
283
    {
 
284
        m_index = m_interface->siblingIndex( -1, m_index );
 
285
        loadQuery( m_interface->queryAt( m_index ) );
 
286
    }
 
287
}
 
288
 
 
289
 
 
290
void
 
291
MetadataEditor::setTitle( const QString& title )
 
292
{
 
293
    ui->titleLineEdit->setText( title );
 
294
}
 
295
 
 
296
 
 
297
void
 
298
MetadataEditor::setArtist( const QString& artist )
 
299
{
 
300
    ui->artistLineEdit->setText( artist );
 
301
}
 
302
 
 
303
 
 
304
void
 
305
MetadataEditor::setAlbum( const QString& album )
 
306
{
 
307
    ui->albumLineEdit->setText( album );
 
308
}
 
309
 
 
310
 
 
311
void
 
312
MetadataEditor::setAlbumPos( unsigned int num )
 
313
{
 
314
    ui->albumPosSpinBox->setValue( num );
 
315
}
 
316
 
 
317
 
 
318
void
 
319
MetadataEditor::setDuration( unsigned int duration )
 
320
{
 
321
    ui->durationLineEdit->setText( TomahawkUtils::timeToString( duration ) );
 
322
}
 
323
 
 
324
 
 
325
void
 
326
MetadataEditor::setYear( int year )
 
327
{
 
328
    ui->yearSpinBox->setValue( year );
 
329
}
 
330
 
 
331
 
 
332
void
 
333
MetadataEditor::setBitrate( unsigned int bitrate )
 
334
{
 
335
    ui->bitrateSpinBox->setValue( bitrate );
 
336
}
 
337
 
 
338
 
 
339
void
 
340
MetadataEditor::setFileName( const QString& fn )
 
341
{
 
342
    ui->fileNameLineEdit->setText( fn );
 
343
}
 
344
 
 
345
 
 
346
void
 
347
MetadataEditor::setFileSize( const QString& size )
 
348
{
 
349
    ui->fileSizeLineEdit->setText( size );
 
350
}
 
351
 
 
352
 
 
353
void
 
354
MetadataEditor::setEditable( bool editable )
 
355
{
 
356
    ui->artistLineEdit->setReadOnly( !editable );
 
357
    ui->albumLineEdit->setReadOnly( !editable );
 
358
    ui->titleLineEdit->setReadOnly( !editable );
 
359
    ui->albumPosSpinBox->setReadOnly( !editable );
 
360
    ui->yearSpinBox->setReadOnly( !editable );
 
361
 
 
362
    m_editable = editable;
 
363
}
 
364
 
 
365
 
 
366
void
 
367
MetadataEditor::setWindowTitle( const QString& title )
 
368
{
 
369
    QDialog::setWindowTitle( title + " - " + tr( "Properties" ) );
 
370
}