Download note as XML and JSON
authorChristian Weiske <cweiske@cweiske.de>
Mon, 27 Oct 2014 22:16:15 +0000 (23:16 +0100)
committerChristian Weiske <cweiske@cweiske.de>
Mon, 27 Oct 2014 22:16:15 +0000 (23:16 +0100)
appinfo/application.php
appinfo/routes.php
controller/guicontroller.php
controller/notescontroller.php [new file with mode: 0644]
grauphel.css
lib/response/xmlresponse.php [new file with mode: 0644]
templates/gui-note.php

index 66ed5571a2315570d891918df3d746daa5df987e..7761eb5a90db76672bd99eb694dcf1c2ea1b9d51 100644 (file)
@@ -56,6 +56,16 @@ class Application extends App
                 );
             }
         );
+        $container->registerService(
+            'NotesController',
+            function($c) {
+                return new \OCA\Grauphel\Controller\NotesController(
+                    $c->query('AppName'),
+                    $c->query('Request'),
+                    $c->query('Session')->getUser()
+                );
+            }
+        );
         $container->registerService(
             'TokenController',
             function($c) {
index ddfc90b861dfdd8494260cf48c20ddb3c362d6b2..1c7e50bf1821d07015f8519f340e111bba5a7463 100644 (file)
@@ -68,6 +68,11 @@ $application->registerRoutes(
                 'name' => 'gui#tag',
                 'verb' => 'GET',
             ),
+            array(
+                'url'  => '/note/{guid}.xml',
+                'name' => 'notes#xml',
+                'verb' => 'GET',
+            ),
             array(
                 'url'  => '/note/{guid}',
                 'name' => 'gui#note',
index 4f74ab53f1fa3e81f9b29e67a4e64f328b79463f..fc97b049a7b2bd687849ba92015b6c87604fcc47 100644 (file)
@@ -96,6 +96,16 @@ class GuiController extends Controller
                 'note-content' => $converter->convert(
                     $note->{'note-content'}
                 ),
+                'links' => array(
+                    'json' => $this->urlGen->linkToRoute(
+                        'grauphel.api.note', array(
+                            'guid' => $guid, 'username' => $this->user->getUid()
+                        )
+                    ),
+                    'xml' => $this->urlGen->linkToRoute(
+                        'grauphel.notes.xml', array('guid' => $guid)
+                    ),
+                )
             )
         );
 
diff --git a/controller/notescontroller.php b/controller/notescontroller.php
new file mode 100644 (file)
index 0000000..02bfc93
--- /dev/null
@@ -0,0 +1,104 @@
+<?php
+/**
+ * Part of grauphel
+ *
+ * PHP version 5
+ *
+ * @category  Tools
+ * @package   Grauphel
+ * @author    Christian Weiske <cweiske@cweiske.de>
+ * @copyright 2014 Christian Weiske
+ * @license   http://www.gnu.org/licenses/agpl.html GNU AGPL v3
+ * @link      http://cweiske.de/grauphel.htm
+ */
+namespace OCA\Grauphel\Controller;
+
+use \OCP\AppFramework\Controller;
+use \OCP\AppFramework\Http\TemplateResponse;
+use \OCA\Grauphel\Lib\Client;
+use \OCA\Grauphel\Lib\TokenStorage;
+use \OCA\Grauphel\Lib\Response\ErrorResponse;
+
+/**
+ * Owncloud frontend: Notes
+ *
+ * @category  Tools
+ * @package   Grauphel
+ * @author    Christian Weiske <cweiske@cweiske.de>
+ * @copyright 2014 Christian Weiske
+ * @license   http://www.gnu.org/licenses/agpl.html GNU AGPL v3
+ * @version   Release: @package_version@
+ * @link      http://cweiske.de/grauphel.htm
+ */
+class NotesController extends Controller
+{
+    /**
+     * constructor of the controller
+     *
+     * @param string   $appName Name of the app
+     * @param IRequest $request Instance of the request
+     */
+    public function __construct($appName, \OCP\IRequest $request, $user)
+    {
+        parent::__construct($appName, $request);
+        $this->user   = $user;
+
+        //default http header: we assume something is broken
+        header('HTTP/1.0 500 Internal Server Error');
+    }
+
+    /**
+     * Output a note in tomboy XML format
+     *
+     * @link https://wiki.gnome.org/Apps/Tomboy/NoteXmlFormat
+     *
+     * @NoAdminRequired
+     * @NoCSRFRequired
+     */
+    public function xml($guid)
+    {
+        $note = $this->getNotes()->load($guid, false);
+
+        $xw = new \XMLWriter();
+        $xw->openMemory();
+        $xw->startDocument('1.0', 'utf-8');
+
+        $xw->startElementNS(null, 'note', 'http://beatniksoftware.com/tomboy');
+        $xw->writeAttribute('version', '0.3');
+        $xw->writeAttribute('xmlns:link', 'http://beatniksoftware.com/tomboy/link');
+        $xw->writeAttribute('xmlns:size', 'http://beatniksoftware.com/tomboy/size');
+
+        $xw->writeElement('title', $note->title);
+        $xw->startElement('text');
+        $xw->writeAttribute('xml:space', 'preserve');
+
+        $xw->startElement('note-content');
+        $xw->writeAttribute('version', $note->{'note-content-version'});
+        $xw->writeRaw($note->{'note-content'});
+        $xw->endElement();//note-content
+        $xw->endElement();//text
+
+        $xw->writeElement('last-change-date', $note->{'last-change-date'});
+        $xw->writeElement('last-metadata-change-date', $note->{'last-metadata-change-date'});
+        $xw->writeElement('create-date', $note->{'create-date'});
+        $xw->writeElement('cursor-position', 0);
+        $xw->writeElement('width', 450);
+        $xw->writeElement('height', 360);
+        $xw->writeElement('x', 0);
+        $xw->writeElement('y', 0);
+        $xw->writeElement('open-on-startup', $note->{'open-on-startup'});
+
+        $xw->endElement();//note
+
+        return new \OCA\Grauphel\Response\XmlResponse($xw->outputMemory());
+    }
+
+    protected function getNotes()
+    {
+        $username = $this->user->getUid();
+        $notes  = new \OCA\Grauphel\Lib\NoteStorage($this->urlGen);
+        $notes->setUsername($username);
+        return $notes;
+    }
+}
+?>
index 1ad5a2d4c0d4ad07cf30adb0942f6d47ad480bfa..571e5800fc93a9a9fcf84a9451cf6d8b85ff55dd 100644 (file)
     color: green;
 }
 
+.app-grauphel #app-content .actions {
+    float: right;
+}
+.app-grauphel #app-content .actions a {
+    color: #555;
+    padding: 14px 10px;
+    position: relative;
+    top: 7px;
+    min-width: 25px;
+    padding: 5px;
+    background-color: rgba(240,240,240,.9);
+}
+
 .app-grauphel .oauth-authorize {
     margin: 2ex;
     text-align: center;
diff --git a/lib/response/xmlresponse.php b/lib/response/xmlresponse.php
new file mode 100644 (file)
index 0000000..050bbff
--- /dev/null
@@ -0,0 +1,43 @@
+<?php
+/**
+ * Part of grauphel
+ *
+ * PHP version 5
+ *
+ * @category  Tools
+ * @package   Grauphel
+ * @author    Christian Weiske <cweiske@cweiske.de>
+ * @copyright 2014 Christian Weiske
+ * @license   http://www.gnu.org/licenses/agpl.html GNU AGPL v3
+ * @link      http://cweiske.de/grauphel.htm
+ */
+namespace OCA\Grauphel\Response;
+
+/**
+ * Returns XML data
+ *
+ * @category  Tools
+ * @package   Grauphel
+ * @author    Christian Weiske <cweiske@cweiske.de>
+ * @copyright 2014 Christian Weiske
+ * @license   http://www.gnu.org/licenses/agpl.html GNU AGPL v3
+ * @version   Release: @package_version@
+ * @link      http://cweiske.de/grauphel.htm
+ */
+class XmlResponse extends \OCP\AppFramework\Http\Response
+{
+    protected $xml;
+
+    public function __construct($xml)
+    {
+        $this->setStatus(\OCP\AppFramework\Http::STATUS_OK);
+        $this->addHeader('Content-Type', 'text/xml; charset=utf-8');
+        $this->xml = $xml;
+    }
+
+    public function render()
+    {
+        return $this->xml;
+    }
+}
+?>
index 296a0d283465e24d4c448645a3ff5cb69700993d..fb2d741c2381803378e954f397388558760fab9c 100644 (file)
@@ -6,6 +6,10 @@
 <script type="text/javascript" src="<?php p(OCP\Util::linkTo('grauphel','js/grauphel.js')); ?>"></script>
 
 <div id="app-content" class="content">
+ <div class="actions">
+  <a class="button" href="<?php echo p($_['links']['json']); ?>">JSON</a>
+  <a class="button" href="<?php echo p($_['links']['xml']); ?>">XML</a>
+ </div>
  <h1><?php echo ($_['note']->title); ?></h1>
  <p class="muted">
   Last modified: