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 66ed557..7761eb5 100644 (file)
@@ -57,6 +57,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) {
                 Dependencies::get()->urlGen
index ddfc90b..1c7e50b 100644 (file)
@@ -69,6 +69,11 @@ $application->registerRoutes(
                 'verb' => 'GET',
             ),
             array(
+                'url'  => '/note/{guid}.xml',
+                'name' => 'notes#xml',
+                'verb' => 'GET',
+            ),
+            array(
                 'url'  => '/note/{guid}',
                 'name' => 'gui#note',
                 'verb' => 'GET',
index 4f74ab5..fc97b04 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 1ad5a2d..571e580 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 296a0d2..fb2d741 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: