diff --git a/gio/gwin32mount.c b/gio/gwin32mount.c index 83d8695a1..f5be8adfa 100644 --- a/gio/gwin32mount.c +++ b/gio/gwin32mount.c @@ -25,7 +25,47 @@ #include #define WIN32_MEAN_AND_LEAN +#define COBJMACROS #include +#include +#include + +/* At the moment of writing IExtractIconW interface in Mingw-w64 + * is missing IUnknown members in its vtable. Use our own + * fixed declaration for now. + */ +#undef INTERFACE +#define INTERFACE IMyExtractIconW +DECLARE_INTERFACE_(IMyExtractIconW,IUnknown) +{ + /*** IUnknown methods ***/ + STDMETHOD_(HRESULT,QueryInterface)(THIS_ REFIID riid, void** ppvObject) PURE; + STDMETHOD_(ULONG,AddRef)(THIS) PURE; + STDMETHOD_(ULONG,Release)(THIS) PURE; + /*** IMyExtractIconW methods ***/ + STDMETHOD(GetIconLocation)(THIS_ UINT uFlags, LPWSTR pszIconFile, UINT cchMax, int *piIndex, PUINT pwFlags) PURE; + STDMETHOD(Extract)(THIS_ LPCWSTR pszFile, UINT nIconIndex, HICON *phiconLarge, HICON *phiconSmall, UINT nIconSize) PURE; +}; +#undef INTERFACE + +#if !defined(__cplusplus) || defined(CINTERFACE) +/*** IUnknown methods ***/ +#define IMyExtractIconW_QueryInterface(p,a,b) (p)->lpVtbl->QueryInterface(p,a,b) +#define IMyExtractIconW_AddRef(p) (p)->lpVtbl->AddRef(p) +#define IMyExtractIconW_Release(p) (p)->lpVtbl->Release(p) +/*** IMyExtractIconW methods ***/ +#define IMyExtractIconW_GetIconLocation(p,a,b,c,d,e) (p)->lpVtbl->GetIconLocation(p,a,b,c,d,e) +#define IMyExtractIconW_Extract(p,a,b,c,d,e) (p)->lpVtbl->Extract(p,a,b,c,d,e) +#else +/*** IUnknown methods ***/ +#define IMyExtractIconW_QueryInterface(p,a,b) (p)->QueryInterface(a,b) +#define IMyExtractIconW_AddRef(p) (p)->AddRef() +#define IMyExtractIconW_Release(p) (p)->Release() +/*** IMyExtractIconW methods ***/ +#define IMyExtractIconW_GetIconLocation(p,a,b,c,d,e) (p)->GetIconLocation(p,a,b,c,d,e) +#define IMyExtractIconW_Extract(p,a,b,c,d,e) (p)->Extract(p,a,b,c,d,e) +#endif + #include #include "gwin32volumemonitor.h" @@ -103,14 +143,52 @@ g_win32_mount_init (GWin32Mount *win32_mount) { } +/* wdrive doesn't need to end with a path separator. + wdrive must use backslashes as path separators, not slashes. + IShellFolder::ParseDisplayName() takes non-const string as input, + so wdrive can't be a const string. + Returns the name on success (free with g_free), + NULL otherwise. + */ +static gchar * +get_mount_display_name (gunichar2 *wdrive) +{ + IShellFolder *desktop; + PIDLIST_RELATIVE volume; + STRRET volume_name; + gchar *result = NULL; + + /* Get the desktop folder object reference */ + if (!SUCCEEDED (SHGetDesktopFolder (&desktop))) + return result; + + if (SUCCEEDED (IShellFolder_ParseDisplayName (desktop, NULL, NULL, wdrive, NULL, &volume, NULL))) + { + volume_name.uType = STRRET_WSTR; + + if (SUCCEEDED (IShellFolder_GetDisplayNameOf (desktop, volume, SHGDN_FORADDRESSBAR, &volume_name))) + { + wchar_t *volume_name_wchar; + + if (SUCCEEDED (StrRetToStrW (&volume_name, volume, &volume_name_wchar))) + { + result = g_utf16_to_utf8 (volume_name_wchar, -1, NULL, NULL, NULL); + CoTaskMemFree (volume_name_wchar); + } + } + CoTaskMemFree (volume); + } + + IShellFolder_Release (desktop); + + return result; +} + static gchar * _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); + gchar *name = get_mount_display_name (wdrive); g_free (wdrive); return name ? name : g_strdup (drive); @@ -210,6 +288,92 @@ _win32_drive_type_to_icon (int type, gboolean use_symbolic) } } +/* mount_path doesn't need to end with a path separator. + mount_path must use backslashes as path separators, not slashes. + IShellFolder::ParseDisplayName() takes non-const string as input, + so mount_path can't be a const string. + result_name and result_index must not be NULL. + Returns TRUE when result_name is set (free with g_free), + FALSE otherwise. + */ +static gboolean +get_icon_name_index (wchar_t *mount_path, + wchar_t **result_name, + int *result_index) +{ + IShellFolder *desktop; + PIDLIST_RELATIVE volume; + IShellFolder *volume_parent; + PCUITEMID_CHILD volume_relative; + IMyExtractIconW *eicon; + int icon_index; + UINT icon_flags; + wchar_t *name_buffer; + gsize name_buffer_size; + gsize arbitrary_reasonable_limit = 5000; + gboolean result = FALSE; + + *result_name = NULL; + + /* Get the desktop folder object reference */ + if (!SUCCEEDED (SHGetDesktopFolder (&desktop))) + return FALSE; + + /* Construct the volume IDList relative to desktop */ + if (SUCCEEDED (IShellFolder_ParseDisplayName (desktop, NULL, NULL, mount_path, NULL, &volume, NULL))) + { + /* Get the parent of the volume (transfer-full) and the IDList relative to parent (transfer-none) */ + if (SUCCEEDED (SHBindToParent (volume, &IID_IShellFolder, (void **) &volume_parent, &volume_relative))) + { + /* Get a reference to IExtractIcon object for the volume */ + if (SUCCEEDED (IShellFolder_GetUIObjectOf (volume_parent, NULL, 1, (LPCITEMIDLIST *) &volume_relative, &IID_IExtractIconW, NULL, (void **) &eicon))) + { + gboolean keep_going = TRUE; + name_buffer = NULL; + name_buffer_size = MAX_PATH / 2; + while (keep_going) + { + name_buffer_size *= 2; + name_buffer = g_renew (wchar_t, name_buffer, name_buffer_size); + name_buffer[name_buffer_size - 1] = 0x1; /* sentinel */ + keep_going = FALSE; + + /* Try to get the icon location */ + if (SUCCEEDED (IMyExtractIconW_GetIconLocation (eicon, GIL_FORSHELL, name_buffer, name_buffer_size, &icon_index, &icon_flags))) + { + if (name_buffer[name_buffer_size - 1] != 0x1) + { + if (name_buffer_size < arbitrary_reasonable_limit) + { + /* Buffer was too small, keep going */ + keep_going = TRUE; + continue; + } + /* Else stop trying */ + } + /* name_buffer might not contain a name */ + else if ((icon_flags & GIL_NOTFILENAME) != GIL_NOTFILENAME) + { + *result_name = g_steal_pointer (&name_buffer); + *result_index = icon_index; + result = TRUE; + } + } + } + + g_free (name_buffer); + IMyExtractIconW_Release (eicon); + } + IShellFolder_Release (volume_parent); + } + CoTaskMemFree (volume); + } + + IShellFolder_Release (desktop); + + return result; +} + static GIcon * g_win32_mount_get_icon (GMount *mount) { @@ -220,15 +384,20 @@ g_win32_mount_get_icon (GMount *mount) /* lazy creation */ if (!win32_mount->icon) { - SHFILEINFOW shfi; + wchar_t *icon_path; + int icon_index; + wchar_t *p; wchar_t *wfn = g_utf8_to_utf16 (win32_mount->mount_path, -1, NULL, NULL, NULL); - if (SHGetFileInfoW (wfn, 0, &shfi, sizeof (shfi), SHGFI_ICONLOCATION)) + for (p = wfn; p != NULL && *p != 0; p++) + if (*p == L'/') + *p = L'\\'; + + if (get_icon_name_index (wfn, &icon_path, &icon_index)) { - gchar *name = g_utf16_to_utf8 (shfi.szDisplayName, -1, NULL, NULL, NULL); - gchar *id = g_strdup_printf ("%s,%i", name, shfi.iIcon); + gchar *id = g_strdup_printf ("%S,%i", icon_path, icon_index); + g_free (icon_path); win32_mount->icon = g_themed_icon_new (id); - g_free (name); g_free (id); } else