MenuManager.m 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /**
  2. * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
  3. *
  4. * This library is free software; you can redistribute it and/or modify it under
  5. * the terms of the GNU Lesser General Public License as published by the Free
  6. * Software Foundation; either version 2.1 of the License, or (at your option)
  7. * any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  12. * details.
  13. */
  14. #import "MenuManager.h"
  15. #import "Finder/Finder.h"
  16. #import "RequestManager.h"
  17. @implementation MenuManager
  18. static MenuManager* sharedInstance = nil;
  19. + (MenuManager*)sharedInstance
  20. {
  21. @synchronized(self)
  22. {
  23. if (sharedInstance == nil)
  24. {
  25. sharedInstance = [[self alloc] init];
  26. }
  27. }
  28. return sharedInstance;
  29. }
  30. - init
  31. {
  32. return [super init];
  33. }
  34. - (void)addChildrenSubMenuItems:(NSMenuItem*)parentMenuItem withChildren:(NSArray*)menuItemsDictionaries forFiles:(NSArray*)files
  35. {
  36. NSMenu* menu = [[NSMenu alloc] init];
  37. for (int i = 0; i < [menuItemsDictionaries count]; ++i)
  38. {
  39. NSDictionary* menuItemDictionary = [menuItemsDictionaries objectAtIndex:i];
  40. NSString* submenuTitle = [menuItemDictionary objectForKey:@"title"];
  41. BOOL enabled = [[menuItemDictionary objectForKey:@"enabled"] boolValue];
  42. NSString* uuid = [menuItemDictionary objectForKey:@"uuid"];
  43. NSArray* childrenSubMenuItems = (NSArray*)[menuItemDictionary objectForKey:@"contextMenuItems"];
  44. if ([submenuTitle isEqualToString:@"_SEPARATOR_"])
  45. {
  46. [menu addItem:[NSMenuItem separatorItem]];
  47. }
  48. else if (childrenSubMenuItems != nil && [childrenSubMenuItems count] != 0)
  49. {
  50. NSMenuItem* submenuItem = [menu addItemWithTitle:submenuTitle action:nil keyEquivalent:@""];
  51. [self addChildrenSubMenuItems:submenuItem withChildren:childrenSubMenuItems forFiles:files];
  52. }
  53. else
  54. {
  55. [self createActionMenuItemIn:menu withTitle:submenuTitle withIndex:i enabled:enabled withUuid:uuid forFiles:files];
  56. }
  57. }
  58. [parentMenuItem setSubmenu:menu];
  59. [menu release];
  60. }
  61. - (void)addItemsToMenu:(TContextMenu*)menu forFiles:(NSArray*)files
  62. {
  63. NSArray* menuItemsArray = [[RequestManager sharedInstance] menuItemsForFiles:files];
  64. if (menuItemsArray == nil)
  65. {
  66. return;
  67. }
  68. if ([menuItemsArray count] == 0)
  69. {
  70. return;
  71. }
  72. NSInteger menuIndex = 4;
  73. BOOL hasSeparatorBefore = [[menu itemAtIndex:menuIndex - 1] isSeparatorItem];
  74. if (!hasSeparatorBefore)
  75. {
  76. [menu insertItem:[NSMenuItem separatorItem] atIndex:menuIndex];
  77. }
  78. for (int i = 0; i < [menuItemsArray count]; ++i)
  79. {
  80. NSDictionary* menuItemDictionary = [menuItemsArray objectAtIndex:i];
  81. NSString* mainMenuTitle = [menuItemDictionary objectForKey:@"title"];
  82. if ([mainMenuTitle isEqualToString:@""])
  83. {
  84. continue;
  85. }
  86. menuIndex++;
  87. BOOL enabled = [[menuItemDictionary objectForKey:@"enabled"] boolValue];
  88. NSString* uuid = [menuItemDictionary objectForKey:@"uuid"];
  89. NSArray* childrenSubMenuItems = (NSArray*)[menuItemDictionary objectForKey:@"contextMenuItems"];
  90. if (childrenSubMenuItems != nil && [childrenSubMenuItems count] != 0)
  91. {
  92. NSMenuItem* mainMenuItem = [menu insertItemWithTitle:mainMenuTitle action:nil keyEquivalent:@"" atIndex:menuIndex];
  93. [self addChildrenSubMenuItems:mainMenuItem withChildren:childrenSubMenuItems forFiles:files];
  94. }
  95. else
  96. {
  97. [self createActionMenuItemIn:menu withTitle:mainMenuTitle withIndex:menuIndex enabled:enabled withUuid:uuid forFiles:files];
  98. }
  99. }
  100. BOOL hasSeparatorAfter = [[menu itemAtIndex:menuIndex + 1] isSeparatorItem];
  101. if (!hasSeparatorAfter)
  102. {
  103. [menu insertItem:[NSMenuItem separatorItem] atIndex:menuIndex + 1];
  104. }
  105. }
  106. - (void)createActionMenuItemIn:(NSMenu*)menu withTitle:(NSString*)title withIndex:(NSInteger*)index enabled:(BOOL)enabled withUuid:(NSString*)uuid forFiles:(NSArray*)files
  107. {
  108. NSMenuItem* mainMenuItem = [menu insertItemWithTitle:title action:@selector(menuItemClicked:) keyEquivalent:@"" atIndex:index];
  109. if (enabled)
  110. {
  111. [mainMenuItem setTarget:self];
  112. }
  113. NSDictionary* menuActionDictionary = [[NSMutableDictionary alloc] init];
  114. [menuActionDictionary setValue:uuid forKey:@"uuid"];
  115. NSMutableArray* filesArray = [files copy];
  116. [menuActionDictionary setValue:filesArray forKey:@"files"];
  117. [mainMenuItem setRepresentedObject:menuActionDictionary];
  118. [filesArray release];
  119. [menuActionDictionary release];
  120. }
  121. - (void)menuItemClicked:(id)param
  122. {
  123. [[RequestManager sharedInstance] menuItemClicked:[param representedObject]];
  124. }
  125. - (NSArray*)pathsForNodes:(const struct TFENodeVector*)nodes
  126. {
  127. struct TFENode* start = nodes->_M_impl._M_start;
  128. struct TFENode* end = nodes->_M_impl._M_finish;
  129. int count = end - start;
  130. NSMutableArray* selectedItems = [[NSMutableArray alloc] initWithCapacity:count];
  131. struct TFENode* current;
  132. for (current = start; current < end; ++current)
  133. {
  134. FINode* node = (FINode*)[NSClassFromString(@"FINode") nodeFromNodeRef:current->fNodeRef];
  135. NSString* path = [[node previewItemURL] path];
  136. if (path)
  137. {
  138. [selectedItems addObject:path];
  139. }
  140. }
  141. return [selectedItems autorelease];
  142. }
  143. @end