-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathPGBookmarkController.m
220 lines (191 loc) · 8.74 KB
/
PGBookmarkController.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
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
/* Copyright © 2007-2008 The Sequential Project. All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal with the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimers.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimers in the
documentation and/or other materials provided with the distribution.
3. Neither the name of The Sequential Project nor the names of its
contributors may be used to endorse or promote products derived from
this Software without specific prior written permission.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS WITH THE SOFTWARE. */
#import "PGBookmarkController.h"
#import <Carbon/Carbon.h>
// Models
#import "PGResourceIdentifier.h"
#import "PGBookmark.h"
// Controllers
#import "PGDocumentController.h"
// Categories
#import "NSObjectAdditions.h"
#import "NSMenuItemAdditions.h"
static NSString *const PGPausedDocumentsKey = @"PGPausedDocuments3";
static NSString *const PGPausedDocumentsDeprecated2Key = @"PGPausedDocuments2"; // Deprecated after 1.3.2.
static NSString *const PGPausedDocumentsDeprecatedKey = @"PGPausedDocuments"; // Deprecated after 1.2.2.
static PGBookmarkController *sharedBookmarkController = nil;
static OSStatus PGBookmarkControllerFlagsChanged(EventHandlerCallRef inHandlerCallRef, EventRef inEvent, void *inUserData)
{
[(PGBookmarkController *)inUserData setDeletesBookmarks:!!(GetCurrentEventKeyModifiers() & optionKey)];
return noErr;
}
@interface PGBookmarkController (Private)
- (void)_updateMenuItemForBookmark:(PGBookmark *)aBookmark;
- (void)_removeBookmarkAtIndex:(unsigned)index; // Removes without updating.
- (void)_saveBookmarks;
@end
@implementation PGBookmarkController
+ (id)sharedBookmarkController
{
return sharedBookmarkController ? sharedBookmarkController : [[[self alloc] init] autorelease];
}
#pragma mark Instance Methods
- (IBAction)open:(id)sender
{
PGBookmark *const bookmark = [sender representedObject];
if(!_deletesBookmarks && [bookmark isValid]) {
[[PGDocumentController sharedDocumentController] openDocumentWithBookmark:bookmark display:YES];
return;
}
NSAlert *const alert = [[[NSAlert alloc] init] autorelease];
[alert setAlertStyle:NSInformationalAlertStyle];
NSButton *const deleteButton = [alert addButtonWithTitle:NSLocalizedString(@"Delete Bookmark", nil)];
NSButton *const cancelButton = [alert addButtonWithTitle:NSLocalizedString(@"Cancel", nil)];
if(_deletesBookmarks) {
[alert setMessageText:[NSString stringWithFormat:NSLocalizedString(@"Are you sure you want to delete the bookmark %@?", @"Confirmation dialog when the user intentionally deletes a bookmark. %@ is the bookmarked file's name."), [[bookmark fileIdentifier] displayName]]];
[alert setInformativeText:NSLocalizedString(@"This operation cannot be undone.", @"Confirmation dialog informative text.")];
[deleteButton setKeyEquivalent:@"\r"];
} else {
[alert setMessageText:[NSString stringWithFormat:NSLocalizedString(@"The file referenced by the bookmark %@ could not be found.", @"Bookmarked file could not be found error. %@ is replaced with the missing page's saved filename."), [[bookmark fileIdentifier] displayName]]];
[alert setInformativeText:NSLocalizedString(@"It may have been moved or deleted.", @"Bookmarked file could not be found error informative text.")];
[deleteButton setKeyEquivalent:@""];
[cancelButton setKeyEquivalent:@"\r"];
}
if([alert runModal] == NSAlertFirstButtonReturn) [self removeBookmark:bookmark];
else [self _updateMenuItemForBookmark:bookmark];
}
#pragma mark -
- (void)addBookmark:(PGBookmark *)aBookmark
{
unsigned i;
while((i = [_bookmarks indexOfObject:aBookmark]) != NSNotFound) [self _removeBookmarkAtIndex:i];
[_bookmarks addObject:aBookmark];
[self addMenuItemForBookmark:aBookmark];
[self _saveBookmarks];
}
- (void)removeBookmark:(PGBookmark *)aBookmark
{
if(!aBookmark) return;
[self _removeBookmarkAtIndex:[_bookmarks indexOfObject:aBookmark]];
[self _saveBookmarks];
}
- (void)addMenuItemForBookmark:(PGBookmark *)aBookmark
{
NSParameterAssert(aBookmark);
[emptyMenuItem AE_removeFromMenu];
if([bookmarkMenu numberOfItems]) [[bookmarkMenu itemAtIndex:0] setKeyEquivalent:@""];
NSMenuItem *const item = [[[NSMenuItem alloc] init] autorelease];
[item setTarget:self];
[item setAction:@selector(open:)];
[item setRepresentedObject:aBookmark];
[bookmarkMenu insertItem:item atIndex:0];
[aBookmark AE_addObserver:self selector:@selector(bookmarkDidUpdate:) name:PGBookmarkDidUpdateNotification];
[self _updateMenuItemForBookmark:aBookmark];
}
#pragma mark -
- (BOOL)deletesBookmarks
{
return _deletesBookmarks;
}
- (void)setDeletesBookmarks:(BOOL)flag
{
_deletesBookmarks = flag;
[bookmarkItem setTitle:NSLocalizedString((flag && PGIsTigerOrLater() ? @"Delete..." : @"Resume"), @"The title of the bookmarks menu. Two states.")]; // The alternate state doesn't really work on Panther.
}
#pragma mark -
- (void)bookmarkDidUpdate:(NSNotification *)aNotif
{
NSParameterAssert(aNotif);
[self _updateMenuItemForBookmark:[aNotif object]];
[self _saveBookmarks];
}
#pragma mark Private Protocol
- (void)_updateMenuItemForBookmark:(PGBookmark *)aBookmark
{
int const index = [bookmarkMenu indexOfItemWithRepresentedObject:aBookmark];
if(-1 == index) return; // Fail gracefully.
NSMenuItem *const item = [bookmarkMenu itemAtIndex:index];
if(![aBookmark isValid]) {
[item setAttributedTitle:nil];
[item setTitle:[NSString stringWithFormat:NSLocalizedString(@"Missing File %@", @"Bookmark menu item used when the file named %@ cannot be found."), [[aBookmark fileIdentifier] displayName]]];
return;
}
NSMutableAttributedString *const title = [[[NSMutableAttributedString alloc] init] autorelease];
[title appendAttributedString:[[aBookmark documentIdentifier] attributedStringWithWithAncestory:NO]];
if(![[aBookmark documentIdentifier] isEqual:[aBookmark fileIdentifier]]) {
[[title mutableString] appendFormat:@" %C ", 0x25B9];
[title appendAttributedString:[[aBookmark fileIdentifier] attributedStringWithWithAncestory:NO]];
}
[item setAttributedTitle:title];
}
- (void)_removeBookmarkAtIndex:(unsigned)index
{
[[_bookmarks objectAtIndex:index] AE_removeObserver:self name:PGBookmarkDidUpdateNotification];
[_bookmarks removeObjectAtIndex:index];
[bookmarkMenu removeItemAtIndex:[bookmarkMenu numberOfItems] - index - 1];
if(![_bookmarks count]) [bookmarkMenu addItem:emptyMenuItem];
}
- (void)_saveBookmarks
{
[[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:_bookmarks] forKey:PGPausedDocumentsKey];
}
#pragma mark NSNibAwaking Protocol
- (void)awakeFromNib
{
[emptyMenuItem retain];
PGBookmark *bookmark;
NSEnumerator *const bookmarkEnum = [_bookmarks objectEnumerator];
while((bookmark = [bookmarkEnum nextObject])) [self addMenuItemForBookmark:bookmark];
}
#pragma mark NSObject
- (id)init
{
if((self = [super init])) {
if(!sharedBookmarkController) {
sharedBookmarkController = [self retain];
EventTypeSpec const list[] = {{kEventClassKeyboard, kEventRawKeyModifiersChanged}, {kEventClassMenu, kEventMenuOpening}};
InstallEventHandler(GetUserFocusEventTarget(), PGBookmarkControllerFlagsChanged, 2, list, self, NULL);
}
NSUserDefaults *const defaults = [NSUserDefaults standardUserDefaults];
NSData *bookmarksData = [defaults objectForKey:PGPausedDocumentsKey];
if(!bookmarksData) {
bookmarksData = [defaults objectForKey:PGPausedDocumentsDeprecated2Key];
[defaults removeObjectForKey:PGPausedDocumentsDeprecated2Key];
}
if(!bookmarksData) {
bookmarksData = [defaults objectForKey:PGPausedDocumentsDeprecatedKey];
[defaults removeObjectForKey:PGPausedDocumentsDeprecatedKey];
}
_bookmarks = bookmarksData ? [[NSKeyedUnarchiver unarchiveObjectWithData:bookmarksData] retain] : [[NSMutableArray alloc] init];
[self _saveBookmarks];
}
return self;
}
- (void)dealloc
{
[self AE_removeObserver];
[emptyMenuItem release];
[_bookmarks release];
[super dealloc];
}
@end