Skip to content

Commit

Permalink
Clearer types in code rather than void pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
teusbenschop committed Jan 9, 2024
1 parent 2c0cd79 commit 40ba07c
Show file tree
Hide file tree
Showing 12 changed files with 56 additions and 82 deletions.
38 changes: 16 additions & 22 deletions dialog/books.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,76 +24,70 @@
#include <filter/url.h>
#include <filter/string.h>
#include <database/books.h>
using namespace std;


// Constructs a Bible books selection dialog
// $action - GET action to take: Will be added to the base url upon selection.
// $inclusions - vector of book IDs to include, empty vector does nothing.
// $exclusions - vector of book IDs to exclude, empty vector does nothing.
Dialog_Books::Dialog_Books (string url, string header, string info_top, string info_bottom, string action, vector <int> inclusions, vector <int> exclusions)
Dialog_Books::Dialog_Books (std::string url, std::string header, std::string info_top, std::string info_bottom, std::string action, std::vector <int> inclusions, std::vector <int> exclusions)
{
Assets_View * view = new Assets_View ();
base_url = url;
view->set_variable ("header", header);
view->set_variable ("info_top", info_top);
view->set_variable ("info_bottom", info_bottom);
assets_view.set_variable ("header", header);
assets_view.set_variable ("info_top", info_top);
assets_view.set_variable ("info_bottom", info_bottom);
selection_action = action;
include = inclusions;
exclude = exclusions;
assets_view = view;
}


Dialog_Books::~Dialog_Books ()
{
Assets_View * view = static_cast<Assets_View *>(assets_view);
delete view;
}


// Add "parameter" and "value" to be added in the base query, or base url.
// If any $query is passed, if Cancel is clicked in this dialog, it should go go back
// to the original caller page with the $query added.
// Same for when a selection is made: It adds the $query to the page where to go.
void Dialog_Books::add_query (string parameter, string value)
void Dialog_Books::add_query (std::string parameter, std::string value)
{
base_url = filter_url_build_http_query (base_url, parameter, value);
}


string Dialog_Books::run ()
std::string Dialog_Books::run ()
{
Assets_View * view = static_cast<Assets_View *>(assets_view);
view->set_variable ("base_url", base_url);
assets_view.set_variable ("base_url", base_url);

vector <int> book_ids {};
std::vector <int> book_ids {};
{
vector <book_id> book_enums = database::books::get_ids ();
for (auto book : book_enums) book_ids.push_back(static_cast<int>(book));
std::vector <book_id> book_enums = database::books::get_ids ();
for (const auto book : book_enums) book_ids.push_back(static_cast<int>(book));
}
if (!include.empty ()) {
book_ids = include;
}
if (!exclude.empty()) {
vector <int> ids;
for (auto & book_id : book_ids) {
std::vector <int> ids;
for (const auto & book_id : book_ids) {
if (find (exclude.begin(), exclude.end(), book_id) == exclude.end ()) {
ids.push_back (book_id);
}
}
book_ids = ids;
}

stringstream book_block;
for (auto & id : book_ids) {
std::stringstream book_block;
for (const auto & id : book_ids) {
book_block << "<a href=";
book_block << quoted(filter_url_build_http_query (base_url, selection_action, filter::strings::convert_to_string (id)));
book_block << ">" << database::books::get_english_from_id (static_cast<book_id>(id)) << "</a>\n";
}
view->set_variable ("book_block", book_block.str());
assets_view.set_variable ("book_block", book_block.str());

string page = view->render ("dialog", "books");
std::string page = assets_view.render ("dialog", "books");
page += assets_page::footer ();
return page;
}
3 changes: 2 additions & 1 deletion dialog/books.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#pragma once

#include <config/libraries.h>
#include <assets/view.h>

class Dialog_Books
{
Expand All @@ -31,7 +32,7 @@ class Dialog_Books
void add_query (std::string parameter, std::string value);
std::string run ();
private:
void * assets_view {nullptr};
Assets_View assets_view {};
std::string base_url {};
std::string selection_action {};
std::vector <int> include {};
Expand Down
11 changes: 3 additions & 8 deletions dialog/color.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,13 @@ using namespace std;

Dialog_Color::Dialog_Color (string url, string question)
{
Assets_View * view = new Assets_View ();
base_url = url;
view->set_variable ("question", question);
assets_view = view;
assets_view.set_variable ("question", question);
}


Dialog_Color::~Dialog_Color ()
{
Assets_View * view = static_cast<Assets_View *>(assets_view);
delete view;
}


Expand All @@ -53,9 +49,8 @@ void Dialog_Color::add_query (string parameter, string value)

string Dialog_Color::run ()
{
Assets_View * view = static_cast<Assets_View *>(assets_view);
view->set_variable ("base_url", base_url);
string page = view->render ("dialog", "color");
assets_view.set_variable ("base_url", base_url);
string page = assets_view.render ("dialog", "color");
page += assets_page::footer ();
return page;
}
3 changes: 2 additions & 1 deletion dialog/color.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#pragma once

#include <config/libraries.h>
#include <assets/view.h>

class Dialog_Color
{
Expand All @@ -31,6 +32,6 @@ class Dialog_Color
void add_query (std::string parameter, std::string value);
std::string run ();
private:
void * assets_view {nullptr};
Assets_View assets_view {};
std::string base_url {};
};
17 changes: 6 additions & 11 deletions dialog/entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,15 @@ Dialog_Entry::Dialog_Entry (string url, string question, string value, string su
{
Assets_View * view = new Assets_View ();
base_url = url;
view->set_variable ("question", question);
view->set_variable ("value", value);
view->set_variable ("submit", submit);
view->set_variable ("help", help);
assets_view = view;
assets_view.set_variable ("question", question);
assets_view.set_variable ("value", value);
assets_view.set_variable ("submit", submit);
assets_view.set_variable ("help", help);
}


Dialog_Entry::~Dialog_Entry ()
{
Assets_View * view = static_cast<Assets_View *>(assets_view);
delete view;
}


Expand All @@ -61,10 +58,8 @@ void Dialog_Entry::add_query (string parameter, string value)

string Dialog_Entry::run ()
{
Assets_View * view = static_cast<Assets_View *>(assets_view);
view->set_variable ("base_url", base_url);
string page = view->render ("dialog", "entry");
assets_view.set_variable ("base_url", base_url);
string page =assets_view.render ("dialog", "entry");
page += assets_page::footer ();
return page;
}

3 changes: 2 additions & 1 deletion dialog/entry.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#pragma once

#include <config/libraries.h>
#include <assets/view.h>

class Dialog_Entry
{
Expand All @@ -31,6 +32,6 @@ class Dialog_Entry
void add_query (std::string parameter, std::string value);
std::string run ();
private:
void * assets_view {nullptr};
Assets_View assets_view {};
std::string base_url {};
};
17 changes: 6 additions & 11 deletions dialog/list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,18 @@ using namespace std;
// $post: causes the result to be sent via the POST method rather than the default GET method.
Dialog_List::Dialog_List (string url, string question, string info_top, string info_bottom, bool post)
{
Assets_View * view = new Assets_View ();
base_url = url;
view->set_variable ("question", question);
assets_view.set_variable ("question", question);
if (info_top == "") info_top = translate("Here are the various options:");
view->set_variable ("info_top", info_top);
assets_view.set_variable ("info_top", info_top);
if (info_bottom == "") info_bottom = translate("Please pick one.");
view->set_variable ("info_bottom", info_bottom);
assets_view.set_variable ("info_bottom", info_bottom);
post_result = post;
assets_view = view;
}


Dialog_List::~Dialog_List ()
{
Assets_View * view = static_cast<Assets_View *>(assets_view);
delete view;
}


Expand Down Expand Up @@ -82,10 +78,9 @@ void Dialog_List::add_row (string text, string parameter, string value)

string Dialog_List::run ()
{
Assets_View * view = static_cast<Assets_View *>(assets_view);
view->set_variable ("base_url", base_url);
view->set_variable ("list_block", list_block);
string page = view->render ("dialog", "list");
assets_view.set_variable ("base_url", base_url);
assets_view.set_variable ("list_block", list_block);
string page = assets_view.render ("dialog", "list");
page += assets_page::footer ();
return page;
}
3 changes: 2 additions & 1 deletion dialog/list.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#pragma once

#include <config/libraries.h>
#include <assets/view.h>

class Dialog_List
{
Expand All @@ -32,7 +33,7 @@ class Dialog_List
void add_row (std::string text, std::string parameter, std::string value);
std::string run ();
private:
void * assets_view {nullptr};
Assets_View assets_view {};
std::string base_url {};
std::string list_block {};
bool post_result {false};
Expand Down
24 changes: 9 additions & 15 deletions dialog/upload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,45 +21,39 @@
#include <assets/view.h>
#include <assets/page.h>
#include <filter/url.h>
using namespace std;


// Dialog that enables the user to upload a file.
// $url: The url of the page where to go to on clicking Cancel or Upload.
// $question: The question to ask.
Dialog_Upload::Dialog_Upload (string url, string question)
Dialog_Upload::Dialog_Upload (std::string url, std::string question)
{
Assets_View * view = new Assets_View ();
base_url = url;
view->set_variable ("question", question);
assets_view = view;
assets_view.set_variable ("question", question);
}


Dialog_Upload::~Dialog_Upload ()
{
Assets_View * view = static_cast<Assets_View *>(assets_view);
delete view;
}


// Adds a query to the URL for going to the page on clicking Upload.
void Dialog_Upload::add_upload_query (string parameter, string value)
void Dialog_Upload::add_upload_query (std::string parameter, std::string value)
{
upload_query [parameter] = value;
}


string Dialog_Upload::run ()
std::string Dialog_Upload::run ()
{
Assets_View * view = static_cast<Assets_View *>(assets_view);
string import;
for (auto & element : upload_query) {
std::string import;
for (const auto & element : upload_query) {
import = filter_url_build_http_query (base_url, element.first, element.second);
}
view->set_variable ("import", import);
view->set_variable ("cancel", base_url);
string page = view->render ("dialog", "upload");
assets_view.set_variable ("import", import);
assets_view.set_variable ("cancel", base_url);
std::string page = assets_view.render ("dialog", "upload");
page += assets_page::footer ();
return page;
}
3 changes: 2 additions & 1 deletion dialog/upload.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#pragma once

#include <config/libraries.h>
#include <assets/view.h>

class Dialog_Upload
{
Expand All @@ -31,7 +32,7 @@ class Dialog_Upload
void add_upload_query (std::string parameter, std::string value);
std::string run ();
private:
void * assets_view = {nullptr};
Assets_View assets_view {};
std::string base_url {};
std::map <std::string, std::string> upload_query {};
};
13 changes: 4 additions & 9 deletions dialog/yes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,13 @@
// $question: The question to ask.
Dialog_Yes::Dialog_Yes (std::string url, std::string question)
{
Assets_View * view = new Assets_View ();
base_url = url;
view->set_variable ("question", question);
assets_view = view;
assets_view.set_variable ("question", question);
}


Dialog_Yes::~Dialog_Yes ()
{
Assets_View * view = static_cast<Assets_View *>(assets_view);
delete view;
}


Expand All @@ -51,12 +47,11 @@ void Dialog_Yes::add_query (std::string parameter, std::string value)

std::string Dialog_Yes::run ()
{
Assets_View * view = static_cast<Assets_View *>(assets_view);
std::string yes = filter_url_build_http_query (base_url, "confirm", "yes");
std::string cancel = filter_url_build_http_query (base_url, "confirm", "cancel");
view->set_variable ("yes", yes);
view->set_variable ("cancel", cancel);
std::string page = view->render ("dialog", "yes");
assets_view.set_variable ("yes", yes);
assets_view.set_variable ("cancel", cancel);
std::string page = assets_view.render ("dialog", "yes");
page += assets_page::footer ();
return page;
}
Expand Down
3 changes: 2 additions & 1 deletion dialog/yes.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#pragma once

#include <config/libraries.h>
#include <assets/view.h>

class Dialog_Yes
{
Expand All @@ -31,6 +32,6 @@ class Dialog_Yes
void add_query (std::string parameter, std::string value);
std::string run ();
private:
void * assets_view {nullptr};
Assets_View assets_view {};
std::string base_url {};
};

0 comments on commit 40ba07c

Please sign in to comment.