libreoffice-online/ios/Mobile/TemplateCollectionViewController.mm

138 lines
5.6 KiB
Text
Raw Normal View History

// -*- Mode: ObjC; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*-
//
// 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 "svtools/strings.hrc"
#import <LibreOfficeKit/LibreOfficeKit.h>
#import <LibreOfficeKit/LibreOfficeKitInit.h>
#import "AppDelegate.h"
#import "TemplateCollectionViewController.h"
#import "TemplateSectionHeaderView.h"
static NSString *mapTemplateExtensionToActual(NSString *templateName) {
NSString *baseName = [templateName stringByDeletingPathExtension];
NSString *extension = [templateName substringFromIndex:baseName.length];
if ([extension isEqualToString:@".ott"])
return [baseName stringByAppendingString:@".odt"];
else if ([extension isEqualToString:@".ots"])
return [baseName stringByAppendingString:@".ods"];
else if ([extension isEqualToString:@".otp"])
return [baseName stringByAppendingString:@".odp"];
else
assert(false);
}
@implementation TemplateCollectionViewController
- (void)viewDidLoad {
kit = lok_init_2(nullptr, nullptr);
// Scan for available templates.
templates[0] = [[NSBundle mainBundle] URLsForResourcesWithExtension:@".ott" subdirectory:@"Templates"];
templates[1] = [[NSBundle mainBundle] URLsForResourcesWithExtension:@".ots" subdirectory:@"Templates"];
templates[2] = [[NSBundle mainBundle] URLsForResourcesWithExtension:@".otp" subdirectory:@"Templates"];
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
// Three sections: Document, Spreadsheet, and Presentation
return 3;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
assert(section >= 0 && section <= 2);
return templates[section].count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
assert(indexPath.length == 2);
assert([indexPath indexAtPosition:0] <= 2);
assert([indexPath indexAtPosition:1] < templates[[indexPath indexAtPosition:0]].count);
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
UIImageView *image = (UIImageView *)[cell viewWithTag:1];
UILabel *title = (UILabel *)[cell viewWithTag:2];
NSString *templateThumbnail = [[templates[[indexPath indexAtPosition:0]][[indexPath indexAtPosition:1]] path] stringByAppendingString:@".png"];
UIImage *thumbnail;
if ([NSFileManager.defaultManager fileExistsAtPath:templateThumbnail])
thumbnail = [UIImage imageWithContentsOfFile:templateThumbnail];
else
thumbnail = [UIImage imageNamed:@"AppIcon"];
image.image = thumbnail;
NSString *fileName = [templates[[indexPath indexAtPosition:0]][[indexPath indexAtPosition:1]] lastPathComponent];
title.text = [fileName stringByDeletingPathExtension];
return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return CGSizeMake(150, 150);
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
assert(kind == UICollectionElementKindSectionHeader);
assert(indexPath.length == 2);
assert([indexPath indexAtPosition:1] == 0);
NSUInteger index = [indexPath indexAtPosition:0];
assert(index <= 2);
TemplateSectionHeaderView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"SectionHeaderView" forIndexPath:indexPath];
char *translatedHeader;
if (index == 0)
translatedHeader = kit->pClass->translateGet(kit, STR_DESCRIPTION_FACTORY_WRITER, "svt", [app_locale UTF8String]);
else if (index == 1)
translatedHeader = kit->pClass->translateGet(kit, STR_DESCRIPTION_FACTORY_CALC, "svt", [app_locale UTF8String]);
else if (index == 2)
translatedHeader = kit->pClass->translateGet(kit, STR_DESCRIPTION_FACTORY_IMPRESS, "svt", [app_locale UTF8String]);
else
abort();
header.title.text = [NSString stringWithUTF8String:translatedHeader];
free(translatedHeader);
return header;
}
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
NSURL *selectedTemplate = templates[[indexPath indexAtPosition:0]][[indexPath indexAtPosition:1]];
NSURL *cacheDirectory = [NSFileManager.defaultManager URLsForDirectory:NSCachesDirectory inDomains:NSUserDomainMask][0];
NSURL *newURL = [cacheDirectory URLByAppendingPathComponent:mapTemplateExtensionToActual(selectedTemplate.lastPathComponent)
isDirectory:NO];
// Load the template into LibreOffice core, save as the corresponding document type (with the
// same basename), and then proceed to edit that.
kit->pClass->registerCallback(kit, [](int, const char *, void*){}, nullptr);
LibreOfficeKitDocument *doc = kit->pClass->documentLoad(kit, [[selectedTemplate absoluteString] UTF8String]);
doc->pClass->saveAs(doc, [[newURL absoluteString] UTF8String], nullptr, nullptr);
doc->pClass->destroy(doc);
self.importHandler(newURL, UIDocumentBrowserImportModeMove);
[self dismissViewControllerAnimated:YES completion:nil];
return YES;
}
@end
// vim:set shiftwidth=4 softtabstop=4 expandtab: