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