aboutsummaryrefslogblamecommitdiff
path: root/src/ui/display.cpp
blob: 7c7b7e9615c959ea08b7c69f3a33d6aa4c210887 (plain) (tree)
1
2
                                                                            
                                                                            





















                                                                             
                
 
                        

















                          

                                          

                                


                                     


                                             






















                                                                                   
                                                                    

                                            

                


                                                                          
                       


                                                                          

                       

                                                                                

                                                                                       
                                  

                           

                                                                                   
                                                                                     
                                   

                            


                                       

                     

                                       

                     


                                                   

                   

                                       






















                                                                         


                                                                                 

 
                                 
 
                         

                                
                                                                 
 
                        


                                         
 

                              
                                  

 
                                    

                   
                                                

                                                                                       

 
                                    

                   
                                                

                                                                                        





                                                
                            

                           
     
                      
                 



                                                


                 
                              




                                                  
                                



                                         
                             



                                                   
                              





                                                    
























































                                                                                  
 

                                                 
                                       






                                           
/***************************************************************************
 *   Copyright (C) 2006-2008 by Ilya Kotov                                 *
 *   forkotov02@hotmail.ru                                                 *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include <QCoreApplication>
#include <QPainter>
#include <QPushButton>
#include <QLabel>
#include <QSettings>
#include <QMenu>

#include <qmmp/output.h>
#include "skin.h"
#include "mainvisual.h"
#include "button.h"
#include "titlebar.h"
#include "positionbar.h"
#include "number.h"
#include "togglebutton.h"
#include "symboldisplay.h"
#include "textscroller.h"
#include "monostereo.h"
#include "playstatus.h"
#include "volumebar.h"
#include "balancebar.h"
#include "mainwindow.h"
#include "timeindicator.h"

#include "display.h"

MainDisplay::MainDisplay (QWidget *parent)
        : PixmapWidget (parent)
{
    m_skin = Skin::getPointer();
    setPixmap (m_skin->getMain());
    setMaximumSize (QSize (275,116));
    setMinimumSize (QSize (275,116));

    m_mw = qobject_cast<MainWindow*>(parent);

    Button *previous = new Button (this, Skin::BT_PREVIOUS_N, Skin::BT_PREVIOUS_P);
    previous->move (16, 88);
    connect (previous,SIGNAL (clicked()), parent, SLOT (previous()));
    Button *play = new Button (this, Skin::BT_PLAY_N, Skin::BT_PLAY_P);
    play->move (39, 88);
    connect (play,SIGNAL (clicked()),parent,SLOT (play()));
    Button *pause = new Button (this, Skin::BT_PAUSE_N,Skin::BT_PAUSE_P);
    pause->move (62, 88);
    connect (pause,SIGNAL (clicked()),parent,SLOT (pause()));
    Button *stop = new Button (this, Skin::BT_STOP_N,Skin::BT_STOP_P);
    stop->move (85, 88);
    connect (stop,SIGNAL (clicked()),parent,SLOT (stop()));
    connect (stop,SIGNAL (clicked()),this,SLOT (hideTimeDisplay()));
    Button *next = new Button (this, Skin::BT_NEXT_N,Skin::BT_NEXT_P);
    next->move (108, 88);
    connect (next,SIGNAL (clicked()),parent,SLOT (next()));
    Button *eject = new Button (this, Skin::BT_EJECT_N,Skin::BT_EJECT_P);
    eject->move (136, 89);
    connect (eject,SIGNAL (clicked()),parent,SLOT (addFile()));
    connect (m_skin, SIGNAL (skinChanged()), this, SLOT (updateSkin()));
    posbar = new PositionBar (this);
    posbar->move (16,72);

    //connect(posbar, SIGNAL(sliderMoved(int)), SLOT(setTime(int)));
    MainVisual* vis = new MainVisual (this);
    vis->move(24,39);
    vis->show();

    m_eqButton = new ToggleButton (this,Skin::BT_EQ_ON_N,Skin::BT_EQ_ON_P,
                                    Skin::BT_EQ_OFF_N,Skin::BT_EQ_OFF_P);
    m_eqButton->move (219,58);
    m_eqButton->show();
    m_plButton = new ToggleButton (this,Skin::BT_PL_ON_N,Skin::BT_PL_ON_P,
                                    Skin::BT_PL_OFF_N,Skin::BT_PL_OFF_P);
    m_plButton->move (241,58);
    m_plButton->show();

    m_repeatButton = new ToggleButton (this,Skin::REPEAT_ON_N,Skin::REPEAT_ON_P,
                                        Skin::REPEAT_OFF_N,Skin::REPEAT_OFF_P);
    connect(m_repeatButton,SIGNAL(clicked(bool)),this,SIGNAL(repeatableToggled(bool)));

    m_repeatButton->move (210,89);
    m_repeatButton->show();

    m_shuffleButton = new ToggleButton (this,Skin::SHUFFLE_ON_N,Skin::SHUFFLE_ON_P,
                                         Skin::SHUFFLE_OFF_N,Skin::SHUFFLE_OFF_P);
    connect(m_shuffleButton,SIGNAL(clicked(bool)),this,SIGNAL(shuffleToggled(bool)));
    m_shuffleButton->move (164,89);
    m_shuffleButton->show();


    m_kbps = new SymbolDisplay(this,3);
    m_kbps -> move (111,43);
    m_kbps -> show();

    m_freq = new SymbolDisplay(this,2);
    m_freq -> move (156,43);
    m_freq -> show();

    TextScroller *m_text = new TextScroller (this);
    m_text->resize (154,15);
    m_text->move (109,23);
    m_text->show();

    m_monoster = new MonoStereo (this);
    m_monoster->move (212,41);
    m_monoster->show();

    m_playstatus = new PlayStatus(this);
    m_playstatus->move(24,28);
    m_playstatus->show();

    m_volumeBar = new VolumeBar(this);
    connect(m_volumeBar, SIGNAL(sliderMoved(int)),SLOT(updateVolume()));
    m_volumeBar->move(107,57);
    m_volumeBar->show();

    m_balanceBar = new BalanceBar(this);
    connect(m_balanceBar, SIGNAL(sliderMoved(int)),SLOT(updateVolume()));
    m_balanceBar->move(177,57);
    m_balanceBar->show();
    m_timeIndicator = new TimeIndicator(this);
    m_timeIndicator->move(34,26);
    m_timeIndicator->show();
}


MainDisplay::~MainDisplay()
{
    QSettings settings (QDir::homePath() +"/.qmmp/qmmprc", QSettings::IniFormat);
    settings.setValue ("Playlist/visible",m_plButton->isChecked());
    settings.setValue ("Equalizer/visible",m_eqButton->isChecked());
}

void MainDisplay::setTime (int t)
{
    posbar->setValue (t);
    m_timeIndicator->setTime(t);
}
void MainDisplay::setMaxTime (long mt) // TODO: should be removed
{
    posbar->setMax (mt);
    m_timeIndicator->setSongDuration(mt);
}


void MainDisplay::updateSkin()
{
    setPixmap (m_skin->getMain());
}

void MainDisplay::setEQ (QWidget* w)
{
    m_equlizer = w;
    m_eqButton->setON (m_equlizer->isVisible());
    connect (m_eqButton, SIGNAL (clicked(bool)), m_equlizer, SLOT (setVisible (bool)));
    connect (m_equlizer, SIGNAL (closed ()), m_eqButton, SLOT (click()));
}

void MainDisplay::setPL (QWidget* w)
{
    m_playlist = w;
    m_plButton->setON (m_playlist->isVisible());
    connect (m_plButton, SIGNAL (clicked (bool)), m_playlist, SLOT (setVisible (bool)));
    connect (m_playlist, SIGNAL (closed ()), m_plButton, SLOT (click()));
}

void MainDisplay::setInfo(const OutputState &st)
{


    switch ((int) st.type())
    {
    case OutputState::Info:
    {
        //if (seeking)
        // break;
        setTime (st.elapsedSeconds());
        m_kbps->display (st.bitrate());
        m_freq->display (st.frequency() /1000);
        m_monoster->setChannels (st.channels());
        update();
        break;
    }
    case OutputState::Playing:
    {
        m_playstatus->setStatus(PlayStatus::PLAY);
        m_timeIndicator->setNeedToShowTime(true);
        break;
    }
    case OutputState::Buffering:
    {
        //ui.label->setText("Buffering");
        break;
    }
    case OutputState::Paused:
    {
        m_playstatus->setStatus(PlayStatus::PAUSE);
        break;
    }
    case OutputState::Stopped:
    {
        m_playstatus->setStatus(PlayStatus::STOP);
        m_monoster->setChannels (0);
        //m_timeIndicator->setNeedToShowTime(false);
        break;
    }
    case OutputState::Volume:
        //qDebug("volume %d, %d", st.rightVolume(), st.leftVolume());
        int maxVol = qMax(st.leftVolume(),st.rightVolume());
        m_volumeBar->setValue(maxVol);
        if (maxVol && !m_volumeBar->isPressed())
            m_balanceBar->setValue((st.rightVolume()-st.leftVolume())*100/maxVol);
        break;

    }
}

bool MainDisplay::isPlaylistVisible() const
{
    return m_plButton->isChecked();
}

bool MainDisplay::isEqualizerVisible() const
{
    return m_eqButton->isChecked();
}

void MainDisplay::updateVolume()
{
    m_mw->setVolume(m_volumeBar->value(), m_balanceBar->value());
}

void MainDisplay::wheelEvent (QWheelEvent *e)
{
    m_mw->setVolume(m_volumeBar->value()+e->delta()/10, m_balanceBar->value());
}

bool MainDisplay::isRepeatable() const
{
    return m_repeatButton->isChecked();
}

bool MainDisplay::isShuffle() const
{
    return m_shuffleButton->isChecked();
}

void MainDisplay::setIsRepeatable(bool yes)
{
    m_repeatButton->setON(yes);
}

void MainDisplay::setIsShuffle(bool yes)
{
    m_shuffleButton->setON(yes);
}


void MainDisplay::hideTimeDisplay()
{
    m_timeIndicator->setNeedToShowTime(false);
}


void MainDisplay::mousePressEvent(QMouseEvent *e)
{
    if (e->button() == Qt::RightButton)
    {
        m_mw->menu()->exec(e->globalPos());
    }
    PixmapWidget::mousePressEvent(e);
}


a id='n230' href='#n230'>230 231 232 233 234 235 236 237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
                                                                            
                                                                            














                                                                            
                                                                            





                                                                             

                    
                      
                             
                      


                               
                       

                          
                      

                        
                              
 

                                                             


                            

                                               
                                                                     





                                                                                  
               







                                                                                    






                                                                                  
               







                                                                                    

                
                                                              
 
                  
                    




                           

                                          

 

                                 





                             
                       

 
                                                                         



                                                                 
                                                                       

                                                               
                     

                                                    
                                                                  
                                                    
     
                                                                                                             

                                            
         







                                                                                      

                                                                




                                                                       

                          


        






                                                                                               
     
 






                                                                 


















                                                                                                       
                         







                                                                         
                                                                        








                                                                                       
                             





                                                                                                      
                             
         









                                                                                                                   




                                                                                               
 
                                                                              


                                                  
                                               









                                                                                                         
                                                                                                                    

                                                                                        
                                                            


                                                                                                











                                                                                             














                                                                                       
                                                       

                                                        
 

                                                                 

                

                                                                
                 
                                                                                           

                                                                      




                                                                  















                                                                                                         




                                                                                 








                                          

                       
                           


                  

                                                                        
                                


                                
                                                                           















                                                                          
                                     

                                                         
                     


                                                   
                     






                                                                                                           
                

 




                                                                         




                                      

                                 
                  
                    
                            
                                                      



                                          
                                         
                                                                                                   


                                                      
                     











                                                                         
                     
     
 
                                                                                   
     
                                                                     

                                                                           

     
                                                    
     
                                                                                                             

                                            
         




                                                               
                             





                                                                                      
                         
         

                                                       


        
                                                                 


                                                           
                         
         
                                                                                               
     
                                         
                     
                                                        


                                                     
                                                                  
                                                
                


 
                                        
 


                       
                                   
 
                     

 
                                                                 
 
                    
     






                                                                                           
                                                                                            



                            

            
                                                                  
                                                
         
     









                                                       
 
 
                                     

                                                        
                    
 
/***************************************************************************
 *   Copyright (C) 2009-2016 by Ilya Kotov                                 *
 *   forkotov02@hotmail.ru                                                 *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
 ***************************************************************************/


#include <QObject>
#include <QRegExp>
#include <QSettings>
#include <QFileInfo>
#include <QDir>
#include <cdio/cdio.h>
#if LIBCDIO_VERSION_NUM <= 83
#include <cdio/cdda.h>
#else
#include <cdio/paranoia/cdda.h>
#endif
#include <cdio/audio.h>
#include <cdio/cd_types.h>
#include <cdio/logging.h>
#include <cddb/cddb.h>
#include <qmmp/buffer.h>
#include <qmmp/output.h>
#include <qmmp/qmmpsettings.h>

#define CDDA_SECTORS 4
#define CDDA_BUFFER_SIZE (CDDA_SECTORS*CDIO_CD_FRAMESIZE_RAW)

#include "decoder_cdaudio.h"

QList <CDATrack> DecoderCDAudio::m_track_cache;

static void log_handler (cdio_log_level_t level, const char *message)
{
    QString str = QString::fromLocal8Bit(message).trimmed();
    switch (level)
    {
    case CDIO_LOG_DEBUG:
        qDebug("DecoderCDAudio: cdio message: %s (level=debug)", qPrintable(str));
        return;
    case CDIO_LOG_INFO:
        qDebug("DecoderCDAudio: cdio message: %s (level=info)", qPrintable(str));
        return;
    default:
        qWarning("DecoderCDAudio: cdio message: %s (level=error)", qPrintable(str));
    }
}

static void cddb_log_handler(cddb_log_level_t level, const char *message)
{
    QString str = QString::fromLocal8Bit(message).trimmed();
    switch (level)
    {
    case CDDB_LOG_DEBUG:
        qDebug("DecoderCDAudio: cddb message: %s (level=debug)", qPrintable(str));
        return;
    case CDDB_LOG_INFO:
        qDebug("DecoderCDAudio: cddb message: %s (level=info)", qPrintable(str));
        return;
    default:
        qWarning("DecoderCDAudio: cddb message: %s (level=error)", qPrintable(str));
    }
}

// Decoder class

DecoderCDAudio::DecoderCDAudio(const QString &url) : Decoder()
{
    m_bitrate = 0;
    m_totalTime = 0;
    m_first_sector = -1;
    m_last_sector  = -1;
    m_current_sector  = -1;
    m_url = url;
    m_cdio = 0;
    m_buffer_at = 0;
    m_buffer = new char[CDDA_BUFFER_SIZE];
}

DecoderCDAudio::~DecoderCDAudio()
{
    m_bitrate = 0;
    if (m_cdio)
    {
        cdio_destroy(m_cdio);
        m_cdio = 0;
    }
    delete [] m_buffer;
}

QList <CDATrack> DecoderCDAudio::generateTrackList(const QString &device)
{
    //read settings
    QSettings settings(Qmmp::configFile(), QSettings::IniFormat);
    int cd_speed = settings.value("cdaudio/speed", 0).toInt();
    bool use_cd_text = settings.value("cdaudio/cdtext", true).toBool();
    QList <CDATrack> tracks;
    cdio_log_set_handler(log_handler); //setup cdio log handler
    CdIo_t *cdio = 0;
    QString device_path = device;
    if (device_path.isEmpty() || device_path == "/")
        device_path = settings.value("cdaudio/device").toString();
    if (device_path.isEmpty() || device_path == "/")
    {
        char **cd_drives = cdio_get_devices_with_cap(0, CDIO_FS_AUDIO, true); //get drive list with CDA disks
        // open first audio capable cd drive
        if (cd_drives && *cd_drives)
        {
            cdio = cdio_open_cd(*cd_drives);
            if (!cdio)
            {
                qWarning("DecoderCDAudio: failed to open CD.");
                cdio_free_device_list(cd_drives);
                return tracks;
            }
            qDebug("DecoderCDAudio: found cd audio capable drive \"%s\"", *cd_drives);
            device_path = QString(*cd_drives);
            cdio_free_device_list(cd_drives); //free device list
        }
        else
        {
            qWarning("DecoderCDAudio: unable to find cd audio drive.");
            cdio_free_device_list(cd_drives);
            return tracks;
        }
    }
    else
    {
        cdio = cdio_open_cd(device_path.toAscii().constData());
        if (!cdio)
        {
            qWarning("DecoderCDAudio: failed to open CD.");
            return tracks;
        }
        qDebug("DecoderCDAudio: using cd audio capable drive \"%s\"", qPrintable(device_path));
    }

    if(!m_track_cache.isEmpty() && !cdio_get_media_changed(cdio))
    {
        qDebug("DecoderCDAudio: using track cache...");
        cdio_destroy(cdio);
        return m_track_cache;
    }

    if (cd_speed)
    {
        qDebug("DecoderCDAudio: setting drive speed to %dX.", cd_speed);
        if (cdio_set_speed(cdio, 1) != DRIVER_OP_SUCCESS)
            qWarning("DecoderCDAudio: unable to set drive speed to %dX.", cd_speed);
    }

    cdrom_drive_t *pcdrom_drive = cdio_cddap_identify_cdio(cdio, 1, 0); //create paranoya CD-ROM object
    //get first and last track numbers
    int first_track_number = cdio_get_first_track_num(pcdrom_drive->p_cdio);
    int last_track_number = cdio_get_last_track_num(pcdrom_drive->p_cdio);

    if ((first_track_number == CDIO_INVALID_TRACK) || (last_track_number == CDIO_INVALID_TRACK))
    {
        qWarning("DecoderCDAudio: invalid first (last) track number.");
        cdio_destroy(cdio);
        cdio = 0;
        return tracks;
    }
    bool use_cddb = true;
    //fill track list
    for (int i = first_track_number; i <= last_track_number; ++i)
    {
        CDATrack t;
        t.first_sector = cdio_get_track_lsn(pcdrom_drive->p_cdio, i);
        t.last_sector = cdio_get_track_last_lsn(pcdrom_drive->p_cdio, i);
        t.info.setLength((t.last_sector - t.first_sector +1) / 75);
        t.info.setMetaData(Qmmp::TRACK, i);
        t.info.setPath(QString("cdda://%1#%2").arg(device_path).arg(i));
        if ((t.first_sector == CDIO_INVALID_LSN) || (t.last_sector== CDIO_INVALID_LSN))
        {
            qWarning("DecoderCDAudio: invalid stard(end) lsn for the track %d.", i);
            tracks.clear();
            cdio_destroy(cdio);
            cdio = 0;
            return tracks;
        }
        //cd text
#if LIBCDIO_VERSION_NUM <= 83
        cdtext_t *cdtext = use_cd_text ? cdio_get_cdtext(pcdrom_drive->p_cdio, i) : 0;
        if (cdtext && cdtext->field[CDTEXT_TITLE])
        {
            t.info.setMetaData(Qmmp::TITLE, QString::fromLocal8Bit(cdtext->field[CDTEXT_TITLE]));
            t.info.setMetaData(Qmmp::ARTIST, QString::fromLocal8Bit(cdtext->field[CDTEXT_PERFORMER]));
            t.info.setMetaData(Qmmp::GENRE, QString::fromLocal8Bit(cdtext->field[CDTEXT_GENRE]));
            use_cddb = false;
        }
#else
        cdtext_t *cdtext = use_cd_text ? cdio_get_cdtext(pcdrom_drive->p_cdio) : 0;
        if (cdtext)
        {
            t.info.setMetaData(Qmmp::TITLE, QString::fromUtf8(cdtext_get_const(cdtext,CDTEXT_FIELD_TITLE,i)));
            t.info.setMetaData(Qmmp::ARTIST, QString::fromUtf8(cdtext_get_const(cdtext,CDTEXT_FIELD_PERFORMER,i)));
            t.info.setMetaData(Qmmp::GENRE, QString::fromUtf8(cdtext_get_const(cdtext,CDTEXT_FIELD_GENRE,i)));
            use_cddb = false;
        }
#endif
        else
            t.info.setMetaData(Qmmp::TITLE, QString("CDA Track %1").arg(i, 2, 10, QChar('0')));
        tracks  << t;
    }
    qDebug("DecoderCDAudio: found %d audio tracks", tracks.size());

    use_cddb = use_cddb && settings.value("cdaudio/use_cddb", false).toBool();
    if(use_cddb)
    {
        qDebug("DecoderCDAudio: reading CDDB...");
        cddb_log_set_handler(cddb_log_handler);
        cddb_conn_t *cddb_conn = cddb_new ();
        cddb_disc_t *cddb_disc = NULL;
        cddb_track_t *cddb_track = NULL;
        lba_t lba;
        if (!cddb_conn)
            qWarning ("DecoderCDAudio: unable to create cddb connection");
        else
        {
            cddb_cache_disable (cddb_conn); //disable libcddb cache, use own cache implementation instead
            settings.beginGroup("cdaudio");
            cddb_set_server_name (cddb_conn, settings.value("cddb_server", "freedb.org").toByteArray().constData());
            cddb_set_server_port (cddb_conn, settings.value("cddb_port", 8880).toInt());

            if (settings.value("cddb_http", false).toBool())
            {
                cddb_http_enable (cddb_conn);
                cddb_set_http_path_query (cddb_conn, settings.value("cddb_path").toByteArray());
                if (QmmpSettings::instance()->isProxyEnabled())
                {
                    QUrl proxy = QmmpSettings::instance()->proxy();
                    cddb_http_proxy_enable (cddb_conn);
                    cddb_set_http_proxy_server_name (cddb_conn, proxy.host().toAscii ());
                    cddb_set_http_proxy_server_port (cddb_conn, proxy.port());
                    if(QmmpSettings::instance()->useProxyAuth())
                    {
                        cddb_set_http_proxy_username (cddb_conn, proxy.userName().toAscii());
                        cddb_set_http_proxy_password (cddb_conn, proxy.password().toAscii());
                    }
                }
            }
            settings.endGroup();

            cddb_disc = cddb_disc_new ();
            lba = cdio_get_track_lba (cdio, CDIO_CDROM_LEADOUT_TRACK);
            cddb_disc_set_length (cddb_disc, FRAMES_TO_SECONDS (lba));

            for (int i = first_track_number; i <= last_track_number; ++i)
            {
                cddb_track = cddb_track_new ();
                cddb_track_set_frame_offset (cddb_track, cdio_get_track_lba (cdio, i));
                cddb_disc_add_track (cddb_disc, cddb_track);
            }

            cddb_disc_calc_discid (cddb_disc);
            uint id = cddb_disc_get_discid (cddb_disc);
            qDebug ("DecoderCDAudio: disc id = %x", id);


            if(readFromCache(&tracks, id))
                qDebug("DecoderCDAudio: using local cddb cache");
            else
            {
                int matches = cddb_query (cddb_conn, cddb_disc);
                if(matches == -1)
                {
                    qWarning ("DecoderCDAudio: unable to query the CDDB server, error: %s",
                              cddb_error_str (cddb_errno(cddb_conn)));
                }
                else if(matches == 0)
                {
                    qDebug ("DecoderCDAudio: no CDDB info found");
                }
                else if(cddb_read(cddb_conn, cddb_disc))
                {
                    for (int i = first_track_number; i <= last_track_number; ++i)
                    {
                        cddb_track_t *cddb_track = cddb_disc_get_track (cddb_disc, i - 1);
                        int t = i - first_track_number;
                        tracks[t].info.setMetaData(Qmmp::ARTIST,
                                                   QString::fromUtf8(cddb_track_get_artist(cddb_track)));
                        tracks[t].info.setMetaData(Qmmp::TITLE,
                                                   QString::fromUtf8(cddb_track_get_title(cddb_track)));
                        tracks[t].info.setMetaData(Qmmp::GENRE,
                                                   QString::fromUtf8(cddb_disc_get_genre(cddb_disc)));
                        tracks[t].info.setMetaData(Qmmp::ALBUM,
                                                   QString::fromUtf8(cddb_disc_get_title(cddb_disc)));
                    }
                    saveToCache(tracks,  id);
                }
                else
                {
                    qWarning ("DecoderCDAudio: unable to read the CDDB info: %s",
                              cddb_error_str (cddb_errno(cddb_conn)));
                }
            }
        }
        if (cddb_disc)
            cddb_disc_destroy (cddb_disc);

        if (cddb_conn)
            cddb_destroy (cddb_conn);
    }

    cdio_destroy(cdio);
    cdio = 0;
    m_track_cache = tracks;
    return tracks;
}

void DecoderCDAudio::saveToCache(QList <CDATrack> tracks,  uint disc_id)
{
    QDir dir(Qmmp::configDir());
    if(!dir.exists("cddbcache"))
        dir.mkdir("cddbcache");
    dir.cd("cddbcache");
    QString path = dir.absolutePath() + QString("/%1").arg(disc_id, 0, 16);
    QSettings settings(path, QSettings::IniFormat);
    settings.clear();
    settings.setValue("count", tracks.size());
    for(int i = 0; i < tracks.size(); ++i)
    {
        CDATrack track = tracks[i];
        QMap<Qmmp::MetaData, QString> meta = track.info.metaData();
        settings.setValue(QString("artist%1").arg(i), meta[Qmmp::ARTIST]);
        settings.setValue(QString("title%1").arg(i), meta[Qmmp::TITLE]);
        settings.setValue(QString("genre%1").arg(i), meta[Qmmp::GENRE]);
        settings.setValue(QString("album%1").arg(i), meta[Qmmp::ALBUM]);
    }
}

bool DecoderCDAudio::readFromCache(QList <CDATrack> *tracks, uint disc_id)
{
    QString path = Qmmp::configDir();
    path += QString("/cddbcache/%1").arg(disc_id, 0, 16);
    if(!QFile::exists(path))
        return false;
    QSettings settings(path, QSettings::IniFormat);
    int count = settings.value("count").toInt();
    if(count != tracks->count())
        return false;
    for(int i = 0; i < count; ++i)
    {
        (*tracks)[i].info.setMetaData(Qmmp::ARTIST, settings.value(QString("artist%1").arg(i)).toString());
        (*tracks)[i].info.setMetaData(Qmmp::TITLE, settings.value(QString("title%1").arg(i)).toString());
        (*tracks)[i].info.setMetaData(Qmmp::GENRE, settings.value(QString("genre%1").arg(i)).toString());
        (*tracks)[i].info.setMetaData(Qmmp::ALBUM, settings.value(QString("album%1").arg(i)).toString());
    }
    return true;
}

qint64 DecoderCDAudio::calculateTrackLength(lsn_t startlsn, lsn_t endlsn)
{
    return ((endlsn - startlsn + 1) * 1000) / 75;
}

void DecoderCDAudio::clearTrackCache()
{
    m_track_cache.clear();
}

bool DecoderCDAudio::initialize()
{
    m_bitrate = 0;
    m_totalTime = 0;
    //extract track from url
    int track_number = m_url.section("#", -1).toInt();
    QString device_path = m_url;
    device_path.remove("cdda://");
    device_path.remove(QRegExp("#\\d+$"));

    track_number = qMax(track_number, 1);
    QList <CDATrack> tracks = DecoderCDAudio::generateTrackList(device_path); //generate track list
    if (tracks.isEmpty())
    {
        qWarning("DecoderCDAudio: initialize failed");
        return false;
    }
    //find track by number
    int track_at = -1;
    for (int i = 0; i < tracks.size(); ++i)
        if (tracks[i].info.metaData(Qmmp::TRACK).toInt() == track_number)
        {
            track_at = i;
            break;
        }
    if (track_at < 0)
    {
        qWarning("DecoderCDAudio: invalid track number");
        return false;
    }

    if (device_path.isEmpty() || device_path == "/") //try default path from config
    {
        QSettings settings(Qmmp::configFile(), QSettings::IniFormat);
        device_path = settings.value("cdaudio/device").toString();
        m_url = QString("cdda://%1#%2").arg(device_path).arg(track_number);
    }

    if (device_path.isEmpty() || device_path == "/")
    {
        char **cd_drives = cdio_get_devices_with_cap(0, CDIO_FS_AUDIO, true); //get drive list with CDA disks
        // open first audio capable cd drive
        if (cd_drives && *cd_drives)
        {
            m_cdio = cdio_open_cd(*cd_drives);
            if (!m_cdio)
            {
                qWarning("DecoderCDAudio: failed to open CD.");
                cdio_free_device_list(cd_drives);
                return false;
            }
            qDebug("DecoderCDAudio: found cd audio capable drive \"%s\"", *cd_drives);
        }
        else
        {
            qWarning("DecoderCDAudio: unable to find cd audio drive.");
            return false;
        }
        if (cd_drives && *cd_drives) //free device list
            cdio_free_device_list(cd_drives);
    }
    else
    {
        m_cdio = cdio_open_cd(device_path.toAscii().constData());
        if (!m_cdio)
        {
            qWarning("DecoderCDAudio: failed to open CD.");
            return false;
        }
        qDebug("DecoderCDAudio: using cd audio capable drive \"%s\"", qPrintable(device_path));
    }
    configure(44100, 2, Qmmp::PCM_S16LE);
    m_bitrate = 1411;
    m_totalTime = tracks[track_at].info.length() * 1000;
    m_first_sector = tracks[track_at].first_sector;
    m_current_sector = tracks[track_at].first_sector;
    m_last_sector = tracks[track_at].last_sector;
    addMetaData(tracks[track_at].info.metaData()); //send metadata
    qDebug("DecoderCDAudio: initialize succes");
    return true;
}


qint64 DecoderCDAudio::totalTime() const
{
    return m_totalTime;
}

int DecoderCDAudio::bitrate() const
{
    return m_bitrate;
}

qint64 DecoderCDAudio::read(unsigned char *audio, qint64 maxSize)
{
    if(!m_buffer_at)
    {

        lsn_t secorts_to_read = qMin(CDDA_SECTORS, (m_last_sector - m_current_sector + 1));

        if (secorts_to_read <= 0)
            return 0;

        if (cdio_read_audio_sectors(m_cdio, m_buffer,
                                    m_current_sector, secorts_to_read) != DRIVER_OP_SUCCESS)
        {
            m_buffer_at = 0;
            return -1;
        }
        else
        {
            m_buffer_at = secorts_to_read * CDIO_CD_FRAMESIZE_RAW;
            m_current_sector += secorts_to_read;
        }
    }

    if(m_buffer_at > 0)
    {
        long len = qMin(maxSize, m_buffer_at);
        memcpy(audio, m_buffer, len);
        m_buffer_at -= len;
        memmove(m_buffer, m_buffer + len, m_buffer_at);
        return len;
    }
    return 0;
}

void DecoderCDAudio::seek(qint64 pos)
{
    m_current_sector = m_first_sector + pos * 75 / 1000;
    m_buffer_at = 0;
}