gio/ docs/reference/gio Merged gio-standalone into glib.
[platform/upstream/glib.git] / gio / gwin32appinfo.c
1 /* GIO - GLib Input, Output and Streaming Library
2  * 
3  * Copyright (C) 2006-2007 Red Hat, Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General
16  * Public License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  * Author: Alexander Larsson <alexl@redhat.com>
21  */
22
23 #include <config.h>
24
25 #include <string.h>
26
27 #include "gcontenttypeprivate.h"
28 #include "gwin32appinfo.h"
29 #include "gioerror.h"
30 #include <glib/gstdio.h>
31 #include "glibintl.h"
32
33 #include <windows.h>
34 #include <shlwapi.h>
35
36 #ifndef ASSOCF_INIT_BYEXENAME
37 #define ASSOCF_INIT_BYEXENAME 0x00000002
38 #endif
39
40 /* These were wrong in MingW */
41 #define REAL_ASSOCSTR_COMMAND 1
42 #define REAL_ASSOCSTR_EXECUTABLE 2
43 #define REAL_ASSOCSTR_FRIENDLYDOCNAME 3
44 #define REAL_ASSOCSTR_FRIENDLYAPPNAME 4
45
46
47 static void g_win32_app_info_iface_init (GAppInfoIface *iface);
48
49 struct _GWin32AppInfo
50 {
51   GObject parent_instance;
52   wchar_t *id;
53   char *id_utf8;
54   gboolean id_is_exename;
55   char *executable;
56   char *name;
57   gboolean no_open_with;
58 };
59
60 G_DEFINE_TYPE_WITH_CODE (GWin32AppInfo, g_win32_app_info, G_TYPE_OBJECT,
61                          G_IMPLEMENT_INTERFACE (G_TYPE_APP_INFO,
62                                                 g_win32_app_info_iface_init))
63
64
65 static void
66 g_win32_app_info_finalize (GObject *object)
67 {
68   GWin32AppInfo *info;
69
70   info = G_WIN32_APP_INFO (object);
71
72   g_free (info->id);
73   g_free (info->id_utf8);
74   g_free (info->name);
75   g_free (info->executable);
76   
77   if (G_OBJECT_CLASS (g_win32_app_info_parent_class)->finalize)
78     (*G_OBJECT_CLASS (g_win32_app_info_parent_class)->finalize) (object);
79 }
80
81 static void
82 g_win32_app_info_class_init (GWin32AppInfoClass *klass)
83 {
84   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
85   
86   gobject_class->finalize = g_win32_app_info_finalize;
87 }
88
89 static void
90 g_win32_app_info_init (GWin32AppInfo *local)
91 {
92 }
93
94 static GAppInfo *
95 g_desktop_app_info_new_from_id (wchar_t *id /* takes ownership */,
96                                 gboolean id_is_exename)
97 {
98   ASSOCF flags;
99   wchar_t buffer[1024];
100   DWORD buffer_size;
101   GWin32AppInfo *info;
102   HKEY app_key;
103   
104   info = g_object_new (G_TYPE_WIN32_APP_INFO, NULL);
105   info->id = id; /* Takes ownership */
106   info->id_utf8 = g_utf16_to_utf8 (id, -1, NULL, NULL, NULL);  
107   info->id_is_exename = id_is_exename;
108
109   flags = 0;
110   if (id_is_exename)
111     flags |= ASSOCF_INIT_BYEXENAME;
112
113   buffer_size = 1024;
114   if (AssocQueryStringW(flags,
115                         REAL_ASSOCSTR_EXECUTABLE,
116                         id,
117                         NULL,
118                         buffer,
119                         &buffer_size) == S_OK)
120     info->executable = g_utf16_to_utf8 (buffer, -1, NULL, NULL, NULL);
121  
122   buffer_size = 1024;
123   if (AssocQueryStringW(flags,
124                         REAL_ASSOCSTR_FRIENDLYAPPNAME,
125                         id,
126                         NULL,
127                         buffer,
128                         &buffer_size) == S_OK)
129     info->name = g_utf16_to_utf8 (buffer, -1, NULL, NULL, NULL);
130
131   if (info->name == NULL)
132     {
133       /* TODO: Should look up name from executable resources */
134       if (info->executable)
135         info->name = g_path_get_basename (info->executable);
136       else
137         info->name = g_strdup (info->id_utf8);
138     }
139
140   if (AssocQueryKeyW(flags,
141                      ASSOCKEY_APP,
142                      info->id,
143                      NULL,
144                      &app_key) == S_OK)
145     {
146       if (RegQueryValueExW (app_key, L"NoOpenWith", 0,
147                             NULL, NULL, NULL) == ERROR_SUCCESS)
148         info->no_open_with = TRUE;
149       RegCloseKey (app_key);
150     }
151   
152   return G_APP_INFO (info);
153 }
154
155 static wchar_t *
156 dup_wstring (wchar_t *str)
157 {
158   gsize len;
159   for (len = 0; str[len] != 0; len++)
160     ;
161   return (wchar_t *)g_memdup (str, (len + 1) * 2);
162 }
163
164 static GAppInfo *
165 g_win32_app_info_dup (GAppInfo *appinfo)
166 {
167   GWin32AppInfo *info = G_WIN32_APP_INFO (appinfo);
168   GWin32AppInfo *new_info;
169   
170   new_info = g_object_new (G_TYPE_WIN32_APP_INFO, NULL);
171
172   new_info->id = dup_wstring (info->id);
173   new_info->id_utf8 = g_strdup (info->id_utf8);
174   new_info->id_is_exename = info->id_is_exename;
175   new_info->name = g_strdup (info->name);
176   new_info->executable = g_strdup (info->executable);
177   new_info->no_open_with = info->no_open_with;
178   
179   return G_APP_INFO (new_info);
180 }
181
182 static gboolean
183 g_win32_app_info_equal (GAppInfo *appinfo1,
184                           GAppInfo *appinfo2)
185 {
186   GWin32AppInfo *info1 = G_WIN32_APP_INFO (appinfo1);
187   GWin32AppInfo *info2 = G_WIN32_APP_INFO (appinfo2);
188
189   if (info1->executable == NULL ||
190       info2->executable == NULL)
191     return FALSE;
192   
193   return strcmp (info1->executable, info2->executable) == 0;
194 }
195
196 static const char *
197 g_win32_app_info_get_id (GAppInfo *appinfo)
198 {
199   GWin32AppInfo *info = G_WIN32_APP_INFO (appinfo);
200
201   return info->id_utf8;
202 }
203
204 static const char *
205 g_win32_app_info_get_name (GAppInfo *appinfo)
206 {
207   GWin32AppInfo *info = G_WIN32_APP_INFO (appinfo);
208
209   if (info->name == NULL)
210     return _("Unnamed");
211   
212   return info->name;
213 }
214
215 static const char *
216 g_win32_app_info_get_description (GAppInfo *appinfo)
217 {
218   /* Win32 has no app descriptions */
219   return NULL;
220 }
221
222 static const char *
223 g_win32_app_info_get_executable (GAppInfo *appinfo)
224 {
225   GWin32AppInfo *info = G_WIN32_APP_INFO (appinfo);
226   
227   return info->executable;
228 }
229
230 static const char *
231 g_win32_app_info_get_icon (GAppInfo *appinfo)
232 {
233   /* GWin32AppInfo *info = G_WIN32_APP_INFO (appinfo); */
234
235   /* TODO: How to handle icons */
236   return NULL;
237 }
238
239 static gboolean
240 g_win32_app_info_launch (GAppInfo                *appinfo,
241                          GList                   *files,
242                          GAppLaunchContext       *launch_context,
243                          GError                 **error)
244 {
245   GWin32AppInfo *info = G_WIN32_APP_INFO (appinfo);
246   ASSOCF flags;
247   HKEY class_key;
248   SHELLEXECUTEINFOW exec_info = {0};
249   GList *l;
250
251   /* TODO:  What might startup_id mean on win32? */
252   
253   flags = 0;
254   if (info->id_is_exename)
255     flags |= ASSOCF_INIT_BYEXENAME;
256
257   if (AssocQueryKeyW(flags,
258                      ASSOCKEY_SHELLEXECCLASS,
259                      info->id,
260                      NULL,
261                      &class_key) != S_OK)
262     {
263       g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, _("Can't find application"));
264       return FALSE;
265     }
266
267   for (l = file; l != NULL; l = l->next)
268     {
269       char *path = g_file_get_path (l->data);
270       wchar_t *wfilename = g_utf8_to_utf16 (path, -1, NULL, NULL, NULL);
271
272       g_free (path);
273       
274       memset (&exec_info, 0, sizeof (exec_info));
275       exec_info.cbSize = sizeof (exec_info);
276       exec_info.fMask = SEE_MASK_FLAG_DDEWAIT | SEE_MASK_CLASSKEY;
277       exec_info.lpFile = wfilename;     
278       exec_info.nShow = SW_SHOWNORMAL;
279       exec_info.hkeyClass = class_key;
280       
281       if (!ShellExecuteExW(&exec_info))
282         {
283           DWORD last_error;
284           LPVOID message;
285           char *message_utf8;
286           
287           last_error = GetLastError ();
288           FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER | 
289                          FORMAT_MESSAGE_FROM_SYSTEM,
290                          NULL,
291                          last_error,
292                          MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
293                          (LPTSTR) &message,
294                          0, NULL );
295           
296           message_utf8 = g_utf16_to_utf8 (message, -1, NULL, NULL, NULL);
297           g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, _("Error launching application: %s"), message_utf8);
298           g_free (message_utf8);
299           LocalFree (message);
300           
301           g_free (wfilename);
302           RegCloseKey (class_key);
303           return FALSE;
304         }
305       
306       g_free (wfilename);
307     }
308   
309   RegCloseKey (class_key);
310   
311   return TRUE;
312 }
313
314 static gboolean
315 g_win32_app_info_supports_uris (GAppInfo *appinfo)
316 {
317   return FALSE;
318 }
319
320 static gboolean
321 g_win32_app_info_launch_uris (GAppInfo *appinfo,
322                               GList *uris,
323                               GAppLaunchContext *launch_context,
324                               GError **error)
325 {
326   g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, _("URIs not supported"));
327   return FALSE;
328 }
329
330 static gboolean
331 g_win32_app_info_should_show (GAppInfo *appinfo,
332                               const char *win32_env)
333 {
334   GWin32AppInfo *info = G_WIN32_APP_INFO (appinfo);
335
336   if (info->no_open_with)
337     return FALSE;
338   
339   return TRUE;
340 }
341
342 static gboolean
343 g_win32_app_info_set_as_default_for_type (GAppInfo    *appinfo,
344                                             const char  *content_type,
345                                             GError     **error)
346 {
347   g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, _("association changes not supported on win32"));
348   return FALSE;
349 }
350
351 GAppInfo *
352 g_app_info_create_from_commandline (const char *commandline,
353                                     const char *application_name,
354                                     GAppInfoCreateFlags flags,
355                                     GError **error)
356 {
357   g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, _("Association creation not supported on win32"));
358   return NULL;
359 }
360
361
362 static void
363 g_win32_app_info_iface_init (GAppInfoIface *iface)
364 {
365   iface->dup = g_win32_app_info_dup;
366   iface->equal = g_win32_app_info_equal;
367   iface->get_id = g_win32_app_info_get_id;
368   iface->get_name = g_win32_app_info_get_name;
369   iface->get_description = g_win32_app_info_get_description;
370   iface->get_executable = g_win32_app_info_get_executable;
371   iface->get_icon = g_win32_app_info_get_icon;
372   iface->launch = g_win32_app_info_launch;
373   iface->supports_uris = g_win32_app_info_supports_uris;
374   iface->launch_uris = g_win32_app_info_launch_uris;
375   iface->should_show = g_win32_app_info_should_show;
376   iface->set_as_default_for_type = g_win32_app_info_set_as_default_for_type;
377 }
378
379 static void
380 enumerate_open_with_list (HKEY dir_key,
381                           GList **prognames)
382 {
383   DWORD index;
384   wchar_t name[256];
385   DWORD name_len, nbytes;
386   wchar_t data[256];
387   wchar_t *data_alloc;
388   DWORD type;
389
390   /* Must also look inside for a,b,c, + MRUList */
391   index = 0;
392   name_len = 256;
393   nbytes = sizeof (data) - 2;
394   while (RegEnumValueW(dir_key,
395                        index,
396                        name,
397                        &name_len,
398                        0,
399                        &type,
400                        (LPBYTE)data,
401                        &nbytes) == ERROR_SUCCESS)
402     {
403       data[nbytes/2] = '\0';
404       if (type == REG_SZ &&
405           /* Ignore things like MRUList, just look at 'a', 'b', 'c', etc */
406           name_len == 1)
407         {
408           data_alloc = (wchar_t *)g_memdup (data, nbytes + 2);
409           data_alloc[nbytes/2] = 0;
410           *prognames = g_list_prepend (*prognames, data_alloc);
411         }
412       index++;
413       name_len = 256;
414       nbytes = sizeof (data) - 2;
415     }
416   
417   index = 0;
418   name_len = 256;
419   while (RegEnumKeyExW(dir_key,
420                        index,
421                        name,
422                        &name_len,
423                        NULL,
424                        NULL,
425                        NULL,
426                        NULL) == ERROR_SUCCESS)
427     {
428       *prognames = g_list_prepend (*prognames, g_memdup (name, (name_len + 1) * 2));
429       index++;
430       name_len = 256;
431     }
432 }
433
434 static void
435 enumerate_open_with_progids (HKEY dir_key,
436                              GList **progids)
437 {
438   DWORD index;
439   wchar_t name[256];
440   DWORD name_len, type;
441
442   index = 0;
443   name_len = 256;
444   while (RegEnumValueW(dir_key,
445                        index,
446                        name,
447                        &name_len,
448                        0,
449                        &type,
450                        NULL,
451                        0) == ERROR_SUCCESS)
452     {
453       *progids = g_list_prepend (*progids, g_memdup (name, (name_len + 1) * 2));
454       index++;
455       name_len = 256;
456     }
457 }
458
459 static void
460 enumerate_open_with_root (HKEY dir_key,
461                           GList **progids,
462                           GList **prognames)
463 {
464   HKEY reg_key = NULL;
465   
466   if (RegOpenKeyExW (dir_key, L"OpenWithList", 0,
467                      KEY_READ, &reg_key) == ERROR_SUCCESS)
468     {
469       enumerate_open_with_list (reg_key, prognames);
470       RegCloseKey (reg_key);
471     }
472   
473   if (RegOpenKeyExW (dir_key, L"OpenWithProgids", 0,
474                      KEY_QUERY_VALUE, &reg_key) == ERROR_SUCCESS)
475     {
476       enumerate_open_with_progids (reg_key, progids);
477       RegCloseKey (reg_key);
478     }
479 }
480
481 static gboolean
482 app_info_in_list (GAppInfo *info, GList *l)
483 {
484   while (l != NULL)
485     {
486       if (g_app_info_equal (info, l->data))
487         return TRUE;
488       l = l->next;
489     }
490   return FALSE;
491 }
492
493 /**
494  * g_app_info_get_all_for_type:
495  * @content_type:
496  * 
497  * Returns a #GList of #GAppInfo for a given @content_type.
498  * 
499  **/
500 GList *
501 g_app_info_get_all_for_type (const char *content_type)
502 {
503   GList *progids = NULL;
504   GList *prognames = NULL;
505   HKEY reg_key, sys_file_assoc_key, reg_key2;
506   wchar_t percieved_type[128];
507   DWORD nchars, key_type;
508   wchar_t *wc_key;
509   GList *l;
510   GList *infos;
511
512   wc_key = g_utf8_to_utf16 (content_type, -1, NULL, NULL, NULL);
513   if (RegOpenKeyExW (HKEY_CLASSES_ROOT, wc_key, 0,
514                      KEY_QUERY_VALUE, &reg_key) == ERROR_SUCCESS)
515     {
516       enumerate_open_with_root (reg_key, &progids, &prognames);
517
518       nchars = sizeof (percieved_type) / sizeof(wchar_t);
519       if (RegQueryValueExW (reg_key, L"PerceivedType", 0,
520                             &key_type, (LPBYTE) percieved_type, &nchars) == ERROR_SUCCESS)
521         {
522           if (key_type == REG_SZ &&
523               RegOpenKeyExW (HKEY_CLASSES_ROOT, L"SystemFileAssociations", 0,
524                              KEY_QUERY_VALUE, &sys_file_assoc_key) == ERROR_SUCCESS)
525             {
526               if (RegOpenKeyExW (sys_file_assoc_key, percieved_type, 0,
527                                  KEY_QUERY_VALUE, &reg_key2) == ERROR_SUCCESS)
528                 {
529                   enumerate_open_with_root (reg_key2, &progids, &prognames);
530                   RegCloseKey (reg_key2);
531                 }
532
533               RegCloseKey (sys_file_assoc_key);
534             }
535         }
536       RegCloseKey (reg_key);
537     }
538
539   if (RegOpenKeyExW (HKEY_CURRENT_USER, L"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts", 0,
540                      KEY_QUERY_VALUE, &reg_key) == ERROR_SUCCESS)
541     {
542       if (RegOpenKeyExW (reg_key, wc_key, 0,
543                          KEY_QUERY_VALUE, &reg_key2) == ERROR_SUCCESS)
544         {
545           enumerate_open_with_root (reg_key2, &progids, &prognames);
546           RegCloseKey (reg_key2);
547         }
548       
549       RegCloseKey (reg_key);
550     }
551
552   infos = NULL;
553   for (l = prognames; l != NULL; l = l->next)
554     {
555       GAppInfo *info;
556
557       /* l->data ownership is taken */
558       info = g_desktop_app_info_new_from_id ((wchar_t *)l->data, TRUE);
559       if (app_info_in_list (info, infos))
560         g_object_unref (info);
561       else
562         infos = g_list_prepend (infos, info);
563     }
564   g_list_free (prognames);
565
566   for (l = progids; l != NULL; l = l->next)
567     {
568       GAppInfo *info;
569
570       /* l->data ownership is taken */
571       info = g_desktop_app_info_new_from_id ((wchar_t *)l->data, FALSE);
572       if (app_info_in_list (info, infos))
573         g_object_unref (info);
574       else
575         infos = g_list_prepend (infos, info);
576     }
577   g_list_free (progids);
578   
579   g_free (wc_key);
580   return g_list_reverse (infos);
581 }
582
583 /**
584  * g_app_info_get_default_for_type:
585  * @content_type:
586  * @must_support_uris:
587  * 
588  * Returns the default #GAppInfo for the given @content_type. If 
589  * @must_support_uris is true, the #GAppInfo is expected to support
590  * URIs. 
591  * 
592  **/
593 GAppInfo *
594 g_app_info_get_default_for_type (const char *content_type,
595                                  gboolean must_support_uris)
596 {
597   wchar_t *wtype;
598   wchar_t buffer[1024];
599   DWORD buffer_size;
600
601   wtype = g_utf8_to_utf16 (content_type, -1, NULL, NULL, NULL);
602
603   /* Verify that we have some sort of app registered for this type */
604   buffer_size = 1024;
605   if (AssocQueryStringW(0,
606                         REAL_ASSOCSTR_COMMAND,
607                         wtype,
608                         NULL,
609                         buffer,
610                         &buffer_size) == S_OK)
611     /* Takes ownership of wtype */
612     return g_desktop_app_info_new_from_id (wtype, FALSE);
613
614   g_free (wtype);
615   return NULL;
616 }
617
618 /**
619  * g_app_info_get_default_for_uri_scheme:
620  * @uri_scheme:
621  * 
622  **/
623 GAppInfo *
624 g_app_info_get_default_for_uri_scheme (const char *uri_scheme)
625 {
626   /* TODO: Implement */
627   return NULL;
628 }
629
630 /**
631  * g_app_info_get_all:
632  * 
633  **/
634 GList *
635 g_app_info_get_all (void)
636 {
637   DWORD index;
638   wchar_t name[256];
639   DWORD name_len;
640   HKEY reg_key;
641   GList *infos;
642   GAppInfo *info;
643
644   if (RegOpenKeyExW (HKEY_CLASSES_ROOT, L"Applications", 0,
645                      KEY_READ, &reg_key) != ERROR_SUCCESS)
646     return NULL;
647
648   infos = NULL;
649   index = 0;
650   name_len = 256;
651   while (RegEnumKeyExW(reg_key,
652                        index,
653                        name,
654                        &name_len,
655                        NULL,
656                        NULL,
657                        NULL,
658                        NULL) == ERROR_SUCCESS)
659     {
660       wchar_t *name_dup = g_memdup (name, (name_len+1)*2);
661       /* name_dup ownership is taken */
662       info = g_desktop_app_info_new_from_id (name_dup, TRUE);
663       infos = g_list_prepend (infos, info);
664       
665       index++;
666       name_len = 256;
667     }
668   
669   RegCloseKey (reg_key);
670
671   return g_list_reverse (infos);
672 }