Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/mac_osx2/src/external/cmcrashreporter/CMCrashReporterWindow.m @ 10022

Last change on this file since 10022 was 8356, checked in by rgrieder, 14 years ago

Merged remaining revisions in mac_osx to mac_osx2 branch.

File size: 6.5 KB
Line 
1//
2//  CMCrashReporterWindow.m
3//  CMCrashReporter-App
4//
5//  Created by Jelle De Laender on 20/01/08.
6//  Copyright 2008 CodingMammoth. All rights reserved.
7//  Copyright 2010 CodingMammoth. Revision. All rights reserved.
8//
9
10#import "CMCrashReporterWindow.h"
11
12@implementation CMCrashReporterWindow
13
14+ (void)runCrashReporterWithPaths:(NSArray *)ar
15{
16        CMCrashReporterWindow *windowController = [[self alloc] init];
17        [windowController setPaths:ar];
18        [[windowController window] makeKeyAndOrderFront:nil];
19}
20
21#pragma mark Default methods
22
23- (id)init
24{
25        self = [super initWithWindowNibName:NSStringFromClass([self class])];
26        if (self) {
27    // something to do?
28        }
29        return self;
30}
31
32- (NSArray *)paths {
33        return paths;
34}
35
36- (void)setPaths:(NSArray *)ar
37{
38        [paths release];
39        [ar retain];
40        paths = ar;
41}
42
43- (void)windowDidLoad
44{
45        [[self window] setTitle:[NSString stringWithFormat:@"%@ - %@ (%@) ",@"CrashReport", [CMCrashReporterGlobal appUiName],[CMCrashReporterGlobal appVersion]]];
46       
47        NSString *email = [CMCrashReporterGlobal crashReportEmail];
48        if (email!=NULL)
49            [mailaddress setStringValue:email];
50}
51
52- (void)removeCrashLog:(NSString *)path
53{
54  NSError *error = nil;
55  [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
56}
57
58- (void)updateIgnoreCrashes
59{
60        NSUserDefaults *defaults = [[NSUserDefaultsController sharedUserDefaultsController] defaults];
61        [defaults setBool:[dontShowThis state] forKey:@"CMCrashReporterIgnoreCrashes"];
62}
63
64- (IBAction)submitData:(id)sender
65{
66        sender; //unused
67
68        int i = 0;
69       
70        BOOL failures = NO;
71       
72   int max = MIN([CMCrashReporterGlobal numberOfMaximumReports],[paths count]);
73       
74        if (max == 0) max = [paths count];
75       
76        for (i = 0; i < max; i++) {
77                if ([self submitFile:[paths objectAtIndex:i]]) {
78                        // report succeed
79                        // File will be removed on close
80                } else
81                        failures = YES;
82        }
83       
84        NSAlert *alert = [[[NSAlert alloc] init] autorelease];
85        [alert addButtonWithTitle:@"OK"];
86        [alert setMessageText:[CMCrashReporterGlobal appName]];
87        [alert setAlertStyle:NSInformationalAlertStyle];
88       
89        if (!failures)
90                [alert setInformativeText:[NSString stringWithFormat:@"Thanks for helping us improve %@",[CMCrashReporterGlobal appName]]];
91        else
92                [alert setInformativeText:[NSString stringWithFormat:@"%@ was unable to send the crashlog.",[CMCrashReporterGlobal appName]]];
93               
94        [alert runModal];
95        [self close];
96}
97
98- (IBAction)dontReport:(id)sender
99{
100        sender; //unused
101        [self close];
102}
103
104- (void)close {
105        [self updateIgnoreCrashes];
106        [self windowWillClose:nil];
107        [[self window] performClose:nil];
108}
109
110- (void)windowWillClose:(NSNotification *)notification
111{
112        notification; //unused
113
114        /* Window need to close -- We remove all the reports -- */
115        unsigned int i = 0;
116        for (i = 0; i < [paths count]; i++) {
117                if ([[NSFileManager defaultManager] fileExistsAtPath:[paths objectAtIndex:i]])
118                        [self removeCrashLog:[paths objectAtIndex:i]];
119        }
120}
121
122#pragma mark send
123
124-(BOOL)submitFile:(NSString *)file
125{
126        NSMutableDictionary* post_dict = [[NSMutableDictionary alloc] init];
127
128        [post_dict setValue:[NSString stringWithString:@"CMCrashReporter"] forKey:@"type"];
129        [post_dict setValue:[CMCrashReporterGlobal appName] forKey:@"application"];
130        [post_dict setValue:[CMCrashReporterGlobal version] forKey:@"appVersion"];
131        [post_dict setValue:[CMCrashReporterGlobal osVersion] forKey:@"osVersion"];
132        [post_dict setValue:[NSString stringWithString:[mailaddress stringValue]] forKey:@"mailaddress"];
133        [post_dict setValue:[NSString stringWithString:[[commentField textStorage] string]] forKey:@"comments"];
134        [post_dict setValue: [[[[NSDateFormatter alloc] initWithDateFormat:@"%H:%M:%S" allowNaturalLanguage:NO] autorelease] stringFromDate:[NSDate date]] forKey:@"time"];
135        [post_dict setValue: [[[[NSDateFormatter alloc] initWithDateFormat:@"%m/%d/%Y" allowNaturalLanguage:NO] autorelease] stringFromDate:[NSDate date]] forKey:@"date"];
136       
137        if ([includeRapport state])
138                [post_dict setValue:[NSString stringWithContentsOfFile:file encoding:NSUTF8StringEncoding error:nil] forKey:@"rapport"];
139        else
140                [post_dict setValue:@"not included" forKey:@"rapport"];
141       
142        NSData* regData = [self generateFormData:post_dict];
143        [post_dict release];
144
145        NSURL *url = [NSURL URLWithString:[CMCrashReporterGlobal crashReportURL]];
146        NSMutableURLRequest* post = [NSMutableURLRequest requestWithURL:url];
147        [post addValue: @"multipart/form-data; boundary=_insert_some_boundary_here_" forHTTPHeaderField: @"Content-Type"];
148        [post setHTTPMethod: @"POST"];
149        [post setHTTPBody:regData];
150       
151        NSURLResponse* response;
152        NSError* error;
153        NSData* result = [NSURLConnection sendSynchronousRequest:post returningResponse:&response error:&error];
154  NSString *res = [[[NSString alloc] initWithData:result encoding:NSASCIIStringEncoding] autorelease];
155  NSString *compare = [res stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
156  return ([compare isEqualToString:@"ok"]);
157
158}
159
160#pragma mark Generate form data
161
162- (NSData*)generateFormData:(NSDictionary*)dict
163{
164        NSString* boundary = [NSString stringWithString:@"_insert_some_boundary_here_"];
165        NSArray* keys = [dict allKeys];
166        NSMutableData* result = [[NSMutableData alloc] initWithCapacity:100];
167
168        unsigned int i;
169        for (i = 0; i < [keys count]; i++)
170        {
171                id value = [dict valueForKey: [keys objectAtIndex: i]];
172                [result appendData:[[NSString stringWithFormat:@"--%@\n", boundary] dataUsingEncoding:NSASCIIStringEncoding]];
173                if ( ([value class] == [NSString class]) || ([@"tmp" isKindOfClass:[value class]]))
174                {
175                        [result appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\n\n", [keys objectAtIndex:i]] dataUsingEncoding:NSASCIIStringEncoding]];
176                        [result appendData:[[NSString stringWithFormat:@"%@",value] dataUsingEncoding:NSASCIIStringEncoding]];
177                }
178                else if ([value class] == [NSURL class] && [value isFileURL])
179                {
180                        [result appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\n", [keys objectAtIndex:i], [[value path] lastPathComponent]] dataUsingEncoding:NSASCIIStringEncoding]];
181                        [result appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\n\n"] dataUsingEncoding:NSASCIIStringEncoding]];
182                        [result appendData:[NSData dataWithContentsOfFile:[value path]]];
183                }
184                else
185                {
186                        NSLog(@"No string or NSURL!");
187                }
188               
189                [result appendData:[[NSString stringWithString:@"\n"] dataUsingEncoding:NSASCIIStringEncoding]];
190        }
191        [result appendData:[[NSString stringWithFormat:@"--%@--\n", boundary] dataUsingEncoding:NSASCIIStringEncoding]];
192       
193        return [result autorelease];
194}
195@end
Note: See TracBrowser for help on using the repository browser.