source: trunk/macosx/GroupsController.m @ 6273

Last change on this file since 6273 was 6273, checked in by livings124, 14 years ago

fix 2 invalid return types; don't show notices when compiling the xib's

  • Property svn:keywords set to Date Rev Author Id
File size: 13.0 KB
Line 
1/******************************************************************************
2 * $Id: GroupsController.m 6273 2008-06-30 23:28:17Z 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- (int) numberOfGroups
110{
111    return [fGroups count];
112}
113
114- (int) rowValueForIndex: (int) index
115{
116    if (index != -1)
117    {
118        int i;
119        for (i = 0; i < [fGroups count]; i++)
120            if (index == [[[fGroups objectAtIndex: i] objectForKey: @"Index"] intValue])
121                return i;
122    }
123    return -1;
124}
125
126- (int) indexForRow: (int) row
127{
128    return [[[fGroups objectAtIndex: row] objectForKey: @"Index"] intValue];
129}
130
131- (NSString *) nameForIndex: (int) index
132{
133    int orderIndex = [self rowValueForIndex: index];
134    return orderIndex != -1 ? [[fGroups objectAtIndex: orderIndex] objectForKey: @"Name"] : nil;
135}
136
137- (void) setName: (NSString *) name forIndex: (int) index
138{
139    int orderIndex = [self rowValueForIndex: index];
140    [[fGroups objectAtIndex: orderIndex] setObject: name forKey: @"Name"];
141    [self saveGroups];
142   
143    [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateGroups" object: self];
144}
145
146- (NSImage *) imageForIndex: (int) index
147{
148    int orderIndex = [self rowValueForIndex: index];
149    return orderIndex != -1 ? [self imageForGroup: [fGroups objectAtIndex: orderIndex]]
150                            : [NSImage imageNamed: @"GroupsNoneTemplate.png"];
151}
152
153- (NSColor *) colorForIndex: (int) index
154{
155    int orderIndex = [self rowValueForIndex: index];
156    return orderIndex != -1 ? [[fGroups objectAtIndex: orderIndex] objectForKey: @"Color"] : nil;
157}
158
159- (void) setColor: (NSColor *) color forIndex: (int) index
160{
161    NSMutableDictionary * dict = [fGroups objectAtIndex: [self rowValueForIndex: index]];
162    [dict removeObjectForKey: @"Icon"];
163   
164    [dict setObject: color forKey: @"Color"];
165   
166    [[GroupsController groups] saveGroups];
167    [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateGroups" object: self];
168}
169
170- (void) addNewGroup
171{
172    //find the lowest index
173    int index;
174    for (index = 0; index < [fGroups count]; index++)
175    {
176        BOOL found = NO;
177        NSEnumerator * enumerator = [fGroups objectEnumerator];
178        NSDictionary * dict;
179        while ((dict = [enumerator nextObject]))
180            if ([[dict objectForKey: @"Index"] intValue] == index)
181            {
182                found = YES;
183                break;
184            }
185       
186        if (!found)
187            break;
188    }
189   
190    [fGroups addObject: [NSMutableDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInt: index], @"Index",
191                            [NSColor cyanColor], @"Color", @"", @"Name", nil]];
192   
193    [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateGroups" object: self];
194    [self saveGroups];
195}
196
197- (void) removeGroupWithRowIndexes: (NSIndexSet *) rowIndexes
198{
199    NSMutableIndexSet * indexes = [NSMutableIndexSet indexSet];
200    int index;
201    for (index = [rowIndexes firstIndex]; index != NSNotFound; index = [rowIndexes indexGreaterThanIndex: index])
202        [indexes addIndex: [[[fGroups objectAtIndex: index] objectForKey: @"Index"] intValue]];
203   
204    [fGroups removeObjectsAtIndexes: rowIndexes];
205   
206    [[NSNotificationCenter defaultCenter] postNotificationName: @"GroupValueRemoved" object: self userInfo:
207        [NSDictionary dictionaryWithObject: indexes forKey: @"Indexes"]];
208   
209    if ([indexes containsIndex: [[NSUserDefaults standardUserDefaults] integerForKey: @"FilterGroup"]])
210        [[NSUserDefaults standardUserDefaults] setInteger: -2 forKey: @"FilterGroup"];
211   
212    [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateGroups" object: self];
213    [self saveGroups];
214}
215
216- (NSIndexSet *) moveGroupsAtRowIndexes: (NSIndexSet *) indexes toRow: (int) newRow oldSelected: (NSIndexSet *) selectedIndexes
217{
218    NSArray * selectedGroups = [fGroups objectsAtIndexes: selectedIndexes];
219   
220    //determine where to move them
221    int i, originalRow = newRow;
222    for (i = [indexes firstIndex]; i < originalRow && i != NSNotFound; i = [indexes indexGreaterThanIndex: i])
223        newRow--;
224   
225    //remove objects to reinsert
226    NSArray * movingGroups = [[fGroups objectsAtIndexes: indexes] retain];
227    [fGroups removeObjectsAtIndexes: indexes];
228   
229    //insert objects at new location
230    for (i = 0; i < [movingGroups count]; i++)
231        [fGroups insertObject: [movingGroups objectAtIndex: i] atIndex: newRow + i];
232   
233    [movingGroups release];
234   
235    [self saveGroups];
236    [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateGroups" object: self];
237   
238    NSMutableIndexSet * newSelectedIndexes = nil;
239    if ([selectedGroups count] > 0)
240    {
241        newSelectedIndexes = [NSMutableIndexSet indexSet];
242        NSEnumerator * enumerator = [selectedGroups objectEnumerator];
243        NSDictionary * dict;
244        while ((dict = [enumerator nextObject]))
245            [newSelectedIndexes addIndex: [fGroups indexOfObject: dict]];
246    }
247   
248    return newSelectedIndexes;
249}
250
251- (NSMenu *) groupMenuWithTarget: (id) target action: (SEL) action isSmall: (BOOL) small
252{
253    NSMenu * menu = [[NSMenu alloc] initWithTitle: @"Groups"];
254   
255    NSMenuItem * item = [[NSMenuItem alloc] initWithTitle: NSLocalizedString(@"None", "Groups -> Menu") action: action
256                            keyEquivalent: @""];
257    [item setTarget: target];
258    [item setTag: -1];
259   
260    NSImage * icon = [NSImage imageNamed: @"GroupsNoneTemplate.png"];
261    if (small)
262    {
263        icon = [icon copy];
264        [icon setScalesWhenResized: YES];
265        [icon setSize: NSMakeSize(ICON_WIDTH_SMALL, ICON_WIDTH_SMALL)];
266       
267        [item setImage: icon];
268        [icon release];
269    }
270    else
271        [item setImage: icon];
272   
273    [menu addItem: item];
274    [item release];
275   
276    NSEnumerator * enumerator = [fGroups objectEnumerator];
277    NSMutableDictionary * dict;
278    while ((dict = [enumerator nextObject]))
279    {
280        item = [[NSMenuItem alloc] initWithTitle: [dict objectForKey: @"Name"] action: action keyEquivalent: @""];
281        [item setTarget: target];
282       
283        [item setTag: [[dict objectForKey: @"Index"] intValue]];
284       
285        NSImage * icon = [self imageForGroup: dict];
286        if (small)
287        {
288            icon = [icon copy];
289            [icon setScalesWhenResized: YES];
290            [icon setSize: NSMakeSize(ICON_WIDTH_SMALL, ICON_WIDTH_SMALL)];
291           
292            [item setImage: icon];
293            [icon release];
294        }
295        else
296            [item setImage: icon];
297       
298        [menu addItem: item];
299        [item release];
300    }
301   
302    return [menu autorelease];
303}
304
305@end
306
307@implementation GroupsController (Private)
308
309- (void) saveGroups
310{
311    //don't archive the icon
312    NSMutableArray * groups = [NSMutableArray arrayWithCapacity: [fGroups count]];
313    NSEnumerator * enumerator = [fGroups objectEnumerator];
314    NSDictionary * dict;
315    while ((dict = [enumerator nextObject]))
316    {
317        NSMutableDictionary * tempDict = [dict mutableCopy];
318        [tempDict removeObjectForKey: @"Icon"];
319        [groups addObject: tempDict];
320        [tempDict release];
321    }
322   
323    [[NSUserDefaults standardUserDefaults] setObject: [NSArchiver archivedDataWithRootObject: groups] forKey: @"Groups"];
324}
325
326- (NSImage *) imageForGroup: (NSMutableDictionary *) dict
327{
328    NSImage * image;
329    if ((image = [dict objectForKey: @"Icon"]))
330        return image;
331   
332    NSRect rect = NSMakeRect(0.0, 0.0, ICON_WIDTH, ICON_WIDTH);
333   
334    NSBezierPath * bp = [NSBezierPath bezierPathWithRoundedRect: rect radius: 3.0];
335    NSImage * icon = [[NSImage alloc] initWithSize: rect.size];
336   
337    NSColor * color = [dict objectForKey: @"Color"];
338   
339    [icon lockFocus];
340   
341    //border
342    CTGradient * gradient = [CTGradient gradientWithBeginningColor: [color blendedColorWithFraction: 0.45 ofColor:
343                                [NSColor whiteColor]] endingColor: color];
344    [gradient fillBezierPath: bp angle: 270.0];
345   
346    //inside
347    bp = [NSBezierPath bezierPathWithRoundedRect: NSInsetRect(rect, 1.0, 1.0) radius: 3.0];
348    gradient = [CTGradient gradientWithBeginningColor: [color blendedColorWithFraction: 0.75 ofColor: [NSColor whiteColor]]
349                endingColor: [color blendedColorWithFraction: 0.2 ofColor: [NSColor whiteColor]]];
350    [gradient fillBezierPath: bp angle: 270.0];
351   
352    [icon unlockFocus];
353   
354    [dict setObject: icon forKey: @"Icon"];
355    [icon release];
356   
357    return icon;
358}
359
360@end
Note: See TracBrowser for help on using the repository browser.