summaryrefslogtreecommitdiffstats
path: root/libk3bdevice/k3bconnection.h
blob: 9deb458d62c8a627c397bf6a1f2b32d7418086a4 (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
/* 
 *
 * $Id: sourceheader,v 1.3 2005/01/19 13:03:46 trueg Exp $
 * Copyright (C) 2005-2007 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.
 */

#ifndef _K3B_CONNECTION_H_
#define _K3B_CONNECTION_H_

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "k3bdevice_export.h"

#include <tqobject.h>
#include <tqmap.h>
#include <tqstringlist.h>
#include <kdemacros.h>

#ifdef HAVE_TDEHWLIB
class TDEGenericDevice;
#else
#define TDEGenericDevice void
#endif

namespace K3bDevice {

  class Device;

  /**
   * This is a simple wrapper which creates TQt signals whenever a new optical
   * drive is plugged into the system or one is unplugged.
   *
   * The Connection class also handles media changes. Whenever a new medium is inserted
   * into a drive or a medium is removed (i.e. ejected) a signal is emitted. This way it
   * is easy to keep track of the inserted media.
   *
   * This class does not deal with K3b devices but with system device names
   * such as /dev/cdrom. These device names can be used in DeviceManager::findDevice().
   */
  class LIBK3BDEVICE_EXPORT Connection : public TQObject
    {
      TQ_OBJECT
  

    public:
      ~Connection();

      /**
       * Creates a new singleton Connection object or returns the already existing one.
       * A newly created Connection will emit newDevice signals for all devices in the 
       * manager. However, since one cannot be sure if this is the first time the Connection
       * is created it is recommended to connect to the signals and query the list of current
       * devices.
       *
       * \return An instance of the singleton Connection object.
       */
      static Connection* instance();

      /**
       * \return true if a connection to the deamon could be established and
       *         communication has been set up.
       */
      bool isConnected() const;

      /**
       * \return a list of optical devices as reported.
       */
      TQStringList devices() const;

      /**
       * Error codes named as the deamon raises them
       */
      enum ErrorCodes {
	Success = 0, //*< The operation was successful.
	CommunicationError,
	NoSuchDevice,
	DeviceAlreadyLocked,
	PermissionDenied,
	Device_Volume_NoSuchDevice,
	Device_Volume_PermissionDenied,
	Device_Volume_AlreadyMounted,
	Device_Volume_InvalidMountOption,
	Device_Volume_UnknownFilesystemType,
	Device_Volume_InvalidMountpoint,
	Device_Volume_MountPointNotAvailable,
	Device_Volume_PermissionDeniedByPolicy,
	Device_Volume_InvalidUnmountOption,
	Device_Volume_InvalidEjectOption
      };

     public slots:
      /**
       * Lock the device
       * 
       * Be aware that once the method returns the deamon has not necessarily 
       * finished the procedure yet.
       *
       * \param dev The device to lock
       * \return An error code
       *
       * \see ErrorCode
       */
      int lock( Device* );

      /**
       * Unlock a previously locked device
       * 
       * Be aware that once the method returns the deamon has not necessarily 
       * finished the procedure yet.
       *
       * \param dev The device to lock
       * \return An error code
       *
       * \see ErrorCode
       */
      int unlock( Device* );

      /**
       * Mounts a device
       * 
       * Be aware that once the method returns the deamon has not necessarily 
       * finished the procedure yet.
       *
       * \param dev The device to lock
       * \return An error code
       *
       * \see ErrorCode
       */
      int mount( Device*, 
		 const TQString& mountPoint = TQString(), 
		 const TQString& fstype = TQString(),
		 const TQStringList& options = TQStringList() );

      /**
       * Unmounts a device
       * 
       * Be aware that once the method returns the deamon has not necessarily 
       * finished the procedure yet.
       *
       * \param dev The device to lock
       * \return An error code
       *
       * \see ErrorCode
       */
      int unmount( Device*,
       const TQStringList& options = TQStringList() );

      /**
       * Unmounts a device
       * 
       * Be aware that once the method returns the deamon has not necessarily 
       * finished the procedure yet.
       *
       * \param dev The device to lock
       * \return An error code
       *
       * \see ErrorCode
       */
      int eject( Device*,
     const TQStringList& options = TQStringList() );

    private slots:
      /**
       * \internal
       */
      void AddDeviceHandler(TDEGenericDevice*);

      /**
       * \internal
       */
      void RemoveDeviceHandler(TDEGenericDevice*);

      /**
       * \internal
       */
      void ModifyDeviceHandler(TDEGenericDevice*);

    signals:
      /**
       * This signal gets emitted whenever it finds a new optical drive.
       *
       * \param dev The block device name of the new drive.
       */
      void deviceAdded( const TQString& dev );

      /**
       * This signal gets emitted whenever it detects that an optical drive
       * has been unplugged.
       *
       * \param dev The block device name of the drive.
       */
      void deviceRemoved( const TQString& dev );

      /**
       * This signal gets emitted whenever a new medium is inserted into a
       * device or an inserted is removed (i.e. ejected)
       *
       * \param dev The block device name of the drive the medium is or was inserted into.
       */
      void mediumChanged( const TQString& dev );

    private:
      /**
       * Connection is a signelton class. Use the instance() method to create it.
       */
      Connection( TQObject* parent = 0, const char* name = 0 );

      /**
       * Tries to open a connection
       */
      bool open();
      void close();

      static Connection* s_instance;

      class Private;
      Private* d;
    };
}

#endif