Removed W->A from DEFWND_ImmIsUIMessageW.
[wine] / dlls / mscms / profile.c
1 /*
2  * MSCMS - Color Management System for Wine
3  *
4  * Copyright 2004 Hans Leidekker
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22 #include "wine/debug.h"
23
24 #include <stdarg.h>
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winnls.h"
29 #include "wingdi.h"
30 #include "winuser.h"
31 #include "icm.h"
32
33 #define LCMS_API_FUNCTION(f) extern typeof(f) * p##f;
34 #include "lcms_api.h"
35 #undef LCMS_API_FUNCTION
36
37 #define IS_SEPARATOR(ch)  ((ch) == '\\' || (ch) == '/')
38
39 static void MSCMS_basename( LPCWSTR path, LPWSTR name )
40 {
41     INT i = lstrlenW( path );
42
43     while (i > 0 && !IS_SEPARATOR(path[i - 1])) i--;
44     lstrcpyW( name, &path[i] );
45 }
46
47 WINE_DEFAULT_DEBUG_CHANNEL(mscms);
48
49 /******************************************************************************
50  * GetColorDirectoryA               [MSCMS.@]
51  *
52  * See GetColorDirectoryW.
53  */
54 BOOL WINAPI GetColorDirectoryA( PCSTR machine, PSTR buffer, PDWORD size )
55 {
56     INT len;
57     LPWSTR bufferW;
58     BOOL ret = FALSE;
59     DWORD sizeW = *size * sizeof(WCHAR);
60
61     TRACE( "( %p, %ld )\n", buffer, *size );
62
63     if (machine || !buffer) return FALSE;
64
65     bufferW = HeapAlloc( GetProcessHeap(), 0, sizeW );
66
67     if (bufferW)
68     {
69         ret = GetColorDirectoryW( NULL, bufferW, &sizeW );
70         *size = sizeW / sizeof(WCHAR);
71
72         if (ret)
73         {
74             len = WideCharToMultiByte( CP_ACP, 0, bufferW, *size, buffer, *size, NULL, NULL );
75             if (!len) ret = FALSE;
76         }
77
78         HeapFree( GetProcessHeap(), 0, bufferW );
79     }
80     return ret;
81 }
82
83 /******************************************************************************
84  * GetColorDirectoryW               [MSCMS.@]
85  *
86  * Get the directory where color profiles are stored.
87  *
88  * PARAMS
89  *  machine  [I]   Name of the machine for which to get the color directory.
90  *                 Must be NULL, which indicates the local machine.
91  *  buffer   [I]   Buffer to recieve the path name in.
92  *  size     [I/O] Size of the buffer in bytes. On return the variable holds
93  *                 the number of bytes actually needed.
94  */
95 BOOL WINAPI GetColorDirectoryW( PCWSTR machine, PWSTR buffer, PDWORD size )
96 {
97     /* FIXME: Get this directory from the registry? */
98     static const WCHAR colordir[] =
99     { 'c',':','\\','w','i','n','d','o','w','s','\\', 's','y','s','t','e','m','3','2',
100       '\\','s','p','o','o','l','\\','d','r','i','v','e','r','s','\\','c','o','l','o','r',0 };
101
102     DWORD len;
103
104     TRACE( "( %p, %ld )\n", buffer, *size );
105
106     if (machine || !buffer) return FALSE;
107
108     len = lstrlenW( colordir ) * sizeof(WCHAR);
109
110     if (len <= *size)
111     {
112         lstrcpyW( buffer, colordir );
113         return TRUE;
114     }
115
116     *size = len;
117     return FALSE;
118 }
119
120 /******************************************************************************
121  * InstallColorProfileA               [MSCMS.@]
122  *
123  * See InstallColorProfileW.
124  */
125 BOOL WINAPI InstallColorProfileA( PCSTR machine, PCSTR profile )
126 {
127     UINT len;
128     LPWSTR profileW;
129     BOOL ret = FALSE;
130
131     TRACE( "( %s )\n", debugstr_a(profile) );
132
133     if (machine || !profile) return FALSE;
134
135     len = MultiByteToWideChar( CP_ACP, 0, profile, -1, NULL, 0 );
136     profileW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
137
138     if (profileW)
139     {
140         MultiByteToWideChar( CP_ACP, 0, profile, -1, profileW, len );
141
142         ret = InstallColorProfileW( NULL, profileW );
143         HeapFree( GetProcessHeap(), 0, profileW );
144     }
145     return ret;
146 }
147
148 /******************************************************************************
149  * InstallColorProfileW               [MSCMS.@]
150  *
151  * Install a color profile.
152  *
153  * PARAMS
154  *  machine  [I] Name of the machine to install the profile on. Must be NULL,
155  *               which indicates the local machine.
156  *  profile  [I] Full path name of the profile to install.
157  *
158  * RETURNS
159  *  Success: TRUE
160  *  Failure: FALSE
161  */
162 BOOL WINAPI InstallColorProfileW( PCWSTR machine, PCWSTR profile )
163 {
164     WCHAR dest[MAX_PATH], base[MAX_PATH];
165     DWORD size = sizeof(dest);
166     static const WCHAR slash[] = { '\\', 0 };
167
168     TRACE( "( %s )\n", debugstr_w(profile) );
169
170     if (machine || !profile) return FALSE;
171
172     if (!GetColorDirectoryW( machine, dest, &size )) return FALSE;
173
174     MSCMS_basename( profile, base );
175
176     lstrcatW( dest, slash );
177     lstrcatW( dest, base );
178
179     /* Is source equal to destination? */
180     if (!lstrcmpW( profile, dest )) return TRUE;
181
182     return CopyFileW( profile, dest, TRUE );
183 }
184
185 /******************************************************************************
186  * IsColorProfileValid               [MSCMS.@]
187  *
188  * Determine if a given color profile is valid.
189  *
190  * PARAMS
191  *  profile  [I] Color profile handle.
192  *  valid    [O] Pointer to a BOOL variable. Set to TRUE if profile is valid,
193  *               FALSE otherwise.
194  *
195  * RETURNS
196  *  Success: TRUE
197  *  Failure: FALSE 
198  */
199 BOOL WINAPI IsColorProfileValid( HPROFILE profile, PBOOL valid )
200 {
201     FIXME( "( %p, %p ) stub\n", profile, valid );
202
203     return *valid = TRUE; 
204 }
205
206 /******************************************************************************
207  * UninstallColorProfileA               [MSCMS.@]
208  *
209  * See UninstallColorProfileW.
210  */
211 BOOL WINAPI UninstallColorProfileA( PCSTR machine, PCSTR profile, BOOL delete )
212 {
213     UINT len;
214     LPWSTR profileW;
215     BOOL ret = FALSE;
216
217     TRACE( "( %s, %x )\n", debugstr_a(profile), delete );
218
219     if (machine || !profile) return FALSE;
220
221     len = MultiByteToWideChar( CP_ACP, 0, profile, -1, NULL, 0 );
222     profileW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
223
224     if (profileW)
225     {
226         MultiByteToWideChar( CP_ACP, 0, profile, -1, profileW, len );
227
228         ret = UninstallColorProfileW( NULL, profileW , delete );
229
230         HeapFree( GetProcessHeap(), 0, profileW );
231     }
232     return ret;
233 }
234
235 /******************************************************************************
236  * UninstallColorProfileW               [MSCMS.@]
237  *
238  * Uninstall a color profile.
239  *
240  * PARAMS
241  *  machine  [I] Name of the machine to uninstall the profile on. Must be NULL,
242  *               which indicates the local machine.
243  *  profile  [I] Full path name of the profile to uninstall.
244  *  delete   [I] Bool that specifies whether the profile file should be deleted.
245  *
246  * RETURNS
247  *  Success: TRUE
248  *  Failure: FALSE
249  */
250 BOOL WINAPI UninstallColorProfileW( PCWSTR machine, PCWSTR profile, BOOL delete )
251 {
252     TRACE( "( %s, %x )\n", debugstr_w(profile), delete );
253
254     if (machine || !profile) return FALSE;
255
256     if (delete)
257         return DeleteFileW( profile );
258
259     return TRUE;
260 }
261
262 /******************************************************************************
263  * OpenColorProfileA               [MSCMS.@]
264  *
265  * See OpenColorProfileW.
266  */
267 HPROFILE WINAPI OpenColorProfileA( PPROFILE profile, DWORD access, DWORD sharing, DWORD creation )
268 {
269     HPROFILE handle = NULL;
270
271     TRACE( "( %p, %lx, %lx, %lx )\n", profile, access, sharing, creation );
272
273     if (!profile || !profile->pProfileData) return NULL;
274
275     /* No AW conversion needed for memory based profiles */
276     if (profile->dwType & PROFILE_MEMBUFFER)
277         return OpenColorProfileW( profile, access, sharing, creation );
278
279     if (profile->dwType & PROFILE_FILENAME)
280     {
281         UINT len;
282         PROFILE profileW;
283
284         profileW.dwType = profile->dwType;
285  
286         len = MultiByteToWideChar( CP_ACP, 0, profile->pProfileData, -1, NULL, 0 );
287         profileW.pProfileData = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
288
289         if (profileW.pProfileData)
290         {
291             profileW.cbDataSize = len * sizeof(WCHAR);
292             MultiByteToWideChar( CP_ACP, 0, profile->pProfileData, -1, profileW.pProfileData, len );
293
294             handle = OpenColorProfileW( &profileW, access, sharing, creation );
295             HeapFree( GetProcessHeap(), 0, profileW.pProfileData );
296         }
297     }
298     return handle;
299 }
300
301 /******************************************************************************
302  * OpenColorProfileW               [MSCMS.@]
303  *
304  * Open a color profile.
305  *
306  * PARAMS
307  *  profile   [I] Pointer to a color profile structure.
308  *  access    [I] Desired access.
309  *  sharing   [I] Sharing mode.
310  *  creation  [I] Creation mode.
311  *
312  * RETURNS
313  *  Success: Handle to the opened profile.
314  *  Failure: NULL
315  *
316  * NOTES
317  *  Values for access:   PROFILE_READ or PROFILE_READWRITE.
318  *  Values for sharing:  0 (no sharing), FILE_SHARE_READ and/or FILE_SHARE_WRITE.
319  *  Values for creation: one of CREATE_NEW, CREATE_ALWAYS, OPEN_EXISTING,
320  *                       OPEN_ALWAYS, TRUNCATE_EXISTING.
321  */
322 HPROFILE WINAPI OpenColorProfileW( PPROFILE profile, DWORD access, DWORD sharing, DWORD creation )
323 {
324 #ifdef HAVE_LCMS_H
325     cmsHPROFILE cmsprofile = NULL;
326     HANDLE handle = NULL;
327
328     TRACE( "( %p, %lx, %lx, %lx )\n", profile, access, sharing, creation );
329
330     if (!profile || !profile->pProfileData) return NULL;
331
332     if (profile->dwType & PROFILE_MEMBUFFER)
333     {
334         FIXME( "Memory based profile not yet supported.\n" ); return NULL;
335     }
336
337     if (profile->dwType & PROFILE_FILENAME)
338     {
339         char *unixname;
340         DWORD flags = 0;
341
342         TRACE("profile file: %s\n", debugstr_w( (WCHAR *)profile->pProfileData ));
343
344         if (access & PROFILE_READ) flags = GENERIC_READ;
345         if (access & PROFILE_READWRITE) flags = GENERIC_READ|GENERIC_WRITE;
346
347         if (!flags) return NULL;
348
349         handle = CreateFileW( profile->pProfileData, flags, sharing, NULL, creation, 0, NULL );
350         if (handle == INVALID_HANDLE_VALUE) return NULL;
351
352         unixname = wine_get_unix_file_name( (WCHAR *)profile->pProfileData );
353
354         if (unixname)
355         {
356             cmsprofile = cmsOpenProfileFromFile( unixname, flags & GENERIC_READ ? "r" : "w" );
357             HeapFree( GetProcessHeap(), 0, unixname );
358         }
359     }
360
361     if (cmsprofile) return MSCMS_create_hprofile_handle( handle, cmsprofile );
362
363 #endif /* HAVE_LCMS_H */
364     return NULL;
365 }
366
367 /******************************************************************************
368  * CloseColorProfile               [MSCMS.@]
369  *
370  * Close a color profile.
371  *
372  * PARAMS
373  *  profile  [I] Handle to the profile.
374  *
375  * RETURNS
376  *  Success: TRUE
377  *  Failure: FALSE
378  */
379 BOOL WINAPI CloseColorProfile( HPROFILE profile )
380 {
381     BOOL ret1, ret2 = FALSE;
382
383     TRACE( "( %p )\n", profile );
384
385 #ifdef HAVE_LCMS_H
386     ret1 = cmsCloseProfile( MSCMS_hprofile2cmsprofile( profile ) );
387     ret2 = CloseHandle( MSCMS_hprofile2handle( profile ) );
388
389     MSCMS_destroy_hprofile_handle( profile );
390
391 #endif /* HAVE_LCMS_H */
392     return ret1 && ret2;
393 }