forked from omz/AppSales-Mobile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImportExportViewController.m
213 lines (177 loc) · 7.62 KB
/
ImportExportViewController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
//
// ImportExportViewController.m
// AppSalesMobile
//
// Created by Ole Zorn on 17.03.10.
// Copyright 2010 omz:software. All rights reserved.
//
#import "ImportExportViewController.h"
#import "HTTPServer.h"
#import "MyHTTPConnection.h"
#import "localhostAddresses.h"
#import "ZipArchive.h"
#import "SFHFKeychainUtils.h"
#import "Day.h"
#import "ReportManager.h"
#import "ProgressHUD.h"
@implementation ImportExportViewController
@synthesize info;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
self.hidesBottomBarWhenPushed = YES;
self.info = NSLocalizedString(@"(Please wait)",nil);
NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) objectAtIndex:0];
NSString *uploadPath = [docPath stringByAppendingPathComponent:@"Uploads"];
[[NSFileManager defaultManager] removeItemAtPath:uploadPath error:NULL];
httpServer = [HTTPServer new];
httpServer.uploadPath = uploadPath;
[httpServer setType:@"_http._tcp."];
[httpServer setConnectionClass:[MyHTTPConnection class]];
[httpServer setDocumentRoot:[NSURL fileURLWithPath:[[ReportManager sharedManager] originalReportsPath]]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(displayInfoUpdate:) name:@"LocalhostAdressesResolved" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(fileUploaded:) name:NewFileUploadedNotification object:nil];
}
return self;
}
- (void)loadView
{
self.title = NSLocalizedString(@"Import / Export",nil);
UIWebView *webView = [[[UIWebView alloc] initWithFrame:CGRectZero] autorelease];
self.view = webView;
[self showInfo];
}
- (void)showInfo
{
NSString *page = [[[NSString alloc] initWithContentsOfFile:
[[NSBundle mainBundle] pathForResource:@"ImportHelp" ofType:@"html"]
encoding:NSUTF8StringEncoding
error:NULL] autorelease];
page = [page stringByReplacingOccurrencesOfString:@"[[[ADDRESS]]]" withString:self.info];
[(UIWebView *)self.view loadHTMLString:page baseURL:nil];
}
- (void)fileUploaded:(NSNotification *)notification
{
NSString *filename = [notification object];
NSString *fullPath = [httpServer.uploadPath stringByAppendingPathComponent:filename];
if ([[[filename pathExtension] lowercaseString] isEqual:@"zip"]) {
//NSLog(@"Zip file uploaded: %@", fullPath);
[[ProgressHUD sharedHUD] setText:NSLocalizedString(@"Importing...",nil)];
[[ProgressHUD sharedHUD] show];
[self performSelector:@selector(unzipAndImportFile:) withObject:fullPath afterDelay:0.0];
} else {
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Error",nil)
message:NSLocalizedString(@"You uploaded a file that does not have a .zip extension.", nil) delegate:nil cancelButtonTitle:NSLocalizedString(@"OK",nil)
otherButtonTitles:nil] autorelease];
[alert show];
}
}
- (void)unzipAndImportFile:(NSString *)fullPath
{
NSString *unzipPath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"Unzip"];
//Unzip the archive:
ZipArchive *archive = [[[ZipArchive alloc] init] autorelease];
[archive UnzipOpenFile:fullPath];
[archive UnzipFileTo:unzipPath overWrite:YES];
//Import the report files:
int importedWeeks = 0;
int importedDays = 0;
NSFileManager *fm = [NSFileManager defaultManager];
NSArray *files = [fm contentsOfDirectoryAtPath:unzipPath error:NULL];
for (NSString *file in files) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
if ([[file pathExtension] isEqual:@"txt"]) {
NSString *reportPath = [unzipPath stringByAppendingPathComponent:file];
NSString *fileContents = [[[NSString alloc] initWithContentsOfFile:reportPath] autorelease];
Day *report = [[[Day alloc] initWithCSV:fileContents] autorelease];
if (!report || !report.date) {
NSLog(@"Invalid report file: %@", file);
} else {
if (report.isWeek) {
importedWeeks++;
if (importedWeeks % 10 == 0)
NSLog(@"Imported week %i", importedWeeks);
} else {
importedDays++;
if (importedDays % 10 == 0)
NSLog(@"Imported day %i", importedDays);
}
[[ReportManager sharedManager] importReport:report];
[fm copyItemAtPath:reportPath toPath:[[[ReportManager sharedManager] originalReportsPath] stringByAppendingPathComponent:file] error:NULL];
}
}else if([[file pathExtension] isEqual:@"dat"]){
NSString *filePath = [unzipPath stringByAppendingPathComponent:file];
Day *report = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
if (!report || !report.date) {
NSLog(@"Invalid report file: %@", file);
} else {
if (report.isWeek) {
importedWeeks++;
if (importedWeeks % 10 == 0)
NSLog(@"Imported week %i", importedWeeks);
} else {
importedDays++;
if (importedDays % 10 == 0)
NSLog(@"Imported day %i", importedDays);
}
[report generateSummary];
[[ReportManager sharedManager] importReport:report];
}
}
[pool release];
}
[[ReportManager sharedManager] saveData];
[[NSNotificationCenter defaultCenter] postNotificationName:ReportManagerDownloadedDailyReportsNotification object:self];
[[NSNotificationCenter defaultCenter] postNotificationName:ReportManagerDownloadedWeeklyReportsNotification object:self];
//clean up:
[fm removeItemAtPath:unzipPath error:NULL];
[[ProgressHUD sharedHUD] hide];
if (importedDays == 0 && importedWeeks == 0) {
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Error" message:NSLocalizedString(@"No valid report files were found in the zip archive you uploaded.",nil) delegate:nil cancelButtonTitle:NSLocalizedString(@"OK",nil) otherButtonTitles:nil] autorelease];
[alert show];
} else {
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"" message:[NSString stringWithFormat:NSLocalizedString(@"Successfully imported %i daily and %i weekly reports.",nil), importedDays, importedWeeks] delegate:nil cancelButtonTitle:NSLocalizedString(@"OK",nil) otherButtonTitles:nil] autorelease];
[alert show];
}
}
- (void)displayInfoUpdate:(NSNotification *) notification
{
NSDictionary *addresses = [notification object];
NSString *localIP = [addresses objectForKey:@"en0"];
if (!localIP) {
localIP = [addresses objectForKey:@"en1"];
}
UInt16 port = [httpServer port];
if (!localIP) {
self.info = NSLocalizedString(@"(No Wifi connection)",nil);
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Error",nil) message:NSLocalizedString(@"No Wifi connection.",nil) delegate:nil cancelButtonTitle:NSLocalizedString(@"OK",nil) otherButtonTitles:nil] autorelease];
[alert show];
} else {
self.info = [NSString stringWithFormat:@"http://%@:%d\n", localIP, port];
}
[self performSelectorOnMainThread:@selector(showInfo) withObject:nil waitUntilDone:YES];
}
- (void)viewDidAppear:(BOOL)animated
{
NSError *error = nil;
if(![httpServer start:&error]) {
NSLog(@"Error starting HTTP Server: %@", error);
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Error",nil) message:[NSString stringWithFormat:NSLocalizedString(@"The server could not be started: %@", nil), [error description]] delegate:nil cancelButtonTitle:NSLocalizedString(@"OK",nil) otherButtonTitles:nil] autorelease];
[alert show];
} else {
NSLog(@"HTTP Server started");
[localhostAddresses performSelectorInBackground:@selector(list) withObject:nil];
}
}
- (void)viewDidDisappear:(BOOL)animated
{
[httpServer stop];
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[httpServer release];
[info release];
[super dealloc];
}
@end