Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/macosx/fcinstaller.m
blob: 49b077b1d46238ea3d7becc284c9d98f2dcd8554 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
//
//  fcinstaller.m
//  TuxPaint
//
//  Created by Martin Fuhrer on 03/02/08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation; either version 2 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//  (See COPYING.txt)
//

#import <Foundation/Foundation.h>
#include <fontconfig/fontconfig.h>

/* Category for NSFileManager */
@interface NSFileManager (CreateDirectoryRecursively)
- (BOOL)createDirectoryRecursively:(NSString *)path attributes:(NSDictionary *)attributes;
@end

int main(int argc, const char* argv[])
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSBundle *bundle = [NSBundle mainBundle];
    NSString *bundlePath = [bundle bundlePath];
    
    BOOL success = TRUE;
    BOOL fileExists = TRUE;
    
    if( argc < 2 )
        return -1;
    
    NSString *globalPreferencesPath = [NSString stringWithCString:(argv[1])];
    NSString *fontsPath = [globalPreferencesPath stringByAppendingString:@"/fontconfig/fonts"];
    NSString *fontsConfInstalledPath = [fontsPath stringByAppendingString:@"/fonts.conf"];
    NSString *fontsDtdInstalledPath = [fontsPath stringByAppendingString:@"/fonts.dtd"];
    NSString *fontsConfBundlePath = [bundle pathForResource:@"fonts" ofType:@"conf"];
    NSString *fontsDtdBundlePath = [bundle pathForResource:@"fonts" ofType:@"dtd"];
    
    fileExists = [fileManager fileExistsAtPath:fontsConfInstalledPath];
    if (!fileExists) {
        success = ([fileManager createDirectoryRecursively:fontsPath attributes:nil] && success);
        success = ([fileManager copyPath:fontsConfBundlePath toPath:fontsConfInstalledPath handler:nil] && success);
    }
    
    fileExists = [fileManager fileExistsAtPath:fontsDtdInstalledPath];
    if (!fileExists) {
        success = ([fileManager createDirectoryRecursively:fontsPath attributes:nil] && success);
        success = ([fileManager copyPath:fontsDtdBundlePath toPath:fontsDtdInstalledPath handler:nil] && success);
    }
    
    /*
    NSString *globalCachePath = [globalPreferencesPath stringByAppendingString:@"/fontconfig/cache"];
    NSString *userCachePath = [[NSString stringWithString:@"~/.fontconfig"] stringByExpandingTildeInPath];
    fileExists = ([fileManager fileExistsAtPath:globalCachePath] || [fileManager fileExistsAtPath:userCachePath]);
    if (!fileExists)
    {
        FcBool initSuccess = FcInit();
        if( initSuccess == FcFalse )
            success = FALSE;
    }
    */
    
    [pool release];
    
    return (int)(!success);
}

@implementation NSFileManager (CreateDirectoryRecursively)

- (BOOL)createDirectoryRecursively:(NSString *)path attributes:(NSDictionary *)attributes
{ 
    BOOL isDir = TRUE; 
    BOOL fileExists;
    
    fileExists = [self fileExistsAtPath:path isDirectory:&isDir]; 
    if (isDir) {
        if (fileExists) {
            /* directory exists */
            return TRUE; 
        }
        else
        {
            /* create directory */
            NSString *parentDirectory = [path stringByDeletingLastPathComponent];
            [self createDirectoryRecursively:parentDirectory attributes:attributes]; 
            return [self createDirectoryAtPath:path attributes:attributes];
        }
    }
    else
    { 
        /* desired directory path is blocked by a file */ 
        return FALSE; 
    }  
} 

@end