2008-03-23 00:56:43 +00:00
|
|
|
/******************************************************************************
|
2012-01-14 17:12:04 +00:00
|
|
|
* Copyright (c) 2007-2012 Transmission authors and contributors
|
2008-03-23 00:56:43 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
#import "GroupsController.h"
|
2012-01-04 00:06:30 +00:00
|
|
|
#import "NSMutableArrayAdditions.h"
|
2008-03-23 00:56:43 +00:00
|
|
|
|
|
|
|
#define ICON_WIDTH 16.0
|
2008-05-31 02:52:49 +00:00
|
|
|
#define ICON_WIDTH_SMALL 12.0
|
2008-03-23 00:56:43 +00:00
|
|
|
|
|
|
|
@interface GroupsController (Private)
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)saveGroups;
|
2008-03-23 00:56:43 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (NSImage*)imageForGroup:(NSMutableDictionary*)dict;
|
2008-03-23 00:56:43 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (BOOL)torrent:(Torrent*)torrent doesMatchRulesForGroupAtIndex:(NSInteger)index;
|
2008-12-08 03:26:28 +00:00
|
|
|
|
2008-03-23 00:56:43 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation GroupsController
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
GroupsController* fGroupsInstance = nil;
|
|
|
|
|
|
|
|
+ (GroupsController*)groups
|
2008-03-23 00:56:43 +00:00
|
|
|
{
|
|
|
|
if (!fGroupsInstance)
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2008-03-23 00:56:43 +00:00
|
|
|
fGroupsInstance = [[GroupsController alloc] init];
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2008-03-23 00:56:43 +00:00
|
|
|
return fGroupsInstance;
|
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (instancetype)init
|
2008-03-23 00:56:43 +00:00
|
|
|
{
|
|
|
|
if ((self = [super init]))
|
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
NSData* data;
|
|
|
|
if ((data = [NSUserDefaults.standardUserDefaults dataForKey:@"GroupDicts"]))
|
|
|
|
{
|
|
|
|
fGroups = [NSKeyedUnarchiver unarchiveObjectWithData:data];
|
|
|
|
}
|
|
|
|
else if ((data = [NSUserDefaults.standardUserDefaults dataForKey:@"Groups"])) //handle old groups
|
2008-12-24 17:41:45 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
fGroups = [NSUnarchiver unarchiveObjectWithData:data];
|
|
|
|
[NSUserDefaults.standardUserDefaults removeObjectForKey:@"Groups"];
|
2008-12-24 18:50:57 +00:00
|
|
|
[self saveGroups];
|
2008-12-24 17:41:45 +00:00
|
|
|
}
|
2008-03-23 00:56:43 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
//default groups
|
2021-08-15 09:41:48 +00:00
|
|
|
NSMutableDictionary* red = [NSMutableDictionary
|
|
|
|
dictionaryWithObjectsAndKeys:NSColor.redColor, @"Color", NSLocalizedString(@"Red", "Groups -> Name"), @"Name", @0, @"Index", nil];
|
|
|
|
|
|
|
|
NSMutableDictionary* orange = [NSMutableDictionary
|
|
|
|
dictionaryWithObjectsAndKeys:NSColor.orangeColor, @"Color", NSLocalizedString(@"Orange", "Groups -> Name"), @"Name", @1, @"Index", nil];
|
|
|
|
|
|
|
|
NSMutableDictionary* yellow = [NSMutableDictionary
|
|
|
|
dictionaryWithObjectsAndKeys:NSColor.yellowColor, @"Color", NSLocalizedString(@"Yellow", "Groups -> Name"), @"Name", @2, @"Index", nil];
|
|
|
|
|
|
|
|
NSMutableDictionary* green = [NSMutableDictionary
|
|
|
|
dictionaryWithObjectsAndKeys:NSColor.greenColor, @"Color", NSLocalizedString(@"Green", "Groups -> Name"), @"Name", @3, @"Index", nil];
|
|
|
|
|
|
|
|
NSMutableDictionary* blue = [NSMutableDictionary
|
|
|
|
dictionaryWithObjectsAndKeys:NSColor.blueColor, @"Color", NSLocalizedString(@"Blue", "Groups -> Name"), @"Name", @4, @"Index", nil];
|
|
|
|
|
|
|
|
NSMutableDictionary* purple = [NSMutableDictionary
|
|
|
|
dictionaryWithObjectsAndKeys:NSColor.purpleColor, @"Color", NSLocalizedString(@"Purple", "Groups -> Name"), @"Name", @5, @"Index", nil];
|
|
|
|
|
|
|
|
NSMutableDictionary* gray = [NSMutableDictionary
|
|
|
|
dictionaryWithObjectsAndKeys:NSColor.grayColor, @"Color", NSLocalizedString(@"Gray", "Groups -> Name"), @"Name", @6, @"Index", nil];
|
|
|
|
|
|
|
|
fGroups = [[NSMutableArray alloc] initWithObjects:red, orange, yellow, green, blue, purple, gray, nil];
|
2008-03-23 00:56:43 +00:00
|
|
|
[self saveGroups]; //make sure this is saved right away
|
|
|
|
}
|
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2008-03-23 00:56:43 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (NSInteger)numberOfGroups
|
2008-03-23 00:56:43 +00:00
|
|
|
{
|
2021-08-07 07:27:56 +00:00
|
|
|
return fGroups.count;
|
2008-03-23 00:56:43 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (NSInteger)rowValueForIndex:(NSInteger)index
|
2008-03-23 00:56:43 +00:00
|
|
|
{
|
|
|
|
if (index != -1)
|
|
|
|
{
|
2021-08-07 07:27:56 +00:00
|
|
|
for (NSUInteger i = 0; i < fGroups.count; i++)
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2017-07-08 14:38:47 +00:00
|
|
|
if (index == [fGroups[i][@"Index"] integerValue])
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2008-03-23 00:56:43 +00:00
|
|
|
return i;
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
|
|
|
}
|
2008-03-23 00:56:43 +00:00
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (NSInteger)indexForRow:(NSInteger)row
|
2008-03-23 04:36:30 +00:00
|
|
|
{
|
2017-07-08 14:38:47 +00:00
|
|
|
return [fGroups[row][@"Index"] integerValue];
|
2008-03-23 04:36:30 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (NSString*)nameForIndex:(NSInteger)index
|
2008-03-23 00:56:43 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
NSInteger orderIndex = [self rowValueForIndex:index];
|
2017-07-08 14:38:47 +00:00
|
|
|
return orderIndex != -1 ? fGroups[orderIndex][@"Name"] : nil;
|
2008-03-23 00:56:43 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)setName:(NSString*)name forIndex:(NSInteger)index
|
2008-03-23 00:56:43 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
NSInteger orderIndex = [self rowValueForIndex:index];
|
2017-07-08 14:38:47 +00:00
|
|
|
fGroups[orderIndex][@"Name"] = name;
|
2008-03-23 00:56:43 +00:00
|
|
|
[self saveGroups];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
[NSNotificationCenter.defaultCenter postNotificationName:@"UpdateGroups" object:self];
|
2008-03-23 00:56:43 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (NSImage*)imageForIndex:(NSInteger)index
|
2008-03-23 00:56:43 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
NSInteger orderIndex = [self rowValueForIndex:index];
|
|
|
|
return orderIndex != -1 ? [self imageForGroup:fGroups[orderIndex]] : [NSImage imageNamed:@"GroupsNoneTemplate"];
|
2008-03-23 00:56:43 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (NSColor*)colorForIndex:(NSInteger)index
|
2008-03-23 00:56:43 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
NSInteger orderIndex = [self rowValueForIndex:index];
|
2017-07-08 14:38:47 +00:00
|
|
|
return orderIndex != -1 ? fGroups[orderIndex][@"Color"] : nil;
|
2008-03-23 00:56:43 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)setColor:(NSColor*)color forIndex:(NSInteger)index
|
2008-03-23 00:56:43 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
NSMutableDictionary* dict = fGroups[[self rowValueForIndex:index]];
|
|
|
|
[dict removeObjectForKey:@"Icon"];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2017-07-08 14:38:47 +00:00
|
|
|
dict[@"Color"] = color;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-07 07:27:56 +00:00
|
|
|
[GroupsController.groups saveGroups];
|
2021-08-15 09:41:48 +00:00
|
|
|
[NSNotificationCenter.defaultCenter postNotificationName:@"UpdateGroups" object:self];
|
2008-03-23 00:56:43 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (BOOL)usesCustomDownloadLocationForIndex:(NSInteger)index
|
2008-11-30 19:23:15 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
if (![self customDownloadLocationForIndex:index])
|
|
|
|
{
|
2008-12-13 22:49:46 +00:00
|
|
|
return NO;
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2008-12-13 22:49:46 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
NSInteger orderIndex = [self rowValueForIndex:index];
|
2017-07-08 14:38:47 +00:00
|
|
|
return [fGroups[orderIndex][@"UsesCustomDownloadLocation"] boolValue];
|
2008-11-30 19:23:15 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)setUsesCustomDownloadLocation:(BOOL)useCustomLocation forIndex:(NSInteger)index
|
2008-11-30 19:23:15 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
NSMutableDictionary* dict = fGroups[[self rowValueForIndex:index]];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2017-07-08 14:38:47 +00:00
|
|
|
dict[@"UsesCustomDownloadLocation"] = @(useCustomLocation);
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-07 07:27:56 +00:00
|
|
|
[GroupsController.groups saveGroups];
|
2008-11-30 19:23:15 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (NSString*)customDownloadLocationForIndex:(NSInteger)index
|
2008-11-30 19:23:15 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
NSInteger orderIndex = [self rowValueForIndex:index];
|
2017-07-08 14:38:47 +00:00
|
|
|
return orderIndex != -1 ? fGroups[orderIndex][@"CustomDownloadLocation"] : nil;
|
2008-11-30 19:23:15 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)setCustomDownloadLocation:(NSString*)location forIndex:(NSInteger)index
|
2008-11-30 19:23:15 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
NSMutableDictionary* dict = fGroups[[self rowValueForIndex:index]];
|
2017-07-08 14:38:47 +00:00
|
|
|
dict[@"CustomDownloadLocation"] = location;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-07 07:27:56 +00:00
|
|
|
[GroupsController.groups saveGroups];
|
2008-12-08 03:26:28 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (BOOL)usesAutoAssignRulesForIndex:(NSInteger)index
|
2008-12-08 03:26:28 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
NSInteger orderIndex = [self rowValueForIndex:index];
|
2008-12-08 03:26:28 +00:00
|
|
|
if (orderIndex == -1)
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2008-12-08 03:26:28 +00:00
|
|
|
return NO;
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
NSNumber* assignRules = fGroups[orderIndex][@"UsesAutoGroupRules"];
|
2021-08-07 07:27:56 +00:00
|
|
|
return assignRules && assignRules.boolValue;
|
2008-12-08 03:26:28 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)setUsesAutoAssignRules:(BOOL)useAutoAssignRules forIndex:(NSInteger)index
|
2008-12-08 03:26:28 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
NSMutableDictionary* dict = fGroups[[self rowValueForIndex:index]];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2017-07-08 14:38:47 +00:00
|
|
|
dict[@"UsesAutoGroupRules"] = @(useAutoAssignRules);
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-07 07:27:56 +00:00
|
|
|
[GroupsController.groups saveGroups];
|
2008-12-08 03:26:28 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (NSPredicate*)autoAssignRulesForIndex:(NSInteger)index
|
2008-12-08 03:26:28 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
NSInteger orderIndex = [self rowValueForIndex:index];
|
2008-12-24 17:41:45 +00:00
|
|
|
if (orderIndex == -1)
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2017-01-24 17:53:16 +00:00
|
|
|
return nil;
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2017-07-08 14:38:47 +00:00
|
|
|
return fGroups[orderIndex][@"AutoGroupRules"];
|
2008-12-08 03:26:28 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)setAutoAssignRules:(NSPredicate*)predicate forIndex:(NSInteger)index
|
2008-12-08 03:26:28 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
NSMutableDictionary* dict = fGroups[[self rowValueForIndex:index]];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2008-12-24 17:41:45 +00:00
|
|
|
if (predicate)
|
2008-12-08 03:26:28 +00:00
|
|
|
{
|
2017-07-08 14:38:47 +00:00
|
|
|
dict[@"AutoGroupRules"] = predicate;
|
2021-08-07 07:27:56 +00:00
|
|
|
[GroupsController.groups saveGroups];
|
2008-12-08 03:26:28 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
[dict removeObjectForKey:@"AutoGroupRules"];
|
|
|
|
[self setUsesAutoAssignRules:NO forIndex:index];
|
2008-12-08 03:26:28 +00:00
|
|
|
}
|
2008-11-30 19:23:15 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)addNewGroup
|
2008-03-23 00:56:43 +00:00
|
|
|
{
|
|
|
|
//find the lowest index
|
2021-08-15 09:41:48 +00:00
|
|
|
NSMutableIndexSet* candidates = [NSMutableIndexSet indexSetWithIndexesInRange:NSMakeRange(0, fGroups.count + 1)];
|
|
|
|
for (NSDictionary* dict in fGroups)
|
|
|
|
{
|
|
|
|
[candidates removeIndex:[dict[@"Index"] integerValue]];
|
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
NSInteger const index = candidates.firstIndex;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
[fGroups addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:@(index),
|
|
|
|
@"Index",
|
|
|
|
[NSColor colorWithCalibratedRed:0.0 green:0.65 blue:1.0
|
|
|
|
alpha:1.0],
|
|
|
|
@"Color",
|
|
|
|
@"",
|
|
|
|
@"Name",
|
|
|
|
nil]];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
[NSNotificationCenter.defaultCenter postNotificationName:@"UpdateGroups" object:self];
|
2008-03-23 00:56:43 +00:00
|
|
|
[self saveGroups];
|
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)removeGroupWithRowIndex:(NSInteger)row
|
2008-03-23 00:56:43 +00:00
|
|
|
{
|
2017-07-08 14:38:47 +00:00
|
|
|
NSInteger index = [fGroups[row][@"Index"] integerValue];
|
2021-08-15 09:41:48 +00:00
|
|
|
[fGroups removeObjectAtIndex:row];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
[NSNotificationCenter.defaultCenter postNotificationName:@"GroupValueRemoved" object:self
|
|
|
|
userInfo:@{ @"Index" : @(index) }];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
if (index == [NSUserDefaults.standardUserDefaults integerForKey:@"FilterGroup"])
|
|
|
|
{
|
|
|
|
[NSUserDefaults.standardUserDefaults setInteger:-2 forKey:@"FilterGroup"];
|
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
[NSNotificationCenter.defaultCenter postNotificationName:@"UpdateGroups" object:self];
|
2008-03-23 00:56:43 +00:00
|
|
|
[self saveGroups];
|
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)moveGroupAtRow:(NSInteger)oldRow toRow:(NSInteger)newRow
|
2008-03-23 00:56:43 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
[fGroups moveObjectAtIndex:oldRow toIndex:newRow];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2008-03-23 00:56:43 +00:00
|
|
|
[self saveGroups];
|
2021-08-15 09:41:48 +00:00
|
|
|
[NSNotificationCenter.defaultCenter postNotificationName:@"UpdateGroups" object:self];
|
2008-03-23 00:56:43 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (NSMenu*)groupMenuWithTarget:(id)target action:(SEL)action isSmall:(BOOL)small
|
2008-03-23 00:56:43 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
NSMenu* menu = [[NSMenu alloc] initWithTitle:@"Groups"];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
NSMenuItem* item = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"None", "Groups -> Menu") action:action
|
|
|
|
keyEquivalent:@""];
|
2021-08-07 07:27:56 +00:00
|
|
|
item.target = target;
|
|
|
|
item.tag = -1;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
NSImage* icon = [NSImage imageNamed:@"GroupsNoneTemplate"];
|
2008-05-31 02:52:49 +00:00
|
|
|
if (small)
|
|
|
|
{
|
2008-06-01 19:08:38 +00:00
|
|
|
icon = [icon copy];
|
2021-08-07 07:27:56 +00:00
|
|
|
icon.size = NSMakeSize(ICON_WIDTH_SMALL, ICON_WIDTH_SMALL);
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-07 07:27:56 +00:00
|
|
|
item.image = icon;
|
2008-05-31 02:52:49 +00:00
|
|
|
}
|
2008-06-01 19:08:38 +00:00
|
|
|
else
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2021-08-07 07:27:56 +00:00
|
|
|
item.image = icon;
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
[menu addItem:item];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
for (NSMutableDictionary* dict in fGroups)
|
2008-03-23 00:56:43 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
item = [[NSMenuItem alloc] initWithTitle:dict[@"Name"] action:action keyEquivalent:@""];
|
2021-08-07 07:27:56 +00:00
|
|
|
item.target = target;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-07 07:27:56 +00:00
|
|
|
item.tag = [dict[@"Index"] integerValue];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
NSImage* icon = [self imageForGroup:dict];
|
2008-05-31 02:52:49 +00:00
|
|
|
if (small)
|
|
|
|
{
|
2008-06-01 19:08:38 +00:00
|
|
|
icon = [icon copy];
|
2021-08-07 07:27:56 +00:00
|
|
|
icon.size = NSMakeSize(ICON_WIDTH_SMALL, ICON_WIDTH_SMALL);
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-07 07:27:56 +00:00
|
|
|
item.image = icon;
|
2008-05-31 02:52:49 +00:00
|
|
|
}
|
2008-06-01 19:08:38 +00:00
|
|
|
else
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2021-08-07 07:27:56 +00:00
|
|
|
item.image = icon;
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
[menu addItem:item];
|
2008-03-23 00:56:43 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2017-07-29 16:14:22 +00:00
|
|
|
return menu;
|
2008-03-23 00:56:43 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (NSInteger)groupIndexForTorrent:(Torrent*)torrent
|
2008-12-08 03:26:28 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
for (NSDictionary* group in fGroups)
|
2008-12-08 03:26:28 +00:00
|
|
|
{
|
2017-07-08 14:38:47 +00:00
|
|
|
NSInteger row = [group[@"Index"] integerValue];
|
2021-08-15 09:41:48 +00:00
|
|
|
if ([self torrent:torrent doesMatchRulesForGroupAtIndex:row])
|
|
|
|
{
|
2008-12-08 03:26:28 +00:00
|
|
|
return row;
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2008-12-08 03:26:28 +00:00
|
|
|
}
|
2009-01-02 01:10:54 +00:00
|
|
|
return -1;
|
2008-12-08 03:26:28 +00:00
|
|
|
}
|
|
|
|
|
2008-03-23 00:56:43 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation GroupsController (Private)
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)saveGroups
|
2008-03-23 00:56:43 +00:00
|
|
|
{
|
2008-05-31 03:11:59 +00:00
|
|
|
//don't archive the icon
|
2021-08-15 09:41:48 +00:00
|
|
|
NSMutableArray* groups = [NSMutableArray arrayWithCapacity:fGroups.count];
|
|
|
|
for (NSDictionary* dict in fGroups)
|
2008-05-31 03:11:59 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
NSMutableDictionary* tempDict = [dict mutableCopy];
|
|
|
|
[tempDict removeObjectForKey:@"Icon"];
|
|
|
|
[groups addObject:tempDict];
|
2008-05-31 03:11:59 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
[NSUserDefaults.standardUserDefaults setObject:[NSKeyedArchiver archivedDataWithRootObject:groups] forKey:@"GroupDicts"];
|
2008-03-23 00:56:43 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (NSImage*)imageForGroup:(NSMutableDictionary*)dict
|
2008-03-23 00:56:43 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
NSImage* image;
|
2017-07-08 14:38:47 +00:00
|
|
|
if ((image = dict[@"Icon"]))
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2008-05-31 02:38:44 +00:00
|
|
|
return image;
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2008-05-31 02:38:44 +00:00
|
|
|
NSRect rect = NSMakeRect(0.0, 0.0, ICON_WIDTH, ICON_WIDTH);
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
NSBezierPath* bp = [NSBezierPath bezierPathWithRoundedRect:rect xRadius:3.0 yRadius:3.0];
|
|
|
|
NSImage* icon = [[NSImage alloc] initWithSize:rect.size];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
NSColor* color = dict[@"Color"];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2008-03-23 00:56:43 +00:00
|
|
|
[icon lockFocus];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2008-05-14 21:35:37 +00:00
|
|
|
//border
|
2021-08-15 09:41:48 +00:00
|
|
|
NSGradient* gradient = [[NSGradient alloc] initWithStartingColor:[color blendedColorWithFraction:0.45 ofColor:NSColor.whiteColor]
|
|
|
|
endingColor:color];
|
|
|
|
[gradient drawInBezierPath:bp angle:270.0];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2008-05-14 21:35:37 +00:00
|
|
|
//inside
|
2021-08-15 09:41:48 +00:00
|
|
|
bp = [NSBezierPath bezierPathWithRoundedRect:NSInsetRect(rect, 1.0, 1.0) xRadius:3.0 yRadius:3.0];
|
|
|
|
gradient = [[NSGradient alloc] initWithStartingColor:[color blendedColorWithFraction:0.75 ofColor:NSColor.whiteColor]
|
|
|
|
endingColor:[color blendedColorWithFraction:0.2 ofColor:NSColor.whiteColor]];
|
|
|
|
[gradient drawInBezierPath:bp angle:270.0];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2008-03-23 00:56:43 +00:00
|
|
|
[icon unlockFocus];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2017-07-08 14:38:47 +00:00
|
|
|
dict[@"Icon"] = icon;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2008-05-31 02:38:44 +00:00
|
|
|
return icon;
|
2008-03-23 00:56:43 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (BOOL)torrent:(Torrent*)torrent doesMatchRulesForGroupAtIndex:(NSInteger)index
|
2008-12-08 03:26:28 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
if (![self usesAutoAssignRulesForIndex:index])
|
|
|
|
{
|
2008-12-08 03:26:28 +00:00
|
|
|
return NO;
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
NSPredicate* predicate = [self autoAssignRulesForIndex:index];
|
2008-12-26 21:28:59 +00:00
|
|
|
BOOL eval = NO;
|
|
|
|
@try
|
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
eval = [predicate evaluateWithObject:torrent];
|
2008-12-26 21:28:59 +00:00
|
|
|
}
|
2021-08-15 09:41:48 +00:00
|
|
|
@catch (NSException* exception)
|
2008-12-26 21:28:59 +00:00
|
|
|
{
|
|
|
|
NSLog(@"Error when evaluating predicate (%@) - %@", predicate, exception);
|
|
|
|
}
|
|
|
|
@finally
|
|
|
|
{
|
|
|
|
return eval;
|
|
|
|
}
|
2008-12-08 03:26:28 +00:00
|
|
|
}
|
|
|
|
|
2008-03-23 00:56:43 +00:00
|
|
|
@end
|