| 1 |
/* |
| 2 |
* This file is part of the apvlv package |
| 3 |
* Copyright (C) <2008> <Alf> |
| 4 |
* |
| 5 |
* Contact: Alf <[email protected]> |
| 6 |
* |
| 7 |
* This program is free software; you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU General Public License as published by |
| 9 |
* the Free Software Foundation; either version 2 of the License, or |
| 10 |
* (at your option) any later version. |
| 11 |
* |
| 12 |
* This program is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License along |
| 18 |
* with this program; if not, write to the Free Software Foundation, Inc., |
| 19 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 |
* |
| 21 |
*/ |
| 22 |
/* @CPPFILE File.cc |
| 23 |
* |
| 24 |
* Author: Alf <[email protected]> |
| 25 |
*/ |
| 26 |
|
| 27 |
#include <QBuffer> |
| 28 |
#include <algorithm> |
| 29 |
#include <filesystem> |
| 30 |
#include <functional> |
| 31 |
#include <iostream> |
| 32 |
#include <optional> |
| 33 |
#include <sstream> |
| 34 |
#include <utility> |
| 35 |
|
| 36 |
#include "ApvlvFile.h" |
| 37 |
#include "ApvlvUtil.h" |
| 38 |
#include "ApvlvWebViewWidget.h" |
| 39 |
|
| 40 |
namespace apvlv |
| 41 |
{ |
| 42 |
|
| 43 |
using namespace std; |
| 44 |
|
| 45 |
const string html_template = "<?xml version='1.0' encoding='UTF-8'?>\n" |
| 46 |
"<html xmlns=\"http://www.w3.org/1999/xhtml\" " |
| 47 |
"lang=\"en\" xml:lang=\"en\">\n" |
| 48 |
" <head>\n" |
| 49 |
" <title></title>\n" |
| 50 |
" <meta http-equiv=\"Content-Type\" " |
| 51 |
"content=\"text/html; charset=utf-8\"/>\n" |
| 52 |
" </head>\n" |
| 53 |
" <body>\n" |
| 54 |
" <div content>" |
| 55 |
" <image src=%s />" |
| 56 |
" </div>" |
| 57 |
" </body>\n" |
| 58 |
"</html>\n"; |
| 59 |
|
| 60 |
map<string, vector<string>> FileFactory::mSupportMimeTypes; |
| 61 |
map<string, FileFactory::ExtClassList> FileFactory::mSupportClass; |
| 62 |
|
| 63 |
const map<string, vector<string>> & |
| 64 |
FileFactory::supportMimeTypes () |
| 65 |
{ |
| 66 |
return mSupportMimeTypes; |
| 67 |
} |
| 68 |
|
| 69 |
vector<string> |
| 70 |
FileFactory::supportFileExts () |
| 71 |
{ |
| 72 |
unordered_set<string> extSet; |
| 73 |
for (const auto &pair : mSupportMimeTypes) |
| 74 |
{ |
| 75 |
extSet.insert (pair.second.begin (), pair.second.end ()); |
| 76 |
} |
| 77 |
vector<string> exts (extSet.begin (), extSet.end ()); |
| 78 |
return exts; |
| 79 |
} |
| 80 |
|
| 81 |
ostream & |
| 82 |
FileFactory::typeEngineDescription (ostream &os) |
| 83 |
{ |
| 84 |
os << "Engines: " << endl; |
| 85 |
for (auto &pair : mSupportClass) |
| 86 |
{ |
| 87 |
string ext = pair.first; |
| 88 |
string engines; |
| 89 |
std::ranges::for_each (pair.second, [&engines] (ExtClass &cls) { |
| 90 |
engines.append (" "); |
| 91 |
engines.append (cls.first); |
| 92 |
}); |
| 93 |
os << "\t" << pair.first << ":\t\t" << engines << endl; |
| 94 |
} |
| 95 |
os << endl; |
| 96 |
return os; |
| 97 |
} |
| 98 |
|
| 99 |
int |
| 100 |
FileFactory::registerClass (const string &name, const function<File *()> &fun, |
| 101 |
initializer_list<string> exts) |
| 102 |
{ |
| 103 |
mSupportMimeTypes.insert ({ name, exts }); |
| 104 |
std::ranges::for_each (exts, [=] (const string &t) { |
| 105 |
auto iter = mSupportClass.find (t); |
| 106 |
if (iter != mSupportClass.end ()) |
| 107 |
{ |
| 108 |
auto &cls_list = iter->second; |
| 109 |
cls_list.emplace_back (name, fun); |
| 110 |
} |
| 111 |
else |
| 112 |
{ |
| 113 |
auto cls_list = ExtClassList{ { name, fun } }; |
| 114 |
mSupportClass.insert ({ t, cls_list }); |
| 115 |
} |
| 116 |
}); |
| 117 |
return static_cast<int> (mSupportMimeTypes.size ()); |
| 118 |
} |
| 119 |
|
| 120 |
optional<FileFactory::ExtClass> |
| 121 |
FileFactory::findMatchClass (const std::string &filename) |
| 122 |
{ |
| 123 |
auto ext = filenameExtension (filename); |
| 124 |
if (ext.empty ()) |
| 125 |
return nullopt; |
| 126 |
|
| 127 |
if (mSupportClass.find (ext) == mSupportClass.end ()) |
| 128 |
return nullopt; |
| 129 |
|
| 130 |
auto cls_list = mSupportClass[ext]; |
| 131 |
if (cls_list.size () == 1) |
| 132 |
return cls_list[0]; |
| 133 |
|
| 134 |
auto cls_name |
| 135 |
= ApvlvParams::instance ()->getGroupStringOrDefault (ext, "engine", ""); |
| 136 |
if (cls_name.empty ()) |
| 137 |
return cls_list[0]; |
| 138 |
|
| 139 |
for (auto const &cls : cls_list) |
| 140 |
{ |
| 141 |
if (strcasecmp (cls.first.c_str (), cls_name.c_str ()) == 0) |
| 142 |
return cls; |
| 143 |
} |
| 144 |
|
| 145 |
return cls_list[0]; |
| 146 |
} |
| 147 |
|
| 148 |
unique_ptr<File> |
| 149 |
FileFactory::loadFile (const string &filename) |
| 150 |
{ |
| 151 |
auto cls = findMatchClass (filename); |
| 152 |
if (!cls.has_value ()) |
| 153 |
{ |
| 154 |
qWarning () << "no engine to open " << filename; |
| 155 |
return nullptr; |
| 156 |
} |
| 157 |
|
| 158 |
auto file = unique_ptr<File> (cls->second ()); |
| 159 |
if (file->setFilename (filename)) |
| 160 |
return file; |
| 161 |
else |
| 162 |
{ |
| 163 |
qWarning () << "open " << QString::fromLocal8Bit (filename) << " error"; |
| 164 |
return nullptr; |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
File::~File () |
| 169 |
{ |
| 170 |
mPages.clear (); |
| 171 |
srcPages.clear (); |
| 172 |
srcMimeTypes.clear (); |
| 173 |
} |
| 174 |
|
| 175 |
unique_ptr<SearchFileMatch> |
| 176 |
File::grepFile (const string &seq, bool is_case, bool is_regex, |
| 177 |
atomic<bool> &is_abort) |
| 178 |
{ |
| 179 |
vector<SearchPageMatch> page_matches; |
| 180 |
auto pageSum = sum (); |
| 181 |
for (auto pn = 0; pn < pageSum; ++pn) |
| 182 |
{ |
| 183 |
if (is_abort.load () == true) |
| 184 |
{ |
| 185 |
qDebug () << "grep " << QString::fromLocal8Bit (mFilename) |
| 186 |
<< " abort before page " << pn; |
| 187 |
return nullptr; |
| 188 |
} |
| 189 |
|
| 190 |
auto size = pageSizeF (pn, 0); |
| 191 |
string content; |
| 192 |
if (pageText (pn, { 0, 0, size.width, size.height }, content) == false) |
| 193 |
continue; |
| 194 |
|
| 195 |
istringstream iss{ content }; |
| 196 |
string line; |
| 197 |
SearchMatchList matches; |
| 198 |
while (getline (iss, line)) |
| 199 |
{ |
| 200 |
if (is_abort.load () == true) |
| 201 |
{ |
| 202 |
qDebug () << "grep " << QString::fromLocal8Bit (mFilename) |
| 203 |
<< " abort at page " << pn; |
| 204 |
return nullptr; |
| 205 |
} |
| 206 |
|
| 207 |
auto founds = apvlv::grep (line, seq, is_case, is_regex); |
| 208 |
for (auto const &found : founds) |
| 209 |
{ |
| 210 |
SearchMatch match{ line.substr (found.first, found.second), line, |
| 211 |
found.first, found.second }; |
| 212 |
matches.push_back (match); |
| 213 |
} |
| 214 |
} |
| 215 |
if (!matches.empty ()) |
| 216 |
{ |
| 217 |
page_matches.push_back ({ pn, matches }); |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
if (page_matches.empty ()) |
| 222 |
return nullptr; |
| 223 |
|
| 224 |
auto file_match = make_unique<SearchFileMatch> (); |
| 225 |
file_match->filename = getFilename (); |
| 226 |
file_match->page_matches = std::move (page_matches); |
| 227 |
return file_match; |
| 228 |
} |
| 229 |
|
| 230 |
bool |
| 231 |
File::pageRenderToImage (int pn, double zm, int rot, QImage *img) |
| 232 |
{ |
| 233 |
return false; |
| 234 |
} |
| 235 |
|
| 236 |
bool |
| 237 |
File::pageRenderToWebView (int pn, double zm, int rot, WebView *webview) |
| 238 |
{ |
| 239 |
webview->setZoomFactor (zm); |
| 240 |
QUrl pdfuri = QString ("apvlv:///%1-%2-%3-%4.html") |
| 241 |
.arg (pn) |
| 242 |
.arg (zm) |
| 243 |
.arg (rot) |
| 244 |
.arg (rand ()); |
| 245 |
webview->load (pdfuri); |
| 246 |
return true; |
| 247 |
} |
| 248 |
|
| 249 |
string |
| 250 |
File::pathMimeType (const string &path) |
| 251 |
{ |
| 252 |
if (srcMimeTypes.find (path) != srcMimeTypes.end ()) |
| 253 |
return srcMimeTypes[path]; |
| 254 |
else if (QString::fromLocal8Bit (path).endsWith (".png")) |
| 255 |
return "image/png"; |
| 256 |
else |
| 257 |
return "text/html"; |
| 258 |
} |
| 259 |
|
| 260 |
int |
| 261 |
File::pathPageNumber (const string &path) |
| 262 |
{ |
| 263 |
if (srcPages.find (path) != srcPages.end ()) |
| 264 |
return srcPages[path]; |
| 265 |
return -1; |
| 266 |
} |
| 267 |
|
| 268 |
optional<QByteArray> |
| 269 |
File::pathContent (const string &path) |
| 270 |
{ |
| 271 |
auto words = QString::fromLocal8Bit (path).split ("-"); |
| 272 |
int pn = words[0].toInt (); |
| 273 |
double zm = words[1].toDouble (); |
| 274 |
int rot = words[2].toInt (); |
| 275 |
|
| 276 |
if (QString::fromLocal8Bit (path).endsWith (".html")) |
| 277 |
return pathContentHtml (pn, zm, rot); |
| 278 |
else |
| 279 |
return pathContentPng (pn, zm, rot); |
| 280 |
} |
| 281 |
|
| 282 |
optional<QByteArray> |
| 283 |
File::pathContentHtml (int pn, double zm, int rot) |
| 284 |
{ |
| 285 |
auto src = QString ("apvlv:///%1-%2-%3-%4.png") |
| 286 |
.arg (pn) |
| 287 |
.arg (zm) |
| 288 |
.arg (rot) |
| 289 |
.arg (rand ()); |
| 290 |
auto html = QString::asprintf (html_template.c_str (), |
| 291 |
src.toStdString ().c_str ()); |
| 292 |
return QByteArray::fromStdString (html.toStdString ()); |
| 293 |
} |
| 294 |
|
| 295 |
optional<QByteArray> |
| 296 |
File::pathContentPng (int pn, double zm, int rot) |
| 297 |
{ |
| 298 |
QImage image; |
| 299 |
if (pageRenderToImage (pn, zm, rot, &image) == false) |
| 300 |
return nullopt; |
| 301 |
|
| 302 |
QByteArray array; |
| 303 |
QBuffer buffer (&array); |
| 304 |
buffer.open (QIODevice::WriteOnly); |
| 305 |
image.save (&buffer, "PNG"); |
| 306 |
buffer.close (); |
| 307 |
return array; |
| 308 |
} |
| 309 |
|
| 310 |
} |
| 311 |
|
| 312 |
// Local Variables: |
| 313 |
// mode: c++ |
| 314 |
// End: |