File indexing completed on 2024-04-21 15:08:51

0001 /***************************************************************************
0002  *   Copyright (C) 2013-2016 by Daniel Nicoletti <dantti12@gmail.com>      *
0003  *                                                                         *
0004  *   This program is free software; you can redistribute it and/or modify  *
0005  *   it under the terms of the GNU General Public License as published by  *
0006  *   the Free Software Foundation; either version 2 of the License, or     *
0007  *   (at your option) any later version.                                   *
0008  *                                                                         *
0009  *   This program is distributed in the hope that it will be useful,       *
0010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0012  *   GNU General Public License for more details.                          *
0013  *                                                                         *
0014  *   You should have received a copy of the GNU General Public License     *
0015  *   along with this program; see the file COPYING. If not, write to       *
0016  *   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,  *
0017  *   Boston, MA 02110-1301, USA.                                           *
0018  ***************************************************************************/
0019 
0020 #include "../colord-kcm/Profile.h"
0021 #include "CdInterface.h"
0022 
0023 #include <stdlib.h>
0024 
0025 #include <QApplication>
0026 #include <QCommandLineParser>
0027 #include <QDebug>
0028 #include <QFileInfo>
0029 
0030 #include <KAboutData>
0031 #include <KLocalizedString>
0032 #include <KMessageBox>
0033 
0034 #include <kwidgetsaddons_version.h>
0035 #include <version.h>
0036 
0037 QString message(const QString &title, const QString &description, const QString &copyright)
0038 {
0039     QString ret = QLatin1String("<p><strong>") % title % QLatin1String("</strong></p>");
0040     if (!description.isEmpty()) {
0041         ret.append(i18n("Description: %1", description));
0042         if (copyright.isEmpty()) {
0043             return ret;
0044         }
0045     }
0046 
0047     if (!copyright.isEmpty()) {
0048         if (!description.isEmpty()) {
0049             ret.append(QLatin1String("<br>"));
0050         }
0051         ret.append(i18n("Copyright: %1", copyright));
0052     }
0053 
0054     return ret;
0055 }
0056 
0057 int main(int argc, char **argv)
0058 {
0059     QApplication app(argc, argv);
0060     QApplication::setWindowIcon(QIcon::fromTheme(QStringLiteral("application-vnd.iccprofile")));
0061     KAboutData about(QStringLiteral("colord-kde-icc-importer"),
0062                      i18n("ICC Profile Installer"),
0063                      QLatin1String(COLORD_KDE_VERSION_STRING),
0064                      i18n("An application to install ICC profiles"),
0065                      KAboutLicense::GPL,
0066                      i18n("(C) 2008-2016 Daniel Nicoletti"));
0067 
0068     about.addAuthor(QStringLiteral("Daniel Nicoletti"), QString(), QStringLiteral("dantti12@gmail.com"), QStringLiteral("http://dantti.wordpress.com"));
0069     about.addCredit(QStringLiteral("Lukáš Tinkl"), i18n("Port to kf5"), QStringLiteral("ltinkl@redhat.com"));
0070 
0071     KAboutData::setApplicationData(about);
0072 
0073     QCommandLineParser parser;
0074     about.setupCommandLine(&parser);
0075     parser.addVersionOption();
0076     parser.addHelpOption();
0077     parser.addOption(QCommandLineOption(QStringLiteral("yes"), i18n("Do not prompt the user if he wants to install")));
0078     parser.addPositionalArgument("file", i18n("Color profile to install"), "+file");
0079     parser.process(app);
0080     about.processCommandLine(&parser);
0081 
0082     const QStringList args = parser.positionalArguments();
0083     if (args.isEmpty()) {
0084         parser.showHelp(EXIT_FAILURE);
0085     }
0086 
0087     QFileInfo fileInfo(args.first());
0088     // ~/.local/share/icc/
0089     const QString destFilename = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) % QLatin1String("/icc/") % fileInfo.fileName();
0090 
0091     Profile profile(fileInfo.filePath());
0092     if (!profile.loaded()) {
0093         KMessageBox::error(nullptr, i18n("Failed to parse file: %1", profile.errorMessage()), i18n("Failed to open ICC profile"));
0094         return 1;
0095     }
0096     qDebug() << fileInfo.filePath();
0097     qDebug() << destFilename;
0098     qDebug() << profile.checksum();
0099 
0100     if (QFile::exists(destFilename)) {
0101         KMessageBox::error(nullptr,
0102                            message(i18n("Color profile is already imported"), profile.description(), profile.copyright()),
0103                            i18n("ICC Profile Importer"));
0104         return 3;
0105     }
0106 
0107     CdInterface interface(QStringLiteral("org.freedesktop.ColorManager"), QStringLiteral("/org/freedesktop/ColorManager"), QDBusConnection::systemBus());
0108     QDBusReply<QDBusObjectPath> reply = interface.FindProfileById(profile.checksum());
0109     qDebug() << reply.error().message();
0110     if (reply.isValid() && reply.error().type() != QDBusError::NoError) {
0111         KMessageBox::error(nullptr,
0112                            message(i18n("ICC profile already installed system-wide"), profile.description(), profile.copyright()),
0113                            i18n("ICC Profile Importer"));
0114         return 3;
0115     }
0116 
0117     if (!parser.isSet(QStringLiteral("yes"))) {
0118         const int ret =
0119             KMessageBox::questionTwoActions(nullptr,
0120                                             message(i18n("Would you like to import the color profile?"), profile.description(), profile.copyright()),
0121                                             i18n("ICC Profile Importer"),
0122                                             KGuiItem(i18n("import")),
0123                                             KStandardGuiItem::cancel());
0124         if (ret == KMessageBox::ButtonCode::SecondaryAction) {
0125             return 2;
0126         }
0127     }
0128 
0129     if (!QFile::copy(fileInfo.filePath(), destFilename)) {
0130         if (QFile::exists(destFilename)) {
0131             KMessageBox::error(nullptr, i18n("Failed to import color profile: color profile is already imported"), i18n("Importing Color Profile"));
0132             return 3;
0133         } else {
0134             KMessageBox::error(nullptr, i18n("Failed to import color profile: could not copy the file"), i18n("Importing Color Profile"));
0135             return 3;
0136         }
0137     }
0138 
0139     return EXIT_SUCCESS;
0140 }