PageRenderTime 54ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/ArchiveTo7z/GCAppDelegate.m

https://bitbucket.org/crewshin/archiveto7z
Objective C | 187 lines | 124 code | 36 blank | 27 comment | 14 complexity | d9f3e34bf32cbfb47b54b70cec0a0893 MD5 | raw file
  1. //
  2. // GCAppDelegate.m
  3. // ArchiveTo7z
  4. //
  5. // Created by Gene Crucean on 8/10/12.
  6. // Copyright (c) 2012 Gene Crucean. All rights reserved.
  7. //
  8. // Read GCConstants.h for toggles and info.
  9. //
  10. #import "GCAppDelegate.h"
  11. #import "GCConstants.h"
  12. @implementation GCAppDelegate
  13. @synthesize outputFileLabel;
  14. @synthesize outputStatusLabel;
  15. @synthesize mainWindow;
  16. - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
  17. {
  18. // Insert code here to initialize your application
  19. LOG3 ? NSLog(@"applicationDidFinishLaunching") : nil;
  20. [outputStatusLabel setStringValue:@"Application is designed to have files dropped onto the icon in the dock."];
  21. }
  22. - (void)application:(NSApplication *)theApplication openFiles:(NSArray *)filenames
  23. {
  24. LOG3 ? NSLog(@"openFiles") : nil;
  25. [outputStatusLabel setStringValue:@"Archiving files..."];
  26. if (filenames.count == 0)
  27. {
  28. // exit(0);
  29. [outputStatusLabel setStringValue:@"Application is designed to have files dropped onto the icon in the dock."];
  30. }
  31. else
  32. {
  33. [outputStatusLabel setStringValue:@"Archiving files..."];
  34. [self processFiles:filenames];
  35. }
  36. if (isWindowless)
  37. {
  38. [_window setIsVisible:NO];
  39. }
  40. else
  41. {
  42. [_window setIsVisible:YES];
  43. }
  44. }
  45. - (BOOL)processFiles:(NSArray *)files
  46. {
  47. NSLog(@"The following file has been dropped or selected: %@", files);
  48. // Set GUI's label so users can see what files were input.
  49. [outputFileLabel setStringValue:[NSString stringWithFormat:@"%@", files]];
  50. // Create NSTask.
  51. task = [[NSTask alloc] init];
  52. // Build path to 7za binary.
  53. NSString *path = [[NSBundle mainBundle] resourcePath];
  54. NSString *pathFull = [path stringByAppendingPathComponent:@"7za"];
  55. NSFileManager *fileManager = [[NSFileManager alloc] init];
  56. // Make sure 7za binary is in the proper location.
  57. if ([fileManager fileExistsAtPath:pathFull]) // 7za exists.
  58. {
  59. LOG3 ? NSLog(@"fileExists") : nil;
  60. LOG5 ? NSLog(@"launchPath: %@", pathFull) : nil;
  61. // [task setLaunchPath:@"/bin/bash"];
  62. [task setLaunchPath:pathFull];
  63. }
  64. // Tell NSTask that you want to Archive. a is archive flag doi.
  65. NSMutableArray *argumentBuilder = [[NSMutableArray alloc] init];
  66. [argumentBuilder addObject:@"a"]; // Set archive flag.
  67. // Get parent folder name.
  68. NSString *parentFolderName = [NSString stringWithFormat:@"%@", [[[files objectAtIndex:0] stringByDeletingLastPathComponent] lastPathComponent]];
  69. LOG5 ? NSLog(@"Parent folder name: %@", parentFolderName) : nil;
  70. // If more than 1 file... handle accordingly.
  71. NSString *fileOutputPath;
  72. if (files.count == 1)
  73. {
  74. fileOutputPath = [NSString stringWithFormat:@"%@.7z", [files objectAtIndex:0]]; // Format output file.
  75. LOG5 ? NSLog(@"fileOutputPath: %@", fileOutputPath) : nil;
  76. }
  77. else
  78. {
  79. fileOutputPath = [NSString stringWithFormat:@"%@/%@.7z", [[files objectAtIndex:0] stringByDeletingLastPathComponent], parentFolderName]; // Format output file.
  80. LOG5 ? NSLog(@"fileOutputPath: %@", fileOutputPath) : nil;
  81. }
  82. [argumentBuilder addObject:fileOutputPath]; // Set as argument.
  83. // Loop through input files and add to argument builder.
  84. for (int i = 0; i < files.count; i++)
  85. {
  86. [argumentBuilder addObject:[files objectAtIndex:i]]; // Add path to files to archive.
  87. LOG5 ? NSLog(@"inputFile: %@", [files objectAtIndex:i]) : nil;
  88. }
  89. // debug.
  90. // NSLog(@"argumentBuilder:%@", argumentBuilder); // Log the input arguments.
  91. // Set arguments.
  92. [task setArguments:argumentBuilder];
  93. // Launch task
  94. [task launch];
  95. // Set timer to update GUI and exit after task finishes.
  96. [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(testIfTaskIsRunning) userInfo:nil repeats:YES];
  97. return YES; // Return YES when file processed succesfull, else return NO.
  98. }
  99. - (void)testIfTaskIsRunning
  100. {
  101. if (![task isRunning])
  102. {
  103. int status = [task terminationStatus];
  104. if (status == 0)
  105. {
  106. NSLog(@"Task succeeded.");
  107. [outputStatusLabel setStringValue:@"Archiving complete. Exiting in 3 seconds."];
  108. [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(exitApp) userInfo:nil repeats:NO];
  109. if (isWindowless)
  110. {
  111. if (!didPlaySound)
  112. {
  113. NSSound *sound;
  114. sound = [[NSSound alloc] initWithContentsOfFile:@"/System/Library/Sounds/Glass.aiff" byReference:YES];
  115. [sound play];
  116. didPlaySound = YES;
  117. }
  118. // [_window setIsVisible:YES];
  119. // [_window setOrderedIndex:0];
  120. }
  121. }
  122. else
  123. {
  124. NSLog(@"Task failed.");
  125. [outputStatusLabel setStringValue:@"Oh noes! Something bombed and the task failed. Exiting in 3 seconds."];
  126. [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(exitApp) userInfo:nil repeats:NO];
  127. if (isWindowless)
  128. {
  129. if (!didPlaySound)
  130. {
  131. NSSound *sound;
  132. sound = [[NSSound alloc] initWithContentsOfFile:@"/System/Library/Sounds/Basso.aiff" byReference:YES];
  133. [sound play];
  134. didPlaySound = YES;
  135. }
  136. [_window setIsVisible:YES];
  137. [_window setOrderedIndex:0];
  138. }
  139. }
  140. }
  141. else
  142. {
  143. LOG4 ? NSLog(@"Task is still running.") : nil;
  144. [outputStatusLabel setStringValue:@"Archiving in progress..."];
  145. }
  146. }
  147. - (void)exitApp
  148. {
  149. exit(0); // BOOSH!! See ya!
  150. }
  151. @end