Skip to content

Commit

Permalink
app changes
Browse files Browse the repository at this point in the history
  • Loading branch information
a3st committed Jun 3, 2024
1 parent 4371bbd commit a8866e3
Show file tree
Hide file tree
Showing 19 changed files with 197 additions and 56 deletions.
3 changes: 0 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,3 @@
[submodule "thirdparty/libwebview"]
path = thirdparty/libwebview
url = https://github.com/a3st/libwebview
[submodule "thirdparty/cppcoro"]
path = thirdparty/cppcoro
url = https://github.com/lewissbaker/cppcoro
14 changes: 13 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,22 @@ add_subdirectory(thirdparty/base64pp)
endif()

add_executable(Notes
src/exporters/txt.cpp
src/main.cpp
src/view_model.cpp
src/note_storage.cpp
src/note.cpp)
src/note.cpp
app.rc)

if(WIN32)
set_target_properties(Notes
PROPERTIES
LINK_FLAGS_DEBUG "/SUBSYSTEM:CONSOLE"
LINK_FLAGS_RELEASE "/SUBSYSTEM:windows /ENTRY:mainCRTStartup"
LINK_FLAGS_RELWITHDEBINFO "/SUBSYSTEM:windows /ENTRY:mainCRTStartup"
LINK_FLAGS_MINSIZEREL "/SUBSYSTEM:windows /ENTRY:mainCRTStartup"
)
endif()

target_include_directories(Notes PRIVATE
${PROJECT_SOURCE_DIR}/src
Expand Down
1 change: 1 addition & 0 deletions app.rc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
IDI_ICON1 ICON DISCARDABLE "favicon.ico"
Binary file added favicon.ico
Binary file not shown.
10 changes: 10 additions & 0 deletions include/exporter.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once

namespace notes
{
class Exporter
{
public:
virtual bool saveFileAs(std::filesystem::path const& filePath) = 0;
};
} // namespace notes
17 changes: 17 additions & 0 deletions include/exporters/txt.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#include "exporter.hpp"

namespace notes::exporters
{
class TxtExport : public Exporter
{
public:
TxtExport(std::string_view const text);

bool saveFileAs(std::filesystem::path const& filePath) override;

private:
std::string text;
};
} // namespace notes::exporters
2 changes: 2 additions & 0 deletions include/note.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ namespace notes
{
struct Note
{
Note() = default;

Note(uint32_t const noteID, std::string_view const noteName, std::string_view const noteData);

/*!
Expand Down
4 changes: 3 additions & 1 deletion include/note_storage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ namespace notes

std::string getNotesData() const;

void saveNoteToDB(Note const& note);
int32_t saveNoteToDB(Note const& note);

void removeNoteFromDB(uint32_t const ID);

bool exportNoteAsFile(uint32_t const ID, std::string_view const format, std::filesystem::path const& filePath);

private:
SQLite::Database database;

Expand Down
4 changes: 3 additions & 1 deletion include/precompiled.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@
#include <iostream>
#include <string>
#include <string_view>
#include <sstream>
#include <sstream>
#include <filesystem>
#include <fstream>
4 changes: 3 additions & 1 deletion include/view_model.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ namespace notes

std::string getNotes();

void saveNote(uint32_t ID, std::string noteName, std::string noteData);
int32_t saveNote(uint32_t ID, std::string noteName, std::string noteData);

void removeNote(uint32_t ID);

void exportNote(uint32_t ID, std::string format);

private:
libwebview::App* app;
NoteStorage noteStorage;
Expand Down
21 changes: 21 additions & 0 deletions src/exporters/txt.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "exporters/txt.hpp"
#include "precompiled.hpp"

namespace notes::exporters
{
TxtExport::TxtExport(std::string_view const text) : text(text)
{
}

bool TxtExport::saveFileAs(std::filesystem::path const& filePath)
{
std::ofstream stream(filePath, std::ios::out);
if (!stream.is_open())
{
return false;
}

stream << text;
return true;
}
} // namespace notes::exporters
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ int32_t main(int32_t argc, char** argv)
}
catch (std::exception e)
{
std::cerr << e.what() << std::endl;
libwebview::showMessageDialog("Notes", e.what(), libwebview::MessageDialogType::Error);
return EXIT_FAILURE;
}
}
59 changes: 52 additions & 7 deletions src/note_storage.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#include "note_storage.hpp"
#include "precompiled.hpp"
#include <base64pp/base64pp.h>

#include "exporters/txt.hpp"

namespace notes
{
Expand Down Expand Up @@ -55,7 +58,7 @@ namespace notes
return true;
}

void NoteStorage::saveNoteToDB(Note const& note)
int32_t NoteStorage::saveNoteToDB(Note const& note)
{
std::string const dbCheckNoteSQL = std::format("SELECT count(*) FROM `notes` WHERE `id`={}", note.noteID);

Expand All @@ -65,11 +68,7 @@ namespace notes
std::string const dbAddNoteSQL = std::format(
"INSERT INTO `notes` (`name`, `data`) VALUES ('{}', '{}') RETURNING id", note.noteName, note.noteData);

int32_t const result = database.tryExec(dbAddNoteSQL);
if (result != SQLite::OK)
{
throw std::runtime_error("An error occurred when adding a row to the database");
}
return database.execAndGet(dbAddNoteSQL).getInt();
}
else
{
Expand All @@ -81,6 +80,8 @@ namespace notes
{
throw std::runtime_error("An error occurred when updating a row to the database");
}

return note.noteID;
}
}

Expand All @@ -99,7 +100,7 @@ namespace notes
{
std::stringstream stream;
stream << "{\"notes\":[";

bool isFirst = true;
for (auto const& note : this->loadNotesFromDB())
{
Expand All @@ -113,4 +114,48 @@ namespace notes
stream << "]}";
return stream.str();
}

bool NoteStorage::exportNoteAsFile(uint32_t const ID, std::string_view const format,
std::filesystem::path const& filePath)
{
std::string const dbSelectSQL = std::format("SELECT `name`, `data` FROM `notes` WHERE `id`={}", ID);

SQLite::Statement query(database, dbSelectSQL);

Note note;
if (query.executeStep())
{
std::string const noteName = query.getColumn(0).getString();
std::string const noteData = query.getColumn(1).getString();

note.noteID = ID;
note.noteName = noteName;
note.noteData = noteData;
}

if (query.hasRow())
{
std::unique_ptr<Exporter> exporter;

std::string text;

auto result = base64pp::decode(note.noteData);
if (!result.has_value())
{
text = "";
}
else
{
auto decodedData = result.value();
text = std::string(reinterpret_cast<char const*>(decodedData.data()), decodedData.size());
}

if (format.compare("txt") == 0)
{
exporter = std::make_unique<exporters::TxtExport>(text);
}

return exporter->saveFileAs(filePath);
}
}
} // namespace notes
25 changes: 20 additions & 5 deletions src/view_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,42 @@

namespace notes
{
ViewModel::ViewModel(libwebview::App* app)
ViewModel::ViewModel(libwebview::App* app) : app(app)
{
app->bind("getNotes", [this]() -> std::string { return getNotes(); });
app->bind("saveNote", [this](uint32_t ID, std::string noteName, std::string noteData) -> void {
saveNote(ID, noteName, noteData);
app->bind("saveNote", [this](uint32_t ID, std::string noteName, std::string noteData) -> int32_t {
return saveNote(ID, noteName, noteData);
});
app->bind("removeNote", [this](uint32_t ID) -> void { removeNote(ID); });
app->bind("exportNote", [this](uint32_t ID, std::string format) -> void { exportNote(ID, format); });
}

std::string ViewModel::getNotes()
{
return noteStorage.getNotesData();
}

void ViewModel::saveNote(uint32_t ID, std::string noteName, std::string noteData)
int32_t ViewModel::saveNote(uint32_t ID, std::string noteName, std::string noteData)
{
noteStorage.saveNoteToDB(Note(ID, noteName, noteData));
return noteStorage.saveNoteToDB(Note(ID, noteName, noteData));
}

void ViewModel::removeNote(uint32_t ID)
{
noteStorage.removeNoteFromDB(ID);
}

void ViewModel::exportNote(uint32_t ID, std::string format)
{
auto result = app->showSaveDialog(std::filesystem::current_path(), "");
if (result.has_value())
{
if (!noteStorage.exportNoteAsFile(ID, format, result.value()))
{
libwebview::showMessageDialog("Notes",
"Произошла ошибка во время сохранения файла. Попробуйте еще раз!",
libwebview::MessageDialogType::Error);
}
}
}
} // namespace notes
Loading

0 comments on commit a8866e3

Please sign in to comment.