Debian Codegraph @

src/main.cc

raw
1 /*
2 * This file is part of the apvlv package
3 *
4 * Copyright (C) 2008 Alf.
5 *
6 * Contact: Alf <[email protected]>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2.0 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public
19 * License along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 *
22 */
23 /*@CPPFILE main.cpp Apvlv start at here
24 *
25 * Author: Alf <[email protected]>
26 */
27
28 #include <filesystem>
29
30 #include <QApplication>
31 #include <QCommandLineParser>
32 #include <QDir>
33 #include <QLocale>
34 #include <QMessageBox>
35 #include <QTranslator>
36 #include <QWebEngineUrlScheme>
37
38 #include "ApvlvInfo.h"
39 #include "ApvlvLog.h"
40 #include "ApvlvParams.h"
41 #include "ApvlvUtil.h"
42 #include "ApvlvView.h"
43
44 using namespace std;
45 using namespace apvlv;
46
47 #if defined WIN32 && defined NDEBUG
48 #pragma comment(linker, "/subsystem:windows")
49 #pragma comment(linker, "/ENTRY:mainCRTStartup")
50 #endif
51
52 static void
53 registerUrlScheme ()
54 {
55 QWebEngineUrlScheme scheme ("apvlv");
56 scheme.setSyntax (QWebEngineUrlScheme::Syntax::Path);
57 QWebEngineUrlScheme::registerScheme (scheme);
58 }
59
60 static void
61 usageExit ()
62 {
63 std::cout << PACKAGE_NAME << " [options] paths" << endl;
64 std::cout << endl;
65 std::cout << "Options: " << endl;
66 std::cout << "\t-h display this and exit\n"
67 "\t-v display version info and exit\n"
68 "\t-c [file] set user configuration file\n"
69 "\t paths document path list"
70 << endl;
71 FileFactory::typeEngineDescription (std::cout);
72 std::cout << "Please send bug report to " << PACKAGE_BUGREPORT << endl;
73 exit (0);
74 }
75
76 static void
77 versionExit ()
78 {
79 fprintf (stdout,
80 "%s %s-%s\n"
81 "Please send bug report to %s\n"
82 "\n",
83 PACKAGE_NAME, PACKAGE_VERSION, RELEASE, PACKAGE_BUGREPORT);
84 exit (0);
85 }
86
87 static list<string>
88 parseCommandLine (const QCoreApplication &app)
89 {
90 QCommandLineParser parser;
91
92 auto versionOption = QCommandLineOption (QStringList () << "v" << "version",
93 QObject::tr ("version number"));
94 parser.addOption (versionOption);
95
96 auto helpOption = QCommandLineOption (QStringList () << "h" << "help",
97 QObject::tr ("help information"));
98 parser.addOption (helpOption);
99
100 auto configFileOption
101 = QCommandLineOption (QStringList () << "c" << "config-file",
102 QObject::tr ("config file"), "config");
103 parser.addOption (configFileOption);
104
105 auto logFileOption = QCommandLineOption (QStringList () << "l" << "log-file",
106 QObject::tr ("log file"), "log");
107 parser.addOption (logFileOption);
108
109 parser.addPositionalArgument ("path", QObject::tr ("document path"));
110
111 parser.process (app);
112
113 if (parser.isSet (helpOption))
114 {
115 usageExit ();
116 }
117 if (parser.isSet (versionOption))
118 {
119 versionExit ();
120 }
121 if (parser.isSet (configFileOption))
122 {
123 auto value = parser.value (configFileOption);
124 IniFile = filesystem::absolute (value.toStdString ()).string ();
125 }
126 if (parser.isSet (logFileOption))
127 {
128 auto value = parser.value (logFileOption);
129 LogFile = filesystem::absolute (value.toStdString ()).string ();
130 }
131
132 /*
133 * load the global sys conf file
134 * */
135 auto sysIni = string (SYSCONFDIR) + "/apvlvrc";
136 auto params = ApvlvParams::instance ();
137 params->loadFile (sysIni);
138
139 /*
140 * load the user conf file
141 * */
142 qDebug () << "using config: " << IniFile;
143 ApvlvParams::instance ()->loadFile (IniFile);
144
145 list<string> paths;
146 auto pathlist = parser.positionalArguments ();
147 for (const auto &path : pathlist)
148 {
149 paths.emplace_back (path.toStdString ());
150 }
151
152 return paths;
153 }
154
155 static void
156 loadTranslator (QTranslator &translator)
157 {
158 map<string, string> lanuage_translator{ { "Chinese", "zh_CN" } };
159 auto lan = QLocale::system ().language ();
160 auto lanstr = QLocale::languageToString (lan).toStdString ();
161 if (lanuage_translator.find (lanstr) != lanuage_translator.end ())
162 {
163 auto lantrans = lanuage_translator[lanstr];
164 if (!translator.load (QString::fromLocal8Bit (lantrans),
165 QString::fromLocal8Bit (Translations)))
166 {
167 qWarning () << "Load i18n file failed, using English";
168 }
169 else
170 {
171 QCoreApplication::installTranslator (&translator);
172 }
173 }
174 }
175
176 int
177 main (int argc, char *argv[])
178 {
179 registerUrlScheme ();
180
181 QApplication app (argc, argv);
182
183 getRuntimePaths ();
184
185 QTranslator translator;
186 loadTranslator (translator);
187
188 ApvlvInfo::instance ()->loadFile (SessionFile);
189
190 auto paths = parseCommandLine (app);
191
192 NotesDir = ApvlvParams::instance ()->getGroupStringOrDefault ("notes", "dir",
193 NotesDir);
194
195 ApvlvLog::instance ()->setLogFile (LogFile);
196
197 string path = HelpPdf;
198 if (!paths.empty ())
199 {
200 path = paths.front ();
201 paths.pop_front ();
202 }
203 if (!filesystem::is_regular_file (path) && !filesystem::is_directory (path))
204 {
205 qFatal () << "File '" << path << "' is not readable.";
206 return 1;
207 }
208
209 ApvlvView sView (nullptr);
210 if (!sView.newTab (path))
211 {
212 exit (1);
213 }
214
215 while (!paths.empty ())
216 {
217 path = paths.front ();
218 paths.pop_front ();
219 auto apath = filesystem::absolute (path).string ();
220 if (!sView.newTab (apath))
221 {
222 qCritical () << "Can't open document: " << apath;
223 }
224 }
225
226 QApplication::exec ();
227
228 return 0;
229 }
230
231 // Local Variables:
232 // mode: c++
233 // End: