1 /**
2 * Implementation of the ocal dialog interfaces defined in ocaldialog.h
3 *
4 * Authors:
5 * Bruno Dilly
6 * Other dudes from The Inkscape Organization
7 *
8 * Copyright (C) 2007 Bruno Dilly <bruno.dilly@gmail.com>
9 *
10 * Released under GNU GPL, read the file 'COPYING' for more information
11 */
13 #ifdef HAVE_CONFIG_H
14 # include <config.h>
15 #endif
17 #include <stdio.h> // rename()
18 #include <unistd.h> // close()
19 #include <errno.h> // errno
20 #include <string.h> // strerror()
22 #include "ocaldialogs.h"
23 #include "filedialogimpl-gtkmm.h"
24 #include "interface.h"
25 #include "gc-core.h"
26 #include <dialogs/dialog-events.h>
28 namespace Inkscape
29 {
30 namespace UI
31 {
32 namespace Dialog
33 {
35 //########################################################################
36 //# F I L E E X P O R T T O O C A L
37 //########################################################################
39 /**
40 * Callback for fileNameEntry widget
41 */
42 void FileExportToOCALDialog::fileNameEntryChangedCallback()
43 {
44 if (!fileNameEntry)
45 return;
47 Glib::ustring fileName = fileNameEntry->get_text();
48 if (!Glib::get_charset()) //If we are not utf8
49 fileName = Glib::filename_to_utf8(fileName);
51 myFilename = fileName;
52 response(Gtk::RESPONSE_OK);
53 }
55 /**
56 * Constructor
57 */
58 FileExportToOCALDialog::FileExportToOCALDialog(Gtk::Window &parentWindow,
59 FileDialogType fileTypes,
60 const Glib::ustring &title) :
61 FileDialogOCALBase(title, parentWindow)
62 {
63 /*
64 * Start Taking the vertical Box and putting a Label
65 * and a Entry to take the filename
66 * Later put the extension selection and checkbox (?)
67 */
68 /* Initalize to Autodetect */
69 extension = NULL;
70 /* No filename to start out with */
71 myFilename = "";
72 /* Set our dialog type (save, export, etc...)*/
73 dialogType = fileTypes;
74 Gtk::VBox *vbox = get_vbox();
76 Gtk::Label *fileLabel = new Gtk::Label(_("File"));
78 fileNameEntry = new Gtk::Entry();
79 fileNameEntry->set_text(myFilename);
80 fileNameEntry->set_max_length(252); // I am giving the extension approach.
81 fileBox.pack_start(*fileLabel);
82 fileBox.pack_start(*fileNameEntry, Gtk::PACK_EXPAND_WIDGET, 3);
83 vbox->pack_start(fileBox);
85 //Let's do some customization
86 fileNameEntry = NULL;
87 Gtk::Container *cont = get_toplevel();
88 std::vector<Gtk::Entry *> entries;
89 findEntryWidgets(cont, entries);
90 if (entries.size() >=1 )
91 {
92 //Catch when user hits [return] on the text field
93 fileNameEntry = entries[0];
94 fileNameEntry->signal_activate().connect(
95 sigc::mem_fun(*this, &FileExportToOCALDialog::fileNameEntryChangedCallback) );
96 }
98 add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
99 set_default(*add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_OK));
101 show_all_children();
102 }
104 /**
105 * Destructor
106 */
107 FileExportToOCALDialog::~FileExportToOCALDialog()
108 {
109 }
111 /**
112 * Show this dialog modally. Return true if user hits [OK]
113 */
114 bool
115 FileExportToOCALDialog::show()
116 {
117 set_modal (TRUE); //Window
118 sp_transientize((GtkWidget *)gobj()); //Make transient
119 gint b = run(); //Dialog
120 hide();
122 if (b == Gtk::RESPONSE_OK)
123 {
124 return TRUE;
125 }
126 else
127 {
128 return FALSE;
129 }
130 }
132 /**
133 * Get the file name chosen by the user. Valid after an [OK]
134 */
135 Glib::ustring
136 FileExportToOCALDialog::getFilename()
137 {
138 myFilename = fileNameEntry->get_text();
139 if (!Glib::get_charset()) //If we are not utf8
140 myFilename = Glib::filename_to_utf8(myFilename);
142 return myFilename;
143 }
146 void
147 FileExportToOCALDialog::change_title(const Glib::ustring& title)
148 {
149 this->set_title(title);
150 }
153 //########################################################################
154 //# F I L E E X P O R T T O O C A L P A S S W O R D
155 //########################################################################
158 /**
159 * Constructor
160 */
161 FileExportToOCALPasswordDialog::FileExportToOCALPasswordDialog(Gtk::Window &parentWindow,
162 const Glib::ustring &title) : FileDialogOCALBase(title, parentWindow)
163 {
164 /*
165 * Start Taking the vertical Box and putting 2 Labels
166 * and 2 Entries to take the username and password
167 */
168 /* No username and password to start out with */
169 myUsername = "";
170 myPassword = "";
172 Gtk::VBox *vbox = get_vbox();
174 Gtk::Label *userLabel = new Gtk::Label(_("Username:"));
175 Gtk::Label *passLabel = new Gtk::Label(_("Password:"));
177 usernameEntry = new Gtk::Entry();
178 usernameEntry->set_text(myUsername);
179 usernameEntry->set_max_length(255);
181 passwordEntry = new Gtk::Entry();
182 passwordEntry->set_text(myPassword);
183 passwordEntry->set_max_length(255);
184 passwordEntry->set_invisible_char('*');
185 passwordEntry->set_visibility(false);
186 passwordEntry->set_activates_default(true);
188 userBox.pack_start(*userLabel);
189 userBox.pack_start(*usernameEntry, Gtk::PACK_EXPAND_WIDGET, 3);
190 vbox->pack_start(userBox);
192 passBox.pack_start(*passLabel);
193 passBox.pack_start(*passwordEntry, Gtk::PACK_EXPAND_WIDGET, 3);
194 vbox->pack_start(passBox);
196 add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
197 set_default(*add_button(Gtk::Stock::OK, Gtk::RESPONSE_OK));
199 show_all_children();
200 }
203 /**
204 * Destructor
205 */
206 FileExportToOCALPasswordDialog::~FileExportToOCALPasswordDialog()
207 {
208 }
210 /**
211 * Show this dialog modally. Return true if user hits [OK]
212 */
213 bool
214 FileExportToOCALPasswordDialog::show()
215 {
216 set_modal (TRUE); //Window
217 sp_transientize((GtkWidget *)gobj()); //Make transient
218 gint b = run(); //Dialog
219 hide();
221 if (b == Gtk::RESPONSE_OK)
222 {
223 return TRUE;
224 }
225 else
226 {
227 return FALSE;
228 }
229 }
231 /**
232 * Get the username. Valid after an [OK]
233 */
234 Glib::ustring
235 FileExportToOCALPasswordDialog::getUsername()
236 {
237 myUsername = usernameEntry->get_text();
238 return myUsername;
239 }
241 /**
242 * Get the password. Valid after an [OK]
243 */
244 Glib::ustring
245 FileExportToOCALPasswordDialog::getPassword()
246 {
247 myPassword = passwordEntry->get_text();
248 return myPassword;
249 }
251 void
252 FileExportToOCALPasswordDialog::change_title(const Glib::ustring& title)
253 {
254 this->set_title(title);
255 }
258 //#########################################################################
259 //### F I L E I M P O R T F R O M O C A L
260 //#########################################################################
262 /*
263 * Calalback for cursor chage
264 */
265 void FileListViewText::on_cursor_changed()
266 {
267 std::vector<Gtk::TreeModel::Path> pathlist;
268 pathlist = this->get_selection()->get_selected_rows();
269 std::vector<int> posArray(1);
270 posArray = pathlist[0].get_indices();
272 #ifdef WITH_GNOME_VFS
273 gnome_vfs_init();
274 GnomeVFSHandle *from_handle = NULL;
275 GnomeVFSHandle *to_handle = NULL;
276 GnomeVFSFileSize bytes_read;
277 GnomeVFSFileSize bytes_written;
278 GnomeVFSResult result;
279 guint8 buffer[8192];
281 // create file path
282 const std::string tmptemplate = "ocal-XXXXXX";
283 std::string tmpname;
284 int fd = Glib::file_open_tmp(tmpname, tmptemplate);
285 if (fd<0) return;
286 close(fd);
287 Glib::ustring myFilename = Glib::path_get_dirname(tmpname);
288 myFilename.append(G_DIR_SEPARATOR_S);
289 myFilename.append(get_text(posArray[0], 2));
290 if (rename(tmpname.c_str(),myFilename.c_str())<0) {
291 unlink(tmpname.c_str());
292 g_warning("Error creating destination file '%s': %s", myFilename.c_str(), strerror(errno));
293 return;
294 }
296 //get file url
297 Glib::ustring fileUrl = get_text(posArray[0], 1); //http url
299 //Glib::ustring fileUrl = "dav://"; //dav url
300 //fileUrl.append(prefs_get_string_attribute("options.ocalurl", "str"));
301 //fileUrl.append("/dav.php/");
302 //fileUrl.append(get_text(posArray[0], 3)); //author dir
303 //fileUrl.append("/");
304 //fileUrl.append(get_text(posArray[0], 2)); //filename
306 if (!Glib::get_charset()) //If we are not utf8
307 fileUrl = Glib::filename_to_utf8(fileUrl);
309 {
310 // open the temp file to receive
311 result = gnome_vfs_open (&to_handle, myFilename.c_str(), GNOME_VFS_OPEN_WRITE);
312 if (result == GNOME_VFS_ERROR_NOT_FOUND){
313 result = gnome_vfs_create (&to_handle, myFilename.c_str(), GNOME_VFS_OPEN_WRITE, FALSE, GNOME_VFS_PERM_USER_ALL);
314 }
315 if (result != GNOME_VFS_OK) {
316 g_warning("Error creating temp file '%s': %s", myFilename.c_str(), gnome_vfs_result_to_string(result));
317 return;
318 }
319 result = gnome_vfs_open (&from_handle, fileUrl.c_str(), GNOME_VFS_OPEN_READ);
320 if (result != GNOME_VFS_OK) {
321 g_warning("Could not find the file in Open Clip Art Library.");
322 return;
323 }
324 // copy the file
325 while (1) {
326 result = gnome_vfs_read (from_handle, buffer, 8192, &bytes_read);
327 if ((result == GNOME_VFS_ERROR_EOF) &&(!bytes_read)){
328 result = gnome_vfs_close (from_handle);
329 result = gnome_vfs_close (to_handle);
330 break;
331 }
332 if (result != GNOME_VFS_OK) {
333 g_warning("%s", gnome_vfs_result_to_string(result));
334 return;
335 }
336 result = gnome_vfs_write (to_handle, buffer, bytes_read, &bytes_written);
337 if (result != GNOME_VFS_OK) {
338 g_warning("%s", gnome_vfs_result_to_string(result));
339 return;
340 }
341 if (bytes_read != bytes_written){
342 g_warning("Bytes read not equal to bytes written");
343 return;
344 }
345 }
346 }
347 myPreview->showImage(myFilename);
348 myLabel->set_text(get_text(posArray[0], 4));
349 #endif
350 }
353 /*
354 * Callback for row activated
355 */
356 void FileListViewText::on_row_activated(const Gtk::TreeModel::Path& path, Gtk::TreeViewColumn* column)
357 {
358 this->on_cursor_changed();
359 myButton->activate();
360 }
363 /*
364 * Returns the selected filename
365 */
366 Glib::ustring FileListViewText::getFilename()
367 {
368 return myFilename;
369 }
371 /**
372 * Read callback for xmlReadIO(), used below
373 */
374 static int vfs_read_callback (GnomeVFSHandle *handle, char* buf, int nb)
375 {
376 GnomeVFSFileSize ndone;
377 GnomeVFSResult result;
379 result = gnome_vfs_read (handle, buf, nb, &ndone);
381 if (result == GNOME_VFS_OK) {
382 return (int)ndone;
383 } else {
384 if (result != GNOME_VFS_ERROR_EOF) {
385 sp_ui_error_dialog(_("Error while reading the Open Clip Art RSS feed"));
386 g_warning("%s\n", gnome_vfs_result_to_string(result));
387 }
388 return -1;
389 }
390 }
392 /**
393 * Callback for user input into searchTagEntry
394 */
395 void FileImportFromOCALDialog::searchTagEntryChangedCallback()
396 {
397 if (!searchTagEntry)
398 return;
400 notFoundLabel->hide();
401 descriptionLabel->set_text("");
403 Glib::ustring searchTag = searchTagEntry->get_text();
404 // create the ocal uri to get rss feed
405 Glib::ustring uri = "http://";
406 uri.append(prefs_get_string_attribute("options.ocalurl", "str"));
407 uri.append("/media/feed/rss/");
408 uri.append(searchTag);
409 if (!Glib::get_charset()) //If we are not utf8
410 uri = Glib::filename_to_utf8(uri);
412 #ifdef WITH_GNOME_VFS
414 // open the rss feed
415 gnome_vfs_init();
416 GnomeVFSHandle *from_handle = NULL;
417 GnomeVFSResult result;
419 result = gnome_vfs_open (&from_handle, uri.c_str(), GNOME_VFS_OPEN_READ);
420 if (result != GNOME_VFS_OK) {
421 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)"));
422 return;
423 }
425 // create the resulting xml document tree
426 // this initialize the library and test mistakes between compiled and shared library used
427 LIBXML_TEST_VERSION
428 xmlDoc *doc = NULL;
429 xmlNode *root_element = NULL;
431 doc = xmlReadIO ((xmlInputReadCallback) vfs_read_callback,
432 (xmlInputCloseCallback) gnome_vfs_close, from_handle, uri.c_str(), NULL,
433 XML_PARSE_RECOVER);
434 if (doc == NULL) {
435 sp_ui_error_dialog(_("Server supplied malformed Clip Art feed"));
436 g_warning("Failed to parse %s\n", uri.c_str());
437 return;
438 }
440 // get the root element node
441 root_element = xmlDocGetRootElement(doc);
443 // clear the fileslist
444 filesList->clear_items();
445 filesList->set_sensitive(false);
447 // print all xml the element names
448 print_xml_element_names(root_element);
450 if (filesList->size() == 0)
451 {
452 notFoundLabel->show();
453 filesList->set_sensitive(false);
454 }
455 else
456 filesList->set_sensitive(true);
458 // free the document
459 xmlFreeDoc(doc);
460 // free the global variables that may have been allocated by the parser
461 xmlCleanupParser();
462 return;
463 #endif
464 }
466 /**
467 * Prints the names of the all the xml elements
468 * that are siblings or children of a given xml node
469 */
470 void FileImportFromOCALDialog::print_xml_element_names(xmlNode * a_node)
471 {
472 xmlNode *cur_node = NULL;
473 guint row_num = 0;
474 for (cur_node = a_node; cur_node; cur_node = cur_node->next) {
475 // get itens information
476 if (strcmp((const char*)cur_node->name, "rss")) //avoid the root
477 if (cur_node->type == XML_ELEMENT_NODE && !strcmp((const char*)cur_node->parent->name, "item"))
478 {
479 if (!strcmp((const char*)cur_node->name, "title"))
480 {
481 xmlChar *title = xmlNodeGetContent(cur_node);
482 row_num = filesList->append_text((const char*)title);
483 xmlFree(title);
484 }
485 #ifdef WITH_GNOME_VFS
486 else if (!strcmp((const char*)cur_node->name, "enclosure"))
487 {
488 xmlChar *urlattribute = xmlGetProp(cur_node, (xmlChar*)"url");
489 filesList->set_text(row_num, 1, (const char*)urlattribute);
490 gchar *tmp_file;
491 tmp_file = gnome_vfs_uri_extract_short_path_name(gnome_vfs_uri_new((const char*)urlattribute));
492 filesList->set_text(row_num, 2, (const char*)tmp_file);
493 xmlFree(urlattribute);
494 }
495 else if (!strcmp((const char*)cur_node->name, "creator"))
496 {
497 filesList->set_text(row_num, 3, (const char*)xmlNodeGetContent(cur_node));
498 }
499 else if (!strcmp((const char*)cur_node->name, "description"))
500 {
501 filesList->set_text(row_num, 4, (const char*)xmlNodeGetContent(cur_node));
502 }
503 #endif
504 }
505 print_xml_element_names(cur_node->children);
506 }
507 }
509 /**
510 * Constructor. Not called directly. Use the factory.
511 */
512 FileImportFromOCALDialog::FileImportFromOCALDialog(Gtk::Window& parentWindow,
513 const Glib::ustring &dir,
514 FileDialogType fileTypes,
515 const Glib::ustring &title) :
516 FileDialogOCALBase(title, parentWindow)
517 {
518 // Initalize to Autodetect
519 extension = NULL;
520 // No filename to start out with
521 Glib::ustring searchTag = "";
523 dialogType = fileTypes;
524 Gtk::VBox *vbox = get_vbox();
525 Gtk::Label *tagLabel = new Gtk::Label(_("Search Tag"));
526 notFoundLabel = new Gtk::Label(_("No files matched your search"));
527 descriptionLabel = new Gtk::Label();
528 descriptionLabel->set_max_width_chars(60);
529 descriptionLabel->set_single_line_mode(false);
530 messageBox.pack_start(*notFoundLabel);
531 descriptionBox.pack_start(*descriptionLabel);
532 searchTagEntry = new Gtk::Entry();
533 searchTagEntry->set_text(searchTag);
534 searchTagEntry->set_max_length(255);
535 searchButton = new Gtk::Button(_("Search"));
536 tagBox.pack_start(*tagLabel);
537 tagBox.pack_start(*searchTagEntry, Gtk::PACK_EXPAND_WIDGET, 3);
538 tagBox.pack_start(*searchButton);
539 filesPreview = new SVGPreview();
540 filesPreview->showNoPreview();
541 // add the buttons in the bottom of the dialog
542 add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
543 okButton = add_button(Gtk::Stock::OPEN, Gtk::RESPONSE_OK);
544 // sets the okbutton to default
545 set_default(*okButton);
546 filesList = new FileListViewText(5, *filesPreview, *descriptionLabel, *okButton);
547 filesList->set_sensitive(false);
548 // add the listview inside a ScrolledWindow
549 listScrolledWindow.add(*filesList);
550 // only show the scrollbars when they are necessary:
551 listScrolledWindow.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
552 filesList->set_column_title(0, _("Files Found"));
553 listScrolledWindow.set_size_request(400, 180);
554 filesList->get_column(1)->set_visible(false); // file url
555 filesList->get_column(2)->set_visible(false); // tmp file path
556 filesList->get_column(3)->set_visible(false); // author dir
557 filesList->get_column(4)->set_visible(false); // file description
558 filesBox.pack_start(listScrolledWindow);
559 filesBox.pack_start(*filesPreview);
560 vbox->pack_start(tagBox);
561 vbox->pack_start(messageBox);
562 vbox->pack_start(filesBox);
563 vbox->pack_start(descriptionBox);
565 //Let's do some customization
566 searchTagEntry = NULL;
567 Gtk::Container *cont = get_toplevel();
568 std::vector<Gtk::Entry *> entries;
569 findEntryWidgets(cont, entries);
570 if (entries.size() >=1 )
571 {
572 //Catch when user hits [return] on the text field
573 searchTagEntry = entries[0];
574 searchTagEntry->signal_activate().connect(
575 sigc::mem_fun(*this, &FileImportFromOCALDialog::searchTagEntryChangedCallback));
576 }
578 searchButton->signal_clicked().connect(
579 sigc::mem_fun(*this, &FileImportFromOCALDialog::searchTagEntryChangedCallback));
581 show_all_children();
582 notFoundLabel->hide();
583 }
585 /**
586 * Destructor
587 */
588 FileImportFromOCALDialog::~FileImportFromOCALDialog()
589 {
591 }
593 /**
594 * Show this dialog modally. Return true if user hits [OK]
595 */
596 bool
597 FileImportFromOCALDialog::show()
598 {
599 set_modal (TRUE); //Window
600 sp_transientize((GtkWidget *)gobj()); //Make transient
601 gint b = run(); //Dialog
602 hide();
604 if (b == Gtk::RESPONSE_OK)
605 {
606 return TRUE;
607 }
608 else
609 {
610 return FALSE;
611 }
612 }
615 /**
616 * Get the file extension type that was selected by the user. Valid after an [OK]
617 */
618 Inkscape::Extension::Extension *
619 FileImportFromOCALDialog::getSelectionType()
620 {
621 return extension;
622 }
625 /**
626 * Get the file name chosen by the user. Valid after an [OK]
627 */
628 Glib::ustring
629 FileImportFromOCALDialog::getFilename (void)
630 {
631 return filesList->getFilename();
632 }
635 } //namespace Dialog
636 } //namespace UI
637 } //namespace Inkscape
641 /*
642 Local Variables:
643 mode:c++
644 c-file-style:"stroustrup"
645 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
646 indent-tabs-mode:nil
647 fill-column:99
648 End:
649 */
650 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :