source: trunk/macosx/GroupsPrefsController.m @ 7216

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

rename GroupsWindowController?.[hm] to GroupsPrefsController?.[hm] and change it to an NSObject; remove dead code

  • Property svn:keywords set to Date Rev Author Id
File size: 10.4 KB
Line 
1/******************************************************************************
2 * $Id: GroupsPrefsController.m 7216 2008-12-01 01:44:38Z 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) updateSelectedColor;
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    }
54   
55    [fSelectedColorView addObserver: self forKeyPath: @"color" options: 0 context: NULL];
56   
57    [self updateSelectedColor];
58}
59
60- (NSInteger) numberOfRowsInTableView: (NSTableView *) tableview
61{
62    return [[GroupsController groups] numberOfGroups];
63}
64
65- (id) tableView: (NSTableView *) tableView objectValueForTableColumn: (NSTableColumn *) tableColumn row: (NSInteger) row
66{
67    GroupsController * groupsController = [GroupsController groups];
68    NSInteger groupsIndex = [groupsController indexForRow: row];
69   
70    NSString * identifier = [tableColumn identifier];
71    if ([identifier isEqualToString: @"Color"])
72        return [groupsController imageForIndex: groupsIndex];
73    else
74        return [groupsController nameForIndex: groupsIndex];
75}
76
77- (void) tableViewSelectionDidChange: (NSNotification *) notification
78{
79    [self updateSelectedColor];
80}
81
82- (void) observeValueForKeyPath: (NSString *) keyPath ofObject: (id) object change: (NSDictionary *) change context: (void *) context
83{
84    if (object == fSelectedColorView && [fTableView numberOfSelectedRows] == 1)
85    {
86       NSInteger index = [[GroupsController groups] indexForRow: [fTableView selectedRow]];
87       [[GroupsController groups] setColor: [fSelectedColorView color] forIndex: index];
88       [fTableView setNeedsDisplay: YES];
89    }
90}
91
92- (void) controlTextDidEndEditing: (NSNotification *) notification
93{
94    if ([notification object] == fSelectedColorNameField)
95    {
96       NSInteger index = [[GroupsController groups] indexForRow: [fTableView selectedRow]];
97       [[GroupsController groups] setName: [fSelectedColorNameField stringValue] forIndex: index];
98       [fTableView setNeedsDisplay: YES];
99    }
100}
101
102- (BOOL) tableView: (NSTableView *) tableView writeRowsWithIndexes: (NSIndexSet *) rowIndexes toPasteboard: (NSPasteboard *) pboard
103{
104    [pboard declareTypes: [NSArray arrayWithObject: GROUP_TABLE_VIEW_DATA_TYPE] owner: self];
105    [pboard setData: [NSKeyedArchiver archivedDataWithRootObject: rowIndexes] forType: GROUP_TABLE_VIEW_DATA_TYPE];
106    return YES;
107}
108
109- (NSDragOperation) tableView: (NSTableView *) tableView validateDrop: (id <NSDraggingInfo>) info
110    proposedRow: (NSInteger) row proposedDropOperation: (NSTableViewDropOperation) operation
111{
112    NSPasteboard * pasteboard = [info draggingPasteboard];
113    if ([[pasteboard types] containsObject: GROUP_TABLE_VIEW_DATA_TYPE])
114    {
115        [fTableView setDropRow: row dropOperation: NSTableViewDropAbove];
116        return NSDragOperationGeneric;
117    }
118   
119    return NSDragOperationNone;
120}
121
122- (BOOL) tableView: (NSTableView *) tableView acceptDrop: (id <NSDraggingInfo>) info row: (NSInteger) newRow
123    dropOperation: (NSTableViewDropOperation) operation
124{
125    NSPasteboard * pasteboard = [info draggingPasteboard];
126    if ([[pasteboard types] containsObject: GROUP_TABLE_VIEW_DATA_TYPE])
127    {
128        NSIndexSet * indexes = [NSKeyedUnarchiver unarchiveObjectWithData: [pasteboard dataForType: GROUP_TABLE_VIEW_DATA_TYPE]],
129            * selectedIndexes = [[GroupsController groups] moveGroupsAtRowIndexes: indexes toRow: newRow
130                                        oldSelected: [fTableView selectedRowIndexes]];
131       
132        [fTableView selectRowIndexes: selectedIndexes byExtendingSelection: NO];
133        [fTableView reloadData];
134    }
135   
136    return YES;
137}
138
139- (void) addRemoveGroup: (id) sender
140{
141    [[NSColorPanel sharedColorPanel] close];
142
143    NSInteger row;
144   
145    switch ([[sender cell] tagForSegment: [sender selectedSegment]])
146    {
147        case ADD_TAG:
148            [[GroupsController groups] addNewGroup];
149           
150            [fTableView reloadData];
151           
152            row = [fTableView numberOfRows]-1;
153            [fTableView selectRow: row byExtendingSelection: NO];
154            [fTableView scrollRowToVisible: row];
155           
156            [[fSelectedColorNameField window] makeFirstResponder: fSelectedColorNameField];
157           
158            break;
159       
160        case REMOVE_TAG:
161            row = [fTableView selectedRow];
162            [[GroupsController groups] removeGroupWithRowIndex: row];           
163                       
164            [fTableView reloadData];
165           
166            NSInteger selectedRow = [fTableView selectedRow];
167            if (selectedRow != -1)
168                [fTableView scrollRowToVisible: selectedRow];
169           
170            break;
171    }
172}
173
174- (void) customDownloadLocationSheetShow: (id) sender
175{
176    NSOpenPanel * panel = [NSOpenPanel openPanel];
177
178    [panel setPrompt: NSLocalizedString(@"Select", "Preferences -> Open panel prompt")];
179    [panel setAllowsMultipleSelection: NO];
180    [panel setCanChooseFiles: NO];
181    [panel setCanChooseDirectories: YES];
182    [panel setCanCreateDirectories: YES];
183
184    [panel beginSheetForDirectory: nil file: nil types: nil
185        modalForWindow: [fCustomLocationPopUp window] modalDelegate: self didEndSelector:
186        @selector(customDownloadLocationSheetClosed:returnCode:contextInfo:) contextInfo: nil];
187}
188
189- (IBAction) toggleUseCustomDownloadLocation: (id) sender
190{
191    NSInteger index = [[GroupsController groups] indexForRow: [fTableView selectedRow]];
192    if ([fCustomLocationEnableCheck state] == NSOnState)
193    {
194        if ([[GroupsController groups] customDownloadLocationForIndex: index])
195            [[GroupsController groups] setUsesCustomDownloadLocation: YES forIndex: index];
196        else
197            [self customDownloadLocationSheetShow: nil];
198    }
199    else
200        [[GroupsController groups] setUsesCustomDownloadLocation: NO forIndex: index];
201
202    [fCustomLocationPopUp setEnabled: ([fCustomLocationEnableCheck state] == NSOnState)];
203}
204
205- (void) customDownloadLocationSheetClosed: (NSOpenPanel *) openPanel returnCode: (int) code contextInfo: (void *) info
206{
207    NSInteger index = [[GroupsController groups] indexForRow: [fTableView selectedRow]];
208    NSString * path = [[GroupsController groups] customDownloadLocationForIndex: index];
209    if (code == NSOKButton)
210    {
211        path = [[openPanel filenames] objectAtIndex: 0];
212        [[GroupsController groups] setCustomDownloadLocation: path forIndex: index];
213        [[GroupsController groups] setUsesCustomDownloadLocation: YES forIndex: index];
214        [self updateSelectedColor]; // Update the popup's icon/title
215    }
216    else if (!path)
217    {
218        [[GroupsController groups] setUsesCustomDownloadLocation: NO forIndex: index];
219        [fCustomLocationEnableCheck setState: NSOffState];
220        [fCustomLocationPopUp setEnabled: NO];
221    }
222
223    [fCustomLocationPopUp selectItemAtIndex: 0];
224}
225
226@end
227
228@implementation GroupsPrefsController (Private)
229
230- (void) updateSelectedColor
231{
232    [fAddRemoveControl setEnabled: [fTableView numberOfSelectedRows] > 0 forSegment: REMOVE_TAG];
233    if ([fTableView numberOfSelectedRows] == 1)
234    {
235        NSInteger index = [[GroupsController groups] indexForRow: [fTableView selectedRow]];
236        [fSelectedColorView setColor: [[GroupsController groups] colorForIndex: index]];
237        [fSelectedColorView setEnabled: YES];
238        [fSelectedColorNameField setStringValue: [[GroupsController groups] nameForIndex: index]];
239        [fSelectedColorNameField setEnabled: YES];
240        [fCustomLocationEnableCheck setState: [[GroupsController groups] usesCustomDownloadLocationForIndex: index]];
241        [fCustomLocationPopUp setEnabled: ([fCustomLocationEnableCheck state] == NSOnState)];
242        if ([[GroupsController groups] customDownloadLocationForIndex: index])
243        {
244            NSString * location = [[GroupsController groups] customDownloadLocationForIndex: index];
245            ExpandedPathToPathTransformer * pathTransformer = [[[ExpandedPathToPathTransformer alloc] init] autorelease];
246            [[fCustomLocationPopUp itemAtIndex: 0] setTitle: [pathTransformer transformedValue: location]];
247            ExpandedPathToIconTransformer * iconTransformer = [[[ExpandedPathToIconTransformer alloc] init] autorelease];
248            [[fCustomLocationPopUp itemAtIndex: 0] setImage: [iconTransformer transformedValue: location]];
249        }
250        else
251        {
252            [[fCustomLocationPopUp itemAtIndex: 0] setTitle: @""];
253            [[fCustomLocationPopUp itemAtIndex: 0] setImage: nil];
254        }
255    }
256    else
257    {
258        [fSelectedColorView setColor: [NSColor whiteColor]];
259        [fSelectedColorView setEnabled: NO];
260        [fSelectedColorNameField setStringValue: @""];
261        [fSelectedColorNameField setEnabled: NO];
262        [fCustomLocationEnableCheck setEnabled: NO];
263        [fCustomLocationPopUp setEnabled: NO];
264    }
265}
266
267@end
Note: See TracBrowser for help on using the repository browser.