-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstaller.cpp
More file actions
269 lines (196 loc) · 8.48 KB
/
Copy pathinstaller.cpp
File metadata and controls
269 lines (196 loc) · 8.48 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#include "installer.hpp"
#include <iostream>
#include <fstream>
#include <boost/filesystem.hpp>
//#include <opencv2/highgui/highgui.hpp>
//#include <opencv2/imgproc/imgproc.hpp>
#include <QtCore>
#include <QtGui>
#include <QApplication>
#include <QtGui/QGridLayout>
#include "config.hpp"
#include "configparser.hpp"
#include "utils.hpp"
#include "data.hpp"
#include "book.hpp"
#include "author.hpp"
#include "serie.hpp"
#include "format.hpp"
#include "booktable.hpp"
#include "renderer.hpp"
#include "filesystemutils.hpp"
Installer::Installer(QDialog* parent) :
_booktable(),
_books(_booktable.fetchAll()),
_metadata(),
_formats(),
_nformats(0),
_ratings(),
_spinner("Installation en cours...", "", 0, 0, parent)
{
// First, we create the destination directory if it doesn't exist yet
createDirectory(Config::get("catalog"));
_metadata["author"] = std::set<Data*>();
_metadata["tag"] = std::set<Data*>();
_metadata["publisher"] = std::set<Data*>();
_metadata["series"] = std::set<Data*>();
for(Data const* data: _books) {
// For each book, we retrieve metadata related to the book
Book* book = static_cast<Book*>(_booktable.fetch(data->id()));
_metadata["author"].insert(book->authors().begin(), book->authors().end());
_metadata["tag"].insert(book->tags().begin(), book->tags().end());
_metadata["publisher"].insert(book->publishers().begin(), book->publishers().end());
_metadata["series"].insert(book->series().begin(), book->series().end());
for(Data const* data: book->formats()) {
// A book can have several formats (epub, pdf, ...) ; we add each book's format to its list
Format const* format = static_cast<Format const*>(data);
_formats[format->format()].insert(format->books().begin(), format->books().end());
}
// TODO find a better way to do it
int rating = book->rating()/2;
std::string ratingStr;
if(book->rating() >= 0)
ratingStr = std::to_string(rating) + Utils::plural(" star", rating);
_ratings[ratingStr].insert(book);
// Usefull for copying data later
_nformats += book->formats().size();
}
_spinner.setCancelButton(nullptr);
_spinner.setRange(0, _nformats);
_spinner.setValue(0);
_spinner.setFixedSize(_spinner.size());
_spinner.setModal(true);
// Create HTML pages (for sections)
create("book", _books);
for(auto const& pair: _metadata)
create(pair.first, pair.second);
create("format", _formats);
create("rating", _ratings);
create("search");
// Create HTML pages (for each book details)
for(Data const* data: _books)
create(data);
// Create HTML pages (we list all books for each metadata)
for(auto const& pair: _metadata)
for(Data const* data: pair.second) {
Metadata const* metadata = static_cast<Metadata const*>(data);
if(pair.first == "series") {
std::set<Data*, comparator_series> books;
books.insert(metadata->books().begin(), metadata->books().end());
create(data, books);
} else
create(data, metadata->books());
}
createHome();
FileSystemUtils::copyDirectory(Config::get("template"), Config::get("catalog") + "template/", true, false);
FileSystemUtils::copyDirectory(Config::get("template") + "css/", Config::get("catalog") + "css/", true, false);
FileSystemUtils::copyFile(Config::get("template"), Config::get("catalog"), "bower.json", true);
FileSystemUtils::copyFile(".", Config::get("catalog") + "search/", "search.cgi", true);
FileSystemUtils::copyFile(Config::get("database"), Config::get("catalog") + "metadata.db", true);
copy(_books);
std::string catalog = Config::get("catalog");
std::string items_per_page = Config::get("items_per_page");
Config::clear();
Config::set("template", "../template/");
Config::set("database", "../metadata.db");
Config::set("items_per_page", items_per_page);
ConfigParser().write(catalog + "search/oook.conf");
}
void Installer::createHome()
{
std::map<std::string, int> items;
items["book"] = _books.size();
for(auto const& pair: _metadata)
items[pair.first] = pair.second.size();
items["format"] = _formats.size();
items["rating"] = _ratings.size();
std::ofstream html(Config::get("catalog") + "index.html");
html << Renderer::instance().header();
for(auto const& pair: items)
html << Renderer::instance().renderHomeItem(
pair.first,
pair.second
);
html << Renderer::instance().renderHomeItem("search");
html << Renderer::instance().footer();
html.close();
}
void Installer::create(std::string const& type, std::set<Data*> const& dataset)
{
std::string dir(Config::get("catalog") + type + "/");
createDirectory(dir);
createPagination(dataset, type, dir);
// icon
FileSystemUtils::copyFile(Config::get("template") + "icon/" + type + ".png", dir + "icon.png", true);
}
void Installer::create(std::string const& type, std::map<std::string, std::set<Data*>> const& dataset)
{
std::string dir(Config::get("catalog") + type + "/");
createDirectory(dir);
std::ofstream html(dir + "index.html");
html << Renderer::instance().renderSection(dataset, type, "", "../");
html.close();
int i = 0;
for(auto const& pair: dataset) {
std::string subdir(dir + std::to_string(i) + "/");
createDirectory(subdir);
createPagination(pair.second, type, subdir, "../../", Utils::titlize(pair.first));
++i;
}
// icon
FileSystemUtils::copyFile(Config::get("template") + "icon/" + type + ".png", dir + "icon.png", true);
}
void Installer::create(Data const* data) {
std::string dir = createDirectory(data);
std::ofstream html(dir + "index.html");
html << data->render();
html.close();
}
void Installer::createDirectory(std::string const& dir) {
boost::filesystem::create_directory(dir);
if(!boost::filesystem::is_directory(dir))
throw std::runtime_error("Error: can't create directory " + dir);
}
std::string Installer::createDirectory(Data const* data) {
std::string dir(Config::get("catalog") + data->type() + "/" + std::to_string(data->id()) + "/");
createDirectory(dir);
return dir;
}
void Installer::copy(std::set<Data*> const& books)
{
std::vector<std::string> mediumSize = Utils::split('x', Config::get("cover_size"));
int mediumSizeL = std::stoi(mediumSize.at(0));
int mediumSizeH = std::stoi(mediumSize.at(1));
std::vector<std::string> thumbnailSize = Utils::split('x', Config::get("thumbnail_size"));
int thumbnailSizeL = std::stoi(thumbnailSize.at(0));
int thumbnailSizeH = std::stoi(thumbnailSize.at(1));
std::size_t i = 0;
for(Data const* data: books) {
Book const* book = static_cast<Book const*>(data);
std::string src = Config::get("calibre") + "/" + book->path() + "/";
std::string dst = Config::get("catalog") + "book/" + std::to_string(book->id()) + "/";
// copy ebooks
for(Data const* data: book->formats()) {
Format const* format = static_cast<Format const*>(data);
++i;
_spinner.setValue(i);
QApplication::processEvents();
try {
FileSystemUtils::copyFile(src, dst, format->name() + "." + format->format(), false);
} catch(std::exception const& e) { }
}
// copy cover
try {
FileSystemUtils::copyFile(src, dst, "cover.jpg", false);
QImage cover((dst + "cover.jpg").c_str());
QSize medium_size(mediumSizeL, mediumSizeH);
QSize thumbnail_size(thumbnailSizeL, thumbnailSizeH);
QImage medium = cover.scaled(medium_size, Qt::KeepAspectRatio);
QImage thumbnail = cover.scaled(thumbnail_size, Qt::KeepAspectRatio);
medium.save((dst + "medium.jpg").c_str());
thumbnail.save((dst + "thumbnail.jpg").c_str());
} catch(std::exception const& e) { }
}
if(_nformats > 0)
std::cout << "Copied " << i << "/" << _nformats << " ebooks." << std::endl;
}