summaryrefslogtreecommitdiffstats
path: root/amarok/src/devicemanager.cpp
blob: a6d8a7d9327a514bee02c2612274eedabdff519b (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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
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
//
// C++ Implementation: devicemanager
//
// Description: Controls device/medium object handling, providing
//              helper functions for other objects
//
//
// Author: Jeff Mitchell <kde-dev@emailgoeshere.com>, (C) 2006
//         Maximilian Kossick <maximilian.kossick@googlemail.com>, (C) 2006
//
// Copyright: See COPYING file that comes with this distribution
//
//

#include "amarok.h"
#include "amarokconfig.h"
#include "debug.h"
#include "devicemanager.h"
#include "medium.h"
#include "mediumpluginmanager.h"

#include <tqptrlist.h>
#include <tqtimer.h>

#include <dcopclient.h>
#include <dcopobject.h>
#include <dcopref.h>
#include <kapplication.h>

typedef Medium::List MediumList;
typedef TQMap<TQString, Medium*>::Iterator MediumIterator;

DeviceManager* DeviceManager::instance()
{
    static DeviceManager dw;
    return &dw;
}

DeviceManager::DeviceManager()
{
    DEBUG_BLOCK
    m_dc = KApplication::dcopClient();
    m_dc->setNotifications(true);
    m_valid = false;

    if (!m_dc->isRegistered())
    {
        debug() << "DeviceManager:  DCOP Client not registered!" << endl;
    }
    else
    {
        if (!m_dc->connectDCOPSignal("kded", "mediamanager", "mediumAdded(TQString)", "devices", "mediumAdded(TQString)", false) ||
            !m_dc->connectDCOPSignal("kded", "mediamanager", "mediumRemoved(TQString)", "devices", "mediumRemoved(TQString)", false) ||
            !m_dc->connectDCOPSignal("kded", "mediamanager", "mediumChanged(TQString)", "devices", "mediumChanged(TQString)", false))
        {
            debug() << "DeviceManager:  Could not connect to signal mediumAdded/Removed/Changed!" << endl;
        }
        else
        {
            m_valid = true;
            //run the DCOP query here because apparently if you don't run KDE as a DM the first call will fail
            //...go figure
            TQByteArray data, replyData;
            TQCString replyType;
            TQDataStream arg(data, IO_WriteOnly);
            TQStringList result;
            arg << 5;
            if (!m_dc->call("kded", "mediamanager", "fullList()", data, replyType, replyData, false, -1))
            {
                debug() << "During DeviceManager init, error during DCOP call" << endl;
            }
            reconcileMediumMap();
            debug() << "DeviceManager:  connectDCOPSignal returned successfully!" << endl;
        }
    }
}

DeviceManager::~DeviceManager()
{
    for( MediumIterator it = m_mediumMap.begin(); it != m_mediumMap.end(); it++ )
        delete (*it);
}

void
DeviceManager::mediumAdded( const TQString name )
{
    DEBUG_BLOCK
    if ( !m_valid )
        return;
    Medium* addedMedium = getDevice(name);
    if ( addedMedium != 0 )
        debug() << "[DeviceManager::mediumAdded] Obtained medium name is " << name << ", id is: " << addedMedium->id() << endl;
    else
        debug() << "[DeviceManager::mediumAdded] Obtained medium is null; name was " << name << endl;
    emit mediumAdded( addedMedium, name );
}


void
DeviceManager::mediumRemoved( const TQString name )
{
    DEBUG_BLOCK
    if ( !m_valid )
        return;
    Medium* removedMedium = 0;
    if ( m_mediumMap.contains(name) )
        removedMedium = m_mediumMap[name];
    if ( removedMedium != 0 )
        debug() << "[DeviceManager::mediumRemoved] Obtained medium name is " << name << ", id is: " << removedMedium->id() << endl;
    else
        debug() << "[DeviceManager::mediumRemoved] Medium was unknown and is null; name was " << name << endl;
    //if you get a null pointer from this signal, it means we did not know about the device
    //before it was removed, i.e. the removal was the first event for the device received while amarok
    //has been running
    //There is no point in calling getDevice, since it will not be in the list anyways
    emit mediumRemoved( removedMedium, name );
    if ( m_mediumMap.contains(name) )
    {
        delete removedMedium;   //If we are to remove it from the map, delete it first
        m_mediumMap.remove(name);
    }
}


void
DeviceManager::mediumChanged( const TQString name )
{
    DEBUG_BLOCK
    if ( !m_valid )
        return;
    Medium *changedMedium = getDevice(name);
    if ( changedMedium != 0 )
        debug() << "[DeviceManager::mediumChanged] Obtained medium name is " << name << ", id is: " << changedMedium->id() << endl;
    else
        debug() << "[DeviceManager::mediumChanged] Obtained medium is null; name was " << name << endl;
    emit mediumChanged( changedMedium, name );
}


/*
BIG FAT WARNING:
Values returned from the below function should not be counted on being unique!
For instance, there may be a Medium object in the TQMap that can be accessed through
other functions that has the same data as the Medium object returned, but is a different object.
As you should not be writing to this object, this is okay, however:

Use the Medium's name or id, not the pointer value, for equality comparison!!!

This function does rebuild the map every time it is called, however this should be rare enough
that it is not a problem.
*/
Medium::List
DeviceManager::getDeviceList()
{
    return Medium::createList( getDeviceStringList() );
}

QStringList
DeviceManager::getDeviceStringList()
{
    DEBUG_BLOCK
    MediumList currMediumList;

    if ( !m_valid )
    {
        TQStringList blah;
        return blah;
    }

    //normal kded Medium doesn't have autodetect, so decrease by 1
    int autodetect_insert = Medium::PROPERTIES_COUNT - 1;

    TQByteArray data, replyData;
    TQCString replyType;
    TQDataStream arg(data, IO_WriteOnly);
    TQStringList result;
    arg << 5;
    if (!m_dc->call("kded", "mediamanager", "fullList()", data, replyType, replyData))
    {
        debug() << "Error during DCOP call" << endl;
    }
    else
    {
        TQDataStream reply(replyData, IO_ReadOnly);
        while(!reply.atEnd())
        {
            reply >> result;
        }
        TQStringList::Iterator it;
        for( it = result.begin(); it != result.end(); ++it )
        {
            if (autodetect_insert == Medium::PROPERTIES_COUNT - 1)
                result.insert(it, TQString("true"));
            autodetect_insert--;
            if (autodetect_insert == -1)
                autodetect_insert = Medium::PROPERTIES_COUNT - 1;
        }
    }

    return result;
}

Medium*
DeviceManager::getDevice( const TQString name )
{
    DEBUG_BLOCK
    if ( !m_valid )
        return 0;
    debug() << "DeviceManager: getDevice called with name argument = " << name << endl;
    Medium* returnedMedium = 0;
    MediumList currMediumList = getDeviceList();

    for ( Medium::List::iterator it = currMediumList.begin(); it != currMediumList.end(); ++it )
    {
        if ( (*it).name() == name )
        {
            MediumIterator secIt;
            if ( (secIt = m_mediumMap.find( name )) != m_mediumMap.end() )
            {
                //Refresh the Medium by reconstructing then copying it over.
                returnedMedium = *secIt;
                *returnedMedium = Medium( *it );
            }
            else
            {
                //No previous version of this Medium - create it
                returnedMedium = new Medium( *it );
                m_mediumMap[ name ] = returnedMedium;
            }
            break;
        }
    }
    return returnedMedium;
}

void
DeviceManager::reconcileMediumMap()
{
    DEBUG_BLOCK
    if ( !m_valid )
        return;

    MediumList currMediumList = getDeviceList();

    Medium::List::iterator it;
    for ( it = currMediumList.begin(); it != currMediumList.end(); ++it )
    {
        MediumIterator locIt;
        if ( (locIt = m_mediumMap.find( (*it).name() )) != m_mediumMap.end() )
        {
            Medium* mediumHolder = (*locIt);
            *mediumHolder = Medium( *it );
        }
        else
            m_mediumMap[ (*it).name() ] = new Medium(*it);
    }

    //Sanity check
    if ( currMediumList.size() != m_mediumMap.size() )
        warning() << "Number of devices does not equal expected number" << endl;
}

TQString DeviceManager::convertMediaUrlToDevice( TQString url )
{
    TQString device;
    if ( url.startsWith( "media:" ) || url.startsWith( "system:" ) )
    {
        KURL devicePath( url );
        DCOPRef mediamanager( "kded", "mediamanager" );
        DCOPReply reply = mediamanager.call( "properties(TQString)", devicePath.fileName() );
        if ( reply.isValid() ) {
            TQStringList properties = reply;
            device = properties[ 5 ];
            //kdDebug() << "DeviceManager::convertMediaUrlToDevice() munged to: " << device << "\n";
        } else
            device = TQString();
    }
    else
        device = url;

    return device;
}

#include "devicemanager.moc"