Code

r15629@tres: ted | 2007-06-26 21:47:00 -0700
authorgouldtj <gouldtj@users.sourceforge.net>
Wed, 27 Jun 2007 06:29:41 +0000 (06:29 +0000)
committergouldtj <gouldtj@users.sourceforge.net>
Wed, 27 Jun 2007 06:29:41 +0000 (06:29 +0000)
 Adding in the ability to open files.  Which required some changes.  But,
 now everything seems happy.  Can't break it all anymore.

src/extension/implementation/script.cpp
src/extension/implementation/script.h

index f8cdfd9f2c17e063774a10300a931b0f4ed8f031..e4a7c57475cebb5f5d51ec43eff8bd9916cf09b3 100644 (file)
@@ -48,6 +48,7 @@ FIXME:
 #include "../system.h"
 #include "extension/effect.h"
 #include "extension/output.h"
+#include "extension/input.h"
 #include "extension/db.h"
 #include "script.h"
 #include "dialogs/dialog-events.h"
@@ -519,8 +520,7 @@ Gtk::Widget *
 Script::prefs_input(Inkscape::Extension::Input *module,
                     const gchar *filename)
 {
-    /*return module->autogui(); */
-    return NULL;
+    return module->autogui(NULL, NULL);
 }
 
 
@@ -594,66 +594,48 @@ SPDocument *
 Script::open(Inkscape::Extension::Input *module,
              const gchar *filenameArg)
 {
-#if 0
-    Glib::ustring filename = filenameArg;
-
-    gchar *tmpname;
+    std::list<std::string> params;
 
     // FIXME: process the GError instead of passing NULL
-    gint tempfd = g_file_open_tmp("ink_ext_XXXXXX", &tmpname, NULL);
-    if (tempfd == -1) {
-        /* Error, couldn't create temporary filename */
-        if (errno == EINVAL) {
-            /* The  last  six characters of template were not XXXXXX.  Now template is unchanged. */
-            perror("Extension::Script:  template for filenames is misconfigured.\n");
-            exit(-1);
-        } else if (errno == EEXIST) {
-            /* Now the  contents of template are undefined. */
-            perror("Extension::Script:  Could not create a unique temporary filename\n");
-            return NULL;
-        } else {
-            perror("Extension::Script:  Unknown error creating temporary filename\n");
-            exit(-1);
-        }
+    std::string tempfilename_out;
+    int tempfd_out = 0;
+    try {
+        tempfd_out = Glib::file_open_tmp(tempfilename_out, "ink_ext_XXXXXX");
+    } catch (...) {
+        /// \todo Popup dialog here
+        return NULL;
     }
 
-    Glib::ustring tempfilename_out = tmpname;
-    g_free(tmpname);
-
-    gsize bytesRead = 0;
-    gsize bytesWritten = 0;
-    GError *error = NULL;
-    Glib::ustring local_filename =
-            g_filename_from_utf8( filename.c_str(), -1,
-                                  &bytesRead,  &bytesWritten, &error);
+    std::string lfilename = Glib::filename_from_utf8(filenameArg);
 
-    int data_read = execute(command, local_filename, tempfilename_out);
+    file_listener fileout;
+    int data_read = execute(command, params, lfilename, fileout);
+    fileout.toFile(tempfilename_out);
 
-    SPDocument *mydoc = NULL;
+    SPDocument * mydoc = NULL;
     if (data_read > 10) {
         if (helper_extension.size()==0) {
             mydoc = Inkscape::Extension::open(
-                Inkscape::Extension::db.get(SP_MODULE_KEY_INPUT_SVG),
-                                            tempfilename_out.c_str());
+                  Inkscape::Extension::db.get(SP_MODULE_KEY_INPUT_SVG),
+                  tempfilename_out.c_str());
         } else {
             mydoc = Inkscape::Extension::open(
-                Inkscape::Extension::db.get(helper_extension.c_str()),
-                                            tempfilename_out.c_str());
+                  Inkscape::Extension::db.get(helper_extension.c_str()),
+                  tempfilename_out.c_str());
         }
-    }
+    } // data_read
 
-    if (mydoc != NULL)
-        sp_document_set_uri(mydoc, (const gchar *)filename.c_str());
+    if (mydoc != NULL) {
+        sp_document_set_uri(mydoc, filenameArg);
+    }
 
     // make sure we don't leak file descriptors from g_file_open_tmp
-    close(tempfd);
-    // FIXME: convert to utf8 (from "filename encoding") and unlink_utf8name
-    unlink(tempfilename_out.c_str());
+    close(tempfd_out);
 
+    unlink(tempfilename_out.c_str());
 
     return mydoc;
-#endif
-}
+} // open
 
 
 
@@ -857,10 +839,11 @@ Script::effect(Inkscape::Extension::Effect *module, Inkscape::UI::View::View *do
     fileout.toFile(tempfilename_out);
 
     SPDocument * mydoc = NULL;
-    if (data_read > 10)
+    if (data_read > 10) {
         mydoc = Inkscape::Extension::open(
               Inkscape::Extension::db.get(SP_MODULE_KEY_INPUT_SVG),
               tempfilename_out.c_str());
+    } // data_read
 
     // make sure we don't leak file descriptors from g_file_open_tmp
     close(tempfd_in);
index d8970cf15ac518ea2d584d6e620c443c7ae4e4cd..417b42a891d6f6185037f22fb451a78f8a22c920 100644 (file)
@@ -148,13 +148,16 @@ private:
         sigc::connection _conn;
         Glib::RefPtr<Glib::IOChannel> _channel;
         Glib::RefPtr<Glib::MainLoop> _main_loop;
+        bool _dead;
         
     public:
-        file_listener () { };
+        file_listener () : _dead(false) { };
         ~file_listener () {
             _conn.disconnect();
         };
 
+        bool isDead () { return _dead; }
+
         void init (int fd, Glib::RefPtr<Glib::MainLoop> main) {
             _channel = Glib::IOChannel::create_from_fd(fd);
             _channel->set_encoding();
@@ -172,14 +175,15 @@ private:
 
             Glib::IOStatus status;
             Glib::ustring out;
-            status = _channel->read_to_end(out);
+            status = _channel->read_line(out);
+            _string += out;
 
             if (status != Glib::IO_STATUS_NORMAL) {
                 _main_loop->quit();
+                _dead = true;
                 return false;
             }
 
-            _string += out;
             return true;
         };