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
|
/*
*
* $Id: k3bdevicecombobox.cpp 619556 2007-01-03 17:38:12Z trueg $
* Copyright (C) 2003 Sebastian Trueg <[email protected]>
*
* This file is part of the K3b project.
* Copyright (C) 1998-2007 Sebastian Trueg <[email protected]>
*
* 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.
* See the file "COPYING" for the exact licensing terms.
*/
#include "k3bdevicecombobox.h"
#include <k3bdevice.h>
#include <k3bdevicemanager.h>
#include <k3bcore.h>
#include <tdelocale.h>
#include <tqmap.h>
#include <tqptrvector.h>
class K3bDeviceComboBox::Private
{
public:
TQMap<TQString, int> deviceIndexMap;
TQPtrVector<K3bDevice::Device> devices;
};
K3bDeviceComboBox::K3bDeviceComboBox( TQWidget* parent, const char* name )
: KComboBox( parent, name )
{
d = new Private();
connect( this, TQT_SIGNAL(activated(int)),
this, TQT_SLOT(slotActivated(int)) );
connect( k3bcore->deviceManager(), TQT_SIGNAL(changed(K3bDevice::DeviceManager*)),
this, TQT_SLOT(slotDeviceManagerChanged(K3bDevice::DeviceManager*)) );
}
K3bDeviceComboBox::~K3bDeviceComboBox()
{
delete d;
}
K3bDevice::Device* K3bDeviceComboBox::selectedDevice() const
{
if ( count() > 0 )
return d->devices[currentItem()];
else
return 0;
}
void K3bDeviceComboBox::addDevice( K3bDevice::Device* dev )
{
int devIndex = -2;
bool addDevice = false;
for( int i = 0; i < count(); ++i ) {
if( dev->vendor() == d->devices[i]->vendor() &&
dev->description() == d->devices[i]->description() ) {
addDevice = true;
if( devIndex < -1 ) // when devIndex == -1 we already found two devices.
devIndex = i;
else
devIndex = -1; // when there are already two or more equal devices they have already been updated
}
}
// update the existing device item
if( devIndex >= 0 ) {
changeItem( d->devices[devIndex]->vendor() + " " +
d->devices[devIndex]->description() +
" (" + d->devices[devIndex]->blockDeviceName() + ")",
devIndex );
d->deviceIndexMap[d->devices[devIndex]->devicename()] = devIndex;
}
// add the new device item
if( addDevice )
insertItem( dev->vendor() + " " + dev->description() + " (" + dev->blockDeviceName() + ")" );
else
insertItem( dev->vendor() + " " + dev->description() );
d->deviceIndexMap[dev->devicename()] = count()-1;
d->devices.resize( count() );
d->devices.insert(count()-1, dev);
}
void K3bDeviceComboBox::removeDevice( K3bDevice::Device* dev )
{
if( dev ) {
if( d->deviceIndexMap.contains(dev->devicename()) ) {
// let's make it easy and recreate the whole list
K3bDevice::Device* selDev = selectedDevice();
TQPtrList<K3bDevice::Device> devices;
for( unsigned int i = 0; i < d->devices.size(); ++i )
devices.append( d->devices[i] );
clear();
devices.removeRef( dev );
addDevices( devices );
setSelectedDevice( selDev );
}
}
}
void K3bDeviceComboBox::addDevices( const TQPtrList<K3bDevice::Device>& list )
{
for( TQPtrListIterator<K3bDevice::Device> it( list );
it.current(); ++it )
addDevice( it.current() );
}
void K3bDeviceComboBox::refreshDevices( const TQPtrList<K3bDevice::Device>& list )
{
K3bDevice::Device* selDev = selectedDevice();
clear();
if( !list.containsRef( selDev ) )
selDev = 0;
addDevices( list );
setSelectedDevice( selDev );
}
void K3bDeviceComboBox::setSelectedDevice( K3bDevice::Device* dev )
{
if( dev ) {
if( d->deviceIndexMap.contains(dev->devicename()) ) {
setCurrentItem( d->deviceIndexMap[dev->devicename()] );
emit selectionChanged( dev );
}
}
}
void K3bDeviceComboBox::clear()
{
d->deviceIndexMap.clear();
d->devices.clear();
KComboBox::clear();
}
void K3bDeviceComboBox::slotActivated( int i )
{
emit selectionChanged( d->devices[i] );
}
void K3bDeviceComboBox::slotDeviceManagerChanged( K3bDevice::DeviceManager* dm )
{
unsigned int i = 0;
while( i < d->devices.size() ) {
if( !dm->allDevices().containsRef( d->devices[i] ) ) {
removeDevice( d->devices[i] );
i = 0;
}
else
++i;
}
}
#include "k3bdevicecombobox.moc"
|