source: trunk/macosx/GroupsController.m @ 7185

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

remove references and dead code to the group window being its own window; other rearrangements of code

  • Property svn:keywords set to Date Rev Author Id
File size: 12.9 KB
Line 
1/******************************************************************************
2 * $Id: GroupsController.m 7185 2008-11-29 20:29:54Z 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 "GroupsController.h"
26#import "CTGradient.h"
27#import "NSBezierPathAdditions.h"
28
29#define ICON_WIDTH 16.0
30#define ICON_WIDTH_SMALL 12.0
31
32@interface GroupsController (Private)
33
34- (void) saveGroups;
35
36- (NSImage *) imageForGroup: (NSMutableDictionary *) dict;
37
38@end
39
40@implementation GroupsController
41
42GroupsController * fGroupsInstance = nil;
43+ (GroupsController *) groups
44{
45    if (!fGroupsInstance)
46        fGroupsInstance = [[GroupsController alloc] init];
47    return fGroupsInstance;
48}
49
50- (id) init
51{
52    if ((self = [super init]))
53    {
54        NSData * data;
55        if ((data = [[NSUserDefaults standardUserDefaults] dataForKey: @"Groups"]))
56            fGroups = [[NSUnarchiver unarchiveObjectWithData: data] retain];
57        else
58        {
59            //default groups
60            NSMutableDictionary * red = [NSMutableDictionary dictionaryWithObjectsAndKeys:
61                                            [NSColor redColor], @"Color",
62                                            NSLocalizedString(@"Red", "Groups -> Name"), @"Name",
63                                            [NSNumber numberWithInt: 0], @"Index", nil];
64           
65            NSMutableDictionary * orange = [NSMutableDictionary dictionaryWithObjectsAndKeys:
66                                            [NSColor orangeColor], @"Color",
67                                            NSLocalizedString(@"Orange", "Groups -> Name"), @"Name",
68                                            [NSNumber numberWithInt: 1], @"Index", nil];
69           
70            NSMutableDictionary * yellow = [NSMutableDictionary dictionaryWithObjectsAndKeys:
71                                            [NSColor yellowColor], @"Color",
72                                            NSLocalizedString(@"Yellow", "Groups -> Name"), @"Name",
73                                            [NSNumber numberWithInt: 2], @"Index", nil];
74           
75            NSMutableDictionary * green = [NSMutableDictionary dictionaryWithObjectsAndKeys:
76                                            [NSColor greenColor], @"Color",
77                                            NSLocalizedString(@"Green", "Groups -> Name"), @"Name",
78                                            [NSNumber numberWithInt: 3], @"Index", nil];
79           
80            NSMutableDictionary * blue = [NSMutableDictionary dictionaryWithObjectsAndKeys:
81                                            [NSColor blueColor], @"Color",
82                                            NSLocalizedString(@"Blue", "Groups -> Name"), @"Name",
83                                            [NSNumber numberWithInt: 4], @"Index", nil];
84           
85            NSMutableDictionary * purple = [NSMutableDictionary dictionaryWithObjectsAndKeys:
86                                            [NSColor purpleColor], @"Color",
87                                            NSLocalizedString(@"Purple", "Groups -> Name"), @"Name",
88                                            [NSNumber numberWithInt: 5], @"Index", nil];
89           
90            NSMutableDictionary * gray = [NSMutableDictionary dictionaryWithObjectsAndKeys:
91                                            [NSColor grayColor], @"Color",
92                                            NSLocalizedString(@"Gray", "Groups -> Name"), @"Name",
93                                            [NSNumber numberWithInt: 6], @"Index", nil];
94           
95            fGroups = [[NSMutableArray alloc] initWithObjects: red, orange, yellow, green, blue, purple, gray, nil];
96            [self saveGroups]; //make sure this is saved right away
97        }
98    }
99   
100    return self;
101}
102
103- (void) dealloc
104{
105    [fGroups release];
106    [super dealloc];
107}
108
109- (NSInteger) numberOfGroups
110{
111    return [fGroups count];
112}
113
114- (NSInteger) rowValueForIndex: (NSInteger) index
115{
116    if (index != -1)
117    {
118        for (NSInteger i = 0; i < [fGroups count]; i++)
119            if (index == [[[fGroups objectAtIndex: i] objectForKey: @"Index"] intValue])
120                return i;
121    }
122    return -1;
123}
124
125- (NSInteger) indexForRow: (NSInteger) row
126{
127    return [[[fGroups objectAtIndex: row] objectForKey: @"Index"] intValue];
128}
129
130- (NSString *) nameForIndex: (NSInteger) index
131{
132    NSInteger orderIndex = [self rowValueForIndex: index];
133    return orderIndex != -1 ? [[fGroups objectAtIndex: orderIndex] objectForKey: @"Name"] : nil;
134}
135
136- (void) setName: (NSString *) name forIndex: (NSInteger) index
137{
138    NSInteger orderIndex = [self rowValueForIndex: index];
139    [[fGroups objectAtIndex: orderIndex] setObject: name forKey: @"Name"];
140    [self saveGroups];
141   
142    [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateGroups" object: self];
143}
144
145- (NSImage *) imageForIndex: (NSInteger) index
146{
147    NSInteger orderIndex = [self rowValueForIndex: index];
148    return orderIndex != -1 ? [self imageForGroup: [fGroups objectAtIndex: orderIndex]]
149                            : [NSImage imageNamed: @"GroupsNoneTemplate.png"];
150}
151
152- (NSColor *) colorForIndex: (NSInteger) index
153{
154    NSInteger orderIndex = [self rowValueForIndex: index];
155    return orderIndex != -1 ? [[fGroups objectAtIndex: orderIndex] objectForKey: @"Color"] : nil;
156}
157
158- (void) setColor: (NSColor *) color forIndex: (NSInteger) index
159{
160    NSMutableDictionary * dict = [fGroups objectAtIndex: [self rowValueForIndex: index]];
161    [dict removeObjectForKey: @"Icon"];
162   
163    [dict setObject: color forKey: @"Color"];
164   
165    [[GroupsController groups] saveGroups];
166    [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateGroups" object: self];
167}
168
169- (void) addNewGroup
170{
171    //find the lowest index
172    NSInteger index;
173    for (index = 0; index < [fGroups count]; index++)
174    {
175        BOOL found = NO;
176        NSEnumerator * enumerator = [fGroups objectEnumerator];
177        NSDictionary * dict;
178        while ((dict = [enumerator nextObject]))
179            if ([[dict objectForKey: @"Index"] intValue] == index)
180            {
181                found = YES;
182                break;
183            }
184       
185        if (!found)
186            break;
187    }
188   
189    [fGroups addObject: [NSMutableDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInt: index], @"Index",
190                            [NSColor cyanColor], @"Color", @"", @"Name", nil]];
191   
192    [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateGroups" object: self];
193    [self saveGroups];
194}
195
196- (void) removeGroupWithRowIndex: (NSInteger) row
197{
198    NSInteger index = [[[fGroups objectAtIndex: row] objectForKey: @"Index"] intValue];
199    [fGroups removeObjectAtIndex: index];
200   
201    [[NSNotificationCenter defaultCenter] postNotificationName: @"GroupValueRemoved" object: self userInfo:
202        [NSDictionary dictionaryWithObject: [NSNumber numberWithInt: index] forKey: @"Index"]];
203   
204    if (index == [[NSUserDefaults standardUserDefaults] integerForKey: @"FilterGroup"])
205        [[NSUserDefaults standardUserDefaults] setInteger: -2 forKey: @"FilterGroup"];
206   
207    [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateGroups" object: self];
208    [self saveGroups];
209}
210
211- (NSIndexSet *) moveGroupsAtRowIndexes: (NSIndexSet *) indexes toRow: (NSInteger) newRow oldSelected: (NSIndexSet *) selectedIndexes
212{
213    NSArray * selectedGroups = [fGroups objectsAtIndexes: selectedIndexes];
214   
215    //determine where to move them
216    for (NSInteger i = [indexes firstIndex], startRow = newRow; i < startRow && i != NSNotFound; i = [indexes indexGreaterThanIndex: i])
217        newRow--;
218   
219    //remove objects to reinsert
220    NSArray * movingGroups = [[fGroups objectsAtIndexes: indexes] retain];
221    [fGroups removeObjectsAtIndexes: indexes];
222   
223    //insert objects at new location
224    for (NSInteger i = 0; i < [movingGroups count]; i++)
225        [fGroups insertObject: [movingGroups objectAtIndex: i] atIndex: newRow + i];
226   
227    [movingGroups release];
228   
229    [self saveGroups];
230    [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateGroups" object: self];
231   
232    NSMutableIndexSet * newSelectedIndexes = nil;
233    if ([selectedGroups count] > 0)
234    {
235        newSelectedIndexes = [NSMutableIndexSet indexSet];
236        NSEnumerator * enumerator = [selectedGroups objectEnumerator];
237        NSDictionary * dict;
238        while ((dict = [enumerator nextObject]))
239            [newSelectedIndexes addIndex: [fGroups indexOfObject: dict]];
240    }
241   
242    return newSelectedIndexes;
243}
244
245- (NSMenu *) groupMenuWithTarget: (id) target action: (SEL) action isSmall: (BOOL) small
246{
247    NSMenu * menu = [[NSMenu alloc] initWithTitle: @"Groups"];
248   
249    NSMenuItem * item = [[NSMenuItem alloc] initWithTitle: NSLocalizedString(@"None", "Groups -> Menu") action: action
250                            keyEquivalent: @""];
251    [item setTarget: target];
252    [item setTag: -1];
253   
254    NSImage * icon = [NSImage imageNamed: @"GroupsNoneTemplate.png"];
255    if (small)
256    {
257        icon = [icon copy];
258        [icon setScalesWhenResized: YES];
259        [icon setSize: NSMakeSize(ICON_WIDTH_SMALL, ICON_WIDTH_SMALL)];
260       
261        [item setImage: icon];
262        [icon release];
263    }
264    else
265        [item setImage: icon];
266   
267    [menu addItem: item];
268    [item release];
269   
270    NSEnumerator * enumerator = [fGroups objectEnumerator];
271    NSMutableDictionary * dict;
272    while ((dict = [enumerator nextObject]))
273    {
274        item = [[NSMenuItem alloc] initWithTitle: [dict objectForKey: @"Name"] action: action keyEquivalent: @""];
275        [item setTarget: target];
276       
277        [item setTag: [[dict objectForKey: @"Index"] intValue]];
278       
279        NSImage * icon = [self imageForGroup: dict];
280        if (small)
281        {
282            icon = [icon copy];
283            [icon setScalesWhenResized: YES];
284            [icon setSize: NSMakeSize(ICON_WIDTH_SMALL, ICON_WIDTH_SMALL)];
285           
286            [item setImage: icon];
287            [icon release];
288        }
289        else
290            [item setImage: icon];
291       
292        [menu addItem: item];
293        [item release];
294    }
295   
296    return [menu autorelease];
297}
298
299@end
300
301@implementation GroupsController (Private)
302
303- (void) saveGroups
304{
305    //don't archive the icon
306    NSMutableArray * groups = [NSMutableArray arrayWithCapacity: [fGroups count]];
307    NSEnumerator * enumerator = [fGroups objectEnumerator];
308    NSDictionary * dict;
309    while ((dict = [enumerator nextObject]))
310    {
311        NSMutableDictionary * tempDict = [dict mutableCopy];
312        [tempDict removeObjectForKey: @"Icon"];
313        [groups addObject: tempDict];
314        [tempDict release];
315    }
316   
317    [[NSUserDefaults standardUserDefaults] setObject: [NSArchiver archivedDataWithRootObject: groups] forKey: @"Groups"];
318}
319
320- (NSImage *) imageForGroup: (NSMutableDictionary *) dict
321{
322    NSImage * image;
323    if ((image = [dict objectForKey: @"Icon"]))
324        return image;
325   
326    NSRect rect = NSMakeRect(0.0, 0.0, ICON_WIDTH, ICON_WIDTH);
327   
328    NSBezierPath * bp = [NSBezierPath bezierPathWithRoundedRect: rect radius: 3.0];
329    NSImage * icon = [[NSImage alloc] initWithSize: rect.size];
330   
331    NSColor * color = [dict objectForKey: @"Color"];
332   
333    [icon lockFocus];
334   
335    //border
336    CTGradient * gradient = [CTGradient gradientWithBeginningColor: [color blendedColorWithFraction: 0.45 ofColor:
337                                [NSColor whiteColor]] endingColor: color];
338    [gradient fillBezierPath: bp angle: 270.0];
339   
340    //inside
341    bp = [NSBezierPath bezierPathWithRoundedRect: NSInsetRect(rect, 1.0, 1.0) radius: 3.0];
342    gradient = [CTGradient gradientWithBeginningColor: [color blendedColorWithFraction: 0.75 ofColor: [NSColor whiteColor]]
343                endingColor: [color blendedColorWithFraction: 0.2 ofColor: [NSColor whiteColor]]];
344    [gradient fillBezierPath: bp angle: 270.0];
345   
346    [icon unlockFocus];
347   
348    [dict setObject: icon forKey: @"Icon"];
349    [icon release];
350   
351    return icon;
352}
353
354@end
Note: See TracBrowser for help on using the repository browser.