81 lines
2.8 KiB
C
81 lines
2.8 KiB
C
|
|
#pragma once
|
||
|
|
#include <string>
|
||
|
|
#include <vector>
|
||
|
|
|
||
|
|
/**
|
||
|
|
* BookManager - Handles book archiving, unarchiving, and deletion
|
||
|
|
*
|
||
|
|
* Archive: Moves book and its cache to /.archive/, preserving reading progress
|
||
|
|
* Unarchive: Restores book and cache to original location
|
||
|
|
* Delete: Permanently removes book and its cache
|
||
|
|
*/
|
||
|
|
class BookManager {
|
||
|
|
public:
|
||
|
|
static constexpr const char* ARCHIVE_DIR = "/.archive";
|
||
|
|
static constexpr const char* ARCHIVE_CACHE_DIR = "/.archive/.cache";
|
||
|
|
static constexpr const char* CROSSPOINT_DIR = "/.crosspoint";
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Archive a book - moves file and cache to /.archive/
|
||
|
|
* @param bookPath Full path to the book file (e.g., "/Books/mybook.epub")
|
||
|
|
* @return true if successful
|
||
|
|
*/
|
||
|
|
static bool archiveBook(const std::string& bookPath);
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Unarchive a book - restores file and cache to original location
|
||
|
|
* @param archivedFilename Filename in /.archive/ (e.g., "mybook.epub")
|
||
|
|
* @return true if successful
|
||
|
|
*/
|
||
|
|
static bool unarchiveBook(const std::string& archivedFilename);
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Delete a book permanently - removes file and cache
|
||
|
|
* @param bookPath Full path to the book file
|
||
|
|
* @param isArchived If true, treats path as archived filename and also deletes .meta file
|
||
|
|
* @return true if successful
|
||
|
|
*/
|
||
|
|
static bool deleteBook(const std::string& bookPath, bool isArchived = false);
|
||
|
|
|
||
|
|
/**
|
||
|
|
* List archived books
|
||
|
|
* @return Vector of archived filenames (without path)
|
||
|
|
*/
|
||
|
|
static std::vector<std::string> listArchivedBooks();
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get the original path of an archived book
|
||
|
|
* @param archivedFilename Filename in /.archive/
|
||
|
|
* @return Original path, or empty string if not found
|
||
|
|
*/
|
||
|
|
static std::string getArchivedBookOriginalPath(const std::string& archivedFilename);
|
||
|
|
|
||
|
|
private:
|
||
|
|
// Extract filename from a full path
|
||
|
|
static std::string getFilename(const std::string& path);
|
||
|
|
|
||
|
|
// Get the file extension (lowercase, including the dot)
|
||
|
|
static std::string getExtension(const std::string& path);
|
||
|
|
|
||
|
|
// Compute the hash used for cache directory naming
|
||
|
|
static size_t computePathHash(const std::string& path);
|
||
|
|
|
||
|
|
// Get cache directory prefix for a file type (epub_, txt_, xtc_)
|
||
|
|
static std::string getCachePrefix(const std::string& path);
|
||
|
|
|
||
|
|
// Get the full cache directory path for a book
|
||
|
|
static std::string getCacheDir(const std::string& bookPath);
|
||
|
|
|
||
|
|
// Write the .meta file for an archived book
|
||
|
|
static bool writeMetaFile(const std::string& archivedPath, const std::string& originalPath);
|
||
|
|
|
||
|
|
// Read the original path from a .meta file
|
||
|
|
static std::string readMetaFile(const std::string& archivedPath);
|
||
|
|
|
||
|
|
// Create parent directories for a path if they don't exist
|
||
|
|
static bool ensureParentDirExists(const std::string& path);
|
||
|
|
|
||
|
|
// Check if a path is a supported book format
|
||
|
|
static bool isSupportedBookFormat(const std::string& path);
|
||
|
|
};
|