[sushi] Add a native LibreOffice renderer using LOKDocView



commit f219bb2d8c535d48175adbc183329ab7bf36bd2d
Author: Cosimo Cecchi <cosimoc gnome org>
Date:   Mon Jun 17 18:44:21 2019 -0700

    Add a native LibreOffice renderer using LOKDocView
    
    This does not really cover all the possible configurations
    LibreOffice and Sushi can get installed, so for now we make this
    renderer optional, and still load LO files by converting them to PDF
    in case we can't find LOKDocView.

 src/org.gnome.NautilusPreviewer.src.gresource.xml |   1 +
 src/viewers/evince.js                             |  20 ++--
 src/viewers/libreoffice.js                        | 112 ++++++++++++++++++++++
 3 files changed, 119 insertions(+), 14 deletions(-)
---
diff --git a/src/org.gnome.NautilusPreviewer.src.gresource.xml 
b/src/org.gnome.NautilusPreviewer.src.gresource.xml
index b464b4a..e5e99a3 100644
--- a/src/org.gnome.NautilusPreviewer.src.gresource.xml
+++ b/src/org.gnome.NautilusPreviewer.src.gresource.xml
@@ -16,6 +16,7 @@
     <file>viewers/gst.js</file>
     <file>viewers/html.js</file>
     <file>viewers/image.js</file>
+    <file>viewers/libreoffice.js</file>
     <file>viewers/text.js</file>
   </gresource>
 </gresources>
diff --git a/src/viewers/evince.js b/src/viewers/evince.js
index abb2839..195bbc1 100644
--- a/src/viewers/evince.js
+++ b/src/viewers/evince.js
@@ -29,6 +29,8 @@ const Constants = imports.util.constants;
 const Renderer = imports.ui.renderer;
 const Utils = imports.ui.utils;
 
+const Libreoffice = imports.viewers.libreoffice;
+
 var Klass = GObject.registerClass({
     Implements: [Renderer.Renderer],
     Properties: {
@@ -111,17 +113,7 @@ var Klass = GObject.registerClass({
 });
 
 EvinceDocument.init();
-
-let officeTypes = [
-    'application/vnd.oasis.opendocument.text',
-    'application/vnd.oasis.opendocument.presentation',
-    'application/vnd.oasis.opendocument.spreadsheet',
-    'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
-    'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
-    'application/vnd.openxmlformats-officedocument.presentationml.presentation',
-    'application/msword',
-    'application/vnd.ms-excel',
-    'application/vnd.ms-powerpoint',
-    'application/rtf'
-];
-var mimeTypes = Sushi.query_supported_document_types().concat(officeTypes);
+var evinceTypes = Sushi.query_supported_document_types();
+var mimeTypes = evinceTypes;
+if (!Libreoffice.isAvailable())
+    mimeTypes = mimeTypes.concat(Libreoffice.officeTypes);
diff --git a/src/viewers/libreoffice.js b/src/viewers/libreoffice.js
new file mode 100644
index 0000000..f3deb98
--- /dev/null
+++ b/src/viewers/libreoffice.js
@@ -0,0 +1,112 @@
+const {GLib, GObject, Gtk} = imports.gi;
+
+var LOKDocView;
+try {
+    LOKDocView = imports.gi.LOKDocView;
+} catch(e) {
+}
+
+var isAvailable = function() {
+    return LOKDocView !== undefined;
+};
+
+const Constants = imports.util.constants;
+const Renderer = imports.ui.renderer;
+const Utils = imports.ui.utils;
+
+var Klass = GObject.registerClass({
+    Implements: [Renderer.Renderer],
+    Properties: {
+        fullscreen: GObject.ParamSpec.boolean('fullscreen', '', '',
+                                              GObject.ParamFlags.READABLE,
+                                              false),
+        ready: GObject.ParamSpec.boolean('ready', '', '',
+                                         GObject.ParamFlags.READABLE,
+                                         false)
+    },
+}, class LibreofficeRenderer extends Gtk.ScrolledWindow {
+    _init(file) {
+        super._init({ hexpand: true,
+                      visible: true,
+                      min_content_height: Constants.VIEW_MIN,
+                      min_content_width: Constants.VIEW_MIN });
+
+        this._lastAllocWidth = 0;
+        this._tickCallbackId = 0;
+
+        this._view = LOKDocView.View.new(null, null);
+        this._view.set_edit(false);
+        this._view.show();
+        this.add(this._view);
+
+        this._view.open_document(file.get_uri(), '{}', null, null);
+        this.isReady();
+    }
+
+    vfunc_size_allocate(allocation) {
+        super.vfunc_size_allocate(allocation);
+
+        if (this._view.width_request == -1)
+            return;
+
+        if (this._tickCallbackId != 0)
+            this.remove_tick_callback(this._tickCallbackId);
+        this._tickCallbackId = this.add_tick_callback(this._resizeView.bind(this));
+    }
+
+    _resizeView() {
+        this._tickCallbackId = 0;
+
+        let allocWidth = this.get_allocated_width();
+        if (this._lastAllocWidth == allocWidth)
+            return;
+
+        // Match the Evince renderer behavior and resize the document upon
+        // receiving a new allocation.
+        // We rely on the fact that LOKDocView always sets its size using
+        // gtk_widget_set_size_request(), so we can know how much it will
+        // scale to after we set a new zoom level.
+        let zoomLevel = this._view.zoom_level;
+
+        if (this._view.width_request < allocWidth) {
+            while ((this._view.width_request < allocWidth) && this._view.can_zoom_in) {
+                zoomLevel = this._view.zoom_level;
+                this._view.zoom_level += 0.1;
+            }
+        } else if (this._view.width_request > allocWidth) {
+            while ((this._view.width_request >= allocWidth) && this._view.can_zoom_out) {
+                this._view.zoom_level -= 0.1;
+                zoomLevel = this._view.zoom_level;
+            }
+        }
+
+        this._view.zoom_level = zoomLevel;
+        this._lastAllocWidth = allocWidth;
+    }
+
+    get moveOnClick() {
+        return false;
+    }
+
+    populateToolbar(toolbar) {
+        let toolbarZoom = Utils.createFullscreenButton(this);
+        toolbar.add(toolbarZoom);
+    }
+});
+
+var officeTypes = [
+    'application/vnd.oasis.opendocument.text',
+    'application/vnd.oasis.opendocument.presentation',
+    'application/vnd.oasis.opendocument.spreadsheet',
+    'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
+    'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
+    'application/vnd.openxmlformats-officedocument.presentationml.presentation',
+    'application/msword',
+    'application/vnd.ms-excel',
+    'application/vnd.ms-powerpoint',
+    'application/rtf'
+];
+
+var mimeTypes = [];
+if (isAvailable())
+    mimeTypes = officeTypes;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]