Browse Source

adding Book class

Bernardo Magri 3 months ago
parent
commit
b7896f9d0c
2 changed files with 140 additions and 0 deletions
  1. 58 0
      src/Book.cpp
  2. 82 0
      src/Book.hpp

+ 58 - 0
src/Book.cpp

@@ -0,0 +1,58 @@
+// Book.cpp
+#include "Book.hpp"
+
+#include <utility>              // std::move
+#include <gdkmm/texture.h>      // pulls in the GTK bits only here
+#include <iostream>
+
+Book::Book(std::string_view idSha256,
+           std::string_view title,
+           std::string_view author,
+           std::string_view filePath,
+           std::string_view coverPath)
+  : m_id(idSha256),
+    m_title(title),
+    m_author(author),
+    m_filePath(filePath),
+    m_coverPath(coverPath) {}
+
+Book::Book() = default;
+Book::Book(const Book&) = default;
+Book::Book(Book&&) noexcept = default;
+Book& Book::operator=(const Book&) = default;
+Book& Book::operator=(Book&&) noexcept = default;
+Book::~Book() = default;
+
+bool Book::operator<(const Book& other) const noexcept {
+  if (m_title != other.m_title)   return m_title  < other.m_title;
+  if (m_author != other.m_author) return m_author < other.m_author;
+  return m_id < other.m_id;
+}
+
+const Glib::RefPtr<Gdk::Texture>& Book::cover() const noexcept {
+  return m_cover;
+}
+
+void Book::set_cover(const Glib::RefPtr<Gdk::Texture>& tex) {
+  m_cover = tex;
+}
+
+void Book::set_cover(Glib::RefPtr<Gdk::Texture>&& tex) {
+  m_cover = std::move(tex);
+}
+
+void Book::load_cover_from_disk(const std::string& fallback_icon_name) {
+  // Try to load the cover from disk first
+  if (!m_coverPath.empty()) {
+    try {
+      auto tex = Gdk::Texture::create_from_filename(m_coverPath);
+      if (tex) {
+        m_cover = tex;
+        return;
+      }
+    } catch (const Glib::Error& e) {
+      std::cerr << "Failed to load cover from " << m_coverPath
+                << ": " << e.what() << std::endl;
+    }
+  }
+}

+ 82 - 0
src/Book.hpp

@@ -0,0 +1,82 @@
+#pragma once
+
+#include <string>
+#include <string_view>
+#include <utility>   // std::move
+#include <gtkmm.h>
+#include <glib.h>
+
+// Forward declarations so we don't pull in heavy GTK headers here.
+//namespace Gdk { class Texture; }
+//namespace Glib { template <class T> class RefPtr; }
+
+/**
+ * @brief Represents a single book in the Bibliotheca library.
+ *
+ * A Book is uniquely identified by its SHA-256 digest of the underlying file.
+ * It stores lightweight metadata (title, author, file/cover paths) and may
+ * optionally hold a Gdk::Texture cover image for fast rendering in the UI.
+ */
+class Book {
+public:
+  // -----------------------------------------------------------------------
+  // Constructors & Rule-of-Five
+  // -----------------------------------------------------------------------
+
+  Book(std::string_view idSha256,
+       std::string_view title,
+       std::string_view author,
+       std::string_view filePath,
+       std::string_view coverPath = {});
+
+  Book();
+  Book(const Book&);
+  Book(Book&&) noexcept;
+  Book& operator=(const Book&);
+  Book& operator=(Book&&) noexcept;
+  ~Book();
+
+  // -----------------------------------------------------------------------
+  // Accessors
+  // -----------------------------------------------------------------------
+
+  const std::string& id()        const noexcept { return m_id; }
+  const std::string& title()     const noexcept { return m_title; }
+  const std::string& author()    const noexcept { return m_author; }
+  const std::string& filePath()  const noexcept { return m_filePath; }
+  const std::string& coverPath() const noexcept { return m_coverPath; }
+
+  // -----------------------------------------------------------------------
+  // Cover management
+  // -----------------------------------------------------------------------
+
+  const Glib::RefPtr<Gdk::Texture>& cover() const noexcept;
+  void set_cover(const Glib::RefPtr<Gdk::Texture>& tex);
+  void set_cover(Glib::RefPtr<Gdk::Texture>&& tex);
+	
+  void load_cover_from_disk(const std::string& fallback_icon_name = "image-x-generic-symbolic");
+	
+
+  // -----------------------------------------------------------------------
+  // Comparison operators
+  // -----------------------------------------------------------------------
+
+  // Equality by unique hash ID
+  bool operator==(const Book& other) const noexcept { return m_id == other.m_id; }
+
+  // Ordering by title, then author, then ID
+  bool operator<(const Book& other) const noexcept;
+
+private:
+  // -----------------------------------------------------------------------
+  // Data members
+  // -----------------------------------------------------------------------
+
+  std::string m_id;        ///< Hex SHA-256 of the file contents
+  std::string m_title;     ///< Book title
+  std::string m_author;    ///< Author name
+  std::string m_filePath;  ///< Absolute path to the file on disk
+  std::string m_coverPath; ///< Path to the cover image (optional)
+
+  Glib::RefPtr<Gdk::Texture> m_cover; ///< Cached cover image for UI rendering
+};