File: manager.h

package info (click to toggle)
libkiwix 14.0.0-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,508 kB
  • sloc: cpp: 20,712; javascript: 5,743; python: 476; xml: 318; ansic: 84; sh: 24; makefile: 7
file content (179 lines) | stat: -rw-r--r-- 5,764 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
 * Copyright 2011 Emmanuel Engelhart <kelson@kiwix.org>
 *
 * 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 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 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 Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 */

#ifndef KIWIX_MANAGER_H
#define KIWIX_MANAGER_H

#include "book.h"
#include "library.h"

#include <string>
#include <vector>
#include <memory>

namespace pugi {
class xml_document;
}

namespace kiwix
{

class LibraryManipulator
{
 public: // functions
  explicit LibraryManipulator(LibraryPtr library);
  virtual ~LibraryManipulator();

  LibraryPtr getLibrary() const { return library; }

  bool addBookToLibrary(const Book& book);
  void addBookmarkToLibrary(const Bookmark& bookmark);
  uint32_t removeBooksNotUpdatedSince(Library::Revision rev);

 protected: // overrides
  virtual void bookWasAddedToLibrary(const Book& book);
  virtual void bookmarkWasAddedToLibrary(const Bookmark& bookmark);
  virtual void booksWereRemovedFromLibrary();

 private: // data
  LibraryPtr library;
};

/**
 * A tool to manage a `Library`.
 */
class Manager
{
 public: // types
  typedef std::vector<std::string> Paths;

 public: // functions
  explicit Manager(LibraryManipulator manipulator);
  explicit Manager(LibraryPtr library);

  /**
   * Read a `library.xml` and add book in the file to the library.
   *
   * @param path The (utf8) path to the `library.xml`.
   * @param readOnly Set if the libray path could be overwritten latter with
   *                 updated content.
   * @param trustLibrary use book metadata coming from XML.
   * @return True if file has been properly parsed.
   */
  bool readFile(const std::string& path, bool readOnly = true, bool trustLibrary = true);

  /**
   * Sync the contents of the library with one or more `library.xml` files.
   *
   * The metadata of the library files is trusted unconditionally.
   * Any books not present in the input library.xml files are removed
   * from the library.
   *
   * @param paths The (utf8) paths to the `library.xml` files.
   */
  void reload(const Paths& paths);

  /**
   * Load a library content store in the string.
   *
   * @param xml The content corresponding of the library xml
   * @param readOnly Set if the libray path could be overwritten latter with
   *                 updated content.
   * @param libraryPath The library path (used to resolve relative path)
   * @return True if the content has been properly parsed.
   */
  bool readXml(const std::string& xml,
               const bool readOnly = true,
               const std::string& libraryPath = "",
               bool trustLibrary = true);

  /**
   * Load a library content stored in a OPDS stream.
   *
   * @param content The content of the OPDS stream.
   * @param readOnly Set if the library path could be overwritten later with
   *                 updated content.
   * @param libraryPath The library path (used to resolve relative path)
   * @return True if the content has been properly parsed.
   */
  bool readOpds(const std::string& content, const std::string& urlHost);


  /**
   * Load a bookmark file.
   *
   * @param path The path of the file to read.
   * @return True if the content has been properly parsed.
   */
  bool readBookmarkFile(const std::string& path);

  /**
   * Add a book to the library.
   *
   * @param pathToOpen The path to the zim file to add.
   * @param pathToSave The path to store in the library in place of pathToOpen.
   * @param url        The url of the book to store in the library.
   * @param checMetaData Tell if we check metadata before adding book to the
   *                     library.
   * @return The id of the book if the book has been added to the library.
   *         Else, an empty string.
   */
  std::string addBookFromPathAndGetId(const std::string& pathToOpen,
                                 const std::string& pathToSave = "",
                                 const std::string& url = "",
                                 const bool checkMetaData = false);

  /**
   * Add a book to the library.
   *
   * @param pathToOpen The path to the zim file to add.
   * @param pathToSave The path to store in the library in place of pathToOpen.
   * @param url        The url of the book to store in the library.
   * @param checMetaData Tell if we check metadata before adding book to the
   *                     library.
   * @return True if the book has been added to the library.
   */

  bool addBookFromPath(const std::string& pathToOpen,
                       const std::string& pathToSave = "",
                       const std::string& url = "",
                       const bool checkMetaData = false);

  std::string writableLibraryPath;

  bool m_hasSearchResult = false;
  uint64_t m_totalBooks = 0;
  uint64_t m_startIndex = 0;
  uint64_t m_itemsPerPage = 0;

 protected:
  kiwix::LibraryManipulator manipulator;

  bool readBookFromPath(const std::string& path, Book* book);
  bool parseXmlDom(const pugi::xml_document& doc,
                   bool readOnly,
                   const std::string& libraryPath,
                   bool trustLibrary);
  bool parseOpdsDom(const pugi::xml_document& doc,
                    const std::string& urlHost);

};
}

#endif