Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 130 lines (114 sloc) 5.474 kb
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
//
// SUSystemProfiler.m
// Sparkle
//
// Created by Andy Matuschak on 12/22/07.
// Copyright 2007 Andy Matuschak. All rights reserved.
// Adapted from Sparkle+, by Tom Harrington.
//

#import "Sparkle.h"
#import "SUSystemProfiler.h"

#import <sys/sysctl.h>

@implementation SUSystemProfiler
+ (SUSystemProfiler *)sharedSystemProfiler
{
static SUSystemProfiler *sharedSystemProfiler = nil;
if (!sharedSystemProfiler)
sharedSystemProfiler = [[self alloc] init];
return sharedSystemProfiler;
}

- (NSDictionary *)modelTranslationTable
{
NSString *path = [[NSBundle bundleForClass:[self class]] pathForResource:@"SUModelTranslation" ofType:@"plist"];
return [[[NSDictionary alloc] initWithContentsOfFile:path] autorelease];
}

- (NSMutableArray *)systemProfileArrayForHostBundle:(NSBundle *)hostBundle
{
NSDictionary *modelTranslation = [self modelTranslationTable];

// Gather profile information and append it to the URL.
NSMutableArray *profileArray = [NSMutableArray array];
NSArray *profileDictKeys = [NSArray arrayWithObjects:@"key", @"visibleKey", @"value", @"visibleValue", nil];
int error = 0 ;
int value = 0 ;
unsigned long length = sizeof(value) ;

// OS version
NSString *currentSystemVersion = [SUUpdater systemVersionString];
if (currentSystemVersion != nil)
[profileArray addObject:[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"osVersion",@"OS Version",currentSystemVersion,currentSystemVersion,nil] forKeys:profileDictKeys]];

// CPU type (decoder info for values found here is in mach/machine.h)
error = sysctlbyname("hw.cputype", &value, &length, NULL, 0);
int cpuType = -1;
if (error == 0) {
cpuType = value;
NSString *visibleCPUType;
switch(value) {
case 7: visibleCPUType=@"Intel"; break;
case 18: visibleCPUType=@"PowerPC"; break;
default: visibleCPUType=@"Unknown"; break;
}
[profileArray addObject:[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"cputype",@"CPU Type", [NSNumber numberWithInt:value], visibleCPUType,nil] forKeys:profileDictKeys]];
}
error = sysctlbyname("hw.cpusubtype", &value, &length, NULL, 0);
if (error == 0) {
NSString *visibleCPUSubType;
if (cpuType == 7) {
// Intel
visibleCPUSubType = @"Intel"; // If anyone knows how to tell a Core Duo from a Core Solo, please email tph@atomicbird.com
} else if (cpuType == 18) {
// PowerPC
switch(value) {
case 9: visibleCPUSubType=@"G3"; break;
case 10: case 11: visibleCPUSubType=@"G4"; break;
case 100: visibleCPUSubType=@"G5"; break;
default: visibleCPUSubType=@"Other"; break;
}
} else {
visibleCPUSubType = @"Other";
}
[profileArray addObject:[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"cpusubtype",@"CPU Subtype", [NSNumber numberWithInt:value], visibleCPUSubType,nil] forKeys:profileDictKeys]];
}
error = sysctlbyname("hw.model", NULL, &length, NULL, 0);
if (error == 0) {
char *cpuModel;
cpuModel = (char *)malloc(sizeof(char) * length);
error = sysctlbyname("hw.model", cpuModel, &length, NULL, 0);
if (error == 0) {
NSString *rawModelName = [NSString stringWithUTF8String:cpuModel];
NSString *visibleModelName = [modelTranslation objectForKey:rawModelName];
if (visibleModelName == nil)
visibleModelName = rawModelName;
[profileArray addObject:[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"model",@"Mac Model", rawModelName, visibleModelName, nil] forKeys:profileDictKeys]];
}
if (cpuModel != NULL)
free(cpuModel);
}

// Number of CPUs
error = sysctlbyname("hw.ncpu", &value, &length, NULL, 0);
if (error == 0)
[profileArray addObject:[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"ncpu",@"Number of CPUs", [NSNumber numberWithInt:value], [NSNumber numberWithInt:value],nil] forKeys:profileDictKeys]];

// User preferred language
NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];
NSArray *languages = [defs objectForKey:@"AppleLanguages"];
if (languages && ([languages count] > 0))
[profileArray addObject:[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"lang",@"Preferred Language", [languages objectAtIndex:0], [languages objectAtIndex:0],nil] forKeys:profileDictKeys]];

// Application sending the request
NSString *appName = [hostBundle name];
if (appName)
[profileArray addObject:[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"appName",@"Application Name", appName, appName,nil] forKeys:profileDictKeys]];
NSString *appVersion = [hostBundle version];
if (appVersion)
[profileArray addObject:[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"appVersion",@"Application Version", appVersion, appVersion,nil] forKeys:profileDictKeys]];

// Number of displays?
// CPU speed
OSErr err;
SInt32 gestaltInfo;
err = Gestalt(gestaltProcClkSpeedMHz,&gestaltInfo);
if (err == noErr)
[profileArray addObject:[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"cpuFreqMHz",@"CPU Speed (MHz)", [NSNumber numberWithInt:gestaltInfo], [NSNumber numberWithInt:gestaltInfo],nil] forKeys:profileDictKeys]];

// amount of RAM
err = Gestalt(gestaltPhysicalRAMSizeInMegabytes,&gestaltInfo);
if (err == noErr)
[profileArray addObject:[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"ramMB",@"Memory (MB)", [NSNumber numberWithInt:gestaltInfo], [NSNumber numberWithInt:gestaltInfo],nil] forKeys:profileDictKeys]];

return profileArray;
}

@end
Something went wrong with that request. Please try again.