4 * ***** BEGIN GPL LICENSE BLOCK *****
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program 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
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21 * All rights reserved.
23 * The Original Code is: all of this file.
25 * Contributor(s): Andrea Weikert (c) 2008 Blender Foundation.
27 * ***** END GPL LICENSE BLOCK *****
35 #include "MEM_guardedalloc.h"
37 #include "DNA_space_types.h" /* FILE_MAX */
39 #include "BLI_blenlib.h"
40 #include "BLI_linklist.h"
41 #include "BLI_dynstr.h"
42 #include "BLI_string.h"
45 #include <windows.h> /* need to include windows.h so _WIN32_IE is defined */
47 #define _WIN32_IE 0x0400 /* minimal requirements for SHGetSpecialFolderPath on MINGW MSVC has this defined already */
49 #include <shlobj.h> /* for SHGetSpecialFolderPath, has to be done before BLI_winstuff because 'near' is disabled through BLI_windstuff */
50 #include "BLI_winstuff.h"
54 /* XXX BIG WARNING: carbon.h can not be included in blender code, it conflicts with struct ID */
56 #include <CoreServices/CoreServices.h>
64 #include "fsmenu.h" /* include ourselves */
69 /* FSMenuEntry's without paths indicate seperators */
70 typedef struct _FSMenuEntry FSMenuEntry;
80 FSMenuEntry *fsmenu_system;
81 FSMenuEntry *fsmenu_bookmarks;
82 FSMenuEntry *fsmenu_recent;
86 static FSMenu *g_fsmenu = NULL;
88 struct FSMenu* fsmenu_get(void)
91 g_fsmenu=MEM_callocN(sizeof(struct FSMenu), "fsmenu");
96 static FSMenuEntry *fsmenu_get_category(struct FSMenu* fsmenu, FSMenuCategory category)
98 FSMenuEntry *fsms = NULL;
101 case FS_CATEGORY_SYSTEM:
102 fsms = fsmenu->fsmenu_system;
104 case FS_CATEGORY_BOOKMARKS:
105 fsms = fsmenu->fsmenu_bookmarks;
107 case FS_CATEGORY_RECENT:
108 fsms = fsmenu->fsmenu_recent;
114 static void fsmenu_set_category(struct FSMenu* fsmenu, FSMenuCategory category, FSMenuEntry *fsms)
117 case FS_CATEGORY_SYSTEM:
118 fsmenu->fsmenu_system = fsms;
120 case FS_CATEGORY_BOOKMARKS:
121 fsmenu->fsmenu_bookmarks = fsms;
123 case FS_CATEGORY_RECENT:
124 fsmenu->fsmenu_recent = fsms;
129 int fsmenu_get_nentries(struct FSMenu* fsmenu, FSMenuCategory category)
134 for (fsme= fsmenu_get_category(fsmenu, category); fsme; fsme= fsme->next)
140 char *fsmenu_get_entry(struct FSMenu* fsmenu, FSMenuCategory category, int idx)
144 for (fsme= fsmenu_get_category(fsmenu, category); fsme && idx; fsme= fsme->next)
147 return fsme?fsme->path:NULL;
150 short fsmenu_can_save (struct FSMenu* fsmenu, FSMenuCategory category, int idx)
154 for (fsme= fsmenu_get_category(fsmenu, category); fsme && idx; fsme= fsme->next)
157 return fsme?fsme->save:0;
160 void fsmenu_insert_entry(struct FSMenu* fsmenu, FSMenuCategory category, char *path, int sorted, short save)
166 fsms = fsmenu_get_category(fsmenu, category);
169 for (; fsme; prev= fsme, fsme= fsme->next) {
171 if (BLI_streq(path, fsme->path)) {
173 } else if (sorted && strcmp(path, fsme->path)<0) {
177 // if we're bookmarking this, file should come
178 // before the last separator, only automatically added
179 // current dir go after the last sep.
186 fsme= MEM_mallocN(sizeof(*fsme), "fsme");
187 fsme->path= BLI_strdup(path);
191 fsme->next= prev->next;
195 fsmenu_set_category(fsmenu, category, fsme);
199 void fsmenu_remove_entry(struct FSMenu* fsmenu, FSMenuCategory category, int idx)
201 FSMenuEntry *prev= NULL, *fsme= NULL;
202 FSMenuEntry *fsms = fsmenu_get_category(fsmenu, category);
204 for (fsme= fsms; fsme && idx; prev= fsme, fsme= fsme->next)
208 /* you should only be able to remove entries that were
209 not added by default, like windows drives.
210 also separators (where path == NULL) shouldn't be removed */
211 if (fsme->save && fsme->path) {
213 /* remove fsme from list */
215 prev->next= fsme->next;
218 fsmenu_set_category(fsmenu, category, fsms);
221 MEM_freeN(fsme->path);
227 void fsmenu_write_file(struct FSMenu* fsmenu, const char *filename)
229 FSMenuEntry *fsme= NULL;
230 int count=FSMENU_RECENT_MAX;
232 FILE *fp = fopen(filename, "w");
235 fprintf(fp, "[Bookmarks]\n");
236 for (fsme= fsmenu_get_category(fsmenu, FS_CATEGORY_BOOKMARKS); fsme; fsme= fsme->next) {
237 if (fsme->path && fsme->save) {
238 fprintf(fp, "%s\n", fsme->path);
241 fprintf(fp, "[Recent]\n");
242 for (fsme= fsmenu_get_category(fsmenu, FS_CATEGORY_RECENT); fsme && count; fsme= fsme->next, --count) {
243 if (fsme->path && fsme->save) {
244 fprintf(fp, "%s\n", fsme->path);
250 void fsmenu_read_bookmarks(struct FSMenu* fsmenu, const char *filename)
253 FSMenuCategory category = FS_CATEGORY_BOOKMARKS;
256 fp = fopen(filename, "r");
259 while ( fgets ( line, 256, fp ) != NULL ) /* read a line */
261 if (strncmp(line, "[Bookmarks]", 11)==0){
262 category = FS_CATEGORY_BOOKMARKS;
263 } else if (strncmp(line, "[Recent]", 8)==0){
264 category = FS_CATEGORY_RECENT;
266 int len = strlen(line);
268 if (line[len-1] == '\n') {
271 if (BLI_exist(line)) {
272 fsmenu_insert_entry(fsmenu, category, line, 0, 1);
280 void fsmenu_read_system(struct FSMenu* fsmenu)
284 /* Add the drive names to the listing */
290 tmp= GetLogicalDrives();
292 for (i=2; i < 26; i++) {
299 fsmenu_insert_entry(fsmenu, FS_CATEGORY_SYSTEM, tmps, 1, 0);
303 /* Adding Desktop and My Documents */
304 SHGetSpecialFolderPath(0, line, CSIDL_PERSONAL, 0);
305 fsmenu_insert_entry(fsmenu,FS_CATEGORY_BOOKMARKS, line, 1, 0);
306 SHGetSpecialFolderPath(0, line, CSIDL_DESKTOPDIRECTORY, 0);
307 fsmenu_insert_entry(fsmenu, FS_CATEGORY_BOOKMARKS, line, 1, 0);
312 #if (MAC_OS_X_VERSION_MIN_REQUIRED <= MAC_OS_X_VERSION_10_4)
317 /* loop through all the OS X Volumes, and add them to the SYSTEM section */
318 for (i=1; err!=nsvErr; i++)
321 unsigned char path[FILE_MAXDIR+FILE_MAXFILE];
323 err = FSGetVolumeInfo(kFSInvalidVolumeRefNum, i, NULL, kFSVolInfoNone, NULL, NULL, &dir);
327 FSRefMakePath(&dir, path, FILE_MAXDIR+FILE_MAXFILE);
328 if (strcmp((char*)path, "/home") && strcmp((char*)path, "/net"))
329 { /* /net and /home are meaningless on OSX, home folders are stored in /Users */
330 fsmenu_insert_entry(fsmenu, FS_CATEGORY_SYSTEM, (char *)path, 1, 0);
334 /* As 10.4 doesn't provide proper API to retrieve the favorite places,
335 assume they are the standard ones
336 TODO : replace hardcoded paths with proper BLI_get_folder calls */
337 home = getenv("HOME");
339 BLI_snprintf(line, 256, "%s/", home);
340 fsmenu_insert_entry(fsmenu, FS_CATEGORY_BOOKMARKS, line, 1, 0);
341 BLI_snprintf(line, 256, "%s/Desktop/", home);
342 if (BLI_exists(line)) {
343 fsmenu_insert_entry(fsmenu, FS_CATEGORY_BOOKMARKS, line, 1, 0);
345 BLI_snprintf(line, 256, "%s/Documents/", home);
346 if (BLI_exists(line)) {
347 fsmenu_insert_entry(fsmenu, FS_CATEGORY_BOOKMARKS, line, 1, 0);
349 BLI_snprintf(line, 256, "%s/Pictures/", home);
350 if (BLI_exists(line)) {
351 fsmenu_insert_entry(fsmenu, FS_CATEGORY_BOOKMARKS, line, 1, 0);
353 BLI_snprintf(line, 256, "%s/Music/", home);
354 if (BLI_exists(line)) {
355 fsmenu_insert_entry(fsmenu, FS_CATEGORY_BOOKMARKS, line, 1, 0);
357 BLI_snprintf(line, 256, "%s/Movies/", home);
358 if (BLI_exists(line)) {
359 fsmenu_insert_entry(fsmenu, FS_CATEGORY_BOOKMARKS, line, 1, 0);
363 /* 10.5 provides ability to retrieve Finder favorite places */
366 CFArrayRef pathesArray;
367 LSSharedFileListRef list;
368 LSSharedFileListItemRef itemRef;
369 CFIndex i, pathesCount;
370 CFURLRef cfURL = NULL;
371 CFStringRef pathString = NULL;
373 /* First get local mounted volumes */
374 list = LSSharedFileListCreate(NULL, kLSSharedFileListFavoriteVolumes, NULL);
375 pathesArray = LSSharedFileListCopySnapshot(list, &seed);
376 pathesCount = CFArrayGetCount(pathesArray);
378 for (i=0; i<pathesCount; i++)
380 itemRef = (LSSharedFileListItemRef)CFArrayGetValueAtIndex(pathesArray, i);
382 err = LSSharedFileListItemResolve(itemRef,
383 kLSSharedFileListNoUserInteraction
384 | kLSSharedFileListDoNotMountVolumes,
389 pathString = CFURLCopyFileSystemPath(cfURL, kCFURLPOSIXPathStyle);
391 if (!CFStringGetCString(pathString,line,256,kCFStringEncodingASCII))
393 fsmenu_insert_entry(fsmenu, FS_CATEGORY_SYSTEM, line, 1, 0);
395 CFRelease(pathString);
399 CFRelease(pathesArray);
402 /* Then get network volumes */
404 for (i=1; err!=nsvErr; i++)
407 FSVolumeRefNum volRefNum;
408 struct GetVolParmsInfoBuffer volParmsBuffer;
409 unsigned char path[FILE_MAXDIR+FILE_MAXFILE];
411 err = FSGetVolumeInfo(kFSInvalidVolumeRefNum, i, &volRefNum, kFSVolInfoNone, NULL, NULL, &dir);
415 err = FSGetVolumeParms(volRefNum, &volParmsBuffer, sizeof(volParmsBuffer));
416 if ((err != noErr) || (volParmsBuffer.vMServerAdr == 0)) /* Exclude local devices */
420 FSRefMakePath(&dir, path, FILE_MAXDIR+FILE_MAXFILE);
421 fsmenu_insert_entry(fsmenu, FS_CATEGORY_SYSTEM, (char *)path, 1, 0);
424 /* Finally get user favorite places */
425 list = LSSharedFileListCreate(NULL, kLSSharedFileListFavoriteItems, NULL);
426 pathesArray = LSSharedFileListCopySnapshot(list, &seed);
427 pathesCount = CFArrayGetCount(pathesArray);
429 for (i=0; i<pathesCount; i++)
431 itemRef = (LSSharedFileListItemRef)CFArrayGetValueAtIndex(pathesArray, i);
433 err = LSSharedFileListItemResolve(itemRef,
434 kLSSharedFileListNoUserInteraction
435 | kLSSharedFileListDoNotMountVolumes,
440 pathString = CFURLCopyFileSystemPath(cfURL, kCFURLPOSIXPathStyle);
442 if (!CFStringGetCString(pathString,line,256,kCFStringEncodingASCII))
444 fsmenu_insert_entry(fsmenu, FS_CATEGORY_BOOKMARKS, line, 1, 0);
446 CFRelease(pathString);
450 CFRelease(pathesArray);
452 #endif /* OSX 10.5+ */
457 char *home= getenv("HOME");
460 BLI_snprintf(line, FILE_MAXDIR, "%s/", home);
461 fsmenu_insert_entry(fsmenu, FS_CATEGORY_BOOKMARKS, line, 1, 0);
462 BLI_snprintf(line, FILE_MAXDIR, "%s/Desktop/", home);
463 if (BLI_exists(line)) {
464 fsmenu_insert_entry(fsmenu, FS_CATEGORY_BOOKMARKS, line, 1, 0);
471 /* loop over mount points */
476 fp = setmntent (MOUNTED, "r");
478 fprintf(stderr, "could not get a list of mounted filesystemts\n");
481 while ((mnt = getmntent (fp))) {
482 /* not sure if this is right, but seems to give the relevant mnts */
483 if(strncmp(mnt->mnt_fsname, "/dev", 4))
486 len= strlen(mnt->mnt_dir);
487 if(len && mnt->mnt_dir[len-1] != '/') {
488 BLI_snprintf(line, FILE_MAXDIR, "%s/", mnt->mnt_dir);
489 fsmenu_insert_entry(fsmenu, FS_CATEGORY_SYSTEM, line, 1, 0);
492 fsmenu_insert_entry(fsmenu, FS_CATEGORY_SYSTEM, mnt->mnt_dir, 1, 0);
496 if (endmntent (fp) == 0) {
497 fprintf(stderr, "could not close the list of mounted filesystemts\n");
504 fsmenu_insert_entry(fsmenu, FS_CATEGORY_SYSTEM, "/", 1, 0);
512 static void fsmenu_free_category(struct FSMenu* fsmenu, FSMenuCategory category)
514 FSMenuEntry *fsme= fsmenu_get_category(fsmenu, category);
517 FSMenuEntry *n= fsme->next;
519 if (fsme->path) MEM_freeN(fsme->path);
526 void fsmenu_free(struct FSMenu* fsmenu)
528 fsmenu_free_category(fsmenu, FS_CATEGORY_SYSTEM);
529 fsmenu_free_category(fsmenu, FS_CATEGORY_BOOKMARKS);
530 fsmenu_free_category(fsmenu, FS_CATEGORY_RECENT);