gwin32mount.c 9.3 KB
Newer Older
1 2 3 4 5 6 7 8
/* GIO - GLib Input, Output and Streaming Library
 * 
 * Copyright (C) 2006-2007 Red Hat, Inc.
 * Copyright (C) 2008 Hans Breuer
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
9
 * version 2.1 of the License, or (at your option) any later version.
10 11 12 13 14 15 16
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General
17
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 19 20 21 22 23
 *
 * Author: Alexander Larsson <alexl@redhat.com>
 *         David Zeuthen <davidz@redhat.com>
 *         Hans Breuer <hans@breuer.org>
 */

24
#include "config.h"
25 26 27 28 29 30 31 32

#include <string.h>
#define WIN32_MEAN_AND_LEAN
#include <windows.h>

#include <glib.h>
#include "gwin32volumemonitor.h"
#include "gwin32mount.h"
33 34
#include "gmount.h"
#include "gfile.h"
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
#include "gmountprivate.h"
#include "gvolumemonitor.h"
#include "gthemedicon.h"
#include "glibintl.h"


struct _GWin32Mount {
  GObject parent;

  GVolumeMonitor   *volume_monitor;

  GWin32Volume      *volume; /* owned by volume monitor */
  int   drive_type;

  /* why does all this stuff need to be duplicated? It is in volume already! */
  char *name;
  GIcon *icon;
52
  GIcon *symbolic_icon;
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
  char *mount_path;

  gboolean can_eject;
};

static void g_win32_mount_mount_iface_init (GMountIface *iface);

#define g_win32_mount_get_type _g_win32_mount_get_type
G_DEFINE_TYPE_WITH_CODE (GWin32Mount, g_win32_mount, G_TYPE_OBJECT,
			 G_IMPLEMENT_INTERFACE (G_TYPE_MOUNT,
						g_win32_mount_mount_iface_init))


static void
g_win32_mount_finalize (GObject *object)
{
  GWin32Mount *mount;
  
  mount = G_WIN32_MOUNT (object);

  if (mount->volume_monitor != NULL)
    g_object_unref (mount->volume_monitor);
#if 0
  if (mount->volume)
    _g_win32_volume_unset_mount (mount->volume, mount);
#endif
  /* TODO: g_warn_if_fail (volume->volume == NULL); */
80 81 82

  if (mount->icon != NULL)
    g_object_unref (mount->icon);
83 84
  if (mount->symbolic_icon != NULL)
    g_object_unref (mount->symbolic_icon);
85

86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
  g_free (mount->name);
  g_free (mount->mount_path);
  
  if (G_OBJECT_CLASS (g_win32_mount_parent_class)->finalize)
    (*G_OBJECT_CLASS (g_win32_mount_parent_class)->finalize) (object);
}

static void
g_win32_mount_class_init (GWin32MountClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);

  gobject_class->finalize = g_win32_mount_finalize;
}

static void
g_win32_mount_init (GWin32Mount *win32_mount)
{
}

Dan Winship's avatar
Dan Winship committed
106
static gchar *
107 108 109 110 111 112 113 114 115 116 117 118
_win32_get_displayname (const char *drive)
{
  gunichar2 *wdrive = g_utf8_to_utf16 (drive, -1, NULL, NULL, NULL);
  gchar *name = NULL;
  SHFILEINFOW sfi;
  if (SHGetFileInfoW(wdrive, 0, &sfi, sizeof(sfi), SHGFI_DISPLAYNAME))
    name = g_utf16_to_utf8 (sfi.szDisplayName, -1, NULL, NULL, NULL);

  g_free (wdrive);
  return name ? name : g_strdup (drive);
}

119
/*
120 121 122
 * _g_win32_mount_new:
 * @volume_monitor: a #GVolumeMonitor.
 * @path: a win32 path.
Matthias Clasen's avatar
Matthias Clasen committed
123
 * @volume: usually NULL
124 125 126 127 128
 * 
 * Returns: a #GWin32Mount for the given win32 path.
 **/
GWin32Mount *
_g_win32_mount_new (GVolumeMonitor  *volume_monitor,
129 130
                    const char      *path,
                    GWin32Volume    *volume)
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
{
  GWin32Mount *mount;
  const gchar *drive = path; //fixme

#if 0  
  /* No volume for mount: Ignore internal things */
  if (volume == NULL && !g_win32_mount_guess_should_display (mount_entry))
    return NULL;
#endif

  mount = g_object_new (G_TYPE_WIN32_MOUNT, NULL);
  mount->volume_monitor = volume_monitor != NULL ? g_object_ref (volume_monitor) : NULL;
  mount->mount_path = g_strdup (path);
  mount->drive_type = GetDriveType (drive);
  mount->can_eject = FALSE; /* TODO */
  mount->name = _win32_get_displayname (drive);

  /* need to do this last */
  mount->volume = volume;
#if 0
  if (volume != NULL)
    _g_win32_volume_set_mount (volume, mount);
#endif
  return mount;
}

void
_g_win32_mount_unmounted (GWin32Mount *mount)
{
  if (mount->volume != NULL)
    {
#if 0
      _g_win32_volume_unset_mount (mount->volume, mount);
#endif
      mount->volume = NULL;
      g_signal_emit_by_name (mount, "changed");
      /* there's really no need to emit mount_changed on the volume monitor 
       * as we're going to be deleted.. */
    }
}

void
173 174
_g_win32_mount_unset_volume (GWin32Mount  *mount,
			     GWin32Volume *volume)
175 176 177 178 179 180 181
{
  if (mount->volume == volume)
    {
      mount->volume = NULL;
      /* TODO: Emit changed in idle to avoid locking issues */
      g_signal_emit_by_name (mount, "changed");
      if (mount->volume_monitor != NULL)
182
        g_signal_emit_by_name (mount->volume_monitor, "mount-changed", mount);
183 184 185 186 187 188 189 190 191 192 193
    }
}

static GFile *
g_win32_mount_get_root (GMount *mount)
{
  GWin32Mount *win32_mount = G_WIN32_MOUNT (mount);

  return g_file_new_for_path (win32_mount->mount_path);
}

Dan Winship's avatar
Dan Winship committed
194
static const char *
195
_win32_drive_type_to_icon (int type, gboolean use_symbolic)
196 197 198
{
  switch (type)
  {
199 200 201 202 203
  case DRIVE_REMOVABLE : return use_symbolic ? "drive-removable-media-symbolic" : "drive-removable-media";
  case DRIVE_FIXED : return use_symbolic ? "drive-harddisk-symbolic" : "drive-harddisk";
  case DRIVE_REMOTE : return use_symbolic ? "folder-remote-symbolic" : "folder-remote";
  case DRIVE_CDROM : return use_symbolic ? "drive-optical-symbolic" : "drive-optical";
  default : return use_symbolic ? "folder-symbolic" : "folder";
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
  }
}

static GIcon *
g_win32_mount_get_icon (GMount *mount)
{
  GWin32Mount *win32_mount = G_WIN32_MOUNT (mount);

  g_return_val_if_fail (win32_mount->mount_path != NULL, NULL);

  /* lazy creation */
  if (!win32_mount->icon)
    {
      SHFILEINFOW shfi;
      wchar_t *wfn = g_utf8_to_utf16 (win32_mount->mount_path, -1, NULL, NULL, NULL);

      if (SHGetFileInfoW (wfn, 0, &shfi, sizeof (shfi), SHGFI_ICONLOCATION))
        {
	  gchar *name = g_utf16_to_utf8 (shfi.szDisplayName, -1, NULL, NULL, NULL);
	  gchar *id = g_strdup_printf ("%s,%i", name, shfi.iIcon);
	  win32_mount->icon = g_themed_icon_new (id);
	  g_free (name);
	  g_free (id);
	}
      else
        {
LRN's avatar
LRN committed
230
          win32_mount->icon = g_themed_icon_new_with_default_fallbacks (_win32_drive_type_to_icon (win32_mount->drive_type, FALSE));
231 232 233 234 235 236
	}
    }

  return g_object_ref (win32_mount->icon);
}

237 238 239 240 241 242 243 244 245 246
static GIcon *
g_win32_mount_get_symbolic_icon (GMount *mount)
{
  GWin32Mount *win32_mount = G_WIN32_MOUNT (mount);

  g_return_val_if_fail (win32_mount->mount_path != NULL, NULL);

  /* lazy creation */
  if (!win32_mount->symbolic_icon)
    {
LRN's avatar
LRN committed
247
      win32_mount->symbolic_icon = g_themed_icon_new_with_default_fallbacks (_win32_drive_type_to_icon (win32_mount->drive_type, TRUE));
248 249 250 251 252
    }

  return g_object_ref (win32_mount->symbolic_icon);
}

253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
static char *
g_win32_mount_get_uuid (GMount *mount)
{
  return NULL;
}

static char *
g_win32_mount_get_name (GMount *mount)
{
  GWin32Mount *win32_mount = G_WIN32_MOUNT (mount);
  
  return g_strdup (win32_mount->name);
}

static GDrive *
g_win32_mount_get_drive (GMount *mount)
{
  GWin32Mount *win32_mount = G_WIN32_MOUNT (mount);

  if (win32_mount->volume != NULL)
    return g_volume_get_drive (G_VOLUME (win32_mount->volume));

  return NULL;
}

static GVolume *
g_win32_mount_get_volume (GMount *mount)
{
  GWin32Mount *win32_mount = G_WIN32_MOUNT (mount);

  if (win32_mount->volume)
    return G_VOLUME (g_object_ref (win32_mount->volume));
  
  return NULL;
}

static gboolean
g_win32_mount_can_unmount (GMount *mount)
{
  return FALSE;
}

static gboolean
g_win32_mount_can_eject (GMount *mount)
{
  GWin32Mount *win32_mount = G_WIN32_MOUNT (mount);
  return win32_mount->can_eject;
}


typedef struct {
  GWin32Mount *win32_mount;
  GAsyncReadyCallback callback;
  gpointer user_data;
  GCancellable *cancellable;
  int error_fd;
  GIOChannel *error_channel;
  guint error_channel_source_id;
  GString *error_string;
} UnmountEjectOp;

static void
315 316 317 318 319
g_win32_mount_unmount (GMount              *mount,
		       GMountUnmountFlags   flags,
		       GCancellable        *cancellable,
		       GAsyncReadyCallback  callback,
		       gpointer             user_data)
320 321 322 323
{
}

static gboolean
324 325 326
g_win32_mount_unmount_finish (GMount        *mount,
			      GAsyncResult  *result,
			      GError       **error)
327 328 329 330 331
{
  return FALSE;
}

static void
332 333 334 335 336
g_win32_mount_eject (GMount              *mount,
		     GMountUnmountFlags   flags,
		     GCancellable        *cancellable,
		     GAsyncReadyCallback  callback,
		     gpointer             user_data)
337 338 339 340
{
}

static gboolean
341 342 343
g_win32_mount_eject_finish (GMount        *mount,
			    GAsyncResult  *result,
			    GError       **error)
344 345 346 347 348 349 350 351 352 353
{
  return FALSE;
}

static void
g_win32_mount_mount_iface_init (GMountIface *iface)
{
  iface->get_root = g_win32_mount_get_root;
  iface->get_name = g_win32_mount_get_name;
  iface->get_icon = g_win32_mount_get_icon;
354
  iface->get_symbolic_icon = g_win32_mount_get_symbolic_icon;
355 356 357 358 359 360 361 362 363 364
  iface->get_uuid = g_win32_mount_get_uuid;
  iface->get_drive = g_win32_mount_get_drive;
  iface->get_volume = g_win32_mount_get_volume;
  iface->can_unmount = g_win32_mount_can_unmount;
  iface->can_eject = g_win32_mount_can_eject;
  iface->unmount = g_win32_mount_unmount;
  iface->unmount_finish = g_win32_mount_unmount_finish;
  iface->eject = g_win32_mount_eject;
  iface->eject_finish = g_win32_mount_eject_finish;
}