Blender  V2.93
GHOST_SystemPathsWin32.cpp
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2011 Blender Foundation.
17  * All rights reserved.
18  */
19 
24 #include "GHOST_SystemPathsWin32.h"
25 #include "GHOST_Debug.h"
26 
27 #ifndef _WIN32_IE
28 # define _WIN32_IE 0x0501
29 #endif
30 #include "utfconv.h"
31 #include <shlobj.h>
32 
34 {
35 }
36 
38 {
39 }
40 
41 const GHOST_TUns8 *GHOST_SystemPathsWin32::getSystemDir(int, const char *versionstr) const
42 {
43  /* 1 utf-16 might translate into 3 utf-8. 2 utf-16 translates into 4 utf-8. */
44  static char knownpath[MAX_PATH * 3 + 128] = {0};
45  PWSTR knownpath_16 = NULL;
46 
47  HRESULT hResult = SHGetKnownFolderPath(
48  FOLDERID_ProgramData, KF_FLAG_DEFAULT, NULL, &knownpath_16);
49 
50  if (hResult == S_OK) {
51  conv_utf_16_to_8(knownpath_16, knownpath, MAX_PATH * 3);
52  CoTaskMemFree(knownpath_16);
53  strcat(knownpath, "\\Blender Foundation\\Blender\\");
54  strcat(knownpath, versionstr);
55  return (GHOST_TUns8 *)knownpath;
56  }
57 
58  return NULL;
59 }
60 
61 const GHOST_TUns8 *GHOST_SystemPathsWin32::getUserDir(int, const char *versionstr) const
62 {
63  static char knownpath[MAX_PATH * 3 + 128] = {0};
64  PWSTR knownpath_16 = NULL;
65 
66  HRESULT hResult = SHGetKnownFolderPath(
67  FOLDERID_RoamingAppData, KF_FLAG_DEFAULT, NULL, &knownpath_16);
68 
69  if (hResult == S_OK) {
70  conv_utf_16_to_8(knownpath_16, knownpath, MAX_PATH * 3);
71  CoTaskMemFree(knownpath_16);
72  strcat(knownpath, "\\Blender Foundation\\Blender\\");
73  strcat(knownpath, versionstr);
74  return (GHOST_TUns8 *)knownpath;
75  }
76 
77  return NULL;
78 }
79 
81 {
82  GUID folderid;
83 
84  switch (type) {
86  folderid = FOLDERID_Desktop;
87  break;
89  folderid = FOLDERID_Documents;
90  break;
92  folderid = FOLDERID_Downloads;
93  break;
95  folderid = FOLDERID_Music;
96  break;
98  folderid = FOLDERID_Pictures;
99  break;
101  folderid = FOLDERID_Videos;
102  break;
103  default:
104  GHOST_ASSERT(
105  false,
106  "GHOST_SystemPathsWin32::getUserSpecialDir(): Invalid enum value for type parameter");
107  return NULL;
108  }
109 
110  static char knownpath[MAX_PATH * 3] = {0};
111  PWSTR knownpath_16 = NULL;
112  HRESULT hResult = SHGetKnownFolderPath(folderid, KF_FLAG_DEFAULT, NULL, &knownpath_16);
113 
114  if (hResult == S_OK) {
115  conv_utf_16_to_8(knownpath_16, knownpath, MAX_PATH * 3);
116  CoTaskMemFree(knownpath_16);
117  return (GHOST_TUns8 *)knownpath;
118  }
119 
120  CoTaskMemFree(knownpath_16);
121  return NULL;
122 }
123 
125 {
126  static char fullname[MAX_PATH * 3] = {0};
127  wchar_t fullname_16[MAX_PATH * 3];
128 
129  if (GetModuleFileNameW(0, fullname_16, MAX_PATH)) {
130  conv_utf_16_to_8(fullname_16, fullname, MAX_PATH * 3);
131  return (GHOST_TUns8 *)fullname;
132  }
133 
134  return NULL;
135 }
136 
137 void GHOST_SystemPathsWin32::addToSystemRecentFiles(const char *filename) const
138 {
139  /* SHARD_PATH resolves to SHARD_PATHA for non-UNICODE build */
140  UTF16_ENCODE(filename);
141  SHAddToRecentDocs(SHARD_PATHW, filename_16);
142  UTF16_UN_ENCODE(filename);
143 }
#define GHOST_ASSERT(x, info)
Definition: GHOST_Debug.h:79
GHOST_TUserSpecialDirTypes
Definition: GHOST_Types.h:568
@ GHOST_kUserSpecialDirDesktop
Definition: GHOST_Types.h:569
@ GHOST_kUserSpecialDirMusic
Definition: GHOST_Types.h:572
@ GHOST_kUserSpecialDirPictures
Definition: GHOST_Types.h:573
@ GHOST_kUserSpecialDirVideos
Definition: GHOST_Types.h:574
@ GHOST_kUserSpecialDirDownloads
Definition: GHOST_Types.h:571
@ GHOST_kUserSpecialDirDocuments
Definition: GHOST_Types.h:570
unsigned char GHOST_TUns8
Definition: GHOST_Types.h:60
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum type
const GHOST_TUns8 * getSystemDir(int version, const char *versionstr) const
void addToSystemRecentFiles(const char *filename) const
const GHOST_TUns8 * getUserSpecialDir(GHOST_TUserSpecialDirTypes type) const
const GHOST_TUns8 * getBinaryDir() const
const GHOST_TUns8 * getUserDir(int version, const char *versionstr) const
int conv_utf_16_to_8(const wchar_t *in16, char *out8, size_t size8)
Definition: utfconv.c:127
#define UTF16_ENCODE(in8str)
Definition: utfconv.h:96
#define UTF16_UN_ENCODE(in8str)
Definition: utfconv.h:100