Skip to content

Commit

Permalink
Fix issue with folders not being created on startup
Browse files Browse the repository at this point in the history
  • Loading branch information
imothee committed Aug 23, 2018
1 parent 3894fd6 commit de8b127
Show file tree
Hide file tree
Showing 4 changed files with 318 additions and 307 deletions.
6 changes: 4 additions & 2 deletions TmpDisk.xcodeproj/project.pbxproj
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -596,8 +596,9 @@
baseConfigurationReference = 00F4F36CB9136F2BB1F0A497 /* Pods-TmpDisk.debug.xcconfig */; baseConfigurationReference = 00F4F36CB9136F2BB1F0A497 /* Pods-TmpDisk.debug.xcconfig */;
buildSettings = { buildSettings = {
CLANG_ENABLE_OBJC_ARC = YES; CLANG_ENABLE_OBJC_ARC = YES;
CODE_SIGN_IDENTITY = "Developer ID Application"; CODE_SIGN_IDENTITY = "Mac Developer";
COMBINE_HIDPI_IMAGES = YES; COMBINE_HIDPI_IMAGES = YES;
DEVELOPMENT_TEAM = B79ZU76C43;
FRAMEWORK_SEARCH_PATHS = ( FRAMEWORK_SEARCH_PATHS = (
"$(inherited)", "$(inherited)",
"\"$(SRCROOT)\"", "\"$(SRCROOT)\"",
Expand All @@ -618,8 +619,9 @@
baseConfigurationReference = 9CD599E58C7540A97A750EFD /* Pods-TmpDisk.release.xcconfig */; baseConfigurationReference = 9CD599E58C7540A97A750EFD /* Pods-TmpDisk.release.xcconfig */;
buildSettings = { buildSettings = {
CLANG_ENABLE_OBJC_ARC = YES; CLANG_ENABLE_OBJC_ARC = YES;
CODE_SIGN_IDENTITY = "Developer ID Application"; CODE_SIGN_IDENTITY = "Mac Developer";
COMBINE_HIDPI_IMAGES = YES; COMBINE_HIDPI_IMAGES = YES;
DEVELOPMENT_TEAM = B79ZU76C43;
FRAMEWORK_SEARCH_PATHS = ( FRAMEWORK_SEARCH_PATHS = (
"$(inherited)", "$(inherited)",
"\"$(SRCROOT)\"", "\"$(SRCROOT)\"",
Expand Down
196 changes: 98 additions & 98 deletions TmpDisk/AppDelegate.m
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -29,131 +29,131 @@ @implementation AppDelegate


- (void)applicationDidFinishLaunching:(NSNotification *)aNotification - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{ {
// Insert code here to initialize your application // Insert code here to initialize your application


// Check if TmpDisk was launched with command line args // Check if TmpDisk was launched with command line args
NSArray *arguments = [NSProcessInfo processInfo].arguments; NSArray *arguments = [NSProcessInfo processInfo].arguments;


NSString *argName = nil; NSString *argName = nil;
NSString *argSize = nil; NSString *argSize = nil;
// TODO: There's likely a better way to parse command line args // TODO: There's likely a better way to parse command line args
for (NSUInteger i=0, n=arguments.count; i<n; i++) { for (NSUInteger i=0, n=arguments.count; i<n; i++) {
NSString *s = arguments[i]; NSString *s = arguments[i];


// We expect args in the format -argname=argval // We expect args in the format -argname=argval


NSArray *arg = [s componentsSeparatedByString:@"="]; NSArray *arg = [s componentsSeparatedByString:@"="];
if (arg.count == 2) { if (arg.count == 2) {
if ([arg[0] isEqualToString:@"-name"]) { if ([arg[0] isEqualToString:@"-name"]) {
argName = [NSString stringWithString:arg[1]]; argName = [NSString stringWithString:arg[1]];
} else if ([arg[0] isEqualToString:@"-size"]) { } else if ([arg[0] isEqualToString:@"-size"]) {
argSize = [NSString stringWithString:arg[1]]; argSize = [NSString stringWithString:arg[1]];
} }
}
} }
}

if (argName != nil && argSize != nil) {
if (argName != nil && argSize != nil) {

int dsize = argSize.intValue;
int dsize = argSize.intValue; u_int64_t size = (((u_int64_t) dsize) * 1024 * 1024 / 512);
u_int64_t size = (((u_int64_t) dsize) * 1024 * 1024 / 512);

if(![[NSFileManager defaultManager] fileExistsAtPath:[TmpDiskManager pathForName:argName] isDirectory:nil]) {
if(![[NSFileManager defaultManager] fileExistsAtPath:[TmpDiskManager pathForName:argName] isDirectory:nil]) { [TmpDiskManager createTmpDiskWithName:argName size:size autoCreate:NO indexed:NO hidden:NO folders:[[NSArray alloc] init] onSuccess:nil];
[TmpDiskManager createTmpDiskWithName:argName size:size autoCreate:NO indexed:NO hidden:NO folders:[[NSArray alloc] init] onSuccess:nil]; }

} }

}


[TmpDiskManager autoCreateVolumesWithNames:nil]; [TmpDiskManager autoCreateVolumesWithNames:nil];
} }


- (void)awakeFromNib { - (void)awakeFromNib {

statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength]; statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];
statusItem.menu = statusMenu; statusItem.menu = statusMenu;
statusItem.image = [NSImage imageNamed:@"status.png"]; statusItem.image = [NSImage imageNamed:@"status.png"];
[statusItem setHighlightMode:YES]; [statusItem setHighlightMode:YES];

[self newTmpDiskCreated:nil]; [self newTmpDiskCreated:nil];


// Add a notification watcher to watch for disks added to refresh the menu // Add a notification watcher to watch for disks added to refresh the menu
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(newTmpDiskCreated:) name:@"TmpDiskCreated" object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(newTmpDiskCreated:) name:@"TmpDiskCreated" object:nil];

[[NSWorkspace sharedWorkspace].notificationCenter addObserver:self selector:@selector(diskUnmounted:) name:NSWorkspaceDidUnmountNotification object:nil]; [[NSWorkspace sharedWorkspace].notificationCenter addObserver:self selector:@selector(diskUnmounted:) name:NSWorkspaceDidUnmountNotification object:nil];

} }


- (void)diskUnmounted:(NSNotification *)notification { - (void)diskUnmounted:(NSNotification *)notification {

// Piggyback off a new disk created call when we hear the system unmounted a disk // Piggyback off a new disk created call when we hear the system unmounted a disk
// Clear the menu and redraw with al the valid volumes, sans any that were removed in finder or another app // Clear the menu and redraw with al the valid volumes, sans any that were removed in finder or another app
[self newTmpDiskCreated:notification]; [self newTmpDiskCreated:notification];

} }


- (void)newTmpDiskCreated:(NSNotification *)notification { - (void)newTmpDiskCreated:(NSNotification *)notification {

NSError *e = nil;
NSArray *volumes = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:@"/Volumes" error:&e];

if (e != nil) {
NSAlert *a = [NSAlert alertWithError:e];
[a runModal];
return;
}

// We have a new disk so remove all the old ones and rebuild the menu
[diskMenu removeAllItems];

for (NSString *s in volumes) {

if (![[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"/Volumes/%@/.tmpdisk", s]]) {
continue;
}

NSMenuItem *mi = [[TmpDiskMenuItem alloc] initWithTitle:s action:@selector(tmpDiskSelected:) keyEquivalent:@""
recreateBlock:^(NSString* s){

// Recreate Block passed to menu to run when the recreate button is clicked for a TmpDisk

[statusMenu cancelTrackingWithoutAnimation];

[self ejectVolumeWithName:s recreate:YES];

return;


} NSError *e = nil;
ejectBlock:^(NSString* s){ NSArray *volumes = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:@"/Volumes" error:&e];

// Eject Block passed to menu to run when the eject button is clicked for a TmpDisk

[statusMenu cancelTrackingWithoutAnimation];

[self ejectVolumeWithName:s recreate:NO];

return;


if (e != nil) {
NSAlert *a = [NSAlert alertWithError:e];
[a runModal];
return;
} }
];


[diskMenu addItem:mi]; // We have a new disk so remove all the old ones and rebuild the menu
[diskMenu removeAllItems];

for (NSString *s in volumes) {


} if (![[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"/Volumes/%@/.tmpdisk", s]]) {
continue;
}

NSMenuItem *mi = [[TmpDiskMenuItem alloc] initWithTitle:s action:@selector(tmpDiskSelected:) keyEquivalent:@""
recreateBlock:^(NSString* s){

// Recreate Block passed to menu to run when the recreate button is clicked for a TmpDisk

[statusMenu cancelTrackingWithoutAnimation];

[self ejectVolumeWithName:s recreate:YES];

return;

}
ejectBlock:^(NSString* s){

// Eject Block passed to menu to run when the eject button is clicked for a TmpDisk

[statusMenu cancelTrackingWithoutAnimation];

[self ejectVolumeWithName:s recreate:NO];

return;

}
];

[diskMenu addItem:mi];

}


} }


- (void)ejectVolumeWithName:(NSString *)name recreate:(BOOL)recreate { - (void)ejectVolumeWithName:(NSString *)name recreate:(BOOL)recreate {
[TmpDiskManager ejectVolumesWithNames:[NSSet setWithObject:name] [TmpDiskManager ejectVolumesWithNames:[NSSet setWithObject:name]
recreate:recreate]; recreate:recreate];
} }


- (void)tmpDiskSelected:(id)sender { - (void)tmpDiskSelected:(id)sender {


// When a tmpDisk menu option is selected, we open the volume in Finder // When a tmpDisk menu option is selected, we open the volume in Finder


NSString *s = [sender title]; NSString *s = [sender title];


[TmpDiskManager openVolumeWithName:s]; [TmpDiskManager openVolumeWithName:s];
} }


@end @end
Loading

0 comments on commit de8b127

Please sign in to comment.