61bd04fefb
Add two settings: One setting "Template list URL" is a string that should either be empty (the typical case for a random user of the app), or contain a https: URL. If this setting is empty, only the templates bundled in the app are provided. If the "Template list URL" is non-empty, it should be a https: URL pointing to a text file (or dynamically generated text resource). That file is downloaded and read when the app starts. Each line in the file should either be a comment (starting with a hash '#'), or a https: URL pointing to a template document, that is of type .ott, .ots, or .otp. That document is downloaded if it hasn't been downloaded already, or if its time stamp is newer than that of the already downloaded copy. Also a thumbnail image for the template, formed by appending ".png" to its URL, is downloaded, if available. Any previously downloaded templates that aren't mentioned in the list file are removed. The intent is that in some managed mass deployment environment, the mobile device management software would set up this setting, so that the end-user devices would see the same templates. Obviously, this URL does not have to point to a static file on a web server, but could point to some dynamically generated resource on a web server, that enumerates the templates available on the server and returns their URLs as a text document. Another setting is "Empty tile cache next time". This is a toggle. If toggled on, the next time a document is opened in the app, the tile cache is emptied (and the toggle is reset off). This is mostly for potential problem solving, and might be removd later. Various refactoring to support the new functionality. Change-Id: Ie2ebf032acb9e43bb1c6f7ae4d0c449ae66eaa05
154 lines
6.4 KiB
Text
154 lines
6.4 KiB
Text
// -*- 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 "AppDelegate.h"
|
|
#import "L10n.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 {
|
|
|
|
static NSString *downloadedTemplates = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingString:@"/downloadedTemplates/"];
|
|
|
|
// Scan for available templates. First downloaded ones.
|
|
NSDirectoryEnumerator<NSString *> *e = [[NSFileManager defaultManager] enumeratorAtPath:downloadedTemplates];
|
|
|
|
templates[0] = [@[] mutableCopy];
|
|
templates[1] = [@[] mutableCopy];
|
|
templates[2] = [@[] mutableCopy];
|
|
|
|
NSString *subPath;
|
|
while ((subPath = [e nextObject]) != nil) {
|
|
NSString *path = [downloadedTemplates stringByAppendingString:subPath];
|
|
if ([[path pathExtension] isEqualToString:@"ott"]) {
|
|
[templates[0] addObject:[NSURL fileURLWithPath:path]];
|
|
} else if ([[path pathExtension] isEqualToString:@"ots"]) {
|
|
[templates[1] addObject:[NSURL fileURLWithPath:path]];
|
|
} else if ([[path pathExtension] isEqualToString:@"otp"]) {
|
|
[templates[2] addObject:[NSURL fileURLWithPath:path]];
|
|
}
|
|
}
|
|
|
|
if ([templates[0] count] == 0)
|
|
templates[0] = [[[NSBundle mainBundle] URLsForResourcesWithExtension:@".ott" subdirectory:@"Templates"] mutableCopy];
|
|
if ([templates[1] count] == 0)
|
|
templates[1] = [[[NSBundle mainBundle] URLsForResourcesWithExtension:@".ots" subdirectory:@"Templates"] mutableCopy];
|
|
if ([templates[2] count] == 0)
|
|
templates[2] = [[[NSBundle mainBundle] URLsForResourcesWithExtension:@".otp" subdirectory:@"Templates"] mutableCopy];
|
|
}
|
|
|
|
- (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 = _(STR_DESCRIPTION_FACTORY_WRITER, "svt");
|
|
else if (index == 1)
|
|
translatedHeader = _(STR_DESCRIPTION_FACTORY_CALC, "svt");
|
|
else if (index == 2)
|
|
translatedHeader = _(STR_DESCRIPTION_FACTORY_IMPRESS, "svt");
|
|
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.
|
|
LibreOfficeKitDocument *doc = lo_kit->pClass->documentLoad(lo_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:
|