Debian Codegraph @

System/GIO/Volumes/Volume.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.Volume (
31 -- * Details
32 --
33 -- | The 'Volume' interface represents user-visible objects that can be mounted. Note, when porting from
34 -- GnomeVFS, 'Volume' is the moral equivalent of GnomeVFSDrive.
35 --
36 -- Mounting a 'Volume' instance is an asynchronous operation. For more information about asynchronous
37 -- operations, see 'AsyncReady' and GSimpleAsyncReady. To mount a 'Volume', first call 'volumeMount'
38 -- with (at least) the 'Volume' instance, optionally a 'MountOperation' object and a 'AsyncReadyCallback'.
39 --
40 -- Typically, one will only want to pass 'Nothing' for the 'MountOperation' if automounting all volumes when
41 -- a desktop session starts since it's not desirable to put up a lot of dialogs asking for credentials.
42 --
43 -- The callback will be fired when the operation has resolved (either with success or failure), and a
44 -- 'AsyncReady' structure will be passed to the callback. That callback should then call
45 -- 'volumeMountFinish' with the 'Volume' instance and the 'AsyncReady' data to see if the operation
46 -- was completed successfully. If an error is present when 'volumeMountFinish' is called, then it
47 -- will be filled with any error information.
48 --
49 -- It is sometimes necessary to directly access the underlying operating system object behind a volume
50 -- (e.g. for passing a volume to an application via the commandline). For this purpose, GIO allows to
51 -- obtain an 'identifier' for the volume. There can be different kinds of identifiers, such as Hal
52 -- UDIs, filesystem labels, traditional Unix devices (e.g. /dev/sda2), uuids. GIO uses predefind
53 -- strings as names for the different kinds of identifiers: 'VolumeIdentifierKindHalUdi',
54 -- 'VolumeIdentifierKindLabel', etc. Use 'volumeGetIdentifier' to obtain an identifier for a
55 -- volume.
56 --
57 -- Note that 'VolumeIdentifierKindHalUdi' will only be available when the gvfs hal volume monitor
58 -- is in use. Other volume monitors will generally be able to provide the
59 -- 'VolumeIdentifierKindUnixDevice' identifier, which can be used to obtain a hal device by means
60 -- of 'mangerFindDeviceStringMatch'.
61
62 -- * Types
63 Volume(..),
64 VolumeClass,
65
66 -- * Methods
67 volumeGetName,
68 volumeGetUUID,
69 volumeGetIcon,
70 volumeGetDrive,
71 volumeGetMount,
72 volumeCanMount,
73 volumeShouldAutomount,
74 #if GLIB_CHECK_VERSION(2,18,0)
75 volumeGetActivationRoot,
76 #endif
77 volumeMount,
78 volumeMountFinish,
79 volumeCanEject,
80 #if GLIB_CHECK_VERSION(2,22,0)
81 volumeEjectWithOperation,
82 volumeEjectWithOperationFinish,
83 #endif
84 volumeEnumerateIdentifiers,
85 volumeGetIdentifier,
86
87 -- * Signals
88 volumeChanged,
89 volumeRemoved,
90 ) where
91
92 import Control.Monad
93 import Data.Maybe (fromMaybe)
94 import System.GIO.Enums
95 import System.Glib.Attributes
96 import System.Glib.FFI
97 import System.Glib.Flags
98 import System.Glib.GError
99 import System.Glib.GObject
100 import System.Glib.Properties
101 import System.Glib.Signals
102 import System.Glib.UTFString
103 {#import System.GIO.Async.AsyncResult#}
104 {#import System.GIO.Signals#}
105 {#import System.GIO.Types#}
106
107 {# context lib = "gio" prefix = "g" #}
108
109 --------------------
110 -- Methods
111 -- | Gets the name of volume.
112 volumeGetName :: (VolumeClass volume, GlibString string) => volume
113 -> IO string -- ^ returns the name for the given volume.
114 volumeGetName volume =
115 {#call g_volume_get_name#} (toVolume volume)
116 >>= readUTFString
117
118 -- | Gets the UUID for the volume. The reference is typically based on the file system UUID for the
119 -- volume in question and should be considered an opaque string. Returns 'Nothing' if there is no UUID
120 -- available.
121 volumeGetUUID :: (VolumeClass volume, GlibString string) => volume
122 -> IO (Maybe string) -- ^ returns the UUID for volume or 'Nothing' if no UUID can be computed.
123 volumeGetUUID volume =
124 {#call g_volume_get_uuid#} (toVolume volume)
125 >>= maybePeek readUTFString
126
127 -- | Gets the icon for volume.
128 volumeGetIcon :: VolumeClass volume => volume
129 -> IO Icon
130 volumeGetIcon volume =
131 wrapNewGObject mkIcon $
132 {#call g_volume_get_icon#} (toVolume volume)
133
134 -- | Gets the drive for the volume.
135 volumeGetDrive :: VolumeClass volume => volume
136 -> IO (Maybe Drive) -- ^ returns a 'Drive' or 'Nothing' if volume is not associated with a drive.
137 volumeGetDrive volume =
138 maybeNull (wrapNewGObject mkDrive) $
139 {#call g_volume_get_drive#} (toVolume volume)
140
141 -- | Gets the mount for the volume.
142 volumeGetMount :: VolumeClass volume => volume
143 -> IO (Maybe Mount) -- ^ returns a 'Mount' or 'Nothing' if volume is not associated with a mount.
144 volumeGetMount volume =
145 maybeNull (wrapNewGObject mkMount) $
146 {#call g_volume_get_mount#} (toVolume volume)
147
148 -- | Checks if a volume can be mounted.
149 volumeCanMount :: VolumeClass volume => volume
150 -> IO Bool -- ^ returns 'True' if the volume can be mounted. 'False' otherwise.
151 volumeCanMount volume =
152 liftM toBool $
153 {#call g_volume_can_mount#} (toVolume volume)
154
155 -- | Returns whether the volume should be automatically mounted.
156 volumeShouldAutomount :: VolumeClass volume => volume
157 -> IO Bool -- ^ returns 'True' if the volume should be automatically mounted.
158 volumeShouldAutomount volume =
159 liftM toBool $
160 {#call g_volume_should_automount#} (toVolume volume)
161
162 #if GLIB_CHECK_VERSION(2,18,0)
163 -- | Gets the activation root for a 'Volume' if it is known ahead of mount time. Returns 'Nothing'
164 -- otherwise. If not 'Nothing' and if volume is mounted, then the result of 'mountGetRoot' on the 'Mount'
165 -- object obtained from 'volumeGetMount' will always either be equal or a prefix of what this
166 -- function returns.
167 volumeGetActivationRoot :: VolumeClass volume => volume
168 -> IO (Maybe File) -- ^ returns the activation root of volume or 'Nothing'.
169 volumeGetActivationRoot volume =
170 maybeNull (wrapNewGObject mkFile) $
171 {#call g_volume_get_activation_root#} (toVolume volume)
172 #endif
173
174 -- | Mounts a volume. This is an asynchronous operation, and is finished by calling
175 -- 'volumeMountFinish' with the volume and 'AsyncResult' returned in the callback.
176 volumeMount :: VolumeClass volume => volume
177 -> [MountMountFlags] -- ^ @flags@ flags affecting the operation
178 -> Maybe MountOperation -- ^ @mountOperation@ a 'MountOperation' or 'Nothing' to avoid user interaction.
179 -> Maybe Cancellable -- ^ @cancellable@ optional 'Cancellable' object, 'Nothing' to ignore.
180 -> AsyncReadyCallback -- ^ @callback@ a 'AsyncReadyCallback'
181 -> IO ()
182 volumeMount volume flags mountOperation cancellable callback = do
183 cCallback <- marshalAsyncReadyCallback callback
184 {#call g_volume_mount #}
185 (toVolume volume)
186 ((fromIntegral . fromFlags) flags)
187 (fromMaybe (MountOperation nullForeignPtr) mountOperation)
188 (fromMaybe (Cancellable nullForeignPtr) cancellable)
189 cCallback
190 (castFunPtrToPtr cCallback)
191
192 -- | Finishes mounting a volume. If any errors occurred during the operation, error will be set to contain
193 -- the errors and 'False' will be returned.
194 --
195 -- If the mount operation succeeded, 'volumeGetMount' on volume is guaranteed to return the mount
196 -- right after calling this function; there's no need to listen for the 'mount-added' signal on
197 -- 'VolumeMonitor'.
198 --
199 -- Throws a 'GError' if an error occurs.
200 volumeMountFinish :: VolumeClass volume => volume
201 -> AsyncResult -- ^ @result@ a 'AsyncResult'
202 -> IO ()
203 volumeMountFinish volume result =
204 propagateGError (\gErrorPtr -> do
205 {#call g_volume_mount_finish#}
206 (toVolume volume)
207 result
208 gErrorPtr
209 return ())
210
211 -- | Checks if a volume can be ejected.
212 volumeCanEject :: VolumeClass volume => volume
213 -> IO Bool -- ^ returns 'True' if the volume can be ejected. 'False' otherwise.
214 volumeCanEject volume =
215 liftM toBool $
216 {#call g_volume_can_eject#} (toVolume volume)
217
218 #if GLIB_CHECK_VERSION(2,22,0)
219 -- | Ejects a volume. This is an asynchronous operation, and is finished by calling
220 -- 'volumeEjectWithOperationFinish' with the volume and 'AsyncResult' data returned in the
221 -- callback.
222 volumeEjectWithOperation :: VolumeClass volume => volume
223 -> [MountUnmountFlags] -- ^ @flags@ flags affecting the unmount if required for eject
224 -> Maybe MountOperation -- ^ @mountOperation@ a 'MountOperation' or 'Nothing' to avoid user interaction.
225 -> Maybe Cancellable -- ^ @cancellable@ optional 'Cancellable' object, 'Nothing' to ignore.
226 -> AsyncReadyCallback -- ^ @callback@ a 'AsyncReadyCallback'
227 -> IO ()
228 volumeEjectWithOperation volume flags mountOperation cancellable callback = do
229 cCallback <- marshalAsyncReadyCallback callback
230 {#call g_volume_eject_with_operation #}
231 (toVolume volume)
232 ((fromIntegral . fromFlags) flags)
233 (fromMaybe (MountOperation nullForeignPtr) mountOperation)
234 (fromMaybe (Cancellable nullForeignPtr) cancellable)
235 cCallback
236 (castFunPtrToPtr cCallback)
237
238 -- | Finishes ejecting a volume. If any errors occurred during the operation, error will be set to
239 -- contain the errors and 'False' will be returned.
240 --
241 -- Throws a 'GError' if an error occurs.
242 volumeEjectWithOperationFinish :: VolumeClass volume => volume
243 -> AsyncResult -- ^ @result@ a 'AsyncResult'.
244 -> IO ()
245 volumeEjectWithOperationFinish volume result =
246 propagateGError (\gErrorPtr -> do
247 {#call g_volume_eject_with_operation_finish #}
248 (toVolume volume)
249 result
250 gErrorPtr
251 return ())
252 #endif
253
254 -- | Gets the kinds of identifiers that volume has. Use 'volumeGetIdentifier' to obtain the
255 -- identifiers themselves.
256 volumeEnumerateIdentifiers :: (VolumeClass volume, GlibString string) => volume
257 -> IO [string]
258 volumeEnumerateIdentifiers volume =
259 {#call g_volume_enumerate_identifiers#} (toVolume volume)
260 >>= readUTFStringArray0
261
262 -- | Gets the identifier of the given kind for volume. See the introduction for more information about
263 -- volume identifiers.
264 volumeGetIdentifier :: (VolumeClass volume, GlibString string) => volume
265 -> string -- ^ @kind@ the kind of identifier to return
266 -> IO string
267 volumeGetIdentifier volume kind =
268 withUTFString kind $ \ kindPtr ->
269 {#call g_volume_get_identifier#} (toVolume volume) kindPtr
270 >>= readUTFString
271
272 --------------------
273 -- Signals
274 -- | Emitted when the volume has been changed.
275 volumeChanged :: VolumeClass volume => Signal volume (IO ())
276 volumeChanged = Signal (connect_NONE__NONE "changed")
277
278 -- | This signal is emitted when the 'Volume' have been removed. If the recipient is holding references to
279 -- the object they should release them so the object can be finalized.
280 volumeRemoved :: VolumeClass volume => Signal volume (IO ())
281 volumeRemoved = Signal (connect_NONE__NONE "removed")
282