File: NSColorList.m

package info (click to toggle)
gnustep-gui 0.32.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,348 kB
  • sloc: objc: 178,763; ansic: 24,089; cpp: 664; yacc: 464; sh: 90; makefile: 72
file content (697 lines) | stat: -rw-r--r-- 18,465 bytes parent folder | download | duplicates (6)
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
/** <title>NSColorList</title>

   <abstract>Manage named lists of NSColors.</abstract>

   Copyright (C) 1996, 2000 Free Software Foundation, Inc.

   Author:  Scott Christley <scottc@net-community.com>
   Date: 1996
   Author: Nicola Pero <n.pero@mi.flashnet.it>
   Date: January 2000
   
   This file is part of the GNUstep GUI Library.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library 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
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; see the file COPYING.LIB.
   If not, see <http://www.gnu.org/licenses/> or write to the 
   Free Software Foundation, 51 Franklin Street, Fifth Floor, 
   Boston, MA 02110-1301, USA.
*/ 

#import "config.h"
#import <Foundation/NSNotification.h>
#import <Foundation/NSNotificationQueue.h>
#import <Foundation/NSLock.h>
#import <Foundation/NSDictionary.h>
#import <Foundation/NSArchiver.h>
#import <Foundation/NSCharacterSet.h>
#import <Foundation/NSException.h>
#import <Foundation/NSFileManager.h>
#import <Foundation/NSPathUtilities.h>
#import <Foundation/NSScanner.h>
#import <Foundation/NSString.h>

#import "AppKit/NSColorList.h"
#import "AppKit/NSColor.h"
#import "AppKit/AppKitExceptions.h"

// The list of available color lists is cached and re-loaded only
// after a time.
static NSMutableArray *_availableColorLists = nil;
static NSLock *_colorListLock = nil;

static NSColorList *defaultSystemColorList = nil;
static NSColorList *themeColorList = nil;

@interface NSColorList (GNUstepPrivate)

/* Loads the available color lists from standard directories.<br />
 * If called with a nil argument, this will check to see if the
 * lists have already been loaded, and only load if they haven't been.
 */
+ (void) _loadAvailableColorLists: (NSNotification*)aNotification;

/* Set the default system color list ... to be used if no system color
 * list has been loaded from file.  This should always be the last of
 * the array of available color lists even though it has noit been
 * written to file.
 */
+ (void) _setDefaultSystemColorList: (NSColorList*)aList;

/* Set the theme system color list ... if this is not nil, it is placed
 * at the start of the array of available lists and is used as the system
 * color list.
 */
+ (void) _setThemeSystemColorList: (NSColorList*)aList;

@end

@implementation NSColorList

//
// Class methods
//
+ (void) initialize
{
  if (self == [NSColorList class])
    {
      [self setVersion: 2];
      _colorListLock = (NSLock *)[NSRecursiveLock new];
    }
}

/*
 * Getting All Color Lists
 */
+ (NSArray*) availableColorLists
{
  NSArray	*a;

  // Serialize access to color list
  [_colorListLock lock];
  [NSColor whiteColor]; // NB This ensures that the System color list is defined
  [NSColorList _loadAvailableColorLists: nil];
  a =  [NSArray arrayWithArray: _availableColorLists];
  [_colorListLock unlock];

  return a;
}

/*
 * Getting a Color List by Name
 */
+ (NSColorList *) colorListNamed: (NSString *)name
{
  NSColorList  *r;
  NSEnumerator *e;

  // Serialize access to color list
  [_colorListLock lock];

  [NSColorList _loadAvailableColorLists: nil];
  e = [_availableColorLists objectEnumerator];
  
  while ((r = (NSColorList *)[e nextObject]) != nil) 
    {
      if ([[r name] isEqualToString: name])
	{
	  RETAIN(r);
	  break;
	}
    }
  
  [_colorListLock unlock];

  return AUTORELEASE(r);
}


/*
 * Instance methods
 */

/*
 * Private method for reading text color list files, with following format:
 * first line  =  <#/colors>
 * each subsequent line describes a color as <int float+ string>
 * the first int describes the method (ARGB, etc.), the floats
 * provide its arguments (e.g., r, g, b, alpha), and string is name.
 */
- (BOOL) _readTextColorFile: (NSString *) filepath
{
  int nColors;
  int method;
  float r;
  float g;
  float b;
  float alpha;
  NSString *cname;
  int i;
  BOOL st;
  NSColor *color;
  NSCharacterSet *newlineSet =
    [NSCharacterSet characterSetWithCharactersInString: @"\n"];
  NSScanner *scanner =
    [NSScanner scannerWithString:
                 [NSString stringWithContentsOfFile: _fullFileName]];

  if ([scanner scanInt: &nColors] == NO)
    {
      NSLog(@"Unable to read color file at \"%@\" -- unknown format.",
            _fullFileName);
      return NO;
    }

  for (i = 0; i < nColors; i++)
    {
      if ([scanner scanInt: &method] == NO)
        {
          NSLog(@"Unable to read color file at \"%@\" -- unknown format.",
                _fullFileName);
          break;
        }
      //FIXME- replace this by switch on method to different
      //       NSColor initializers
      if (method != 0)
        {
          NSLog(@"Unable to read color file at \"%@\" -- only RGBA form "
                @"supported.", _fullFileName);
          break;
        }
      st = [scanner scanFloat: &r];
      st = st && [scanner scanFloat: &g];
      st = st && [scanner scanFloat: &b];
      st = st && [scanner scanFloat: &alpha];
      st = st && [scanner scanUpToCharactersFromSet: newlineSet
                                         intoString: &cname];
      if (st == NO)
        {
          NSLog(@"Unable to read color file at \"%@\" -- unknown format.",
                _fullFileName);
          break;
        }
      color = [NSColor colorWithCalibratedRed: r green: g blue: b alpha: alpha];
      [self insertColor: color key: cname atIndex: i];
    }

  return i == nColors;
}

- (id) initWithName: (NSString *)name
{
  return [self initWithName: name
		   fromFile: nil];
}

- (id) initWithName: (NSString *)name
	   fromFile: (NSString *)path
{
  NSColorList *cl;
  BOOL        could_load = NO; 

  ASSIGN (_name, name);

  if (path != nil)
    {
      BOOL isDir = NO;
      // previously impl wrongly expected directory containing color file
      // rather than color file; we support this for apps that rely on it
      if (([[NSFileManager defaultManager] fileExistsAtPath: path
                                                isDirectory: &isDir] == NO)
          || (isDir == YES))
        {
          NSLog(@"NSColorList -initWithName:fromFile: warning: excluding "
                @"filename from path (%@) is deprecated.", path);
          ASSIGN (_fullFileName, [[path stringByAppendingPathComponent: name] 
                                   stringByAppendingPathExtension: @"clr"]);
        }
      else
        {
          ASSIGN (_fullFileName, path);
        }
  
      // Unarchive the color list
      
      // TODO [Optm]: Rewrite to initialize directly without unarchiving 
      // in another object
      NS_DURING
        {
          cl =
            (NSColorList*)[NSUnarchiver unarchiveObjectWithFile: _fullFileName];
        }
      NS_HANDLER
        {
          cl = nil;
        }
      NS_ENDHANDLER ;

      if (cl && [cl isKindOfClass: [NSColorList class]])
	{
	  could_load = YES;

	  _is_editable = [[NSFileManager defaultManager] 
	    isWritableFileAtPath: _fullFileName];
	  
	  ASSIGN(_colorDictionary, [NSMutableDictionary 
	    dictionaryWithDictionary: cl->_colorDictionary]);

	  ASSIGN(_orderedColorKeys, [NSMutableArray 
	    arrayWithArray: cl->_orderedColorKeys]);
	}
      else if ([[NSFileManager defaultManager] fileExistsAtPath: path])
        {
          _colorDictionary = [[NSMutableDictionary alloc] init];
          _orderedColorKeys = [[NSMutableArray alloc] init];
	  _is_editable = YES;

          if ([self _readTextColorFile: _fullFileName])
            {
              could_load = YES;
              _is_editable = [[NSFileManager defaultManager] 
                                   isWritableFileAtPath: _fullFileName];
            }
          else
            {
              RELEASE (_colorDictionary);
              RELEASE (_orderedColorKeys);
            }
        }
    }
  
  if (could_load == NO)
    {
      _fullFileName = nil;
      _colorDictionary = [[NSMutableDictionary alloc] init];
      _orderedColorKeys = [[NSMutableArray alloc] init];
      _is_editable = YES;  
    }
  
  return self;
}

- (void) dealloc
{
  RELEASE (_name);
  TEST_RELEASE (_fullFileName);
  RELEASE (_colorDictionary);
  RELEASE (_orderedColorKeys);
  [super dealloc];
}

/*
 * Getting a Color List by Name
 */
- (NSString *) name
{
  return _name;
}

/*
 * Managing Colors by Key
 */
- (NSArray *) allKeys
{
  return [NSArray arrayWithArray: _orderedColorKeys];
}

- (NSColor *) colorWithKey: (NSString *)key
{
  return [_colorDictionary objectForKey: key];
}

- (void) insertColor: (NSColor *)color
		 key: (NSString *)key
	     atIndex: (unsigned)location
{
  NSNotification	*n;

  if (_is_editable == NO)
    [NSException raise: NSColorListNotEditableException
		format: @"Color list cannot be edited\n"];

  [_colorDictionary setObject: color forKey: key];
  [_orderedColorKeys removeObject: key];
  [_orderedColorKeys insertObject: key atIndex: location];
  
  n = [NSNotification notificationWithName: NSColorListDidChangeNotification
				    object: self
				  userInfo: nil];
  [[NSNotificationQueue defaultQueue] 
    enqueueNotification: n
	   postingStyle: NSPostASAP
	   coalesceMask: NSNotificationCoalescingOnSender
	       forModes: nil];
}

- (void) removeColorWithKey: (NSString *)key
{
  NSNotification	*n;

  if (_is_editable == NO)
    [NSException raise: NSColorListNotEditableException
		format: @"Color list cannot be edited\n"];
  
  [_colorDictionary removeObjectForKey: key];
  [_orderedColorKeys removeObject: key];

  n = [NSNotification notificationWithName: NSColorListDidChangeNotification
				    object: self
				  userInfo: nil];
  [[NSNotificationQueue defaultQueue] 
    enqueueNotification: n
	   postingStyle: NSPostASAP
	   coalesceMask: NSNotificationCoalescingOnSender
	       forModes: nil];
}

- (void) setColor: (NSColor *)aColor
	   forKey: (NSString *)key
{
  NSNotification	*n;

  if (_is_editable == NO)
    [NSException raise: NSColorListNotEditableException
		format: @"Color list cannot be edited\n"];
  
  [_colorDictionary setObject: aColor forKey: key];

  if ([_orderedColorKeys containsObject: key] == NO)
    [_orderedColorKeys addObject: key];

  n = [NSNotification notificationWithName: NSColorListDidChangeNotification
				    object: self
				  userInfo: nil];
  [[NSNotificationQueue defaultQueue] 
    enqueueNotification: n
	   postingStyle: NSPostASAP
	   coalesceMask: NSNotificationCoalescingOnSender
	       forModes: nil];
}

/*
 * Editing
 */
- (BOOL) isEditable
{
  return _is_editable;
}

/*
 * Writing and Removing Files
 */
- (BOOL) writeToFile: (NSString *)path
{
  NSFileManager *fm = [NSFileManager defaultManager];
  NSString      *tmpPath;
  BOOL          isDir;
  BOOL          success;
  BOOL          path_is_standard = YES;

  /*
   * We need to initialize before saving, to avoid the new file being 
   * counted as a different list thus making us appear twice
   */
  [NSColorList _loadAvailableColorLists: nil];

  if (path == nil)
    {
      NSArray	*paths;

      // FIXME the standard path for saving color lists?
      paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,
	NSUserDomainMask, YES);
      if ([paths count] == 0)
	{
	  NSLog (@"Failed to find Library directory for user");
	  return NO;	// No directory to save to.
	}
      path = [[paths objectAtIndex: 0]
	stringByAppendingPathComponent: @"Colors"]; 
      isDir = YES;
    }
  else
    {
      [fm fileExistsAtPath: path isDirectory: &isDir];
    }

  if (isDir)
    {
      ASSIGN (_fullFileName, [[path stringByAppendingPathComponent: _name] 
        stringByAppendingPathExtension: @"clr"]);
    }
  else // it is a file
    {
      if ([[path pathExtension] isEqual: @"clr"] == YES)
	{
	  ASSIGN (_fullFileName, path);
	}
      else
	{
	  ASSIGN (_fullFileName, [[path stringByDeletingPathExtension]
	    stringByAppendingPathExtension: @"clr"]);
	}
      path = [path stringByDeletingLastPathComponent];
    }

  // Check if the path is a standard path
  if ([[path lastPathComponent] isEqualToString: @"Colors"] == NO)
    {
      path_is_standard = NO;
    }
  else 
    {
      tmpPath = [path stringByDeletingLastPathComponent];
      if (![NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,
	NSAllDomainsMask, YES) containsObject: tmpPath])
	{
	  path_is_standard = NO;
	}
    }

  /*
   * If path is standard and it does not exist, try to create it.
   * System standard paths should always be assumed to exist; 
   * this will normally then only try to create user paths.
   */
  if (path_is_standard && ([fm fileExistsAtPath: path] == NO))
    {
      if ([fm createDirectoryAtPath: path 
        withIntermediateDirectories: YES
			 attributes: nil
                              error: NULL])
	{
	  NSLog (@"Created standard directory %@", path);
	}
      else
	{
	  NSLog (@"Failed attempt to create directory %@", path);
	}
    }

  success = [NSArchiver archiveRootObject: self 
				   toFile: _fullFileName];

  if (success && path_is_standard)
    {
      [_colorListLock lock];
      if ([_availableColorLists containsObject: self] == NO)
	[_availableColorLists addObject: self];
      [_colorListLock unlock];      
      return YES;
    }
  
  return success;
}

- (void) removeFile
{
  if (_fullFileName && _is_editable)
    {
      // Remove the file
      [[NSFileManager defaultManager] removeFileAtPath: _fullFileName
					       handler: nil];
      
      // Remove the color list from the global list of colors
      [_colorListLock lock];
      [_availableColorLists removeObject: self];
      [_colorListLock unlock];

      // Reset file name
      _fullFileName = nil;
    }
}

- (void) encodeWithCoder: (NSCoder*)aCoder
{
  if ([aCoder allowsKeyedCoding])
    {
      NSMutableArray *colors = [[NSMutableArray alloc] init];
      NSInteger i;

      for (i = 0; i < [_orderedColorKeys count]; i++)
        {
          NSString *name = [_orderedColorKeys objectAtIndex: i];
          [colors insertObject: [_colorDictionary objectForKey: name]
                       atIndex: i];
        }
      [aCoder encodeObject: _name forKey: @"NSName"];
      [aCoder encodeObject: _orderedColorKeys forKey: @"NSKeys"];
      [aCoder encodeObject: colors forKey: @"NSColors"];
      RELEASE(colors);
     }
  else
    {
      [aCoder encodeObject: _name];
      [aCoder encodeObject: _colorDictionary];
      [aCoder encodeObject: _orderedColorKeys];
    }
}

- (id) initWithCoder: (NSCoder*)aDecoder
{
  if ([aDecoder allowsKeyedCoding])
    {
      NSArray *colors;

      ASSIGN(_name, [aDecoder decodeObjectForKey: @"NSName"]);
      ASSIGN(_orderedColorKeys, [aDecoder decodeObjectForKey: @"NSKeys"]);
      colors = [aDecoder decodeObjectForKey: @"NSColors"];
      _colorDictionary = [[NSMutableDictionary alloc]
                           initWithObjects: colors
                                   forKeys: _orderedColorKeys];
    }
  else
    {
      [aDecoder decodeValueOfObjCType: @encode(id) at: &_name];
      [aDecoder decodeValueOfObjCType: @encode(id) at: &_colorDictionary];
      [aDecoder decodeValueOfObjCType: @encode(id) at: &_orderedColorKeys];
    }
  return self;
}

@end

@implementation NSColorList (GNUstepPrivate)

+ (void) _loadAvailableColorLists: (NSNotification*)aNotification
{
  [_colorListLock lock];
  /* FIXME ... we should ensure that we get housekeeping notifications */
  if (_availableColorLists != nil && aNotification == nil)
    {
      // Nothing to do ... already loaded
      [_colorListLock unlock];
    }
  else
    {
      NSString			*dir;
      NSString			*file;
      NSEnumerator		*e;
      NSFileManager		*fm = [NSFileManager defaultManager];
      NSDirectoryEnumerator	*de;
      NSColorList		*newList;

      if (_availableColorLists == nil)
	{
	  // Create the global array of color lists
	  _availableColorLists = [[NSMutableArray alloc] init];
	}
      else
	{
	  [_availableColorLists removeAllObjects];
	}
      
      /*
       * Keep any pre-loaded system color list.
       */
      if (themeColorList != nil)
	{
	  [_availableColorLists addObject: themeColorList];
	}

      /*
       * Load color lists found in standard paths into the array
       * FIXME: Check exactly where in the directory tree we should scan.
       */
      e = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,
	NSAllDomainsMask, YES) objectEnumerator];

      while ((dir = (NSString *)[e nextObject])) 
	{
	  BOOL flag;

	  dir = [dir stringByAppendingPathComponent: @"Colors"];
	  if (![fm fileExistsAtPath: dir isDirectory: &flag] || !flag)
	    {
	      // Only process existing directories
	      continue;
	    }

	  de = [fm enumeratorAtPath: dir];
	  while ((file = [de nextObject])) 
	    {
	      if ([[file pathExtension] isEqualToString: @"clr"])
		{
		  NSString	*name;

		  name = [file stringByDeletingPathExtension];
		  newList = [[NSColorList alloc] initWithName: name
		    fromFile: [dir stringByAppendingPathComponent: file]];
		  [_availableColorLists addObject: newList];
		  RELEASE(newList);
		}
	    }
	}  

      if (defaultSystemColorList != nil)
        {
	  [_availableColorLists addObject: defaultSystemColorList];
	}
      [_colorListLock unlock];
    }
}

+ (void) _setDefaultSystemColorList: (NSColorList*)aList
{
  [_colorListLock lock];
  if (defaultSystemColorList != aList)
    {
      if (defaultSystemColorList != nil
        && [_availableColorLists lastObject] == defaultSystemColorList)
	{
	  [_availableColorLists removeLastObject];
	}
      ASSIGN(defaultSystemColorList, aList);
      [_availableColorLists addObject: aList];
    }
  [_colorListLock unlock];
}

+ (void) _setThemeSystemColorList: (NSColorList*)aList
{
  [_colorListLock lock];
  if (themeColorList != aList)
    {
      if (themeColorList != nil && [_availableColorLists count] > 0
        && [_availableColorLists objectAtIndex: 0] == themeColorList)
	{
	  [_availableColorLists removeObjectAtIndex: 0];
	}
      ASSIGN(themeColorList, aList);
      [_availableColorLists insertObject: aList atIndex: 0];
    }
  [_colorListLock unlock];
}

@end