source: trunk/macosx/GroupsPrefsController.m @ 7324

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

add a description to the groups rules sheet

  • Property svn:keywords set to Date Rev Author Id
File size: 17.7 KB
Line 
1/******************************************************************************
2 * $Id: GroupsPrefsController.m 7324 2008-12-08 23:38:01Z 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    [NSApp beginSheet: fGroupRulesSheetWindow
290       modalForWindow: [fTableView window]
291        modalDelegate: nil
292       didEndSelector: NULL
293          contextInfo: NULL];
294}
295
296- (IBAction) cancelRules: (id) sender;
297{
298    [fGroupRulesSheetWindow orderOut: nil];
299    [NSApp endSheet: fGroupRulesSheetWindow];
300   
301    NSInteger index = [[GroupsController groups] indexForRow: [fTableView selectedRow]];
302    if (![[GroupsController groups] autoAssignRulesForIndex: index])
303    {
304        [[GroupsController groups] setUsesAutoAssignRules: NO forIndex: index];
305        [fAutoAssignRulesEnableCheck setState: NO];
306        [fAutoAssignRulesEditButton setEnabled: NO];
307    }
308}
309
310- (IBAction) saveRules: (id) sender;
311{
312    [fGroupRulesSheetWindow orderOut: nil];
313    [NSApp endSheet: fGroupRulesSheetWindow];
314   
315    NSInteger index = [[GroupsController groups] indexForRow: [fTableView selectedRow]];
316    [[GroupsController groups] setUsesAutoAssignRules: YES forIndex: index];
317   
318    NSMutableArray * rules = [NSMutableArray arrayWithCapacity: [fRuleEditor numberOfRows]];
319    for (NSInteger index = 0; index < [fRuleEditor numberOfRows]; ++index)
320    {
321        NSString * string = [[[fRuleEditor displayValuesForRow: index] objectAtIndex: 2] stringValue];
322        if (string && [string length] > 0)
323        {
324            NSMutableArray * rule = [[[fRuleEditor criteriaForRow: index] mutableCopy] autorelease];
325            [rule replaceObjectAtIndex: 2 withObject: string];
326            [rules addObject: rule];
327        }
328    }
329   
330    [[GroupsController groups] setAutoAssignRules: rules forIndex: index];
331    [fAutoAssignRulesEnableCheck setState: [[GroupsController groups] usesAutoAssignRulesForIndex: index]];
332    [fAutoAssignRulesEditButton setEnabled: [fAutoAssignRulesEnableCheck state] == NSOnState];
333}
334
335static NSString * torrentTitleCriteria = @"title";
336static NSString * trackerURLCriteria = @"tracker";
337static NSString * startsWithCriteria = @"begins";
338static NSString * containsCriteria = @"contains";
339static NSString * endsWithCriteria = @"ends";
340
341- (NSInteger) ruleEditor: (NSRuleEditor *) editor numberOfChildrenForCriterion: (id) criterion withRowType: (NSRuleEditorRowType) rowType
342{
343    if (!criterion)
344        return 2;
345    else if ([criterion isEqualToString: torrentTitleCriteria] || [criterion isEqualToString: trackerURLCriteria])
346        return 3;
347    else if ([criterion isEqualToString: startsWithCriteria] || [criterion isEqualToString: containsCriteria]
348                || [criterion isEqualToString: endsWithCriteria])
349        return 1;
350    else
351        return 0;
352}
353
354- (id) ruleEditor: (NSRuleEditor *) editor child: (NSInteger) index forCriterion: (id) criterion
355    withRowType: (NSRuleEditorRowType) rowType
356{
357    if (criterion == nil)
358        return [[NSArray arrayWithObjects: torrentTitleCriteria, trackerURLCriteria, nil] objectAtIndex: index];
359    else if ([criterion isEqualToString: torrentTitleCriteria] || [criterion isEqualToString: trackerURLCriteria])
360        return [[NSArray arrayWithObjects: startsWithCriteria, containsCriteria, endsWithCriteria, nil] objectAtIndex: index];
361    else
362        return @"";
363}
364
365- (id) ruleEditor: (NSRuleEditor *) editor displayValueForCriterion: (id) criterion inRow: (NSInteger) row
366{
367    if ([criterion isEqualToString: torrentTitleCriteria])
368        return NSLocalizedString(@"Torrent Title", "Groups -> rule editor");
369    else if ([criterion isEqualToString: trackerURLCriteria])
370        return NSLocalizedString(@"Tracker URL", "Groups -> rule editor");
371    else if ([criterion isEqualToString: startsWithCriteria])
372        return NSLocalizedString(@"Starts With", "Groups -> rule editor");
373    else if ([criterion isEqualToString: containsCriteria])
374        return NSLocalizedString(@"Contains", "Groups -> rule editor");
375    else if ([criterion isEqualToString: endsWithCriteria])
376        return NSLocalizedString(@"Ends With", "Groups -> rule editor");
377    else
378    {
379        NSTextField * field = [[NSTextField alloc] initWithFrame: NSMakeRect(0, 0, 130, 22)];
380        [field setStringValue: criterion];
381        return [field autorelease];
382    }
383}
384
385- (void) ruleEditorRowsDidChange: (NSNotification *) notification
386{
387    CGFloat rowHeight        = [fRuleEditor rowHeight];
388    NSInteger numberOfRows   = [fRuleEditor numberOfRows];
389    CGFloat ruleEditorHeight = numberOfRows * rowHeight;
390    CGFloat heightDifference = ruleEditorHeight - [fRuleEditor frame].size.height;
391    NSRect windowFrame       = [fRuleEditor window].frame;
392    windowFrame.size.height += heightDifference;
393    windowFrame.origin.y    -= heightDifference;
394    [fRuleEditor.window setFrame: windowFrame display: YES animate: YES];
395}
396
397@end
398
399@implementation GroupsPrefsController (Private)
400
401- (void) updateSelectedGroup
402{
403    [fAddRemoveControl setEnabled: [fTableView numberOfSelectedRows] > 0 forSegment: REMOVE_TAG];
404    if ([fTableView numberOfSelectedRows] == 1)
405    {
406        NSInteger index = [[GroupsController groups] indexForRow: [fTableView selectedRow]];
407        [fSelectedColorView setColor: [[GroupsController groups] colorForIndex: index]];
408        [fSelectedColorView setEnabled: YES];
409        [fSelectedColorNameField setStringValue: [[GroupsController groups] nameForIndex: index]];
410        [fSelectedColorNameField setEnabled: YES];
411        [fCustomLocationEnableCheck setState: [[GroupsController groups] usesCustomDownloadLocationForIndex: index]];
412        [fCustomLocationEnableCheck setEnabled: YES];
413        [fCustomLocationPopUp setEnabled: [fCustomLocationEnableCheck state] == NSOnState];
414        if ([[GroupsController groups] customDownloadLocationForIndex: index])
415        {
416            NSString * location = [[GroupsController groups] customDownloadLocationForIndex: index];
417            ExpandedPathToPathTransformer * pathTransformer = [[[ExpandedPathToPathTransformer alloc] init] autorelease];
418            [[fCustomLocationPopUp itemAtIndex: 0] setTitle: [pathTransformer transformedValue: location]];
419            ExpandedPathToIconTransformer * iconTransformer = [[[ExpandedPathToIconTransformer alloc] init] autorelease];
420            [[fCustomLocationPopUp itemAtIndex: 0] setImage: [iconTransformer transformedValue: location]];
421        }
422        else
423        {
424            [[fCustomLocationPopUp itemAtIndex: 0] setTitle: @""];
425            [[fCustomLocationPopUp itemAtIndex: 0] setImage: nil];
426        }
427
428        [fAutoAssignRulesEnableCheck setState: [[GroupsController groups] usesAutoAssignRulesForIndex: index]];
429        [fAutoAssignRulesEnableCheck setEnabled: YES];
430        [fAutoAssignRulesEditButton setEnabled: ([fAutoAssignRulesEnableCheck state] == NSOnState)];
431    }
432    else
433    {
434        [fSelectedColorView setColor: [NSColor whiteColor]];
435        [fSelectedColorView setEnabled: NO];
436        [fSelectedColorNameField setStringValue: @""];
437        [fSelectedColorNameField setEnabled: NO];
438        [fCustomLocationEnableCheck setEnabled: NO];
439        [fCustomLocationPopUp setEnabled: NO];
440        [fAutoAssignRulesEnableCheck setEnabled: NO];
441        [fAutoAssignRulesEditButton setEnabled: NO];
442    }
443}
444
445@end
Note: See TracBrowser for help on using the repository browser.