Skip to content

Commit

Permalink
Added user input validation for default filenames
Browse files Browse the repository at this point in the history
 - fixes #5123 partially
  • Loading branch information
Febbe committed Oct 4, 2023
1 parent d8b9ea5 commit 79f7c8c
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 25 deletions.
12 changes: 10 additions & 2 deletions src/core/control/Control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <cstdlib> // for size_t
#include <exception> // for exce...
#include <iterator> // for end
#include <locale>
#include <memory> // for make...
#include <numeric> // for accu...
#include <optional> // for opti...
Expand Down Expand Up @@ -2876,11 +2877,18 @@ void Control::closeDocument() {
}

void Control::applyPreferredLanguage() {
auto const& lang = this->settings->getPreferredLocale();
#ifdef _WIN32
_putenv_s("LANGUAGE", this->settings->getPreferredLocale().c_str());
_putenv_s("LANGUAGE", lang.c_str());
#else
setenv("LANGUAGE", this->settings->getPreferredLocale().c_str(), 1);
setenv("LANGUAGE", lang.c_str(), 1);
#endif
try {
auto glob = std::locale{std::locale(".UTF-8"), lang, std::locale::all};
std::locale::global(glob);
} catch (std::runtime_error const& e) {
g_warning("Failed to set locale %s: %s", lang.c_str(), e.what());
}
}

void Control::initButtonTool() {
Expand Down
2 changes: 1 addition & 1 deletion src/core/control/XournalMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ void initLocalisation() {

// Not working on GNU g++(mingww) forWindows! Only working on Linux/macOS and with msvc
try {
std::locale::global(std::locale("")); // "" - system default locale
std::locale::global(std::locale(".UTF-8")); // "" - system default locale
} catch (const std::runtime_error& e) {
g_warning("XournalMain: System default locale could not be set.\n - Caused by: %s\n - Note that it is not "
"supported to set the locale using mingw-w64 on windows.\n - This could be solved by compiling "
Expand Down
18 changes: 10 additions & 8 deletions src/core/gui/dialog/LanguageConfigGui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,18 @@ LanguageConfigGui::LanguageConfigGui(GladeSearchpath* gladeSearchPath, GtkWidget


// Set the current locale if previously selected
auto prefPos = availableLocales.begin();
if (auto preferred = settings->getPreferredLocale(); !preferred.empty()) {
prefPos = std::find(availableLocales.begin(), availableLocales.end(), preferred);
if (*prefPos != preferred) {
auto prefPos = [&]() {
if (auto preferred = settings->getPreferredLocale(); !preferred.empty()) {
if (auto &&endi = availableLocales.end(), prefPos = std::find(availableLocales.begin(), endi, preferred);
prefPos != endi) {
return prefPos;
}
XojMsgBox::showErrorToUser(nullptr, _("Previously selected language not available anymore!"));

// Use system default
prefPos = availableLocales.begin();
}
}
// Use system default
return availableLocales.begin();
}();

gtk_combo_box_set_active(GTK_COMBO_BOX(dropdown), prefPos - availableLocales.begin());
}

Expand Down
22 changes: 16 additions & 6 deletions src/core/model/Document.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#include "Document.h"

#include <array>
#include <ctime> // for size_t, localtime, strf...
#include <iomanip>
#include <sstream>
#include <string> // for string
#include <utility> // for move, pair

Expand Down Expand Up @@ -131,6 +134,7 @@ auto Document::createSaveFolder(fs::path lastSavePath) -> fs::path {
}

auto Document::createSaveFilename(DocumentType type, const std::string& defaultSaveName, const std::string& defaultPdfName) -> fs::path {
constexpr static std::string_view forbiddenChars = {"\\/:*?\"<>|"};
std::string wildcardString;
if (type != Document::PDF) {
if (!filepath.empty()) {
Expand All @@ -152,17 +156,23 @@ auto Document::createSaveFilename(DocumentType type, const std::string& defaultS

const char* format = wildcardString.empty() ? defaultSaveName.c_str() : wildcardString.c_str();

// Todo (cpp20): use <format>
std::ostringstream ss;
ss.imbue(std::locale());
time_t curtime = time(nullptr);
char stime[128];
strftime(stime, sizeof(stime), format, localtime(&curtime));

// Remove the extension, file format is handled by the filter combo box
fs::path p = stime;
ss << std::put_time(localtime(&curtime), format);
auto filename = ss.str();
// Todo (cpp20): use <ranges>
for (auto& c: filename) {
if ((c < 32 && c > 0) || c == 127 || forbiddenChars.find(c) != std::string::npos) {
c = '_';
}
}
fs::path p = filename;
Util::clearExtensions(p);
return p;
}


auto Document::getPreview() const -> cairo_surface_t* { return this->preview; }

void Document::setPreview(cairo_surface_t* preview) {
Expand Down
18 changes: 10 additions & 8 deletions ui/settings.glade
Original file line number Diff line number Diff line change
Expand Up @@ -572,19 +572,20 @@ If the file was not yet saved you can find it in ~/.cache/xournalpp/autosaves&lt
%H Hour in 24h format (00-23)
%I Hour in 12h format (01-12)
%j Day of the year (001-366)
%m Month as a decimal number (01-12)
%M Minute (00-59)
%m Month as a decimal number (01-12)
%M Minute (00-59)
%p AM or PM designation (e.g. PM)
%S Second (00-61)
%U Week number with the first Sunday as the first day of week one (00-53)
%w Weekday as a decimal number with Sunday as 0 (0-6)
%W Week number with the first Monday as the first day of week one (00-53)
%W Week number with the first Monday as the first day of week one (00-53)
%x Date representation (e.g. 08/23/01)
%X Time representation (e.g. 14:55:02)
%y Year, last two digits (00-99)
%Y Year (e.g. 2001)
%Z Timezone name or abbreviation (e.g. CDT)
%% A % sign</property>
%% %
Illegal characters are replaced with "_"</property>
<property name="xalign">0</property>
</object>
</child>
Expand Down Expand Up @@ -743,19 +744,20 @@ If the file was not yet saved you can find it in ~/.cache/xournalpp/autosaves&lt
%H Hour in 24h format (00-23)
%I Hour in 12h format (01-12)
%j Day of the year (001-366)
%m Month as a decimal number (01-12)
%M Minute (00-59)
%m Month as a decimal number (01-12)
%M Minute (00-59)
%p AM or PM designation (e.g. PM)
%S Second (00-61)
%U Week number with the first Sunday as the first day of week one (00-53)
%w Weekday as a decimal number with Sunday as 0 (0-6)
%W Week number with the first Monday as the first day of week one (00-53)
%W Week number with the first Monday as the first day of week one (00-53)
%x Date representation (e.g. 08/23/01)
%X Time representation (e.g. 14:55:02 or 2:55:02 PM)
%y Year, last two digits (00-99)
%Y Year (e.g. 2001)
%Z Timezone name or abbreviation (e.g. CDT)
%% A % sign</property>
%% %
Illegal characters are replaced with "_"</property>
<property name="xalign">0</property>
</object>
</child>
Expand Down

0 comments on commit 79f7c8c

Please sign in to comment.