Blame gio/gwin32volumemonitor.c

Packit ae235b
/* GIO - GLib Input, Output and Streaming Library
Packit ae235b
 * 
Packit ae235b
 * Copyright (C) 2006-2007 Red Hat, Inc.
Packit ae235b
 * Copyright (C) 2008 Hans Breuer
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General
Packit ae235b
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 *
Packit ae235b
 * Author: Alexander Larsson <alexl@redhat.com>
Packit ae235b
 *         David Zeuthen <davidz@redhat.com>
Packit ae235b
 *         Hans Breuer <hans@breuer.org>
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
Packit ae235b
#include <string.h>
Packit ae235b
Packit ae235b
#include <glib.h>
Packit ae235b
#include "glibintl.h"
Packit ae235b
Packit ae235b
#include "gwin32volumemonitor.h"
Packit ae235b
#include "gwin32mount.h"
Packit ae235b
#include "gmount.h"
Packit ae235b
#include "giomodule.h"
Packit ae235b
Packit ae235b
#include <windows.h>
Packit ae235b
Packit ae235b
struct _GWin32VolumeMonitor {
Packit ae235b
  GNativeVolumeMonitor parent;
Packit ae235b
};
Packit ae235b
Packit ae235b
#define g_win32_volume_monitor_get_type _g_win32_volume_monitor_get_type
Packit ae235b
G_DEFINE_TYPE_WITH_CODE (GWin32VolumeMonitor, g_win32_volume_monitor, G_TYPE_NATIVE_VOLUME_MONITOR,
Packit ae235b
                         g_io_extension_point_implement (G_NATIVE_VOLUME_MONITOR_EXTENSION_POINT_NAME,
Packit ae235b
							 g_define_type_id,
Packit ae235b
							 "win32",
Packit ae235b
							 0));
Packit ae235b
							 
Packit ae235b
/**
Packit ae235b
 * get_viewable_logical_drives:
Packit ae235b
 *
Packit ae235b
 * Returns the list of logical and viewable drives as defined by
Packit ae235b
 * GetLogicalDrives() and the registry keys
Packit ae235b
 * Software\Microsoft\Windows\CurrentVersion\Policies\Explorer under
Packit ae235b
 * HKLM or HKCU. If neither key exists the result of
Packit ae235b
 * GetLogicalDrives() is returned.
Packit ae235b
 *
Packit ae235b
 * Returns: bitmask with same meaning as returned by GetLogicalDrives()
Packit ae235b
 */
Packit ae235b
static guint32 
Packit ae235b
get_viewable_logical_drives (void)
Packit ae235b
{
Packit ae235b
  guint viewable_drives = GetLogicalDrives ();
Packit ae235b
  HKEY key;
Packit ae235b
Packit ae235b
  DWORD var_type = REG_DWORD; //the value's a REG_DWORD type
Packit ae235b
  DWORD no_drives_size = 4;
Packit ae235b
  DWORD no_drives;
Packit ae235b
  gboolean hklm_present = FALSE;
Packit ae235b
Packit ae235b
  if (RegOpenKeyEx (HKEY_LOCAL_MACHINE,
Packit ae235b
		    "Software\\Microsoft\\Windows\\"
Packit ae235b
		    "CurrentVersion\\Policies\\Explorer",
Packit ae235b
		    0, KEY_READ, &key) == ERROR_SUCCESS)
Packit ae235b
    {
Packit ae235b
      if (RegQueryValueEx (key, "NoDrives", NULL, &var_type,
Packit ae235b
			   (LPBYTE) &no_drives, &no_drives_size) == ERROR_SUCCESS)
Packit ae235b
	{
Packit ae235b
	  /* We need the bits that are set in viewable_drives, and
Packit ae235b
	   * unset in no_drives.
Packit ae235b
	   */
Packit ae235b
	  viewable_drives = viewable_drives & ~no_drives;
Packit ae235b
	  hklm_present = TRUE;
Packit ae235b
	}
Packit ae235b
      RegCloseKey (key);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* If the key is present in HKLM then the one in HKCU should be ignored */
Packit ae235b
  if (!hklm_present)
Packit ae235b
    {
Packit ae235b
      if (RegOpenKeyEx (HKEY_CURRENT_USER,
Packit ae235b
			"Software\\Microsoft\\Windows\\"
Packit ae235b
			"CurrentVersion\\Policies\\Explorer",
Packit ae235b
			0, KEY_READ, &key) == ERROR_SUCCESS)
Packit ae235b
	{
Packit ae235b
	  if (RegQueryValueEx (key, "NoDrives", NULL, &var_type,
Packit ae235b
			       (LPBYTE) &no_drives, &no_drives_size) == ERROR_SUCCESS)
Packit ae235b
	    {
Packit ae235b
	      viewable_drives = viewable_drives & ~no_drives;
Packit ae235b
	    }
Packit ae235b
	  RegCloseKey (key);
Packit ae235b
	}
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return viewable_drives; 
Packit ae235b
}
Packit ae235b
Packit ae235b
/* deliver accesible (aka 'mounted') volumes */
Packit ae235b
static GList *
Packit ae235b
get_mounts (GVolumeMonitor *volume_monitor)
Packit ae235b
{
Packit ae235b
  DWORD   drives;
Packit ae235b
  gchar   drive[4] = "A:\\";
Packit ae235b
  GList *list = NULL;
Packit ae235b
  
Packit ae235b
  drives = get_viewable_logical_drives ();
Packit ae235b
Packit ae235b
  if (!drives)
Packit ae235b
    g_warning ("get_viewable_logical_drives failed.");
Packit ae235b
Packit ae235b
  while (drives && drive[0] <= 'Z')
Packit ae235b
    {
Packit ae235b
      if (drives & 1)
Packit ae235b
      {
Packit ae235b
	list = g_list_prepend (list, _g_win32_mount_new (volume_monitor, drive, NULL));
Packit ae235b
      }
Packit ae235b
      drives >>= 1;
Packit ae235b
      drive[0]++;
Packit ae235b
    }
Packit ae235b
  return list;
Packit ae235b
}
Packit ae235b
Packit ae235b
/* actually 'mounting' volumes is out of GIOs business on win32, so no volumes are delivered either */
Packit ae235b
static GList *
Packit ae235b
get_volumes (GVolumeMonitor *volume_monitor)
Packit ae235b
{
Packit ae235b
  return NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
/* real hardware */
Packit ae235b
static GList *
Packit ae235b
get_connected_drives (GVolumeMonitor *volume_monitor)
Packit ae235b
{
Packit ae235b
  GList *list = NULL;
Packit ae235b
Packit ae235b
#if 0
Packit ae235b
  HANDLE  find_handle;
Packit ae235b
  BOOL    found;
Packit ae235b
  wchar_t wc_name[MAX_PATH+1];
Packit ae235b
  
Packit ae235b
  find_handle = FindFirstVolumeW (wc_name, MAX_PATH);
Packit ae235b
  found = (find_handle != INVALID_HANDLE_VALUE);
Packit ae235b
  while (found)
Packit ae235b
    {
Packit ae235b
      /* I don't know what this code is supposed to do; clearly it now
Packit ae235b
       * does nothing, the returned GList is always NULL. But what was
Packit ae235b
       * this code supposed to be a start of? The volume names that
Packit ae235b
       * the FindFirstVolume/FindNextVolume loop iterates over returns
Packit ae235b
       * device names like
Packit ae235b
       *
Packit ae235b
       *   \Device\HarddiskVolume1
Packit ae235b
       *   \Device\HarddiskVolume2
Packit ae235b
       *   \Device\CdRom0
Packit ae235b
       *
Packit ae235b
       * No DOS devices there, so I don't see the point with the
Packit ae235b
       * QueryDosDevice call below. Probably this code is confusing volumes
Packit ae235b
       * with something else that does contain the mapping from DOS devices
Packit ae235b
       * to volumes.
Packit ae235b
       */
Packit ae235b
      wchar_t wc_dev_name[MAX_PATH+1];
Packit ae235b
      guint trailing = wcslen (wc_name) - 1;
Packit ae235b
Packit ae235b
      /* remove trailing backslash and leading \\?\\ */
Packit ae235b
      wc_name[trailing] = L'\0';
Packit ae235b
      if (QueryDosDeviceW (&wc_name[4], wc_dev_name, MAX_PATH))
Packit ae235b
        {
Packit ae235b
          gchar *name = g_utf16_to_utf8 (wc_dev_name, -1, NULL, NULL, NULL);
Packit ae235b
          g_print ("%s\n", name);
Packit ae235b
	  g_free (name);
Packit ae235b
	}
Packit ae235b
Packit ae235b
      found = FindNextVolumeW (find_handle, wc_name, MAX_PATH);
Packit ae235b
    }
Packit ae235b
  if (find_handle != INVALID_HANDLE_VALUE)
Packit ae235b
    FindVolumeClose (find_handle);
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  return list;
Packit ae235b
}
Packit ae235b
Packit ae235b
static GVolume *
Packit ae235b
get_volume_for_uuid (GVolumeMonitor *volume_monitor, const char *uuid)
Packit ae235b
{
Packit ae235b
  return NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
static GMount *
Packit ae235b
get_mount_for_uuid (GVolumeMonitor *volume_monitor, const char *uuid)
Packit ae235b
{
Packit ae235b
  return NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
is_supported (void)
Packit ae235b
{
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static GMount *
Packit ae235b
get_mount_for_mount_path (const char *mount_path,
Packit ae235b
                          GCancellable *cancellable)
Packit ae235b
{
Packit ae235b
  GWin32Mount *mount;
Packit ae235b
Packit ae235b
  /* TODO: Set mountable volume? */
Packit ae235b
  mount = _g_win32_mount_new (NULL, mount_path, NULL);
Packit ae235b
Packit ae235b
  return G_MOUNT (mount);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_win32_volume_monitor_class_init (GWin32VolumeMonitorClass *klass)
Packit ae235b
{
Packit ae235b
  GVolumeMonitorClass *monitor_class = G_VOLUME_MONITOR_CLASS (klass);
Packit ae235b
  GNativeVolumeMonitorClass *native_class = G_NATIVE_VOLUME_MONITOR_CLASS (klass);
Packit ae235b
  
Packit ae235b
  monitor_class->get_mounts = get_mounts;
Packit ae235b
  monitor_class->get_volumes = get_volumes;
Packit ae235b
  monitor_class->get_connected_drives = get_connected_drives;
Packit ae235b
  monitor_class->get_volume_for_uuid = get_volume_for_uuid;
Packit ae235b
  monitor_class->get_mount_for_uuid = get_mount_for_uuid;
Packit ae235b
  monitor_class->is_supported = is_supported;
Packit ae235b
Packit ae235b
  native_class->get_mount_for_mount_path = get_mount_for_mount_path;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_win32_volume_monitor_init (GWin32VolumeMonitor *win32_monitor)
Packit ae235b
{
Packit ae235b
  /* maybe we shoud setup a callback window to listern for WM_DEVICECHANGE ? */
Packit ae235b
#if 0
Packit ae235b
  unix_monitor->mount_monitor = g_win32_mount_monitor_new ();
Packit ae235b
Packit ae235b
  g_signal_connect (win32_monitor->mount_monitor,
Packit ae235b
		    "mounts-changed", G_CALLBACK (mounts_changed),
Packit ae235b
		    win32_monitor);
Packit ae235b
  
Packit ae235b
  g_signal_connect (win32_monitor->mount_monitor,
Packit ae235b
		    "mountpoints-changed", G_CALLBACK (mountpoints_changed),
Packit ae235b
		    win32_monitor);
Packit ae235b
		    
Packit ae235b
  update_volumes (win32_monitor);
Packit ae235b
  update_mounts (win32_monitor);
Packit ae235b
#endif
Packit ae235b
}