source: trunk/macosx/GroupsPrefsController.m @ 13108

Last change on this file since 13108 was 13108, checked in by livings124, 10 years ago

eliminate all warnings by switching NSOpenPanel/NSSavePanels to use completion handler blocks

  • Property svn:keywords set to Date Rev Author Id
File size: 13.8 KB
Line 
1/******************************************************************************
2 * $Id: GroupsPrefsController.m 13108 2011-12-11 22:31:01Z livings124 $
3 *
4 * Copyright (c) 2007-2011 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 "GroupsPrefsController.h"
26#import "GroupsController.h"
27#import "ExpandedPathToPathTransformer.h"
28#import "ExpandedPathToIconTransformer.h"
29
30#define GROUP_TABLE_VIEW_DATA_TYPE @"GroupTableViewDataType"
31
32#define ADD_TAG 0
33#define REMOVE_TAG 1
34
35@interface GroupsPrefsController (Private)
36
37- (void) updateSelectedGroup;
38- (void) refreshCustomLocationWithSingleGroup;
39
40@end
41
42@implementation GroupsPrefsController
43
44- (void) awakeFromNib
45{
46    [fTableView registerForDraggedTypes: [NSArray arrayWithObject: GROUP_TABLE_VIEW_DATA_TYPE]];
47   
48    [fSelectedColorView addObserver: self forKeyPath: @"color" options: 0 context: NULL];
49   
50    [self updateSelectedGroup];
51}
52
53- (NSInteger) numberOfRowsInTableView: (NSTableView *) tableview
54{
55    return [[GroupsController groups] numberOfGroups];
56}
57
58- (id) tableView: (NSTableView *) tableView objectValueForTableColumn: (NSTableColumn *) tableColumn row: (NSInteger) row
59{
60    GroupsController * groupsController = [GroupsController groups];
61    NSInteger groupsIndex = [groupsController indexForRow: row];
62   
63    NSString * identifier = [tableColumn identifier];
64    if ([identifier isEqualToString: @"Color"])
65        return [groupsController imageForIndex: groupsIndex];
66    else
67        return [groupsController nameForIndex: groupsIndex];
68}
69
70- (void) tableViewSelectionDidChange: (NSNotification *) notification
71{
72    [self updateSelectedGroup];
73}
74
75- (void) observeValueForKeyPath: (NSString *) keyPath ofObject: (id) object change: (NSDictionary *) change context: (void *) context
76{
77    if (object == fSelectedColorView && [fTableView numberOfSelectedRows] == 1)
78    {
79       NSInteger index = [[GroupsController groups] indexForRow: [fTableView selectedRow]];
80       [[GroupsController groups] setColor: [fSelectedColorView color] forIndex: index];
81       [fTableView setNeedsDisplay: YES];
82    }
83}
84
85- (void) controlTextDidEndEditing: (NSNotification *) notification
86{
87    if ([notification object] == fSelectedColorNameField)
88    {
89       NSInteger index = [[GroupsController groups] indexForRow: [fTableView selectedRow]];
90       [[GroupsController groups] setName: [fSelectedColorNameField stringValue] forIndex: index];
91       [fTableView setNeedsDisplay: YES];
92    }
93}
94
95- (BOOL) tableView: (NSTableView *) tableView writeRowsWithIndexes: (NSIndexSet *) rowIndexes toPasteboard: (NSPasteboard *) pboard
96{
97    [pboard declareTypes: [NSArray arrayWithObject: GROUP_TABLE_VIEW_DATA_TYPE] owner: self];
98    [pboard setData: [NSKeyedArchiver archivedDataWithRootObject: rowIndexes] forType: GROUP_TABLE_VIEW_DATA_TYPE];
99    return YES;
100}
101
102- (NSDragOperation) tableView: (NSTableView *) tableView validateDrop: (id <NSDraggingInfo>) info
103    proposedRow: (NSInteger) row proposedDropOperation: (NSTableViewDropOperation) operation
104{
105    NSPasteboard * pasteboard = [info draggingPasteboard];
106    if ([[pasteboard types] containsObject: GROUP_TABLE_VIEW_DATA_TYPE])
107    {
108        [fTableView setDropRow: row dropOperation: NSTableViewDropAbove];
109        return NSDragOperationGeneric;
110    }
111   
112    return NSDragOperationNone;
113}
114
115- (BOOL) tableView: (NSTableView *) tableView acceptDrop: (id <NSDraggingInfo>) info row: (NSInteger) newRow
116    dropOperation: (NSTableViewDropOperation) operation
117{
118    NSPasteboard * pasteboard = [info draggingPasteboard];
119    if ([[pasteboard types] containsObject: GROUP_TABLE_VIEW_DATA_TYPE])
120    {
121        NSIndexSet * indexes = [NSKeyedUnarchiver unarchiveObjectWithData: [pasteboard dataForType: GROUP_TABLE_VIEW_DATA_TYPE]];
122        NSInteger oldRow = [indexes firstIndex], selectedRow = [fTableView selectedRow];
123       
124        [[GroupsController groups] moveGroupAtRow: oldRow toRow: newRow];
125       
126        if (oldRow < newRow)
127            newRow--;
128       
129        if (selectedRow == oldRow)
130            selectedRow = newRow;
131        else if (selectedRow > oldRow && selectedRow <= newRow)
132            selectedRow--;
133        else if (selectedRow < oldRow && selectedRow >= newRow)
134            selectedRow++;
135        else;
136       
137        [fTableView selectRowIndexes: [NSIndexSet indexSetWithIndex: selectedRow] byExtendingSelection: NO];
138        [fTableView reloadData];
139    }
140   
141    return YES;
142}
143
144- (void) addRemoveGroup: (id) sender
145{
146    [[NSColorPanel sharedColorPanel] close];
147
148    NSInteger row;
149   
150    switch ([[sender cell] tagForSegment: [sender selectedSegment]])
151    {
152        case ADD_TAG:
153            [[GroupsController groups] addNewGroup];
154           
155            [fTableView reloadData];
156           
157            row = [fTableView numberOfRows]-1;
158            [fTableView selectRowIndexes: [NSIndexSet indexSetWithIndex: row] byExtendingSelection: NO];
159            [fTableView scrollRowToVisible: row];
160           
161            [[fSelectedColorNameField window] makeFirstResponder: fSelectedColorNameField];
162           
163            break;
164       
165        case REMOVE_TAG:
166            row = [fTableView selectedRow];
167            [[GroupsController groups] removeGroupWithRowIndex: row];           
168                       
169            [fTableView reloadData];
170           
171            if ([fTableView numberOfRows] > 0)
172            {
173                if (row == [fTableView numberOfRows])
174                {
175                    --row;
176                    [fTableView selectRowIndexes: [NSIndexSet indexSetWithIndex: row] byExtendingSelection: NO];
177                }
178               
179                [fTableView scrollRowToVisible: row];
180            }
181           
182            break;
183    }
184   
185    [self updateSelectedGroup];
186}
187
188- (void) customDownloadLocationSheetShow: (id) sender
189{
190    NSOpenPanel * panel = [NSOpenPanel openPanel];
191
192    [panel setPrompt: NSLocalizedString(@"Select", "Preferences -> Open panel prompt")];
193    [panel setAllowsMultipleSelection: NO];
194    [panel setCanChooseFiles: NO];
195    [panel setCanChooseDirectories: YES];
196    [panel setCanCreateDirectories: YES];
197   
198    [panel beginSheetModalForWindow: [fCustomLocationPopUp window] completionHandler: ^(NSInteger result) {
199        const NSInteger index = [[GroupsController groups] indexForRow: [fTableView selectedRow]];
200        if (result == NSFileHandlingPanelOKButton)
201        {
202            NSString * path = [[[panel URLs] objectAtIndex: 0] path];
203            [[GroupsController groups] setCustomDownloadLocation: path forIndex: index];
204            [[GroupsController groups] setUsesCustomDownloadLocation: YES forIndex: index];
205        }
206        else
207        {
208            if (![[GroupsController groups] customDownloadLocationForIndex: index])
209                [[GroupsController groups] setUsesCustomDownloadLocation: NO forIndex: index];
210        }
211       
212        [self refreshCustomLocationWithSingleGroup];
213       
214        [fCustomLocationPopUp selectItemAtIndex: 0];
215    }];
216}
217
218- (IBAction) toggleUseCustomDownloadLocation: (id) sender
219{
220    NSInteger index = [[GroupsController groups] indexForRow: [fTableView selectedRow]];
221    if ([fCustomLocationEnableCheck state] == NSOnState)
222    {
223        if ([[GroupsController groups] customDownloadLocationForIndex: index])
224            [[GroupsController groups] setUsesCustomDownloadLocation: YES forIndex: index];
225        else
226            [self customDownloadLocationSheetShow: nil];
227    }
228    else
229        [[GroupsController groups] setUsesCustomDownloadLocation: NO forIndex: index];
230
231    [fCustomLocationPopUp setEnabled: ([fCustomLocationEnableCheck state] == NSOnState)];
232}
233
234#pragma mark -
235#pragma mark Rule editor
236
237- (IBAction) toggleUseAutoAssignRules: (id) sender;
238{
239    NSInteger index = [[GroupsController groups] indexForRow: [fTableView selectedRow]];
240    if ([fAutoAssignRulesEnableCheck state] == NSOnState)
241    {
242        if ([[GroupsController groups] autoAssignRulesForIndex: index])
243            [[GroupsController groups] setUsesAutoAssignRules: YES forIndex: index];
244        else
245            [self orderFrontRulesSheet: nil];
246    }
247    else
248        [[GroupsController groups] setUsesAutoAssignRules: NO forIndex: index];
249
250    [fAutoAssignRulesEditButton setEnabled: [fAutoAssignRulesEnableCheck state] == NSOnState];
251}
252
253- (IBAction) orderFrontRulesSheet: (id) sender;
254{
255    if (!fGroupRulesSheetWindow)
256        [NSBundle loadNibNamed: @"GroupRules" owner: self];
257
258    NSInteger index = [[GroupsController groups] indexForRow: [fTableView selectedRow]];
259        NSPredicate *predicate = [[GroupsController groups] autoAssignRulesForIndex: index];
260        [fRuleEditor setObjectValue: predicate];
261       
262    if ([fRuleEditor numberOfRows] == 0)
263        [fRuleEditor addRow: nil];
264       
265    [NSApp beginSheet: fGroupRulesSheetWindow modalForWindow: [fTableView window] modalDelegate: nil didEndSelector: NULL
266        contextInfo: NULL];
267}
268
269- (IBAction) cancelRules: (id) sender;
270{
271    [fGroupRulesSheetWindow orderOut: nil];
272    [NSApp endSheet: fGroupRulesSheetWindow];
273   
274    NSInteger index = [[GroupsController groups] indexForRow: [fTableView selectedRow]];
275    if (![[GroupsController groups] autoAssignRulesForIndex: index])
276    {
277        [[GroupsController groups] setUsesAutoAssignRules: NO forIndex: index];
278        [fAutoAssignRulesEnableCheck setState: NO];
279        [fAutoAssignRulesEditButton setEnabled: NO];
280    }
281}
282
283- (IBAction) saveRules: (id) sender;
284{
285    [fGroupRulesSheetWindow orderOut: nil];
286    [NSApp endSheet: fGroupRulesSheetWindow];
287   
288    NSInteger index = [[GroupsController groups] indexForRow: [fTableView selectedRow]];
289    [[GroupsController groups] setUsesAutoAssignRules: YES forIndex: index];
290   
291    NSPredicate * predicate = [fRuleEditor objectValue];
292    [[GroupsController groups] setAutoAssignRules: predicate forIndex: index];
293       
294    [fAutoAssignRulesEnableCheck setState: [[GroupsController groups] usesAutoAssignRulesForIndex: index]];
295    [fAutoAssignRulesEditButton setEnabled: [fAutoAssignRulesEnableCheck state] == NSOnState];
296}
297
298- (void) ruleEditorRowsDidChange: (NSNotification *) notification
299{
300    const CGFloat heightDifference = [fRuleEditor numberOfRows] * [fRuleEditor rowHeight] - [fRuleEditor frame].size.height;
301    NSRect windowFrame = [fRuleEditor window].frame;
302    windowFrame.size.height += heightDifference;
303    windowFrame.origin.y -= heightDifference;
304   
305    [fRuleEditor.window setFrame: windowFrame display: YES animate: YES];
306}
307
308@end
309
310@implementation GroupsPrefsController (Private)
311
312- (void) updateSelectedGroup
313{
314    [fAddRemoveControl setEnabled: [fTableView numberOfSelectedRows] > 0 forSegment: REMOVE_TAG];
315    if ([fTableView numberOfSelectedRows] == 1)
316    {
317        const NSInteger index = [[GroupsController groups] indexForRow: [fTableView selectedRow]];
318        [fSelectedColorView setColor: [[GroupsController groups] colorForIndex: index]];
319        [fSelectedColorView setEnabled: YES];
320        [fSelectedColorNameField setStringValue: [[GroupsController groups] nameForIndex: index]];
321        [fSelectedColorNameField setEnabled: YES];
322       
323        [self refreshCustomLocationWithSingleGroup];
324
325        [fAutoAssignRulesEnableCheck setState: [[GroupsController groups] usesAutoAssignRulesForIndex: index]];
326        [fAutoAssignRulesEnableCheck setEnabled: YES];
327        [fAutoAssignRulesEditButton setEnabled: ([fAutoAssignRulesEnableCheck state] == NSOnState)];
328    }
329    else
330    {
331        [fSelectedColorView setColor: [NSColor whiteColor]];
332        [fSelectedColorView setEnabled: NO];
333        [fSelectedColorNameField setStringValue: @""];
334        [fSelectedColorNameField setEnabled: NO];
335        [fCustomLocationEnableCheck setEnabled: NO];
336        [fCustomLocationPopUp setEnabled: NO];
337        [fAutoAssignRulesEnableCheck setEnabled: NO];
338        [fAutoAssignRulesEditButton setEnabled: NO];
339    }
340}
341
342- (void) refreshCustomLocationWithSingleGroup
343{
344    const NSInteger index = [[GroupsController groups] indexForRow: [fTableView selectedRow]];
345   
346    const BOOL hasCustomLocation = [[GroupsController groups] usesCustomDownloadLocationForIndex: index];
347    [fCustomLocationEnableCheck setState: hasCustomLocation];
348    [fCustomLocationEnableCheck setEnabled: YES];
349    [fCustomLocationPopUp setEnabled: hasCustomLocation];
350   
351    NSString * location = [[GroupsController groups] customDownloadLocationForIndex: index];
352    if (location)
353    {
354        ExpandedPathToPathTransformer * pathTransformer = [[[ExpandedPathToPathTransformer alloc] init] autorelease];
355        [[fCustomLocationPopUp itemAtIndex: 0] setTitle: [pathTransformer transformedValue: location]];
356        ExpandedPathToIconTransformer * iconTransformer = [[[ExpandedPathToIconTransformer alloc] init] autorelease];
357        [[fCustomLocationPopUp itemAtIndex: 0] setImage: [iconTransformer transformedValue: location]];
358    }
359    else
360    {
361        [[fCustomLocationPopUp itemAtIndex: 0] setTitle: @""];
362        [[fCustomLocationPopUp itemAtIndex: 0] setImage: nil];
363    }
364}
365
366@end
Note: See TracBrowser for help on using the repository browser.