diff --git a/src/ui/dialog/export.cpp b/src/ui/dialog/export.cpp index 2b2fcc82a52dff81cd10af2170d5f285584786fc..bb6102cb9a92e2c45a3c04be43ae68c4164e82f3 100644 --- a/src/ui/dialog/export.cpp +++ b/src/ui/dialog/export.cpp @@ -578,6 +578,23 @@ Glib::RefPtr Export::createSpinbutton( gchar const * /*key*/, } // end of createSpinbutton() +//moved up because there was no declaration in header file. Should we add? +static std::string absolutize_path_from_document_location(SPDocument *doc, const std::string &filename) +{ + std::string path; + //Make relative paths go from the document location, if possible: + if (!Glib::path_is_absolute(filename) && doc->getDocumentURI()) { + auto dirname = Glib::path_get_dirname(doc->getDocumentURI()); + if (!dirname.empty()) { + path = Glib::build_filename(dirname, filename); + } + } + if (path.empty()) { + path = filename; + } + return path; +} + std::string create_filepath_from_id(Glib::ustring id, const Glib::ustring &file_entry_text) { if (id.empty()) @@ -606,6 +623,39 @@ std::string create_filepath_from_id(Glib::ustring id, const Glib::ustring &file_ return Glib::build_filename(directory, Glib::filename_from_utf8(id) + ".png"); } +std::string +create_filepath_from_id_with_suffix(gchar const* id, const Glib::ustring &file_entry_text,const gchar *filename,SPDocument *doc) +{ + + std::string path = ""; + std::string temp_id; + std::string temp_filename; + for(int i=0;i<100;i++) + { + if (!filename) { + if (!id) { + g_warning("object has no id"); + path = ""; + return path; + } + temp_id = id; + path = create_filepath_from_id(temp_id + "_" + std::to_string(i), file_entry_text); + } else { + temp_filename = filename; + path = absolutize_path_from_document_location(doc, temp_filename + "_" + std::to_string(i)); + } + + if (!Inkscape::IO::file_test(path.c_str(), G_FILE_TEST_EXISTS)) + { + return path; + } + } + + //if not found return empty path + path = ""; + return path; +} + void Export::onBatchClicked () { if (batch_export.get_active()) { @@ -959,22 +1009,6 @@ Export::create_progress_dialog(Glib::ustring progress_text) return dlg; } -static std::string absolutize_path_from_document_location(SPDocument *doc, const std::string &filename) -{ - std::string path; - //Make relative paths go from the document location, if possible: - if (!Glib::path_is_absolute(filename) && doc->getDocumentURI()) { - auto dirname = Glib::path_get_dirname(doc->getDocumentURI()); - if (!dirname.empty()) { - path = Glib::build_filename(dirname, filename); - } - } - if (path.empty()) { - path = filename; - } - return path; -} - // Called when unit is changed void Export::onUnitChanged() { @@ -1028,6 +1062,42 @@ void Export::onExport () gint export_count = 0; auto itemlist= desktop->getSelection()->items(); + + //Get set of items which have existing filename + std::set conflicting_paths; + for(auto i = itemlist.begin();i!=itemlist.end() && !interrupted ;++i) + { + SPItem *item = *i; + std::string path; + const gchar *filename = item->getRepr()->attribute("inkscape:export-filename"); + if (!filename) + { + auto id = item->getId(); + if (!id) + { + g_warning("object has no id"); + continue; + } + path = create_filepath_from_id(id, filename_entry.get_text()); + } + else + { + path = absolutize_path_from_document_location(doc, filename); + } + if (Inkscape::IO::file_test(path.c_str(), G_FILE_TEST_EXISTS)) + { + conflicting_paths.insert(path); + } + } + + //get a common suffix for all conflicting path + //should every conflicting file have its own suffix? + auto conflict_response = sp_ui_overwrite_file_batch(conflicting_paths); + + //If export is cancelled by user set interrupt + if(conflict_response == -1) + interrupted = true; + for(auto i = itemlist.begin();i!=itemlist.end() && !interrupted ;++i){ SPItem *item = *i; @@ -1049,6 +1119,10 @@ void Export::onExport () path = absolutize_path_from_document_location(doc, filename); } + //if skip is choosed + if(conflicting_paths.count(path) && conflict_response == 0) + continue; + // retrieve export dpi hints const gchar *dpi_hint = item->getRepr()->attribute("inkscape:export-xdpi"); // only xdpi, ydpi is always the same now gdouble dpi = 0.0; @@ -1074,6 +1148,16 @@ void Export::onExport () _("Exporting file %s..."), safeFile), desktop); std::vector x; std::vector selected(desktop->getSelection()->items().begin(), desktop->getSelection()->items().end()); + + //if response was add suffix + if(conflicting_paths.count(path) && conflict_response == 2) + { + path = create_filepath_from_id_with_suffix(item->getId(), filename_entry.get_text(),filename,doc); + if(path.empty()) + continue; + } + + if (!sp_export_png_file (doc, path.c_str(), *area, width, height, pHYs, pHYs, nv->pagecolor, diff --git a/src/ui/dialog/export.h b/src/ui/dialog/export.h index 0e138243a94366eb49dbbd05a86b92359fa051a4..e573172cd189059594163621b3ea8f5d91fdefc9 100644 --- a/src/ui/dialog/export.h +++ b/src/ui/dialog/export.h @@ -24,6 +24,9 @@ #include "ui/dialog/dialog-base.h" #include "ui/widget/scrollprotected.h" +//For set<> in batch export +#include + namespace Inkscape { namespace UI { namespace Dialog { diff --git a/src/ui/interface.cpp b/src/ui/interface.cpp index 089327c7ac2ac1a0dc7ad67ae2db69cf89e65c8d..68bf3ab0cdde721c0c5bba86d29d82594c143dfc 100644 --- a/src/ui/interface.cpp +++ b/src/ui/interface.cpp @@ -221,6 +221,43 @@ sp_ui_overwrite_file(gchar const *filename) return return_value; } +int +sp_ui_overwrite_file_batch(std::set& conflicting_paths) +{ + //0 means no conflicting paths hence take no action. + if(conflicting_paths.size()<1) + return 1; + + //get space separated list of paths for printing in dialog + Glib::ustring sec_message = "These files already exists:\n"; + for(auto& path:conflicting_paths) + sec_message = sec_message + "- " + path + "\n"; + sec_message = sec_message + "You can also skip these files or make a copy with suitable suffix"; + + //dialog creation. reference taken from sp_ui_overwrite_file + Gtk::Window *window = SP_ACTIVE_DESKTOP->getToplevel(); + + Gtk::MessageDialog *dialog = new Gtk::MessageDialog(*window, + "", + true, + Gtk::MESSAGE_WARNING, + Gtk::BUTTONS_NONE, + true + ); + + dialog->set_message("Files already exists. Do you want to overwrite them?"); + dialog->set_secondary_text(sec_message); + dialog->add_button(_("_Cancel"), -1); + dialog->add_button(_("Skip"), 0); + dialog->add_button(_("Overwrite"), 1); + dialog->add_button(_("Add Suffix"), 2); + dialog->set_default_response(1); + auto response = dialog->run(); + + delete dialog; + return response; +} + /* Local Variables: mode:c++ diff --git a/src/ui/interface.h b/src/ui/interface.h index 4fa80dab13eda80e7d966b56ac4074f7fcdb3a84..14fa2723e1fbf066d7db28a41fae249bbf11db85 100644 --- a/src/ui/interface.h +++ b/src/ui/interface.h @@ -20,6 +20,10 @@ #include +//Added for sp_ui_overwrite_file_batch +#include +#include + typedef struct _GtkWidget GtkWidget; namespace Inkscape { @@ -59,6 +63,7 @@ Glib::ustring getLayoutPrefPath( Inkscape::UI::View::View *view ); */ void sp_ui_error_dialog (char const* message); bool sp_ui_overwrite_file (char const* filename); +int sp_ui_overwrite_file_batch(std::set &conflicting_paths); #endif // SEEN_SP_INTERFACE_H