Statistics
| Branch: | Tag: | Revision:

root / Classes / FolderDetailViewController.m @ 54fd5c36

History | View | Annotate | Download (14.6 kB)

1
//
2
//  FolderDetailViewController.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 "FolderDetailViewController.h"
39
#import "EditMetadataViewController.h"
40
#import "EditPermissionsViewController.h"
41
#import "StorageObject.h"
42
#import "APICallback.h"
43
#import "AccountManager.h"
44
#import "UIViewController+Conveniences.h"
45

    
46
#define kOverview 0
47
#define kMetadata 1
48
#define kPermissions 2
49

    
50
#define maxMetadataViewableLength 12
51

    
52

    
53
@implementation FolderDetailViewController
54

    
55
@synthesize account, container, folder, folderViewController;
56

    
57
#pragma mark - 
58
#pragma mark Memory management
59

    
60
- (void)dealloc
61
{
62
    [object release];
63
    [account release];
64
    [container release];
65
    [folderViewController release];
66
    [folder release];
67
    [permissions release];
68
    [super dealloc];
69
}
70

    
71
- (void)didReceiveMemoryWarning
72
{
73
    // Releases the view if it doesn't have a superview.
74
    [super didReceiveMemoryWarning];
75
    
76
    // Release any cached data, images, etc that aren't in use.
77
}
78

    
79
#pragma mark - View lifecycle
80

    
81
- (void)viewDidLoad
82
{
83
    [super viewDidLoad];
84
    permissions = [[NSMutableDictionary alloc] init];
85
    if (folder.sharing.length > 0) {
86
        NSArray *sharingArray = [folder.sharing componentsSeparatedByString:@";"];
87
        for (NSString *typeSpecificPermissions in sharingArray) { 
88
            NSArray *array=[typeSpecificPermissions componentsSeparatedByString:@"="];
89
            NSString *permissionsType = [array objectAtIndex:0];
90
            if ([permissionsType hasPrefix:@" "])
91
                permissionsType = [permissionsType substringFromIndex:1];
92
            
93
            NSArray *users = [[array objectAtIndex:1] componentsSeparatedByString:@","];
94
            for (NSString *user in users) {
95
                [permissions setObject:permissionsType forKey:user];
96
            }
97
        }
98
    }
99
    
100
    folderIsReadOnly = NO;
101
    if (account.sharingAccount) { 
102
        if ([permissions count] > 0) {
103
            folderIsReadOnly = [[permissions objectForKey:[account username]] isEqualToString:@"read"];
104
        }
105
    }
106
    
107
    object = [[StorageObject alloc] init];
108
    object.name = folder.name;
109
    object.metadata = folder.metadata;
110
    object.fullPath = [folder fullPath];
111
    object.sharing = folder.sharing;
112
    object.contentType = folder.contentType;
113
}
114

    
115
- (void)viewDidUnload
116
{
117
    [super viewDidUnload];
118
}
119

    
120
- (void)viewWillAppear:(BOOL)animated
121
{
122
    [super viewWillAppear:animated];
123
    [self.tableView reloadData];
124
}
125

    
126
- (void)viewDidAppear:(BOOL)animated
127
{
128
    [super viewDidAppear:animated];
129
    if (folder.metadata == nil) {
130
        [self reloadMetadataSection];
131
    }
132
}
133

    
134
- (void)viewWillDisappear:(BOOL)animated
135
{
136
    [super viewWillDisappear:animated];
137
    folder.sharing = object.sharing;
138
}
139

    
140
- (void)viewDidDisappear:(BOOL)animated
141
{
142
    [super viewDidDisappear:animated];
143
}
144

    
145
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
146
    return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) || (toInterfaceOrientation == UIInterfaceOrientationPortrait);
147
}
148

    
149
#pragma mark - Table view data source
150

    
151
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
152
{
153
    return 3;
154
}
155

    
156
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
157
{
158
    if (section == kOverview)
159
        return 2;
160
    else if (section == kPermissions) {
161
        if (account.sharingAccount)
162
            return [permissions count];
163
        else
164
            return 1 + [permissions count];
165
    }
166
    else if (section == kMetadata)
167
        if (folderIsReadOnly)
168
            return [folder.metadata count];
169
        else
170
            return 1 + [folder.metadata count];     
171
        return 0;
172
}
173

    
174
- (CGFloat)findLabelHeight:(NSString*)text font:(UIFont *)font {
175
    CGSize textLabelSize;    
176
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
177
        textLabelSize = CGSizeMake(537.0, 9000.0f);
178
    } else {
179
        textLabelSize = CGSizeMake(221.0, 9000.0f);
180
    }
181

    
182
    CGSize stringSize = [text sizeWithFont:font constrainedToSize:textLabelSize lineBreakMode:UILineBreakModeCharacterWrap];
183
    return stringSize.height;
184
}
185

    
186
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
187
    CGFloat result = tableView.rowHeight;
188
    
189
    if (indexPath.section == kOverview) {
190
        if (indexPath.row == 0) {
191
            result = 22 + [self findLabelHeight:folder.name font:[UIFont systemFontOfSize:18.0]];
192
            
193
            return MAX(tableView.rowHeight, result);
194
        } else if (indexPath.row == 1) {
195
            NSString *folderFullPathToShow = folder.fullPath;
196
            if ([folderFullPathToShow hasPrefix:@"/"]) {
197
                folderFullPathToShow = [folderFullPathToShow substringFromIndex:1];
198
            }
199
            result = 22 + [self findLabelHeight:folderFullPathToShow font:[UIFont systemFontOfSize:18.0]];
200
        
201
            return MAX(tableView.rowHeight, result);
202
        }
203
    }
204
    
205
    return tableView.rowHeight;
206
}
207

    
208

    
209
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
210
{
211
    static NSString *CellIdentifier = @"Cell";
212
    
213
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
214
    if (cell == nil) {
215
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
216

    
217
        cell.textLabel.backgroundColor = [UIColor clearColor];
218
        cell.detailTextLabel.backgroundColor = [UIColor clearColor];
219
        cell.detailTextLabel.numberOfLines = 0;
220
        cell.detailTextLabel.lineBreakMode = UILineBreakModeWordWrap;
221
        cell.detailTextLabel.textAlignment = UITextAlignmentRight;
222
    }
223
    
224
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
225
        cell.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.8];
226
    }
227
    
228
    if (indexPath.section == kOverview) {
229
        cell.accessoryType = UITableViewCellAccessoryNone;
230
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
231
        cell.accessoryView = nil;
232
        if (indexPath.row == 0) {
233
            cell.textLabel.text = @"Name";
234
            cell.detailTextLabel.text = folder.name;
235
        } else if (indexPath.row == 1) {
236
            cell.textLabel.text = @"Full Path";
237
            NSString *folderFullPathToShow = folder.fullPath;
238
            if ([folderFullPathToShow hasPrefix:@"/"]) {
239
                folderFullPathToShow = [folderFullPathToShow substringFromIndex:1];
240
            }
241
            cell.detailTextLabel.text = folderFullPathToShow;
242
        }
243
    } else if (indexPath.section == kMetadata) {
244
        if (folderIsReadOnly) {
245
            cell.accessoryType = UITableViewCellAccessoryNone;
246
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
247
            cell.userInteractionEnabled = NO;
248
        }
249
        else {
250
            cell.selectionStyle = UITableViewCellSelectionStyleBlue;
251
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
252
        }
253
        cell.accessoryView = nil;
254
        if (indexPath.row == [folder.metadata count]) {
255
            cell.textLabel.text = @"Add Metadata";
256
            cell.detailTextLabel.text = @"";
257
        } else {
258
            NSString *key = [[folder.metadata allKeys] objectAtIndex:indexPath.row];
259
            NSString *value = [folder.metadata objectForKey:key];
260
            NSString *metadataKeyCellText = key;
261
            NSString *metadataValueCellText = value;
262
            if ([metadataKeyCellText length] > maxMetadataViewableLength) {
263
                metadataKeyCellText = [metadataKeyCellText substringToIndex:(maxMetadataViewableLength - 3)];
264
                metadataKeyCellText = [metadataKeyCellText stringByAppendingString:@"..."];
265
            }
266
            if ([metadataValueCellText length] > maxMetadataViewableLength) {
267
                metadataValueCellText = [metadataValueCellText substringToIndex:(maxMetadataViewableLength - 3)];
268
                metadataValueCellText = [metadataValueCellText stringByAppendingString:@"..."];
269
            }
270

    
271
            cell.textLabel.text = metadataKeyCellText;
272
            cell.detailTextLabel.text = metadataValueCellText;
273
        }
274
    } else if (indexPath.section == kPermissions) {
275
        if (account.sharingAccount) {
276
            cell.accessoryType = UITableViewCellAccessoryNone;
277
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
278
            cell.userInteractionEnabled = NO;
279
        }
280
        else {
281
            cell.selectionStyle = UITableViewCellSelectionStyleBlue;
282
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
283
        }
284
        cell.accessoryView = nil;
285
        
286
        if (indexPath.row == [permissions count]) {
287
            cell.textLabel.text = @"Add Permissions";
288
            cell.detailTextLabel.text = @""; 
289
        }
290
        else {
291
            NSString *user = [[permissions allKeys] objectAtIndex:indexPath.row];
292
            cell.textLabel.text = user;
293
            cell.detailTextLabel.text = [permissions objectForKey:user];
294
        }
295
    }
296

    
297
    return cell;
298
}
299

    
300

    
301
#pragma mark - Table view delegate
302

    
303
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
304
{
305
    if (indexPath.section == kMetadata) {
306
        EditMetadataViewController *vc = [[EditMetadataViewController alloc] initWithNibName:@"EditMetadataViewController" bundle:nil];
307
        NSString *metadataKey;
308
        NSString *metadataValue;
309
        
310
        if (indexPath.row == [self.folder.metadata count]) {
311
            metadataKey = @"";
312
            metadataValue = @"";
313
            vc.removeMetadataEnabled = FALSE;
314
            vc.navigationItem.title = @"Add Metadata";
315
        }
316
        else {
317
            metadataKey = [[self.folder.metadata allKeys] objectAtIndex:indexPath.row];
318
            metadataValue = [self.folder.metadata objectForKey:metadataKey];
319
            vc.removeMetadataEnabled = TRUE;
320
            vc.navigationItem.title = @"Edit Metadata";
321
        }
322
                
323
        vc.metadataKey = metadataKey;
324
        vc.metadataValue = metadataValue;
325
        vc.account = account;
326
        vc.container = container;
327
        vc.object = object;
328
        vc.objectIsFolder = YES;
329
        vc.folderViewController = folderViewController;
330

    
331
        [self.navigationController pushViewController:vc animated:YES];
332
        [vc release];
333
    } else if (indexPath.section == kPermissions) {
334
        EditPermissionsViewController *vc = [[EditPermissionsViewController alloc] initWithNibName:@"EditPermissionsViewController" bundle:nil];
335
        NSString *user;
336
        
337
        if (indexPath.row == [permissions count]) {
338
            user = @"";
339
            vc.removePermissionsEnabled = NO;
340
            vc.navigationItem.title = @"Add Permissions";
341
        }
342
        else {
343
            user = [[permissions allKeys] objectAtIndex:indexPath.row];
344
            NSString *userPermissions = [permissions objectForKey:user];
345
            if ([userPermissions rangeOfString:@"read"].location != NSNotFound)
346
                vc.readPermissionSelected = YES;
347
            else
348
                vc.readPermissionSelected = NO;
349
            
350
            if ([userPermissions rangeOfString:@"write"].location != NSNotFound)
351
                vc.writePermissionSelected = YES;
352
            else
353
                vc.writePermissionSelected = NO;
354
            
355
            vc.removePermissionsEnabled = YES;
356
            vc.navigationItem.title = @"Edit Permissions";
357
        }
358
        
359
        vc.user = user;
360
        vc.permissions = permissions;
361
        vc.account = account;
362
        vc.container = container;
363
        vc.object = object;
364
        vc.objectIsFolder = YES;
365
        vc.folderViewController = folderViewController;
366
        [self.navigationController pushViewController:vc animated:YES];
367
        [vc release];
368
    } 
369
}
370

    
371
#pragma mark - 
372
#pragma mark Helper functions
373

    
374
- (void)reloadMetadataSection {
375
    NSString *activityMessage = @"Loading metadata...";
376
    ActivityIndicatorView *activityIndicatorView = [[ActivityIndicatorView alloc] initWithFrame:[ActivityIndicatorView frameForText:activityMessage] text:activityMessage];
377
    [activityIndicatorView addToView:self.view];         
378
    [[self.account.manager getObjectInfo:container object:object version:nil] 
379
     success:^(OpenStackRequest *request) {
380
         [activityIndicatorView removeFromSuperviewAndRelease];
381
         folder.metadata = [NSMutableDictionary dictionary];
382
         for (NSString *header in request.responseHeaders) {
383
             NSString *metadataKey;
384
             NSString *metadataValue;
385
             if ([header rangeOfString:@"X-Object-Meta-"].location != NSNotFound) {
386
                 metadataKey = [header substringFromIndex:14];
387
                 metadataValue = [request.responseHeaders objectForKey:header];
388
                 [folder.metadata setObject:metadataValue forKey:metadataKey];
389
             }
390
         }   
391
         object.metadata = folder.metadata;
392
         NSIndexSet *metadataSections = [NSIndexSet indexSetWithIndex:kMetadata];
393
         [self.tableView reloadSections:metadataSections withRowAnimation:UITableViewRowAnimationFade];
394
     }
395
     failure:^(OpenStackRequest *request) {
396
         [activityIndicatorView removeFromSuperviewAndRelease];
397
         [self alert:@"There was a problem retrieving the object's metadata." request:request]; 
398
     }];
399
}
400

    
401
@end