Clean up some docs
[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 GList *
494 g_app_info_get_all_for_type (const char *content_type)
495 {
496   GList *progids = NULL;
497   GList *prognames = NULL;
498   HKEY reg_key, sys_file_assoc_key, reg_key2;
499   wchar_t percieved_type[128];
500   DWORD nchars, key_type;
501   wchar_t *wc_key;
502   GList *l;
503   GList *infos;
504
505   wc_key = g_utf8_to_utf16 (content_type, -1, NULL, NULL, NULL);
506   if (RegOpenKeyExW (HKEY_CLASSES_ROOT, wc_key, 0,
507                      KEY_QUERY_VALUE, &reg_key) == ERROR_SUCCESS)
508     {
509       enumerate_open_with_root (reg_key, &progids, &prognames);
510
511       nchars = sizeof (percieved_type) / sizeof(wchar_t);
512       if (RegQueryValueExW (reg_key, L"PerceivedType", 0,
513                             &key_type, (LPBYTE) percieved_type, &nchars) == ERROR_SUCCESS)
514         {
515           if (key_type == REG_SZ &&
516               RegOpenKeyExW (HKEY_CLASSES_ROOT, L"SystemFileAssociations", 0,
517                              KEY_QUERY_VALUE, &sys_file_assoc_key) == ERROR_SUCCESS)
518             {
519               if (RegOpenKeyExW (sys_file_assoc_key, percieved_type, 0,
520                                  KEY_QUERY_VALUE, &reg_key2) == ERROR_SUCCESS)
521                 {
522                   enumerate_open_with_root (reg_key2, &progids, &prognames);
523                   RegCloseKey (reg_key2);
524                 }
525
526               RegCloseKey (sys_file_assoc_key);
527             }
528         }
529       RegCloseKey (reg_key);
530     }
531
532   if (RegOpenKeyExW (HKEY_CURRENT_USER, L"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts", 0,
533                      KEY_QUERY_VALUE, &reg_key) == ERROR_SUCCESS)
534     {
535       if (RegOpenKeyExW (reg_key, wc_key, 0,
536                          KEY_QUERY_VALUE, &reg_key2) == ERROR_SUCCESS)
537         {
538           enumerate_open_with_root (reg_key2, &progids, &prognames);
539           RegCloseKey (reg_key2);
540         }
541       
542       RegCloseKey (reg_key);
543     }
544
545   infos = NULL;
546   for (l = prognames; l != NULL; l = l->next)
547     {
548       GAppInfo *info;
549
550       /* l->data ownership is taken */
551       info = g_desktop_app_info_new_from_id ((wchar_t *)l->data, TRUE);
552       if (app_info_in_list (info, infos))
553         g_object_unref (info);
554       else
555         infos = g_list_prepend (infos, info);
556     }
557   g_list_free (prognames);
558
559   for (l = progids; l != NULL; l = l->next)
560     {
561       GAppInfo *info;
562
563       /* l->data ownership is taken */
564       info = g_desktop_app_info_new_from_id ((wchar_t *)l->data, FALSE);
565       if (app_info_in_list (info, infos))
566         g_object_unref (info);
567       else
568         infos = g_list_prepend (infos, info);
569     }
570   g_list_free (progids);
571   
572   g_free (wc_key);
573   return g_list_reverse (infos);
574 }
575
576 GAppInfo *
577 g_app_info_get_default_for_type (const char *content_type,
578                                  gboolean must_support_uris)
579 {
580   wchar_t *wtype;
581   wchar_t buffer[1024];
582   DWORD buffer_size;
583
584   wtype = g_utf8_to_utf16 (content_type, -1, NULL, NULL, NULL);
585
586   /* Verify that we have some sort of app registered for this type */
587   buffer_size = 1024;
588   if (AssocQueryStringW(0,
589                         REAL_ASSOCSTR_COMMAND,
590                         wtype,
591                         NULL,
592                         buffer,
593                         &buffer_size) == S_OK)
594     /* Takes ownership of wtype */
595     return g_desktop_app_info_new_from_id (wtype, FALSE);
596
597   g_free (wtype);
598   return NULL;
599 }
600
601 GAppInfo *
602 g_app_info_get_default_for_uri_scheme (const char *uri_scheme)
603 {
604   /* TODO: Implement */
605   return NULL;
606 }
607
608 GList *
609 g_app_info_get_all (void)
610 {
611   DWORD index;
612   wchar_t name[256];
613   DWORD name_len;
614   HKEY reg_key;
615   GList *infos;
616   GAppInfo *info;
617
618   if (RegOpenKeyExW (HKEY_CLASSES_ROOT, L"Applications", 0,
619                      KEY_READ, &reg_key) != ERROR_SUCCESS)
620     return NULL;
621
622   infos = NULL;
623   index = 0;
624   name_len = 256;
625   while (RegEnumKeyExW(reg_key,
626                        index,
627                        name,
628                        &name_len,
629                        NULL,
630                        NULL,
631                        NULL,
632                        NULL) == ERROR_SUCCESS)
633     {
634       wchar_t *name_dup = g_memdup (name, (name_len+1)*2);
635       /* name_dup ownership is taken */
636       info = g_desktop_app_info_new_from_id (name_dup, TRUE);
637       infos = g_list_prepend (infos, info);
638       
639       index++;
640       name_len = 256;
641     }
642   
643   RegCloseKey (reg_key);
644
645   return g_list_reverse (infos);
646 }