| 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.Mount ( |
| 31 |
-- * Details |
| 32 |
-- |
| 33 |
-- | The 'Mount' interface represents user-visible mounts. Note, when porting from GnomeVFS, 'Mount' is the |
| 34 |
-- moral equivalent of GnomeVFSVolume. |
| 35 |
-- |
| 36 |
-- 'Mount' is a "mounted" filesystem that you can access. Mounted is in quotes because it's not the same |
| 37 |
-- as a unix mount, it might be a gvfs mount, but you can still access the files on it if you use |
| 38 |
-- GIO. Might or might not be related to a volume object. |
| 39 |
-- |
| 40 |
-- Unmounting a 'Mount' instance is an asynchronous operation. For more information about asynchronous |
| 41 |
-- operations, see 'AsyncReady' and GSimpleAsyncReady. To unmount a 'Mount' instance, first call |
| 42 |
-- 'mountUnmountWithOperation' the 'Mount' instance and a 'AsyncReadyCallback'. The |
| 43 |
-- 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 |
-- 'mountUnmountWithOperationFinish' with the 'Mount' and the 'AsyncReady' data to see if the |
| 46 |
-- operation was completed successfully. If an error is present when |
| 47 |
-- 'mountUnmountWithOperationFinish' is called, then it will be filled with any error |
| 48 |
-- information. |
| 49 |
|
| 50 |
-- * Types |
| 51 |
Mount(..), |
| 52 |
MountClass, |
| 53 |
|
| 54 |
-- * Methods |
| 55 |
mountGetName, |
| 56 |
mountGetUUID, |
| 57 |
mountGetIcon, |
| 58 |
mountGetDrive, |
| 59 |
mountGetRoot, |
| 60 |
mountGetVolume, |
| 61 |
#if GLIB_CHECK_VERSION(2,24,0) |
| 62 |
mountGetDefaultLocation, |
| 63 |
#endif |
| 64 |
mountCanUnmount, |
| 65 |
#if GLIB_CHECK_VERSION(2,22,0) |
| 66 |
mountUnmountWithOperation, |
| 67 |
mountUnmountWithOperationFinish, |
| 68 |
#endif |
| 69 |
mountRemount, |
| 70 |
mountRemountFinish, |
| 71 |
mountCanEject, |
| 72 |
#if GLIB_CHECK_VERSION(2,22,0) |
| 73 |
mountEjectWithOperation, |
| 74 |
mountEjectWithOperationFinish, |
| 75 |
#endif |
| 76 |
#if GLIB_CHECK_VERSION(2,18,0) |
| 77 |
mountGuessContentType, |
| 78 |
mountGuessContentTypeFinish, |
| 79 |
mountGuessContentTypeSync, |
| 80 |
#endif |
| 81 |
#if GLIB_CHECK_VERSION(2,20,0) |
| 82 |
mountIsShadowed, |
| 83 |
mountShadow, |
| 84 |
mountUnshadow, |
| 85 |
#endif |
| 86 |
|
| 87 |
-- * Signals |
| 88 |
mountChanged, |
| 89 |
#if GLIB_CHECK_VERSION(2,22,0) |
| 90 |
mountPreUnmount, |
| 91 |
#endif |
| 92 |
mountUnmounted, |
| 93 |
) where |
| 94 |
|
| 95 |
import Control.Monad |
| 96 |
import Data.Maybe (fromMaybe) |
| 97 |
import System.GIO.Enums |
| 98 |
import System.Glib.Attributes |
| 99 |
import System.Glib.FFI |
| 100 |
import System.Glib.Flags |
| 101 |
import System.Glib.GError |
| 102 |
import System.Glib.GList |
| 103 |
import System.Glib.GObject |
| 104 |
import System.Glib.Properties |
| 105 |
import System.Glib.Signals |
| 106 |
import System.Glib.UTFString |
| 107 |
{#import System.GIO.Async.AsyncResult#} |
| 108 |
{#import System.GIO.Signals#} |
| 109 |
{#import System.GIO.Types#} |
| 110 |
|
| 111 |
{# context lib = "gio" prefix = "g" #} |
| 112 |
|
| 113 |
-------------------- |
| 114 |
-- Methods |
| 115 |
-- | Gets the name of mount. |
| 116 |
mountGetName :: (MountClass mount, GlibString string) => mount |
| 117 |
-> IO string -- ^ returns the name for the given mount. |
| 118 |
mountGetName mount = |
| 119 |
{#call g_mount_get_name#} (toMount mount) |
| 120 |
>>= readUTFString |
| 121 |
|
| 122 |
-- | Gets the UUID for the mount. The reference is typically based on the file system UUID for the mount |
| 123 |
-- in question and should be considered an opaque string. Returns 'Nothing' if there is no UUID available. |
| 124 |
mountGetUUID :: (MountClass mount, GlibString string) => mount |
| 125 |
-> IO (Maybe string) -- ^ returns the UUID for mount or 'Nothing' if no UUID can be computed. |
| 126 |
mountGetUUID mount = do |
| 127 |
{#call g_mount_get_uuid#} (toMount mount) |
| 128 |
>>= maybePeek readUTFString |
| 129 |
|
| 130 |
-- | Gets the icon for mount. |
| 131 |
mountGetIcon :: MountClass mount => mount |
| 132 |
-> IO Icon -- ^ returns a 'Icon'. |
| 133 |
mountGetIcon mount = |
| 134 |
wrapNewGObject mkIcon $ |
| 135 |
{#call g_mount_get_icon#} (toMount mount) |
| 136 |
|
| 137 |
-- | Gets the drive for the mount. |
| 138 |
-- |
| 139 |
-- This is a convenience method for getting the 'Volume' and then using that object to get the 'Drive'. |
| 140 |
mountGetDrive :: MountClass mount => mount |
| 141 |
-> IO (Maybe Drive) -- ^ returns the 'Drive' for mount or 'Nothing' if no 'Drive' can be computed. |
| 142 |
mountGetDrive mount = |
| 143 |
maybeNull (wrapNewGObject mkDrive) $ |
| 144 |
{#call g_mount_get_drive#} (toMount mount) |
| 145 |
|
| 146 |
-- | Gets the root directory on mount. |
| 147 |
mountGetRoot :: MountClass mount => mount |
| 148 |
-> IO File |
| 149 |
mountGetRoot mount = |
| 150 |
wrapNewGObject mkFile $ |
| 151 |
{#call g_mount_get_root#} (toMount mount) |
| 152 |
|
| 153 |
-- | Gets the volume directory on mount. |
| 154 |
mountGetVolume :: MountClass mount => mount |
| 155 |
-> IO (Maybe Volume) -- ^ returns a 'Volume' or 'Nothing' if mount is not associated with a volume. |
| 156 |
mountGetVolume mount = |
| 157 |
maybeNull (wrapNewGObject mkVolume) $ |
| 158 |
{#call g_mount_get_volume#} (toMount mount) |
| 159 |
|
| 160 |
#if GLIB_CHECK_VERSION(2,24,0) |
| 161 |
-- | Gets the default location of mount. The default location of the given mount is a path that reflects |
| 162 |
-- the main entry point for the user (e.g. the home directory, or the root of the volume). |
| 163 |
-- | Gets the root directory on mount. |
| 164 |
mountGetDefaultLocation :: MountClass mount => mount |
| 165 |
-> IO File |
| 166 |
mountGetDefaultLocation mount = |
| 167 |
wrapNewGObject mkFile $ |
| 168 |
{#call g_mount_get_default_location#} (toMount mount) |
| 169 |
#endif |
| 170 |
|
| 171 |
-- | Checks if mount can be mounted. |
| 172 |
mountCanUnmount :: MountClass mount => mount |
| 173 |
-> IO Bool -- ^ returns 'True' if the mount can be unmounted. |
| 174 |
mountCanUnmount mount = |
| 175 |
liftM toBool $ |
| 176 |
{#call g_mount_can_unmount#} (toMount mount) |
| 177 |
|
| 178 |
#if GLIB_CHECK_VERSION(2,22,0) |
| 179 |
-- | Unmounts a mount. This is an asynchronous operation, and is finished by calling |
| 180 |
-- 'mountUnmountWithOperationFinish' with the mount and 'AsyncResult' data returned in the |
| 181 |
-- callback. |
| 182 |
mountUnmountWithOperation :: MountClass mount |
| 183 |
=> mount |
| 184 |
-> [MountUnmountFlags] -- ^ @flags@ flags affecting the unmount if required for eject |
| 185 |
-> Maybe MountOperation -- ^ @mountOperation@ a 'MountOperation' or 'Nothing' to avoid user interaction. |
| 186 |
-> Maybe Cancellable -- ^ @cancellable@ optional 'Cancellable' object, 'Nothing' to ignore. |
| 187 |
-> AsyncReadyCallback -- ^ @callback@ a 'AsyncReadyCallback' |
| 188 |
-> IO () |
| 189 |
mountUnmountWithOperation mount flags mountOperation cancellable callback = do |
| 190 |
cCallback <- marshalAsyncReadyCallback callback |
| 191 |
{#call g_mount_unmount_with_operation #} |
| 192 |
(toMount mount) |
| 193 |
((fromIntegral . fromFlags) flags) |
| 194 |
(fromMaybe (MountOperation nullForeignPtr) mountOperation) |
| 195 |
(fromMaybe (Cancellable nullForeignPtr) cancellable) |
| 196 |
cCallback |
| 197 |
(castFunPtrToPtr cCallback) |
| 198 |
|
| 199 |
-- | Finishes unmounting a mount. If any errors occurred during the operation, error will be set to |
| 200 |
-- contain the errors and 'False' will be returned. |
| 201 |
-- |
| 202 |
-- Throws a 'GError' if an error occurs. |
| 203 |
mountUnmountWithOperationFinish :: MountClass mount |
| 204 |
=> mount |
| 205 |
-> AsyncResult -- ^ @result@ a 'AsyncResult'. |
| 206 |
-> IO () |
| 207 |
mountUnmountWithOperationFinish mount result = |
| 208 |
propagateGError (\gErrorPtr -> do |
| 209 |
{#call g_mount_unmount_with_operation_finish #} |
| 210 |
(toMount mount) |
| 211 |
result |
| 212 |
gErrorPtr |
| 213 |
return ()) |
| 214 |
#endif |
| 215 |
|
| 216 |
-- | Remounts a mount. This is an asynchronous operation, and is finished by calling |
| 217 |
-- 'mountRemountFinish' with the mount and 'AsyncResult's data returned in the callback. |
| 218 |
-- |
| 219 |
-- Remounting is useful when some setting affecting the operation of the volume has been changed, as |
| 220 |
-- these may need a remount to take affect. While this is semantically equivalent with unmounting and |
| 221 |
-- then remounting not all backends might need to actually be unmounted. |
| 222 |
mountRemount :: MountClass mount |
| 223 |
=> mount |
| 224 |
-> [MountMountFlags] -- ^ @flags@ flags affecting the unmount if required for eject |
| 225 |
-> Maybe MountOperation -- ^ @mountOperation@ a 'MountOperation' or 'Nothing' to avoid user interaction. |
| 226 |
-> Maybe Cancellable -- ^ @cancellable@ optional 'Cancellable' object, 'Nothing' to ignore. |
| 227 |
-> AsyncReadyCallback -- ^ @callback@ a 'AsyncReadyCallback' |
| 228 |
-> IO () |
| 229 |
mountRemount mount flags mountOperation cancellable callback = do |
| 230 |
cCallback <- marshalAsyncReadyCallback callback |
| 231 |
{#call g_mount_remount #} |
| 232 |
(toMount mount) |
| 233 |
((fromIntegral . fromFlags) flags) |
| 234 |
(fromMaybe (MountOperation nullForeignPtr) mountOperation) |
| 235 |
(fromMaybe (Cancellable nullForeignPtr) cancellable) |
| 236 |
cCallback |
| 237 |
(castFunPtrToPtr cCallback) |
| 238 |
|
| 239 |
-- | Finishes remounting a mount. If any errors occurred during the operation, error will be set to |
| 240 |
-- contain the errors and 'False' will be returned. |
| 241 |
-- |
| 242 |
-- Throws a 'GError' if an error occurs. |
| 243 |
mountRemountFinish :: MountClass mount |
| 244 |
=> mount |
| 245 |
-> AsyncResult -- ^ @result@ a 'AsyncResult'. |
| 246 |
-> IO () |
| 247 |
mountRemountFinish mount result = |
| 248 |
propagateGError (\gErrorPtr -> do |
| 249 |
{#call g_mount_remount_finish #} |
| 250 |
(toMount mount) |
| 251 |
result |
| 252 |
gErrorPtr |
| 253 |
return ()) |
| 254 |
|
| 255 |
-- | Checks if mount can be eject. |
| 256 |
mountCanEject :: MountClass mount => mount |
| 257 |
-> IO Bool -- ^ returns 'True' if the mount can be ejected. |
| 258 |
mountCanEject mount = |
| 259 |
liftM toBool $ |
| 260 |
{#call g_mount_can_eject#} (toMount mount) |
| 261 |
|
| 262 |
#if GLIB_CHECK_VERSION(2,22,0) |
| 263 |
-- | Ejects a mount. This is an asynchronous operation, and is finished by calling |
| 264 |
-- 'mountEjectWithOperationFinish' with the mount and 'AsyncResult' data returned in the callback. |
| 265 |
mountEjectWithOperation :: MountClass mount |
| 266 |
=> mount |
| 267 |
-> [MountUnmountFlags] -- ^ @flags@ flags affecting the unmount if required for eject |
| 268 |
-> Maybe MountOperation -- ^ @mountOperation@ a 'MountOperation' or 'Nothing' to avoid user interaction. |
| 269 |
-> Maybe Cancellable -- ^ @cancellable@ optional 'Cancellable' object, 'Nothing' to ignore. |
| 270 |
-> AsyncReadyCallback -- ^ @callback@ a 'AsyncReadyCallback' |
| 271 |
-> IO () |
| 272 |
mountEjectWithOperation mount flags mountOperation cancellable callback = do |
| 273 |
cCallback <- marshalAsyncReadyCallback callback |
| 274 |
{#call g_mount_eject_with_operation #} |
| 275 |
(toMount mount) |
| 276 |
((fromIntegral . fromFlags) flags) |
| 277 |
(fromMaybe (MountOperation nullForeignPtr) mountOperation) |
| 278 |
(fromMaybe (Cancellable nullForeignPtr) cancellable) |
| 279 |
cCallback |
| 280 |
(castFunPtrToPtr cCallback) |
| 281 |
|
| 282 |
-- | Finishes ejecting a mount. If any errors occurred during the operation. |
| 283 |
-- |
| 284 |
-- Throws a 'GError' if an error occurs. |
| 285 |
mountEjectWithOperationFinish :: MountClass mount |
| 286 |
=> mount |
| 287 |
-> AsyncResult -- ^ @result@ a 'AsyncResult'. |
| 288 |
-> IO () |
| 289 |
mountEjectWithOperationFinish mount result = |
| 290 |
propagateGError (\gErrorPtr -> do |
| 291 |
{#call g_mount_eject_with_operation_finish #} |
| 292 |
(toMount mount) |
| 293 |
result |
| 294 |
gErrorPtr |
| 295 |
return ()) |
| 296 |
#endif |
| 297 |
|
| 298 |
#if GLIB_CHECK_VERSION(2,18,0) |
| 299 |
-- | Tries to guess the type of content stored on mount. Returns one or more textual identifiers of |
| 300 |
-- well-known content types (typically prefixed with \"x-content/\"), e.g. x-content/image-dcf for camera |
| 301 |
-- memory cards. See the shared-mime-info specification for more on x-content types. |
| 302 |
-- |
| 303 |
-- This is an asynchronous operation (see 'mountGuessContentTypeSync' for the synchronous |
| 304 |
-- version), and is finished by calling 'mountGuessContentTypeFinish' with the mount and |
| 305 |
-- 'AsyncResult' data returned in the callback. |
| 306 |
mountGuessContentType :: MountClass mount => mount |
| 307 |
-> Bool -- ^ @forceRescan@ Whether to force a rescan of the content. Otherwise a cached result will be used if available |
| 308 |
-> Maybe Cancellable -- ^ @cancellable@ optional 'Cancellable' object, 'Nothing' to ignore |
| 309 |
-> AsyncReadyCallback -- ^ @callback@ a 'AsyncReadyCallback' |
| 310 |
-> IO () |
| 311 |
mountGuessContentType mount forceRescan cancellable callback = do |
| 312 |
cCallback <- marshalAsyncReadyCallback callback |
| 313 |
{#call g_mount_guess_content_type #} |
| 314 |
(toMount mount) |
| 315 |
(fromBool forceRescan) |
| 316 |
(fromMaybe (Cancellable nullForeignPtr) cancellable) |
| 317 |
cCallback |
| 318 |
(castFunPtrToPtr cCallback) |
| 319 |
|
| 320 |
-- | Finishes guessing content types of mount. If any errors occurred during the operation, error will be |
| 321 |
-- set to contain the errors and 'False' will be returned. In particular, you may get an |
| 322 |
-- 'IoErrorNotSupported' if the mount does not support content guessing. |
| 323 |
mountGuessContentTypeFinish :: (MountClass mount, GlibString string) => mount |
| 324 |
-> AsyncResult -- ^ @result@ a 'AsyncResult'. |
| 325 |
-> IO [string] -- ^ returns 'True' if the mount was successfully ejected. 'False' otherwise. |
| 326 |
mountGuessContentTypeFinish mount result = |
| 327 |
propagateGError ({#call g_mount_guess_content_type_finish #} (toMount mount) result) |
| 328 |
>>= readUTFStringArray0 |
| 329 |
|
| 330 |
-- | Tries to guess the type of content stored on mount. Returns one or more textual identifiers of |
| 331 |
-- well-known content types (typically prefixed with \"x-content/\"), e.g. x-content/image-dcf for camera |
| 332 |
-- memory cards. See the shared-mime-info specification for more on x-content types. |
| 333 |
-- |
| 334 |
-- This is an synchronous operation and as such may block doing IO; see 'mountGuessContentType' |
| 335 |
-- for the asynchronous version. |
| 336 |
mountGuessContentTypeSync :: (MountClass mount, GlibString string) => mount |
| 337 |
-> Bool -- ^ @forceRescan@ Whether to force a rescan of the content. Otherwise a cached result will be used if available |
| 338 |
-> Maybe Cancellable -- ^ @cancellable@ optional 'Cancellable' object, 'Nothing' to ignore |
| 339 |
-> IO [string] |
| 340 |
mountGuessContentTypeSync mount forceRescan cancellable = |
| 341 |
propagateGError ({#call g_mount_guess_content_type_sync #} |
| 342 |
(toMount mount) |
| 343 |
(fromBool forceRescan) |
| 344 |
(fromMaybe (Cancellable nullForeignPtr) cancellable) |
| 345 |
) |
| 346 |
>>= readUTFStringArray0 |
| 347 |
#endif |
| 348 |
|
| 349 |
#if GLIB_CHECK_VERSION(2,20,0) |
| 350 |
-- | Determines if mount is shadowed. Applications or libraries should avoid displaying mount in the user |
| 351 |
-- interface if it is shadowed. |
| 352 |
-- |
| 353 |
-- A mount is said to be shadowed if there exists one or more user visible objects (currently 'Mount' |
| 354 |
-- objects) with a root that is inside the root of mount. |
| 355 |
-- |
| 356 |
-- One application of shadow mounts is when exposing a single file system that is used to address |
| 357 |
-- several logical volumes. In this situation, a 'VolumeMonitor' implementation would create two 'Volume' |
| 358 |
-- objects (for example, one for the camera functionality of the device and one for a SD card reader on |
| 359 |
-- the device) with activation URIs gphoto2://[usb:001,002]/store1/ and |
| 360 |
-- gphoto2://[usb:001,002]/store2/. When the underlying mount (with root gphoto2://[usb:001,002]/) is |
| 361 |
-- mounted, said 'VolumeMonitor' implementation would create two 'Mount' objects (each with their root |
| 362 |
-- matching the corresponding volume activation root) that would shadow the original mount. |
| 363 |
-- |
| 364 |
-- The proxy monitor in GVfs 2.26 and later, automatically creates and manage shadow mounts (and |
| 365 |
-- shadows the underlying mount) if the activation root on a 'Volume' is set. |
| 366 |
mountIsShadowed :: MountClass mount => mount |
| 367 |
-> IO Bool -- ^ returns 'True' if mount is shadowed. |
| 368 |
mountIsShadowed mount = |
| 369 |
liftM toBool $ |
| 370 |
{#call g_mount_is_shadowed#} (toMount mount) |
| 371 |
|
| 372 |
-- | Increments the shadow count on mount. Usually used by 'VolumeMonitor' implementations when creating a |
| 373 |
-- shadow mount for mount, see 'mountIsShadowed' for more information. The caller will need to emit |
| 374 |
-- the "changed" signal on mount manually. |
| 375 |
mountShadow :: MountClass mount => mount -> IO () |
| 376 |
mountShadow mount = |
| 377 |
{#call g_mount_shadow#} (toMount mount) |
| 378 |
|
| 379 |
-- | Decrements the shadow count on mount. Usually used by 'VolumeMonitor' implementations when destroying |
| 380 |
-- a shadow mount for mount, see 'mountIsShadowed' for more information. The caller will need to |
| 381 |
-- emit the "changed" signal on mount manually. |
| 382 |
mountUnshadow :: MountClass mount => mount -> IO () |
| 383 |
mountUnshadow mount = |
| 384 |
{#call g_mount_unshadow#} (toMount mount) |
| 385 |
#endif |
| 386 |
|
| 387 |
-------------------- |
| 388 |
-- Signals |
| 389 |
-- | Emitted when the mount has been changed. |
| 390 |
mountChanged :: MountClass mount => Signal mount (IO ()) |
| 391 |
mountChanged = Signal (connect_NONE__NONE "changed") |
| 392 |
|
| 393 |
#if GLIB_CHECK_VERSION(2,22,0) |
| 394 |
-- | This signal is emitted when the 'Mount' is about to be unmounted. |
| 395 |
mountPreUnmount :: MountClass mount => Signal mount (IO ()) |
| 396 |
mountPreUnmount = Signal (connect_NONE__NONE "pre-unmount") |
| 397 |
#endif |
| 398 |
|
| 399 |
-- | This signal is emitted when the 'Mount' have been unmounted. If the recipient is holding references |
| 400 |
-- to the object they should release them so the object can be finalized. |
| 401 |
mountUnmounted :: MountClass mount => Signal mount (IO ()) |
| 402 |
mountUnmounted = Signal (connect_NONE__NONE "unmounted") |