Code

Warning and space cleanup
[inkscape.git] / src / ui / dialog / ocaldialogs.cpp
index 7f1d72c70f3cfa5b08221c75cd0bc36f91f9d65a..e9ef6344369ec8910b73402a0d1a02a27576c372 100644 (file)
 # include <config.h>
 #endif
 
+#include <stdio.h>  // rename()
+#include <unistd.h> // close()
+#include <errno.h>  // errno
+#include <string.h> // strerror()
+
 #include "ocaldialogs.h"
 #include "filedialogimpl-gtkmm.h"
 #include "interface.h"
@@ -67,7 +72,7 @@ FileExportToOCALDialog::FileExportToOCALDialog(Gtk::Window &parentWindow,
     /* Set our dialog type (save, export, etc...)*/
     dialogType = fileTypes;
     Gtk::VBox *vbox = get_vbox();
-    
+
     Gtk::Label *fileLabel = new Gtk::Label(_("File"));
 
     fileNameEntry = new Gtk::Entry();
@@ -187,7 +192,7 @@ FileExportToOCALPasswordDialog::FileExportToOCALPasswordDialog(Gtk::Window &pare
     passBox.pack_start(*passLabel);
     passBox.pack_start(*passwordEntry, Gtk::PACK_EXPAND_WIDGET, 3);
     vbox->pack_start(passBox);
-    
+
     add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
     set_default(*add_button(Gtk::Stock::OK,   Gtk::RESPONSE_OK));
 
@@ -259,14 +264,10 @@ FileExportToOCALPasswordDialog::change_title(const Glib::ustring& title)
  */
 void FileListViewText::on_cursor_changed()
 {
-    // create file path
-    myFilename = Glib::get_tmp_dir();
-    myFilename.append(G_DIR_SEPARATOR_S);
     std::vector<Gtk::TreeModel::Path> pathlist;
     pathlist = this->get_selection()->get_selected_rows();
     std::vector<int> posArray(1);
     posArray = pathlist[0].get_indices();
-    myFilename.append(get_text(posArray[0], 2));
 
 #ifdef WITH_GNOME_VFS
     gnome_vfs_init();
@@ -276,9 +277,33 @@ void FileListViewText::on_cursor_changed()
     GnomeVFSFileSize  bytes_written;
     GnomeVFSResult    result;
     guint8 buffer[8192];
+    Glib::ustring fileUrl;
+
+    // FIXME: this would be better as a per-user OCAL cache of files
+    // instead of filling /tmp with downloads.
+    //
+    // create file path
+    const std::string tmptemplate = "ocal-";
+    std::string tmpname;
+    int fd = Glib::file_open_tmp(tmpname, tmptemplate);
+    if (fd<0) {
+        g_warning("Error creating temp file");
+        return;
+    }
+    close(fd);
+    // make sure we don't collide with other users on the same machine
+    myFilename = tmpname;
+    myFilename.append("-");
+    myFilename.append(get_text(posArray[0], 2));
+    // rename based on original image's name, retaining extension
+    if (rename(tmpname.c_str(),myFilename.c_str())<0) {
+        unlink(tmpname.c_str());
+        g_warning("Error creating destination file '%s': %s", myFilename.c_str(), strerror(errno));
+        goto failquit;
+    }
 
     //get file url
-    Glib::ustring fileUrl = get_text(posArray[0], 1); //http url
+    fileUrl = get_text(posArray[0], 1); //http url
 
     //Glib::ustring fileUrl = "dav://"; //dav url
     //fileUrl.append(prefs_get_string_attribute("options.ocalurl", "str"));
@@ -290,8 +315,6 @@ void FileListViewText::on_cursor_changed()
     if (!Glib::get_charset()) //If we are not utf8
         fileUrl = Glib::filename_to_utf8(fileUrl);
 
-    // verifies if the file wasn't previously downloaded
-    if(gnome_vfs_open(&to_handle, myFilename.c_str(), GNOME_VFS_OPEN_READ) == GNOME_VFS_ERROR_NOT_FOUND)
     {
         // open the temp file to receive
         result = gnome_vfs_open (&to_handle, myFilename.c_str(), GNOME_VFS_OPEN_WRITE);
@@ -299,13 +322,13 @@ void FileListViewText::on_cursor_changed()
             result = gnome_vfs_create (&to_handle, myFilename.c_str(), GNOME_VFS_OPEN_WRITE, FALSE, GNOME_VFS_PERM_USER_ALL);
         }
         if (result != GNOME_VFS_OK) {
-            g_warning("Error creating temp file: %s", gnome_vfs_result_to_string(result));
-            return;
+            g_warning("Error creating temp file '%s': %s", myFilename.c_str(), gnome_vfs_result_to_string(result));
+            goto fail;
         }
         result = gnome_vfs_open (&from_handle, fileUrl.c_str(), GNOME_VFS_OPEN_READ);
         if (result != GNOME_VFS_OK) {
             g_warning("Could not find the file in Open Clip Art Library.");
-            return;
+            goto fail;
         }
         // copy the file
         while (1) {
@@ -317,33 +340,34 @@ void FileListViewText::on_cursor_changed()
             }
             if (result != GNOME_VFS_OK) {
                 g_warning("%s", gnome_vfs_result_to_string(result));
-                return;
+                goto fail;
             }
             result = gnome_vfs_write (to_handle, buffer, bytes_read, &bytes_written);
             if (result != GNOME_VFS_OK) {
                 g_warning("%s", gnome_vfs_result_to_string(result));
-                return;
+                goto fail;
             }
             if (bytes_read != bytes_written){
                 g_warning("Bytes read not equal to bytes written");
-                return;
+                goto fail;
             }
         }
     }
-    else
-    {
-        gnome_vfs_close(to_handle);
-    }
     myPreview->showImage(myFilename);
     myLabel->set_text(get_text(posArray[0], 4));
 #endif
+    return;
+fail:
+    unlink(myFilename.c_str());
+failquit:
+    myFilename = "";
 }
 
 
 /*
  * Callback for row activated
  */
-void FileListViewText::on_row_activated(const Gtk::TreeModel::Path& path, Gtk::TreeViewColumn* column)
+void FileListViewText::on_row_activated(const Gtk::TreeModel::Path& /*path*/, Gtk::TreeViewColumn* /*column*/)
 {
     this->on_cursor_changed();
     myButton->activate();
@@ -358,6 +382,31 @@ Glib::ustring FileListViewText::getFilename()
     return myFilename;
 }
 
+
+#ifdef WITH_GNOME_VFS
+/**
+ * Read callback for xmlReadIO(), used below
+ */
+static int vfs_read_callback (GnomeVFSHandle *handle, char* buf, int nb)
+{
+    GnomeVFSFileSize ndone;
+    GnomeVFSResult    result;
+
+    result = gnome_vfs_read (handle, buf, nb, &ndone);
+
+    if (result == GNOME_VFS_OK) {
+        return (int)ndone;
+    } else {
+        if (result != GNOME_VFS_ERROR_EOF) {
+            sp_ui_error_dialog(_("Error while reading the Open Clip Art RSS feed"));
+            g_warning("%s\n", gnome_vfs_result_to_string(result));
+        }
+        return -1;
+    }
+}
+#endif
+
+
 /**
  * Callback for user input into searchTagEntry
  */
@@ -380,76 +429,32 @@ void FileImportFromOCALDialog::searchTagEntryChangedCallback()
 
 #ifdef WITH_GNOME_VFS
 
-    // get the rss feed
+    // open the rss feed
     gnome_vfs_init();
     GnomeVFSHandle    *from_handle = NULL;
-    GnomeVFSHandle    *to_handle = NULL;
-    GnomeVFSFileSize  bytes_read;
-    GnomeVFSFileSize  bytes_written;
     GnomeVFSResult    result;
-    guint8 buffer[8192];
-
-    // create the temp file name
-    Glib::ustring fileName = Glib::get_tmp_dir ();
-    fileName.append(G_DIR_SEPARATOR_S);
-    fileName.append("ocalfeed.xml");
-
-    // open the temp file to receive
-    result = gnome_vfs_open (&to_handle, fileName.c_str(), GNOME_VFS_OPEN_WRITE);
-    if (result == GNOME_VFS_ERROR_NOT_FOUND){
-        result = gnome_vfs_create (&to_handle, fileName.c_str(), GNOME_VFS_OPEN_WRITE, FALSE, GNOME_VFS_PERM_USER_ALL);
-    }
-    if (result != GNOME_VFS_OK) {
-        g_warning("Error creating temp file: %s", gnome_vfs_result_to_string(result));
-        return;
-    }
 
-    // open the rss feed
     result = gnome_vfs_open (&from_handle, uri.c_str(), GNOME_VFS_OPEN_READ);
     if (result != GNOME_VFS_OK) {
         sp_ui_error_dialog(_("Failed to receive the Open Clip Art Library RSS feed. Verify if the server name is correct in Configuration->Misc (e.g.: openclipart.org)"));
         return;
     }
 
-    // copy the file
-    while (1) {
-
-        result = gnome_vfs_read (from_handle, buffer, 8192, &bytes_read);
-
-        if ((result == GNOME_VFS_ERROR_EOF) &&(!bytes_read)){
-            result = gnome_vfs_close (from_handle);
-            result = gnome_vfs_close (to_handle);
-            break;
-        }
-
-        if (result != GNOME_VFS_OK) {
-            g_warning("%s", gnome_vfs_result_to_string(result));
-            return;
-        }
-        result = gnome_vfs_write (to_handle, buffer, bytes_read, &bytes_written);
-        if (result != GNOME_VFS_OK) {
-            g_warning("%s", gnome_vfs_result_to_string(result));
-            return;
-        }
-
-        if (bytes_read != bytes_written){
-            g_warning("Bytes read not equal to bytes written");
-            return;
-        }
-
-    }
-
     // create the resulting xml document tree
     // this initialize the library and test mistakes between compiled and shared library used
-    LIBXML_TEST_VERSION 
+    LIBXML_TEST_VERSION
     xmlDoc *doc = NULL;
     xmlNode *root_element = NULL;
-    doc = xmlReadFile(fileName.c_str(), NULL, 0);
+
+    doc = xmlReadIO ((xmlInputReadCallback) vfs_read_callback,
+        (xmlInputCloseCallback) gnome_vfs_close, from_handle, uri.c_str(), NULL,
+        XML_PARSE_RECOVER);
     if (doc == NULL) {
-        g_warning("Failed to parse %s\n", fileName.c_str());
-    return;
+        sp_ui_error_dialog(_("Server supplied malformed Clip Art feed"));
+        g_warning("Failed to parse %s\n", uri.c_str());
+        return;
     }
-    
+
     // get the root element node
     root_element = xmlDocGetRootElement(doc);
 
@@ -473,11 +478,12 @@ void FileImportFromOCALDialog::searchTagEntryChangedCallback()
     // free the global variables that may have been allocated by the parser
     xmlCleanupParser();
     return;
-#endif    
+#endif
 }
 
+
 /**
- * Prints the names of the all the xml elements 
+ * Prints the names of the all the xml elements
  * that are siblings or children of a given xml node
  */
 void FileImportFromOCALDialog::print_xml_element_names(xmlNode * a_node)
@@ -522,11 +528,11 @@ void FileImportFromOCALDialog::print_xml_element_names(xmlNode * a_node)
 /**
  * Constructor.  Not called directly.  Use the factory.
  */
-FileImportFromOCALDialog::FileImportFromOCALDialog(Gtk::Window& parentWindow, 
-                                      const Glib::ustring &dir,
-                                       FileDialogType fileTypes,
-                                       const Glib::ustring &title) :
-     FileDialogOCALBase(title, parentWindow)
+FileImportFromOCALDialog::FileImportFromOCALDialog(Gtk::Window& parentWindow,
+                                                   const Glib::ustring &/*dir*/,
+                                                   FileDialogType fileTypes,
+                                                   const Glib::ustring &title) :
+    FileDialogOCALBase(title, parentWindow)
 {
     // Initalize to Autodetect
     extension = NULL;
@@ -535,11 +541,13 @@ FileImportFromOCALDialog::FileImportFromOCALDialog(Gtk::Window& parentWindow,
 
     dialogType = fileTypes;
     Gtk::VBox *vbox = get_vbox();
-    Gtk::Label *tagLabel = new Gtk::Label(_("Search Tag"));
+    Gtk::Label *tagLabel = new Gtk::Label(_("Search for:"));
     notFoundLabel = new Gtk::Label(_("No files matched your search"));
     descriptionLabel = new Gtk::Label();
-    descriptionLabel->set_max_width_chars(60);
+    descriptionLabel->set_max_width_chars(260);
+    descriptionLabel->set_size_request(500, -1);
     descriptionLabel->set_single_line_mode(false);
+    descriptionLabel->set_line_wrap(true);
     messageBox.pack_start(*notFoundLabel);
     descriptionBox.pack_start(*descriptionLabel);
     searchTagEntry = new Gtk::Entry();
@@ -562,7 +570,7 @@ FileImportFromOCALDialog::FileImportFromOCALDialog(Gtk::Window& parentWindow,
     listScrolledWindow.add(*filesList);
     // only show the scrollbars when they are necessary:
     listScrolledWindow.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
-    filesList->set_column_title(0, _("Files Found"));
+    filesList->set_column_title(0, _("Files found"));
     listScrolledWindow.set_size_request(400, 180);
     filesList->get_column(1)->set_visible(false); // file url
     filesList->get_column(2)->set_visible(false); // tmp file path
@@ -570,7 +578,7 @@ FileImportFromOCALDialog::FileImportFromOCALDialog(Gtk::Window& parentWindow,
     filesList->get_column(4)->set_visible(false); // file description
     filesBox.pack_start(listScrolledWindow);
     filesBox.pack_start(*filesPreview);
-    vbox->pack_start(tagBox);
+    vbox->pack_start(tagBox, false, false);
     vbox->pack_start(messageBox);
     vbox->pack_start(filesBox);
     vbox->pack_start(descriptionBox);