• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 /* ========================= Module _Folder ========================= */
3 
4 #include "Python.h"
5 
6 
7 
8 #include "pymactoolbox.h"
9 
10 /* Macro to test whether a weak-loaded CFM function exists */
11 #define PyMac_PRECHECK(rtn) do { if ( &rtn == NULL )  {\
12         PyErr_SetString(PyExc_NotImplementedError, \
13             "Not available in this shared library/OS version"); \
14         return NULL; \
15     }} while(0)
16 
17 
18 #include <Carbon/Carbon.h>
19 
20 
21 static PyObject *Folder_Error;
22 
Folder_FindFolder(PyObject * _self,PyObject * _args)23 static PyObject *Folder_FindFolder(PyObject *_self, PyObject *_args)
24 {
25     PyObject *_res = NULL;
26     OSErr _err;
27     short vRefNum;
28     OSType folderType;
29     Boolean createFolder;
30     FSVolumeRefNum  foundVRefNum;
31     SInt32 foundDirID;
32     if (!PyArg_ParseTuple(_args, "hO&b",
33                           &vRefNum,
34                           PyMac_GetOSType, &folderType,
35                           &createFolder))
36         return NULL;
37     _err = FindFolder(vRefNum,
38                       folderType,
39                       createFolder,
40                       &foundVRefNum,
41                       &foundDirID);
42     if (_err != noErr) return PyMac_Error(_err);
43     _res = Py_BuildValue("hl",
44                          foundVRefNum,
45                          foundDirID);
46     return _res;
47 }
48 
Folder_ReleaseFolder(PyObject * _self,PyObject * _args)49 static PyObject *Folder_ReleaseFolder(PyObject *_self, PyObject *_args)
50 {
51     PyObject *_res = NULL;
52     OSErr _err;
53     short vRefNum;
54     OSType folderType;
55     if (!PyArg_ParseTuple(_args, "hO&",
56                           &vRefNum,
57                           PyMac_GetOSType, &folderType))
58         return NULL;
59     _err = ReleaseFolder(vRefNum,
60                          folderType);
61     if (_err != noErr) return PyMac_Error(_err);
62     Py_INCREF(Py_None);
63     _res = Py_None;
64     return _res;
65 }
66 
Folder_FSFindFolder(PyObject * _self,PyObject * _args)67 static PyObject *Folder_FSFindFolder(PyObject *_self, PyObject *_args)
68 {
69     PyObject *_res = NULL;
70     OSErr _err;
71     short vRefNum;
72     OSType folderType;
73     Boolean createFolder;
74     FSRef foundRef;
75     if (!PyArg_ParseTuple(_args, "hO&b",
76                           &vRefNum,
77                           PyMac_GetOSType, &folderType,
78                           &createFolder))
79         return NULL;
80     _err = FSFindFolder(vRefNum,
81                         folderType,
82                         createFolder,
83                         &foundRef);
84     if (_err != noErr) return PyMac_Error(_err);
85     _res = Py_BuildValue("O&",
86                          PyMac_BuildFSRef, &foundRef);
87     return _res;
88 }
89 
Folder_AddFolderDescriptor(PyObject * _self,PyObject * _args)90 static PyObject *Folder_AddFolderDescriptor(PyObject *_self, PyObject *_args)
91 {
92     PyObject *_res = NULL;
93     OSErr _err;
94     FolderType foldType;
95     FolderDescFlags flags;
96     FolderClass foldClass;
97     FolderLocation foldLocation;
98     OSType badgeSignature;
99     OSType badgeType;
100     Str255 name;
101     Boolean replaceFlag;
102     if (!PyArg_ParseTuple(_args, "O&lO&O&O&O&O&b",
103                           PyMac_GetOSType, &foldType,
104                           &flags,
105                           PyMac_GetOSType, &foldClass,
106                           PyMac_GetOSType, &foldLocation,
107                           PyMac_GetOSType, &badgeSignature,
108                           PyMac_GetOSType, &badgeType,
109                           PyMac_GetStr255, name,
110                           &replaceFlag))
111         return NULL;
112     _err = AddFolderDescriptor(foldType,
113                                flags,
114                                foldClass,
115                                foldLocation,
116                                badgeSignature,
117                                badgeType,
118                                name,
119                                replaceFlag);
120     if (_err != noErr) return PyMac_Error(_err);
121     Py_INCREF(Py_None);
122     _res = Py_None;
123     return _res;
124 }
125 
Folder_GetFolderTypes(PyObject * _self,PyObject * _args)126 static PyObject *Folder_GetFolderTypes(PyObject *_self, PyObject *_args)
127 {
128     PyObject *_res = NULL;
129     OSErr _err;
130     UInt32 requestedTypeCount;
131     UInt32 totalTypeCount;
132     FolderType theTypes;
133     if (!PyArg_ParseTuple(_args, "l",
134                           &requestedTypeCount))
135         return NULL;
136     _err = GetFolderTypes(requestedTypeCount,
137                           &totalTypeCount,
138                           &theTypes);
139     if (_err != noErr) return PyMac_Error(_err);
140     _res = Py_BuildValue("lO&",
141                          totalTypeCount,
142                          PyMac_BuildOSType, theTypes);
143     return _res;
144 }
145 
Folder_RemoveFolderDescriptor(PyObject * _self,PyObject * _args)146 static PyObject *Folder_RemoveFolderDescriptor(PyObject *_self, PyObject *_args)
147 {
148     PyObject *_res = NULL;
149     OSErr _err;
150     FolderType foldType;
151     if (!PyArg_ParseTuple(_args, "O&",
152                           PyMac_GetOSType, &foldType))
153         return NULL;
154     _err = RemoveFolderDescriptor(foldType);
155     if (_err != noErr) return PyMac_Error(_err);
156     Py_INCREF(Py_None);
157     _res = Py_None;
158     return _res;
159 }
160 
161 #ifndef __LP64__
Folder_GetFolderName(PyObject * _self,PyObject * _args)162 static PyObject *Folder_GetFolderName(PyObject *_self, PyObject *_args)
163 {
164     PyObject *_res = NULL;
165     OSErr _err;
166     short vRefNum;
167     OSType foldType;
168     short foundVRefNum;
169     Str255 name;
170     if (!PyArg_ParseTuple(_args, "hO&O&",
171                           &vRefNum,
172                           PyMac_GetOSType, &foldType,
173                           PyMac_GetStr255, name))
174         return NULL;
175     _err = GetFolderName(vRefNum,
176                          foldType,
177                          &foundVRefNum,
178                          name);
179     if (_err != noErr) return PyMac_Error(_err);
180     _res = Py_BuildValue("h",
181                          foundVRefNum);
182     return _res;
183 }
184 
Folder_AddFolderRouting(PyObject * _self,PyObject * _args)185 static PyObject *Folder_AddFolderRouting(PyObject *_self, PyObject *_args)
186 {
187     PyObject *_res = NULL;
188     OSErr _err;
189     OSType fileType;
190     FolderType routeFromFolder;
191     FolderType routeToFolder;
192     RoutingFlags flags;
193     Boolean replaceFlag;
194     if (!PyArg_ParseTuple(_args, "O&O&O&lb",
195                           PyMac_GetOSType, &fileType,
196                           PyMac_GetOSType, &routeFromFolder,
197                           PyMac_GetOSType, &routeToFolder,
198                           &flags,
199                           &replaceFlag))
200         return NULL;
201     _err = AddFolderRouting(fileType,
202                             routeFromFolder,
203                             routeToFolder,
204                             flags,
205                             replaceFlag);
206     if (_err != noErr) return PyMac_Error(_err);
207     Py_INCREF(Py_None);
208     _res = Py_None;
209     return _res;
210 }
211 
Folder_RemoveFolderRouting(PyObject * _self,PyObject * _args)212 static PyObject *Folder_RemoveFolderRouting(PyObject *_self, PyObject *_args)
213 {
214     PyObject *_res = NULL;
215     OSErr _err;
216     OSType fileType;
217     FolderType routeFromFolder;
218     if (!PyArg_ParseTuple(_args, "O&O&",
219                           PyMac_GetOSType, &fileType,
220                           PyMac_GetOSType, &routeFromFolder))
221         return NULL;
222     _err = RemoveFolderRouting(fileType,
223                                routeFromFolder);
224     if (_err != noErr) return PyMac_Error(_err);
225     Py_INCREF(Py_None);
226     _res = Py_None;
227     return _res;
228 }
229 
Folder_FindFolderRouting(PyObject * _self,PyObject * _args)230 static PyObject *Folder_FindFolderRouting(PyObject *_self, PyObject *_args)
231 {
232     PyObject *_res = NULL;
233     OSErr _err;
234     OSType fileType;
235     FolderType routeFromFolder;
236     FolderType routeToFolder;
237     RoutingFlags flags;
238     if (!PyArg_ParseTuple(_args, "O&O&",
239                           PyMac_GetOSType, &fileType,
240                           PyMac_GetOSType, &routeFromFolder))
241         return NULL;
242     _err = FindFolderRouting(fileType,
243                              routeFromFolder,
244                              &routeToFolder,
245                              &flags);
246     if (_err != noErr) return PyMac_Error(_err);
247     _res = Py_BuildValue("O&l",
248                          PyMac_BuildOSType, routeToFolder,
249                          flags);
250     return _res;
251 }
252 #endif /* !__LP64__ */
253 
Folder_InvalidateFolderDescriptorCache(PyObject * _self,PyObject * _args)254 static PyObject *Folder_InvalidateFolderDescriptorCache(PyObject *_self, PyObject *_args)
255 {
256     PyObject *_res = NULL;
257     OSErr _err;
258     short vRefNum;
259     long dirID;
260     if (!PyArg_ParseTuple(_args, "hl",
261                           &vRefNum,
262                           &dirID))
263         return NULL;
264     _err = InvalidateFolderDescriptorCache(vRefNum,
265                                            dirID);
266     if (_err != noErr) return PyMac_Error(_err);
267     Py_INCREF(Py_None);
268     _res = Py_None;
269     return _res;
270 }
271 
Folder_IdentifyFolder(PyObject * _self,PyObject * _args)272 static PyObject *Folder_IdentifyFolder(PyObject *_self, PyObject *_args)
273 {
274     PyObject *_res = NULL;
275     OSErr _err;
276     short vRefNum;
277     long dirID;
278     FolderType foldType;
279     if (!PyArg_ParseTuple(_args, "hl",
280                           &vRefNum,
281                           &dirID))
282         return NULL;
283     _err = IdentifyFolder(vRefNum,
284                           dirID,
285                           &foldType);
286     if (_err != noErr) return PyMac_Error(_err);
287     _res = Py_BuildValue("O&",
288                          PyMac_BuildOSType, foldType);
289     return _res;
290 }
291 
292 static PyMethodDef Folder_methods[] = {
293     {"FindFolder", (PyCFunction)Folder_FindFolder, 1,
294      PyDoc_STR("(short vRefNum, OSType folderType, Boolean createFolder) -> (short foundVRefNum, long foundDirID)")},
295     {"ReleaseFolder", (PyCFunction)Folder_ReleaseFolder, 1,
296      PyDoc_STR("(short vRefNum, OSType folderType) -> None")},
297     {"FSFindFolder", (PyCFunction)Folder_FSFindFolder, 1,
298      PyDoc_STR("(short vRefNum, OSType folderType, Boolean createFolder) -> (FSRef foundRef)")},
299     {"AddFolderDescriptor", (PyCFunction)Folder_AddFolderDescriptor, 1,
300      PyDoc_STR("(FolderType foldType, FolderDescFlags flags, FolderClass foldClass, FolderLocation foldLocation, OSType badgeSignature, OSType badgeType, Str255 name, Boolean replaceFlag) -> None")},
301     {"GetFolderTypes", (PyCFunction)Folder_GetFolderTypes, 1,
302      PyDoc_STR("(UInt32 requestedTypeCount) -> (UInt32 totalTypeCount, FolderType theTypes)")},
303     {"RemoveFolderDescriptor", (PyCFunction)Folder_RemoveFolderDescriptor, 1,
304      PyDoc_STR("(FolderType foldType) -> None")},
305 #ifndef __LP64__
306     {"GetFolderName", (PyCFunction)Folder_GetFolderName, 1,
307      PyDoc_STR("(short vRefNum, OSType foldType, Str255 name) -> (short foundVRefNum)")},
308     {"AddFolderRouting", (PyCFunction)Folder_AddFolderRouting, 1,
309      PyDoc_STR("(OSType fileType, FolderType routeFromFolder, FolderType routeToFolder, RoutingFlags flags, Boolean replaceFlag) -> None")},
310     {"RemoveFolderRouting", (PyCFunction)Folder_RemoveFolderRouting, 1,
311      PyDoc_STR("(OSType fileType, FolderType routeFromFolder) -> None")},
312     {"FindFolderRouting", (PyCFunction)Folder_FindFolderRouting, 1,
313      PyDoc_STR("(OSType fileType, FolderType routeFromFolder) -> (FolderType routeToFolder, RoutingFlags flags)")},
314 #endif /* !__LP64__ */
315     {"InvalidateFolderDescriptorCache", (PyCFunction)Folder_InvalidateFolderDescriptorCache, 1,
316      PyDoc_STR("(short vRefNum, long dirID) -> None")},
317     {"IdentifyFolder", (PyCFunction)Folder_IdentifyFolder, 1,
318      PyDoc_STR("(short vRefNum, long dirID) -> (FolderType foldType)")},
319     {NULL, NULL, 0}
320 };
321 
322 
323 
324 
init_Folder(void)325 void init_Folder(void)
326 {
327     PyObject *m;
328     PyObject *d;
329 
330 
331 
332 
333     m = Py_InitModule("_Folder", Folder_methods);
334     d = PyModule_GetDict(m);
335     Folder_Error = PyMac_GetOSErrException();
336     if (Folder_Error == NULL ||
337         PyDict_SetItemString(d, "Error", Folder_Error) != 0)
338         return;
339 }
340 
341 /* ======================= End module _Folder ======================= */
342 
343