summaryrefslogtreecommitdiffstats
path: root/src/cdmanager.cpp
blob: d0687fc5c993455c109da7c64a2195f6f2a92021 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190

#include "cdmanager.h"
#include "paranoia.h"
#include "cddb.h"
#include "conversionoptions.h"

#include <tqstringlist.h>
#include <tqvaluelist.h>

#include <tdelocale.h>
#include <tdemessagebox.h>
#include <kinputdialog.h>
#include <dcopref.h>


// ### soundkonverter 0.4 implement reading of milliseconds/frames

// TODO implement reading of cd data

CDDevice::CDDevice( const TQString& _device )
{
    TQStringList s;
    bool init = false;
    TQValueList<int> qvl;
    int i;
    TQStringList dcopList, devList;
    bool ok = false;

    tags.clear();

    if( !_device.isEmpty() )
        s.append( _device );
    else {
        DCOPRef mediamanager( "kded", "mediamanager" );
        DCOPReply reply = mediamanager.call( "fullList()" );
        if( reply.isValid() ) {
            dcopList = reply;
            i = 0;
            while( i < (int)dcopList.count() ) {
                if( dcopList[i+10] == "media/audiocd" ) {
                    devList.append( dcopList[i+5] );
                }
                i += 13;
            }
            if( devList.count() > 1 ) {
                TQString choice = KInputDialog::getItem( i18n("Audio CD"), i18n("Several audio CDs found. Choose one:"), devList, 0, false, &ok );
                if( ok ) s.append( choice );
                else s.append( 0 ); // TODO if canceled, the cd opener should close, not use the first item
            }
            else if( devList.count()==1 ) {
                s.append( devList[0] );
            }
            else {
                s.append( "/dev/cdrom" );
                s.append( "/dev/dvd" );
            }
        }
        else {
            s.append( "/dev/cdrom" );
            s.append( "/dev/dvd" );
        }
    }

    para = new Paranoia();
    for( i = 0; i < (int)s.count(); i++ ) {
        if( init = para->init(s[i]) ) {
            device = s[i];
            break;
        }
    }
    if( init ) {
        trackCount = para->getTracks();
        timeCount = para->trackTime( -1 );
        for( i = 0; i < para->getTracks(); i++) {
            qvl.append( para->trackFirstSector(i+1) + 150 );
        }
        qvl.append( para->discFirstSector() );
        qvl.append( para->discLastSector() );
        CDDB* cddb = new CDDB();
        cddb->save_cddb( true );
        if( cddb->queryCD(qvl) ) {
            for( i = 0; i < para->getTracks(); i++ ) {
               tags += new TagData( cddb->artist(i), "", cddb->title(), cddb->track(i), cddb->genre(), "",
                                      i+1, cddb->disc(), cddb->year(), para->trackTime(i) );
            }
        }
        else {
            cddb->set_server( "gnudb.gnudb.org", 8880 );
            if( cddb->queryCD(qvl) ) {
                for( i = 0; i < para->getTracks(); i++ ) {
                    tags += new TagData( cddb->artist(i), "", cddb->title(), cddb->track(i), cddb->genre(), "",
                                           i+1, cddb->disc(), cddb->year(), para->trackTime(i) );
                }
            }
            else {
                for( i = 0; i < para->getTracks(); i++ ) {
                    tags += new TagData( i18n("Unknown"), "", i18n("Unknown"), i18n("Unknown"), "", "", i+1, 1, 0, para->trackTime(i) );
                }
            }
        }
        delete cddb;
    }
    else {
        KMessageBox::information( 0, i18n("No audio CD found."), i18n("Warning") );
        device = "";
        delete para;
        para = 0;
    }
}

CDDevice::~CDDevice()
{}


CDManager::CDManager()
{}

CDManager::~CDManager()
{
    while( cdDevices.count() > 0 ) delete cdDevices.first();
}

TQString CDManager::newCDDevice( const TQString& device )
{
    CDDevice* cdDevice = new CDDevice( device );

    for( TQValueList<CDDevice*>::Iterator it = cdDevices.begin(); it != cdDevices.end(); ++it ) {
        if( (*it)->device = cdDevice->device ) {
            cdDevices.remove( *it );
            delete (*it);
            break;
        }
    }

    cdDevices += cdDevice;
    return cdDevice->device;
}

TQValueList<TagData*> CDManager::getTrackList( const TQString& device )
{
    for( TQValueList<CDDevice*>::Iterator it = cdDevices.begin(); it != cdDevices.end(); ++it ) {
        if( (*it)->device = device ) return (*it)->tags;
    }

    TQValueList<TagData*> list;
    return list;
}

TagData* CDManager::getTags( const TQString& device, int track )
{
    for( TQValueList<CDDevice*>::Iterator it = cdDevices.begin(); it != cdDevices.end(); ++it ) {
        if( (*it)->device = device ) {
            if( track > 0 ) {
                TQValueList<TagData*>::Iterator tag = (*it)->tags.at( track - 1 );
                return (*tag);
            }
            else {
                return (*it)->discTags;
            }
        }
    }

    return 0;
}

int CDManager::getTrackCount( const TQString& device )
{
    for( TQValueList<CDDevice*>::Iterator it = cdDevices.begin(); it != cdDevices.end(); ++it ) {
        if( (*it)->device = device ) return (*it)->trackCount;
    }

    return 0;
}

int CDManager::getTimeCount( const TQString& device )
{
    for( TQValueList<CDDevice*>::Iterator it = cdDevices.begin(); it != cdDevices.end(); ++it ) {
        if( (*it)->device = device ) return (*it)->timeCount;
    }

    return 0;
}

void CDManager::setDiscTags( const TQString& device, TagData* tags )
{
    for( TQValueList<CDDevice*>::Iterator it = cdDevices.begin(); it != cdDevices.end(); ++it ) {
        if( (*it)->device = device ) (*it)->discTags = tags;
    }
}