2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright © 2007-2022 Transmission authors and contributors.
|
|
|
|
// It may be used under the MIT (SPDX: MIT) license.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2008-03-23 00:56:43 +00:00
|
|
|
|
|
|
|
#import "GroupsController.h"
|
2022-06-08 21:35:51 +00:00
|
|
|
#import "NSImageAdditions.h"
|
2012-01-04 00:06:30 +00:00
|
|
|
#import "NSMutableArrayAdditions.h"
|
2008-03-23 00:56:43 +00:00
|
|
|
|
2022-10-19 19:28:21 +00:00
|
|
|
static CGFloat const kIconWidth = 16.0;
|
|
|
|
static CGFloat const kBorderWidth = 1.25;
|
|
|
|
static CGFloat const kIconWidthSmall = 12.0;
|
2008-03-23 00:56:43 +00:00
|
|
|
|
2022-01-24 01:32:45 +00:00
|
|
|
@interface GroupsController ()
|
2008-03-23 00:56:43 +00:00
|
|
|
|
2022-04-29 22:51:40 +00:00
|
|
|
@property(nonatomic, readonly) NSMutableArray<NSMutableDictionary*>* fGroups;
|
2022-02-22 16:04:20 +00:00
|
|
|
|
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"]))
|
|
|
|
{
|
2022-06-18 14:26:45 +00:00
|
|
|
_fGroups = [NSKeyedUnarchiver unarchivedObjectOfClasses:[NSSet setWithObjects:NSMutableArray.class,
|
|
|
|
NSMutableDictionary.class,
|
|
|
|
NSNumber.class,
|
|
|
|
NSColor.class,
|
|
|
|
NSString.class,
|
|
|
|
NSPredicate.class,
|
|
|
|
nil]
|
|
|
|
fromData:data
|
|
|
|
error:nil];
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
|
|
|
else if ((data = [NSUserDefaults.standardUserDefaults dataForKey:@"Groups"])) //handle old groups
|
2008-12-24 17:41:45 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
_fGroups = [NSUnarchiver unarchiveObjectWithData:data];
|
2021-08-15 09:41:48 +00:00
|
|
|
[NSUserDefaults.standardUserDefaults removeObjectForKey:@"Groups"];
|
2008-12-24 18:50:57 +00:00
|
|
|
[self saveGroups];
|
2008-12-24 17:41:45 +00:00
|
|
|
}
|
2022-04-29 21:44:59 +00:00
|
|
|
if (_fGroups == nil)
|
2008-03-23 00:56:43 +00:00
|
|
|
{
|
|
|
|
//default groups
|
2021-08-15 09:41:48 +00:00
|
|
|
NSMutableDictionary* red = [NSMutableDictionary
|
2022-05-14 00:54:10 +00:00
|
|
|
dictionaryWithObjectsAndKeys:NSColor.systemRedColor, @"Color", NSLocalizedString(@"Red", "Groups -> Name"), @"Name", @0, @"Index", nil];
|
2021-08-15 09:41:48 +00:00
|
|
|
|
|
|
|
NSMutableDictionary* orange = [NSMutableDictionary
|
2022-05-14 00:54:10 +00:00
|
|
|
dictionaryWithObjectsAndKeys:NSColor.systemOrangeColor, @"Color", NSLocalizedString(@"Orange", "Groups -> Name"), @"Name", @1, @"Index", nil];
|
2021-08-15 09:41:48 +00:00
|
|
|
|
|
|
|
NSMutableDictionary* yellow = [NSMutableDictionary
|
2022-05-14 00:54:10 +00:00
|
|
|
dictionaryWithObjectsAndKeys:NSColor.systemYellowColor, @"Color", NSLocalizedString(@"Yellow", "Groups -> Name"), @"Name", @2, @"Index", nil];
|
2021-08-15 09:41:48 +00:00
|
|
|
|
|
|
|
NSMutableDictionary* green = [NSMutableDictionary
|
2022-05-14 00:54:10 +00:00
|
|
|
dictionaryWithObjectsAndKeys:NSColor.systemGreenColor, @"Color", NSLocalizedString(@"Green", "Groups -> Name"), @"Name", @3, @"Index", nil];
|
2021-08-15 09:41:48 +00:00
|
|
|
|
|
|
|
NSMutableDictionary* blue = [NSMutableDictionary
|
2022-05-14 00:54:10 +00:00
|
|
|
dictionaryWithObjectsAndKeys:NSColor.systemBlueColor, @"Color", NSLocalizedString(@"Blue", "Groups -> Name"), @"Name", @4, @"Index", nil];
|
2021-08-15 09:41:48 +00:00
|
|
|
|
|
|
|
NSMutableDictionary* purple = [NSMutableDictionary
|
2022-05-14 00:54:10 +00:00
|
|
|
dictionaryWithObjectsAndKeys:NSColor.systemPurpleColor, @"Color", NSLocalizedString(@"Purple", "Groups -> Name"), @"Name", @5, @"Index", nil];
|
2021-08-15 09:41:48 +00:00
|
|
|
|
|
|
|
NSMutableDictionary* gray = [NSMutableDictionary
|
2022-05-14 00:54:10 +00:00
|
|
|
dictionaryWithObjectsAndKeys:NSColor.systemGrayColor, @"Color", NSLocalizedString(@"Gray", "Groups -> Name"), @"Name", @6, @"Index", nil];
|
2021-08-15 09:41:48 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
_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
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
return self.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)
|
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
for (NSUInteger i = 0; i < self.fGroups.count; i++)
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
if (index == [self.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
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
return [self.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];
|
2022-02-22 16:04:20 +00:00
|
|
|
return orderIndex != -1 ? self.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];
|
2022-02-22 16:04:20 +00:00
|
|
|
self.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];
|
2022-05-26 21:04:20 +00:00
|
|
|
return orderIndex != -1 ? [self imageForGroup:self.fGroups[orderIndex]] : [self imageForGroupNone];
|
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];
|
2022-02-22 16:04:20 +00:00
|
|
|
return orderIndex != -1 ? self.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
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
NSMutableDictionary* dict = self.fGroups[[self rowValueForIndex:index]];
|
2021-08-15 09:41:48 +00:00
|
|
|
[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];
|
2022-02-22 16:04:20 +00:00
|
|
|
return [self.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
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
NSMutableDictionary* dict = self.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];
|
2022-02-22 16:04:20 +00:00
|
|
|
return orderIndex != -1 ? self.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
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
NSMutableDictionary* dict = self.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
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
NSNumber* assignRules = self.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
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
NSMutableDictionary* dict = self.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
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
return self.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
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
NSMutableDictionary* dict = self.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
|
2022-02-22 16:04:20 +00:00
|
|
|
NSMutableIndexSet* candidates = [NSMutableIndexSet indexSetWithIndexesInRange:NSMakeRange(0, self.fGroups.count + 1)];
|
|
|
|
for (NSDictionary* dict in self.fGroups)
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
|
|
|
[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
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
[self.fGroups addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:@(index),
|
2022-03-30 21:52:23 +00:00
|
|
|
@"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
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
NSInteger index = [self.fGroups[row][@"Index"] integerValue];
|
|
|
|
[self.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
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
[self.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
|
|
|
{
|
2022-10-31 14:37:34 +00:00
|
|
|
NSMenu* menu = [[NSMenu alloc] initWithTitle:@""];
|
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
|
|
|
|
2022-05-26 21:04:20 +00:00
|
|
|
NSImage* icon = [self imageForGroupNone];
|
2008-05-31 02:52:49 +00:00
|
|
|
if (small)
|
|
|
|
{
|
2008-06-01 19:08:38 +00:00
|
|
|
icon = [icon copy];
|
2022-10-19 19:28:21 +00:00
|
|
|
icon.size = NSMakeSize(kIconWidthSmall, kIconWidthSmall);
|
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
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
for (NSMutableDictionary* dict in self.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];
|
2022-10-19 19:28:21 +00:00
|
|
|
icon.size = NSMakeSize(kIconWidthSmall, kIconWidthSmall);
|
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
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
for (NSDictionary* group in self.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
|
|
|
}
|
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
#pragma mark - 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
|
2022-02-22 16:04:20 +00:00
|
|
|
NSMutableArray* groups = [NSMutableArray arrayWithCapacity:self.fGroups.count];
|
|
|
|
for (NSDictionary* dict in self.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
|
|
|
}
|
|
|
|
|
2022-05-26 21:04:20 +00:00
|
|
|
- (NSImage*)imageForGroupNone
|
2008-03-23 00:56:43 +00:00
|
|
|
{
|
2022-05-26 21:04:20 +00:00
|
|
|
static NSImage* icon;
|
|
|
|
if (icon)
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2022-05-26 21:04:20 +00:00
|
|
|
return icon;
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-10-19 19:28:21 +00:00
|
|
|
icon = [NSImage imageWithSize:NSMakeSize(kIconWidth, kIconWidth) flipped:NO drawingHandler:^BOOL(NSRect rect) {
|
2022-05-26 21:04:20 +00:00
|
|
|
//shape
|
2022-10-19 19:28:21 +00:00
|
|
|
rect = NSInsetRect(rect, kBorderWidth / 2, kBorderWidth / 2);
|
2022-05-26 21:04:20 +00:00
|
|
|
NSBezierPath* bp = [NSBezierPath bezierPathWithOvalInRect:rect];
|
2022-10-19 19:28:21 +00:00
|
|
|
bp.lineWidth = kBorderWidth;
|
2022-05-26 21:04:20 +00:00
|
|
|
|
|
|
|
//border
|
|
|
|
// code reference for dashed style
|
|
|
|
//CGFloat dashAndGapLength = M_PI * rect.size.width / 8;
|
|
|
|
//CGFloat pattern[2] = { dashAndGapLength * .5, dashAndGapLength * .5 };
|
|
|
|
//[bp setLineDash:pattern count:2 phase:0];
|
|
|
|
|
|
|
|
[NSColor.controlTextColor setStroke];
|
|
|
|
[bp stroke];
|
2022-05-14 00:54:10 +00:00
|
|
|
|
2022-05-26 21:04:20 +00:00
|
|
|
return YES;
|
|
|
|
}];
|
|
|
|
[icon setTemplate:YES];
|
|
|
|
|
|
|
|
return icon;
|
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-05-26 21:04:20 +00:00
|
|
|
- (NSImage*)imageForGroup:(NSMutableDictionary*)dict
|
|
|
|
{
|
|
|
|
NSImage* icon;
|
|
|
|
if ((icon = dict[@"Icon"]))
|
|
|
|
{
|
|
|
|
return icon;
|
|
|
|
}
|
|
|
|
|
2022-06-08 21:35:51 +00:00
|
|
|
icon = [NSImage discIconWithColor:dict[@"Color"] insetFactor:0];
|
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];
|
2022-06-14 15:17:02 +00:00
|
|
|
[predicate allowEvaluation];
|
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
|