Changeset View
Changeset View
Standalone View
Standalone View
src/qtquick/ArchiveBookModel.cpp
Context not available. | |||||
30 | #include <AcbfPage.h> | 30 | #include <AcbfPage.h> | ||
---|---|---|---|---|---|
31 | #include <AcbfPublishinfo.h> | 31 | #include <AcbfPublishinfo.h> | ||
32 | #include <AcbfDocumentinfo.h> | 32 | #include <AcbfDocumentinfo.h> | ||
33 | #include <AcbfTextlayer.h> | ||||
34 | #include <AcbfTextarea.h> | ||||
33 | 35 | | |||
34 | #include <QCoreApplication> | 36 | #include <QCoreApplication> | ||
35 | #include <QDebug> | 37 | #include <QDebug> | ||
Context not available. | |||||
1251 | } | 1253 | } | ||
1252 | return false; | 1254 | return false; | ||
1253 | } | 1255 | } | ||
1256 | | ||||
1257 | bool ArchiveBookModel::generatePot(const QString fileName, const QString language) | ||||
1258 | { | ||||
1259 | QFile file(fileName); | ||||
1260 | qDebug() << fileName; | ||||
1261 | bool success = false; | ||||
1262 | | ||||
1263 | if (file.open(QFile::WriteOnly| QFile::Truncate) && acbfData()) { | ||||
1264 | AdvancedComicBookFormat::Document* acbf = qobject_cast<AdvancedComicBookFormat::Document*>(acbfData()); | ||||
1265 | QTextStream pot(&file); | ||||
1266 | QString quote = "\""; | ||||
1267 | QString newLine = "\n"; | ||||
1268 | | ||||
1269 | pot << "msid " << quote+quote+newLine; | ||||
1270 | pot << "msgstr " << quote+quote+newLine; | ||||
1271 | | ||||
1272 | pot << quote << "POT-Creation-Date: " << QDateTime::currentDateTimeUtc().toString() << "\\n" << quote << newLine; | ||||
1273 | pot << quote << "Content-Type: text/plain; charset=UTF-8\\n" << quote << newLine; | ||||
1274 | pot << quote << "Content-Transfer-Encoding: 8bit\\n" << quote << newLine; | ||||
1275 | pot << quote << "X-Generator: Peruse Creator\\n" << quote << newLine; | ||||
1276 | | ||||
1277 | pot << newLine; | ||||
1278 | pot << "#. Title of the work" << newLine; | ||||
1279 | pot << "msgctxt \"@meta-title\"" << newLine; | ||||
1280 | pot << "msgid " << quote << acbf->metaData()->bookInfo()->title(language) << quote << newLine; | ||||
1281 | pot << "msgstr " << quote << quote << newLine; | ||||
1282 | pot << newLine; | ||||
1283 | | ||||
1284 | pot << "#. The summary" << newLine; | ||||
1285 | pot << "msgctxt \"@meta-summary\"" << newLine; | ||||
1286 | pot << "msgid " << quote << quote << newLine; | ||||
1287 | for (int i =0; i < acbf->metaData()->bookInfo()->annotation(language).size(); i++) { | ||||
1288 | QString paragraph = acbf->metaData()->bookInfo()->annotation(language).at(i); | ||||
1289 | paragraph.replace(quote, "\\\""); | ||||
1290 | paragraph.replace("\'", "\\\'"); | ||||
1291 | paragraph.replace("#", "\\#"); | ||||
1292 | pot << quote << "<p>" << paragraph << "</p>" << quote << newLine; | ||||
1293 | } | ||||
1294 | pot << "msgstr " << quote << quote << newLine; | ||||
1295 | pot << newLine; | ||||
1296 | | ||||
1297 | pot << "#. The keywords, these need to be comma separated." + newLine; | ||||
1298 | pot << "msgctxt \"@meta-keywords\"" << newLine; | ||||
1299 | pot << "msgid " << quote << acbf->metaData()->bookInfo()->keywords(language).join(", ") << quote << newLine; | ||||
1300 | pot << "msgstr " << quote << quote << newLine; | ||||
1301 | pot << newLine; | ||||
1302 | | ||||
1303 | // cover. | ||||
1304 | AdvancedComicBookFormat::Textlayer* textlayer = acbf->metaData()->bookInfo()->coverpage()->textLayer(language); | ||||
1305 | | ||||
1306 | for (int t = 0; t < textlayer->textareas().size(); t++) { | ||||
1307 | AdvancedComicBookFormat::Textarea* textarea = textlayer->textarea(t); | ||||
1308 | | ||||
1309 | pot << newLine; | ||||
1310 | pot << "msgid " << quote << quote << newLine; | ||||
1311 | for (int i =0; i < textarea->paragraphs().size(); i++) { | ||||
1312 | QString paragraph = textarea->paragraphs().at(i); | ||||
1313 | paragraph.replace(quote, "\\\""); | ||||
1314 | paragraph.replace("\'", "\\\'"); | ||||
1315 | paragraph.replace("#", "\\#"); | ||||
1316 | pot << quote << "<p>" << paragraph << "</p>" << quote << newLine; | ||||
1317 | } | ||||
1318 | pot << "msgstr " << quote << quote << newLine; | ||||
1319 | pot << newLine; | ||||
1320 | } | ||||
1321 | | ||||
1322 | // pages. | ||||
1323 | for (int p = 0; p < acbf->body()->pageCount(); p++) { | ||||
1324 | textlayer = acbf->body()->page(p)->textLayer(language); | ||||
1325 | | ||||
1326 | if (!acbf->body()->page(p)->title("").isEmpty()) { | ||||
1327 | pot << "#. Page title" << newLine; | ||||
1328 | pot << "msgid " << quote << acbf->body()->page(p)->title("") << quote << newLine; | ||||
1329 | pot << "msgstr " << quote << quote << newLine; | ||||
1330 | pot << newLine; | ||||
1331 | } | ||||
1332 | | ||||
1333 | for (int t = 0; t < textlayer->textareas().size(); t++) { | ||||
1334 | AdvancedComicBookFormat::Textarea* textarea = textlayer->textarea(t); | ||||
1335 | | ||||
1336 | pot << newLine; | ||||
1337 | pot << "msgid " << quote << quote << newLine; | ||||
1338 | for (int i =0; i < textarea->paragraphs().size(); i++) { | ||||
1339 | QString paragraph = textarea->paragraphs().at(i); | ||||
1340 | paragraph.replace(quote, "\\\""); | ||||
1341 | paragraph.replace("\'", "\\\'"); | ||||
1342 | paragraph.replace("#", "\\#"); | ||||
1343 | pot << quote << "<p>" << paragraph << "</p>" << quote << newLine; | ||||
1344 | } | ||||
1345 | pot << "msgstr " << quote << quote << newLine; | ||||
1346 | pot << newLine; | ||||
1347 | } | ||||
1348 | } | ||||
1349 | success = true; | ||||
1350 | } | ||||
1351 | file.close(); | ||||
1352 | return success; | ||||
1353 | } | ||||
1354 | | ||||
1355 | bool ArchiveBookModel::readPoFile(const QString fileName, bool addTranslatorEmail) | ||||
1356 | { | ||||
1357 | QFile file(fileName); | ||||
1358 | bool success = false; | ||||
1359 | QString language; | ||||
1360 | QString translator; | ||||
1361 | QString multiline; | ||||
1362 | QString context; | ||||
1363 | QString id; | ||||
1364 | QString translation; | ||||
1365 | QHash<QString, QString> table; | ||||
1366 | | ||||
1367 | if (file.open(QFile::ReadOnly|QFile::Text)) { | ||||
1368 | QTextStream po(&file); | ||||
1369 | QString line; | ||||
1370 | while (po.readLineInto(&line)) { | ||||
1371 | if (line.startsWith("\"Last-Translator: ")) | ||||
1372 | { | ||||
1373 | line.remove("\"Last-Translator: "); | ||||
1374 | translator = line.replace("\\n\"", ""); | ||||
1375 | } | ||||
1376 | | ||||
1377 | else if (line.startsWith("\"Language: ")) | ||||
1378 | { | ||||
1379 | line.remove("\"Language: "); | ||||
1380 | language = line.replace("\\n\"", ""); | ||||
1381 | } | ||||
1382 | | ||||
1383 | else if (line.startsWith("msgctxt")) | ||||
1384 | { | ||||
1385 | if (!line.endsWith("\"\"")) { | ||||
1386 | line.remove("msgctxt \""); | ||||
1387 | context = line.replace("\"", ""); | ||||
1388 | } | ||||
1389 | } | ||||
1390 | | ||||
1391 | else if (line.startsWith("msgid")) | ||||
1392 | { | ||||
1393 | if (!multiline.isEmpty()) { | ||||
1394 | context.append(multiline); | ||||
1395 | multiline.clear(); | ||||
1396 | } | ||||
1397 | if (!line.endsWith("\"\"")) { | ||||
1398 | line.remove("msgid \""); | ||||
1399 | id = line.replace("\"", ""); | ||||
1400 | } | ||||
1401 | } | ||||
1402 | | ||||
1403 | else if (line.startsWith("msgstr")) | ||||
1404 | { | ||||
1405 | if (!multiline.isEmpty()) { | ||||
1406 | id.append(multiline); | ||||
1407 | multiline.clear(); | ||||
1408 | } | ||||
1409 | if (!line.endsWith("\"\"")) { | ||||
1410 | line.remove("msgstr \""); | ||||
1411 | translation = line.replace("\"", ""); | ||||
1412 | } | ||||
1413 | } | ||||
1414 | | ||||
1415 | else if (line.startsWith("\"")) | ||||
1416 | { | ||||
1417 | if (!line.endsWith("\"\"")) { | ||||
1418 | line.remove("\""); | ||||
1419 | multiline.append(line.replace("\"", "")); | ||||
1420 | } | ||||
1421 | } | ||||
1422 | | ||||
1423 | else if (line.isEmpty()) { | ||||
1424 | translation.append(multiline); | ||||
1425 | multiline.clear(); | ||||
1426 | if (!id.isEmpty()) { | ||||
1427 | if (context.isEmpty()) { | ||||
1428 | table.insert(id, translation); | ||||
1429 | } else { | ||||
1430 | table.insert(context, translation); | ||||
1431 | } | ||||
1432 | } | ||||
1433 | context.clear(); | ||||
1434 | id.clear(); | ||||
1435 | translation.clear(); | ||||
1436 | } else { | ||||
1437 | multiline.clear(); | ||||
1438 | context.clear(); | ||||
1439 | id.clear(); | ||||
1440 | translation.clear(); | ||||
1441 | } | ||||
1442 | | ||||
1443 | } | ||||
1444 | | ||||
1445 | AdvancedComicBookFormat::Document* acbf = qobject_cast<AdvancedComicBookFormat::Document*>(acbfData()); | ||||
1446 | if (!acbf->metaData()->bookInfo()->languageEntryList().contains(language)) { | ||||
1447 | acbf->metaData()->bookInfo()->addLanguage(language); | ||||
1448 | } | ||||
1449 | if (!table.value("@meta-title").isEmpty()) { | ||||
1450 | acbf->metaData()->bookInfo()->setTitle(table.value("@meta-title"), language); | ||||
1451 | qDebug() << Q_FUNC_INFO << "adding translation" << language << acbf->metaData()->bookInfo()->title(language); | ||||
1452 | } | ||||
1453 | if (!table.value("@meta-summary").isEmpty()) { | ||||
1454 | QStringList paragraphs = table.value("@meta-summary").split("</p>", QString::SkipEmptyParts); | ||||
1455 | for (int i=0; i < paragraphs.size(); i++) { | ||||
1456 | QString p = paragraphs.at(i); | ||||
1457 | p.replace("\\\"", "\""); | ||||
1458 | p.replace("\\\'", "\'"); | ||||
1459 | p.replace("\\#", "#"); | ||||
1460 | paragraphs.replace(i, p.replace("<p>", "").trimmed()); | ||||
1461 | } | ||||
1462 | acbf->metaData()->bookInfo()->setAnnotation(paragraphs, language); | ||||
1463 | qDebug() << Q_FUNC_INFO << "adding translation" << language << paragraphs; | ||||
1464 | } | ||||
1465 | if (!table.value("@meta-keywords").isEmpty()) { | ||||
1466 | QStringList keys = table.value("@meta-keywords").split(",", QString::SkipEmptyParts); | ||||
1467 | for (int i=0; i < keys.size(); i++) { | ||||
1468 | QString p = keys.at(i); | ||||
1469 | keys.replace(i, p.trimmed()); | ||||
1470 | } | ||||
1471 | qDebug() << Q_FUNC_INFO << "adding translation" << language << keys; | ||||
1472 | acbf->metaData()->bookInfo()->setKeywords(keys, language); | ||||
1473 | } | ||||
1474 | | ||||
1475 | // cover | ||||
1476 | acbf->metaData()->bookInfo()->coverpage()->duplicateTextLayer("", language); | ||||
1477 | AdvancedComicBookFormat::Textlayer* translation = acbf->metaData()->bookInfo()->coverpage()->textLayer(language); | ||||
1478 | for (int t=0; t < translation->textareaPointStrings().size(); t++) { | ||||
1479 | QString key; | ||||
1480 | for (int i =0; i < translation->textarea(t)->paragraphs().size(); i++) { | ||||
1481 | QString paragraph = translation->textarea(t)->paragraphs().at(i); | ||||
1482 | paragraph.replace("\"", "\\\""); | ||||
1483 | paragraph.replace("\'", "\\\'"); | ||||
1484 | paragraph.replace("#", "\\#"); | ||||
1485 | key.append(paragraph); | ||||
1486 | } | ||||
1487 | QStringList paragraphs = table.value("<p>"+key+"</p>").split("</p>", QString::SkipEmptyParts); | ||||
1488 | for (int i=0; i < paragraphs.size(); i++) { | ||||
1489 | QString p = paragraphs.at(i); | ||||
1490 | p.replace("\\\"", "\""); | ||||
1491 | p.replace("\\\'", "\'"); | ||||
1492 | p.replace("\\#", "#"); | ||||
1493 | paragraphs.replace(i, p.replace("<p>", "").trimmed()); | ||||
1494 | } | ||||
1495 | translation->textarea(t)->setParagraphs(paragraphs); | ||||
1496 | if (!paragraphs.isEmpty()) { | ||||
1497 | qDebug() << Q_FUNC_INFO << "adding translation" << language << translation->textarea(t)->paragraphs(); | ||||
1498 | } | ||||
1499 | } | ||||
1500 | // pages | ||||
1501 | for (int p = 0; p < acbf->body()->pageCount(); p++) { | ||||
1502 | acbf->body()->page(p)->duplicateTextLayer("", language); | ||||
1503 | if (!table.value(acbf->body()->page(p)->title("")).isEmpty()) { | ||||
1504 | acbf->body()->page(p)->setTitle(table.value(acbf->body()->page(p)->title("")), language); | ||||
1505 | qDebug() << Q_FUNC_INFO << "adding translation" << language << acbf->body()->page(p)->title(language); | ||||
1506 | } | ||||
1507 | AdvancedComicBookFormat::Textlayer* translation = acbf->body()->page(p)->textLayer(language); | ||||
1508 | for (int t=0; t < translation->textareaPointStrings().size(); t++) { | ||||
1509 | QString key; | ||||
1510 | for (int i =0; i < translation->textarea(t)->paragraphs().size(); i++) { | ||||
1511 | QString paragraph = translation->textarea(t)->paragraphs().at(i); | ||||
1512 | paragraph.replace("\"", "\\\""); | ||||
1513 | paragraph.replace("\'", "\\\'"); | ||||
1514 | paragraph.replace("#", "\\#"); | ||||
1515 | key.append(paragraph); | ||||
1516 | } | ||||
1517 | QStringList paragraphs = table.value("<p>"+key+"</p>").split("</p>", QString::SkipEmptyParts); | ||||
1518 | for (int i=0; i < paragraphs.size(); i++) { | ||||
1519 | QString p = paragraphs.at(i); | ||||
1520 | p.replace("\\\"", "\""); | ||||
1521 | p.replace("\\\'", "\'"); | ||||
1522 | p.replace("\\#", "#"); | ||||
1523 | paragraphs.replace(i, p.replace("<p>", "").trimmed()); | ||||
1524 | } | ||||
1525 | translation->textarea(t)->setParagraphs(paragraphs); | ||||
1526 | if (!paragraphs.isEmpty()) { | ||||
1527 | qDebug() << Q_FUNC_INFO << "adding translation" << language << translation->textarea(t)->paragraphs(); | ||||
1528 | } | ||||
1529 | } | ||||
1530 | } | ||||
1531 | // Finally, add translator. | ||||
1532 | QStringList emails; | ||||
1533 | if (addTranslatorEmail) { | ||||
1534 | QString email = translator.split("<").at(1); | ||||
1535 | email.replace(">", ""); | ||||
1536 | emails.append(email); | ||||
1537 | } | ||||
1538 | translator = translator.split("<").at(0); | ||||
1539 | qDebug() << Q_FUNC_INFO << "adding translator" << translator << emails; | ||||
1540 | | ||||
1541 | if (acbf->metaData()->bookInfo()->authorNames().indexOf(translator) < 0) { | ||||
1542 | acbf->metaData()->bookInfo()->addAuthor("Translator", language, "", "", "", translator, QStringList(), emails); | ||||
1543 | } | ||||
1544 | | ||||
1545 | } | ||||
1546 | file.close(); | ||||
1547 | return success; | ||||
1548 | } | ||||
1549 | | ||||
1550 | QStringList ArchiveBookModel::readPoFileSummary(const QString fileName) | ||||
1551 | { | ||||
1552 | QFile file(fileName); | ||||
1553 | QString translator; | ||||
1554 | QString language; | ||||
1555 | if (file.open(QFile::ReadOnly|QFile::Text)) { | ||||
1556 | QTextStream po(&file); | ||||
1557 | QString line; | ||||
1558 | while (po.readLineInto(&line) && (translator.isEmpty() || language.isEmpty())) { | ||||
1559 | if (line.startsWith("\"Last-Translator: ")) | ||||
1560 | { | ||||
1561 | line.remove("\"Last-Translator: "); | ||||
1562 | translator = line.replace("\\n\"", ""); | ||||
1563 | } | ||||
1564 | | ||||
1565 | else if (line.startsWith("\"Language: ")) | ||||
1566 | { | ||||
1567 | line.remove("\"Language: "); | ||||
1568 | language = line.replace("\\n\"", ""); | ||||
1569 | } | ||||
1570 | } | ||||
1571 | } | ||||
1572 | QStringList strings; | ||||
1573 | strings << language; | ||||
1574 | strings << translator; | ||||
1575 | return strings; | ||||
1576 | } | ||||
Context not available. |