summaryrefslogtreecommitdiffstats
path: root/libk3bdevice/k3bhalconnection.h
blob: 583bbf2f340cf21667ec52582b471b5f76ff735f (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
/* 
 *
 * $Id: sourceheader,v 1.3 2005/01/19 13:03:46 trueg Exp $
 * Copyright (C) 2005-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 _K3B_HAL_CONNECTION_H_
#define _K3B_HAL_CONNECTION_H_

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

#include "k3bdevice_export.h"

#include <qobject.h>
#include <qmap.h>
#include <qstringlist.h>

class DBusConnection;


namespace K3bDevice {

  class Device;

  /**
   * This is a simple HAL/DBUS wrapper which creates QT signals whenever a new optical
   * drive is plugged into the system or one is unplugged.
   *
   * The HalConnection 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 HalConnection : public QObject
    {
      Q_OBJECT

    public:
      ~HalConnection();

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

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

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

      /**
       * \internal
       */
      void addDevice( const char* udi );

      /**
       * \internal
       */
      void removeDevice( const char* udi );

      /**
       * Error codes named as the HAL deamon raises them
       */
      enum ErrorCodes {
	org_freedesktop_Hal_Success = 0, //*< The operation was successful. This code does not match any in HAL
	org_freedesktop_Hal_CommunicationError, //*< DBus communication error. This code does not match any in HAL
	org_freedesktop_Hal_NoSuchDevice,
	org_freedesktop_Hal_DeviceAlreadyLocked,
	org_freedesktop_Hal_PermissionDenied,
	org_freedesktop_Hal_Device_Volume_NoSuchDevice,
	org_freedesktop_Hal_Device_Volume_PermissionDenied,
	org_freedesktop_Hal_Device_Volume_AlreadyMounted,
	org_freedesktop_Hal_Device_Volume_InvalidMountOption,
	org_freedesktop_Hal_Device_Volume_UnknownFilesystemType,
	org_freedesktop_Hal_Device_Volume_InvalidMountpoint,
	org_freedesktop_Hal_Device_Volume_MountPointNotAvailable,
	org_freedesktop_Hal_Device_Volume_PermissionDeniedByPolicy,
	org_freedesktop_Hal_Device_Volume_InvalidUnmountOption,
	org_freedesktop_Hal_Device_Volume_InvalidEjectOption
      };

     public slots:
      /**
       * Lock the device in HAL
       * 
       * Be aware that once the method returns the HAL 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 in HAL
       * 
       * Be aware that once the method returns the HAL 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 via HAL
       * 
       * Be aware that once the method returns the HAL deamon has not necessarily 
       * finished the procedure yet.
       *
       * \param dev The device to lock
       * \return An error code
       *
       * \see ErrorCode
       */
      int mount( Device*, 
		 const QString& mountPoint = QString::null, 
		 const QString& fstype = QString::null,
		 const QStringList& options = QStringList() );

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

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

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

      /**
       * This signal gets emitted whenever HAL detects that an optical drive
       * has been unplugged.
       *
       * \param dev The block device name of the drive.
       */
      void deviceRemoved( const QString& 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 QString& dev );

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

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

      static HalConnection* s_instance;

      class Private;
      Private* d;

      void setupDBusQtConnection( DBusConnection* dbusConnection );
    };
}

#endif