Debian Codegraph @

System/GIO/Volumes/Drive.chs

raw
1 {-# LANGUAGE CPP #-}
2 -- GIMP Toolkit (GTK) Binding for Haskell: binding to gio -*-haskell-*-
3 --
4 -- Author : Andy Stewart
5 -- Created: 30-Apirl-2010
6 --
7 -- Copyright (c) 2010 Andy Stewart
8 --
9 -- This library is free software: you can redistribute it and/or
10 -- modify it under the terms of the GNU Lesser General Public License
11 -- as published by the Free Software Foundation, either version 3 of
12 -- the License, or (at your option) any later version.
13 --
14 -- This library is distributed in the hope that it will be useful,
15 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
16 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 -- Lesser General Public License for more details.
18 --
19 -- You should have received a copy of the GNU Lesser General Public
20 -- License along with this program. If not, see
21 -- <http://www.gnu.org/licenses/>.
22 --
23 -- GIO, the C library which this Haskell library depends on, is
24 -- available under LGPL Version 2. The documentation included with
25 -- this library is based on the original GIO documentation.
26 --
27 -- | Maintainer : [email protected]
28 -- Stability : alpha
29 -- Portability : portable (depends on GHC)
30 module System.GIO.Volumes.Drive (
31 -- * Details
32 --
33 -- | 'Drive' - this represent a piece of hardware connected to the machine. It's generally only created
34 -- for removable hardware or hardware with removable media.
35 --
36 -- 'Drive' is a container class for 'Volume' objects that stem from the same piece of media. As such,
37 -- 'Drive' abstracts a drive with (or without) removable media and provides operations for querying
38 -- whether media is available, determining whether media change is automatically detected and ejecting
39 -- the media.
40 --
41 -- If the 'Drive' reports that media isn't automatically detected, one can poll for media; typically one
42 -- should not do this periodically as a poll for media operation is potentially expensive and may
43 -- spin up the drive creating noise.
44 --
45 -- 'Drive' supports starting and stopping drives with authentication support for the former. This can be
46 -- used to support a diverse set of use cases including connecting/disconnecting iSCSI devices,
47 -- powering down external disk enclosures and starting/stopping multi-disk devices such as RAID
48 -- devices. Note that the actual semantics and side-effects of starting/ stopping a 'Drive' may vary
49 -- according to implementation. To choose the correct verbs in e.g. a file manager, use
50 -- 'driveGetStartStopType'.
51 --
52 -- For porting from GnomeVFS note that there is no equivalent of 'Drive' in that API.
53
54 -- * Types
55 Drive(..),
56 DriveClass,
57
58 -- * Enums,
59 #if GLIB_CHECK_VERSION(2,22,0)
60 DriveStartStopType (..),
61 DriveStartFlags (..),
62 #endif
63
64 -- * Methods
65 driveGetName,
66 driveGetIcon,
67 driveHasVolumes,
68 driveGetVolumes,
69 driveCanEject,
70 #if GLIB_CHECK_VERSION(2,22,0)
71 driveGetStartStopType,
72 driveCanStart,
73 driveCanStartDegraded,
74 driveCanStop,
75 #endif
76 driveCanPollForMedia,
77 drivePollForMedia,
78 driveHasMedia,
79 driveIsMediaCheckAutomatic,
80 driveIsMediaRemovable,
81 #if GLIB_CHECK_VERSION(2,22,0)
82 driveEjectWithOperation,
83 driveEjectWithOperationFinish,
84 driveStart,
85 driveStartFinish,
86 driveStopFinish,
87 #endif
88 driveEnumerateIdentifiers,
89 driveGetIdentifier,
90
91 -- * Signals
92 driveChanged,
93 driveDisconnected,
94 driveEjectButton,
95 #if GLIB_CHECK_VERSION(2,22,0)
96 driveStopButton,
97 #endif
98 ) where
99
100 import Control.Monad
101 import Data.Maybe (fromMaybe)
102 import System.GIO.Enums
103 import System.Glib.Attributes
104 import System.Glib.FFI
105 import System.Glib.Flags
106 import System.Glib.GError
107 import System.Glib.GList
108 import System.Glib.GObject
109 import System.Glib.Properties
110 import System.Glib.Signals
111 import System.Glib.UTFString
112 {#import System.GIO.Async.AsyncResult#}
113 {#import System.GIO.Signals#}
114 {#import System.GIO.Types#}
115
116 {# context lib = "gio" prefix = "g" #}
117
118 --------------------
119 -- Methods
120 -- | Gets the name of drive.
121 driveGetName :: (DriveClass drive, GlibString string) => drive
122 -> IO string -- ^ returns the name for the given drive.
123 driveGetName drive =
124 {#call g_drive_get_name#} (toDrive drive)
125 >>= readUTFString
126
127 -- | Gets the icon for drive.
128 driveGetIcon :: DriveClass drive => drive
129 -> IO Icon -- ^ returns a 'Icon'.
130 driveGetIcon drive =
131 wrapNewGObject mkIcon $
132 {#call g_drive_get_icon#} (toDrive drive)
133
134 -- | Check if drive has any mountable volumes.
135 driveHasVolumes :: DriveClass drive => drive
136 -> IO Bool -- ^ returns 'True' if the drive contains volumes, 'False' otherwise.
137 driveHasVolumes drive =
138 liftM toBool $
139 {#call g_drive_has_volumes#} (toDrive drive)
140
141 -- | Get a list of mountable volumes for drive.
142 driveGetVolumes :: DriveClass drive => drive
143 -> IO [Volume]
144 driveGetVolumes drive = do
145 glistPtr <- {#call g_drive_get_volumes #} (toDrive drive)
146 volumePtrs <- fromGList glistPtr
147 mapM (wrapNewGObject mkVolume . return) volumePtrs
148
149 -- | Checks if drive can be eject.
150 driveCanEject :: DriveClass drive => drive
151 -> IO Bool -- ^ returns 'True' if the drive can be ejected.
152 driveCanEject drive =
153 liftM toBool $
154 {#call g_drive_can_eject#} (toDrive drive)
155
156 #if GLIB_CHECK_VERSION(2,22,0)
157 -- | Gets a hint about how a drive can be started/stopped.
158 driveGetStartStopType :: DriveClass drive => drive
159 -> IO DriveStartStopType -- ^ returns A value from the 'DriveStartStopType' enumeration.
160 driveGetStartStopType drive =
161 liftM (toEnum . fromIntegral) $
162 {#call g_drive_get_start_stop_type#} (toDrive drive)
163
164 -- | Checks if a drive can be started.
165 driveCanStart :: DriveClass drive => drive
166 -> IO Bool -- ^ returns 'True' if the drive can be started, 'False' otherwise.
167 driveCanStart drive =
168 liftM toBool $
169 {#call g_drive_can_start#} (toDrive drive)
170
171 -- | Checks if a drive can be started degraded.
172 driveCanStartDegraded :: DriveClass drive => drive
173 -> IO Bool -- ^ returns 'True' if the drive can be started degraded, 'False' otherwise.
174 driveCanStartDegraded drive =
175 liftM toBool $
176 {#call g_drive_can_start_degraded#} (toDrive drive)
177
178 -- | Checks if a drive can be stopped.
179 driveCanStop :: DriveClass drive => drive
180 -> IO Bool -- ^ returns 'True' if the drive can be stopped, 'False' otherwise.
181 driveCanStop drive =
182 liftM toBool $
183 {#call g_drive_can_stop#} (toDrive drive)
184 #endif
185
186 -- | Checks if a drive can be polled for media changes.
187 driveCanPollForMedia :: DriveClass drive => drive
188 -> IO Bool
189 driveCanPollForMedia drive =
190 liftM toBool $
191 {#call g_drive_can_poll_for_media#} (toDrive drive)
192
193 -- | Asynchronously polls drive to see if media has been inserted or removed.
194 --
195 -- When the operation is finished, callback will be called. You can then call
196 -- 'drivePollForMediaFinish' to obtain the result of the operation.
197 drivePollForMedia :: DriveClass drive => drive
198 -> Maybe Cancellable
199 -> AsyncReadyCallback
200 -> IO ()
201 drivePollForMedia drive cancellable callback = do
202 cCallback <- marshalAsyncReadyCallback callback
203 {#call g_drive_poll_for_media #}
204 (toDrive drive)
205 (fromMaybe (Cancellable nullForeignPtr) cancellable)
206 cCallback
207 (castFunPtrToPtr cCallback)
208
209 -- | Finishes an operation started with 'drivePollForMedia' on a drive.
210 --
211 -- Throws a 'GError' if an error occurs.
212 drivePollForMediaFinish :: DriveClass drive => drive
213 -> AsyncResult -- ^ @result@ a 'AsyncResult'.
214 -> IO ()
215 drivePollForMediaFinish drive result =
216 propagateGError (\gErrorPtr -> do
217 {#call g_drive_poll_for_media_finish #}
218 (toDrive drive)
219 result
220 gErrorPtr
221 return ())
222
223 -- | Checks if the drive has media. Note that the OS may not be polling the drive for media changes; see
224 -- 'driveIsMediaCheckAutomatic' for more details.
225 driveHasMedia :: DriveClass drive => drive
226 -> IO Bool -- ^ returns 'True' if drive has media, 'False' otherwise.
227 driveHasMedia drive =
228 liftM toBool $
229 {#call g_drive_has_media#} (toDrive drive)
230
231 -- | Checks if drive is capable of automatically detecting media changes.
232 driveIsMediaCheckAutomatic :: DriveClass drive => drive
233 -> IO Bool -- ^ returns 'True' if the drive is capable of automatically detecting media changes, 'False' otherwise.
234 driveIsMediaCheckAutomatic drive =
235 liftM toBool $
236 {#call g_drive_is_media_check_automatic#} (toDrive drive)
237
238 -- | Checks if the drive supports removable media.
239 driveIsMediaRemovable :: DriveClass drive => drive
240 -> IO Bool -- ^ returns 'True' if drive supports removable media, 'False' otherwise.
241 driveIsMediaRemovable drive =
242 liftM toBool $
243 {#call g_drive_is_media_removable#} (toDrive drive)
244
245 #if GLIB_CHECK_VERSION(2,22,0)
246 -- | Ejects a drive. This is an asynchronous operation, and is finished by calling
247 -- 'driveEjectWithOperationFinish' with the drive and 'AsyncResult' data returned in the callback.
248 driveEjectWithOperation :: DriveClass drive
249 => drive
250 -> [MountUnmountFlags] -- ^ @flags@ flags affecting the unmount if required for eject
251 -> Maybe MountOperation -- ^ @mountOperation@ a 'MountOperation' or 'Nothing' to avoid user interaction.
252 -> Maybe Cancellable -- ^ @cancellable@ optional 'Cancellable' object, 'Nothing' to ignore.
253 -> AsyncReadyCallback -- ^ @callback@ a 'AsyncReadyCallback'
254 -> IO ()
255 driveEjectWithOperation drive flags mountOperation cancellable callback = do
256 cCallback <- marshalAsyncReadyCallback callback
257 {#call g_drive_eject_with_operation #}
258 (toDrive drive)
259 ((fromIntegral . fromFlags) flags)
260 (fromMaybe (MountOperation nullForeignPtr) mountOperation)
261 (fromMaybe (Cancellable nullForeignPtr) cancellable)
262 cCallback
263 (castFunPtrToPtr cCallback)
264
265 -- | Finishes ejecting a drive. If any errors occurred during the operation, error will be set to contain
266 -- the errors and 'False' will be returned.
267 --
268 -- Throws a 'GError' if an error occurs.
269 driveEjectWithOperationFinish :: DriveClass drive
270 => drive
271 -> AsyncResult -- ^ @result@ a 'AsyncResult'.
272 -> IO ()
273 driveEjectWithOperationFinish drive result =
274 propagateGError (\gErrorPtr -> do
275 {#call g_drive_eject_with_operation_finish #}
276 (toDrive drive)
277 result
278 gErrorPtr
279 return ())
280
281 -- | Asynchronously starts a drive.
282 --
283 -- When the operation is finished, callback will be called. You can then call 'driveStartFinish' to
284 -- obtain the result of the operation.
285 driveStart :: DriveClass drive
286 => drive
287 -> [DriveStartFlags] -- ^ @flags@ flags affecting the start operation.
288 -> Maybe MountOperation -- ^ @mountOperation@ a 'MountOperation' or 'Nothing' to avoid user interaction.
289 -> Maybe Cancellable -- ^ @cancellable@ optional 'Cancellable' object, 'Nothing' to ignore.
290 -> AsyncReadyCallback -- ^ @callback@ a 'AsyncReadyCallback'
291 -> IO ()
292 driveStart drive flags mountOperation cancellable callback = do
293 cCallback <- marshalAsyncReadyCallback callback
294 {#call g_drive_start #}
295 (toDrive drive)
296 ((fromIntegral . fromFlags) flags)
297 (fromMaybe (MountOperation nullForeignPtr) mountOperation)
298 (fromMaybe (Cancellable nullForeignPtr) cancellable)
299 cCallback
300 (castFunPtrToPtr cCallback)
301
302 -- | Finishes starting a drive.
303 --
304 -- Throws a 'GError' if an error occurs.
305 driveStartFinish :: DriveClass drive
306 => drive
307 -> AsyncResult -- ^ @result@ a 'AsyncResult'.
308 -> IO ()
309 driveStartFinish drive result =
310 propagateGError (\gErrorPtr -> do
311 {#call g_drive_start_finish #}
312 (toDrive drive)
313 result
314 gErrorPtr
315 return ())
316
317 -- | Asynchronously stops a drive.
318 --
319 -- When the operation is finished, callback will be called. You can then call 'driveStopFinish' to
320 -- obtain the result of the operation.
321 driveStop :: DriveClass drive
322 => drive
323 -> [MountUnmountFlags] -- ^ @flags@ flags affecting the stop operation.
324 -> Maybe MountOperation -- ^ @mountOperation@ a 'MountOperation' or 'Nothing' to avoid user interaction.
325 -> Maybe Cancellable -- ^ @cancellable@ optional 'Cancellable' object, 'Nothing' to ignore.
326 -> AsyncReadyCallback -- ^ @callback@ a 'AsyncReadyCallback'
327 -> IO ()
328 driveStop drive flags mountOperation cancellable callback = do
329 cCallback <- marshalAsyncReadyCallback callback
330 {#call g_drive_stop #}
331 (toDrive drive)
332 ((fromIntegral . fromFlags) flags)
333 (fromMaybe (MountOperation nullForeignPtr) mountOperation)
334 (fromMaybe (Cancellable nullForeignPtr) cancellable)
335 cCallback
336 (castFunPtrToPtr cCallback)
337
338 -- | Finishes stopping a drive.
339 --
340 -- Throws a 'GError' if an error occurs.
341 driveStopFinish :: DriveClass drive
342 => drive
343 -> AsyncResult -- ^ @result@ a 'AsyncResult'.
344 -> IO ()
345 driveStopFinish drive result =
346 propagateGError (\gErrorPtr -> do
347 {#call g_drive_stop_finish #}
348 (toDrive drive)
349 result
350 gErrorPtr
351 return ())
352 #endif
353
354 -- | Gets the kinds of identifiers that drive has. Use 'driveGetIdentifier' to obtain the
355 -- identifiers themselves.
356 driveEnumerateIdentifiers :: (DriveClass drive, GlibString string) => drive
357 -> IO [string]
358 driveEnumerateIdentifiers drive =
359 {#call g_drive_enumerate_identifiers#} (toDrive drive)
360 >>= readUTFStringArray0
361
362 -- | Gets the identifier of the given kind for drive. See the introduction for more information about
363 -- drive identifiers.
364 driveGetIdentifier :: (DriveClass drive, GlibString string) => drive
365 -> string -- ^ @kind@ the kind of identifier to return
366 -> IO string
367 driveGetIdentifier drive kind =
368 withUTFString kind $ \ kindPtr ->
369 {#call g_drive_get_identifier#} (toDrive drive) kindPtr
370 >>= readUTFString
371
372 --------------------
373 -- Signals
374 -- | Emitted when a drive changes.
375 driveChanged :: DriveClass drive => Signal drive (Drive -> IO ())
376 driveChanged = Signal (connect_OBJECT__NONE "drive-changed")
377
378 -- | Emitted when a drive changes.
379 driveDisconnected :: DriveClass drive => Signal drive (Drive -> IO ())
380 driveDisconnected = Signal (connect_OBJECT__NONE "drive-disconnected")
381
382 -- | Emitted when the eject button is pressed on drive.
383 driveEjectButton :: DriveClass drive => Signal drive (Drive -> IO ())
384 driveEjectButton = Signal (connect_OBJECT__NONE "drive-eject-button")
385
386 #if GLIB_CHECK_VERSION(2,22,0)
387 -- | Emitted when the stop button is pressed on drive.
388 driveStopButton :: DriveClass drive => Signal drive (Drive -> IO ())
389 driveStopButton = Signal (connect_OBJECT__NONE "drive-stop-button")
390 #endif