Fixed a regression, that "export as" ran into a segmentation fault

Fixes #2386
master
Febbe 5 years ago committed by Fabian Keßler
parent 6de8e9a066
commit a63639003f
  1. 23
      src/control/jobs/BaseExportJob.cpp
  2. 4
      src/control/jobs/BaseExportJob.h
  3. 13
      src/control/jobs/CustomExportJob.cpp
  4. 2
      src/control/jobs/CustomExportJob.h
  5. 4
      src/control/jobs/PdfExportJob.cpp
  6. 2
      src/control/jobs/PdfExportJob.h

@ -41,7 +41,7 @@ auto BaseExportJob::checkOverwriteBackgroundPDF(fs::path const& file) const -> b
return true;
}
auto BaseExportJob::getFilterName() -> string {
auto BaseExportJob::getFilterName() const -> string {
GtkFileFilter* filter = gtk_file_chooser_get_filter(GTK_FILE_CHOOSER(dialog));
return gtk_file_filter_get_name(filter);
}
@ -68,10 +68,10 @@ auto BaseExportJob::showFilechooser() -> bool {
gtk_widget_destroy(dialog);
return false;
}
this->filepath = Util::fromGFilename(gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog)));
Util::clearExtensions(this->filepath);
auto file = Util::fromGFilename(gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog)));
Util::clearExtensions(file);
// Since we add the extension after the OK button, we have to check manually on existing files
if (control->askToReplace(filepath)) {
if (testAndSetFilepath(std::move(file)) && control->askToReplace(this->filepath)) {
break;
}
}
@ -83,14 +83,17 @@ auto BaseExportJob::showFilechooser() -> bool {
return true;
}
auto BaseExportJob::isUriValid(string& uri) -> bool {
if (!StringUtils::startsWith(uri, "file://")) {
string msg = FS(_F("Only local files are supported\nPath: {1}") % uri);
auto BaseExportJob::testAndSetFilepath(fs::path file) -> bool {
try {
if(fs::is_directory(file.parent_path())){
this->filepath = std::move(file);
return true;
}
} catch (fs::filesystem_error const& e) {
string msg = FS(_F("Failed to resolve path with the following error:\n{1}") % e.what());
XojMsgBox::showErrorToUser(control->getGtkWindow(), msg);
return false;
}
return true;
return false;
}
void BaseExportJob::afterRun() {

@ -33,14 +33,14 @@ public:
public:
virtual bool showFilechooser();
string getFilterName();
string getFilterName() const;
protected:
void initDialog();
virtual void addFilterToDialog() = 0;
void addFileFilterToDialog(const string& name, const string& pattern);
bool checkOverwriteBackgroundPDF(fs::path const& file) const;
virtual bool isUriValid(string& uri);
virtual bool testAndSetFilepath(fs::path file);
private:
protected:

@ -45,17 +45,18 @@ void CustomExportJob::addFilterToDialog() {
}
}
auto CustomExportJob::isUriValid(string& uri) -> bool {
if (!BaseExportJob::isUriValid(uri)) {
auto CustomExportJob::testAndSetFilepath(fs::path file) -> bool {
if (!BaseExportJob::testAndSetFilepath(std::move(file))) {
return false;
}
// Extract the file filter selected
this->chosenFilterName = BaseExportJob::getFilterName();
auto chosenFilter = filters.at(this->chosenFilterName);
// Remove any pre-existing extension and adds the chosen one
Util::clearExtensions(filepath, filters[this->chosenFilterName]->extension);
filepath += filters[this->chosenFilterName]->extension;
Util::clearExtensions(filepath, chosenFilter->extension);
filepath += chosenFilter->extension;
return checkOverwriteBackgroundPDF(filepath);
}
@ -105,12 +106,10 @@ auto CustomExportJob::showFilechooser() -> bool {
* Create one Graphics file per page
*/
void CustomExportJob::exportGraphics() {
bool hideBackground = filters[this->chosenFilterName]->withoutBackground;
bool hideBackground = filters.at(this->chosenFilterName)->withoutBackground;
ImageExport imgExport(control->getDocument(), filepath, format, hideBackground, exportRange);
imgExport.setPngDpi(pngDpi);
imgExport.exportGraphics(control);
errorMsg = imgExport.getLastErrorMsg();
}

@ -44,7 +44,7 @@ protected:
*/
void exportGraphics();
virtual bool isUriValid(string& uri);
bool testAndSetFilepath(fs::path file) override;
private:
/**

@ -12,8 +12,8 @@ PdfExportJob::~PdfExportJob() = default;
void PdfExportJob::addFilterToDialog() { addFileFilterToDialog(_("PDF files"), "*.pdf"); }
auto PdfExportJob::isUriValid(string& uri) -> bool {
if (!BaseExportJob::isUriValid(uri)) {
auto PdfExportJob::testAndSetFilepath(fs::path file) -> bool {
if (!BaseExportJob::testAndSetFilepath(std::move(file))) {
return false;
}

@ -25,7 +25,7 @@ public:
protected:
virtual void addFilterToDialog();
virtual bool isUriValid(string& uri);
bool testAndSetFilepath(fs::path file) override;
private:
};

Loading…
Cancel
Save