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 * Callback for user input into searchTagEntry
363 */
364 void FileImportFromOCALDialog::searchTagEntryChangedCallback()
365 {
366 if (!searchTagEntry)
367 return;
369 notFoundLabel->hide();
370 descriptionLabel->set_text("");
372 Glib::ustring searchTag = searchTagEntry->get_text();
373 // create the ocal uri to get rss feed
374 Glib::ustring uri = "http://";
375 uri.append(prefs_get_string_attribute("options.ocalurl", "str"));
376 uri.append("/media/feed/rss/");
377 uri.append(searchTag);
378 if (!Glib::get_charset()) //If we are not utf8
379 uri = Glib::filename_to_utf8(uri);
381 #ifdef WITH_GNOME_VFS
383 // get the rss feed
384 gnome_vfs_init();
385 GnomeVFSHandle *from_handle = NULL;
386 GnomeVFSHandle *to_handle = NULL;
387 GnomeVFSFileSize bytes_read;
388 GnomeVFSFileSize bytes_written;
389 GnomeVFSResult result;
390 guint8 buffer[8192];
392 // create the temp file name
393 Glib::ustring fileName = Glib::get_tmp_dir ();
394 fileName.append(G_DIR_SEPARATOR_S);
395 fileName.append("ocalfeed.xml");
397 // open the temp file to receive
398 result = gnome_vfs_open (&to_handle, fileName.c_str(), GNOME_VFS_OPEN_WRITE);
399 if (result == GNOME_VFS_ERROR_NOT_FOUND){
400 result = gnome_vfs_create (&to_handle, fileName.c_str(), GNOME_VFS_OPEN_WRITE, FALSE, GNOME_VFS_PERM_USER_ALL);
401 }
402 if (result != GNOME_VFS_OK) {
403 g_warning("Error creating temp file: %s", gnome_vfs_result_to_string(result));
404 return;
405 }
407 // open the rss feed
408 result = gnome_vfs_open (&from_handle, uri.c_str(), GNOME_VFS_OPEN_READ);
409 if (result != GNOME_VFS_OK) {
410 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)"));
411 return;
412 }
414 // copy the file
415 while (1) {
417 result = gnome_vfs_read (from_handle, buffer, 8192, &bytes_read);
419 if ((result == GNOME_VFS_ERROR_EOF) &&(!bytes_read)){
420 result = gnome_vfs_close (from_handle);
421 result = gnome_vfs_close (to_handle);
422 break;
423 }
425 if (result != GNOME_VFS_OK) {
426 g_warning("%s", gnome_vfs_result_to_string(result));
427 return;
428 }
429 result = gnome_vfs_write (to_handle, buffer, bytes_read, &bytes_written);
430 if (result != GNOME_VFS_OK) {
431 g_warning("%s", gnome_vfs_result_to_string(result));
432 return;
433 }
435 if (bytes_read != bytes_written){
436 g_warning("Bytes read not equal to bytes written");
437 return;
438 }
440 }
442 // create the resulting xml document tree
443 // this initialize the library and test mistakes between compiled and shared library used
444 LIBXML_TEST_VERSION
445 xmlDoc *doc = NULL;
446 xmlNode *root_element = NULL;
447 doc = xmlReadFile(fileName.c_str(), NULL, 0);
448 if (doc == NULL) {
449 g_warning("Failed to parse %s\n", fileName.c_str());
450 return;
451 }
453 // get the root element node
454 root_element = xmlDocGetRootElement(doc);
456 // clear the fileslist
457 filesList->clear_items();
458 filesList->set_sensitive(false);
460 // print all xml the element names
461 print_xml_element_names(root_element);
463 if (filesList->size() == 0)
464 {
465 notFoundLabel->show();
466 filesList->set_sensitive(false);
467 }
468 else
469 filesList->set_sensitive(true);
471 // free the document
472 xmlFreeDoc(doc);
473 // free the global variables that may have been allocated by the parser
474 xmlCleanupParser();
475 return;
476 #endif
477 }
479 /**
480 * Prints the names of the all the xml elements
481 * that are siblings or children of a given xml node
482 */
483 void FileImportFromOCALDialog::print_xml_element_names(xmlNode * a_node)
484 {
485 xmlNode *cur_node = NULL;
486 guint row_num = 0;
487 for (cur_node = a_node; cur_node; cur_node = cur_node->next) {
488 // get itens information
489 if (strcmp((const char*)cur_node->name, "rss")) //avoid the root
490 if (cur_node->type == XML_ELEMENT_NODE && !strcmp((const char*)cur_node->parent->name, "item"))
491 {
492 if (!strcmp((const char*)cur_node->name, "title"))
493 {
494 xmlChar *title = xmlNodeGetContent(cur_node);
495 row_num = filesList->append_text((const char*)title);
496 xmlFree(title);
497 }
498 #ifdef WITH_GNOME_VFS
499 else if (!strcmp((const char*)cur_node->name, "enclosure"))
500 {
501 xmlChar *urlattribute = xmlGetProp(cur_node, (xmlChar*)"url");
502 filesList->set_text(row_num, 1, (const char*)urlattribute);
503 gchar *tmp_file;
504 tmp_file = gnome_vfs_uri_extract_short_path_name(gnome_vfs_uri_new((const char*)urlattribute));
505 filesList->set_text(row_num, 2, (const char*)tmp_file);
506 xmlFree(urlattribute);
507 }
508 else if (!strcmp((const char*)cur_node->name, "creator"))
509 {
510 filesList->set_text(row_num, 3, (const char*)xmlNodeGetContent(cur_node));
511 }
512 else if (!strcmp((const char*)cur_node->name, "description"))
513 {
514 filesList->set_text(row_num, 4, (const char*)xmlNodeGetContent(cur_node));
515 }
516 #endif
517 }
518 print_xml_element_names(cur_node->children);
519 }
520 }
522 /**
523 * Constructor. Not called directly. Use the factory.
524 */
525 FileImportFromOCALDialog::FileImportFromOCALDialog(Gtk::Window& parentWindow,
526 const Glib::ustring &dir,
527 FileDialogType fileTypes,
528 const Glib::ustring &title) :
529 FileDialogOCALBase(title, parentWindow)
530 {
531 // Initalize to Autodetect
532 extension = NULL;
533 // No filename to start out with
534 Glib::ustring searchTag = "";
536 dialogType = fileTypes;
537 Gtk::VBox *vbox = get_vbox();
538 Gtk::Label *tagLabel = new Gtk::Label(_("Search Tag"));
539 notFoundLabel = new Gtk::Label(_("No files matched your search"));
540 descriptionLabel = new Gtk::Label();
541 descriptionLabel->set_max_width_chars(60);
542 descriptionLabel->set_single_line_mode(false);
543 messageBox.pack_start(*notFoundLabel);
544 descriptionBox.pack_start(*descriptionLabel);
545 searchTagEntry = new Gtk::Entry();
546 searchTagEntry->set_text(searchTag);
547 searchTagEntry->set_max_length(255);
548 searchButton = new Gtk::Button(_("Search"));
549 tagBox.pack_start(*tagLabel);
550 tagBox.pack_start(*searchTagEntry, Gtk::PACK_EXPAND_WIDGET, 3);
551 tagBox.pack_start(*searchButton);
552 filesPreview = new SVGPreview();
553 filesPreview->showNoPreview();
554 // add the buttons in the bottom of the dialog
555 add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
556 okButton = add_button(Gtk::Stock::OPEN, Gtk::RESPONSE_OK);
557 // sets the okbutton to default
558 set_default(*okButton);
559 filesList = new FileListViewText(5, *filesPreview, *descriptionLabel, *okButton);
560 filesList->set_sensitive(false);
561 // add the listview inside a ScrolledWindow
562 listScrolledWindow.add(*filesList);
563 // only show the scrollbars when they are necessary:
564 listScrolledWindow.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
565 filesList->set_column_title(0, _("Files Found"));
566 listScrolledWindow.set_size_request(400, 180);
567 filesList->get_column(1)->set_visible(false); // file url
568 filesList->get_column(2)->set_visible(false); // tmp file path
569 filesList->get_column(3)->set_visible(false); // author dir
570 filesList->get_column(4)->set_visible(false); // file description
571 filesBox.pack_start(listScrolledWindow);
572 filesBox.pack_start(*filesPreview);
573 vbox->pack_start(tagBox);
574 vbox->pack_start(messageBox);
575 vbox->pack_start(filesBox);
576 vbox->pack_start(descriptionBox);
578 //Let's do some customization
579 searchTagEntry = NULL;
580 Gtk::Container *cont = get_toplevel();
581 std::vector<Gtk::Entry *> entries;
582 findEntryWidgets(cont, entries);
583 if (entries.size() >=1 )
584 {
585 //Catch when user hits [return] on the text field
586 searchTagEntry = entries[0];
587 searchTagEntry->signal_activate().connect(
588 sigc::mem_fun(*this, &FileImportFromOCALDialog::searchTagEntryChangedCallback));
589 }
591 searchButton->signal_clicked().connect(
592 sigc::mem_fun(*this, &FileImportFromOCALDialog::searchTagEntryChangedCallback));
594 show_all_children();
595 notFoundLabel->hide();
596 }
598 /**
599 * Destructor
600 */
601 FileImportFromOCALDialog::~FileImportFromOCALDialog()
602 {
604 }
606 /**
607 * Show this dialog modally. Return true if user hits [OK]
608 */
609 bool
610 FileImportFromOCALDialog::show()
611 {
612 set_modal (TRUE); //Window
613 sp_transientize((GtkWidget *)gobj()); //Make transient
614 gint b = run(); //Dialog
615 hide();
617 if (b == Gtk::RESPONSE_OK)
618 {
619 return TRUE;
620 }
621 else
622 {
623 return FALSE;
624 }
625 }
628 /**
629 * Get the file extension type that was selected by the user. Valid after an [OK]
630 */
631 Inkscape::Extension::Extension *
632 FileImportFromOCALDialog::getSelectionType()
633 {
634 return extension;
635 }
638 /**
639 * Get the file name chosen by the user. Valid after an [OK]
640 */
641 Glib::ustring
642 FileImportFromOCALDialog::getFilename (void)
643 {
644 return filesList->getFilename();
645 }
648 } //namespace Dialog
649 } //namespace UI
650 } //namespace Inkscape
654 /*
655 Local Variables:
656 mode:c++
657 c-file-style:"stroustrup"
658 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
659 indent-tabs-mode:nil
660 fill-column:99
661 End:
662 */
663 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :