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