summaryrefslogtreecommitdiffstats
path: root/libk3bdevice/k3bdevicemanager.h
blob: fa687a912d288ca6250ba609a6f17cc29ce5019f (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
/*
 *
 * $Id: k3bdevicemanager.h 619556 2007-01-03 17:38:12Z trueg $
 * Copyright (C) 2003-2007 Sebastian Trueg <trueg@k3b.org>
 *
 * This file is part of the K3b project.
 * Copyright (C) 1998-2007 Sebastian Trueg <trueg@k3b.org>
 *
 * 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.
 */


#ifndef K3BDEVICEMANAGER_H
#define K3BDEVICEMANAGER_H

#include <tqobject.h>
#include <tqstring.h>
#include <tqstringlist.h>
#include <tqmemarray.h>
#include <tqptrlist.h>

#include "k3bdevice_export.h"
#include <kdebug.h>

class TDEProcess;
class TDEConfig;
class K3bExternalBin;


namespace K3bDevice {

  class Device;

  /**
   * \brief Manages all devices.
   *
   * Searches the system for devices and maintains lists of them.
   *
   * <b>Basic usage:</b>
   * \code
   *   K3bDevice::DeviceManager* manager = new K3bDevice::DeviceManager( this );
   *   manager->scanBus();
   *   K3bDevice::Device* dev = manager->findDevice( "/dev/cdrom" );
   * \endcode
   */
  class LIBK3BDEVICE_EXPORT DeviceManager : public TQObject
    {
      Q_OBJECT
  

    public:
      /**
       * Creates a new DeviceManager
       */
      DeviceManager( TQObject* parent = 0, const char* name = 0 );
      virtual ~DeviceManager();

      /**
       * By default the DeviceManager makes the Devices check their writing modes.
       * This includes commands to be sent which require writing permissions on the
       * devices and might take some time.
       *
       * So if you don't need the information about the writing modes use this method
       * to speed up the device detection procedure.
       *
       * Be aware that this only refers to CD writing modes. If you only want to handle
       * DVD devices it's always save to set this to false.
       */
      void setCheckWritingModes( bool b );

      /**
       * \deprecated use findDevice( const TQString& )
       */
      Device* deviceByName( const TQString& );

      /**
       * Search an SCSI device by SCSI bus, id, and lun.
       *
       * \note This method does not initialize new devices.
       *       Devices cannot be found until they have been added via addDevice(const TQString&)
       *       or scanBus().
       *
       * \return The corresponding device or 0 if there is no such device.
       */
      Device* findDevice( int bus, int id, int lun );

      /**
       * Search a device by blockdevice name.
       *
       * \note This method does not initialize new devices.
       *       Devices cannot be found until they have been added via addDevice(const TQString&)
       *       or scanBus().
       *
       * \return The corresponding device or 0 if there is no such device.
       */
      Device* findDevice( const TQString& devicename );

      /**
       * Before getting the devices do a @ref scanBus().
       * \return List of all cd writer devices.
       * \deprecated use cdWriter()
       */
      const TQPtrList<Device>& burningDevices() const;

      /**
       * \return List of all reader devices without writer devices.
       * \deprecated use cdReader()
       **/
      const TQPtrList<Device>& readingDevices() const;

      /**
       * Before getting the devices do a @ref scanBus() or add 
       * devices via addDevice( const TQString& ).
       *
       * \return List of all devices.
       */
      const TQPtrList<Device>& allDevices() const;

      /**
       * Before getting the devices do a @ref scanBus() or add 
       * devices via addDevice( const TQString& ).
       *
       * \return List of all cd writer devices.
       */
      const TQPtrList<Device>& cdWriter() const;

      /**
       * Before getting the devices do a @ref scanBus() or add 
       * devices via addDevice( const TQString& ).
       *
       * \return List of all cd reader devices.
       */
      const TQPtrList<Device>& cdReader() const;

      /**
       * Before getting the devices do a @ref scanBus() or add 
       * devices via addDevice( const TQString& ).
       *
       * \return List of all DVD writer devices.
       */
      const TQPtrList<Device>& dvdWriter() const;

      /**
       * Before getting the devices do a @ref scanBus() or add 
       * devices via addDevice( const TQString& ).
       *
       * \return List of all DVD reader devices.
       */
      const TQPtrList<Device>& dvdReader() const;

      /**
       * Before getting the devices do a @ref scanBus() or add 
       * devices via addDevice( const TQString& ).
       *
       * \return List of all Blue Ray reader devices.
       */
      const TQPtrList<Device>& blueRayReader() const;

      /**
       * Before getting the devices do a @ref scanBus() or add 
       * devices via addDevice( const TQString& ).
       *
       * \return List of all Blue Ray writer devices.
       */
      const TQPtrList<Device>& blueRayWriters() const;

      /**
       * Reads the device information from the config file.
       */
      virtual bool readConfig( TDEConfig* );

      virtual bool saveConfig( TDEConfig* );


    public slots:
      /**
       * Writes a list of all devices to stderr.
       */
      void printDevices();

      /**
       * Scan the system for devices. Call this to initialize all devices.
       * 
       * If the system uses the HAL device deamon it is possible to use
       * HalConnection instead of calling this method.
       *
       * \return Number of found devices.
       **/
      virtual int scanBus();

      /**
       * Clears the writers and readers list of devices.
       */
      virtual void clear();

      /**
       * Add a new device.
       *
       * \param dev Name of a block device or link to a block device. If the 
       *            corresponding device has already been detected it will simply
       *            be returned. Otherwise if a device is found it will be initialized
       *            and added to the internal lists (meaning it can be accessed through
       *            emthods like cdReader()).
       *
       * Called by scanBus()
       *
       * \return The device if it could be found or 0 otherwise.
       */
      virtual Device* addDevice( const TQString& dev );

      /**
       * Remove a device from the device manager. Basicly this method
       * only makes sense in combination with the HalConnection. Connect
       * it to the deviceRemoved signal.
       */
      virtual void removeDevice( const TQString& dev );

    signals:
      /**
       * Emitted if the device configuration changed, i.e. a device was added or removed.
       */
      void changed( K3bDevice::DeviceManager* );
      void changed();

    private:
      bool testForCdrom( const TQString& );
      bool determineBusIdLun( const TQString &dev, int& bus, int& id, int& lun );
      TQString resolveSymLink( const TQString& path );

      class Private;
      Private* d;

      /**
       * Add a device to the managers device lists and initialize the device.
       */
      Device *addDevice( Device* );

      void BSDDeviceScan();
      void NetBSDDeviceScan();
      void LinuxDeviceScan();
    };
}

#endif