File indexing completed on 2022-09-27 16:24:38

0001 /***************************************************************************
0002  *   Copyright (C) 2009-2011 by Daniel Nicoletti                           *
0003  *   dantti12@gmail.com                                                    *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  *                                                                         *
0010  *   This program is distributed in the hope that it will be useful,       *
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0013  *   GNU General Public License for more details.                          *
0014  *                                                                         *
0015  *   You should have received a copy of the GNU General Public License     *
0016  *   along with this program; see the file COPYING. If not, write to       *
0017  *   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,  *
0018  *   Boston, MA 02110-1301, USA.                                           *
0019  ***************************************************************************/
0020 
0021 #include "PkInstallFontconfigResources.h"
0022 
0023 #include "IntroDialog.h"
0024 #include <Daemon>
0025 #include <PkStrings.h>
0026 
0027 #include <QStandardItemModel>
0028 #include <QXmlQuery>
0029 #include <QFile>
0030 
0031 #include <KLocalizedString>
0032 #include <QLoggingCategory>
0033 
0034 Q_DECLARE_LOGGING_CATEGORY(APPER_SESSION)
0035 
0036 PkInstallFontconfigResources::PkInstallFontconfigResources(uint xid,
0037                                                            const QStringList &resources,
0038                                                            const QString &interaction,
0039                                                            const QDBusMessage &message,
0040                                                            QWidget *parent)
0041  : SessionTask(xid, interaction, message, parent)
0042 {
0043     setWindowTitle(i18n("Installs new Fonts"));
0044 
0045     auto introDialog = new IntroDialog(this);
0046     auto model = new QStandardItemModel(this);
0047     introDialog->setModel(model);
0048     connect(introDialog, &IntroDialog::continueChanged, this, &PkInstallFontconfigResources::enableButtonOk);
0049     setMainWidget(introDialog);
0050 
0051     // This will only validate fields
0052     QStringList errors;
0053     QStringList iso639;
0054     for (const QString &font : resources) {
0055         // TODO never return in here
0056         // TODO add name field from /usr/share/xml/iso-codes/iso_639.xml into model
0057         if (!font.startsWith(QLatin1String(":lang="))) {
0058             errors << QString(QLatin1String("not recognised prefix: '%1'")).arg(font);
0059             qCWarning(APPER_SESSION) << QString(QLatin1String("not recognised prefix: '%1'")).arg(font);
0060             continue;
0061         }
0062         int size = font.size();
0063         if (size < 7 || size > 20) {
0064             errors << QString(QLatin1String("lang tag malformed: '%1'")).arg(font);
0065             qCWarning(APPER_SESSION) << QString(QLatin1String("lang tag malformed: '%1'")).arg(font);
0066             continue;
0067         }
0068 
0069         m_resources << font;
0070         iso639 << font.mid(6);
0071     }
0072 
0073     if (m_resources.isEmpty()) {
0074         setError(i18n("Could interpret request"), i18n("Please verify if the request was valid"));
0075         sendErrorFinished(InternalError, errors.join(QLatin1Char('\n')));
0076         return;
0077     }
0078     enableButtonOk(true);
0079 
0080     // Search for the iso 639 names to present it nicely to the user
0081     QStringList niceNames;
0082     QFile file(QLatin1String("/usr/share/xml/iso-codes/iso_639.xml"));
0083     file.open(QFile::ReadOnly);
0084     QXmlQuery query;
0085     query.bindVariable(QLatin1String("path"), &file);
0086     for (const QString &font : iso639) {
0087         QString queryTxt;
0088         queryTxt = QString(QLatin1String("declare variable $path external;"
0089                                          "doc($path)/iso_639_entries/"
0090                                          "iso_639_entry[@iso_639_2B_code=\"%1\" or "
0091                                          "@iso_639_2T_code=\"%1\" or "
0092                                          "@iso_639_1_code=\"%1\"]/string(@name)")).arg(font);
0093         query.setQuery(queryTxt);
0094         QStringList result;
0095         query.evaluateTo(&result);
0096         niceNames.append(result);
0097     }
0098 
0099 //    kDebug() << "result" << niceNames << iso639;
0100     for (const QString &name : niceNames) {
0101         auto item = new QStandardItem(name);
0102         item->setIcon(QIcon::fromTheme(QLatin1String("fonts-package")).pixmap(32, 32));
0103         item->setFlags(Qt::ItemIsEnabled);
0104         model->appendRow(item);
0105     }
0106 
0107     QString description;
0108     description = i18np("An additional font is required to view this document correctly.\n"
0109                         "Do you want to search for a suitable package now?",
0110                         "Additional fonts are required to view this document correctly.\n"
0111                         "Do you want to search for suitable packages now?",
0112                         m_resources.size());
0113     introDialog->setDescription(description);
0114 
0115     QString title;
0116     // this will come from DBus interface
0117     if (parentTitle.isNull()) {
0118         title = i18np("A program wants to install a font",
0119                       "A program wants to install fonts",
0120                       m_resources.size());
0121     } else {
0122         title = i18np("The application %2 wants to install a font",
0123                       "The application %2 wants to install fonts",
0124                       m_resources.size(),
0125                       parentTitle);
0126     }
0127     setTitle(title);
0128 }
0129 
0130 PkInstallFontconfigResources::~PkInstallFontconfigResources()
0131 {
0132 }
0133 
0134 void PkInstallFontconfigResources::search()
0135 {
0136     auto transaction = new PkTransaction(this);
0137     Transaction *t;
0138     t = Daemon::whatProvides(m_resources,
0139                              Transaction::FilterNotInstalled | Transaction::FilterArch | Transaction::FilterNewest);
0140     transaction->setupTransaction(t);
0141     setTransaction(Transaction::RoleWhatProvides, transaction);
0142     connect(transaction, &PkTransaction::finished, this, &PkInstallFontconfigResources::searchFinished, Qt::UniqueConnection);
0143     connect(transaction, &PkTransaction::package, this, &PkInstallFontconfigResources::addPackage);
0144 }
0145 
0146 void PkInstallFontconfigResources::notFound()
0147 {
0148     QString msg = i18n("Failed to find font");
0149     if (showWarning()) {
0150         setInfo(msg, i18n("No new fonts could be found for this document"));
0151     }
0152     sendErrorFinished(NoPackagesFound, msg);
0153 }
0154 
0155 void PkInstallFontconfigResources::searchFailed()
0156 {
0157     QString msg = i18n("Failed to find font");
0158     if (showWarning()) {
0159         setError(msg, i18n("Failed to search for provides"));
0160     }
0161     sendErrorFinished(Failed, QLatin1String("failed to search for provides"));
0162 }
0163 
0164 #include "moc_PkInstallFontconfigResources.cpp"