1 /**********************************************************************************************
2     Copyright (C) 2018 Oliver Eichler <oliver.eichler@gmx.de>
3 
4     This program is free software: you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation, either version 3 of the License, or
7     (at your option) any later version.
8 
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13 
14     You should have received a copy of the GNU General Public License
15     along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 
17 **********************************************************************************************/
18 
19 #include "CApp.h"
20 #include "version.h"
21 #include <QtCore>
22 
23 #ifdef Q_OS_MACOS
getApplicationDir(QString subdir)24 static QDir getApplicationDir(QString subdir)
25 {
26     QDir appDir(QCoreApplication::applicationDirPath());
27     appDir.cdUp();
28     appDir.cd(subdir);
29     return appDir;
30 }
31 #endif
prepareTranslator(QString translationPath,QString translationPrefix)32 static void prepareTranslator(QString translationPath, QString translationPrefix)
33 {
34     QString locale = QLocale::system().name();
35     QDir dir(translationPath);
36     if(!QFile::exists(dir.absoluteFilePath(translationPrefix + locale)))
37     {
38         locale = locale.left(2);
39     }
40 
41     QCoreApplication* app = (QCoreApplication*) QCoreApplication::instance();
42     QTranslator* qtTranslator = new QTranslator(app);
43 
44     if (qtTranslator->load(translationPrefix + locale, translationPath))
45     {
46         app->installTranslator(qtTranslator);
47     }
48 }
49 
loadTranslations()50 static void loadTranslations()
51 {
52 #if defined(Q_OS_LINUX) || defined(Q_OS_FREEBSD) || defined(__FreeBSD_kernel__) || defined(__GNU__) || defined(Q_OS_CYGWIN)
53     QString resourceDir = QLibraryInfo::location(QLibraryInfo::TranslationsPath);
54     QString translationPath = QCoreApplication::applicationDirPath();
55     translationPath.replace(QRegExp("bin$"), "share/" APP_STR "/translations");
56     prepareTranslator(resourceDir, "qt_");
57     prepareTranslator(translationPath, APP_STR "_");
58 #endif
59 
60 #ifdef Q_OS_OSX
61     // os x
62     static QString relTranslationDir = "Resources/translations"; // app
63     QString translationPath = getApplicationDir(relTranslationDir).absolutePath();
64     prepareTranslator(translationPath, "qt_");
65     prepareTranslator(translationPath, APP_STR "_");
66 #endif
67 
68 #ifdef Q_OS_WIN
69     QString apppath = QCoreApplication::applicationDirPath();
70     apppath = apppath.replace("/", "\\");
71     QString appResourceDir = QString("%1\\translations").arg(apppath).toUtf8();
72     prepareTranslator(appResourceDir, "qtbase_");
73     prepareTranslator(appResourceDir, APP_STR "_");
74 #endif
75 }
76 
main(int argc,char ** argv)77 int main(int argc, char** argv)
78 {
79     QCoreApplication app(argc, argv);
80     QCoreApplication::setApplicationName(APP_STR);
81     QCoreApplication::setApplicationVersion(VER_STR);
82     if(QString(VER_SUFFIX).isEmpty())
83     {
84         QCoreApplication::setApplicationVersion(VER_STR);
85     }
86     else
87     {
88         QCoreApplication::setApplicationVersion(VER_STR "." VER_SUFFIX);
89     }
90 
91 
92     loadTranslations();
93 
94     QCommandLineParser parser;
95     parser.setApplicationDescription(QCoreApplication::translate("main", "\nConvert a map file with RGBA color coding to a color palette coding."));
96     parser.addHelpOption();
97     parser.addVersionOption();
98     parser.addPositionalArgument("source", QCoreApplication::translate("main", "Source file."));
99     parser.addPositionalArgument("target", QCoreApplication::translate("main", "Target file."));
100 
101     parser.addOptions({
102         {
103             {"n", "ncolors"}, QCoreApplication::translate("main", "Number of colors. (default: 255)"), "number", "255"
104         },
105         {
106             {"p", "pct"}, QCoreApplication::translate("main", "Input palette file for color table (*.vrt)"), "filename", ""
107         },
108         {
109             {"s", "sct"}, QCoreApplication::translate("main", "Save color table to palette file (*.vrt)"), "filename", ""
110         },
111     });
112 
113     // Process the actual command line arguments given by the user
114     parser.process(app);
115 
116     if(parser.positionalArguments().count() == 1 && parser.value("sct").isEmpty())
117     {
118         printStderrQString("");
119         printStderrQString(QCoreApplication::translate("main", "There must be a source and destination file."));
120         printStderrQString("");
121         parser.showHelp(-1);
122     }
123 
124     if(parser.positionalArguments().isEmpty())
125     {
126         parser.showHelp(-1);
127     }
128 
129     QString srcFilename = parser.positionalArguments()[0];
130     QString tarFilename;
131     if(parser.positionalArguments().count() > 1)
132     {
133         tarFilename = parser.positionalArguments()[1];
134     }
135 
136 
137     bool ok = false;
138     const qint32 ncolors = parser.value("ncolors").toInt(&ok);
139     if(!ok || ncolors > 255)
140     {
141         printStderrQString("");
142         printStderrQString(QCoreApplication::translate("main", "--ncolors must be an integer value less than 256"));
143         printStderrQString("");
144         parser.showHelp(-1);
145     }
146 
147     QString pctFilename = parser.value("pct");
148     QString sctFilename = parser.value("sct");
149 
150     CApp theApp(ncolors, pctFilename, sctFilename, srcFilename, tarFilename);
151     return theApp.exec();
152 }
153 
154 
155