gthreadedresolver: Unref unexpected address
[platform/upstream/glib.git] / gio / gwin32appinfo.c
index 0192eff..7762a2b 100644 (file)
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General
- * Public License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
- * Boston, MA 02111-1307, USA.
+ * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
  *
  * Author: Alexander Larsson <alexl@redhat.com>
  */
 
-#include <config.h>
+#include "config.h"
 
 #include <string.h>
 
-#include "gcontenttypeprivate.h"
+#include "gcontenttype.h"
 #include "gwin32appinfo.h"
+#include "gappinfo.h"
 #include "gioerror.h"
+#include "gfile.h"
 #include <glib/gstdio.h>
 #include "glibintl.h"
 
 #include <windows.h>
 #include <shlwapi.h>
 
-#include "gioalias.h"
 
 #ifndef ASSOCF_INIT_BYEXENAME
 #define ASSOCF_INIT_BYEXENAME 0x00000002
@@ -45,6 +44,9 @@
 #define REAL_ASSOCSTR_FRIENDLYDOCNAME 3
 #define REAL_ASSOCSTR_FRIENDLYAPPNAME 4
 
+#ifndef AssocQueryString
+#pragma message("AssocQueryString not available with SDK used")
+#endif
 
 static void g_win32_app_info_iface_init (GAppInfoIface *iface);
 
@@ -75,9 +77,8 @@ g_win32_app_info_finalize (GObject *object)
   g_free (info->id_utf8);
   g_free (info->name);
   g_free (info->executable);
-  
-  if (G_OBJECT_CLASS (g_win32_app_info_parent_class)->finalize)
-    (*G_OBJECT_CLASS (g_win32_app_info_parent_class)->finalize) (object);
+
+  G_OBJECT_CLASS (g_win32_app_info_parent_class)->finalize (object);
 }
 
 static void
@@ -97,7 +98,9 @@ static GAppInfo *
 g_desktop_app_info_new_from_id (wchar_t *id /* takes ownership */,
                                gboolean id_is_exename)
 {
+#ifdef AssocQueryString
   ASSOCF flags;
+#endif
   wchar_t buffer[1024];
   DWORD buffer_size;
   GWin32AppInfo *info;
@@ -108,6 +111,7 @@ g_desktop_app_info_new_from_id (wchar_t *id /* takes ownership */,
   info->id_utf8 = g_utf16_to_utf8 (id, -1, NULL, NULL, NULL);  
   info->id_is_exename = id_is_exename;
 
+#ifdef AssocQueryString  
   flags = 0;
   if (id_is_exename)
     flags |= ASSOCF_INIT_BYEXENAME;
@@ -129,6 +133,7 @@ g_desktop_app_info_new_from_id (wchar_t *id /* takes ownership */,
                        buffer,
                        &buffer_size) == S_OK)
     info->name = g_utf16_to_utf8 (buffer, -1, NULL, NULL, NULL);
+#endif
 
   if (info->name == NULL)
     {
@@ -139,6 +144,7 @@ g_desktop_app_info_new_from_id (wchar_t *id /* takes ownership */,
        info->name = g_strdup (info->id_utf8);
     }
 
+#ifdef AssocQueryString
   if (AssocQueryKeyW(flags,
                     ASSOCKEY_APP,
                     info->id,
@@ -150,6 +156,7 @@ g_desktop_app_info_new_from_id (wchar_t *id /* takes ownership */,
        info->no_open_with = TRUE;
       RegCloseKey (app_key);
     }
+#endif
   
   return G_APP_INFO (info);
 }
@@ -183,7 +190,7 @@ g_win32_app_info_dup (GAppInfo *appinfo)
 
 static gboolean
 g_win32_app_info_equal (GAppInfo *appinfo1,
-                         GAppInfo *appinfo2)
+                        GAppInfo *appinfo2)
 {
   GWin32AppInfo *info1 = G_WIN32_APP_INFO (appinfo1);
   GWin32AppInfo *info2 = G_WIN32_APP_INFO (appinfo2);
@@ -229,7 +236,7 @@ g_win32_app_info_get_executable (GAppInfo *appinfo)
   return info->executable;
 }
 
-static const char *
+static GIcon *
 g_win32_app_info_get_icon (GAppInfo *appinfo)
 {
   /* GWin32AppInfo *info = G_WIN32_APP_INFO (appinfo); */
@@ -239,34 +246,46 @@ g_win32_app_info_get_icon (GAppInfo *appinfo)
 }
 
 static gboolean
-g_win32_app_info_launch (GAppInfo                *appinfo,
-                        GList                   *files,
-                        GAppLaunchContext       *launch_context,
-                        GError                 **error)
+g_win32_app_info_launch (GAppInfo           *appinfo,
+                        GList              *files,
+                        GAppLaunchContext  *launch_context,
+                        GError            **error)
 {
   GWin32AppInfo *info = G_WIN32_APP_INFO (appinfo);
+#ifdef AssocQueryString
   ASSOCF flags;
+#endif
   HKEY class_key;
   SHELLEXECUTEINFOW exec_info = {0};
   GList *l;
 
   /* TODO:  What might startup_id mean on win32? */
-  
+#ifdef AssocQueryString  
   flags = 0;
   if (info->id_is_exename)
     flags |= ASSOCF_INIT_BYEXENAME;
 
-  if (AssocQueryKeyW(flags,
-                    ASSOCKEY_SHELLEXECCLASS,
-                    info->id,
-                    NULL,
-                    &class_key) != S_OK)
+  if (AssocQueryKeyW (flags,
+                     ASSOCKEY_SHELLEXECCLASS,
+                     info->id,
+                     NULL,
+                     &class_key) != S_OK)
     {
-      g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, _("Can't find application"));
+      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED, _("Can't find application"));
       return FALSE;
     }
+#endif
 
-  for (l = file; l != NULL; l = l->next)
+  /* FIXME: Need to do something with
+   * g_app_launch_context_get_environment()... ShellExecuteExW()
+   * doesn't have any way to pass an environment though. We need to
+   * either (a) update environment, ShellExecuteExW(), revert
+   * environment; or (b) find an API to figure out what app
+   * ShellExecuteExW() would launch, and then use g_spawn_async()
+   * instead.
+   */
+
+  for (l = files; l != NULL; l = l->next)
     {
       char *path = g_file_get_path (l->data);
       wchar_t *wfilename = g_utf8_to_utf16 (path, -1, NULL, NULL, NULL);
@@ -280,25 +299,12 @@ g_win32_app_info_launch (GAppInfo                *appinfo,
       exec_info.nShow = SW_SHOWNORMAL;
       exec_info.hkeyClass = class_key;
       
-      if (!ShellExecuteExW(&exec_info))
+      if (!ShellExecuteExW (&exec_info))
        {
-         DWORD last_error;
-         LPVOID message;
-         char *message_utf8;
-         
-         last_error = GetLastError ();
-         FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER | 
-                        FORMAT_MESSAGE_FROM_SYSTEM,
-                        NULL,
-                        last_error,
-                        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
-                        (LPTSTR) &message,
-                        0, NULL );
-         
-         message_utf8 = g_utf16_to_utf8 (message, -1, NULL, NULL, NULL);
+         char *message_utf8 = g_win32_error_message (GetLastError ());
+
          g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, _("Error launching application: %s"), message_utf8);
          g_free (message_utf8);
-         LocalFree (message);
          
          g_free (wfilename);
          RegCloseKey (class_key);
@@ -320,18 +326,25 @@ g_win32_app_info_supports_uris (GAppInfo *appinfo)
 }
 
 static gboolean
-g_win32_app_info_launch_uris (GAppInfo *appinfo,
-                             GList *uris,
-                             GAppLaunchContext *launch_context,
-                             GError **error)
+g_win32_app_info_supports_files (GAppInfo *appinfo)
 {
-  g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, _("URIs not supported"));
+  return TRUE;
+}
+
+static gboolean
+g_win32_app_info_launch_uris (GAppInfo           *appinfo,
+                             GList              *uris,
+                             GAppLaunchContext  *launch_context,
+                             GError            **error)
+{
+  g_set_error_literal (error, G_IO_ERROR, 
+                       G_IO_ERROR_NOT_SUPPORTED, 
+                       _("URIs not supported"));
   return FALSE;
 }
 
 static gboolean
-g_win32_app_info_should_show (GAppInfo *appinfo,
-                             const char *win32_env)
+g_win32_app_info_should_show (GAppInfo *appinfo)
 {
   GWin32AppInfo *info = G_WIN32_APP_INFO (appinfo);
 
@@ -343,20 +356,24 @@ g_win32_app_info_should_show (GAppInfo *appinfo,
 
 static gboolean
 g_win32_app_info_set_as_default_for_type (GAppInfo    *appinfo,
-                                           const char  *content_type,
-                                           GError     **error)
+                                          const char  *content_type,
+                                          GError     **error)
 {
-  g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, _("association changes not supported on win32"));
+  g_set_error_literal (error, G_IO_ERROR, 
+                       G_IO_ERROR_NOT_SUPPORTED, 
+                       _("association changes not supported on win32"));
   return FALSE;
 }
 
 GAppInfo *
-g_app_info_create_from_commandline (const char *commandline,
-                                   const char *application_name,
-                                   GAppInfoCreateFlags flags,
-                                   GError **error)
+g_app_info_create_from_commandline (const char           *commandline,
+                                   const char           *application_name,
+                                   GAppInfoCreateFlags   flags,
+                                   GError              **error)
 {
-  g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, _("Association creation not supported on win32"));
+  g_set_error_literal (error, G_IO_ERROR, 
+                       G_IO_ERROR_NOT_SUPPORTED, 
+                       _("Association creation not supported on win32"));
   return NULL;
 }
 
@@ -373,13 +390,14 @@ g_win32_app_info_iface_init (GAppInfoIface *iface)
   iface->get_icon = g_win32_app_info_get_icon;
   iface->launch = g_win32_app_info_launch;
   iface->supports_uris = g_win32_app_info_supports_uris;
+  iface->supports_files = g_win32_app_info_supports_files;
   iface->launch_uris = g_win32_app_info_launch_uris;
   iface->should_show = g_win32_app_info_should_show;
   iface->set_as_default_for_type = g_win32_app_info_set_as_default_for_type;
 }
 
 static void
-enumerate_open_with_list (HKEY dir_key,
+enumerate_open_with_list (HKEY    dir_key,
                          GList **prognames)
 {
   DWORD index;
@@ -393,14 +411,14 @@ enumerate_open_with_list (HKEY dir_key,
   index = 0;
   name_len = 256;
   nbytes = sizeof (data) - 2;
-  while (RegEnumValueW(dir_key,
-                      index,
-                      name,
-                      &name_len,
-                      0,
-                      &type,
-                      (LPBYTE)data,
-                      &nbytes) == ERROR_SUCCESS)
+  while (RegEnumValueW (dir_key,
+                       index,
+                       name,
+                       &name_len,
+                       0,
+                       &type,
+                       (LPBYTE)data,
+                       &nbytes) == ERROR_SUCCESS)
     {
       data[nbytes/2] = '\0';
       if (type == REG_SZ &&
@@ -418,14 +436,14 @@ enumerate_open_with_list (HKEY dir_key,
   
   index = 0;
   name_len = 256;
-  while (RegEnumKeyExW(dir_key,
-                      index,
-                      name,
-                      &name_len,
-                      NULL,
-                      NULL,
-                      NULL,
-                      NULL) == ERROR_SUCCESS)
+  while (RegEnumKeyExW (dir_key,
+                       index,
+                       name,
+                       &name_len,
+                       NULL,
+                       NULL,
+                       NULL,
+                       NULL) == ERROR_SUCCESS)
     {
       *prognames = g_list_prepend (*prognames, g_memdup (name, (name_len + 1) * 2));
       index++;
@@ -443,14 +461,14 @@ enumerate_open_with_progids (HKEY dir_key,
 
   index = 0;
   name_len = 256;
-  while (RegEnumValueW(dir_key,
-                      index,
-                      name,
-                      &name_len,
-                      0,
-                      &type,
-                      NULL,
-                      0) == ERROR_SUCCESS)
+  while (RegEnumValueW (dir_key,
+                       index,
+                       name,
+                       &name_len,
+                       0,
+                       &type,
+                       NULL,
+                       0) == ERROR_SUCCESS)
     {
       *progids = g_list_prepend (*progids, g_memdup (name, (name_len + 1) * 2));
       index++;
@@ -459,7 +477,7 @@ enumerate_open_with_progids (HKEY dir_key,
 }
 
 static void
-enumerate_open_with_root (HKEY dir_key,
+enumerate_open_with_root (HKEY    dir_key,
                          GList **progids,
                          GList **prognames)
 {
@@ -481,13 +499,14 @@ enumerate_open_with_root (HKEY dir_key,
 }
 
 static gboolean
-app_info_in_list (GAppInfo *info, GList *l)
+app_info_in_list (GAppInfo *info, 
+                  GList    *list)
 {
-  while (l != NULL)
+  while (list != NULL)
     {
-      if (g_app_info_equal (info, l->data))
+      if (g_app_info_equal (info, list->data))
        return TRUE;
-      l = l->next;
+      list = list->next;
     }
   return FALSE;
 }
@@ -575,9 +594,31 @@ g_app_info_get_all_for_type (const char *content_type)
   return g_list_reverse (infos);
 }
 
+GList *
+g_app_info_get_recommended_for_type (const char *content_type)
+{
+  /* FIXME: this should generate a list of applications that are registered
+   * as direct handlers for the given content type, without using MIME subclassing.
+   * See g_app_info_get_recommended_for_type() in gdesktopappinfo.c for a reference
+   * UNIX implementation.
+   */
+  return g_app_info_get_all_for_type (content_type);
+}
+
+GList *
+g_app_info_get_fallback_for_type (const char *content_type)
+{
+  /* FIXME: this should generate a list of applications that are registered
+   * as handlers for a superclass of the given content type, but are not
+   * direct handlers for the content type itself. See g_app_info_get_fallback_for_type()
+   * in gdesktopappinfo.c for a reference UNIX implementation.
+   */
+  return g_app_info_get_all_for_type (content_type);
+}
+
 GAppInfo *
 g_app_info_get_default_for_type (const char *content_type,
-                                gboolean must_support_uris)
+                                gboolean    must_support_uris)
 {
   wchar_t *wtype;
   wchar_t buffer[1024];
@@ -586,15 +627,17 @@ g_app_info_get_default_for_type (const char *content_type,
   wtype = g_utf8_to_utf16 (content_type, -1, NULL, NULL, NULL);
 
   /* Verify that we have some sort of app registered for this type */
+#ifdef AssocQueryString
   buffer_size = 1024;
-  if (AssocQueryStringW(0,
-                       REAL_ASSOCSTR_COMMAND,
-                       wtype,
-                       NULL,
-                       buffer,
-                       &buffer_size) == S_OK)
+  if (AssocQueryStringW (0,
+                        REAL_ASSOCSTR_COMMAND,
+                        wtype,
+                        NULL,
+                        buffer,
+                        &buffer_size) == S_OK)
     /* Takes ownership of wtype */
     return g_desktop_app_info_new_from_id (wtype, FALSE);
+#endif
 
   g_free (wtype);
   return NULL;
@@ -624,14 +667,14 @@ g_app_info_get_all (void)
   infos = NULL;
   index = 0;
   name_len = 256;
-  while (RegEnumKeyExW(reg_key,
-                      index,
-                      name,
-                      &name_len,
-                      NULL,
-                      NULL,
-                      NULL,
-                      NULL) == ERROR_SUCCESS)
+  while (RegEnumKeyExW (reg_key,
+                       index,
+                       name,
+                       &name_len,
+                       NULL,
+                       NULL,
+                       NULL,
+                       NULL) == ERROR_SUCCESS)
     {
       wchar_t *name_dup = g_memdup (name, (name_len+1)*2);
       /* name_dup ownership is taken */
@@ -646,3 +689,9 @@ g_app_info_get_all (void)
 
   return g_list_reverse (infos);
 }
+
+void
+g_app_info_reset_type_associations (const char *content_type)
+{
+  /* nothing to do */
+}