Skip to content

Commit

Permalink
Better typed code
Browse files Browse the repository at this point in the history
  • Loading branch information
teusbenschop committed Dec 31, 2023
1 parent cb82aa7 commit 2a3e2ca
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 133 deletions.
27 changes: 6 additions & 21 deletions bootstrap/bootstrap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -402,18 +402,18 @@ void bootstrap_index (Webserver_Request& webserver_request)
return;
}

if ((url == edit_index_url ()) && browser_request_security_okay (webserver_request) && edit_index_acl (std::addressof(webserver_request))) {
webserver_request.reply = edit_index (std::addressof(webserver_request));
if ((url == edit_index_url ()) && browser_request_security_okay (webserver_request) && edit_index_acl (webserver_request)) {
webserver_request.reply = edit_index (webserver_request);
return;
}

if ((url == edit_position_url ()) && browser_request_security_okay (webserver_request) && edit_position_acl (std::addressof(webserver_request))) {
webserver_request.reply = edit_position (std::addressof(webserver_request));
if ((url == edit_position_url ()) && browser_request_security_okay (webserver_request) && edit_position_acl (webserver_request)) {
webserver_request.reply = edit_position (webserver_request);
return;
}

if ((url == edit_navigate_url ()) && browser_request_security_okay (webserver_request) && edit_navigate_acl (std::addressof(webserver_request))) {
webserver_request.reply = edit_navigate (std::addressof(webserver_request));
if ((url == edit_navigate_url ()) && browser_request_security_okay (webserver_request) && edit_navigate_acl (webserver_request)) {
webserver_request.reply = edit_navigate (webserver_request);
return;
}

Expand Down Expand Up @@ -1141,21 +1141,6 @@ void bootstrap_index (Webserver_Request& webserver_request)
return;
}

if ((url == edit_index_url ()) && browser_request_security_okay (webserver_request) && edit_index_acl (std::addressof(webserver_request))) {
webserver_request.reply = edit_index (std::addressof(webserver_request));
return;
}

if ((url == edit_position_url ()) && browser_request_security_okay (webserver_request) && edit_position_acl (std::addressof(webserver_request))) {
webserver_request.reply = edit_position (std::addressof(webserver_request));
return;
}

if ((url == edit_navigate_url ()) && browser_request_security_okay (webserver_request) && edit_navigate_acl (std::addressof(webserver_request))) {
webserver_request.reply = edit_navigate (std::addressof(webserver_request));
return;
}

if ((url == edit_preview_url ()) && browser_request_security_okay (webserver_request) && edit_preview_acl (std::addressof(webserver_request))) {
webserver_request.reply = edit_preview (std::addressof(webserver_request));
return;
Expand Down
99 changes: 48 additions & 51 deletions edit/index.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,61 +40,59 @@
#include <bb/logic.h>
#include <config/globals.h>
#include <workspace/logic.h>
using namespace std;


string edit_index_url ()
std::string edit_index_url ()
{
return "edit/index";
}


bool edit_index_acl (void * webserver_request)
bool edit_index_acl (Webserver_Request& webserver_request)
{
if (Filter_Roles::access_control (webserver_request, Filter_Roles::translator ())) return true;
auto [ read, write ] = access_bible::any (webserver_request);
if (Filter_Roles::access_control (std::addressof(webserver_request), Filter_Roles::translator ()))
return true;
auto [ read, write ] = access_bible::any (std::addressof(webserver_request));
return write;
}


string edit_index (void * webserver_request)
std::string edit_index (Webserver_Request& webserver_request)
{
Webserver_Request * request = static_cast<Webserver_Request *>(webserver_request);


bool touch = request->session_logic ()->touchEnabled ();
const bool touch = webserver_request.session_logic ()->touchEnabled ();


if (request->query.count ("switchbook") && request->query.count ("switchchapter")) {
int switchbook = filter::strings::convert_to_int (request->query ["switchbook"]);
int switchchapter = filter::strings::convert_to_int (request->query ["switchchapter"]);
if (webserver_request.query.count ("switchbook") && webserver_request.query.count ("switchchapter")) {
const int switchbook = filter::strings::convert_to_int (webserver_request.query ["switchbook"]);
const int switchchapter = filter::strings::convert_to_int (webserver_request.query ["switchchapter"]);
int switchverse = 1;
if (request->query.count ("switchverse")) switchverse = filter::strings::convert_to_int (request->query ["switchverse"]);
Ipc_Focus::set (request, switchbook, switchchapter, switchverse);
Navigation_Passage::record_history (request, switchbook, switchchapter, switchverse);
if (webserver_request.query.count ("switchverse"))
switchverse = filter::strings::convert_to_int (webserver_request.query ["switchverse"]);
Ipc_Focus::set (std::addressof(webserver_request), switchbook, switchchapter, switchverse);
Navigation_Passage::record_history (std::addressof(webserver_request), switchbook, switchchapter, switchverse);
}


// Set the user chosen Bible as the current Bible.
if (request->post.count ("bibleselect")) {
string bibleselect = request->post ["bibleselect"];
request->database_config_user ()->setBible (bibleselect);
if (webserver_request.post.count ("bibleselect")) {
const std::string bibleselect = webserver_request.post ["bibleselect"];
webserver_request.database_config_user ()->setBible (bibleselect);
// Going to another Bible, ensure that the focused book exists there.
int book = Ipc_Focus::getBook (request);
vector <int> books = request->database_bibles()->get_books (bibleselect);
int book = Ipc_Focus::getBook (std::addressof(webserver_request));
const std::vector <int> books = webserver_request.database_bibles()->get_books (bibleselect);
if (find (books.begin(), books.end(), book) == books.end()) {
if (!books.empty ()) book = books [0];
else book = 0;
Ipc_Focus::set (request, book, 1, 1);
Ipc_Focus::set (std::addressof(webserver_request), book, 1, 1);
}
return string();
return std::string();
}


string page;
std::string page{};


Assets_Header header = Assets_Header (translate("Edit"), request);
Assets_Header header = Assets_Header (translate("Edit"), std::addressof(webserver_request));
header.set_navigator ();
header.set_editor_stylesheet ();
if (touch) header.jquery_touch_on ();
Expand All @@ -103,17 +101,18 @@ string edit_index (void * webserver_request)
page = header.run ();


Assets_View view;
Assets_View view{};


// Active Bible, and check access.
// Or if the user have used query to preset the active Bible, get the preset Bible.
// Set the chosen Bible on the option HTML tag.
string bible = access_bible::clamp (request, request->database_config_user()->getBible ());
if (request->query.count ("bible")) bible = access_bible::clamp (request, request->query ["bible"]);
string bible_html;
vector <string> bibles = access_bible::bibles (request);
for (auto selectable_bible : bibles) {
std::string bible = access_bible::clamp (std::addressof(webserver_request), webserver_request.database_config_user()->getBible ());
if (webserver_request.query.count ("bible"))
bible = access_bible::clamp (std::addressof(webserver_request), webserver_request.query ["bible"]);
std::string bible_html{};
const std::vector <std::string> bibles = access_bible::bibles (std::addressof(webserver_request));
for (const auto& selectable_bible : bibles) {
bible_html = Options_To_Select::add_selection (selectable_bible, selectable_bible, bible_html);
}
view.set_variable ("bibleoptags", Options_To_Select::mark_selected (bible, bible_html));
Expand All @@ -127,7 +126,7 @@ string edit_index (void * webserver_request)
// Create the script.
// Quote the text to be sure it's a legal Javascript string.
// https://github.com/bibledit/cloud/issues/900
stringstream script_stream {};
std::stringstream script_stream {};
script_stream << "var editorChapterLoaded = " << quoted(locale_logic_text_loaded ()) << ";\n";
script_stream << "var editorChapterUpdating = " << quoted(locale_logic_text_updating ()) << ";\n";
script_stream << "var editorChapterUpdated = " << quoted(locale_logic_text_updated ()) << ";\n";
Expand All @@ -136,46 +135,44 @@ string edit_index (void * webserver_request)
script_stream << "var editorChapterSaved = " << quoted(locale_logic_text_saved ()) << ";\n";
script_stream << "var editorChapterRetrying = " << quoted(locale_logic_text_retrying ()) << ";\n";
script_stream << "var editorChapterVerseUpdatedLoaded = " << quoted(locale_logic_text_reload ()) << ";\n";
script_stream << "var verticalCaretPosition = " << request->database_config_user ()->getVerticalCaretPosition () << ";\n";
script_stream << "var verticalCaretPosition = " << webserver_request.database_config_user ()->getVerticalCaretPosition () << ";\n";
script_stream << "var verseSeparator = " << quoted(Database_Config_General::getNotesVerseSeparator ()) << ";\n";
string script = script_stream.str();
config::logic::swipe_enabled (webserver_request, script);
std::string script = script_stream.str();
config::logic::swipe_enabled (std::addressof(webserver_request), script);
view.set_variable ("script", script);


string clss = Filter_Css::getClass (bible);
string font = fonts::logic::get_text_font (bible);
int current_theme_index = request->database_config_user ()->getCurrentTheme ();
int direction = Database_Config_Bible::getTextDirection (bible);
int lineheight = Database_Config_Bible::getLineHeight (bible);
int letterspacing = Database_Config_Bible::getLetterSpacing (bible);
string versebeam_current_theme = Filter_Css::theme_picker (current_theme_index, 5);
if (versebeam_current_theme == "") versebeam_current_theme = "versebeam";
const std::string clss = Filter_Css::getClass (bible);
const std::string font = fonts::logic::get_text_font (bible);
const int current_theme_index = webserver_request.database_config_user ()->getCurrentTheme ();
const int direction = Database_Config_Bible::getTextDirection (bible);
const int lineheight = Database_Config_Bible::getLineHeight (bible);
const int letterspacing = Database_Config_Bible::getLetterSpacing (bible);
std::string versebeam_current_theme = Filter_Css::theme_picker (current_theme_index, 5);
if (versebeam_current_theme.empty())
versebeam_current_theme = "versebeam";
view.set_variable ("versebeam_theme_color", versebeam_current_theme);
view.set_variable ("editor_theme_color", Filter_Css::theme_picker (current_theme_index, 2));
view.set_variable ("active_editor_theme_color", Filter_Css::theme_picker (current_theme_index, 3));
view.set_variable ("custom_class", clss);
view.set_variable ("custom_css", Filter_Css::get_css (clss,
fonts::logic::get_font_path (font),
direction,
lineheight,
letterspacing));
view.set_variable ("custom_css", Filter_Css::get_css (clss, fonts::logic::get_font_path (font),
direction, lineheight, letterspacing));


// In basic mode the editor has no controls and fewer indicators.
// In basic mode, the user can just edit text, and cannot style it.
bool basic_mode = config::logic::basic_mode (webserver_request);
const bool basic_mode = config::logic::basic_mode (std::addressof(webserver_request));
if (!basic_mode) view.enable_zone ("advancedmode");


// Whether to enable fast Bible editor switching.
if (!basic_mode && request->database_config_user ()->getFastEditorSwitchingAvailable ()) {
if (!basic_mode && webserver_request.database_config_user ()->getFastEditorSwitchingAvailable ()) {
view.enable_zone ("fastswitcheditor");
}


// Whether to enable the styles button.
if (request->database_config_user ()->getEnableStylesButtonVisualEditors ()) {
if (webserver_request.database_config_user ()->getEnableStylesButtonVisualEditors ()) {
view.enable_zone ("stylesbutton");
}

Expand Down
6 changes: 4 additions & 2 deletions edit/index.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

#include <config/libraries.h>

class Webserver_Request;

std::string edit_index_url ();
bool edit_index_acl (void * webserver_request);
std::string edit_index (void * webserver_request);
bool edit_index_acl (Webserver_Request& webserver_request);
std::string edit_index (Webserver_Request& webserver_request);
43 changes: 20 additions & 23 deletions edit/navigate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,43 +26,40 @@
#include <editor/usfm2html.h>
#include <access/bible.h>
#include <database/config/bible.h>
using namespace std;


string edit_navigate_url ()
std::string edit_navigate_url ()
{
return "edit/navigate";
}


bool edit_navigate_acl (void * webserver_request)
bool edit_navigate_acl (Webserver_Request& webserver_request)
{
if (Filter_Roles::access_control (webserver_request, Filter_Roles::translator ())) return true;
auto [ read, write ] = access_bible::any (webserver_request);
if (Filter_Roles::access_control (std::addressof(webserver_request), Filter_Roles::translator ()))
return true;
auto [ read, write ] = access_bible::any (std::addressof(webserver_request));
return write;
}


string edit_navigate (void * webserver_request)
std::string edit_navigate (Webserver_Request& webserver_request)
{
Webserver_Request * request = static_cast<Webserver_Request *>(webserver_request);


string bible = request->query ["bible"];
int book = filter::strings::convert_to_int (request->query ["book"]);
int chapter = filter::strings::convert_to_int (request->query ["chapter"]);
const std::string bible = webserver_request.query ["bible"];
const int book = filter::strings::convert_to_int (webserver_request.query ["book"]);
const int chapter = filter::strings::convert_to_int (webserver_request.query ["chapter"]);


// At first the browser used the rangy library to get the offset of the caret.
// But the rangy library provides the offset relative to the element that contains the caret,
// not relative to the main editor element.
// Therefore a pure Javascript implementation was Googled for and implemented.
// This provides the offset of the caret relative to the <div id="editor">.
size_t offset = static_cast<size_t> (filter::strings::convert_to_int (request->query ["offset"]));
const size_t offset = static_cast<size_t> (filter::strings::convert_to_int (webserver_request.query ["offset"]));


string stylesheet = Database_Config_Bible::getEditorStylesheet (bible);
string usfm = request->database_bibles()->get_chapter (bible, book, chapter);
const std::string stylesheet = Database_Config_Bible::getEditorStylesheet (bible);
const std::string usfm = webserver_request.database_bibles()->get_chapter (bible, book, chapter);


Editor_Usfm2Html editor_usfm2html;
Expand All @@ -79,7 +76,7 @@ string edit_navigate (void * webserver_request)
// Get the number of verses in the USFM.
// This covers combined verses also.
int last_offset {0};
vector <int> verses = filter::usfm::get_verse_numbers (usfm);
const std::vector <int> verses = filter::usfm::get_verse_numbers (usfm);
for (int i = 0; i < static_cast<int>(verses.size ()); i++) {
if (editor_usfm2html.m_verse_start_offsets.count (i)) {
last_offset = editor_usfm2html.m_verse_start_offsets [i];
Expand All @@ -90,15 +87,15 @@ string edit_navigate (void * webserver_request)


// Get the starting offsets for each verse.
vector <size_t> starting_offsets;
std::vector <size_t> starting_offsets{};
for (int i = 0; i < static_cast<int> (verses.size ()); i++) {
starting_offsets.push_back (static_cast<size_t>(editor_usfm2html.m_verse_start_offsets [i]));
}
starting_offsets.push_back (editor_usfm2html.m_text_tength);


// Get the ending offsets for each verse.
vector <size_t> ending_offsets;
std::vector <size_t> ending_offsets;
for (size_t i = 0; i < verses.size (); i++) {
size_t offset2 = starting_offsets [i];
for (size_t i2 = 0; i2 < starting_offsets.size (); i2++) {
Expand All @@ -112,12 +109,12 @@ string edit_navigate (void * webserver_request)


// If the offset is between the focused verse's min and max values, then do nothing.
int verse = Ipc_Focus::getVerse (request);
int verse = Ipc_Focus::getVerse (std::addressof(webserver_request));
for (size_t i = 0; i < verses.size (); i++) {
if (verse == verses[i]) {
if (offset >= starting_offsets [i]) {
if (offset <= ending_offsets [i]) {
return string();
return std::string();
}
}
}
Expand All @@ -140,8 +137,8 @@ string edit_navigate (void * webserver_request)
if (verse >= 0) {
// Only update navigation in case the verse changed.
// This avoids unnecessary focus operations in the clients.
if (verse != Ipc_Focus::getVerse (request)) {
Ipc_Focus::set (request, book, chapter, verse);
if (verse != Ipc_Focus::getVerse (std::addressof(webserver_request))) {
Ipc_Focus::set (std::addressof(webserver_request), book, chapter, verse);
}
// The editor should scroll the verse into view,
// because the caret is in the Bible text.
Expand All @@ -151,5 +148,5 @@ string edit_navigate (void * webserver_request)
}


return "";
return std::string();
}
6 changes: 4 additions & 2 deletions edit/navigate.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

#include <config/libraries.h>

class Webserver_Request;

std::string edit_navigate_url ();
bool edit_navigate_acl (void * webserver_request);
std::string edit_navigate (void * webserver_request);
bool edit_navigate_acl (Webserver_Request& webserver_request);
std::string edit_navigate (Webserver_Request& webserver_request);
Loading

0 comments on commit 2a3e2ca

Please sign in to comment.