forked from codykrieger/gfxCardStatus
-
Notifications
You must be signed in to change notification settings - Fork 20
/
GSNotifier.m
162 lines (131 loc) · 5.67 KB
/
GSNotifier.m
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
//
// GSNotifier.m
// gfxCardStatus
//
// Created by Cody Krieger on 6/12/12.
// Copyright (c) 2012 Cody Krieger. All rights reserved.
//
#import "GSNotifier.h"
#import "NSAttributedString+Hyperlink.h"
#import "GSMux.h"
#import "GSPreferences.h"
#define kGPUChangedNotificationKey @"GrowlGPUChanged"
#define kIntegratedOnlyMessageExplanationURL [kApplicationWebsiteURL stringByAppendingString:@"/switching.html#integrated-only-mode-limitations"]
static NSString *_lastMessage = nil;
@interface GSNotifier () <NSUserNotificationCenterDelegate>
+ (NSString *)_keyForNotificationType:(GSGPUType)type;
@end
@implementation GSNotifier
#pragma mark - Initializers
+ (GSNotifier *)sharedInstance
{
static dispatch_once_t pred = 0;
__strong static GSNotifier *_sharedObject = nil;
dispatch_once(&pred, ^{
_sharedObject = [[self alloc] init];
});
return _sharedObject;
}
- (id)init
{
if (!(self = [super init]))
return nil;
[NSUserNotificationCenter defaultUserNotificationCenter].delegate = self;
return self;
}
#pragma mark - GSNotifier API
+ (void)showGPUChangeNotification:(GSGPUType)type
{
// Get the localized notification name and message, as well as the current
// GPU name for display in the message.
NSString *key = [self _keyForNotificationType:type];
NSString *title = Str(key);
NSString *cardName = type == GSGPUTypeIntegrated ? [GSGPU integratedGPUName] : [GSGPU discreteGPUName];
NSString *message = [NSString stringWithFormat:Str([key stringByAppendingString:@"Message"]), cardName];
// Make sure that we don't display the notification if it's the same message
// as the last one we fired off. Because that's unbelievably annoying. Also
// check to make sure the user even wants to see the notifications in the
// first place.
if (![message isEqualToString:_lastMessage]
&& ([GSPreferences sharedInstance].shouldDisplayNotifications || [self notificationCenterIsAvailable])) {
if ([self notificationCenterIsAvailable]) {
NSUserNotification *notification = [[NSUserNotification alloc] init];
notification.deliveryDate = [NSDate date];
notification.hasActionButton = NO;
notification.title = title;
notification.informativeText = message;
[[NSUserNotificationCenter defaultUserNotificationCenter] scheduleNotification:notification];
} else {
[GrowlApplicationBridge notifyWithTitle:title
description:message
notificationName:key
iconData:nil
priority:0
isSticky:NO
clickContext:nil];
}
_lastMessage = message;
}
}
+ (void)showOneTimeNotification
{
NSAlert *versionInfo = [[NSAlert alloc] init];
[versionInfo setMessageText:Str(@"ThanksForDownloading")];
[versionInfo setInformativeText:Str(@"PleaseConsiderDonating")];
NSTextView *accessory = [[NSTextView alloc] initWithFrame:NSMakeRect(0, 0, 300, 15)];
[accessory insertText:[NSAttributedString hyperlinkFromString:kApplicationWebsiteURL
withURL:[NSURL URLWithString:kApplicationWebsiteURL]]];
[accessory setEditable:NO];
[accessory setDrawsBackground:NO];
[versionInfo setAccessoryView:accessory];
[versionInfo addButtonWithTitle:Str(@"DontShowAgain")];
[versionInfo runModal];
}
+ (void)showUnsupportedMachineMessage
{
NSAlert *alert = [NSAlert alertWithMessageText:Str(@"UnsupportedMachine")
defaultButton:Str(@"OhISee")
alternateButton:nil
otherButton:nil
informativeTextWithFormat:@""];
[alert runModal];
}
+ (void)showCantSwitchToIntegratedOnlyMessage:(NSArray *)taskList
{
NSString *messageKey = [NSString stringWithFormat:@"Can'tSwitchToIntegratedOnly%@", (taskList.count > 1 ? @"Plural" : @"Singular")];
NSMutableString *descriptionText = [[NSMutableString alloc] init];
for (NSString *taskName in taskList)
[descriptionText appendFormat:@"%@\n", taskName];
NSAlert *alert = [NSAlert alertWithMessageText:Str(messageKey)
defaultButton:@"OK"
alternateButton:@"Why?"
otherButton:nil
informativeTextWithFormat:@"%@", descriptionText];
if ([alert runModal] == NSAlertAlternateReturn)
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:kIntegratedOnlyMessageExplanationURL]];
}
+ (BOOL)notificationCenterIsAvailable
{
return !!NSClassFromString(@"NSUserNotification");
}
#pragma mark - GrowlApplicationBridgeDelegate protocol
- (NSDictionary *)registrationDictionaryForGrowl
{
return [NSDictionary dictionaryWithContentsOfFile:
[[NSBundle mainBundle] pathForResource:@"Growl Registration Ticket"
ofType:@"growlRegDict"]];
}
#pragma mark - NSUserNotificationCenterDelegate protocol
- (void)userNotificationCenter:(NSUserNotificationCenter *)center didDeliverNotification:(NSUserNotification *)notification
{
[center removeDeliveredNotification:notification];
}
#pragma mark - Private helpers
+ (NSString *)_keyForNotificationType:(GSGPUType)type
{
if (type == GSGPUTypeIntegrated || type == GSGPUTypeDiscrete)
return kGPUChangedNotificationKey;
assert(false); // We shouldn't ever get here.
return nil;
}
@end