source: trunk/macosx/GroupsPrefsController.m @ 8227

Last change on this file since 8227 was 7659, checked in by livings124, 13 years ago

update the Mac code's copyright dates

  • Property svn:keywords set to Date Rev Author Id
File size: 13.8 KB
Line 
1/******************************************************************************
2 * $Id: GroupsPrefsController.m 7659 2009-01-10 23:37:37Z livings124 $
3 *
4 * Copyright (c) 2007-2009 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
39@end
40
41@implementation GroupsPrefsController
42
43- (void) awakeFromNib
44{
45    [fTableView registerForDraggedTypes: [NSArray arrayWithObject: GROUP_TABLE_VIEW_DATA_TYPE]];
46   
47    [fSelectedColorView addObserver: self forKeyPath: @"color" options: 0 context: NULL];
48   
49    [self updateSelectedGroup];
50}
51
52- (NSInteger) numberOfRowsInTableView: (NSTableView *) tableview
53{
54    return [[GroupsController groups] numberOfGroups];
55}
56
57- (id) tableView: (NSTableView *) tableView objectValueForTableColumn: (NSTableColumn *) tableColumn row: (NSInteger) row
58{
59    GroupsController * groupsController = [GroupsController groups];
60    NSInteger groupsIndex = [groupsController indexForRow: row];
61   
62    NSString * identifier = [tableColumn identifier];
63    if ([identifier isEqualToString: @"Color"])
64        return [groupsController imageForIndex: groupsIndex];
65    else
66        return [groupsController nameForIndex: groupsIndex];
67}
68
69- (void) tableViewSelectionDidChange: (NSNotification *) notification
70{
71    [self updateSelectedGroup];
72}
73
74- (void) observeValueForKeyPath: (NSString *) keyPath ofObject: (id) object change: (NSDictionary *) change context: (void *) context
75{
76    if (object == fSelectedColorView && [fTableView numberOfSelectedRows] == 1)
77    {
78       NSInteger index = [[GroupsController groups] indexForRow: [fTableView selectedRow]];
79       [[GroupsController groups] setColor: [fSelectedColorView color] forIndex: index];
80       [fTableView setNeedsDisplay: YES];
81    }
82}
83
84- (void) controlTextDidEndEditing: (NSNotification *) notification
85{
86    if ([notification object] == fSelectedColorNameField)
87    {
88       NSInteger index = [[GroupsController groups] indexForRow: [fTableView selectedRow]];
89       [[GroupsController groups] setName: [fSelectedColorNameField stringValue] forIndex: index];
90       [fTableView setNeedsDisplay: YES];
91    }
92}
93
94- (BOOL) tableView: (NSTableView *) tableView writeRowsWithIndexes: (NSIndexSet *) rowIndexes toPasteboard: (NSPasteboard *) pboard
95{
96    [pboard declareTypes: [NSArray arrayWithObject: GROUP_TABLE_VIEW_DATA_TYPE] owner: self];
97    [pboard setData: [NSKeyedArchiver archivedDataWithRootObject: rowIndexes] forType: GROUP_TABLE_VIEW_DATA_TYPE];
98    return YES;
99}
100
101- (NSDragOperation) tableView: (NSTableView *) tableView validateDrop: (id <NSDraggingInfo>) info
102    proposedRow: (NSInteger) row proposedDropOperation: (NSTableViewDropOperation) operation
103{
104    NSPasteboard * pasteboard = [info draggingPasteboard];
105    if ([[pasteboard types] containsObject: GROUP_TABLE_VIEW_DATA_TYPE])
106    {
107        [fTableView setDropRow: row dropOperation: NSTableViewDropAbove];
108        return NSDragOperationGeneric;
109    }
110   
111    return NSDragOperationNone;
112}
113
114- (BOOL) tableView: (NSTableView *) tableView acceptDrop: (id <NSDraggingInfo>) info row: (NSInteger) newRow
115    dropOperation: (NSTableViewDropOperation) operation
116{
117    NSPasteboard * pasteboard = [info draggingPasteboard];
118    if ([[pasteboard types] containsObject: GROUP_TABLE_VIEW_DATA_TYPE])
119    {
120        NSIndexSet * indexes = [NSKeyedUnarchiver unarchiveObjectWithData: [pasteboard dataForType: GROUP_TABLE_VIEW_DATA_TYPE]];
121        NSInteger oldRow = [indexes firstIndex], selectedRow = [fTableView selectedRow];
122       
123        [[GroupsController groups] moveGroupAtRow: oldRow toRow: newRow];
124       
125        if (oldRow < newRow)
126            newRow--;
127       
128        if (selectedRow == oldRow)
129            selectedRow = newRow;
130        else if (selectedRow > oldRow && selectedRow <= newRow)
131            selectedRow--;
132        else if (selectedRow < oldRow && selectedRow >= newRow)
133            selectedRow++;
134        else;
135       
136        [fTableView selectRow: selectedRow byExtendingSelection: NO];
137        [fTableView reloadData];
138    }
139   
140    return YES;
141}
142
143- (void) addRemoveGroup: (id) sender
144{
145    [[NSColorPanel sharedColorPanel] close];
146
147    NSInteger row;
148   
149    switch ([[sender cell] tagForSegment: [sender selectedSegment]])
150    {
151        case ADD_TAG:
152            [[GroupsController groups] addNewGroup];
153           
154            [fTableView reloadData];
155           
156            row = [fTableView numberOfRows]-1;
157            [fTableView selectRow: row byExtendingSelection: NO];
158            [fTableView scrollRowToVisible: row];
159           
160            [[fSelectedColorNameField window] makeFirstResponder: fSelectedColorNameField];
161           
162            break;
163       
164        case REMOVE_TAG:
165            row = [fTableView selectedRow];
166            [[GroupsController groups] removeGroupWithRowIndex: row];           
167                       
168            [fTableView reloadData];
169           
170            NSInteger selectedRow = [fTableView selectedRow];
171            if (selectedRow != -1)
172                [fTableView scrollRowToVisible: selectedRow];
173           
174            break;
175    }
176   
177    [self updateSelectedGroup];
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    if (code == NSOKButton)
215    {
216        NSString * path = [[openPanel filenames] objectAtIndex: 0];
217        [[GroupsController groups] setCustomDownloadLocation: path forIndex: index];
218        [[GroupsController groups] setUsesCustomDownloadLocation: YES forIndex: index];
219        [self updateSelectedGroup]; //update the popup's icon/title
220    }
221    else
222    {
223        if (![[GroupsController groups] customDownloadLocationForIndex: index])
224        {
225            [[GroupsController groups] setUsesCustomDownloadLocation: NO forIndex: index];
226            [fCustomLocationEnableCheck setState: NSOffState];
227            [fCustomLocationPopUp setEnabled: NO];
228        }
229    }
230
231    [fCustomLocationPopUp selectItemAtIndex: 0];
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    CGFloat rowHeight        = [fRuleEditor rowHeight];
301    NSInteger numberOfRows   = [fRuleEditor numberOfRows];
302    CGFloat ruleEditorHeight = numberOfRows * rowHeight;
303    CGFloat heightDifference = ruleEditorHeight - [fRuleEditor frame].size.height;
304    NSRect windowFrame       = [fRuleEditor window].frame;
305    windowFrame.size.height += heightDifference;
306    windowFrame.origin.y    -= heightDifference;
307    [fRuleEditor.window setFrame: windowFrame display: YES animate: YES];
308}
309
310@end
311
312@implementation GroupsPrefsController (Private)
313
314- (void) updateSelectedGroup
315{
316    [fAddRemoveControl setEnabled: [fTableView numberOfSelectedRows] > 0 forSegment: REMOVE_TAG];
317    if ([fTableView numberOfSelectedRows] == 1)
318    {
319        NSInteger index = [[GroupsController groups] indexForRow: [fTableView selectedRow]];
320        [fSelectedColorView setColor: [[GroupsController groups] colorForIndex: index]];
321        [fSelectedColorView setEnabled: YES];
322        [fSelectedColorNameField setStringValue: [[GroupsController groups] nameForIndex: index]];
323        [fSelectedColorNameField setEnabled: YES];
324        [fCustomLocationEnableCheck setState: [[GroupsController groups] usesCustomDownloadLocationForIndex: index]];
325        [fCustomLocationEnableCheck setEnabled: YES];
326        [fCustomLocationPopUp setEnabled: [fCustomLocationEnableCheck state] == NSOnState];
327        if ([[GroupsController groups] customDownloadLocationForIndex: index])
328        {
329            NSString * location = [[GroupsController groups] customDownloadLocationForIndex: index];
330            ExpandedPathToPathTransformer * pathTransformer = [[[ExpandedPathToPathTransformer alloc] init] autorelease];
331            [[fCustomLocationPopUp itemAtIndex: 0] setTitle: [pathTransformer transformedValue: location]];
332            ExpandedPathToIconTransformer * iconTransformer = [[[ExpandedPathToIconTransformer alloc] init] autorelease];
333            [[fCustomLocationPopUp itemAtIndex: 0] setImage: [iconTransformer transformedValue: location]];
334        }
335        else
336        {
337            [[fCustomLocationPopUp itemAtIndex: 0] setTitle: @""];
338            [[fCustomLocationPopUp itemAtIndex: 0] setImage: nil];
339        }
340
341        [fAutoAssignRulesEnableCheck setState: [[GroupsController groups] usesAutoAssignRulesForIndex: index]];
342        [fAutoAssignRulesEnableCheck setEnabled: YES];
343        [fAutoAssignRulesEditButton setEnabled: ([fAutoAssignRulesEnableCheck state] == NSOnState)];
344    }
345    else
346    {
347        [fSelectedColorView setColor: [NSColor whiteColor]];
348        [fSelectedColorView setEnabled: NO];
349        [fSelectedColorNameField setStringValue: @""];
350        [fSelectedColorNameField setEnabled: NO];
351        [fCustomLocationEnableCheck setEnabled: NO];
352        [fCustomLocationPopUp setEnabled: NO];
353        [fAutoAssignRulesEnableCheck setEnabled: NO];
354        [fAutoAssignRulesEditButton setEnabled: NO];
355    }
356}
357
358@end
Note: See TracBrowser for help on using the repository browser.