1/* NSObject+Utilities.m - this file is part of SOGo 2 * 3 * Copyright (C) 2007 Inverse inc. 4 * 5 * Author: Wolfgang Sourdeau <wsourdeau@inverse.ca> 6 * 7 * This file is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation; either version 2, or (at your option) 10 * any later version. 11 * 12 * This file is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with this program; see the file COPYING. If not, write to 19 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 20 * Boston, MA 02111-1307, USA. 21 */ 22 23#import <Foundation/NSArray.h> 24#import <Foundation/NSBundle.h> 25#import <Foundation/NSDebug.h> 26#import <Foundation/NSEnumerator.h> 27#import <Foundation/NSString.h> 28 29#import <NGObjWeb/WOContext+SoObjects.h> 30#import <NGObjWeb/WORequest.h> 31 32#import "NSDictionary+Utilities.h" 33#import "SOGoUser.h" 34#import "SOGoUserDefaults.h" 35 36#import "NSObject+Utilities.h" 37 38@implementation NSObject (SOGoObjectUtilities) 39 40- (NSString *) jsonRepresentation 41{ 42 [self subclassResponsibility: _cmd]; 43 44 return nil; 45} 46 47- (NSArray *) domNode: (id <DOMNode>) node 48 getChildNodesByType: (DOMNodeType ) type 49{ 50 NSMutableArray *nodes; 51 id <DOMNode> currentChild; 52 53 nodes = [NSMutableArray array]; 54 55 currentChild = [node firstChild]; 56 while (currentChild) 57 { 58 if ([currentChild nodeType] == type) 59 [nodes addObject: currentChild]; 60 currentChild = [currentChild nextSibling]; 61 } 62 63 return nodes; 64} 65 66- (NSArray *) _languagesForLabelsInContext: (WOContext *) context 67{ 68 NSMutableArray *languages; 69 NSArray *browserLanguages; 70 NSString *language; 71 SOGoUser *user; 72 73#warning the purpose of this method needs to be reviewed 74 languages = [NSMutableArray array]; 75 76 user = [context activeUser]; 77 if ([user isKindOfClass: [SOGoUser class]]) 78 { 79 language = [[user userDefaults] language]; 80 [languages addObject: language]; 81 } 82 else 83 { 84 browserLanguages = [[context request] browserLanguages]; 85 [languages addObjectsFromArray: browserLanguages]; 86 } 87 88 return languages; 89} 90 91- (NSString *) labelForKey: (NSString *) key 92 inContext: (WOContext *) context 93{ 94 NSString *language, *label; 95 NSArray *paths; 96 NSEnumerator *languages; 97 NSBundle *bundle; 98 NSDictionary *strings; 99 100 label = nil; 101 102 bundle = [NSBundle bundleForClass: [self class]]; 103 if (!bundle) 104 bundle = [NSBundle mainBundle]; 105 languages = [[self _languagesForLabelsInContext: context] objectEnumerator]; 106 107 while (!label && (language = [languages nextObject])) 108 { 109 paths = [bundle pathsForResourcesOfType: @"strings" 110 inDirectory: [NSString stringWithFormat: @"%@.lproj", 111 language] 112 forLocalization: language]; 113 if ([paths count] > 0) 114 { 115 strings = [NSDictionary 116 dictionaryFromStringsFile: [paths objectAtIndex: 0]]; 117 label = [strings objectForKey: key]; 118 } 119 } 120 if (!label) 121 label = key; 122 123 return label; 124} 125 126// 127// Set SOGoDebugLeaks = YES in your defaults to enable. 128// 129+ (void) memoryStatistics 130{ 131 Class *classList = GSDebugAllocationClassList (); 132 Class *pointer; 133 int i, count, total, peak; 134 NSString *className; 135 136 pointer = classList; 137 i = 0; 138 139 printf("Class count total peak\n"); 140 while (pointer[i] != NULL) 141 { 142 className = NSStringFromClass (pointer[i]); 143 count = GSDebugAllocationCount (pointer[i]); 144 total = GSDebugAllocationTotal (pointer[i]); 145 peak = GSDebugAllocationPeak (pointer[i]); 146 147 printf("%s %d %d %d\n", [className UTF8String], count, total, peak); 148 i++; 149 } 150 NSZoneFree(NSDefaultMallocZone(), classList); 151 152 printf("Done!\n"); 153} 154 155@end 156