summaryrefslogtreecommitdiff
path: root/ios/shared/ios_sharedlo/objective_c/MLOManager.m
blob: 466f9f34f0d5307803dfcd4abc808c915522cd60 (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
// -*- 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 "MLOManager.h"
#import "MLOMainViewController.h"
#import "MLOInvoker.h"
#include "touch/touch.h"

#import "mlo.h"
#import "mlo_uno.h"

#define APP_STUB_DEF (UIApplication *) X { (void) X;}

static MLOManager * instance = nil;

static const NSTimeInterval FADE_IN_DURATION = 0.3;

@interface MLOManager ()

@property NSObject<MLOInvoker> * invoker;
@property NSString * openedFileNameWithExtension;
@property NSString * openedFilePath;

@end

@implementation MLOManager

-(id) init{
    self = [super init];
    
    if (self) {
        self.mainViewController = nil;
        self.invoker = nil;
        [self resetOpenedFile];
    }
    return self;
}

-(BOOL)isInit{
    return self.mainViewController != nil;
}

- (void)applicationDidEnterBackground: APP_STUB_DEF
- (void)applicationWillEnterForeground: APP_STUB_DEF
- (void)applicationDidBecomeActive: APP_STUB_DEF
- (void)applicationWillTerminate:  APP_STUB_DEF


- (void)applicationWillResignActive:(UIApplication *) application{
    if (_mainViewController) {
        [_mainViewController hideLibreOffice];
    }
}

-(BOOL)application:(UIApplication *) application didFinishLaunchingWithOptions:(NSDictionary *) launchOptions{

    return YES;
}

- (void)application:(UIApplication *)application didChangeStatusBarFrame:(CGRect)oldStatusBarFrame
{
    IGNORE_ARG(application);
    
    IGNORE_ARG(oldStatusBarFrame);
    
    [self.mainViewController rotate];
}

-(void)start{
    
    NSLog(@"L O : START LIBREOFFICE");
    
    if (![self isInit]) {
        
        NSLog(@"L O : BEGINNING INITIALIZATION");
        
        [self initLo];
        
        NSLog(@"L O : INITIALIZATION COMPLETED!!!");
    }else{
        NSLog(@"L O : SKIPPED. ALREADY INITIALIZED.");
    }
}

+(MLOManager *) getInstance{
    if(instance ==nil){
        instance = [self new];
    }
    return instance;
}

-(void) initLo{
    
    self.mainViewController = [MLOMainViewController new];
    
    [[[NSThread alloc] initWithTarget:self selector:@selector(threadMainMethod:) object:nil] start];
    
}

-(void)addLibreOfficeAsSubview:(UIView * ) superview{
    
    self.mainViewController.view.alpha = 0;
    
    [superview addSubview: self.mainViewController.view];
    
    [UIView animateWithDuration:FADE_IN_DURATION animations:^(){
        self.mainViewController.view.alpha = 1;
    }];
}

-(void)showLibreOfficeAfterAddingToSuperView:(UIWindow *) window{
    window.backgroundColor = [UIColor whiteColor];
    
    [_mainViewController showLibreOffice:window];
}

- (void)threadMainMethod:(id)argument
{
    IGNORE_ARG(argument);
    
    @autoreleasepool {
        NSLog(@"CALLING mlo_initialize");
        
        mlo_initialize();
        
        NSLog(@"touch_lo_runMain RETURNED\r\n\r\nCALLING lo_runMain");
        
        touch_lo_runMain();
        
        NSLog(@"lo_runMain RETURNED");
    }
}

-(void) hideLibreOffice{
    
    [self.invoker willHideLibreOffice];
    
    mlo_close();
    
    [self resetOpenedFile];
    
    [self.mainViewController.view removeFromSuperview];
    
    [self.invoker didHideLibreOffice];
    
}

-(CGRect) bounds{
    return [self.invoker bounds];
}

-(void)resetOpenedFile{
    self.openedFileNameWithExtension = nil;
    self.openedFilePath = nil;
}

-(void)openInLibreOfficeFilePath:(NSString *) filePath fileNameWithExtension:(NSString *) fileNameWithExtension superView:(UIView *) superview window:(UIWindow *) window invoker:
(NSObject<MLOInvoker> *) invoker{
    self.invoker = invoker;
    self.openedFileNameWithExtension = fileNameWithExtension;
    self.openedFilePath = filePath;
    if(mlo_open_file(filePath)){
        [invoker willShowLibreOffice];
        [self addLibreOfficeAsSubview:superview];
        [self showLibreOfficeAfterAddingToSuperView:window];
        [invoker didShowLibreOffice];
    }else{
        [self hideLibreOffice];
        
    }
}

-(void)openInLibreOfficeFilePath:(NSString *) filePath superView:(UIView *) superview window:(UIWindow *) window invoker:(NSObject<MLOInvoker> *) invoker{
    [self openInLibreOfficeFilePath:filePath
              fileNameWithExtension:[filePath lastPathComponent]
                          superView:superview
                             window:window
                            invoker:invoker];
}


-(NSString *)extension{
    
    NSString * extension= [self.openedFilePath pathExtension];
    NSLog(@"File extension is %@",extension);
    return extension;
}
-(NSString *)filenameWithExtension{
    NSLog(@"Filename with extension is %@",self.openedFileNameWithExtension);
    return self.openedFileNameWithExtension;
}

@end