Skip to content

Commit

Permalink
Use MIME type for export FileChooser filters
Browse files Browse the repository at this point in the history
  • Loading branch information
bhennion committed Feb 24, 2024
1 parent 24e2e1b commit e032aa4
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/core/control/Control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1713,7 +1713,7 @@ auto Control::showSaveDialog() -> bool {

GtkFileFilter* filterXoj = gtk_file_filter_new();
gtk_file_filter_set_name(filterXoj, _("Xournal++ files"));
gtk_file_filter_add_pattern(filterXoj, "*.xopp");
gtk_file_filter_add_mime_type(filterXoj, "application/x-xopp");
gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filterXoj);

this->doc->lock();
Expand Down
4 changes: 2 additions & 2 deletions src/core/control/jobs/BaseExportJob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ static GtkWidget* initDialog(GtkWindow* win) {
GTK_RESPONSE_CANCEL, _("_Export"), GTK_RESPONSE_OK, nullptr);
}

void BaseExportJob::addFileFilterToDialog(GtkFileChooser* dialog, const std::string& name, const std::string& pattern) {
void BaseExportJob::addFileFilterToDialog(GtkFileChooser* dialog, const std::string& name, const std::string& mime) {
GtkFileFilter* filter = gtk_file_filter_new();
gtk_file_filter_set_name(filter, name.c_str());
gtk_file_filter_add_pattern(filter, pattern.c_str());
gtk_file_filter_add_mime_type(filter, mime.c_str());
gtk_file_chooser_add_filter(dialog, filter);
}

Expand Down
5 changes: 3 additions & 2 deletions src/core/control/jobs/BaseExportJob.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class BaseExportJob: public BlockingJob {

protected:
virtual void addFilterToDialog(GtkFileChooser* dialog) = 0;
static void addFileFilterToDialog(GtkFileChooser* dialog, const std::string& name, const std::string& pattern);
static void addFileFilterToDialog(GtkFileChooser* dialog, const std::string& name, const std::string& mimetype);
bool checkOverwriteBackgroundPDF(fs::path const& file) const;
virtual bool testAndSetFilepath(const fs::path& file, const char* filterName = nullptr);

Expand All @@ -59,7 +59,8 @@ class BaseExportJob: public BlockingJob {
class ExportType {
public:
std::string extension;
std::string mimeType;

ExportType(std::string ext): extension(std::move(ext)) {}
ExportType(std::string ext, std::string mime): extension(std::move(ext)), mimeType(std::move(mime)) {}
};
};
10 changes: 5 additions & 5 deletions src/core/control/jobs/CustomExportJob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,18 @@

CustomExportJob::CustomExportJob(Control* control): BaseExportJob(control, _("Custom Export")) {
// Supported filters
filters.insert({_("PDF files"), ExportType(".pdf")});
filters.insert({_("PNG graphics"), ExportType(".png")});
filters.insert({_("SVG graphics"), ExportType(".svg")});
filters.insert({_("Xournal (Compatibility)"), ExportType(".xoj")});
filters.insert({_("PDF files"), ExportType(".pdf", "application/pdf")});
filters.insert({_("PNG graphics"), ExportType(".png", "image/png")});
filters.insert({_("SVG graphics"), ExportType(".svg", "image/svg+xml")});
filters.insert({_("Xournal (Compatibility)"), ExportType(".xoj", "application/x-xojpp")});
}

CustomExportJob::~CustomExportJob() = default;

void CustomExportJob::addFilterToDialog(GtkFileChooser* dialog) {
// Runs on every filter inside the filters map
for (auto& filter: filters) {
addFileFilterToDialog(dialog, filter.first, "*" + filter.second.extension); // Adds * for the pattern
addFileFilterToDialog(dialog, filter.first, filter.second.mimeType);
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/core/control/jobs/PdfExportJob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ PdfExportJob::PdfExportJob(Control* control): BaseExportJob(control, _("PDF Expo

PdfExportJob::~PdfExportJob() = default;

void PdfExportJob::addFilterToDialog(GtkFileChooser* dialog) { addFileFilterToDialog(dialog, _("PDF files"), "*.pdf"); }
void PdfExportJob::addFilterToDialog(GtkFileChooser* dialog) {
addFileFilterToDialog(dialog, _("PDF files"), "application/pdf");
}

auto PdfExportJob::testAndSetFilepath(const fs::path& file, const char* /*filterName*/) -> bool {
if (!BaseExportJob::testAndSetFilepath(file)) {
Expand Down

0 comments on commit e032aa4

Please sign in to comment.