2012-08-20 21:34:54 +02:00
/ *
* FanControl
*
2012-08-21 00:05:39 +02:00
* Copyright ( c ) 2006 -2012 Hendrik Holtmann
2012-08-20 21:34:54 +02:00
*
* MachineDefaults . m - MacBook ( Pro ) FanControl application
*
* 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
* /
# import "MachineDefaults.h"
# import "NSFileManager+DirectoryLocations.h"
2014-10-21 17:57:58 +02:00
@ interface MachineDefaults ( )
{
}
@ end
2012-08-20 21:34:54 +02:00
@ implementation MachineDefaults
2014-10-21 17:57:58 +02:00
- ( instancetype ) init : ( NSString * ) p_machine {
2014-03-25 15:13:47 -04:00
if ( self = [ super init ] ) {
machine = [ MachineDefaults computerModel ] ;
2014-10-21 17:57:58 +02:00
[ self refreshPlist ] ;
2014-03-25 15:13:47 -04:00
}
2012-08-20 21:34:54 +02:00
return self ;
}
2014-10-21 17:57:58 +02:00
- ( void ) refreshPlist
{
supported_machines = [ [ NSArray alloc ] initWithContentsOfFile : [ [ [ NSFileManager defaultManager ] applicationSupportDirectory ] stringByAppendingPathComponent : @ "Machines.plist" ] ] ;
supported = NO ;
int i ;
for ( i = 0 ; i < [ supported_machines count ] ; i + + ) {
if ( [ machine isEqualToString : supported_machines [ i ] [ @ "Machine" ] ] ) {
supported = YES ;
machine_num = i ;
}
}
2012-08-20 21:34:54 +02:00
}
2014-10-21 17:57:58 +02:00
- ( NSDictionary * ) readFromPlist {
2012-08-20 21:34:54 +02:00
if ( ! supported ) { return nil ; }
2014-10-21 17:57:58 +02:00
return supported_machines [ machine_num ] ;
2012-08-20 21:34:54 +02:00
}
2014-10-21 17:57:58 +02:00
- ( void ) readFromSMC {
NSUInteger num_fans = [ smcWrapper get_fan _num ] ;
2012-08-20 21:34:54 +02:00
NSString * desc ;
NSNumber * min , * max ;
NSData * xmldata ;
NSString * error ;
NSMutableArray * fans = [ [ NSMutableArray alloc ] init ] ;
2014-10-21 17:57:58 +02:00
for ( NSUInteger i = 0 ; i < num_fans ; i + + ) {
min = @ ( [ smcWrapper get_min _speed : i ] ) ;
max = @ ( [ smcWrapper get_max _speed : i ] ) ;
2012-08-20 21:34:54 +02:00
desc = [ smcWrapper get_fan _descr : i ] ;
2014-10-21 17:57:58 +02:00
[ fans addObject : [ [ NSMutableDictionary alloc ] initWithDictionary : @ { @ "Description" : desc , @ "Minspeed" : min , @ "Maxspeed" : max , @ "selspeed" : min } ] ] ;
2012-08-20 21:34:54 +02:00
}
// save to plist for future
2016-03-21 15:20:59 +01:00
NSMutableArray * supported_m = [ [ NSMutableArray alloc ] initWithContentsOfFile : [ [ [ NSFileManager defaultManager ] applicationSupportDirectory ] stringByAppendingPathComponent : @ "Machines.plist" ] ] ;
NSMutableDictionary * new_machine ;
if ( fans = = nil )
{
new_machine = [ [ NSMutableDictionary alloc ] initWithDictionary : @ { @ "Fans" : [ NSNull null ] , @ "NumFans" : @ ( 0 ) , @ "Machine" : machine , @ "Comment" : @ "Autogenerated" , @ "Minspeed" : min , @ "Maxspeed" : max } ] ;
} else {
new_machine = [ [ NSMutableDictionary alloc ] initWithDictionary : @ { @ "Fans" : fans , @ "NumFans" : @ ( num_fans ) , @ "Machine" : machine , @ "Comment" : @ "Autogenerated" , @ "Minspeed" : min , @ "Maxspeed" : max } ] ;
}
2012-08-20 21:34:54 +02:00
[ supported_m addObject : new_machine ] ;
// save to plist
xmldata = [ NSPropertyListSerialization dataFromPropertyList : supported_m
format : NSPropertyListXMLFormat_v1 _0
errorDescription : & error ] ;
[ xmldata writeToFile : [ [ [ NSFileManager defaultManager ] applicationSupportDirectory ] stringByAppendingPathComponent : @ "Machines.plist" ] atomically : YES ] ;
}
- ( NSDictionary * ) get_machine _defaults {
2014-10-21 17:57:58 +02:00
if ( ! supported ) {
NSAlert * alert = [ NSAlert alertWithMessageText : NSLocalizedString ( @ "Alert!" , nil )
2012-08-20 21:34:54 +02:00
defaultButton : NSLocalizedString ( @ "Continue" , nil ) alternateButton : NSLocalizedString ( @ "Quit" , nil ) otherButton : nil
2020-04-23 14:19:06 +05:30
informativeTextWithFormat : NSLocalizedString ( @ "smcFanControl has not been tested on this machine yet, but it should run if you follow the instructions. \n\nIf you choose to continue, please make sure you have no other FanControl-software running. Otherwise please quit, deinstall the other software, restart your machine and rerun smcFanControl!" , nil ) ] ;
2014-10-21 17:57:58 +02:00
NSModalResponse code = [ alert runModal ] ;
if ( code = = NSAlertDefaultReturn ) {
[ self readFromSMC ] ;
[ self refreshPlist ] ;
2012-08-20 21:34:54 +02:00
} else {
[ [ NSApplication sharedApplication ] terminate : nil ] ;
}
}
2014-10-21 17:57:58 +02:00
NSDictionary * defaultsDict = [ self readFromPlist ] ;
NSUInteger i ;
// localize fan - descriptions
for ( i = 0 ; i < [ defaultsDict [ @ "Fans" ] count ] ; i + + ) {
NSString * newvalue = NSLocalizedString ( defaultsDict [ @ "Fans" ] [ i ] [ @ "Description" ] , nil ) ;
[ defaultsDict [ @ "Fans" ] [ i ] setValue : newvalue forKey : @ "Description" ] ;
}
return defaultsDict ;
2012-08-20 21:34:54 +02:00
}
+ ( NSString * ) computerModel
{
static NSString * computerModel = nil ;
if ( ! computerModel ) {
io_service _t pexpdev ;
if ( ( pexpdev = IOServiceGetMatchingService ( kIOMasterPortDefault , IOServiceMatching ( "IOPlatformExpertDevice" ) ) ) )
{
NSData * data ;
2014-10-21 17:57:58 +02:00
if ( ( data = ( id ) CFBridgingRelease ( IORegistryEntryCreateCFProperty ( pexpdev , CFSTR ( "model" ) , kCFAllocatorDefault , 0 ) ) ) ) {
2012-08-20 21:34:54 +02:00
computerModel = [ [ NSString allocWithZone : NULL ] initWithCString : [ data bytes ] encoding : NSASCIIStringEncoding ] ;
}
}
}
return computerModel ;
}
@ end