1 | /****************************************************************************** |
---|
2 | * $Id: GroupsWindowController.m 7212 2008-11-30 21:23:59Z livings124 $ |
---|
3 | * |
---|
4 | * Copyright (c) 2007-2008 Transmission authors and contributors |
---|
5 | * |
---|
6 | * Permission is hereby granted, free of charge, to any person obtaining a |
---|
7 | * copy of this software and associated documentation files (the "Software"), |
---|
8 | * to deal in the Software without restriction, including without limitation |
---|
9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
---|
10 | * and/or sell copies of the Software, and to permit persons to whom the |
---|
11 | * Software is furnished to do so, subject to the following conditions: |
---|
12 | * |
---|
13 | * The above copyright notice and this permission notice shall be included in |
---|
14 | * all copies or substantial portions of the Software. |
---|
15 | * |
---|
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
---|
21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
22 | * DEALINGS IN THE SOFTWARE. |
---|
23 | *****************************************************************************/ |
---|
24 | |
---|
25 | #import "GroupsWindowController.h" |
---|
26 | #import "GroupsController.h" |
---|
27 | #import "NSApplicationAdditions.h" |
---|
28 | #import "ExpandedPathToPathTransformer.h" |
---|
29 | #import "ExpandedPathToIconTransformer.h" |
---|
30 | |
---|
31 | #define GROUP_TABLE_VIEW_DATA_TYPE @"GroupTableViewDataType" |
---|
32 | |
---|
33 | #define ADD_TAG 0 |
---|
34 | #define REMOVE_TAG 1 |
---|
35 | |
---|
36 | @interface GroupsWindowController (Private) |
---|
37 | |
---|
38 | - (void) updateSelectedColor; |
---|
39 | |
---|
40 | @end |
---|
41 | |
---|
42 | @implementation GroupsWindowController |
---|
43 | |
---|
44 | #warning should this still be a window controller? |
---|
45 | - (void) awakeFromNib |
---|
46 | { |
---|
47 | [[[fTableView tableColumnWithIdentifier: @"Button"] dataCell] setTitle: NSLocalizedString(@"Color", "Groups -> color button")]; |
---|
48 | |
---|
49 | [fTableView registerForDraggedTypes: [NSArray arrayWithObject: GROUP_TABLE_VIEW_DATA_TYPE]]; |
---|
50 | |
---|
51 | if ([NSApp isOnLeopardOrBetter]) |
---|
52 | [[self window] setContentBorderThickness: [[fTableView enclosingScrollView] frame].origin.y forEdge: NSMinYEdge]; |
---|
53 | else |
---|
54 | { |
---|
55 | [fAddRemoveControl sizeToFit]; |
---|
56 | [fAddRemoveControl setLabel: @"+" forSegment: ADD_TAG]; |
---|
57 | [fAddRemoveControl setLabel: @"-" forSegment: REMOVE_TAG]; |
---|
58 | } |
---|
59 | |
---|
60 | [fAddRemoveControl setEnabled: NO forSegment: REMOVE_TAG]; |
---|
61 | [fSelectedColorView addObserver: self forKeyPath: @"color" options: 0 context: NULL]; |
---|
62 | |
---|
63 | [self updateSelectedColor]; |
---|
64 | } |
---|
65 | |
---|
66 | - (NSInteger) numberOfRowsInTableView: (NSTableView *) tableview |
---|
67 | { |
---|
68 | return [[GroupsController groups] numberOfGroups]; |
---|
69 | } |
---|
70 | |
---|
71 | - (id) tableView: (NSTableView *) tableView objectValueForTableColumn: (NSTableColumn *) tableColumn row: (NSInteger) row |
---|
72 | { |
---|
73 | GroupsController * groupsController = [GroupsController groups]; |
---|
74 | NSInteger groupsIndex = [groupsController indexForRow: row]; |
---|
75 | |
---|
76 | NSString * identifier = [tableColumn identifier]; |
---|
77 | if ([identifier isEqualToString: @"Color"]) |
---|
78 | return [groupsController imageForIndex: groupsIndex]; |
---|
79 | else |
---|
80 | return [groupsController nameForIndex: groupsIndex]; |
---|
81 | } |
---|
82 | |
---|
83 | - (void) tableViewSelectionDidChange: (NSNotification *) notification |
---|
84 | { |
---|
85 | [self updateSelectedColor]; |
---|
86 | } |
---|
87 | |
---|
88 | - (void) observeValueForKeyPath: (NSString *) keyPath ofObject: (id) object change: (NSDictionary *) change context: (void *) context |
---|
89 | { |
---|
90 | if (object == fSelectedColorView && [fTableView numberOfSelectedRows] == 1) |
---|
91 | { |
---|
92 | NSInteger index = [[GroupsController groups] indexForRow: [fTableView selectedRow]]; |
---|
93 | [[GroupsController groups] setColor: [fSelectedColorView color] forIndex: index]; |
---|
94 | [fTableView setNeedsDisplay: YES]; |
---|
95 | } |
---|
96 | } |
---|
97 | |
---|
98 | - (void) controlTextDidEndEditing: (NSNotification *) notification |
---|
99 | { |
---|
100 | if ([notification object] == fSelectedColorNameField) |
---|
101 | { |
---|
102 | NSInteger index = [[GroupsController groups] indexForRow: [fTableView selectedRow]]; |
---|
103 | [[GroupsController groups] setName: [fSelectedColorNameField stringValue] forIndex: index]; |
---|
104 | [fTableView setNeedsDisplay: YES]; |
---|
105 | } |
---|
106 | } |
---|
107 | |
---|
108 | - (BOOL) tableView: (NSTableView *) tableView writeRowsWithIndexes: (NSIndexSet *) rowIndexes toPasteboard: (NSPasteboard *) pboard |
---|
109 | { |
---|
110 | [pboard declareTypes: [NSArray arrayWithObject: GROUP_TABLE_VIEW_DATA_TYPE] owner: self]; |
---|
111 | [pboard setData: [NSKeyedArchiver archivedDataWithRootObject: rowIndexes] forType: GROUP_TABLE_VIEW_DATA_TYPE]; |
---|
112 | return YES; |
---|
113 | } |
---|
114 | |
---|
115 | - (NSDragOperation) tableView: (NSTableView *) tableView validateDrop: (id <NSDraggingInfo>) info |
---|
116 | proposedRow: (NSInteger) row proposedDropOperation: (NSTableViewDropOperation) operation |
---|
117 | { |
---|
118 | NSPasteboard * pasteboard = [info draggingPasteboard]; |
---|
119 | if ([[pasteboard types] containsObject: GROUP_TABLE_VIEW_DATA_TYPE]) |
---|
120 | { |
---|
121 | [fTableView setDropRow: row dropOperation: NSTableViewDropAbove]; |
---|
122 | return NSDragOperationGeneric; |
---|
123 | } |
---|
124 | |
---|
125 | return NSDragOperationNone; |
---|
126 | } |
---|
127 | |
---|
128 | - (BOOL) tableView: (NSTableView *) tableView acceptDrop: (id <NSDraggingInfo>) info row: (NSInteger) newRow |
---|
129 | dropOperation: (NSTableViewDropOperation) operation |
---|
130 | { |
---|
131 | NSPasteboard * pasteboard = [info draggingPasteboard]; |
---|
132 | if ([[pasteboard types] containsObject: GROUP_TABLE_VIEW_DATA_TYPE]) |
---|
133 | { |
---|
134 | NSIndexSet * indexes = [NSKeyedUnarchiver unarchiveObjectWithData: [pasteboard dataForType: GROUP_TABLE_VIEW_DATA_TYPE]], |
---|
135 | * selectedIndexes = [[GroupsController groups] moveGroupsAtRowIndexes: indexes toRow: newRow |
---|
136 | oldSelected: [fTableView selectedRowIndexes]]; |
---|
137 | |
---|
138 | [fTableView selectRowIndexes: selectedIndexes byExtendingSelection: NO]; |
---|
139 | [fTableView reloadData]; |
---|
140 | } |
---|
141 | |
---|
142 | return YES; |
---|
143 | } |
---|
144 | |
---|
145 | - (void) addRemoveGroup: (id) sender |
---|
146 | { |
---|
147 | [[NSColorPanel sharedColorPanel] close]; |
---|
148 | |
---|
149 | NSInteger row; |
---|
150 | |
---|
151 | switch ([[sender cell] tagForSegment: [sender selectedSegment]]) |
---|
152 | { |
---|
153 | case ADD_TAG: |
---|
154 | [[GroupsController groups] addNewGroup]; |
---|
155 | |
---|
156 | [fTableView reloadData]; |
---|
157 | |
---|
158 | row = [fTableView numberOfRows]-1; |
---|
159 | [fTableView selectRow: row byExtendingSelection: NO]; |
---|
160 | [fTableView scrollRowToVisible: row]; |
---|
161 | |
---|
162 | [[fSelectedColorNameField window] makeFirstResponder: fSelectedColorNameField]; |
---|
163 | |
---|
164 | break; |
---|
165 | |
---|
166 | case REMOVE_TAG: |
---|
167 | row = [fTableView selectedRow]; |
---|
168 | [[GroupsController groups] removeGroupWithRowIndex: row]; |
---|
169 | |
---|
170 | [fTableView reloadData]; |
---|
171 | |
---|
172 | NSInteger selectedRow = [fTableView selectedRow]; |
---|
173 | if (selectedRow != -1) |
---|
174 | [fTableView scrollRowToVisible: selectedRow]; |
---|
175 | |
---|
176 | break; |
---|
177 | } |
---|
178 | } |
---|
179 | |
---|
180 | - (void) customDownloadLocationSheetShow: (id) sender |
---|
181 | { |
---|
182 | NSOpenPanel * panel = [NSOpenPanel openPanel]; |
---|
183 | |
---|
184 | [panel setPrompt: NSLocalizedString(@"Select", "Preferences -> Open panel prompt")]; |
---|
185 | [panel setAllowsMultipleSelection: NO]; |
---|
186 | [panel setCanChooseFiles: NO]; |
---|
187 | [panel setCanChooseDirectories: YES]; |
---|
188 | [panel setCanCreateDirectories: YES]; |
---|
189 | |
---|
190 | [panel beginSheetForDirectory: nil file: nil types: nil |
---|
191 | modalForWindow: [fCustomLocationPopUp window] modalDelegate: self didEndSelector: |
---|
192 | @selector(customDownloadLocationSheetClosed:returnCode:contextInfo:) contextInfo: nil]; |
---|
193 | } |
---|
194 | |
---|
195 | - (IBAction) toggleUseCustomDownloadLocation: (id) sender |
---|
196 | { |
---|
197 | NSInteger index = [[GroupsController groups] indexForRow: [fTableView selectedRow]]; |
---|
198 | if ([fCustomLocationEnableCheck state] == NSOnState) |
---|
199 | { |
---|
200 | if ([[GroupsController groups] customDownloadLocationForIndex: index]) |
---|
201 | [[GroupsController groups] setUsesCustomDownloadLocation: YES forIndex: index]; |
---|
202 | else |
---|
203 | [self customDownloadLocationSheetShow: nil]; |
---|
204 | } |
---|
205 | else |
---|
206 | [[GroupsController groups] setUsesCustomDownloadLocation: NO forIndex: index]; |
---|
207 | |
---|
208 | [fCustomLocationPopUp setEnabled: ([fCustomLocationEnableCheck state] == NSOnState)]; |
---|
209 | } |
---|
210 | |
---|
211 | - (void) customDownloadLocationSheetClosed: (NSOpenPanel *) openPanel returnCode: (int) code contextInfo: (void *) info |
---|
212 | { |
---|
213 | NSInteger index = [[GroupsController groups] indexForRow: [fTableView selectedRow]]; |
---|
214 | NSString * path = [[GroupsController groups] customDownloadLocationForIndex: index]; |
---|
215 | if (code == NSOKButton) |
---|
216 | { |
---|
217 | path = [[openPanel filenames] objectAtIndex: 0]; |
---|
218 | [[GroupsController groups] setCustomDownloadLocation: path forIndex: index]; |
---|
219 | [[GroupsController groups] setUsesCustomDownloadLocation: YES forIndex: index]; |
---|
220 | [self updateSelectedColor]; // Update the popup's icon/title |
---|
221 | } |
---|
222 | else if (!path) |
---|
223 | { |
---|
224 | [[GroupsController groups] setUsesCustomDownloadLocation: NO forIndex: index]; |
---|
225 | [fCustomLocationEnableCheck setState: NSOffState]; |
---|
226 | [fCustomLocationPopUp setEnabled: NO]; |
---|
227 | } |
---|
228 | |
---|
229 | [fCustomLocationPopUp selectItemAtIndex: 0]; |
---|
230 | } |
---|
231 | |
---|
232 | @end |
---|
233 | |
---|
234 | @implementation GroupsWindowController (Private) |
---|
235 | |
---|
236 | - (void) updateSelectedColor |
---|
237 | { |
---|
238 | [fAddRemoveControl setEnabled: [fTableView numberOfSelectedRows] > 0 forSegment: REMOVE_TAG]; |
---|
239 | if ([fTableView numberOfSelectedRows] == 1) |
---|
240 | { |
---|
241 | NSInteger index = [[GroupsController groups] indexForRow: [fTableView selectedRow]]; |
---|
242 | [fSelectedColorView setColor: [[GroupsController groups] colorForIndex: index]]; |
---|
243 | [fSelectedColorView setEnabled: YES]; |
---|
244 | [fSelectedColorNameField setStringValue: [[GroupsController groups] nameForIndex: index]]; |
---|
245 | [fSelectedColorNameField setEnabled: YES]; |
---|
246 | [fCustomLocationEnableCheck setState: [[GroupsController groups] usesCustomDownloadLocationForIndex: index]]; |
---|
247 | [fCustomLocationPopUp setEnabled: ([fCustomLocationEnableCheck state] == NSOnState)]; |
---|
248 | if ([[GroupsController groups] customDownloadLocationForIndex: index]) |
---|
249 | { |
---|
250 | NSString * location = [[GroupsController groups] customDownloadLocationForIndex: index]; |
---|
251 | ExpandedPathToPathTransformer * pathTransformer = [[[ExpandedPathToPathTransformer alloc] init] autorelease]; |
---|
252 | [[fCustomLocationPopUp itemAtIndex: 0] setTitle: [pathTransformer transformedValue: location]]; |
---|
253 | ExpandedPathToIconTransformer * iconTransformer = [[[ExpandedPathToIconTransformer alloc] init] autorelease]; |
---|
254 | [[fCustomLocationPopUp itemAtIndex: 0] setImage: [iconTransformer transformedValue: location]]; |
---|
255 | } |
---|
256 | else |
---|
257 | { |
---|
258 | [[fCustomLocationPopUp itemAtIndex: 0] setTitle: @""]; |
---|
259 | [[fCustomLocationPopUp itemAtIndex: 0] setImage: nil]; |
---|
260 | } |
---|
261 | } |
---|
262 | else |
---|
263 | { |
---|
264 | [fSelectedColorView setColor: [NSColor whiteColor]]; |
---|
265 | [fSelectedColorView setEnabled: NO]; |
---|
266 | [fSelectedColorNameField setStringValue: @""]; |
---|
267 | [fSelectedColorNameField setEnabled: NO]; |
---|
268 | [fCustomLocationEnableCheck setEnabled: NO]; |
---|
269 | [fCustomLocationPopUp setEnabled: NO]; |
---|
270 | } |
---|
271 | } |
---|
272 | |
---|
273 | @end |
---|