PageRenderTime 72ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 0ms

/jira/mobile/ios/Classes/TiJiraModule.m

https://bitbucket.org/plepic/titanium_modules
Objective C | 225 lines | 173 code | 41 blank | 11 comment | 16 complexity | 7d58244abd823117521b11941537ff38 MD5 | raw file
  1. /**
  2. * Ti.Jira Module
  3. * Copyright (c) 2011-2013 by Appcelerator, Inc. All Rights Reserved.
  4. * Please see the LICENSE included with this distribution for details.
  5. */
  6. #import "TiJiraModule.h"
  7. @implementation TiJiraModule
  8. #pragma mark Internal
  9. -(id)moduleGUID
  10. {
  11. return @"6e69fb14-df2b-4e78-8708-e8c0c3df770d";
  12. }
  13. -(NSString*)moduleId
  14. {
  15. return @"ti.jira";
  16. }
  17. #pragma mark Lifecycle
  18. -(void)startup
  19. {
  20. [super startup];
  21. [self rememberSelf];
  22. }
  23. -(void)shutdown:(id)sender
  24. {
  25. [super shutdown:sender];
  26. [self forgetSelf];
  27. }
  28. #pragma mark Utility Methods
  29. -(JMCAttachmentItem*)processAttachment:(id)arg
  30. {
  31. NSData* data;
  32. NSString* name;
  33. NSString* contentType = @"text/plain";
  34. JMCAttachmentType type = JMCAttachmentTypeCustom;
  35. if ([arg isKindOfClass:[TiBlob class]])
  36. {
  37. name = @"unknown";
  38. TiBlob* blob = (TiBlob*)arg;
  39. data = [blob data];
  40. if (blob.type == TiBlobTypeImage) {
  41. type = JMCAttachmentTypeImage;
  42. }
  43. }
  44. else if ([arg isKindOfClass:[TiFile class]])
  45. {
  46. TiFile *file = (TiFile*)arg;
  47. NSString *path = [file path];
  48. name = [path lastPathComponent];
  49. data = [NSData dataWithContentsOfFile:path];
  50. contentType = [Mimetypes mimeTypeForExtension:path];
  51. }
  52. else if ([arg isKindOfClass:[NSString class]]) {
  53. NSURL* url = [TiUtils toURL:arg proxy:self];
  54. name = [arg lastPathComponent];
  55. data = [NSURLConnection sendSynchronousRequest:[NSURLRequest requestWithURL:url]
  56. returningResponse:nil
  57. error:nil];
  58. contentType = [Mimetypes mimeTypeForExtension:name];
  59. }
  60. else {
  61. [self throwException:@"Unsupported attachment provided!" subreason:@"Please provide a URL, TiFile, or Blob!" location:CODELOCATION];
  62. }
  63. if ([contentType hasPrefix:@"image/"]) {
  64. type = JMCAttachmentTypeImage;
  65. }
  66. NSLog(@"Attachment provided with %@, %@", name, [name stringByDeletingPathExtension]);
  67. NSLog(@"Content Type: %@", contentType);
  68. NSLog(@"Type: %d", type);
  69. NSLog(@"Data: %@", data);
  70. JMCAttachmentItem* item = [[JMCAttachmentItem alloc] initWithName:[name stringByDeletingPathExtension]
  71. data:data
  72. type:type
  73. contentType:contentType
  74. filenameFormat:name];
  75. return item;
  76. }
  77. #pragma mark Public API
  78. -(void)initialize:(id)args
  79. {
  80. ENSURE_UI_THREAD(initialize, args);
  81. ENSURE_SINGLE_ARG(args, NSDictionary);
  82. [[JMC instance] configureWithOptions:
  83. [JMCOptions optionsWithUrl:[args valueForKey:@"url"]
  84. project:[args valueForKey:@"projectKey"]
  85. apiKey:[args valueForKey:@"apiKey"]
  86. photos:[TiUtils boolValue:[args valueForKey:@"allowPhotos"] def:YES]
  87. voice:[TiUtils boolValue:[args valueForKey:@"allowVoice"] def:YES]
  88. location:[TiUtils boolValue:[args valueForKey:@"allowLocation"] def:YES]
  89. crashreporting:[TiUtils boolValue:[args valueForKey:@"allowCrashReporting"] def:YES]
  90. customFields:[args valueForKey:@"customFields"]]];
  91. _pinger = [[[JMCPing alloc] init] retain];
  92. _issueTransport = [[JMCIssueTransport alloc] init];
  93. _issueTransport.delegate = [[TiJiraIssueDelegate alloc] initWithParentProxy:self];
  94. [JMCRequestQueue sharedInstance].issueTransport = _issueTransport;
  95. _replyTransport = [[JMCReplyTransport alloc] init];
  96. _replyTransport.delegate = [[TiJiraReplyDelegate alloc] initWithParentProxy:self];
  97. [JMCRequestQueue sharedInstance].replyTransport = _replyTransport;
  98. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedComments:) name:kJMCReceivedCommentsNotification object:nil];
  99. }
  100. -(void)ping:(id)arguments
  101. {
  102. [_pinger sendPing];
  103. }
  104. -(void)receivedComments:(id)args
  105. {
  106. [self fireEvent:@"receivedComments"];
  107. }
  108. -(void)submitIssue:(id)arguments
  109. {
  110. id props = [arguments objectAtIndex:0];
  111. NSString* description = [props valueForKey:@"description"];
  112. NSArray* rawAttachments = [props valueForKey:@"attachments"];
  113. if ([description stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]].length <= 0 && rawAttachments.count <= 0) {
  114. // No data entered, just return.
  115. return;
  116. }
  117. // parse the raw attachments in to JMCAttachmentItems
  118. NSMutableArray* attachments = [NSMutableArray arrayWithCapacity:[rawAttachments count]];
  119. for (id attachment in rawAttachments) {
  120. [attachments addObject:[self processAttachment:attachment]];
  121. }
  122. // add all custom fields as one attachment item
  123. NSMutableDictionary* customFields = [[JMC instance] getCustomFields];
  124. NSData* customFieldsJSON = [[customFields JSONRepresentation] dataUsingEncoding:NSUTF8StringEncoding];
  125. JMCAttachmentItem* customFieldsItem = [[JMCAttachmentItem alloc] initWithName:@"customfields"
  126. data:customFieldsJSON
  127. type:JMCAttachmentTypeCustom
  128. contentType:@"application/json"
  129. filenameFormat:@"customfields.json"];
  130. [attachments addObject:customFieldsItem];
  131. [customFieldsItem release];
  132. // use the first 80 chars of the description as the issue summary
  133. u_int toIndex = [description length] > 80 ? 80 : [description length];
  134. NSString* truncationMarker = [description length] > 80 ? @"..." : @"";
  135. [_issueTransport send:[[description substringToIndex:toIndex] stringByAppendingString:truncationMarker]
  136. description:description
  137. attachments:attachments];
  138. }
  139. -(void)commentOnIssue:(id)arguments
  140. {
  141. id props = [arguments objectAtIndex:0];
  142. NSString* description = [props valueForKey:@"description"];
  143. NSArray* rawAttachments = [props valueForKey:@"attachments"];
  144. if ([description stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]].length <= 0 && rawAttachments.count <= 0) {
  145. // No data entered, just return.
  146. return;
  147. }
  148. // parse the raw attachments in to JMCAttachmentItems
  149. NSMutableArray* attachments = [NSMutableArray arrayWithCapacity:[rawAttachments count]];
  150. for (id attachment in rawAttachments) {
  151. [attachments addObject:[self processAttachment:attachment]];
  152. }
  153. NSString* issueKey = [props valueForKey:@"key"];
  154. JMCIssue* issue = [[JMCIssue alloc] initWithDictionary:[NSDictionary dictionaryWithObject:issueKey forKey:@"key"]];
  155. [_replyTransport sendReply:issue
  156. description:description
  157. attachments:attachments];
  158. [issue release];
  159. }
  160. -(id)retrieveIssues:(id)args
  161. {
  162. JMCIssueStore* store = [JMCIssueStore instance];
  163. int count = [store count];
  164. NSMutableArray* retVal = [NSMutableArray arrayWithCapacity:count];
  165. for (int i = 0; i < count; i++) {
  166. [retVal addObject:[TiJiraIssueProxy issueWithIssue:[store newIssueAtIndex:i]]];
  167. }
  168. return retVal;
  169. }
  170. #pragma mark Internal Memory Management
  171. -(void)didReceiveMemoryWarning:(NSNotification*)notification
  172. {
  173. [super didReceiveMemoryWarning:notification];
  174. }
  175. -(void)dealloc
  176. {
  177. [_issueTransport.delegate release];
  178. [_issueTransport release];
  179. [_replyTransport.delegate release];
  180. [_replyTransport release];
  181. [_pinger release];
  182. [super dealloc];
  183. }
  184. @end