source: trunk/macosx/GroupsPrefsController.m @ 7326

Last change on this file since 7326 was 7326, checked in by livings124, 12 years ago

load the rules table before showing the group rules sheet

  • Property svn:keywords set to Date Rev Author Id
File size: 17.7 KB
Line 
1/******************************************************************************
2 * $Id: GroupsPrefsController.m 7326 2008-12-09 00:51:08Z 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 "GroupsPrefsController.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 GroupsPrefsController (Private)
37
38- (void) updateSelectedGroup;
39
40@end
41
42@implementation GroupsPrefsController
43
44- (void) awakeFromNib
45{
46    [fTableView registerForDraggedTypes: [NSArray arrayWithObject: GROUP_TABLE_VIEW_DATA_TYPE]];
47   
48    if (![NSApp isOnLeopardOrBetter])
49    {
50        [fAddRemoveControl sizeToFit];
51        [fAddRemoveControl setLabel: @"+" forSegment: ADD_TAG];
52        [fAddRemoveControl setLabel: @"-" forSegment: REMOVE_TAG];
53        [fGroupRulesPrefsContainer setHidden: YES]; //get rid of container when 10.5-only
54    }
55   
56    [fRulesSheetOKButton setStringValue: NSLocalizedString(@"OK", "Groups -> rule editor -> button")];
57    [fRulesSheetCancelButton setStringValue: NSLocalizedString(@"Cancel", "Groups -> rule editor -> button")];
58    [fRulesSheetDescriptionField setStringValue: NSLocalizedString(@"All criteria must be met to assign a transfer on add.",
59                                                    "Groups -> rule editor -> button")];
60   
61    [fSelectedColorView addObserver: self forKeyPath: @"color" options: 0 context: NULL];
62   
63    [self updateSelectedGroup];
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 updateSelectedGroup];
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        NSInteger oldRow = [indexes firstIndex], selectedRow = [fTableView selectedRow];
136       
137        [[GroupsController groups] moveGroupAtRow: oldRow toRow: newRow];
138       
139        if (oldRow < newRow)
140            newRow--;
141       
142        if (selectedRow == oldRow)
143            selectedRow = newRow;
144        else if (selectedRow > oldRow && selectedRow <= newRow)
145            selectedRow--;
146        else if (selectedRow < oldRow && selectedRow >= newRow)
147            selectedRow++;
148        else;
149       
150        [fTableView selectRow: selectedRow byExtendingSelection: NO];
151        [fTableView reloadData];
152    }
153   
154    return YES;
155}
156
157- (void) addRemoveGroup: (id) sender
158{
159    [[NSColorPanel sharedColorPanel] close];
160
161    NSInteger row;
162   
163    switch ([[sender cell] tagForSegment: [sender selectedSegment]])
164    {
165        case ADD_TAG:
166            [[GroupsController groups] addNewGroup];
167           
168            [fTableView reloadData];
169           
170            row = [fTableView numberOfRows]-1;
171            [fTableView selectRow: row byExtendingSelection: NO];
172            [fTableView scrollRowToVisible: row];
173           
174            [[fSelectedColorNameField window] makeFirstResponder: fSelectedColorNameField];
175           
176            break;
177       
178        case REMOVE_TAG:
179            row = [fTableView selectedRow];
180            [[GroupsController groups] removeGroupWithRowIndex: row];           
181                       
182            [fTableView reloadData];
183           
184            NSInteger selectedRow = [fTableView selectedRow];
185            if (selectedRow != -1)
186                [fTableView scrollRowToVisible: selectedRow];
187           
188            break;
189    }
190   
191    [self updateSelectedGroup];
192}
193
194- (void) customDownloadLocationSheetShow: (id) sender
195{
196    NSOpenPanel * panel = [NSOpenPanel openPanel];
197
198    [panel setPrompt: NSLocalizedString(@"Select", "Preferences -> Open panel prompt")];
199    [panel setAllowsMultipleSelection: NO];
200    [panel setCanChooseFiles: NO];
201    [panel setCanChooseDirectories: YES];
202    [panel setCanCreateDirectories: YES];
203
204    [panel beginSheetForDirectory: nil file: nil types: nil
205        modalForWindow: [fCustomLocationPopUp window] modalDelegate: self didEndSelector:
206        @selector(customDownloadLocationSheetClosed:returnCode:contextInfo:) contextInfo: nil];
207}
208
209- (IBAction) toggleUseCustomDownloadLocation: (id) sender
210{
211    NSInteger index = [[GroupsController groups] indexForRow: [fTableView selectedRow]];
212    if ([fCustomLocationEnableCheck state] == NSOnState)
213    {
214        if ([[GroupsController groups] customDownloadLocationForIndex: index])
215            [[GroupsController groups] setUsesCustomDownloadLocation: YES forIndex: index];
216        else
217            [self customDownloadLocationSheetShow: nil];
218    }
219    else
220        [[GroupsController groups] setUsesCustomDownloadLocation: NO forIndex: index];
221
222    [fCustomLocationPopUp setEnabled: ([fCustomLocationEnableCheck state] == NSOnState)];
223}
224
225- (void) customDownloadLocationSheetClosed: (NSOpenPanel *) openPanel returnCode: (int) code contextInfo: (void *) info
226{
227    NSInteger index = [[GroupsController groups] indexForRow: [fTableView selectedRow]];
228    if (code == NSOKButton)
229    {
230        NSString * path = [[openPanel filenames] objectAtIndex: 0];
231        [[GroupsController groups] setCustomDownloadLocation: path forIndex: index];
232        [[GroupsController groups] setUsesCustomDownloadLocation: YES forIndex: index];
233        [self updateSelectedGroup]; //update the popup's icon/title
234    }
235    else
236    {
237        if (![[GroupsController groups] customDownloadLocationForIndex: index])
238        {
239            [[GroupsController groups] setUsesCustomDownloadLocation: NO forIndex: index];
240            [fCustomLocationEnableCheck setState: NSOffState];
241            [fCustomLocationPopUp setEnabled: NO];
242        }
243    }
244
245    [fCustomLocationPopUp selectItemAtIndex: 0];
246}
247
248#pragma mark -
249#pragma mark Rule editor
250
251- (IBAction) toggleUseAutoAssignRules: (id) sender;
252{
253    NSInteger index = [[GroupsController groups] indexForRow: [fTableView selectedRow]];
254    if ([fAutoAssignRulesEnableCheck state] == NSOnState)
255    {
256        if ([[GroupsController groups] autoAssignRulesForIndex: index])
257            [[GroupsController groups] setUsesAutoAssignRules: YES forIndex: index];
258        else
259            [self orderFrontRulesSheet: nil];
260    }
261    else
262        [[GroupsController groups] setUsesAutoAssignRules: NO forIndex: index];
263
264    [fAutoAssignRulesEditButton setEnabled: [fAutoAssignRulesEnableCheck state] == NSOnState];
265}
266
267- (IBAction) orderFrontRulesSheet: (id) sender;
268{
269    if (!fGroupRulesSheetWindow)
270        [NSBundle loadNibNamed: @"GroupRules" owner: self];
271
272    [fRuleEditor removeRowsAtIndexes: [NSIndexSet indexSetWithIndexesInRange: NSMakeRange(0, [fRuleEditor numberOfRows])]
273        includeSubrows: YES];
274
275    NSInteger index = [[GroupsController groups] indexForRow: [fTableView selectedRow]];
276    NSArray * rules = [[GroupsController groups] autoAssignRulesForIndex: index];
277    if (rules)
278    {
279        for (NSInteger index = 0; index < [rules count]; index++)
280        {
281            [fRuleEditor addRow: nil];
282            [fRuleEditor setCriteria: [rules objectAtIndex: index] andDisplayValues: [NSArray array] forRowAtIndex: index];
283        }
284    }
285
286    if ([fRuleEditor numberOfRows] == 0)
287        [fRuleEditor addRow: nil];
288   
289    [fRuleEditor reloadCriteria];
290
291    [NSApp beginSheet: fGroupRulesSheetWindow modalForWindow: [fTableView window] modalDelegate: nil didEndSelector: NULL
292        contextInfo: NULL];
293}
294
295- (IBAction) cancelRules: (id) sender;
296{
297    [fGroupRulesSheetWindow orderOut: nil];
298    [NSApp endSheet: fGroupRulesSheetWindow];
299   
300    NSInteger index = [[GroupsController groups] indexForRow: [fTableView selectedRow]];
301    if (![[GroupsController groups] autoAssignRulesForIndex: index])
302    {
303        [[GroupsController groups] setUsesAutoAssignRules: NO forIndex: index];
304        [fAutoAssignRulesEnableCheck setState: NO];
305        [fAutoAssignRulesEditButton setEnabled: NO];
306    }
307}
308
309- (IBAction) saveRules: (id) sender;
310{
311    [fGroupRulesSheetWindow orderOut: nil];
312    [NSApp endSheet: fGroupRulesSheetWindow];
313   
314    NSInteger index = [[GroupsController groups] indexForRow: [fTableView selectedRow]];
315    [[GroupsController groups] setUsesAutoAssignRules: YES forIndex: index];
316   
317    NSMutableArray * rules = [NSMutableArray arrayWithCapacity: [fRuleEditor numberOfRows]];
318    for (NSInteger index = 0; index < [fRuleEditor numberOfRows]; ++index)
319    {
320        NSString * string = [[[fRuleEditor displayValuesForRow: index] objectAtIndex: 2] stringValue];
321        if (string && [string length] > 0)
322        {
323            NSMutableArray * rule = [[[fRuleEditor criteriaForRow: index] mutableCopy] autorelease];
324            [rule replaceObjectAtIndex: 2 withObject: string];
325            [rules addObject: rule];
326        }
327    }
328   
329    [[GroupsController groups] setAutoAssignRules: rules forIndex: index];
330    [fAutoAssignRulesEnableCheck setState: [[GroupsController groups] usesAutoAssignRulesForIndex: index]];
331    [fAutoAssignRulesEditButton setEnabled: [fAutoAssignRulesEnableCheck state] == NSOnState];
332}
333
334static NSString * torrentTitleCriteria = @"title";
335static NSString * trackerURLCriteria = @"tracker";
336static NSString * startsWithCriteria = @"begins";
337static NSString * containsCriteria = @"contains";
338static NSString * endsWithCriteria = @"ends";
339
340- (NSInteger) ruleEditor: (NSRuleEditor *) editor numberOfChildrenForCriterion: (id) criterion withRowType: (NSRuleEditorRowType) rowType
341{
342    if (!criterion)
343        return 2;
344    else if ([criterion isEqualToString: torrentTitleCriteria] || [criterion isEqualToString: trackerURLCriteria])
345        return 3;
346    else if ([criterion isEqualToString: startsWithCriteria] || [criterion isEqualToString: containsCriteria]
347                || [criterion isEqualToString: endsWithCriteria])
348        return 1;
349    else
350        return 0;
351}
352
353- (id) ruleEditor: (NSRuleEditor *) editor child: (NSInteger) index forCriterion: (id) criterion
354    withRowType: (NSRuleEditorRowType) rowType
355{
356    if (criterion == nil)
357        return [[NSArray arrayWithObjects: torrentTitleCriteria, trackerURLCriteria, nil] objectAtIndex: index];
358    else if ([criterion isEqualToString: torrentTitleCriteria] || [criterion isEqualToString: trackerURLCriteria])
359        return [[NSArray arrayWithObjects: startsWithCriteria, containsCriteria, endsWithCriteria, nil] objectAtIndex: index];
360    else
361        return @"";
362}
363
364- (id) ruleEditor: (NSRuleEditor *) editor displayValueForCriterion: (id) criterion inRow: (NSInteger) row
365{
366    if ([criterion isEqualToString: torrentTitleCriteria])
367        return NSLocalizedString(@"Torrent Title", "Groups -> rule editor");
368    else if ([criterion isEqualToString: trackerURLCriteria])
369        return NSLocalizedString(@"Tracker URL", "Groups -> rule editor");
370    else if ([criterion isEqualToString: startsWithCriteria])
371        return NSLocalizedString(@"Starts With", "Groups -> rule editor");
372    else if ([criterion isEqualToString: containsCriteria])
373        return NSLocalizedString(@"Contains", "Groups -> rule editor");
374    else if ([criterion isEqualToString: endsWithCriteria])
375        return NSLocalizedString(@"Ends With", "Groups -> rule editor");
376    else
377    {
378        NSTextField * field = [[NSTextField alloc] initWithFrame: NSMakeRect(0, 0, 130, 22)];
379        [field setStringValue: criterion];
380        return [field autorelease];
381    }
382}
383
384- (void) ruleEditorRowsDidChange: (NSNotification *) notification
385{
386    CGFloat rowHeight        = [fRuleEditor rowHeight];
387    NSInteger numberOfRows   = [fRuleEditor numberOfRows];
388    CGFloat ruleEditorHeight = numberOfRows * rowHeight;
389    CGFloat heightDifference = ruleEditorHeight - [fRuleEditor frame].size.height;
390    NSRect windowFrame       = [fRuleEditor window].frame;
391    windowFrame.size.height += heightDifference;
392    windowFrame.origin.y    -= heightDifference;
393    [fRuleEditor.window setFrame: windowFrame display: YES animate: YES];
394}
395
396@end
397
398@implementation GroupsPrefsController (Private)
399
400- (void) updateSelectedGroup
401{
402    [fAddRemoveControl setEnabled: [fTableView numberOfSelectedRows] > 0 forSegment: REMOVE_TAG];
403    if ([fTableView numberOfSelectedRows] == 1)
404    {
405        NSInteger index = [[GroupsController groups] indexForRow: [fTableView selectedRow]];
406        [fSelectedColorView setColor: [[GroupsController groups] colorForIndex: index]];
407        [fSelectedColorView setEnabled: YES];
408        [fSelectedColorNameField setStringValue: [[GroupsController groups] nameForIndex: index]];
409        [fSelectedColorNameField setEnabled: YES];
410        [fCustomLocationEnableCheck setState: [[GroupsController groups] usesCustomDownloadLocationForIndex: index]];
411        [fCustomLocationEnableCheck setEnabled: YES];
412        [fCustomLocationPopUp setEnabled: [fCustomLocationEnableCheck state] == NSOnState];
413        if ([[GroupsController groups] customDownloadLocationForIndex: index])
414        {
415            NSString * location = [[GroupsController groups] customDownloadLocationForIndex: index];
416            ExpandedPathToPathTransformer * pathTransformer = [[[ExpandedPathToPathTransformer alloc] init] autorelease];
417            [[fCustomLocationPopUp itemAtIndex: 0] setTitle: [pathTransformer transformedValue: location]];
418            ExpandedPathToIconTransformer * iconTransformer = [[[ExpandedPathToIconTransformer alloc] init] autorelease];
419            [[fCustomLocationPopUp itemAtIndex: 0] setImage: [iconTransformer transformedValue: location]];
420        }
421        else
422        {
423            [[fCustomLocationPopUp itemAtIndex: 0] setTitle: @""];
424            [[fCustomLocationPopUp itemAtIndex: 0] setImage: nil];
425        }
426
427        [fAutoAssignRulesEnableCheck setState: [[GroupsController groups] usesAutoAssignRulesForIndex: index]];
428        [fAutoAssignRulesEnableCheck setEnabled: YES];
429        [fAutoAssignRulesEditButton setEnabled: ([fAutoAssignRulesEnableCheck state] == NSOnState)];
430    }
431    else
432    {
433        [fSelectedColorView setColor: [NSColor whiteColor]];
434        [fSelectedColorView setEnabled: NO];
435        [fSelectedColorNameField setStringValue: @""];
436        [fSelectedColorNameField setEnabled: NO];
437        [fCustomLocationEnableCheck setEnabled: NO];
438        [fCustomLocationPopUp setEnabled: NO];
439        [fAutoAssignRulesEnableCheck setEnabled: NO];
440        [fAutoAssignRulesEditButton setEnabled: NO];
441    }
442}
443
444@end
Note: See TracBrowser for help on using the repository browser.