~kelemeng/banshee/bug743928

« back to all changes in this revision

Viewing changes to src/Backends/Banshee.Hal/Banshee.HalBackend/Volume.cs

  • Committer: Bazaar Package Importer
  • Author(s): Chow Loong Jin
  • Date: 2011-05-14 22:25:36 UTC
  • mfrom: (6.3.15 experimental)
  • Revision ID: james.westby@ubuntu.com-20110514222536-u1x7ikxdqkmfvyuz
Tags: 2.1.0-1ubuntu1
* [2396c18] Merge from Debian Unstable, remaining changes:
  + Enable SoundMenu and Disable NotificationArea by default
  + Disable boo and karma extensions
  + Enable and recommnd u1ms and soundmenu extensions
  + Move desktop file for Meego UI to /usr/share/une/applications
  + Change the url for the Amazon store redirector
  + Create the U1MS widget earlier and bump libu1 requirement
* [9d7c600] Drop upstreamed u1ms-initialize-earlier patch

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
//
2
 
// Volume.cs
3
 
//
4
 
// Author:
5
 
//   Aaron Bockover <abockover@novell.com>
6
 
//
7
 
// Copyright (C) 2008 Novell, Inc.
8
 
//
9
 
// Permission is hereby granted, free of charge, to any person obtaining
10
 
// a copy of this software and associated documentation files (the
11
 
// "Software"), to deal in the Software without restriction, including
12
 
// without limitation the rights to use, copy, modify, merge, publish,
13
 
// distribute, sublicense, and/or sell copies of the Software, and to
14
 
// permit persons to whom the Software is furnished to do so, subject to
15
 
// the following conditions:
16
 
//
17
 
// The above copyright notice and this permission notice shall be
18
 
// included in all copies or substantial portions of the Software.
19
 
//
20
 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21
 
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22
 
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23
 
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24
 
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25
 
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26
 
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27
 
//
28
 
 
29
 
using System;
30
 
using System.Collections.Generic;
31
 
 
32
 
using Banshee.Hardware;
33
 
 
34
 
namespace Banshee.HalBackend
35
 
{
36
 
    public class Volume : Device, IVolume
37
 
    {
38
 
        private static Dictionary<Hal.Device, Volume> mounted_volumes = new Dictionary<Hal.Device, Volume> ();
39
 
        private static Dictionary<Hal.Device, Volume> unmounted_volumes = new Dictionary<Hal.Device, Volume> ();
40
 
 
41
 
        internal static HardwareManager HardwareManager;
42
 
 
43
 
        private const string method_names_property = "org.freedesktop.Hal.Device.Volume.method_names";
44
 
 
45
 
        public static Volume Resolve (BlockDevice parent, Hal.Manager manager, Hal.Device device)
46
 
        {
47
 
            if (!device.IsVolume) {
48
 
                return null;
49
 
            }
50
 
 
51
 
            try {
52
 
                Volume volume = (parent is ICdromDevice || (parent == null && device.QueryCapability ("volume.disc")))
53
 
                    ? DiscVolume.Resolve (parent, manager, device)
54
 
                    : new Volume (parent, manager, device);
55
 
 
56
 
                return CheckVolumeMounted (volume) ? volume : null;
57
 
            } catch (Exception e) {
58
 
                Hyena.Log.Exception (e);
59
 
            }
60
 
 
61
 
            return null;
62
 
        }
63
 
 
64
 
        private DkDisk dk_disk;
65
 
        private BlockDevice parent;
66
 
        private string [] method_names;
67
 
 
68
 
        protected Volume (BlockDevice parent, Hal.Manager manager, Hal.Device device) : base (manager, device)
69
 
        {
70
 
            this.parent = parent ?? BlockDevice.Resolve<IBlockDevice> (manager, device.Parent);
71
 
            dk_disk = DkDisk.FindByDevice (DeviceNode);
72
 
 
73
 
            method_names = HalDevice.PropertyExists (method_names_property)
74
 
                ? device.GetPropertyStringList (method_names_property)
75
 
                : new string[0];
76
 
        }
77
 
 
78
 
        public string DeviceNode {
79
 
            get { return HalDevice["block.device"]; }
80
 
        }
81
 
 
82
 
        public string MountPoint {
83
 
            get {
84
 
                if (dk_disk != null && dk_disk.MountPoint != null) {
85
 
                    return dk_disk.MountPoint;
86
 
                } else {
87
 
                    return HalDevice["volume.mount_point"];
88
 
                }
89
 
            }
90
 
        }
91
 
 
92
 
        public string FileSystem {
93
 
            get { return HalDevice["volume.fstype"]; }
94
 
        }
95
 
 
96
 
        /*private string serial;
97
 
        public override string Serial {
98
 
            get {
99
 
                if (serial == null) {
100
 
                    serial = String.IsNullOrEmpty (HalDevice["volume.uuid"])
101
 
                        ? base.Serial
102
 
                        : HalDevice["volume.uuid"];
103
 
                }
104
 
 
105
 
                return serial;
106
 
            }
107
 
        }*/
108
 
 
109
 
        public bool IsMounted {
110
 
            get { return (dk_disk != null && dk_disk.IsMounted) || HalDevice.GetPropertyBoolean ("volume.is_mounted"); }
111
 
        }
112
 
 
113
 
        public bool ShouldIgnore {
114
 
            get { return HalDevice.GetPropertyBoolean ("volume.ignore"); }
115
 
        }
116
 
 
117
 
        public bool IsReadOnly {
118
 
            get { return (dk_disk != null && dk_disk.IsReadOnly) || HalDevice.GetPropertyBoolean ("volume.is_mounted_read_only"); }
119
 
        }
120
 
 
121
 
        public ulong Capacity {
122
 
            get { return HalDevice.GetPropertyUInt64 ("volume.size"); }
123
 
        }
124
 
 
125
 
        public long Available {
126
 
            get {
127
 
                if (!IsMounted) {
128
 
                    return -1;
129
 
                }
130
 
 
131
 
                // FIXME: maybe we should use UnixDriveInfo? Advantages?
132
 
 
133
 
                try {
134
 
                    Mono.Unix.Native.Statvfs statvfs_info;
135
 
                    if (Mono.Unix.Native.Syscall.statvfs (MountPoint, out statvfs_info) != -1) {
136
 
                        return ((long)statvfs_info.f_bavail) * ((long)statvfs_info.f_bsize);
137
 
                    }
138
 
                } catch {
139
 
                }
140
 
 
141
 
                return -1;
142
 
            }
143
 
        }
144
 
 
145
 
        public IBlockDevice Parent {
146
 
            get { return parent; }
147
 
        }
148
 
 
149
 
        public bool CanEject {
150
 
            get { return Array.IndexOf<string> (method_names, "Eject") >= 0; }
151
 
        }
152
 
 
153
 
        public void Eject ()
154
 
        {
155
 
            if (CanEject && HalDevice.IsVolume) {
156
 
                HalDevice.Volume.Eject ();
157
 
            }
158
 
        }
159
 
 
160
 
        public bool CanMount {
161
 
            get { return Array.IndexOf <string>(method_names, "Mount") >= 0; }
162
 
        }
163
 
 
164
 
        public bool CanUnmount {
165
 
            get { return Array.IndexOf<string> (method_names, "Unmount") >= 0; }
166
 
        }
167
 
 
168
 
        public void Mount ()
169
 
        {
170
 
            if (CanMount && HalDevice.IsVolume)
171
 
                HalDevice.Volume.Mount ();
172
 
        }
173
 
 
174
 
        public void Unmount ()
175
 
        {
176
 
            if (CanUnmount && HalDevice.IsVolume) {
177
 
                if (dk_disk != null) {
178
 
                    dk_disk.Unmount ();
179
 
                } else {
180
 
                    HalDevice.Volume.Unmount ();
181
 
                }
182
 
            }
183
 
        }
184
 
 
185
 
        public override string ToString ()
186
 
        {
187
 
            if (IsMounted) {
188
 
                return String.Format ("`{0}': mounted {1} volume at {2} with {3} bytes free (of {4})",
189
 
                    Name, IsReadOnly ? "read only" : "read/write", MountPoint, Available, Capacity);
190
 
            }
191
 
 
192
 
            return String.Format ("`{0}': not mounted (capacity: {1} bytes)", Name, Capacity);
193
 
        }
194
 
 
195
 
        private static bool CheckVolumeMounted (Volume volume)
196
 
        {
197
 
            if (volume != null && !(volume is IDiscVolume)) {
198
 
                lock (mounted_volumes) {
199
 
                    if (mounted_volumes.ContainsKey (volume.HalDevice)) {
200
 
                        return true;
201
 
                    } else if (unmounted_volumes.ContainsKey (volume.HalDevice)) {
202
 
                        return false;
203
 
                    }
204
 
 
205
 
                    volume.HalDevice.PropertyModified += HandleVolumeChanged;
206
 
 
207
 
                    if (!volume.IsMounted) {
208
 
                        unmounted_volumes[volume.HalDevice] = volume;
209
 
                        return false;
210
 
                    } else {
211
 
                        mounted_volumes[volume.HalDevice] = volume;
212
 
                    }
213
 
                }
214
 
            }
215
 
            return true;
216
 
        }
217
 
 
218
 
        private static void HandleVolumeChanged (object o, Hal.PropertyModifiedArgs args)
219
 
        {
220
 
            Hal.Device device = o as Hal.Device;
221
 
            if (device == null) {
222
 
                return;
223
 
            }
224
 
 
225
 
            lock (mounted_volumes) {
226
 
                if (mounted_volumes.ContainsKey (device)) {
227
 
                    Volume volume = mounted_volumes[device];
228
 
                    bool mounted = false;
229
 
                    try {
230
 
                        mounted = volume.IsMounted;
231
 
                    } catch (Exception) {}
232
 
 
233
 
                    if (!mounted) {
234
 
                        mounted_volumes.Remove (device);
235
 
                        unmounted_volumes[device] = volume;
236
 
                        HardwareManager.OnDeviceRemoved (volume.Uuid);
237
 
                    }
238
 
                } else if (unmounted_volumes.ContainsKey (device)) {
239
 
                    Volume volume = unmounted_volumes[device];
240
 
                    if (volume.IsMounted) {
241
 
                        unmounted_volumes.Remove (device);
242
 
                        mounted_volumes[device] = volume;
243
 
                        HardwareManager.OnHalDeviceAdded (volume);
244
 
                    }
245
 
                }
246
 
            }
247
 
        }
248
 
    }
249
 
}