root / Classes / EditMetadataViewController.m @ e06c24cf
History | View | Annotate | Download (15 kB)
1 |
// |
---|---|
2 |
// EditMetadataViewController.m |
3 |
// pithos-ios |
4 |
// |
5 |
// Copyright 2011 GRNET S.A. All rights reserved. |
6 |
// |
7 |
// Redistribution and use in source and binary forms, with or |
8 |
// without modification, are permitted provided that the following |
9 |
// conditions are met: |
10 |
// |
11 |
// 1. Redistributions of source code must retain the above |
12 |
// copyright notice, this list of conditions and the following |
13 |
// disclaimer. |
14 |
// |
15 |
// 2. Redistributions in binary form must reproduce the above |
16 |
// copyright notice, this list of conditions and the following |
17 |
// disclaimer in the documentation and/or other materials |
18 |
// provided with the distribution. |
19 |
// |
20 |
// THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS |
21 |
// OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
22 |
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
23 |
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR |
24 |
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
25 |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
26 |
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
27 |
// USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
28 |
// AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
29 |
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
30 |
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
31 |
// POSSIBILITY OF SUCH DAMAGE. |
32 |
// |
33 |
// The views and conclusions contained in the software and |
34 |
// documentation are those of the authors and should not be |
35 |
// interpreted as representing official policies, either expressed |
36 |
// or implied, of GRNET S.A. |
37 |
|
38 |
#import "EditMetadataViewController.h" |
39 |
#import "AccountManager.h" |
40 |
#import "UIViewController+Conveniences.h" |
41 |
#import "FolderViewController.h" |
42 |
#import "Folder.h" |
43 |
#import "PithosUtilities.h" |
44 |
#import "APICallback.h" |
45 |
|
46 |
|
47 |
|
48 |
@implementation EditMetadataViewController |
49 |
|
50 |
|
51 |
@synthesize container, account, object; |
52 |
|
53 |
@synthesize metadataKey; |
54 |
@synthesize metadataValue; |
55 |
@synthesize userInputMetaKey, userInputMetaValue; |
56 |
@synthesize removeMetadataEnabled, objectIsFolder, folderViewController, objectIsContainer; |
57 |
|
58 |
#define kMetadata 0 |
59 |
#define kSaveMedata 1 |
60 |
#define kDeleteMetadata 2 |
61 |
|
62 |
|
63 |
- (void)dealloc |
64 |
{ |
65 |
[account release]; |
66 |
[container release]; |
67 |
[object release]; |
68 |
[metadataKey release]; |
69 |
[metadataValue release]; |
70 |
[userInputMetaKey release]; |
71 |
[userInputMetaValue release]; |
72 |
[super dealloc]; |
73 |
} |
74 |
|
75 |
- (void)didReceiveMemoryWarning |
76 |
{ |
77 |
// Releases the view if it doesn't have a superview. |
78 |
[super didReceiveMemoryWarning]; |
79 |
|
80 |
// Release any cached data, images, etc that aren't in use. |
81 |
} |
82 |
|
83 |
#pragma mark - View lifecycle |
84 |
|
85 |
- (void)viewDidLoad |
86 |
{ |
87 |
[super viewDidLoad]; |
88 |
} |
89 |
|
90 |
- (void)viewDidUnload |
91 |
{ |
92 |
[super viewDidUnload]; |
93 |
} |
94 |
|
95 |
- (void)viewWillAppear:(BOOL)animated |
96 |
{ |
97 |
[super viewWillAppear:animated]; |
98 |
} |
99 |
|
100 |
- (void)viewDidAppear:(BOOL)animated |
101 |
{ |
102 |
[super viewDidAppear:animated]; |
103 |
} |
104 |
|
105 |
- (void)viewWillDisappear:(BOOL)animated |
106 |
{ |
107 |
[super viewWillDisappear:animated]; |
108 |
} |
109 |
|
110 |
- (void)viewDidDisappear:(BOOL)animated |
111 |
{ |
112 |
[super viewDidDisappear:animated]; |
113 |
} |
114 |
|
115 |
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation { |
116 |
return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) || (toInterfaceOrientation == UIInterfaceOrientationPortrait); |
117 |
} |
118 |
|
119 |
#pragma mark - Table view data source |
120 |
|
121 |
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView |
122 |
{ |
123 |
return (removeMetadataEnabled)? 3:2; |
124 |
} |
125 |
|
126 |
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section |
127 |
{ |
128 |
if (section == kMetadata) |
129 |
return 2; |
130 |
else |
131 |
return 1; |
132 |
} |
133 |
|
134 |
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath |
135 |
{ |
136 |
static NSString *CellIdentifier = @"Cell"; |
137 |
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
138 |
if (cell == nil) { |
139 |
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; |
140 |
} |
141 |
|
142 |
if (indexPath.section == kMetadata) { |
143 |
UITextField *textField = nil; |
144 |
for (id subView in cell.contentView.subviews) { |
145 |
if ([subView isKindOfClass:[UITextField class]]) { |
146 |
textField = (UITextField *)subView; |
147 |
} |
148 |
} |
149 |
|
150 |
if (textField == nil) { |
151 |
CGRect bounds = [cell.contentView bounds]; |
152 |
CGRect rect = CGRectInset(bounds, 10.0, 10.0); |
153 |
textField = [[UITextField alloc] initWithFrame:rect]; |
154 |
[textField setFrame:rect]; |
155 |
} |
156 |
[textField setClearButtonMode:UITextFieldViewModeWhileEditing]; |
157 |
[textField setBackgroundColor:[UIColor clearColor]]; |
158 |
[textField setOpaque:YES]; |
159 |
[textField setAutocorrectionType:UITextAutocorrectionTypeNo]; |
160 |
[textField setAutocapitalizationType:UITextAutocapitalizationTypeNone]; |
161 |
[textField setDelegate:self]; |
162 |
textField.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; |
163 |
|
164 |
if (indexPath.row == 0) { |
165 |
[textField setReturnKeyType:UIReturnKeyNext]; |
166 |
textField.placeholder = @"Key"; |
167 |
textField.text = self.metadataKey; |
168 |
textField.tag = 0; |
169 |
} |
170 |
else if (indexPath.row == 1) { |
171 |
textField.placeholder = @"Value"; |
172 |
textField.text = self.metadataValue; |
173 |
textField.tag = 1; |
174 |
} |
175 |
|
176 |
cell.selectionStyle = UITableViewCellSelectionStyleNone; |
177 |
[cell.contentView addSubview:textField]; |
178 |
} |
179 |
else if (indexPath.section == kSaveMedata) |
180 |
cell.textLabel.text = @"Save"; |
181 |
else if (indexPath.section == kDeleteMetadata) |
182 |
cell.textLabel.text = @"Remove"; |
183 |
|
184 |
return cell; |
185 |
} |
186 |
|
187 |
|
188 |
#pragma mark - Table view delegate |
189 |
|
190 |
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath |
191 |
{ |
192 |
|
193 |
|
194 |
if (indexPath.section != kMetadata) |
195 |
[self.view endEditing:YES]; |
196 |
|
197 |
NSString *activityMessage; |
198 |
NSIndexPath *keyCellIndexPath; |
199 |
NSIndexPath *valueCellIndexPath; |
200 |
UITableViewCell *cell; |
201 |
|
202 |
switch (indexPath.section) { |
203 |
case kSaveMedata: |
204 |
keyCellIndexPath = [NSIndexPath indexPathForRow:0 inSection:kMetadata]; |
205 |
cell = [self.tableView cellForRowAtIndexPath:keyCellIndexPath]; |
206 |
UITextField *textField = [[cell.contentView subviews] objectAtIndex:0]; |
207 |
self.userInputMetaKey = textField.text; |
208 |
|
209 |
if ([userInputMetaKey length] == 0 |
210 |
|| [[userInputMetaKey stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] == 0) { |
211 |
[self.tableView deselectRowAtIndexPath:indexPath animated:YES]; |
212 |
[self alert:@"Invalid input" message:@"Metadata key cannot be empty"]; |
213 |
return; |
214 |
} |
215 |
|
216 |
valueCellIndexPath = [NSIndexPath indexPathForRow:1 inSection:kMetadata]; |
217 |
cell = [self.tableView cellForRowAtIndexPath:valueCellIndexPath]; |
218 |
textField = [[cell.contentView subviews] objectAtIndex:0]; |
219 |
self.userInputMetaValue = textField.text; |
220 |
[object.metadata removeObjectForKey:metadataKey]; |
221 |
[object.metadata setObject:userInputMetaValue forKey:userInputMetaKey]; |
222 |
|
223 |
if (objectIsFolder && ![PithosUtilities isContentTypeDirectory:object.contentType]) { |
224 |
if (((UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) && |
225 |
[folderViewController.parentFolderViewController.folder.objects objectForKey:object.name]) || |
226 |
((UI_USER_INTERFACE_IDIOM() != UIUserInterfaceIdiomPad) && |
227 |
[folderViewController.folder.objects objectForKey:object.name])) { |
228 |
NSString *alertMessage = [NSString stringWithFormat:@"In order to apply the changes in '%@', the object at the same path must be replaced. Continue?",object.name]; |
229 |
NSString *alertTitle = @"Apply changes"; |
230 |
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:alertTitle |
231 |
message:alertMessage |
232 |
delegate:self |
233 |
cancelButtonTitle:@"Cancel" |
234 |
otherButtonTitles:@"OK", nil]; |
235 |
[alert show]; |
236 |
[alert release]; |
237 |
return; |
238 |
} else { |
239 |
activityMessage = @"Saving metadata..."; |
240 |
activityIndicatorView = [[ActivityIndicatorView alloc] initWithFrame:[ActivityIndicatorView frameForText:activityMessage] text:activityMessage]; |
241 |
[activityIndicatorView addToView:self.view]; |
242 |
|
243 |
object.name = object.fullPath; |
244 |
object.contentType = @"application/directory"; |
245 |
object.data = [NSData data]; |
246 |
[self createNewFolder]; |
247 |
return; |
248 |
} |
249 |
} |
250 |
|
251 |
activityMessage = @"Saving metadata"; |
252 |
activityIndicatorView = [[ActivityIndicatorView alloc] initWithFrame:[ActivityIndicatorView frameForText:activityMessage] text:activityMessage]; |
253 |
[activityIndicatorView addToView:self.view]; |
254 |
|
255 |
[[self.account.manager writeObjectMetadata:container object:object] |
256 |
success:^(OpenStackRequest *request) { |
257 |
self.metadataKey = userInputMetaKey; |
258 |
self.metadataValue = userInputMetaValue; |
259 |
removeMetadataEnabled = YES; |
260 |
[self.tableView reloadData]; |
261 |
[activityIndicatorView removeFromSuperviewAndRelease]; |
262 |
[self.tableView deselectRowAtIndexPath:indexPath animated:YES]; |
263 |
if (objectIsContainer) { |
264 |
container.metadata = object.metadata; |
265 |
[self.account.containers setObject:container forKey:container.name]; |
266 |
} |
267 |
} |
268 |
failure:^(OpenStackRequest *request) { |
269 |
[object.metadata removeObjectForKey:userInputMetaKey]; |
270 |
[object.metadata setObject:metadataValue forKey:metadataKey]; |
271 |
[activityIndicatorView removeFromSuperviewAndRelease]; |
272 |
[self.tableView deselectRowAtIndexPath:indexPath animated:YES]; |
273 |
[self alert:@"There was a problem saving the metadata." request:request]; |
274 |
}]; |
275 |
|
276 |
break; |
277 |
case kDeleteMetadata: |
278 |
activityMessage = @"Deleting metadata"; |
279 |
activityIndicatorView = [[ActivityIndicatorView alloc] initWithFrame:[ActivityIndicatorView frameForText:activityMessage] text:activityMessage]; |
280 |
[activityIndicatorView addToView:self.view]; |
281 |
|
282 |
[object.metadata removeObjectForKey:metadataKey]; |
283 |
[[self.account.manager writeObjectMetadata:container object:object] |
284 |
success:^(OpenStackRequest *request) { |
285 |
[activityIndicatorView removeFromSuperviewAndRelease]; |
286 |
[self.tableView deselectRowAtIndexPath:indexPath animated:YES]; |
287 |
self.metadataKey = @""; |
288 |
self.metadataValue = @""; |
289 |
removeMetadataEnabled = NO; |
290 |
[self.tableView reloadData]; |
291 |
if (objectIsContainer) { |
292 |
container.metadata = object.metadata; |
293 |
[self.account.containers setObject:container forKey:container.name]; |
294 |
} |
295 |
} |
296 |
failure:^(OpenStackRequest *request) { |
297 |
[object.metadata setObject:metadataValue forKey:metadataKey]; |
298 |
[activityIndicatorView removeFromSuperviewAndRelease]; |
299 |
[self.tableView deselectRowAtIndexPath:indexPath animated:YES]; |
300 |
[self alert:@"There was a problem saving the metadata." request:request]; |
301 |
|
302 |
}]; |
303 |
break; |
304 |
} |
305 |
} |
306 |
|
307 |
#pragma mark - Alertview delegate |
308 |
|
309 |
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex |
310 |
{ |
311 |
if (buttonIndex == 1) { |
312 |
NSString *activityMessage = @"Applying permissions..."; |
313 |
|
314 |
activityIndicatorView = [[ActivityIndicatorView alloc] initWithFrame:[ActivityIndicatorView frameForText:activityMessage] text:activityMessage]; |
315 |
[activityIndicatorView addToView:self.view]; |
316 |
|
317 |
object.name = object.fullPath; |
318 |
object.contentType = @"application/directory"; |
319 |
object.data = [NSData data]; |
320 |
[self createNewFolder]; |
321 |
} else { |
322 |
[self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES]; |
323 |
} |
324 |
} |
325 |
|
326 |
#pragma mark - Textfield delegate |
327 |
|
328 |
|
329 |
- (BOOL)textFieldShouldReturn:(UITextField *)textField |
330 |
{ |
331 |
|
332 |
if ([textField returnKeyType] == UIReturnKeyNext) |
333 |
{ |
334 |
if ([textField.text length] == 0 |
335 |
|| [[textField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] == 0) { |
336 |
[self alert:@"Invalid input" message:@"Metadata key cannot be empty"]; |
337 |
} |
338 |
|
339 |
NSInteger nextTag = [textField tag] + 1; |
340 |
UIView *nextTextField = [self.tableView viewWithTag:nextTag]; |
341 |
[nextTextField becomeFirstResponder]; |
342 |
} |
343 |
else |
344 |
{ |
345 |
[textField resignFirstResponder]; |
346 |
} |
347 |
|
348 |
return YES; |
349 |
} |
350 |
|
351 |
#pragma mark - add observers |
352 |
|
353 |
- (void)createNewFolder { |
354 |
[[self.account.manager writeObject:container object:object downloadProgressDelegate:nil] |
355 |
success:^(OpenStackRequest *request) { |
356 |
Folder *newFolder = [[Folder alloc] init]; |
357 |
newFolder.name = [[object.name componentsSeparatedByString:@"/"] lastObject]; |
358 |
newFolder.parent = folderViewController.folder; |
359 |
newFolder.sharing = folderViewController.folder.sharing; |
360 |
newFolder.metadata = object.metadata; |
361 |
[folderViewController.folder.folders setObject:newFolder forKey:newFolder.name]; |
362 |
[activityIndicatorView removeFromSuperviewAndRelease]; |
363 |
[newFolder release]; |
364 |
self.metadataKey = userInputMetaKey; |
365 |
self.metadataValue = userInputMetaValue; |
366 |
removeMetadataEnabled = YES; |
367 |
[self.tableView reloadData]; |
368 |
[self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES]; |
369 |
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { |
370 |
[self.folderViewController refreshButtonPressed:nil]; |
371 |
} |
372 |
else { |
373 |
self.folderViewController.needsRefreshing = YES; |
374 |
self.folderViewController.refreshWhenAppeared = YES; |
375 |
} |
376 |
} |
377 |
failure:^(OpenStackRequest *request) { |
378 |
[object.metadata removeObjectForKey:userInputMetaKey]; |
379 |
[object.metadata setObject:metadataValue forKey:metadataKey]; |
380 |
[activityIndicatorView removeFromSuperviewAndRelease]; |
381 |
[self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES]; |
382 |
[self.tableView reloadData]; |
383 |
[self alert:@"There was a problem saving the metadata." request:request]; |
384 |
}]; |
385 |
} |
386 |
|
387 |
@end |