Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/gcompris/gcompris-nsbundle.m
blob: 1cc6c6c1920cc5bb269f025374f314cf8b56f30e (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include <Foundation/Foundation.h>
#include <AppKit/AppKit.h>

#include "gcompris.h"

#include "config.h"

void gcompris_fix_gtk_etc (void);
void gcompris_fix_lang(void);

void set_prefix( NSString *prefix_source_dir,
		 NSString *source_dir, 
		 NSString *filename, 
		 NSString *tmp_dir,
		 NSString *prefix,
		 const char *var);


@interface GComprisController : NSObject
- (void) quit: (id)sender;
- (void) applicationWillFinishLaunching: (NSNotification *)not;
@end


gchar *gcompris_nsbundle_resource(void)
{
  NSAutoreleasePool *pool;
  pool = [NSAutoreleasePool new];

  [NSApplication sharedApplication];
  [NSApp setDelegate: [GComprisController new]];

  gchar *resourcePath = NULL;
  
  resourcePath = g_strdup_printf("%s",[[[NSBundle mainBundle] resourcePath] UTF8String]);

  printf("NSBundle resourcePath %s\n", resourcePath);

  gcompris_fix_gtk_etc ();

  gcompris_fix_lang();

  [pool release];
  return resourcePath;
}

void gcompris_fix_lang (void)
{
  NSUserDefaults* defs = [NSUserDefaults standardUserDefaults];
  NSString* locale = [defs objectForKey:@"AppleLocale"];

  if (locale != nil) {
     printf ("Language %s\n", [locale UTF8String]);
     setenv("LANG", [locale UTF8String], 0);
  }
}

void gcompris_fix_gtk_etc (void)
{
  NSMutableString *gtk_path = [[[[[NSBundle mainBundle] executablePath] stringByDeletingLastPathComponent] mutableCopy] autorelease] ;

  NSString *gtk_dir = @NSBUNDLE_GTK_DIR ;
  NSString *gtk_conf_dir = @"/etc/gtk-2.0";
  NSString *pango_conf_dir = @"/etc/pango";
  NSString *gtk_immodules = @"/gtk.immodules";
  NSString *gdk_pixbuf_loaders =  @"/gdk-pixbuf.loaders";
  NSString *pango_modules = @"/pango.modules";
  NSString *pangorc = @"/pangorc";
  NSString *pythonpath1 = @"/lib/python/site-packages:";
  NSString *pythonpath2 = @"/lib/python/site-packages/gtk-2.0";

  printf("NSBundle executablePath %s\n\n", [gtk_path UTF8String]);

  // first is to suppress last component (executable name)
  [gtk_path appendString: gtk_dir];
  gtk_path = [gtk_path  stringByStandardizingPath];

  printf("NSBundle Gtk Dir Path %s\n\n", [gtk_path UTF8String]);

  // Get the temporary filenames
  NSString *tmp_dir = NSTemporaryDirectory();
  printf("Temporary directory %s\n\n", [tmp_dir UTF8String]);

  set_prefix( gtk_path,
	      gtk_conf_dir, 
	      gtk_immodules, 
	      tmp_dir,
	      gtk_path,
	      "GTK_IM_MODULE_FILE");


  set_prefix( gtk_path,
	      gtk_conf_dir, 
	      gdk_pixbuf_loaders, 
	      tmp_dir,
	      gtk_path,
	      "GDK_PIXBUF_MODULE_FILE");

  set_prefix( gtk_path,
	      pango_conf_dir, 
	      pango_modules, 
	      tmp_dir,
	      gtk_path,
	      NULL);

  // Warning -> the tmp_dir because the files are in tmp directory too !!!
  set_prefix( gtk_path,
	      pango_conf_dir, 
	      pangorc, 
	      tmp_dir,
	      tmp_dir,
	      "PANGO_RC_FILE");

  //Now we just need to adjust some environnement variables
  setenv ("GTK_EXE_PREFIX", g_strdup([gtk_path UTF8String]), TRUE);
  setenv ("GTK_DATA_PREFIX", g_strdup([gtk_path UTF8String]), TRUE);
  printf ("GTK_EXE_PREFIX  environnemnt set to %s\n", getenv("GTK_EXE_PREFIX"));
  printf ("GTK_DATA_PREFIX environnemnt set to %s\n", getenv("GTK_DATA_PREFIX"));

  // Unused ?
  NSMutableString *gtk_rc = [[gtk_path mutableCopy] autorelease];
  [gtk_rc appendString: gtk_conf_dir];
  [gtk_rc appendString: @"/gtkrc"];
  setenv ("GTK2_RC_FILES", g_strdup([gtk_rc UTF8String]), TRUE);
  printf ("GTK2_RC_FILES   environnemnt set to %s\n", getenv("GTK2_RC_FILES"));

  // is that usefull for GCompris ?
  NSMutableString *fontconfig_path = [[gtk_path mutableCopy] autorelease];
  [fontconfig_path appendString: @"/etc/fonts"];
  setenv ("FONTCONFIG_PATH", g_strdup([fontconfig_path UTF8String]), TRUE);
  printf ("FONTCONFIG_PATH environnemnt set %s\n", getenv("FONTCONFIG_PATH"));

  NSMutableString *python_modules_path = [[gtk_path mutableCopy] autorelease];
  [python_modules_path appendString: pythonpath1];
  [python_modules_path appendString: gtk_path];
  [python_modules_path appendString: pythonpath2];
  setenv ("PYTHONPATH", g_strdup([python_modules_path UTF8String]), TRUE);
  printf ("PYTHONPATH      environnemnt set %s\n", getenv("PYTHONPATH"));
  
}


void set_prefix( NSString *prefix_source_dir,
		 NSString *source_dir, 
		 NSString *filename, 
		 NSString *tmp_dir,
		 NSString *prefix,
		 const char *var)
{
  NSMutableString *source_file = [[prefix_source_dir mutableCopy] autorelease];
  [source_file appendString: source_dir];
  [source_file appendString: filename];

  NSMutableString *tmp_file = [[tmp_dir mutableCopy] autorelease];
  [tmp_file appendString: filename];

  NSMutableString *file_content = [[[NSString stringWithContentsOfFile: source_file] mutableCopy] autorelease];
  [file_content replaceOccurrencesOfString: @"@{prefix}" 
	    withString: prefix
	    options: NSLiteralSearch
	    range: NSMakeRange(0, [file_content length])];
  [file_content writeToFile: tmp_file atomically: NO ];

  printf("Write %s\n   to %s\n", [source_file UTF8String], [tmp_file  UTF8String]);

  if (var) {
    setenv(var, g_strdup([tmp_file UTF8String]), 1);
    printf ("%s environnemnt set to %s\n\n", var, getenv(var));
  }
  
}

@implementation GComprisController : NSObject 

- (void) quit: (id)sender
{
  printf ("Received Quit from NSMenu!\n");

  [NSApp terminate: self];
  gc_exit();
}

- (void) applicationWillFinishLaunching: (NSNotification *)not
{
  NSMenu *menu;

  menu = AUTORELEASE ([NSMenu new]);

  [menu addItemWithTitle: @"Quit"  
        action: @selector (quit:)  
        keyEquivalent: @"q"];

  [NSApp setMainMenu: menu];
}

@end