Skip to content

Commit

Permalink
Import an .ics directly through Files app
Browse files Browse the repository at this point in the history
  • Loading branch information
tcitworld committed Sep 27, 2016
1 parent dcef3cb commit f6d2197
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 2 deletions.
11 changes: 11 additions & 0 deletions appinfo/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,16 @@
*/
namespace OCA\Calendar\AppInfo;

use OCP\User;
use OCP\Util;

$app = new Application();
$app->registerNavigation();

// only load text editor if the user is logged in
if (User::isLoggedIn()) {
$eventDispatcher = \OC::$server->getEventDispatcher();
$eventDispatcher->addListener('OCA\Files::loadAdditionalScripts', function() {
Util::addScript('calendar', 'public/registerUtility');
});
}
8 changes: 8 additions & 0 deletions appinfo/application.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

use OCP\AppFramework\App;
use OCP\AppFramework\IAppContainer;
use OCP\Files\Folder;

class Application extends App {

Expand Down Expand Up @@ -61,6 +62,13 @@ public function __construct($params=[]) {

return new Controller\ProxyController($c->getAppName(), $request, $client);
});
$container->registerService('ImportController', function(IAppContainer $c) {
$request = $c->query('Request');
/** @var Folder $userFolder */
$rootFolder = $c->query('ServerContainer')->getRootFolder();

return new Controller\ImportController($c->getAppName(), $request, $rootFolder);
});
}

/**
Expand Down
4 changes: 4 additions & 0 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@
['name' => 'contact#searchAttendee', 'url' => '/v1/autocompletion/attendee', 'verb' => 'GET'],
['name' => 'contact#searchLocation', 'url' => '/v1/autocompletion/location', 'verb' => 'GET'],

// WebCal
['name' => 'proxy#proxy', 'url' => '/v1/proxy', 'verb' => 'GET'],

// Import from Files
['name' => 'import#import', 'url' => '/v1/import', 'verb' => 'GET'],
]
];
55 changes: 55 additions & 0 deletions controller/importcontroller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/**
* Created by PhpStorm.
* User: tcit
* Date: 27/09/16
* Time: 12:23
*/

namespace OCA\calendar\controller;


use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\JSONResponse;
use OCP\Files\File;
use OCP\Files\Folder;
use OCP\Files\NotFoundException;
use OCP\IRequest;

class ImportController extends Controller {

private $folder;

/**
* ImportController constructor.
*
* @param string $appName
* @param IRequest $request
* @param Folder $folder
*/
public function __construct($appName, IRequest $request, Folder $folder) {
parent::__construct($appName, $request);
$this->folder = $folder;
}

/**
* @param integer $fileid
* @return JSONResponse
* @throws \Exception
*/
public function import($fileid) {
try {
$file = $this->folder->getById($fileid);
if($file[0] instanceof File) {
return new JSONResponse([
'body' => $file[0]->getContent(),
'name' => $file[0]->getName(),
]);
} else {
throw new \Exception('Can not read from folder');
}
} catch(NotFoundException $e) {
throw new \Exception('File does not exist');
}
}
}
36 changes: 34 additions & 2 deletions js/app/controllers/calcontroller.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
* Description: The fullcalendar controller.
*/

app.controller('CalController', ['$scope', 'Calendar', 'CalendarService', 'VEventService', 'SettingsService', 'TimezoneService', 'VEvent', 'is', 'fc', 'EventsEditorDialogService', 'PopoverPositioningUtility',
function ($scope, Calendar, CalendarService, VEventService, SettingsService, TimezoneService, VEvent, is, fc, EventsEditorDialogService, PopoverPositioningUtility) {
app.controller('CalController', ['$scope', 'Calendar', 'CalendarService', 'VEventService', 'SettingsService', 'TimezoneService', 'VEvent', 'is', 'fc', 'EventsEditorDialogService', 'PopoverPositioningUtility', '$http', '$rootScope', '$uibModal',
function ($scope, Calendar, CalendarService, VEventService, SettingsService, TimezoneService, VEvent, is, fc, EventsEditorDialogService, PopoverPositioningUtility, $http, $rootScope, $uibModal) {
'use strict';

is.loading = true;
Expand Down Expand Up @@ -133,6 +133,38 @@ app.controller('CalController', ['$scope', 'Calendar', 'CalendarService', 'VEven
$scope.$apply();
});

$scope.importFileById = function(id) {
return $http.get($rootScope.baseUrl + 'import', {
params: {
fileid: id
}
}).then(function(response) {
const fileName = response.data.name;
const fileBody = response.data.body;
let file = new File([fileBody], fileName, {type: 'text/calendar'});

$uibModal.open({
templateUrl: 'import.html',
controller: 'ImportController',
windowClass: 'import',
backdropClass: 'import-backdrop',
keyboard: false,
appendTo: angular.element('#importpopover-container'),
resolve: {
files: function () {
return [file];
}
},
scope: $scope
});
});
};

var hashParts = window.location.hash.substr(1).split('/');
if (!hashParts[0] && hashParts[1] === 'file' && hashParts[2]) {
$scope.importFileById(hashParts[2]);
}


/**
* Calendar UI Configuration.
Expand Down

0 comments on commit f6d2197

Please sign in to comment.