[gedit-list] Re: patch for bonobo interface



On Sat, 2002-08-24 at 08:18, Paolo Maggi wrote:
> Hi James,
> 
> >I added the insert and delete methods like we had discussed (and the
> >exception for read only docs).  
> >
> Cool. Have you tested the exception code?
> Please, test it and commit your patch.
> I think we should also add a function to get the lenght of the document, 
> in this way who uses the document interface can implement the append 
> function too.
> 

Yup, I tested the exception stuff.  I have now added a getChars() method
and a getLength() method.

> >I have attached the patch.  Note, that I
> >have not yet fixed the stdin bug.  In gedit-file.c you have a comment at
> >the top of gedit_file_open_from_stdin() that it is broken.  Can you
> >elaborate?  
> >
> hmm... I think it is an old comment. AFAIK, it should work quite well now.
> 

Well, it didn't work for me.  fstat() was always saying that stdin was
empty.  So, I implemented a different way to tell if there was data to
be read by using select().  It seems to work well.  I added a function
in gedit-utils.c to read data from the stdin, and also a function to
convert text to utf8.

> >Also, will it be alright to merge the email plugin now that
> >we have branched?
> >
> Yep, commit it.
> I have written a new plugin to, called indent (you can (un)indent the 
> selected text or the current line if no text is selected).
> 

Cool!  I was just thinking about writing a plugin like that the other
day.  I will commit the email plugin tonight after going over it once
more....


> P.S. I think we should try to use the gedit-list more in order to try to 
> involve other people in patch reviewing.
> 

Yeah, I aggree....cc'ing it
? stamp-h1
? src/.gedit-document-server.c.swp
? src/.gedit-document.c.swp
? src/.gedit-utils.c.swp
? src/.gedit2.c.swp
? src/afile
Index: src/ChangeLog
===================================================================
RCS file: /cvs/gnome/gedit/src/ChangeLog,v
retrieving revision 1.465
diff -u -5 -t -r1.465 ChangeLog
--- src/ChangeLog	24 Aug 2002 16:00:39 -0000	1.465
+++ src/ChangeLog	24 Aug 2002 23:20:46 -0000
@@ -1,5 +1,15 @@
+2002-08-24  James Willcox  <jwillcox gnome org>
+
+        * GNOME_Gedit.idl:  Added "insert", "delete" "getLength", and
+        "getChars" methods.  Also added the DocumentReadOnly exception.
+
+        * gedit-document-server.c:  Implemented said methods.
+
+        * gedit-utils.[ch] (gedit_utils_get_stdin, gedit_utils_convert_to_utf8):
+        New functions.
+
 2002-08-24  Paolo Maggi  <maggi athena polito it>
 
         * gedit-file.c (gedit_file_revert_dialog): new function
         (gedit_file_revert): ask the user if she really wants to revert the file
         
Index: src/GNOME_Gedit.idl
===================================================================
RCS file: /cvs/gnome/gedit/src/GNOME_Gedit.idl,v
retrieving revision 1.2
diff -u -5 -t -r1.2 GNOME_Gedit.idl
--- src/GNOME_Gedit.idl	4 Jul 2002 17:26:21 -0000	1.2
+++ src/GNOME_Gedit.idl	24 Aug 2002 23:20:46 -0000
@@ -13,11 +13,24 @@
                  */
                 typedef string URI;
                 typedef sequence<URI> URIList;
 
                 interface Document : Bonobo::Unknown {
+                        exception DocumentReadOnly {};
+                
                         oneway void setLinePosition (in long line);
+
+                        void insert (in long offset, in string str,
+                                            in long length)
+                                raises (DocumentReadOnly);
+
+                        void delete (in long offset, in long length)
+                                raises (DocumentReadOnly);
+
+                        string getChars (in long offset, in long length);
+
+                        long getLength ();
                 };
 
                 interface Window : Bonobo::Unknown {
                         oneway void openURIList (in URIList locations);
                         oneway void grabFocus ();
Index: src/gedit-document-server.c
===================================================================
RCS file: /cvs/gnome/gedit/src/gedit-document-server.c,v
retrieving revision 1.2
diff -u -5 -t -r1.2 gedit-document-server.c
--- src/gedit-document-server.c	4 Jul 2002 17:26:21 -0000	1.2
+++ src/gedit-document-server.c	24 Aug 2002 23:20:46 -0000
@@ -69,10 +69,100 @@
         doc_server = GEDIT_DOCUMENT_SERVER (bonobo_object_from_servant (_servant));
 
         gedit_document_goto_line (doc_server->doc, position);
 }
 
+static void
+impl_gedit_document_server_insert (PortableServer_Servant _servant,
+                                   const CORBA_long offset,
+                                   const CORBA_char *str,
+                                   const CORBA_long len,
+                                   CORBA_Environment *ev)
+{
+        GeditDocumentServer *doc_server;
+
+        doc_server = GEDIT_DOCUMENT_SERVER (bonobo_object_from_servant (_servant));
+
+        if (gedit_document_is_readonly (doc_server->doc)) {
+                g_warning ("Doc is readonly.  Sending exception.");
+                CORBA_exception_set (ev, CORBA_USER_EXCEPTION,
+                                     ex_GNOME_Gedit_Document_DocumentReadOnly,
+                                     NULL);
+                return;
+        }
+                
+        gedit_document_insert_text (doc_server->doc, (gint)offset,
+                                    (const gchar *)str, (gint)len);
+}
+
+static void
+impl_gedit_document_server_delete (PortableServer_Servant _servant,
+                                   const CORBA_long offset,
+                                   const CORBA_long len,
+                                   CORBA_Environment *ev)
+{
+        GeditDocumentServer *doc_server;
+
+        doc_server = GEDIT_DOCUMENT_SERVER (bonobo_object_from_servant (_servant));
+
+        if (gedit_document_is_readonly (doc_server->doc)) {
+                g_warning ("Doc is readonly.  Sending exception.");
+                CORBA_exception_set (ev, CORBA_USER_EXCEPTION,
+                                     ex_GNOME_Gedit_Document_DocumentReadOnly,
+                                     NULL);
+                return;
+        }
+
+        gedit_document_delete_text (doc_server->doc, (gint)offset, (gint)len);
+}
+
+static CORBA_char *
+impl_gedit_document_server_getChars (PortableServer_Servant _servant,
+                                     const CORBA_long offset,
+                                     const CORBA_long len,
+                                     CORBA_Environment *ev)
+{
+        CORBA_char *ret;
+        gchar *chars;
+        GeditDocumentServer *doc_server;
+        
+        doc_server = GEDIT_DOCUMENT_SERVER (bonobo_object_from_servant (_servant));
+
+        chars = gedit_document_get_chars (doc_server->doc, (gint)offset,
+                                          (gint)offset+(gint)len);
+
+        if (chars == NULL)
+                return NULL;
+
+        ret = CORBA_string_dup (chars);
+
+        g_free (chars);
+
+        return ret;
+}
+
+static CORBA_long
+impl_gedit_document_server_getLength (PortableServer_Servant _servant,
+                                      CORBA_Environment *ev)
+{
+        GeditDocumentServer *doc_server;
+        gchar *buf;
+        CORBA_long len;
+
+        doc_server = GEDIT_DOCUMENT_SERVER (bonobo_object_from_servant (_servant));
+
+        buf = gedit_document_get_buffer (doc_server->doc);
+
+        if (buf == NULL) {
+                return 0;
+        }
+
+        len = strlen (buf);
+        g_free (buf);
+
+        return len;
+}
 
 static void
 gedit_document_server_class_init (GeditDocumentServerClass *klass)
 {
         GObjectClass *object_class = (GObjectClass *) klass;
@@ -82,10 +172,14 @@
 
         object_class->finalize = gedit_document_server_object_finalize;
 
         /* connect implementation callbacks */
         epv->setLinePosition = impl_gedit_document_server_setLinePosition;
+        epv->insert          = impl_gedit_document_server_insert;
+        epv->delete          = impl_gedit_document_server_delete;
+        epv->getChars        = impl_gedit_document_server_getChars;
+        epv->getLength       = impl_gedit_document_server_getLength;
 }
 
 static void
 gedit_document_server_init (GeditDocumentServer *c) 
 {
Index: src/gedit-document.c
===================================================================
RCS file: /cvs/gnome/gedit/src/gedit-document.c,v
retrieving revision 1.48
diff -u -5 -t -r1.48 gedit-document.c
--- src/gedit-document.c	13 Aug 2002 18:31:00 -0000	1.48
+++ src/gedit-document.c	24 Aug 2002 23:20:50 -0000
@@ -838,115 +838,55 @@
         g_signal_emit (G_OBJECT (doc), document_signals[LOADED], 0);
 
         return TRUE;
 }
 
-#define GEDIT_STDIN_BUFSIZE 1024
-
 gboolean
 gedit_document_load_from_stdin (GeditDocument* doc, GError **error)
 {
-        GString * file_contents;
-        gchar *tmp_buf = NULL;
-        struct stat stats;
-        guint buffer_length;
-
+        gchar *contents;
+        gchar *converted_contents;
         GtkTextIter iter, end;
-        GnomeVFSResult  res;
+        int len;
         
         gedit_debug (DEBUG_DOCUMENT, "");
 
         g_return_val_if_fail (doc != NULL, FALSE);
         
-        fstat (STDIN_FILENO, &stats);
-        
-        if (stats.st_size  == 0)
+        contents = gedit_utils_get_stdin ();
+        if (contents == NULL)
                 return FALSE;
-
-        tmp_buf = g_new0 (gchar, GEDIT_STDIN_BUFSIZE + 1);
-        g_return_val_if_fail (tmp_buf != NULL, FALSE);
-
-        file_contents = g_string_new (NULL);
-        
-        while (feof (stdin) == 0)
-        {
-                buffer_length = fread (tmp_buf, 1, GEDIT_STDIN_BUFSIZE, stdin);
-                tmp_buf [buffer_length] = '\0';
-                g_string_append (file_contents, tmp_buf);
-
-                if (ferror (stdin) != 0)
-                {
-                        res = gnome_vfs_result_from_errno (); 
                 
-                        g_set_error (error, GEDIT_DOCUMENT_IO_ERROR, res,
-                                gnome_vfs_result_to_string (res));
-
-                        g_free (tmp_buf);
-                        g_string_free (file_contents, TRUE);
-                        return FALSE;
-                }
+        converted_contents = gedit_utils_convert_to_utf8 (contents);
+        if (converted_contents == NULL) {
+                g_set_error (error, GEDIT_DOCUMENT_IO_ERROR, 
+                             GEDIT_ERROR_INVALID_UTF8_DATA,
+                             _("Invalid UTF-8 data"));
+                g_free (contents);
+                return FALSE;
         }
 
-        fclose (stdin);
+        len = strlen (converted_contents);
 
-        if (file_contents->len > 0)
-        {
-                if (!g_utf8_validate (file_contents->str, file_contents->len, NULL))
-                {
-                        /* The file contains invalid UTF8 data */
-                        /* Try to convert it to UTF-8 from currence locale */
-                        GError *conv_error = NULL;
-                        gchar* converted_file_contents = NULL;
-                        gsize bytes_written;
-                        
-                        converted_file_contents = g_locale_to_utf8 (file_contents->str, file_contents->len,
-                                        NULL, &bytes_written, &conv_error); 
-                                                
-                        if ((conv_error != NULL) || 
-                            !g_utf8_validate (converted_file_contents, bytes_written, NULL))            
-                        {
-
-                                /* Coversion failed */  
-                                if (conv_error != NULL)
-                                        g_error_free (conv_error);
-
-                                g_set_error (error, GEDIT_DOCUMENT_IO_ERROR, 
-                                             GEDIT_ERROR_INVALID_UTF8_DATA,
-                                             _("Invalid UTF-8 data"));
-                                
-                                if (converted_file_contents != NULL)
-                                        g_free (converted_file_contents);
-                                
-                                g_string_free (file_contents, TRUE);
-                                
-                                return FALSE;
-                        }
-
-                        g_string_free (file_contents, TRUE);
-
-                        /* FIXME: this could be more efficient */
-                        file_contents = g_string_new (converted_file_contents);
-                }
-
-                gedit_undo_manager_begin_not_undoable_action (doc->priv->undo_manager);
-                /* Insert text in the buffer */
-                gtk_text_buffer_get_iter_at_offset (GTK_TEXT_BUFFER (doc), &iter, 0);
-                gtk_text_buffer_insert (GTK_TEXT_BUFFER (doc), &iter, file_contents->str, file_contents->len);
-
-                /* We had a newline in the buffer to begin with. (The buffer always contains
-                 * a newline, so we delete to the end of the buffer to clean up. */
-                gtk_text_buffer_get_end_iter (GTK_TEXT_BUFFER (doc), &end);
-                gtk_text_buffer_delete (GTK_TEXT_BUFFER (doc), &iter, &end);
-
-                /* Place the cursor at the start of the document */
-                gtk_text_buffer_get_iter_at_offset (GTK_TEXT_BUFFER (doc), &iter, 0);
-                gtk_text_buffer_place_cursor (GTK_TEXT_BUFFER (doc), &iter);
+        gedit_undo_manager_begin_not_undoable_action (doc->priv->undo_manager);
+        /* Insert text in the buffer */
+        gtk_text_buffer_get_iter_at_offset (GTK_TEXT_BUFFER (doc), &iter, 0);
+        gtk_text_buffer_insert (GTK_TEXT_BUFFER (doc), &iter, converted_contents, len);
+
+        /* We had a newline in the buffer to begin with. (The buffer always contains
+         * a newline, so we delete to the end of the buffer to clean up. */
+        gtk_text_buffer_get_end_iter (GTK_TEXT_BUFFER (doc), &end);
+        gtk_text_buffer_delete (GTK_TEXT_BUFFER (doc), &iter, &end);
+
+        /* Place the cursor at the start of the document */
+        gtk_text_buffer_get_iter_at_offset (GTK_TEXT_BUFFER (doc), &iter, 0);
+        gtk_text_buffer_place_cursor (GTK_TEXT_BUFFER (doc), &iter);
 
-                gedit_undo_manager_end_not_undoable_action (doc->priv->undo_manager);
-        }
+        gedit_undo_manager_end_not_undoable_action (doc->priv->undo_manager);
 
-        g_string_free (file_contents, TRUE);
+        g_free (contents);
+        g_free (converted_contents);
 
         gedit_document_set_readonly (doc, FALSE);
         gtk_text_buffer_set_modified (GTK_TEXT_BUFFER (doc), TRUE);
 
         g_signal_emit (G_OBJECT (doc), document_signals [LOADED], 0);
Index: src/gedit-utils.c
===================================================================
RCS file: /cvs/gnome/gedit/src/gedit-utils.c,v
retrieving revision 1.33
diff -u -5 -t -r1.33 gedit-utils.c
--- src/gedit-utils.c	23 Jul 2002 15:30:46 -0000	1.33
+++ src/gedit-utils.c	24 Aug 2002 23:20:55 -0000
@@ -45,10 +45,14 @@
 #include "gedit2.h"
 #include "bonobo-mdi.h"
 #include "gedit-document.h"
 #include "gedit-debug.h"
 
+#define GEDIT_STDIN_BUFSIZE 1024
+#define DEFAULT_ENCODING "ISO-8859-15"
+
+
 /* =================================================== */
 /* Flash */
 
 struct _MessageInfo {
   BonoboWindow * win;
@@ -1532,6 +1536,118 @@
         
         if (fd == -1)
                 return FALSE;
         
         return (close (fd) == 0);
+}
+
+gchar *
+gedit_utils_get_stdin (void)
+{
+        GString * file_contents;
+        gchar *tmp_buf = NULL;
+        guint buffer_length;
+        fd_set readfds;
+        struct timeval to;
+
+        gedit_debug (DEBUG_DOCUMENT, "");
+
+        FD_ZERO (&readfds);
+        FD_SET (STDIN_FILENO, &readfds);
+        to.tv_sec = 0;
+        to.tv_usec = 1; /* wait for 1 microsecond */
+        if (select (STDIN_FILENO+1, &readfds, NULL, NULL, &to) <= 0)
+                return NULL;
+
+        tmp_buf = g_new0 (gchar, GEDIT_STDIN_BUFSIZE + 1);
+        g_return_val_if_fail (tmp_buf != NULL, NULL);
+
+        file_contents = g_string_new (NULL);
+        
+        while (feof (stdin) == 0)
+        {
+                buffer_length = fread (tmp_buf, 1, GEDIT_STDIN_BUFSIZE, stdin);
+                tmp_buf [buffer_length] = '\0';
+                g_string_append (file_contents, tmp_buf);
+
+                if (ferror (stdin) != 0)
+                {
+                        g_warning ("Couldn't read stdin: %s", strerror (errno));
+                        g_free (tmp_buf);
+                        g_string_free (file_contents, TRUE);
+                        return NULL;
+                }
+        }
+
+        fclose (stdin);
+
+        return g_string_free (file_contents, FALSE);
+}
+
+gchar *
+gedit_utils_convert_to_utf8 (const gchar *text)
+{
+        GString *file_contents;
+
+        file_contents = g_string_new (text);
+        
+        if (file_contents->len > 0)
+        {
+                if (!g_utf8_validate (file_contents->str, file_contents->len, NULL))
+                {
+                        /* The file contains invalid UTF8 data */
+                        /* Try to convert it to UTF-8 from currence locale */
+                        GError *conv_error = NULL;
+                        gchar* converted_file_contents = NULL;
+                        gsize bytes_written;
+                        
+                        converted_file_contents = g_locale_to_utf8 (file_contents->str, file_contents->len,
+                                        NULL, &bytes_written, &conv_error); 
+                                                
+                        if ((conv_error != NULL) || 
+                            !g_utf8_validate (converted_file_contents, bytes_written, NULL))            
+                        {
+
+                                /* Coversion failed */  
+                                if (conv_error != NULL) {
+                                        g_error_free (conv_error);
+                                        conv_error = NULL;
+                                }
+
+                                
+                                if (converted_file_contents != NULL)
+                                        g_free (converted_file_contents);
+
+                                /* Try to convert it to UTF-8 from default encoding */
+                                converted_file_contents = g_convert (file_contents->str, file_contents->len, 
+                                                "UTF-8", DEFAULT_ENCODING,
+                                                NULL, &bytes_written, &conv_error); 
+                                                        
+                                if ((conv_error != NULL) || 
+                                        !g_utf8_validate (converted_file_contents, bytes_written, NULL))                
+                                {
+                                        /* Coversion failed */  
+                                        if (conv_error != NULL)
+                                                g_error_free (conv_error);
+
+                                        g_warning ("Couldn't read stdin:  Invalid UTF-8 data");
+                                
+                                        if (converted_file_contents != NULL)
+                                                g_free (converted_file_contents);
+                                        g_string_free (file_contents, TRUE);
+
+                                        return NULL;
+                                }
+                                
+                        }
+
+                        g_string_free (file_contents, TRUE);
+
+                        /* FIXME: this could be more efficient */
+                        file_contents = g_string_new (converted_file_contents);
+                }
+
+                return g_string_free (file_contents, FALSE);
+        }
+
+        return NULL;
 }
Index: src/gedit-utils.h
===================================================================
RCS file: /cvs/gnome/gedit/src/gedit-utils.h,v
retrieving revision 1.19
diff -u -5 -t -r1.19 gedit-utils.h
--- src/gedit-utils.h	16 Jul 2002 16:26:24 -0000	1.19
+++ src/gedit-utils.h	24 Aug 2002 23:20:55 -0000
@@ -87,8 +87,12 @@
 
 gchar *gedit_utils_convert_search_text (const gchar *text);
 
 gboolean gedit_utils_create_empty_file (const gchar *uri);
 
+gchar *  gedit_utils_get_stdin (void);
+
+gchar *  gedit_utils_convert_to_utf8 (const gchar *text);
+
 #endif /* __GEDIT_UTILS_H__ */
 
 
Index: src/gedit2.c
===================================================================
RCS file: /cvs/gnome/gedit/src/gedit2.c,v
retrieving revision 1.25
diff -u -5 -t -r1.25 gedit2.c
--- src/gedit2.c	16 Jul 2002 16:26:24 -0000	1.25
+++ src/gedit2.c	24 Aug 2002 23:20:56 -0000
@@ -209,20 +209,24 @@
         GNOME_Gedit_Window window;
         GNOME_Gedit_Document document;
         CommandLineData *data;
         GNOME_Gedit_URIList *uri_list;
         GList *list;
+        gchar *stdin_data;
         int i;
         
         CORBA_exception_init (&env);
 
         server = bonobo_activation_activate_from_id ("OAFIID:GNOME_Gedit_Application",
                                                      0, NULL, &env);
         g_return_if_fail (server != NULL);
 
         if (quit_option)
+        {
                 GNOME_Gedit_Application_quit (server, &env);
+                return;
+        }
                 
         
         if (new_window_option) 
                 GNOME_Gedit_Application_newWindow (server, &env);
 
@@ -263,18 +267,35 @@
 
                 g_list_foreach (data->file_list, (GFunc)g_free, NULL);
                 g_list_free (data->file_list);
                 g_free (data);
         }
-
-        if (!quit_option)
+        
+        stdin_data = gedit_utils_get_stdin ();
+        if (stdin_data)
         {
-                window = GNOME_Gedit_Application_getActiveWindow (server, &env);
+                gchar *converted_text = gedit_utils_convert_to_utf8 (stdin_data);
+
+                if (converted_text != NULL)
+                {
+                        window = GNOME_Gedit_Application_getActiveWindow (server, &env);
+                        document = GNOME_Gedit_Window_newDocument (window, &env);
 
-                /* at the very least, we focus the active window */
-                GNOME_Gedit_Window_grabFocus (window, &env);
+                        GNOME_Gedit_Document_insert (document, 0, converted_text,
+                                             strlen (converted_text), &env);
+
+                        g_free (converted_text);
+                }
+
+                g_free (stdin_data);
         }
+
+
+        window = GNOME_Gedit_Application_getActiveWindow (server, &env);
+
+        /* at the very least, we focus the active window */
+        GNOME_Gedit_Window_grabFocus (window, &env);
 
         bonobo_object_release_unref (server, &env);
         CORBA_exception_free (&env);
 }
 


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