# HG changeset patch
# User Pantxo Diribarne <pantxo.diribarne@gmail.com>
# Date 1522324137 -7200
#      Thu Mar 29 13:48:57 2018 +0200
# Branch stable
# Node ID 0768968673d8db580c563dd3b1b64eb72e025bd3
# Parent  aad35d9302b19a6b03ca811b940de7a970fb4b2c
Allow registering compressed help at run time (bug #53006)

* octave-link.h/cc (octave_link::register_doc, octave_link::unregister_doc):
  new methods
  (octave_link::do_register_doc, octave_link::do_unregister_doc):
  new virtual methods

* octave-qt-link.h/cc (octave_qt_link::do_register_doc,
  octave_qt_link::do_unregister_doc): reimplement methods
  (register_doc_signal, unregister_doc_signal): new signals

* main-window.cc/h (handle_register_doc, handle_unregister_doc) new methods
  (main_window::register_doc_signal,
   main_window::unregister_doc_signal) new signals
  (main_window::construct_octave_qt_link) connect qt-link's
  (un)register_doc_signal to corresponding handle_ method

* documentation-dock-widget.cc/h (documentation_dock_widget::registerDoc,
  documentation_dock_widget::unregisterDoc) new methods

* documentation.cc/h (documentation::registerDoc, documentation::unregisterDoc)
  new methods
  (documentation::documentation): read collection from oct_doc_dir if necessary

diff -r aad35d9302b1 -r 0768968673d8 libgui/src/documentation-dock-widget.cc
--- a/libgui/src/documentation-dock-widget.cc	Thu Mar 29 12:58:16 2018 +0200
+++ b/libgui/src/documentation-dock-widget.cc	Thu Mar 29 13:48:57 2018 +0200
@@ -45,6 +45,12 @@
 
     connect (p, SIGNAL (show_doc_signal (const QString&)),
              this, SLOT (showDoc (const QString&)));
+
+    connect (p, SIGNAL (register_doc_signal (const QString&)),
+             this, SLOT (registerDoc (const QString&)));
+    
+    connect (p, SIGNAL (unregister_doc_signal (const QString&)),
+             this, SLOT (unregisterDoc (const QString&)));
   }
 
   documentation_dock_widget::~documentation_dock_widget (void)
@@ -83,4 +89,14 @@
 
     m_docs->load_ref (name);
   }
+
+  void documentation_dock_widget::registerDoc (const QString& name)
+  {
+    m_docs->registerDoc (name);
+  }
+
+  void documentation_dock_widget::unregisterDoc (const QString& name)
+  {
+    m_docs->unregisterDoc (name);
+  }
 }
diff -r aad35d9302b1 -r 0768968673d8 libgui/src/documentation-dock-widget.h
--- a/libgui/src/documentation-dock-widget.h	Thu Mar 29 12:58:16 2018 +0200
+++ b/libgui/src/documentation-dock-widget.h	Thu Mar 29 13:48:57 2018 +0200
@@ -48,6 +48,8 @@
     void selectAll (void);
 
     void showDoc (const QString & name);
+    void registerDoc (const QString & name);
+    void unregisterDoc (const QString & name);
 
   private:
 
diff -r aad35d9302b1 -r 0768968673d8 libgui/src/documentation.cc
--- a/libgui/src/documentation.cc	Thu Mar 29 12:58:16 2018 +0200
+++ b/libgui/src/documentation.cc	Thu Mar 29 13:48:57 2018 +0200
@@ -24,6 +24,9 @@
 #  include "config.h"
 #endif
 
+#include "defaults.h"
+#include "file-ops.h"
+
 #include <QApplication>
 #include <QCompleter>
 #include <QHelpContentWidget>
@@ -47,6 +50,10 @@
     : QSplitter (Qt::Horizontal, p)
   {
     QString collection = getenv ("OCTAVE_QTHELP_COLLECTION");
+    if (collection.isEmpty ())
+      collection = QString::fromStdString (octave::config::oct_doc_dir ()
+                                         + octave::sys::file_ops::dir_sep_str ()
+                                         + "octave_interpreter.qhc");
 
     // Setup the help engine and load the help data
     m_help_engine = new QHelpEngine (collection, this);
@@ -225,6 +232,36 @@
     m_filter->setCurrentIndex (0);
   }
 
+  void documentation::registerDoc (const QString& qch)
+  { 
+    if (m_help_engine)
+      {
+        QString ns = m_help_engine->namespaceName (qch);
+        if (m_help_engine->registeredDocumentations ().contains (ns))
+          {
+            if (m_help_engine->documentationFileName (ns) == qch)
+              ; // Do nothing
+            else
+              {
+                m_help_engine->unregisterDocumentation (ns);
+                m_help_engine->registerDocumentation (qch);
+              }
+          }
+        else if (! m_help_engine->registerDocumentation (qch))
+          QMessageBox::warning (this, tr ("Octave Documentation"),
+                                tr ("Unable to register help file %1.").
+                                arg (qch));
+      }
+  }
+  
+  void documentation::unregisterDoc (const QString& qch)
+  {
+    QString ns = m_help_engine->namespaceName (qch);
+    if (m_help_engine
+        && m_help_engine->registeredDocumentations ().contains (ns)
+        && m_help_engine->documentationFileName (ns) == qch)
+      m_help_engine->unregisterDocumentation (ns);
+  }
 
 
   // The documentation browser
diff -r aad35d9302b1 -r 0768968673d8 libgui/src/documentation.h
--- a/libgui/src/documentation.h	Thu Mar 29 12:58:16 2018 +0200
+++ b/libgui/src/documentation.h	Thu Mar 29 13:48:57 2018 +0200
@@ -76,6 +76,8 @@
     void selectAll (void);
 
     void load_ref (const QString & name);
+    void registerDoc (const QString & name);
+    void unregisterDoc (const QString & name);
 
   private slots:
 
diff -r aad35d9302b1 -r 0768968673d8 libgui/src/main-window.cc
--- a/libgui/src/main-window.cc	Thu Mar 29 12:58:16 2018 +0200
+++ b/libgui/src/main-window.cc	Thu Mar 29 13:48:57 2018 +0200
@@ -1476,6 +1476,16 @@
     emit show_doc_signal (file);
   }
 
+  void main_window::handle_register_doc (const QString& file)
+  {
+    emit register_doc_signal (file);
+  }
+
+  void main_window::handle_unregister_doc (const QString& file)
+  {
+    emit unregister_doc_signal (file);
+  }
+
   void main_window::handle_octave_ready (void)
   {
     // actions after the startup files are executed
@@ -1984,6 +1994,14 @@
         connect (m_octave_qt_link,
                  SIGNAL (show_doc_signal (const QString &)),
                  this, SLOT (handle_show_doc (const QString &)));
+        
+        connect (m_octave_qt_link,
+                 SIGNAL (register_doc_signal (const QString &)),
+                 this, SLOT (handle_register_doc (const QString &)));
+        
+        connect (m_octave_qt_link,
+                 SIGNAL (unregister_doc_signal (const QString &)),
+                 this, SLOT (handle_unregister_doc (const QString &)));
       }
 
     // Defer initializing and executing the interpreter until after the main
diff -r aad35d9302b1 -r 0768968673d8 libgui/src/main-window.h
--- a/libgui/src/main-window.h	Thu Mar 29 12:58:16 2018 +0200
+++ b/libgui/src/main-window.h	Thu Mar 29 13:48:57 2018 +0200
@@ -127,6 +127,8 @@
     void open_file_signal (const QString& file, const QString& enc, int line);
 
     void show_doc_signal (const QString&);
+    void register_doc_signal (const QString&);
+    void unregister_doc_signal (const QString&);
 
     void insert_debugger_pointer_signal (const QString& file, int line);
     void delete_debugger_pointer_signal (const QString& file, int line);
@@ -243,6 +245,8 @@
                                    const QString& multimode);
 
     void handle_show_doc (const QString& file);
+    void handle_register_doc (const QString& file);
+    void handle_unregister_doc (const QString& file);
 
     void handle_octave_ready ();
     void handle_octave_finished (int);
diff -r aad35d9302b1 -r 0768968673d8 libgui/src/octave-qt-link.cc
--- a/libgui/src/octave-qt-link.cc	Thu Mar 29 12:58:16 2018 +0200
+++ b/libgui/src/octave-qt-link.cc	Thu Mar 29 13:48:57 2018 +0200
@@ -587,6 +587,16 @@
     emit show_doc_signal (QString::fromStdString (file));
   }
 
+  void octave_qt_link::do_register_doc (const std::string& file)
+  {
+    emit register_doc_signal (QString::fromStdString (file));
+  }
+
+  void octave_qt_link::do_unregister_doc (const std::string& file)
+  {
+    emit unregister_doc_signal (QString::fromStdString (file));
+  }
+
   void octave_qt_link::do_edit_variable (const std::string& expr,
                                          const octave_value& val)
   {
diff -r aad35d9302b1 -r 0768968673d8 libgui/src/octave-qt-link.h
--- a/libgui/src/octave-qt-link.h	Thu Mar 29 12:58:16 2018 +0200
+++ b/libgui/src/octave-qt-link.h	Thu Mar 29 13:48:57 2018 +0200
@@ -142,6 +142,8 @@
     void do_show_preferences (void);
 
     void do_show_doc (const std::string& file);
+    void do_register_doc (const std::string& file);
+    void do_unregister_doc (const std::string& file);
 
     void do_edit_variable (const std::string& name, const octave_value& val);
 
@@ -196,6 +198,10 @@
 
     void show_doc_signal (const QString& file);
 
+    void register_doc_signal (const QString& file);
+
+    void unregister_doc_signal (const QString& file);
+
     void edit_variable_signal (const QString& name, const octave_value& val);
 
     void refresh_variable_editor_signal (void);
diff -r aad35d9302b1 -r 0768968673d8 libinterp/corefcn/octave-link.cc
--- a/libinterp/corefcn/octave-link.cc	Thu Mar 29 12:58:16 2018 +0200
+++ b/libinterp/corefcn/octave-link.cc	Thu Mar 29 13:48:57 2018 +0200
@@ -432,3 +432,31 @@
 
   return ovl (octave_link::show_doc (file));
 }
+
+DEFUN (__octave_link_register_doc__, args, ,
+       doc: /* -*- texinfo -*-
+@deftypefn {} {} __octave_link_register_doc__ (@var{filename})
+Undocumented internal function.
+@end deftypefn */)
+{
+  std::string file;
+
+  if (args.length () >= 1)
+    file = args(0).string_value();
+
+  return ovl (octave_link::register_doc (file));
+}
+
+DEFUN (__octave_link_unregister_doc__, args, ,
+       doc: /* -*- texinfo -*-
+@deftypefn {} {} __octave_link_unregister_doc__ (@var{filename})
+Undocumented internal function.
+@end deftypefn */)
+{
+  std::string file;
+
+  if (args.length () >= 1)
+    file = args(0).string_value();
+
+  return ovl (octave_link::unregister_doc (file));
+}
diff -r aad35d9302b1 -r 0768968673d8 libinterp/corefcn/octave-link.h
--- a/libinterp/corefcn/octave-link.h	Thu Mar 29 12:58:16 2018 +0200
+++ b/libinterp/corefcn/octave-link.h	Thu Mar 29 13:48:57 2018 +0200
@@ -430,6 +430,32 @@
   }
 
   static bool
+  register_doc (const std::string & file)
+  {
+    if (enabled ())
+      {
+        instance->do_register_doc (file);
+        return true;
+      }
+    else
+      return false;
+
+  }
+
+  static bool
+  unregister_doc (const std::string & file)
+  {
+    if (enabled ())
+      {
+        instance->do_unregister_doc (file);
+        return true;
+      }
+    else
+      return false;
+
+  }
+
+  static bool
   edit_variable (const std::string &name, const octave_value& val)
   {
     if (enabled ())
@@ -601,6 +627,10 @@
   virtual void do_show_preferences (void) = 0;
 
   virtual void do_show_doc (const std::string& file) = 0;
+  
+  virtual void do_register_doc (const std::string& file) = 0;
+
+  virtual void do_unregister_doc (const std::string& file) = 0;
 
   virtual void
   do_edit_variable (const std::string& name, const octave_value& val) = 0;
