Skip to content

Commit

Permalink
Services Preferences: group service data related methods as category …
Browse files Browse the repository at this point in the history
…to GSServicesManager.
  • Loading branch information
trunkmaster committed Apr 25, 2024
1 parent 21a810d commit 9582f3b
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 60 deletions.
3 changes: 1 addition & 2 deletions Applications/Preferences/Modules/Services/Services.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@

GSServicesManager *serviceManager;
NSDictionary *allServices;
NSDictionary *disabledServices;
NSArray *disabledServices;
NSMutableDictionary *services;
}

@end

114 changes: 56 additions & 58 deletions Applications/Preferences/Modules/Services/Services.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,61 +21,31 @@
//

#import "Services.h"
#include "AppKit/NSFont.h"

@implementation Services

static NSBundle *bundle = nil;

- (id)init
{
self = [super init];

bundle = [NSBundle bundleForClass:[self class]];
NSString *imagePath = [bundle pathForResource:@"Services" ofType:@"tiff"];
image = [[NSImage alloc] initWithContentsOfFile:imagePath];

return self;
}
@implementation GSServicesManager (Preferences)

- (void)dealloc
- (NSDictionary *)allServices
{
[image release];
[super dealloc];
}

- (void)_updateAllServices
{
NSString *_servicesPath;
NSDictionary *_allServices = nil;

_servicesPath = [NSHomeDirectory() stringByAppendingString:@"/Library/Services/.GNUstepServices"];
NSDictionary *_services = nil;

if ([[NSFileManager defaultManager] fileExistsAtPath:_servicesPath]) {
NSData *data = [NSData dataWithContentsOfFile:_servicesPath];

if (data) {
id plist = [NSDeserializer deserializePropertyListFromData:data mutableContainers:YES];
if (plist) {
[[NSDictionary dictionaryWithDictionary:plist] writeToFile:@"SERVICES.plist"
atomically:YES];
_allServices =
_services =
[[NSDictionary alloc] initWithDictionary:[plist valueForKeyPath:@"ByService.default"]];
ASSIGN(allServices, _allServices);
[_allServices release];
}
}
}

return;
return _services;
}

- (void)_updateDisabledServices
- (NSArray *)disabledServices
{
NSString *_disabledPath;
NSArray *_disabledServices = nil;

_disabledPath = [NSHomeDirectory() stringByAppendingString:@"/Library/Services/.GNUstepDisabled"];
NSArray *_services = nil;

if ([[NSFileManager defaultManager] fileExistsAtPath:_disabledPath]) {
NSData *data = [NSData dataWithContentsOfFile:_disabledPath];
Expand All @@ -84,23 +54,59 @@ - (void)_updateDisabledServices
id plist = [NSDeserializer deserializePropertyListFromData:data mutableContainers:NO];
if (plist) {
NSLog(@"Disabled SERVICES: %@", plist);
_disabledServices = [[NSArray alloc] initWithArray:plist];
ASSIGN(disabledServices, _disabledServices);
[_disabledServices release];
_services = [[NSArray alloc] initWithArray:plist];
}
}
}

return _services;
}

- (NSString *)itemTitleForService:(NSDictionary *)svc menuLevel:(int)level
{
NSString *itemTitle = [svc valueForKeyPath:@"NSMenuItem.default"];
NSArray *components = [itemTitle pathComponents];

if (components.count == 0) {
return nil;
}

return components[level];
}

@end

@implementation Services

static NSBundle *bundle = nil;

- (id)init
{
self = [super init];

bundle = [NSBundle bundleForClass:[self class]];
NSString *imagePath = [bundle pathForResource:@"Services" ofType:@"tiff"];
image = [[NSImage alloc] initWithContentsOfFile:imagePath];

return self;
}

- (void)dealloc
{
[allServices release];
[disabledServices release];
[image release];
[super dealloc];
}

- (void)_fetchServices
{
NSString *appName;
NSMutableDictionary *applications = [NSMutableDictionary new];
NSMutableArray *appServicesList;

[self _updateAllServices];
[self _updateDisabledServices];
ASSIGN(allServices, [serviceManager allServices]);
ASSIGN(disabledServices, [serviceManager disabledServices]);

for (NSDictionary *svc in [allServices allValues]) {
appName = [svc valueForKey:@"NSPortName"];
Expand Down Expand Up @@ -132,6 +138,8 @@ - (void)awakeFromNib
[servicesList loadColumnZero];
}

#pragma mark - Protocol

- (NSView *)view
{
if (view == nil) {
Expand Down Expand Up @@ -201,23 +209,15 @@ - (void)serviceListClicked:(id)sender
} else {
[actionButton setTitle:@"Disable"];
}
[actionButton setEnabled:YES];
} else {
[actionButton setEnabled:NO];
[actionButton setTitle:@"Disable"];
}
}

#pragma mark - Browser

- (NSString *)menuItemForService:(NSDictionary *)svc level:(int)level
{
NSString *itemTitle = [svc valueForKeyPath:@"NSMenuItem.default"];
NSArray *components = [itemTitle pathComponents];

if (components.count == 0) {
return nil;
}

return components[level];
}

- (BOOL)menuItemIsOnly:(NSArray *)svc
{
if ([svc count] > 1) {
Expand All @@ -241,16 +241,14 @@ - (void)browser:(NSBrowser *)browser
NSArray *apps = [services allKeys];
NSArray *appServices = services[apps[row]];

[cell setStringValue:[self menuItemForService:appServices[0] level:0]];
[cell setStringValue:[serviceManager itemTitleForService:appServices[0] menuLevel:0]];
[cell setLeaf:[self menuItemIsOnly:appServices]];
[cell setRepresentedObject:appServices];
} else {
NSArray *appServices = [[browser selectedCellInColumn:0] representedObject];
NSDictionary *svc = [appServices objectAtIndex:row];

[cell setStringValue:[self menuItemForService:svc level:1]];
// [cell setEnabled:[serviceManager
// showsServicesMenuItem:[svc valueForKeyPath:@"NSMenuItem.default"]]];
[cell setStringValue:[serviceManager itemTitleForService:svc menuLevel:1]];
if ([serviceManager showsServicesMenuItem:[svc valueForKeyPath:@"NSMenuItem.default"]] == NO) {
[cell setFont:[NSFont fontWithName:@"Helvetica-Oblique" size:12.0]];
}
Expand Down

0 comments on commit 9582f3b

Please sign in to comment.