#define QT_USE_QSTRINGBUILDER
#include "utilites.h"

#include <QFileInfo>
#include <QDir>
#include <QSettings>
#include <QSqlQuery>
#include <QNetworkProxy>
#include <QTranslator>
#include <QTextCodec>
#include <QLibraryInfo>
#include <QProcess>

#include "options.h"
#include "config-freelib.h"

QString RelativeToAbsolutePath(QString path)
{
    if(QDir(path).isRelative() && path.indexOf(QLatin1String("%"))<0 && !path.startsWith(QLatin1String("mtp:/")))
    {
        return QApplication::applicationDirPath() + QStringLiteral("/") + path;
    }
    return path;
}

void creatDB(const QString &sFileDB)
{
    QDir dir;
    QFile file;
    dir.mkpath(QFileInfo(sFileDB).absolutePath());
    file.setFileName(QStringLiteral(":/freeLib.sqlite"));
    file.open(QFile::ReadOnly);
    QByteArray data = file.readAll();
    file.close();
    file.setFileName(sFileDB);
    file.open(QFile::WriteOnly);
    file.write(data);
    file.close();
}

bool openDB(const QString &sName)
{
    if(!QSqlDatabase::contains(sName))
        QSqlDatabase::addDatabase(u"QSQLITE"_s, sName);

    QString sAppDir,sFileDB;
    auto settings = GetSettings();

    QFileInfo fi(RelativeToAbsolutePath(g::options.sDatabasePath));
    if(fi.exists() && fi.isFile())
    {
        sFileDB = fi.canonicalFilePath();
    }
    else
    {
        sAppDir = QStandardPaths::standardLocations(QStandardPaths::AppDataLocation).constFirst();
        sFileDB = sAppDir + u"/freeLib.sqlite"_s;
        g::options.sDatabasePath = sFileDB;
        settings->setValue(u"database_path"_s, sFileDB);
    }
    QFile file(sFileDB);
    if(!file.exists())
        creatDB(sFileDB);
    QSqlDatabase dbase = QSqlDatabase::database(sName, false);
    dbase.setDatabaseName(sFileDB);
    if (!dbase.open())
    {
        qDebug() << ("Error connect! ") << sFileDB;
        return false;
    }
    QSqlQuery query(dbase);
    query.exec(u"SELECT value FROM params WHERE name='version'"_s);
    int version = 0;
    if(query.next())
    {
        version = query.value(0).toUInt();
    }
    if(version != 7){
        dbase.close();
        creatDB(sFileDB);
        if (!dbase.open())
        {
            qDebug() << ("Error connect! ")<<sFileDB;
            return false;
        }
    }
    query.exec(u"SELECT value FROM params WHERE name='version_minor'"_s);
    int nMinorVersion = 0;
    if(query.next())
    {
        nMinorVersion = query.value(0).toUInt();
    }
    if(nMinorVersion == 0){
        query.exec(QStringLiteral("ALTER TABLE 'favorite' RENAME TO 'tag';"));
        query.exec(QStringLiteral("ALTER TABLE 'tag' ADD COLUMN 'id_icon' INTEGER;"));
        query.exec(QStringLiteral("ALTER TABLE author RENAME COLUMN 'favorite' TO 'id_tag';"));
        query.exec(QStringLiteral("ALTER TABLE book RENAME COLUMN 'favorite' TO 'id_tag';"));
        query.exec(QStringLiteral("ALTER TABLE seria RENAME COLUMN 'favorite' TO 'id_tag';"));
        query.exec(QStringLiteral("ALTER TABLE 'janre' RENAME TO 'genre';"));
        query.exec(QStringLiteral("ALTER TABLE 'book_janre' RENAME TO 'book_genre';"));
        query.exec(QStringLiteral("ALTER TABLE book_genre RENAME COLUMN 'id_janre' TO 'id_genre';"));
        query.exec(QStringLiteral("CREATE TABLE 'icon' ('id' INTEGER NOT NULL,'dark_theme'	BLOB,'light_theme'	BLOB, PRIMARY KEY('id' AUTOINCREMENT));"));
        query.exec(QStringLiteral("CREATE TABLE 'tag' ('id'	INTEGER NOT NULL,'name'	TEXT,'id_icon'	INTEGER,  PRIMARY KEY('id'));"));
        query.exec(QStringLiteral("INSERT INTO 'icon' ('id','dark_theme','light_theme') VALUES (1,'<?xml version=\"1.0\" encoding=\"UTF-8\"?><svg enable-background=\"new 0 0 489.708 489.708\" version=\"1.1\" viewBox=\"0 0 489.71 489.71\" xml:space=\"preserve\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"m95.246 418.53h294.48c2.761 0 5 2.239 5 5s-2.239 5-5 5h-294.48c-2.761 0-5-2.239-5-5s2.238-5 5-5zm294.48 18.496h-294.48c-2.761 0-5 2.239-5 5s2.239 5 5 5h294.48c2.761 0 5-2.239 5-5s-2.24-5-5.001-5zm53.633-432.03v479.71c0 2.761-2.239 5-5 5h-337.38c-25.194 0-54.615-15.139-54.615-57.804v-386.75c0-24.897 20.256-45.153 45.153-45.153h346.85c2.761 0 5 2.239 5 5zm-10 474.71v-72.766h-15.79v54.922c0 2.761-2.239 5-5 5h-309.83c-19.277 0-34.961-15.684-34.961-34.961 0-19.278 15.684-34.961 34.961-34.961h330.62v-10.396h-339.07c-5.577 0-12.192 2.511-18.628 7.071-0.877 0.622-1.887 0.921-2.886 0.921-1.567 0-3.109-0.734-4.084-2.11-1.597-2.253-1.064-5.374 1.189-6.97 8.111-5.747 16.78-8.912 24.41-8.912h1.683v-12.229c0-2.761 2.239-5 5-5s5 2.239 5 5v12.229h327.38v-366.55h-327.39v324.3c0 2.761-2.239 5-5 5s-5-2.239-5-5v-324.3h-4.462c-19.384 0-35.153 15.77-35.153 35.153v386.75c0 45.5 37.165 47.804 44.615 47.804h332.38zm-330.62-72.766c-13.764 0-24.961 11.198-24.961 24.961s11.197 24.961 24.961 24.961h304.83v-49.922h-304.83zm64.132-253.74c1.364-4.198 5.097-6.91 9.511-6.91h59.656l18.435-56.737c1.364-4.198 5.097-6.91 9.511-6.91 4.413 0 8.146 2.712 9.511 6.91l18.435 56.737h59.657c4.414 0 8.146 2.712 9.511 6.91 1.364 4.197-0.062 8.586-3.633 11.18l-48.263 35.065 18.435 56.737c1.003 3.087 0.499 6.343-1.383 8.933-1.902 2.618-4.947 4.18-8.145 4.18-2.094 0-4.121-0.668-5.861-1.933l-48.264-35.065-48.263 35.065c-1.74 1.264-3.766 1.933-5.86 1.933-3.198 0-6.243-1.562-8.145-4.179-1.882-2.59-2.386-5.846-1.383-8.934l18.435-56.737-48.263-35.065c-3.572-2.594-4.998-6.983-3.634-11.18zm9.519 3.09 48.255 35.065c3.516 2.554 4.976 7.047 3.633 11.18l-18.433 56.729 48.261-35.057c1.72-1.25 3.752-1.91 5.878-1.91s4.158 0.66 5.877 1.909l48.258 35.062-18.429-56.733c-1.343-4.133 0.117-8.626 3.633-11.18l48.256-35.061-59.65-4e-3c-4.346 0-8.167-2.777-9.511-6.91l-18.432-56.73-18.438 56.729c-1.343 4.133-5.165 6.91-9.511 6.91h-59.647z\" fill=\"#fff\"/></svg>','<?xml version=\"1.0\" encoding=\"UTF-8\"?><svg enable-background=\"new 0 0 489.708 489.708\" version=\"1.1\" viewBox=\"0 0 489.71 489.71\" xml:space=\"preserve\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"m95.246 418.53h294.48c2.761 0 5 2.239 5 5s-2.239 5-5 5h-294.48c-2.761 0-5-2.239-5-5s2.238-5 5-5zm294.48 18.496h-294.48c-2.761 0-5 2.239-5 5s2.239 5 5 5h294.48c2.761 0 5-2.239 5-5s-2.24-5-5.001-5zm53.633-432.03v479.71c0 2.761-2.239 5-5 5h-337.38c-25.194 0-54.615-15.139-54.615-57.804v-386.75c0-24.897 20.256-45.153 45.153-45.153h346.85c2.761 0 5 2.239 5 5zm-10 474.71v-72.766h-15.79v54.922c0 2.761-2.239 5-5 5h-309.83c-19.277 0-34.961-15.684-34.961-34.961 0-19.278 15.684-34.961 34.961-34.961h330.62v-10.396h-339.07c-5.577 0-12.192 2.511-18.628 7.071-0.877 0.622-1.887 0.921-2.886 0.921-1.567 0-3.109-0.734-4.084-2.11-1.597-2.253-1.064-5.374 1.189-6.97 8.111-5.747 16.78-8.912 24.41-8.912h1.683v-12.229c0-2.761 2.239-5 5-5s5 2.239 5 5v12.229h327.38v-366.55h-327.39v324.3c0 2.761-2.239 5-5 5s-5-2.239-5-5v-324.3h-4.462c-19.384 0-35.153 15.77-35.153 35.153v386.75c0 45.5 37.165 47.804 44.615 47.804h332.38zm-330.62-72.766c-13.764 0-24.961 11.198-24.961 24.961s11.197 24.961 24.961 24.961h304.83v-49.922h-304.83zm64.132-253.74c1.364-4.198 5.097-6.91 9.511-6.91h59.656l18.435-56.737c1.364-4.198 5.097-6.91 9.511-6.91 4.413 0 8.146 2.712 9.511 6.91l18.435 56.737h59.657c4.414 0 8.146 2.712 9.511 6.91 1.364 4.197-0.062 8.586-3.633 11.18l-48.263 35.065 18.435 56.737c1.003 3.087 0.499 6.343-1.383 8.933-1.902 2.618-4.947 4.18-8.145 4.18-2.094 0-4.121-0.668-5.861-1.933l-48.264-35.065-48.263 35.065c-1.74 1.264-3.766 1.933-5.86 1.933-3.198 0-6.243-1.562-8.145-4.179-1.882-2.59-2.386-5.846-1.383-8.934l18.435-56.737-48.263-35.065c-3.572-2.594-4.998-6.983-3.634-11.18zm9.519 3.09 48.255 35.065c3.516 2.554 4.976 7.047 3.633 11.18l-18.433 56.729 48.261-35.057c1.72-1.25 3.752-1.91 5.878-1.91s4.158 0.66 5.877 1.909l48.258 35.062-18.429-56.733c-1.343-4.133 0.117-8.626 3.633-11.18l48.256-35.061-59.65-4e-3c-4.346 0-8.167-2.777-9.511-6.91l-18.432-56.73-18.438 56.729c-1.343 4.133-5.165 6.91-9.511 6.91h-59.647z\"/></svg>'),"
                                  "(2,'<svg width=\"512\" height=\"512\" version=\"1.1\" viewBox=\"0 0 135.47 135.47\" xmlns=\"http://www.w3.org/2000/svg\"><g fill=\"none\" stroke=\"#fff\" stroke-miterlimit=\"3.25\" stroke-width=\"4.2333\"><g><path d=\"m30.427 4.0996 74.061 0.00288\"/><path d=\"m19.976 14.684 0.0076 91.289\"/><path d=\"m114.91 14.679 0.16114 91.288m-82.552 0h82.552\" stroke-linecap=\"round\"/><path d=\"m30.567 95.39 85.317 0.06916\"/><path d=\"m72.995 116.56h31.655\" stroke-linecap=\"round\"/></g><path d=\"m47.364 105.97 4e-6 25.623 8.9838-7.4131 7.6223 7.4131-5e-6 -25.623\" stroke-linecap=\"round\" stroke-linejoin=\"round\"/><circle cx=\"38.896\" cy=\"86.15\" r=\".39687\"/><g stroke-linecap=\"round\"><path d=\"m38.846 86.188-2e-6 9.2019\"/><path d=\"m38.846 4.101v59.61\"/><path d=\"m38.846 71.478v6.9641\"/><path d=\"m30.567 116.56 8.2704 8e-3\"/></g><g stroke-linejoin=\"round\"><path d=\"m19.976 14.684a10.583 10.583 0 0 1 3.0998-7.4835 10.583 10.583 0 0 1 7.4835-3.0998\"/><path d=\"m104.49 4.1025a10.583 10.583 0 0 1 7.4836 3.0998 10.583 10.583 0 0 1 3.0998 7.4835\"/><ellipse d=\"m 115.07187,105.96629 a 10.583333,10.583333 0 0 1 -10.58333,10.58334\"/><path d=\"m30.567 116.56a10.583 10.583 0 0 1-9.1654-5.2917 10.583 10.583 0 0 1 0-10.583 10.583 10.583 0 0 1 9.1654-5.2917\"/></g><path d=\"m77.281 76.591c-9.2493-6.7604-17.805-14.083-22.932-23.273-2.7611-4.7394-5.1559-9.6615-3.8405-16.434 0.38059-1.2086 2.0242-8.3639 8.2788-11.199 4.0054-1.8156 8.2452-1.8417 12.117 0.41005 2.4787 1.4415 4.3697 3.2021 6.3762 4.9139\" stroke-linecap=\"round\"/><path d=\"m77.281 76.591c9.2493-6.7604 17.805-14.083 22.932-23.273 2.7611-4.7394 5.1559-9.6615 3.8405-16.434-0.38059-1.2086-2.0242-8.3639-8.2788-11.199-4.0054-1.8156-8.2452-1.8417-12.117 0.41005-2.4787 1.4415-4.3697 3.2021-6.3762 4.9139\" stroke-linecap=\"round\"/></g></svg>','<svg width=\"512\" height=\"512\" version=\"1.1\" viewBox=\"0 0 135.47 135.47\" xmlns=\"http://www.w3.org/2000/svg\"><g fill=\"none\" stroke=\"#000\" stroke-miterlimit=\"3.25\" stroke-width=\"4.2333\"><g><path d=\"m30.427 4.0996 74.061 0.00288\"/><path d=\"m19.976 14.684 0.0076 91.289\"/><path d=\"m114.91 14.679 0.16114 91.288m-82.552 0h82.552\" stroke-linecap=\"round\"/><path d=\"m30.567 95.39 85.317 0.06916\"/><path d=\"m72.995 116.56h31.655\" stroke-linecap=\"round\"/></g><path d=\"m47.364 105.97 4e-6 25.623 8.9838-7.4131 7.6223 7.4131-5e-6 -25.623\" stroke-linecap=\"round\" stroke-linejoin=\"round\"/><circle cx=\"38.896\" cy=\"86.15\" r=\".39687\"/><g stroke-linecap=\"round\"><path d=\"m38.846 86.188-2e-6 9.2019\"/><path d=\"m38.846 4.101v59.61\"/><path d=\"m38.846 71.478v6.9641\"/><path d=\"m30.567 116.56 8.2704 8e-3\"/></g><g stroke-linejoin=\"round\"><path d=\"m19.976 14.684a10.583 10.583 0 0 1 3.0998-7.4835 10.583 10.583 0 0 1 7.4835-3.0998\"/><path d=\"m104.49 4.1025a10.583 10.583 0 0 1 7.4836 3.0998 10.583 10.583 0 0 1 3.0998 7.4835\"/><ellipse d=\"m 115.07187,105.96629 a 10.583333,10.583333 0 0 1 -10.58333,10.58334\"/><path d=\"m30.567 116.56a10.583 10.583 0 0 1-9.1654-5.2917 10.583 10.583 0 0 1 0-10.583 10.583 10.583 0 0 1 9.1654-5.2917\"/></g><path d=\"m77.281 76.591c-9.2493-6.7604-17.805-14.083-22.932-23.273-2.7611-4.7394-5.1559-9.6615-3.8405-16.434 0.38059-1.2086 2.0242-8.3639 8.2788-11.199 4.0054-1.8156 8.2452-1.8417 12.117 0.41005 2.4787 1.4415 4.3697 3.2021 6.3762 4.9139\" stroke-linecap=\"round\"/><path d=\"m77.281 76.591c9.2493-6.7604 17.805-14.083 22.932-23.273 2.7611-4.7394 5.1559-9.6615 3.8405-16.434-0.38059-1.2086-2.0242-8.3639-8.2788-11.199-4.0054-1.8156-8.2452-1.8417-12.117 0.41005-2.4787 1.4415-4.3697 3.2021-6.3762 4.9139\" stroke-linecap=\"round\"/></g></svg>'),"
                                  "(3,'<?xml version=\"1.0\" encoding=\"UTF-8\"?><svg enable-background=\"new 0 0 512.004 512.004\" version=\"1.1\" viewBox=\"0 0 512 512\" xml:space=\"preserve\" xmlns=\"http://www.w3.org/2000/svg\"><g fill=\"#fff\" stroke=\"#fff\"><g fill=\"#fff\" stroke=\"#fff\"><g fill=\"#fff\" stroke=\"#fff\"><path d=\"m291.06 242.81c1.51 2.953 4.514 4.659 7.62 4.659 1.297 0 2.628-0.299 3.866-0.93 0.503-0.256 50.731-25.771 75.503-33.596 4.489-1.425 6.98-6.221 5.555-10.709-1.417-4.489-6.178-6.989-10.709-5.572-26.095 8.252-75.981 33.596-78.097 34.671-4.199 2.133-5.871 7.271-3.738 11.477z\"/><path d=\"m298.68 145.07c1.297 0 2.628-0.299 3.866-0.93 0.503-0.256 50.731-25.771 75.503-33.596 4.489-1.425 6.98-6.221 5.555-10.709-1.417-4.489-6.178-6.989-10.709-5.572-26.095 8.252-75.981 33.596-78.097 34.671-4.198 2.133-5.871 7.27-3.738 11.477 1.51 2.952 4.514 4.659 7.62 4.659z\"/><path d=\"m503.47 128c-4.719 0-8.533 3.823-8.533 8.533v332.8c0 14.114-11.486 25.6-25.6 25.6h-204.8v-19.635c12.442-4.352 44.851-14.498 76.8-14.498 74.334 0 124.81 16.461 125.31 16.631 2.568 0.853 5.436 0.427 7.68-1.178 2.227-1.604 3.541-4.181 3.541-6.921v-375.46c0-4.002-2.773-7.467-6.682-8.329 0 0-6.69-1.493-18.125-3.593-4.617-0.853-9.079 2.219-9.933 6.844-0.853 4.642 2.21 9.088 6.844 9.941 4.361 0.802 8.013 1.51 10.829 2.074v357.19c-19.337-5.069-62.276-14.259-119.47-14.259-37.18 0-73.702 12.211-85.001 16.35-10.044-4.437-40.405-16.35-77.133-16.35-58.778 0-107.2 9.694-128 14.618v-357.88c17.041-4.19 67.371-15.138 128-15.138 31.113 0 57.796 9.685 68.267 14.063v335.8c0 3.072 1.655 5.914 4.326 7.424 2.671 1.519 5.965 1.476 8.602-0.111 0.845-0.503 85.393-51.004 160.44-76.015 3.49-1.169 5.837-4.42 5.837-8.098v-349.87c0-2.799-1.374-5.419-3.678-7.014-2.287-1.596-5.222-1.963-7.859-0.981-68.275 25.608-137.36 68.599-138.05 69.026-3.994 2.5-5.214 7.765-2.714 11.759 2.492 3.994 7.757 5.214 11.759 2.714 0.631-0.401 60.732-37.794 123.48-63.027v331.28c-58.249 20.241-119.07 53.291-145.07 68.087v-326.51c0-3.234-1.826-6.187-4.719-7.637-1.468-0.725-36.437-17.963-80.614-17.963-77.107 0-136.39 16.683-138.88 17.399-3.661 1.041-6.187 4.395-6.187 8.201v375.47c0 2.671 1.263 5.197 3.388 6.81 1.502 1.135 3.311 1.724 5.146 1.724 0.785 0 1.57-0.111 2.338-0.333 0.589-0.162 59.597-16.734 134.2-16.734 31.198 0 57.856 9.711 68.267 14.071v20.062h-204.8c-14.114 0-25.6-11.486-25.6-25.6v-332.8c0-4.71-3.823-8.533-8.533-8.533s-8.533 3.823-8.533 8.533v332.8c0 23.526 19.14 42.667 42.667 42.667h426.67c23.526 0 42.667-19.14 42.667-42.667v-332.8c-1e-3 -4.711-3.815-8.534-8.534-8.534z\"/><path d=\"m291.06 191.61c1.51 2.953 4.514 4.659 7.62 4.659 1.297 0 2.628-0.299 3.866-0.93 0.503-0.256 50.731-25.771 75.503-33.596 4.489-1.425 6.98-6.221 5.555-10.709-1.417-4.489-6.178-6.989-10.709-5.572-26.095 8.252-75.981 33.596-78.097 34.671-4.199 2.133-5.871 7.271-3.738 11.477z\"/><path d=\"m291.06 294.01c1.51 2.953 4.514 4.659 7.62 4.659 1.297 0 2.628-0.299 3.866-0.93 0.503-0.256 50.731-25.771 75.503-33.596 4.489-1.425 6.98-6.221 5.555-10.709-1.417-4.489-6.178-6.989-10.709-5.572-26.095 8.252-75.981 33.596-78.097 34.671-4.199 2.133-5.871 7.271-3.738 11.477z\"/><path d=\"m206.75 158.37c-52.693-12.365-112.57 3.388-115.09 4.062-4.548 1.22-7.253 5.896-6.033 10.453 1.024 3.814 4.471 6.323 8.235 6.323 0.734 0 1.476-0.094 2.219-0.29 0.572-0.162 58.223-15.326 106.78-3.934 4.565 1.067 9.182-1.775 10.257-6.366 1.074-4.582-1.776-9.173-6.367-10.248z\"/><path d=\"m206.75 209.57c-52.693-12.356-112.57 3.388-115.09 4.062-4.548 1.22-7.253 5.897-6.033 10.453 1.024 3.814 4.471 6.323 8.235 6.323 0.734 0 1.476-0.094 2.219-0.29 0.572-0.162 58.223-15.326 106.78-3.934 4.565 1.067 9.182-1.775 10.257-6.366 1.074-4.582-1.776-9.173-6.367-10.248z\"/><path d=\"m291.06 345.21c1.51 2.953 4.514 4.659 7.62 4.659 1.297 0 2.628-0.299 3.866-0.93 0.503-0.256 50.731-25.771 75.503-33.596 4.489-1.425 6.98-6.221 5.555-10.709-1.417-4.488-6.178-6.989-10.709-5.572-26.095 8.252-75.981 33.596-78.097 34.671-4.199 2.133-5.871 7.271-3.738 11.477z\"/><path d=\"m206.75 260.77c-52.693-12.356-112.57 3.379-115.09 4.062-4.548 1.22-7.253 5.897-6.033 10.453 1.024 3.814 4.471 6.332 8.235 6.332 0.734 0 1.476-0.102 2.219-0.299 0.572-0.162 58.223-15.326 106.78-3.934 4.565 1.067 9.182-1.775 10.257-6.366 1.074-4.582-1.776-9.173-6.367-10.248z\"/><path d=\"m206.75 363.17c-52.693-12.365-112.57 3.388-115.09 4.062-4.548 1.22-7.253 5.897-6.033 10.453 1.024 3.814 4.471 6.332 8.235 6.332 0.734 0 1.476-0.102 2.219-0.299 0.572-0.162 58.223-15.326 106.78-3.934 4.565 1.058 9.182-1.775 10.257-6.366 1.074-4.582-1.776-9.173-6.367-10.248z\"/><path d=\"m206.75 311.97c-52.693-12.365-112.57 3.379-115.09 4.062-4.548 1.22-7.253 5.897-6.033 10.453 1.024 3.814 4.471 6.332 8.235 6.332 0.734 0 1.476-0.102 2.219-0.299 0.572-0.162 58.223-15.326 106.78-3.934 4.565 1.067 9.182-1.775 10.257-6.366 1.074-4.582-1.776-9.173-6.367-10.248z\"/></g></g></g></svg>','<?xml version=\"1.0\" encoding=\"UTF-8\"?><svg enable-background=\"new 0 0 512.004 512.004\" version=\"1.1\" viewBox=\"0 0 512 512\" xml:space=\"preserve\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"m291.06 242.81c1.51 2.953 4.514 4.659 7.62 4.659 1.297 0 2.628-0.299 3.866-0.93 0.503-0.256 50.731-25.771 75.503-33.596 4.489-1.425 6.98-6.221 5.555-10.709-1.417-4.489-6.178-6.989-10.709-5.572-26.095 8.252-75.981 33.596-78.097 34.671-4.199 2.133-5.871 7.271-3.738 11.477z\"/><path d=\"m298.68 145.07c1.297 0 2.628-0.299 3.866-0.93 0.503-0.256 50.731-25.771 75.503-33.596 4.489-1.425 6.98-6.221 5.555-10.709-1.417-4.489-6.178-6.989-10.709-5.572-26.095 8.252-75.981 33.596-78.097 34.671-4.198 2.133-5.871 7.27-3.738 11.477 1.51 2.952 4.514 4.659 7.62 4.659z\"/><path d=\"m503.47 128c-4.719 0-8.533 3.823-8.533 8.533v332.8c0 14.114-11.486 25.6-25.6 25.6h-204.8v-19.635c12.442-4.352 44.851-14.498 76.8-14.498 74.334 0 124.81 16.461 125.31 16.631 2.568 0.853 5.436 0.427 7.68-1.178 2.227-1.604 3.541-4.181 3.541-6.921v-375.46c0-4.002-2.773-7.467-6.682-8.329 0 0-6.69-1.493-18.125-3.593-4.617-0.853-9.079 2.219-9.933 6.844-0.853 4.642 2.21 9.088 6.844 9.941 4.361 0.802 8.013 1.51 10.829 2.074v357.19c-19.337-5.069-62.276-14.259-119.47-14.259-37.18 0-73.702 12.211-85.001 16.35-10.044-4.437-40.405-16.35-77.133-16.35-58.778 0-107.2 9.694-128 14.618v-357.88c17.041-4.19 67.371-15.138 128-15.138 31.113 0 57.796 9.685 68.267 14.063v335.8c0 3.072 1.655 5.914 4.326 7.424 2.671 1.519 5.965 1.476 8.602-0.111 0.845-0.503 85.393-51.004 160.44-76.015 3.49-1.169 5.837-4.42 5.837-8.098v-349.87c0-2.799-1.374-5.419-3.678-7.014-2.287-1.596-5.222-1.963-7.859-0.981-68.275 25.608-137.36 68.599-138.05 69.026-3.994 2.5-5.214 7.765-2.714 11.759 2.492 3.994 7.757 5.214 11.759 2.714 0.631-0.401 60.732-37.794 123.48-63.027v331.28c-58.249 20.241-119.07 53.291-145.07 68.087v-326.51c0-3.234-1.826-6.187-4.719-7.637-1.468-0.725-36.437-17.963-80.614-17.963-77.107 0-136.39 16.683-138.88 17.399-3.661 1.041-6.187 4.395-6.187 8.201v375.47c0 2.671 1.263 5.197 3.388 6.81 1.502 1.135 3.311 1.724 5.146 1.724 0.785 0 1.57-0.111 2.338-0.333 0.589-0.162 59.597-16.734 134.2-16.734 31.198 0 57.856 9.711 68.267 14.071v20.062h-204.8c-14.114 0-25.6-11.486-25.6-25.6v-332.8c0-4.71-3.823-8.533-8.533-8.533s-8.533 3.823-8.533 8.533v332.8c0 23.526 19.14 42.667 42.667 42.667h426.67c23.526 0 42.667-19.14 42.667-42.667v-332.8c-1e-3 -4.711-3.815-8.534-8.534-8.534z\"/><path d=\"m291.06 191.61c1.51 2.953 4.514 4.659 7.62 4.659 1.297 0 2.628-0.299 3.866-0.93 0.503-0.256 50.731-25.771 75.503-33.596 4.489-1.425 6.98-6.221 5.555-10.709-1.417-4.489-6.178-6.989-10.709-5.572-26.095 8.252-75.981 33.596-78.097 34.671-4.199 2.133-5.871 7.271-3.738 11.477z\"/><path d=\"m291.06 294.01c1.51 2.953 4.514 4.659 7.62 4.659 1.297 0 2.628-0.299 3.866-0.93 0.503-0.256 50.731-25.771 75.503-33.596 4.489-1.425 6.98-6.221 5.555-10.709-1.417-4.489-6.178-6.989-10.709-5.572-26.095 8.252-75.981 33.596-78.097 34.671-4.199 2.133-5.871 7.271-3.738 11.477z\"/><path d=\"m206.75 158.37c-52.693-12.365-112.57 3.388-115.09 4.062-4.548 1.22-7.253 5.896-6.033 10.453 1.024 3.814 4.471 6.323 8.235 6.323 0.734 0 1.476-0.094 2.219-0.29 0.572-0.162 58.223-15.326 106.78-3.934 4.565 1.067 9.182-1.775 10.257-6.366 1.074-4.582-1.776-9.173-6.367-10.248z\"/><path d=\"m206.75 209.57c-52.693-12.356-112.57 3.388-115.09 4.062-4.548 1.22-7.253 5.897-6.033 10.453 1.024 3.814 4.471 6.323 8.235 6.323 0.734 0 1.476-0.094 2.219-0.29 0.572-0.162 58.223-15.326 106.78-3.934 4.565 1.067 9.182-1.775 10.257-6.366 1.074-4.582-1.776-9.173-6.367-10.248z\"/><path d=\"m291.06 345.21c1.51 2.953 4.514 4.659 7.62 4.659 1.297 0 2.628-0.299 3.866-0.93 0.503-0.256 50.731-25.771 75.503-33.596 4.489-1.425 6.98-6.221 5.555-10.709-1.417-4.488-6.178-6.989-10.709-5.572-26.095 8.252-75.981 33.596-78.097 34.671-4.199 2.133-5.871 7.271-3.738 11.477z\"/><path d=\"m206.75 260.77c-52.693-12.356-112.57 3.379-115.09 4.062-4.548 1.22-7.253 5.897-6.033 10.453 1.024 3.814 4.471 6.332 8.235 6.332 0.734 0 1.476-0.102 2.219-0.299 0.572-0.162 58.223-15.326 106.78-3.934 4.565 1.067 9.182-1.775 10.257-6.366 1.074-4.582-1.776-9.173-6.367-10.248z\"/><path d=\"m206.75 363.17c-52.693-12.365-112.57 3.388-115.09 4.062-4.548 1.22-7.253 5.897-6.033 10.453 1.024 3.814 4.471 6.332 8.235 6.332 0.734 0 1.476-0.102 2.219-0.299 0.572-0.162 58.223-15.326 106.78-3.934 4.565 1.058 9.182-1.775 10.257-6.366 1.074-4.582-1.776-9.173-6.367-10.248z\"/><path d=\"m206.75 311.97c-52.693-12.365-112.57 3.379-115.09 4.062-4.548 1.22-7.253 5.897-6.033 10.453 1.024 3.814 4.471 6.332 8.235 6.332 0.734 0 1.476-0.102 2.219-0.299 0.572-0.162 58.223-15.326 106.78-3.934 4.565 1.067 9.182-1.775 10.257-6.366 1.074-4.582-1.776-9.173-6.367-10.248z\"/></svg>'),"
                                  "(4,'<?xml version=\"1.0\" encoding=\"UTF-8\"?><svg width=\"512mm\" height=\"512mm\" version=\"1.1\" viewBox=\"0 0 512 512\" xmlns=\"http://www.w3.org/2000/svg\"><g fill=\"none\" stroke=\"#fff\"><g stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"14.175\"><path d=\"m7.5304 69.385-0.44274 288.72s32.827-9.0433 49.256-12.095c15.282-2.8383 46.173-6.5058 46.173-6.5058\"/><path d=\"m138.26 489.51s2.8806-35.253-2.755-49.879c-5.1293-13.312-5.9358-15.307-20.904-24.001-9.7842-5.6829-19.343-11.488-26.597-20.172-11.482-13.747-14.404-49.396-14.404-49.396\"/><path d=\"m255.44 381.43-0.12032 106.72\"/></g><path d=\"m256.26 65.674 0.42574 281.74\" stroke-width=\"14.766\"/><g stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"14.175\"><path d=\"m111.6 312.52c-23.873-0.71561-47.787-0.58089-71.806 1.6594l-0.0967-284.43s73.384-10.393 109.5-4.873c12.914 1.9735 30.836 4.8934 47.193 10.883 31.107 11.391 59.874 29.919 59.874 29.919s40.321-26.358 68.98-33.648c26.035-6.6222 65.297-9.5635 92.15-8.8225 17.965 0.49572 53.969 7.1929 53.969 7.1929l-0.23086 284.97-68.548-2.3402\"/><path d=\"m410.09 339.67s32.385 4.7115 48.462 7.4532c15.369 2.621 46.353 13.184 46.353 10.112s0.0113-287.72 0.0113-287.72\"/><path d=\"m373.17 487.47s-0.35466-42.772 3.8017-51.621c6.8591-14.604 10.911-16.059 27.433-25.679 13.355-7.7764 21.671-19.098 25.95-29.104 3.8547-9.012 5.4124-24.026 8.2097-34.255\"/><path d=\"m122.21 359.24c3.3344-4.6679 35.883-71.739 43.796-81.509 3.8392-4.7404 7.4691-8.7372 16.115-6.7809 6.5119 1.4735 11.122 6.4454 12.07 12.051 1.7241 10.191-15.956 74.52-15.956 74.52s23.724 5.0389 33.429 17.279c9.0439 11.406 12.005 26.431 12.005 26.431\"/><path d=\"m286.69 403.01s6.5285-22.597 14.74-30.657c11.036-10.832 24.349-12.147 29.773-13.608 2.8808-0.77595-13.746-68.255-13.776-72.13-0.0191-2.5296-1.7943-8.8779 9.08-14.286 9.5219-4.7352 15.695 0.81495 18.926 4.8686 3.1401 3.9387 44.187 82.617 44.187 82.617\"/></g><path d=\"m186.67 338c26.681 2.1621 46.171 5.5134 67.998 11.368 22.106-6.5984 48.18-11.704 69.42-13.283\" stroke-width=\"17.719\"/><path d=\"m319.83 323.1c-18.744 3.338-46.498 16.922-64.907 25.876-21.908-10.891-44.02-21.299-67.89-27.524\" stroke-width=\"16.243\"/><g stroke-linecap=\"round\" stroke-width=\"14.175\"><path d=\"m88.329 236.94s25.153-6.065 59.522-2.0627c41.374 4.8181 57.759 15.024 57.759 15.024\"/><path d=\"m85.749 185.05s29.405-6.237 67.8-0.68681c38.981 5.6349 53.724 14.736 53.724 14.736\"/><path d=\"m85.935 133.54s22.502-6.0913 65.616-0.47618c41.757 5.4382 55.342 14.644 55.342 14.644\"/><path d=\"m85.87 82.271s23.797-6.0069 61.461-1.6448c42 4.8642 60.085 16.238 60.085 16.238\"/><path d=\"m302.33 249.57s20.16-10.48 55.273-14.13c39.408-4.0963 65.606 0.86923 65.606 0.86923\"/><path d=\"m302.66 199.24s12.36-10.341 50.851-14.859c33.063-3.8805 69.815 0.6791 69.815 0.6791\"/><path d=\"m304.23 147.74s17.069-9.1316 48.408-13.926c25.622-3.9194 68.582-0.31857 68.582-0.31857\"/><path d=\"m302.74 96.453s16.556-9.3425 52.343-14.012c32.498-4.2408 67.885-0.74149 67.885-0.74149\"/></g></g></svg>','<?xml version=\"1.0\" encoding=\"UTF-8\"?><svg width=\"512mm\" height=\"512mm\" version=\"1.1\" viewBox=\"0 0 512 512\" xmlns=\"http://www.w3.org/2000/svg\"><g fill=\"none\" stroke=\"#000\"><g stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"14.175\"><path d=\"m7.5304 69.385-0.44274 288.72s32.827-9.0433 49.256-12.095c15.282-2.8383 46.173-6.5058 46.173-6.5058\"/><path d=\"m138.26 489.51s2.8806-35.253-2.755-49.879c-5.1293-13.312-5.9358-15.307-20.904-24.001-9.7842-5.6829-19.343-11.488-26.597-20.172-11.482-13.747-14.404-49.396-14.404-49.396\"/><path d=\"m255.44 381.43-0.12032 106.72\"/></g><path d=\"m256.26 65.674 0.42574 281.74\" stroke-width=\"14.766\"/><g stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"14.175\"><path d=\"m111.6 312.52c-23.873-0.71561-47.787-0.58089-71.806 1.6594l-0.0967-284.43s73.384-10.393 109.5-4.873c12.914 1.9735 30.836 4.8934 47.193 10.883 31.107 11.391 59.874 29.919 59.874 29.919s40.321-26.358 68.98-33.648c26.035-6.6222 65.297-9.5635 92.15-8.8225 17.965 0.49572 53.969 7.1929 53.969 7.1929l-0.23086 284.97-68.548-2.3402\"/><path d=\"m410.09 339.67s32.385 4.7115 48.462 7.4532c15.369 2.621 46.353 13.184 46.353 10.112s0.0113-287.72 0.0113-287.72\"/><path d=\"m373.17 487.47s-0.35466-42.772 3.8017-51.621c6.8591-14.604 10.911-16.059 27.433-25.679 13.355-7.7764 21.671-19.098 25.95-29.104 3.8547-9.012 5.4124-24.026 8.2097-34.255\"/><path d=\"m122.21 359.24c3.3344-4.6679 35.883-71.739 43.796-81.509 3.8392-4.7404 7.4691-8.7372 16.115-6.7809 6.5119 1.4735 11.122 6.4454 12.07 12.051 1.7241 10.191-15.956 74.52-15.956 74.52s23.724 5.0389 33.429 17.279c9.0439 11.406 12.005 26.431 12.005 26.431\"/><path d=\"m286.69 403.01s6.5285-22.597 14.74-30.657c11.036-10.832 24.349-12.147 29.773-13.608 2.8808-0.77595-13.746-68.255-13.776-72.13-0.0191-2.5296-1.7943-8.8779 9.08-14.286 9.5219-4.7352 15.695 0.81495 18.926 4.8686 3.1401 3.9387 44.187 82.617 44.187 82.617\"/></g><path d=\"m186.67 338c26.681 2.1621 46.171 5.5134 67.998 11.368 22.106-6.5984 48.18-11.704 69.42-13.283\" stroke-width=\"17.719\"/><path d=\"m319.83 323.1c-18.744 3.338-46.498 16.922-64.907 25.876-21.908-10.891-44.02-21.299-67.89-27.524\" stroke-width=\"16.243\"/><g stroke-linecap=\"round\" stroke-width=\"14.175\"><path d=\"m88.329 236.94s25.153-6.065 59.522-2.0627c41.374 4.8181 57.759 15.024 57.759 15.024\"/><path d=\"m85.749 185.05s29.405-6.237 67.8-0.68681c38.981 5.6349 53.724 14.736 53.724 14.736\"/><path d=\"m85.935 133.54s22.502-6.0913 65.616-0.47618c41.757 5.4382 55.342 14.644 55.342 14.644\"/><path d=\"m85.87 82.271s23.797-6.0069 61.461-1.6448c42 4.8642 60.085 16.238 60.085 16.238\"/><path d=\"m302.33 249.57s20.16-10.48 55.273-14.13c39.408-4.0963 65.606 0.86923 65.606 0.86923\"/><path d=\"m302.66 199.24s12.36-10.341 50.851-14.859c33.063-3.8805 69.815 0.6791 69.815 0.6791\"/><path d=\"m304.23 147.74s17.069-9.1316 48.408-13.926c25.622-3.9194 68.582-0.31857 68.582-0.31857\"/><path d=\"m302.74 96.453s16.556-9.3425 52.343-14.012c32.498-4.2408 67.885-0.74149 67.885-0.74149\"/></g></g></svg>'),"
                                  "(5,'<?xml version=\"1.0\" encoding=\"UTF-8\"?><svg enable-background=\"new 0 0 487.887 487.887\" version=\"1.1\" viewBox=\"0 0 487.89 487.89\" xml:space=\"preserve\" xmlns=\"http://www.w3.org/2000/svg\"><g fill=\"#fff\"><path d=\"m374.92 426.94h-308.38c-2.762 0-5 2.238-5 5s2.238 5 5 5h308.38c2.762 0 5-2.238 5-5s-2.239-5-5-5z\"/><path d=\"m374.92 451.53h-308.38c-2.762 0-5 2.238-5 5s2.238 5 5 5h308.38c2.762 0 5-2.238 5-5s-2.239-5-5-5z\"/><path d=\"m465.14 295.56-20.693-26.197c-0.392-0.496-0.874-0.901-1.41-1.213 2.938-10.562 0.985-22.338-6.315-31.58l-16.768-21.227v-210.35c0-2.762-2.238-5-5-5h-329.94c-34.923 0-63.335 28.412-63.335 63.335v379.73c0 24.713 20.105 44.818 44.818 44.818h348.46c0.018 0 0.034-5e-3 0.051-5e-3 0.283-3e-3 0.565-0.034 0.843-0.085 0.053-9e-3 0.107-0.014 0.16-0.026 0.255-0.055 0.505-0.137 0.75-0.232 0.075-0.029 0.15-0.056 0.223-0.088 0.224-0.1 0.44-0.219 0.65-0.353 0.077-0.049 0.152-0.1 0.226-0.153 0.195-0.14 0.384-0.292 0.561-0.462 0.07-0.067 0.131-0.14 0.197-0.211 0.058-0.063 0.12-0.121 0.175-0.188 0.036-0.043 0.058-0.092 0.092-0.136 0.14-0.179 0.265-0.365 0.38-0.563 0.055-0.094 0.107-0.188 0.155-0.285 0.095-0.19 0.175-0.387 0.245-0.59 0.038-0.11 0.073-0.22 0.103-0.332 0.052-0.196 0.091-0.395 0.12-0.599 0.018-0.126 0.03-0.251 0.038-0.379 7e-3 -0.105 0.031-0.206 0.031-0.313v-145.26l44.362-35.043c2.167-1.712 2.536-4.857 0.825-7.024zm-433.46 147.5c0-19.199 15.619-34.818 34.818-34.818h338.14c-5.813 8.237-13.319 21.46-13.319 34.818 0 15.313 6.585 27.301 12.286 34.818h-337.11c-19.199 1e-3 -34.818-15.618-34.818-34.818zm369.64 0c0-8.73 4.315-17.822 8.637-24.74v51.194c-4.281-6.291-8.637-15.401-8.637-26.454zm8.637-44.818h-328.5v-15.463c0-2.762-2.238-5-5-5s-5 2.238-5 5v15.463h-4.96c-14.044 0-26.595 6.499-34.818 16.641v-351.56c0-24.726 16.914-45.57 39.778-51.584v332.51c0 2.762 2.238 5 5 5s5-2.238 5-5v-334.13c1.177-0.078 2.36-0.132 3.557-0.132h324.94v192.69l-11.071-14.016-25.004-31.654c-5.552-7.028-13.509-11.474-22.404-12.518-8.886-1.045-17.664 1.439-24.692 6.99l-51.289 40.514-27.668-35.027c-9.079-11.491-25.871-13.94-37.438-5.453-5.967 4.377-9.798 10.819-10.79 18.138-0.991 7.322 0.984 14.548 5.563 20.346l77.468 98.069c-5.991-0.977-12.252 0.142-17.571 3.2-6.037 3.472-10.306 9.013-12.018 15.604-3.504 13.486 4.564 27.515 17.985 31.271l52.295 14.642c3.134 0.878 6.342 1.308 9.536 1.308 6.262 0 12.459-1.659 17.91-4.825l17.82 22.559c0.987 1.249 2.45 1.9 3.927 1.9 1.086 0 2.179-0.352 3.097-1.076l34.345-27.13v52.72zm-36.62-36.536-17.595-22.274c-1.713-2.167-4.857-2.533-7.023-0.824-6.381 5.043-14.867 6.742-22.701 4.55l-52.295-14.642c-8.21-2.299-13.146-10.88-11.003-19.127 1.032-3.972 3.634-7.328 7.324-9.45 3.848-2.211 8.476-2.8 12.698-1.62l11.819 3.309c2.081 0.587 4.31-0.241 5.509-2.043 1.2-1.802 1.104-4.172-0.237-5.871l-86.822-109.91c-2.882-3.648-4.126-8.195-3.501-12.804 0.623-4.605 3.037-8.66 6.796-11.418 7.272-5.336 17.894-3.729 23.674 3.59l30.766 38.948c1e-3 1e-3 1e-3 2e-3 2e-3 3e-3l27.544 34.869c0.196 0.248 0.415 0.465 0.644 0.665 0.035 0.03 0.068 0.063 0.104 0.092 0.222 0.184 0.458 0.343 0.704 0.483 0.048 0.027 0.095 0.052 0.143 0.078 0.25 0.132 0.507 0.244 0.773 0.331 0.039 0.013 0.079 0.022 0.119 0.034 0.267 0.08 0.538 0.141 0.814 0.175 0.019 2e-3 0.038 7e-3 0.057 0.01 0.189 0.021 0.379 0.033 0.57 0.033 0.069 0 0.138-0.014 0.208-0.017 0.119-5e-3 0.236-0.015 0.354-0.029 0.184-0.021 0.366-0.053 0.548-0.095 0.115-0.026 0.229-0.051 0.342-0.086 0.193-0.059 0.38-0.135 0.566-0.218 0.095-0.042 0.192-0.077 0.285-0.125 0.275-0.143 0.542-0.308 0.794-0.507 2.167-1.712 2.535-4.856 0.824-7.023l-24.445-30.946 21.544-17.018 24.445 30.946c0.196 0.248 0.415 0.465 0.644 0.665 0.035 0.03 0.068 0.063 0.104 0.092 0.222 0.184 0.458 0.343 0.704 0.483 0.048 0.027 0.095 0.052 0.143 0.078 0.25 0.132 0.507 0.244 0.773 0.331 0.039 0.013 0.079 0.022 0.119 0.034 0.267 0.08 0.538 0.141 0.814 0.175 0.019 2e-3 0.038 7e-3 0.057 0.01 0.189 0.021 0.379 0.033 0.57 0.033 0.069 0 0.138-0.014 0.208-0.017 0.119-5e-3 0.236-0.015 0.354-0.029 0.184-0.021 0.366-0.053 0.548-0.095 0.115-0.026 0.229-0.051 0.342-0.086 0.193-0.059 0.38-0.135 0.566-0.218 0.095-0.042 0.192-0.077 0.285-0.125 0.275-0.143 0.542-0.308 0.794-0.507 2.167-1.712 2.535-4.856 0.824-7.023l-24.445-30.946 17.975-14.198 24.445 30.946c1.715 2.168 4.858 2.535 7.023 0.824 2.167-1.712 2.536-4.856 0.824-7.023l-24.117-30.53c4.02-1.974 8.525-2.754 13.077-2.222 6.242 0.732 11.826 3.853 15.722 8.785l62.843 79.556c4.227 5.35 6.115 12.023 5.321 18.795-0.795 6.769-4.177 12.823-9.525 17.049l-52.841 41.74c-2.167 1.712-2.535 4.856-0.824 7.023 1.714 2.168 4.858 2.535 7.023 0.824l52.83-41.732c2e-3 -2e-3 5e-3 -3e-3 7e-3 -5e-3 8e-3 -6e-3 0.015-0.012 0.022-0.019l8.811-6.96 14.495 18.351-80.863 63.874z\"/><path d=\"m309.44 317.61c-1.408 0-2.809-0.592-3.797-1.745l-13.542-15.79c-1.798-2.096-1.557-5.253 0.54-7.05 2.096-1.799 5.253-1.557 7.05 0.54l13.542 15.79c1.798 2.096 1.557 5.253-0.54 7.05-0.943 0.81-2.1 1.205-3.253 1.205z\" fill=\"#fff\"/></g></svg>','<?xml version=\"1.0\" encoding=\"UTF-8\"?><svg enable-background=\"new 0 0 487.887 487.887\" version=\"1.1\" viewBox=\"0 0 487.89 487.89\" xml:space=\"preserve\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"m374.92 426.94h-308.38c-2.762 0-5 2.238-5 5s2.238 5 5 5h308.38c2.762 0 5-2.238 5-5s-2.239-5-5-5z\"/><path d=\"m374.92 451.53h-308.38c-2.762 0-5 2.238-5 5s2.238 5 5 5h308.38c2.762 0 5-2.238 5-5s-2.239-5-5-5z\"/><path d=\"m465.14 295.56-20.693-26.197c-0.392-0.496-0.874-0.901-1.41-1.213 2.938-10.562 0.985-22.338-6.315-31.58l-16.768-21.227v-210.35c0-2.762-2.238-5-5-5h-329.94c-34.923 0-63.335 28.412-63.335 63.335v379.73c0 24.713 20.105 44.818 44.818 44.818h348.46c0.018 0 0.034-5e-3 0.051-5e-3 0.283-3e-3 0.565-0.034 0.843-0.085 0.053-9e-3 0.107-0.014 0.16-0.026 0.255-0.055 0.505-0.137 0.75-0.232 0.075-0.029 0.15-0.056 0.223-0.088 0.224-0.1 0.44-0.219 0.65-0.353 0.077-0.049 0.152-0.1 0.226-0.153 0.195-0.14 0.384-0.292 0.561-0.462 0.07-0.067 0.131-0.14 0.197-0.211 0.058-0.063 0.12-0.121 0.175-0.188 0.036-0.043 0.058-0.092 0.092-0.136 0.14-0.179 0.265-0.365 0.38-0.563 0.055-0.094 0.107-0.188 0.155-0.285 0.095-0.19 0.175-0.387 0.245-0.59 0.038-0.11 0.073-0.22 0.103-0.332 0.052-0.196 0.091-0.395 0.12-0.599 0.018-0.126 0.03-0.251 0.038-0.379 7e-3 -0.105 0.031-0.206 0.031-0.313v-145.26l44.362-35.043c2.167-1.712 2.536-4.857 0.825-7.024zm-433.46 147.5c0-19.199 15.619-34.818 34.818-34.818h338.14c-5.813 8.237-13.319 21.46-13.319 34.818 0 15.313 6.585 27.301 12.286 34.818h-337.11c-19.199 1e-3 -34.818-15.618-34.818-34.818zm369.64 0c0-8.73 4.315-17.822 8.637-24.74v51.194c-4.281-6.291-8.637-15.401-8.637-26.454zm8.637-44.818h-328.5v-15.463c0-2.762-2.238-5-5-5s-5 2.238-5 5v15.463h-4.96c-14.044 0-26.595 6.499-34.818 16.641v-351.56c0-24.726 16.914-45.57 39.778-51.584v332.51c0 2.762 2.238 5 5 5s5-2.238 5-5v-334.13c1.177-0.078 2.36-0.132 3.557-0.132h324.94v192.69l-11.071-14.016-25.004-31.654c-5.552-7.028-13.509-11.474-22.404-12.518-8.886-1.045-17.664 1.439-24.692 6.99l-51.289 40.514-27.668-35.027c-9.079-11.491-25.871-13.94-37.438-5.453-5.967 4.377-9.798 10.819-10.79 18.138-0.991 7.322 0.984 14.548 5.563 20.346l77.468 98.069c-5.991-0.977-12.252 0.142-17.571 3.2-6.037 3.472-10.306 9.013-12.018 15.604-3.504 13.486 4.564 27.515 17.985 31.271l52.295 14.642c3.134 0.878 6.342 1.308 9.536 1.308 6.262 0 12.459-1.659 17.91-4.825l17.82 22.559c0.987 1.249 2.45 1.9 3.927 1.9 1.086 0 2.179-0.352 3.097-1.076l34.345-27.13v52.72zm-36.62-36.536-17.595-22.274c-1.713-2.167-4.857-2.533-7.023-0.824-6.381 5.043-14.867 6.742-22.701 4.55l-52.295-14.642c-8.21-2.299-13.146-10.88-11.003-19.127 1.032-3.972 3.634-7.328 7.324-9.45 3.848-2.211 8.476-2.8 12.698-1.62l11.819 3.309c2.081 0.587 4.31-0.241 5.509-2.043 1.2-1.802 1.104-4.172-0.237-5.871l-86.822-109.91c-2.882-3.648-4.126-8.195-3.501-12.804 0.623-4.605 3.037-8.66 6.796-11.418 7.272-5.336 17.894-3.729 23.674 3.59l30.766 38.948c1e-3 1e-3 1e-3 2e-3 2e-3 3e-3l27.544 34.869c0.196 0.248 0.415 0.465 0.644 0.665 0.035 0.03 0.068 0.063 0.104 0.092 0.222 0.184 0.458 0.343 0.704 0.483 0.048 0.027 0.095 0.052 0.143 0.078 0.25 0.132 0.507 0.244 0.773 0.331 0.039 0.013 0.079 0.022 0.119 0.034 0.267 0.08 0.538 0.141 0.814 0.175 0.019 2e-3 0.038 7e-3 0.057 0.01 0.189 0.021 0.379 0.033 0.57 0.033 0.069 0 0.138-0.014 0.208-0.017 0.119-5e-3 0.236-0.015 0.354-0.029 0.184-0.021 0.366-0.053 0.548-0.095 0.115-0.026 0.229-0.051 0.342-0.086 0.193-0.059 0.38-0.135 0.566-0.218 0.095-0.042 0.192-0.077 0.285-0.125 0.275-0.143 0.542-0.308 0.794-0.507 2.167-1.712 2.535-4.856 0.824-7.023l-24.445-30.946 21.544-17.018 24.445 30.946c0.196 0.248 0.415 0.465 0.644 0.665 0.035 0.03 0.068 0.063 0.104 0.092 0.222 0.184 0.458 0.343 0.704 0.483 0.048 0.027 0.095 0.052 0.143 0.078 0.25 0.132 0.507 0.244 0.773 0.331 0.039 0.013 0.079 0.022 0.119 0.034 0.267 0.08 0.538 0.141 0.814 0.175 0.019 2e-3 0.038 7e-3 0.057 0.01 0.189 0.021 0.379 0.033 0.57 0.033 0.069 0 0.138-0.014 0.208-0.017 0.119-5e-3 0.236-0.015 0.354-0.029 0.184-0.021 0.366-0.053 0.548-0.095 0.115-0.026 0.229-0.051 0.342-0.086 0.193-0.059 0.38-0.135 0.566-0.218 0.095-0.042 0.192-0.077 0.285-0.125 0.275-0.143 0.542-0.308 0.794-0.507 2.167-1.712 2.535-4.856 0.824-7.023l-24.445-30.946 17.975-14.198 24.445 30.946c1.715 2.168 4.858 2.535 7.023 0.824 2.167-1.712 2.536-4.856 0.824-7.023l-24.117-30.53c4.02-1.974 8.525-2.754 13.077-2.222 6.242 0.732 11.826 3.853 15.722 8.785l62.843 79.556c4.227 5.35 6.115 12.023 5.321 18.795-0.795 6.769-4.177 12.823-9.525 17.049l-52.841 41.74c-2.167 1.712-2.535 4.856-0.824 7.023 1.714 2.168 4.858 2.535 7.023 0.824l52.83-41.732c2e-3 -2e-3 5e-3 -3e-3 7e-3 -5e-3 8e-3 -6e-3 0.015-0.012 0.022-0.019l8.811-6.96 14.495 18.351-80.863 63.874z\"/><path d=\"m309.44 317.61c-1.408 0-2.809-0.592-3.797-1.745l-13.542-15.79c-1.798-2.096-1.557-5.253 0.54-7.05 2.096-1.799 5.253-1.557 7.05 0.54l13.542 15.79c1.798 2.096 1.557 5.253-0.54 7.05-0.943 0.81-2.1 1.205-3.253 1.205z\"/></svg>'),"
                                  "(6,'<?xml version=\"1.0\" encoding=\"UTF-8\"?><svg enable-background=\"new 0 0 455.978 455.978\" version=\"1.1\" viewBox=\"0 0 455.98 455.98\" xml:space=\"preserve\" xmlns=\"http://www.w3.org/2000/svg\"><g fill=\"#fff\"><path d=\"m335.95 169.7v-111.02h-191.04v111.02h191.04zm-176.04-96.019h161.04v81.02h-161.04v-81.02z\"/><rect x=\"175.94\" y=\"89.519\" width=\"128.99\" height=\"15\"/><rect x=\"175.94\" y=\"119.52\" width=\"128.99\" height=\"15\"/><path d=\"m391.08 0h-286.85c-21.682 0-39.322 17.64-39.322 39.323 0 0 3e-3 332.51 3e-3 332.51 0 21.68 17.638 39.318 39.318 39.318h22.913v44.823h55.897v-44.823h208.04l6e-3 -411.16zm-15 332.51h-264.35v-317.51h264.35v317.51zm-279.35-316.32v317.03c-6.142 1.18-11.883 3.811-16.822 7.715v-301.61c0-10.795 7.071-19.965 16.822-23.134zm7.499 379.97c-13.409 0-24.318-10.909-24.318-24.318 0-6.505 2.529-12.617 7.122-17.209 4.592-4.592 10.699-7.121 17.196-7.121h22.913v48.648h-22.913zm37.913 44.823v-93.472h25.897v93.472h-25.897zm233.94-44.823h-193.04v-48.648h193.04v48.648z\"/></g></svg>','<?xml version=\"1.0\" encoding=\"UTF-8\"?><svg enable-background=\"new 0 0 455.978 455.978\" version=\"1.1\" viewBox=\"0 0 455.98 455.98\" xml:space=\"preserve\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"m335.95 169.7v-111.02h-191.04v111.02h191.04zm-176.04-96.019h161.04v81.02h-161.04v-81.02z\"/><rect x=\"175.94\" y=\"89.519\" width=\"128.99\" height=\"15\"/><rect x=\"175.94\" y=\"119.52\" width=\"128.99\" height=\"15\"/><path d=\"m391.08 0h-286.85c-21.682 0-39.322 17.64-39.322 39.323 0 0 3e-3 332.51 3e-3 332.51 0 21.68 17.638 39.318 39.318 39.318h22.913v44.823h55.897v-44.823h208.04l6e-3 -411.16zm-15 332.51h-264.35v-317.51h264.35v317.51zm-279.35-316.32v317.03c-6.142 1.18-11.883 3.811-16.822 7.715v-301.61c0-10.795 7.071-19.965 16.822-23.134zm7.499 379.97c-13.409 0-24.318-10.909-24.318-24.318 0-6.505 2.529-12.617 7.122-17.209 4.592-4.592 10.699-7.121 17.196-7.121h22.913v48.648h-22.913zm37.913 44.823v-93.472h25.897v93.472h-25.897zm233.94-44.823h-193.04v-48.648h193.04v48.648z\"/></svg>'),"
                                  "(7,'<svg enable-background=\"new 0 0 509.117 509.117\" version=\"1.1\" viewBox=\"0 0 509.12 509.12\" xmlns=\"http://www.w3.org/2000/svg\"><g transform=\"translate(1 1)\" fill=\"#fff\"><g fill=\"#fff\"><g fill=\"#fff\"><path d=\"m500.96 256.16c-13.017-16.488-39.919-11.281-54.671-1.736l-75.498 50.332h-115.42c-1.736-1.736-2.603-5.207-2.603-7.81 0-3.471 1.736-7.81 4.339-10.414l51.2-17.356c15.62-6.942 28.637-28.637 21.695-45.993-2.603-6.075-7.81-11.281-14.753-13.885-9.546-3.471-21.695-3.471-31.241 0.868l-77.234 26.034-93.722 8.678c-6.942 0-18.224 6.942-22.563 9.546l-59.878 25.166c-26.902 12.149-39.051 43.39-26.902 70.292l39.919 97.193c0.868 2.603 2.603 4.339 5.207 5.207 1.736 0.868 2.603 0.868 3.471 0.868 1.736 0 2.603 0 3.471-0.868l102.4-51.2h154.47c0.868 0 1.736 0 2.603-1.736l104.14-34.712c0.868 0 1.736 0 2.603-0.868l69.424-52.068c8.678-6.075 14.753-17.356 16.488-28.637 0.869-10.413-0.867-19.959-6.941-26.901zm-10.414 25.166c-0.868 6.942-4.339 13.885-9.546 17.356l-68.556 51.2-101.53 33.844h-154.47-4.339l-95.458 47.729-36.447-88.515c-7.81-18.224 0-39.051 18.224-47.729l60.746-26.034c0.868 0 0.868 0 1.736-0.868 3.471-2.603 10.414-6.942 13.017-6.942l95.458-8.678h1.736l78.969-26.034c3.471-0.868 6.942-1.736 10.414-1.736 2.603 0 5.207 0 7.81 0.868s4.339 2.603 5.207 4.339c2.603 6.942-4.339 19.959-12.149 23.431l-52.068 17.356c-0.868 0.868-1.736 0.868-2.603 1.736-7.81 6.075-12.149 15.62-12.149 26.034 0.868 9.546 5.207 17.356 13.017 22.563 0.868 0.868 2.603 1.736 4.339 1.736h121.49c1.736 0 3.471-0.868 3.471-1.736l78.102-52.068c9.546-6.075 26.902-8.678 32.108-1.736 3.469 3.47 4.337 8.677 3.469 13.884z\"/><path d=\"m60.992 175.46h399.19c5.207 0 8.678-3.471 8.678-8.678v-104.14c0-5.207-3.471-8.678-8.678-8.678h-399.19c-5.207 0-8.678 3.471-8.678 8.678v104.14c0 5.206 3.471 8.678 8.678 8.678zm95.457-17.356v-86.78h208.27v86.78h-208.27zm295.05 0h-34.712v-86.78h34.712v86.78zm-52.067-86.78v86.78h-17.356v-86.78h17.356zm-260.34 0v86.78h-17.356v-86.78h17.356zm-69.424 0h34.712v86.78h-34.712v-86.78z\"/><path d=\"m321.33 106.03h-121.49c-5.207 0-8.678 3.471-8.678 8.678s3.471 8.678 8.678 8.678h121.49c5.207 0 8.678-3.471 8.678-8.678s-3.471-8.678-8.678-8.678z\"/></g></g></g></svg>','<svg enable-background=\"new 0 0 509.117 509.117\" version=\"1.1\" viewBox=\"0 0 509.12 509.12\" xmlns=\"http://www.w3.org/2000/svg\"><g transform=\"translate(1 1)\"><path d=\"m500.96 256.16c-13.017-16.488-39.919-11.281-54.671-1.736l-75.498 50.332h-115.42c-1.736-1.736-2.603-5.207-2.603-7.81 0-3.471 1.736-7.81 4.339-10.414l51.2-17.356c15.62-6.942 28.637-28.637 21.695-45.993-2.603-6.075-7.81-11.281-14.753-13.885-9.546-3.471-21.695-3.471-31.241 0.868l-77.234 26.034-93.722 8.678c-6.942 0-18.224 6.942-22.563 9.546l-59.878 25.166c-26.902 12.149-39.051 43.39-26.902 70.292l39.919 97.193c0.868 2.603 2.603 4.339 5.207 5.207 1.736 0.868 2.603 0.868 3.471 0.868 1.736 0 2.603 0 3.471-0.868l102.4-51.2h154.47c0.868 0 1.736 0 2.603-1.736l104.14-34.712c0.868 0 1.736 0 2.603-0.868l69.424-52.068c8.678-6.075 14.753-17.356 16.488-28.637 0.869-10.413-0.867-19.959-6.941-26.901zm-10.414 25.166c-0.868 6.942-4.339 13.885-9.546 17.356l-68.556 51.2-101.53 33.844h-154.47-4.339l-95.458 47.729-36.447-88.515c-7.81-18.224 0-39.051 18.224-47.729l60.746-26.034c0.868 0 0.868 0 1.736-0.868 3.471-2.603 10.414-6.942 13.017-6.942l95.458-8.678h1.736l78.969-26.034c3.471-0.868 6.942-1.736 10.414-1.736 2.603 0 5.207 0 7.81 0.868s4.339 2.603 5.207 4.339c2.603 6.942-4.339 19.959-12.149 23.431l-52.068 17.356c-0.868 0.868-1.736 0.868-2.603 1.736-7.81 6.075-12.149 15.62-12.149 26.034 0.868 9.546 5.207 17.356 13.017 22.563 0.868 0.868 2.603 1.736 4.339 1.736h121.49c1.736 0 3.471-0.868 3.471-1.736l78.102-52.068c9.546-6.075 26.902-8.678 32.108-1.736 3.469 3.47 4.337 8.677 3.469 13.884z\"/><path d=\"m60.992 175.46h399.19c5.207 0 8.678-3.471 8.678-8.678v-104.14c0-5.207-3.471-8.678-8.678-8.678h-399.19c-5.207 0-8.678 3.471-8.678 8.678v104.14c0 5.206 3.471 8.678 8.678 8.678zm95.457-17.356v-86.78h208.27v86.78h-208.27zm295.05 0h-34.712v-86.78h34.712v86.78zm-52.067-86.78v86.78h-17.356v-86.78h17.356zm-260.34 0v86.78h-17.356v-86.78h17.356zm-69.424 0h34.712v86.78h-34.712v-86.78z\"/><path d=\"m321.33 106.03h-121.49c-5.207 0-8.678 3.471-8.678 8.678s3.471 8.678 8.678 8.678h121.49c5.207 0 8.678-3.471 8.678-8.678s-3.471-8.678-8.678-8.678z\"/></g></svg>');"));
        query.exec(QStringLiteral("DELETE FROM 'tag';"));
        query.exec(QStringLiteral("INSERT INTO 'tag' ('id','name','id_icon') VALUES (1,'Favorite',1),(4,'Reading',4),(5,'To read',5),(6,'Read',6);"));
        query.exec(QStringLiteral("CREATE TABLE book_tag (id_book INTEGER NOT NULL, id_tag INTEGER NOT NULL, FOREIGN KEY (id_tag) REFERENCES tag (id) ON DELETE CASCADE, FOREIGN KEY (id_book) REFERENCES book (id) ON DELETE CASCADE, UNIQUE (id_book, id_tag) ON CONFLICT IGNORE);"));
        query.exec(QStringLiteral("CREATE TABLE seria_tag (id_seria INTEGER NOT NULL, id_tag INTEGER NOT NULL, FOREIGN KEY (id_seria) REFERENCES seria (id) ON DELETE CASCADE, FOREIGN KEY (id_tag) REFERENCES tag (id) ON DELETE CASCADE, UNIQUE (id_seria, id_tag) ON CONFLICT IGNORE);"));
        query.exec(QStringLiteral("CREATE TABLE author_tag (id_author INTEGER NOT NULL, id_tag INTEGER NOT NULL, FOREIGN KEY (id_tag) REFERENCES tag (id) ON DELETE CASCADE, FOREIGN KEY (id_author) REFERENCES author (id) ON DELETE CASCADE, UNIQUE (id_author, id_tag) ON CONFLICT IGNORE);"));

        query.exec(QStringLiteral("INSERT OR REPLACE INTO params (id, name, value) VALUES ((SELECT id FROM params WHERE name = 'version_minor'), 'version_minor', 4)"));
    }else
    if(nMinorVersion < 4){
        query.exec(QStringLiteral("PRAGMA foreign_keys = 0;"));
        query.exec(QStringLiteral("BEGIN TRANSACTION;"));
        query.exec(QStringLiteral("CREATE TABLE temp_table AS SELECT * FROM book_tag;"));
        query.exec(QStringLiteral("DROP TABLE book_tag;"));
        query.exec(QStringLiteral("CREATE TABLE book_tag (id_book INTEGER NOT NULL, id_tag INTEGER NOT NULL, FOREIGN KEY (id_tag) REFERENCES tag (id) ON DELETE CASCADE, FOREIGN KEY (id_book) REFERENCES book (id) ON DELETE CASCADE, UNIQUE (id_book, id_tag) ON CONFLICT IGNORE);"));
        query.exec(QStringLiteral("INSERT INTO book_tag (id_book, id_tag) SELECT id_book, id_tag FROM temp_table;"));
        query.exec(QStringLiteral("DROP TABLE temp_table;"));

        query.exec(QStringLiteral("CREATE TABLE temp_table AS SELECT * FROM author_tag;"));
        query.exec(QStringLiteral("DROP TABLE author_tag;"));
        query.exec(QStringLiteral("CREATE TABLE author_tag (id_author INTEGER NOT NULL, id_tag INTEGER NOT NULL, FOREIGN KEY (id_tag) REFERENCES tag (id) ON DELETE CASCADE, FOREIGN KEY (id_author) REFERENCES author (id) ON DELETE CASCADE, UNIQUE (id_author, id_tag) ON CONFLICT IGNORE);"));
        query.exec(QStringLiteral("INSERT INTO author_tag (id_author, id_tag) SELECT id_author, id_tag FROM temp_table;"));
        query.exec(QStringLiteral("DROP TABLE temp_table;"));

        query.exec(QStringLiteral("CREATE TABLE temp_table AS SELECT * FROM seria_tag;"));
        query.exec(QStringLiteral("DROP TABLE seria_tag;"));
        query.exec(QStringLiteral("CREATE TABLE seria_tag (id_seria INTEGER NOT NULL, id_tag INTEGER NOT NULL, FOREIGN KEY (id_seria) REFERENCES seria (id) ON DELETE CASCADE, FOREIGN KEY (id_tag) REFERENCES tag (id) ON DELETE CASCADE, UNIQUE (id_seria, id_tag) ON CONFLICT IGNORE);"));
        query.exec(QStringLiteral("INSERT INTO seria_tag (id_seria, id_tag) SELECT id_seria, id_tag FROM temp_table;"));
        query.exec(QStringLiteral("DROP TABLE temp_table;"));

        query.exec(QStringLiteral("COMMIT;"));
    }
    if(nMinorVersion < 5){
        query.exec(QStringLiteral("PRAGMA foreign_keys = 0;"));
        query.exec(QStringLiteral("BEGIN TRANSACTION;"));

        query.exec(QStringLiteral("CREATE TABLE temp_table AS SELECT * FROM book_genre;"));
        query.exec(QStringLiteral("DROP TABLE book_genre;"));
        query.exec(QStringLiteral("CREATE TABLE book_genre (id_book INTEGER, id_genre INTEGER, id_lib INTEGER, PRIMARY KEY (id_book, id_genre, id_lib), FOREIGN KEY (id_lib) REFERENCES lib (id) ON DELETE CASCADE, FOREIGN KEY (id_book) REFERENCES book (id) ON DELETE CASCADE);"));
        query.exec(QStringLiteral("INSERT INTO book_genre (id_book, id_genre, id_lib) SELECT id_book, id_genre, id_lib FROM temp_table;"));
        query.exec(QStringLiteral("DROP TABLE temp_table;"));
        query.exec(QStringLiteral("CREATE INDEX book_genre_id ON book_genre (\"id_genre\" ASC);"));
        query.exec(QStringLiteral("CREATE INDEX book_genre_id_book ON book_genre (\"id_book\" ASC);"));

        query.exec(QStringLiteral("UPDATE genre SET keys='prose;prose_all;' WHERE id=207;"));

        query.exec(QStringLiteral("INSERT OR REPLACE INTO params (id, name, value) VALUES ((SELECT id FROM params WHERE name = 'version_minor'), 'version_minor', 5)"));

        query.exec(QStringLiteral("COMMIT;"));
    }
    if(nMinorVersion < 3){
        query.exec(QStringLiteral("BEGIN TRANSACTION;"));

        query.exec(QStringLiteral("CREATE TABLE temp_table AS SELECT * FROM lib;"));
        query.exec(QStringLiteral("DROP TABLE lib;"));
        query.exec(QStringLiteral("CREATE TABLE lib (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, name TEXT, path TEXT, inpx TEXT, version TEXT, firstAuthor BOOL, woDeleted BOOL);"));
        query.exec(QStringLiteral("INSERT INTO lib (id, name, path, inpx, firstAuthor, woDeleted) SELECT id, name, path, inpx, firstAuthor, woDeleted FROM temp_table;"));
        query.exec(QStringLiteral("DROP TABLE temp_table;"));

        query.exec(QStringLiteral("COMMIT;"));
    }
    if(nMinorVersion < 2){
        query.exec(QStringLiteral("BEGIN TRANSACTION;"));

        query.exec(QStringLiteral("CREATE TABLE temp_table AS SELECT * FROM author;"));
        query.exec(QStringLiteral("DROP TABLE author;"));
        query.exec(QStringLiteral("CREATE TABLE author (id INTEGER, name1 TEXT, name2 TEXT, name3 TEXT, id_lib INTEGER, FOREIGN KEY (id_lib) REFERENCES lib (id) ON DELETE CASCADE, PRIMARY KEY (id));"));
        query.exec(QStringLiteral("INSERT INTO author (id, name1, name2, name3, id_lib) SELECT id, name1, name2, name3, id_lib FROM temp_table;"));
        query.exec(QStringLiteral("DROP TABLE temp_table;"));
        query.exec(QStringLiteral("CREATE INDEX author_sort ON author (\"name1\" ASC, \"name2\" ASC, \"name3\" ASC);"));

        query.exec(QStringLiteral("CREATE TABLE temp_table AS SELECT * FROM book;"));
        query.exec(QStringLiteral("DROP TABLE book;"));
        query.exec(QStringLiteral("CREATE TABLE book (id INTEGER, name TEXT, star INTEGER, id_seria INTEGER, num_in_seria INTEGER, language TEXT, id_lib INTEGER, file TEXT, size INTEGER, deleted BOOL, date DATETIME, format TEXT, keys TEXT, id_inlib INTEGER, archive TEXT, first_author_id INTEGER, FOREIGN KEY (id_lib) REFERENCES lib (id) ON DELETE CASCADE, PRIMARY KEY (id), FOREIGN KEY (id_seria) REFERENCES seria (id) ON DELETE CASCADE);"));
        query.exec(QStringLiteral("INSERT INTO book (id, name, star, id_seria, num_in_seria, language, id_lib, file, size, deleted, date, format, keys, id_inlib, archive, first_author_id) SELECT id, name, star, id_seria, num_in_seria, language, id_lib, file, size, deleted, date, format, keys, id_inlib, archive, first_author_id FROM temp_table;"));
        query.exec(QStringLiteral("DROP TABLE temp_table;"));
        query.exec(QStringLiteral("CREATE INDEX book_file ON book (\"id_lib\", \"file\", \"archive\");"));
        query.exec(QStringLiteral("CREATE INDEX book_first_author ON book (\"first_author_id\" ASC);"));
        query.exec(QStringLiteral("CREATE INDEX book_seria ON book (\"id_seria\" ASC);"));

        query.exec(QStringLiteral("CREATE TABLE temp_table AS SELECT * FROM book_author;"));
        query.exec(QStringLiteral("DROP TABLE book_author;"));
        query.exec(QStringLiteral("CREATE TABLE book_author (id_book INTEGER, id_author INTEGER, id_lib INTEGER, FOREIGN KEY (id_lib) REFERENCES lib (id) ON DELETE CASCADE, FOREIGN KEY (id_author) REFERENCES author (id), PRIMARY KEY (id_book, id_author, id_lib), FOREIGN KEY (id_book) REFERENCES book (id));"));
        query.exec(QStringLiteral("INSERT INTO book_author (id_book, id_author, id_lib) SELECT id_book, id_author, id_lib FROM temp_table;"));
        query.exec(QStringLiteral("DROP TABLE temp_table;"));
        query.exec(QStringLiteral("CREATE INDEX book_author_id_author ON book_author (\"id_author\");"));
        query.exec(QStringLiteral("CREATE INDEX book_author_id_book ON book_author (\"id_book\" ASC);"));

        query.exec(QStringLiteral("CREATE TABLE temp_table AS SELECT * FROM seria;"));
        query.exec(QStringLiteral("DROP TABLE seria;"));
        query.exec(QStringLiteral("CREATE TABLE seria (id INTEGER, name TEXT, id_lib INTEGER, PRIMARY KEY (id), FOREIGN KEY (id_lib) REFERENCES lib (id) ON DELETE CASCADE);"));
        query.exec(QStringLiteral("INSERT INTO seria (id, name, id_lib) SELECT id, name, id_lib FROM temp_table;"));
        query.exec(QStringLiteral("DROP TABLE temp_table;"));
        query.exec(QStringLiteral("CREATE INDEX seria_id ON seria (\"id\" ASC);"));
        query.exec(QStringLiteral("CREATE INDEX seria_name ON seria (\"name\" ASC, \"id_lib\" ASC);"));
        query.exec(QStringLiteral("COMMIT;"));
    }
    query.exec(QStringLiteral("PRAGMA foreign_keys = 1;"));

    return true;
}

void ClearLib(const QSqlDatabase &dbase, qlonglong id_lib, bool delete_only)
{
    QSqlQuery query(dbase);
    if(delete_only)
    {
        query.exec(QStringLiteral("update book set deleted=1 where id_lib=") + QString::number(id_lib));
    }
    else
    {
        query.exec(QStringLiteral("PRAGMA foreign_keys = ON"));
        query.exec(QStringLiteral("delete from book where id_lib=") + QString::number(id_lib));
        query.exec(QStringLiteral("delete from author where id_lib=") + QString::number(id_lib));
        query.exec(QStringLiteral("delete from seria where id_lib=") + QString::number(id_lib));
        query.exec(QStringLiteral("VACUUM"));
    }
}

QString Transliteration(QString str)
{
    str = str.trimmed();
    QString fn;
    int i, rU, rL;
    QString validChars = QStringLiteral("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890-_,.()[]{}<>!@#$%^&+=\\/");
    QString rusUpper = QStringLiteral("АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЫЭЮЯ");
    QString rusLower = QStringLiteral("абвгдеёжзийклмнопрстуфхцчшщыэюя");
    QStringList latUpper, latLower;
    latUpper <<QStringLiteral("A")<<QStringLiteral("B")<<QStringLiteral("V")<<QStringLiteral("G")<<QStringLiteral("D")<<QStringLiteral("E")<<QStringLiteral("Jo")<<QStringLiteral("Zh")<<
        QStringLiteral("Z")<<QStringLiteral("I")<<QStringLiteral("J")<<QStringLiteral("K")<<QStringLiteral("L")<<QStringLiteral("M")<<QStringLiteral("N")<<QStringLiteral("O")<<
        QStringLiteral("P")<<QStringLiteral("R")<<QStringLiteral("S")<<QStringLiteral("T")<<QStringLiteral("U")<<QStringLiteral("F")<<QStringLiteral("H")<<QStringLiteral("C")<<
        QStringLiteral("Ch")<<QStringLiteral("Sh")<<QStringLiteral("Sh")<<QStringLiteral("I")<<QStringLiteral("E")<<QStringLiteral("Ju")<<QStringLiteral("Ja");
    latLower <<QStringLiteral("a")<<QStringLiteral("b")<<QStringLiteral("v")<<QStringLiteral("g")<<QStringLiteral("d")<<QStringLiteral("e")<<QStringLiteral("jo")<<QStringLiteral("zh")<<
        QStringLiteral("z")<<QStringLiteral("i")<<QStringLiteral("j")<<QStringLiteral("k")<<QStringLiteral("l")<<QStringLiteral("m")<<QStringLiteral("n")<<QStringLiteral("o")<<
        QStringLiteral("p")<<QStringLiteral("r")<<QStringLiteral("s")<<QStringLiteral("t")<<QStringLiteral("u")<<QStringLiteral("f")<<QStringLiteral("h")<<QStringLiteral("c")<<
        QStringLiteral("ch")<<QStringLiteral("sh")<<QStringLiteral("sh")<<QStringLiteral("i")<<QStringLiteral("e")<<QStringLiteral("ju")<<QStringLiteral("ja");
    for (i=0; i < str.size(); ++i){
        if ( validChars.contains(str[i]) ){
            fn = fn + str[i];
        }else if (str[i] == ' '){  //replace spaces
            fn = fn + QStringLiteral(" ");
        }else if (str[i] == '?'){  //replace ?
            fn = fn + QStringLiteral(".");
        }else if (str[i] == '*'){  //replace *
            fn = fn + QStringLiteral(".");
        }else if (str[i] == '~'){  //replace ~
            fn = fn + QStringLiteral(".");
        }else{
            rU = rusUpper.indexOf(str[i]);
            rL = rusLower.indexOf(str[i]);
            if (rU >= 0)
                fn = fn + latUpper[rU];
            else if (rL >= 0)
                fn = fn + latLower[rL];
        }
    }
    if (fn.isEmpty() )
        fn = QStringLiteral("file");
    return fn;
}

#ifdef USE_HTTSERVER
QNetworkProxy proxy;
void setProxy()
{
    proxy.setPort(g::options.nProxyPort);
    proxy.setHostName(g::options.sProxyHost);
    proxy.setPassword(g::options.sProxyPassword);
    proxy.setUser(g::options.sProxyUser);
    switch(g::options.nProxyType)
    {
    case 0:
        proxy.setType(QNetworkProxy::NoProxy);
        break;
    case 1:
        proxy.setType(QNetworkProxy::Socks5Proxy);
        break;
    case 2:
        proxy.setType(QNetworkProxy::HttpProxy);
        break;
    }
    QNetworkProxy::setApplicationProxy(proxy);
}
#endif

QSharedPointer<QSettings> GetSettings(bool bReopen)
{
    static QSharedPointer<QSettings> pSettings;
    if(bReopen || !pSettings)
    {
        QString sFile = QApplication::applicationDirPath() + u"/freeLib.cfg"_s;
        if(!QFile::exists(sFile))
            sFile = QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation) + u"/freeLib.conf"_s;
        pSettings = QSharedPointer<QSettings> (new QSettings(sFile, QSettings::IniFormat));
        #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
        pSettings->setIniCodec("UTF-8");
        #endif
    }
    return pSettings;
}

void setLocale(const QString &sLocale)
{
    static std::unique_ptr<QTranslator> translator;
    static std::unique_ptr<QTranslator> translator_qt;

    setlocale(LC_ALL, (sLocale + QStringLiteral(".UTF-8")).toLatin1().data());
    QLocale::setDefault(QLocale(sLocale));

    QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));

    if(translator)
        QApplication::removeTranslator(translator.get());
    else
        translator = std::unique_ptr<QTranslator> (new QTranslator());

    if(translator_qt)
        QApplication::removeTranslator(translator_qt.get());
    else
        translator_qt = std::unique_ptr<QTranslator> (new QTranslator());

    QString sQmFile = QStringLiteral("/translations/language_%1.qm").arg(sLocale.left(2));
    QString sQmFileFull = QApplication::applicationDirPath() + sQmFile;
    if(!QFile::exists(sQmFileFull))
        sQmFileFull = FREELIB_DATA_DIR + sQmFile;
    if(translator->load(sQmFileFull))
        QApplication::installTranslator(translator.get());
    else
        translator.reset();

    if(translator_qt->load(QStringLiteral("qtbase_%1.qm").arg(sLocale),
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
        QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
#else
        QLibraryInfo::path(QLibraryInfo::TranslationsPath)))
#endif
    {
        QApplication::installTranslator(translator_qt.get());
    }
    else
    {
        translator_qt.reset();
    }

    g::vTags.clear();
    g::vTags.emplace_back(tag(QApplication::translate("SettingsDlg", "Top level captions"), u".h0"_s, u"top_caption_font"_s, 140));
    g::vTags.emplace_back(tag(QApplication::translate("SettingsDlg", "Captions"), u".h1,.h2,.h3,.h4,.h5,.h6"_s, u"caption_font"_s, 120));
    g::vTags.emplace_back(tag(QApplication::translate("SettingsDlg", "Dropcaps"), u"span.dropcaps"_s, u"dropcaps_font"_s, 300));
    g::vTags.emplace_back(tag(QApplication::translate("SettingsDlg", "Footnotes"), u".inlinenote,.blocknote"_s, u"footnotes_font"_s, 80));
    g::vTags.emplace_back(tag(QApplication::translate("SettingsDlg", "Annotation"), u".annotation"_s, QStringLiteral("annotation_font"), 100));
    g::vTags.emplace_back(tag(QApplication::translate("SettingsDlg", "Poems"), u".poem"_s, u"poem_font"_s, 100));
    g::vTags.emplace_back(tag(QApplication::translate("SettingsDlg", "Epigraph"), u".epigraph"_s, u"epigraph_font"_s, 100));
    g::vTags.emplace_back(tag(QApplication::translate("SettingsDlg", "Book"), u"body"_s, u"body_font"_s, 100));
}

bool setCurrentZipFileName(QuaZip *zip, const QString &name)
{
    bool result = zip->setCurrentFile(name, QuaZip::csInsensitive);
    if(!result)
    {
        zip->setFileNameCodec(QTextCodec::codecForName("IBM 866"));
        result = zip->setCurrentFile(name, QuaZip::csInsensitive);
    }
    return result;
}

bool kindlegenInstalled()
{
    //Проверка наличия kindlegen в /usr/bin
    QFileInfo fi(u"/usr/bin/kindlegen"_s);
    if(fi.isExecutable())
        return true;

    //Проверка установленного kindlegen через pkg-config
    QProcess process;
    process.start(u"pkg-config"_s, QStringList() << u"--exists"_s << u"kindlegen"_s);
    process.waitForFinished();
    return process.exitCode() == 0;
}

bool localeStringCompare(const QString &str1, const QString &str2)
{
    static QCollator collator;
    auto minSize = std::min(str1.size(), str2.size());
    for(uint i=0; i<minSize; ++i){
        auto ch1 = str1.at(i);
        auto ch2 = str2.at(i);
        bool bLetter1 = ch1.isLetter();
        bool bLetter2 = ch2.isLetter();
        if(bLetter1 && bLetter2)
            return collator.compare(str1, str2) < 0;
        if(bLetter1 && !bLetter2)
            return true;
        if(!bLetter1 && bLetter2)
            return false;
    }
    return collator.compare(str1, str2) < 0;
}

