summaryrefslogtreecommitdiff
path: root/ios/MobileLibreOffice/MobileLibreOffice/file_manager/MLOFileCacheManager.m
blob: 51dd6924a131df188dda915597e0921858b06fe6 (plain)
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
// -*- Mode: ObjC; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
//
// This file is part of the LibreOffice project.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#import "MLOFileCacheManager.h"
#import "MLOFileManagerViewController_Impl.h"
#import "MLOCachedFile.h"
#import "MLOAppViewController.h"
#import "MLOAppDelegate.h"
#import "MLOManager.h"
#import "NSObject+MLOUtils.h"
#import "NSObject+MLOFileUtils.h"

#define CACHED_DATA_FILE_NAME @"mlo_cache_data"

static const NSTimeInterval DELAY_BEFORE_OPENEING_FIRST_DOCUMENT = 2.0f;

@interface MLOFileCacheManager ()
@property NSMutableArray * files;
@property NSString *dbFilePath;
@end

static NSString * OpenedFilesCountKey = @"openedFiles";
static NSString * CachedFilesKey = @"cachedFiles";

@implementation MLOFileCacheManager

-(id)initWithFileManager:(MLOFileManagerViewController *) fileManager{
    self = [self init];
    if(self){
        self.fileManager =fileManager;
        self.dbFilePath =  [self cachedFilePath:CACHED_DATA_FILE_NAME];
        [self load];
    }
    return self;
}

-(void)loadFile{
    
    NSDictionary * dictionary = [NSDictionary dictionaryWithContentsOfFile:self.dbFilePath];
    
    [MLOCachedFile setCachedFilesCount:[((NSNumber *)dictionary[OpenedFilesCountKey]) unsignedIntValue]];
    
    NSArray * array = dictionary[CachedFilesKey];
    
    for (id loadable in array) {
        [self.files addObject:[[MLOCachedFile alloc] initByLoading:loadable]];
    }
}

-(void)load{
    
    self.files = [NSMutableArray new];
    
    if([[NSFileManager defaultManager] fileExistsAtPath:self.dbFilePath]){
        
        [self loadFile];
        
    }else{
        self.files = [NSMutableArray new];
        
        NSString * exampleFileNameWithExtension = @"test1.odt";
      
        [self addFileAndGetNewIndexForPath:
            [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:exampleFileNameWithExtension]];
        
    }
    
    [self.fileManager reloadData];
    
}
-(void)save{
    NSMutableArray * array = [NSMutableArray new];
    
    for (MLOCachedFile * file in self.files) {
        
        [array addObject:[file toSavable]];
        
    }
    
    [@{OpenedFilesCountKey:[NSNumber numberWithUnsignedInt:[MLOCachedFile cachedFilesCount]],
            CachedFilesKey:array}
     
                writeToFile:self.dbFilePath
                 atomically:YES];
    
}

-(NSUInteger)addFileAndGetNewIndexForPath:(NSString *) newFilePath{
    
    NSUInteger nextFileIndex = self.files.count;
    
    MLOCachedFile * file = [[MLOCachedFile alloc] initWithOriginFilePath:newFilePath];
    
    if([file exists]){
    
        [self.files addObject:file];
        
        [self save];
        
        [self.fileManager reloadData];
    }else{
        nextFileIndex = self.files.count;
    }
    return nextFileIndex;
}

-(NSUInteger)count{
    return self.files.count;
}

-(MLOCachedFile *)getFileAtIndex:(NSUInteger) index{
    return self.files[index];
}

-(void)deleteFileAtIndex:(NSUInteger)index{
    
    MLOCachedFile * toDelete = [self getFileAtIndex:index];
    
    if([toDelete deleteFile]){
        [self.files removeObject:toDelete];
        [self save];
    }
}

-(void)deleteIndexPath:(NSIndexPath*) indexPath{
    [self deleteFileAtIndex:indexPath.row];
}

-(UITableViewCell*)cellForTableView:(UITableView *)tableView atIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell * cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    
    MLOCachedFile * file = [self getFileAtIndex:indexPath.row];
    
    cell.textLabel.text =file.cachedFileNameWithExtension;
    cell.detailTextLabel.text = [NSDateFormatter localizedStringFromDate:[file lastModified]
                                                                  dateStyle:NSDateFormatterShortStyle
                                                                  timeStyle:NSDateFormatterFullStyle];
    cell.accessoryType =UITableViewCellAccessoryDetailDisclosureButton;
    
    return cell;
}

-(void)openFilePath:(NSString *) filePath{
    [self openFileAtIndex:[self addFileAndGetNewIndexForPath:filePath]];
}
-(void)openFileAtIndex:(NSUInteger) index{
    if(index != self.files.count){
        MLOCachedFile * file = [self getFileAtIndex:index];
        
        [self performBlock:^{
        
            [[MLOManager getInstance] openInLibreOfficeFilePath:[file cachedFilePath]
                                          fileNameWithExtension:[file cachedFileNameWithExtension]
                                                      superView:self.fileManager.appViewController.view
                                                         window:self.fileManager.appViewController.appDelegate.window
                                                        invoker:self.fileManager.appViewController.appDelegate];
            
        } afterDelay:DELAY_BEFORE_OPENEING_FIRST_DOCUMENT];
        
    }
}

-(void)sendFileAtIndexPath:(NSIndexPath *) indexPath{
    
    MLOCachedFile * file = [self getFileAtIndex:indexPath.row];
    
    MFMailComposeViewController *mailer = [MFMailComposeViewController new];
    
    mailer.mailComposeDelegate = self;
    [mailer setMessageBody:@"Best" isHTML:NO];
    [mailer setSubject:file.cachedFileNameWithExtension];
    
    NSData *myData =[[NSData alloc] initWithContentsOfFile:file.cachedFilePath];
    [mailer addAttachmentData:myData mimeType:@"iapplication/msword" fileName:file.cachedFileNameWithExtension];
    
    [self.fileManager presentViewController:mailer animated:YES completion:nil];
    
}
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
    switch (result)
    {
        case MFMailComposeResultCancelled:
            NSLog(@"Sending file: canceled");
            break;
        case MFMailComposeResultSaved:
            NSLog(@"Sending file: saved");
            break;
        case MFMailComposeResultSent:
            NSLog(@"Sending file: sent");
            break;
        case MFMailComposeResultFailed:
            NSLog(@"Sending file: failed");
            break;
        default:
            NSLog(@"Sending file: not sent");
            break;
    }
    [self.fileManager dismissViewControllerAnimated:YES completion:nil];
}


@end