1#include "osxbits.h"
2#import <AppKit/AppKit.h>
3#import <Foundation/Foundation.h>
4
5char *osx_gethomedir(void)
6{
7    NSURL *url = [[NSFileManager defaultManager] URLForDirectory:NSUserDirectory
8                                                        inDomain:NSUserDomainMask
9                                               appropriateForURL:nil
10                                                          create:FALSE
11                                                           error:nil];
12    if (url && [url isFileURL]) {
13        return strdup([[url path] UTF8String]);
14    }
15    return NULL;
16}
17
18char *osx_getappdir(void)
19{
20    NSString *path = [[NSBundle mainBundle] resourcePath];
21    if (path) {
22        return strdup([path cStringUsingEncoding:NSUTF8StringEncoding]);
23    }
24    return NULL;
25}
26
27char *osx_getsupportdir(int global)
28{
29    NSURL *url = [[NSFileManager defaultManager] URLForDirectory:NSApplicationSupportDirectory
30                                                        inDomain:global ? NSLocalDomainMask : NSUserDomainMask
31                                               appropriateForURL:nil
32                                                          create:FALSE
33                                                           error:nil];
34    if (url && [url isFileURL]) {
35        return strdup([[url path] UTF8String]);
36    }
37    return NULL;
38}
39
40char * wmosx_filechooser(const char *initialdir, const char *initialfile, const char *type, int foropen, char **choice)
41{
42    NSSavePanel *panel = nil;
43    NSModalResponse resp;
44    NSArray *filetypes = [[NSArray alloc] initWithObjects:[NSString stringWithUTF8String:type], nil];
45    NSURL *initialdirurl = [NSURL fileURLWithPath:[NSString stringWithUTF8String:initialdir]];
46
47    *choice = NULL;
48
49    if (foropen) {
50        panel = [NSOpenPanel openPanel];    // Inherits from NSSavePanel.
51    } else {
52        panel = [NSSavePanel savePanel];
53        if (initialfile) {
54            [panel setNameFieldStringValue:[NSString stringWithUTF8String:initialfile]];
55        }
56    }
57    [panel setAllowedFileTypes:filetypes];
58    [panel setDirectoryURL:initialdirurl];
59
60    resp = [panel runModal];
61    if (resp == NSFileHandlingPanelOKButton) {
62        NSURL *file = [panel URL];
63        if ([file isFileURL]) {
64            *choice = strdup([[file path] UTF8String]);
65        }
66    }
67
68    [panel release];
69    [filetypes release];
70    [initialdirurl release];
71
72    return resp == NSFileHandlingPanelOKButton ? 1 : 0;
73}
74