Program Listing for File item.h

Return to documentation for file (include/zim/writer/item.h)

/*
 * Copyright (C) 2020-2021 Matthieu Gautier <mgautier@kymeria.fr>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * is provided AS IS, WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, and
 * NON-INFRINGEMENT.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 *
 */

#ifndef ZIM_WRITER_ITEM_H
#define ZIM_WRITER_ITEM_H

#include <stdexcept>
#include <zim/blob.h>
#include <zim/zim.h>
#include <zim/uuid.h>
#include <string>

#include <map>

namespace zim
{
  namespace writer
  {
    enum HintKeys {
      COMPRESS,
      FRONT_ARTICLE,
    };
    using Hints = std::map<HintKeys, uint64_t>;

    class ContentProvider;

    class LIBZIM_API IndexData {
      public:
        using GeoPosition = std::tuple<bool, double, double>;
        virtual ~IndexData() = default;

        virtual bool hasIndexData() const = 0;

        virtual std::string getTitle() const = 0;

        virtual std::string getContent() const = 0;

        virtual std::string getKeywords() const = 0;

        virtual uint32_t getWordCount() const = 0;

        virtual GeoPosition getGeoPosition() const = 0;
    };

    class LIBZIM_API Item
    {
      public:
        virtual std::string getPath() const = 0;

        virtual std::string getTitle() const = 0;

        virtual std::string getMimeType() const = 0;

        virtual std::unique_ptr<ContentProvider> getContentProvider() const = 0;

        virtual std::shared_ptr<IndexData> getIndexData() const;

        virtual Hints getHints() const;

        Hints getAmendedHints() const;
        virtual ~Item() = default;
    };

    class LIBZIM_API BasicItem : public Item
    {
      public:
        BasicItem(const std::string& path, const std::string& mimetype, const std::string& title, Hints hints)
          : path(path),
            mimetype(mimetype),
            title(title),
            hints(hints)
        {}

        std::string getPath() const { return path; }
        std::string getTitle() const { return title; }
        std::string getMimeType() const { return mimetype; }
        Hints       getHints() const { return hints; }

      protected:
        std::string path;
        std::string mimetype;
        std::string title;
        Hints hints;
    };

    class LIBZIM_API StringItem : public BasicItem, public std::enable_shared_from_this<StringItem>
    {
      public:
        template<typename... Ts>
        static std::shared_ptr<StringItem> create(Ts&&... params) {
          return std::shared_ptr<StringItem>(new StringItem(std::forward<Ts>(params)...));
        }

        std::unique_ptr<ContentProvider> getContentProvider() const;

      protected:
        std::string content;

      private:
        StringItem(const std::string& path, const std::string& mimetype,
                   const std::string& title, Hints hints, const std::string& content)
          : BasicItem(path, mimetype, title, hints),
            content(content)
        {}
    };

    class LIBZIM_API FileItem : public BasicItem
    {
      public:
        FileItem(const std::string& path, const std::string& mimetype,
                 const std::string& title, Hints hints, const std::string& filepath)
          : BasicItem(path, mimetype, title, hints),
            filepath(filepath)
        {}

        std::unique_ptr<ContentProvider> getContentProvider() const;

      protected:
        std::string filepath;
    };

  }
}

#endif // ZIM_WRITER_ITEM_H