5#include <QtCore/QFileInfo>
6#include <QtCore/QStringBuilder>
7#include <QtWidgets/QMessageBox>
13using namespace Qt::StringLiterals;
15static const char helpMessage[] = QT_TRANSLATE_NOOP(
"CmdLineParser",
16 "Usage: assistant [Options]\n\n"
17 "-collectionFile file Uses the specified collection\n"
18 " file instead of the default one\n"
19 "-showUrl url Shows the document with the\n"
21 "-enableRemoteControl Enables Assistant to be\n"
22 " remotely controlled.\n"
23 "-show widget Shows the specified dockwidget\n"
24 " which can be \"contents\", \"index\",\n"
25 " \"bookmarks\" or \"search\".\n"
26 "-activate widget Activates the specified dockwidget\n"
27 " which can be \"contents\", \"index\",\n"
28 " \"bookmarks\" or \"search\".\n"
29 "-hide widget Hides the specified dockwidget\n"
30 " which can be \"contents\", \"index\"\n"
31 " \"bookmarks\" or \"search\".\n"
32 "-register helpFile Registers the specified help file\n"
33 " (.qch) in the given collection\n"
35 "-unregister helpFile Unregisters the specified help file\n"
36 " (.qch) from the give collection\n"
38 "-setCurrentFilter filter Set the filter as the active filter.\n"
39 "-remove-search-index Removes the full text search index.\n"
40 "-rebuild-search-index Obsolete. Use -remove-search-index instead.\n"
41 " Removes the full text search index.\n"
42 " It will be rebuilt on next Assistant run.\n"
43 "-quiet Does not display any error or\n"
45 "-help Displays this help.\n"
51 m_enableRemoteControl(
false),
57 m_removeSearchIndex(
false),
61 for (
int i = 1; i < arguments.size(); ++i) {
62 const QString &arg = arguments.at(i);
63 if (arg.toLower() ==
"-quiet")
66 m_arguments.append(arg);
73 bool showHelp =
false;
75 while (m_error.isEmpty() && hasMoreArgs()) {
76 const QString &arg = nextArg().toLower();
77 if (arg ==
"-collectionfile"_L1)
78 handleCollectionFileOption();
79 else if (arg ==
"-showurl"_L1)
80 handleShowUrlOption();
81 else if (arg ==
"-enableremotecontrol"_L1)
82 m_enableRemoteControl =
true;
83 else if (arg ==
"-show"_L1)
85 else if (arg ==
"-hide"_L1)
87 else if (arg ==
"-activate"_L1)
88 handleActivateOption();
89 else if (arg ==
"-register"_L1)
90 handleRegisterOption();
91 else if (arg ==
"-unregister"_L1)
92 handleUnregisterOption();
93 else if (arg ==
"-setcurrentfilter"_L1)
94 handleSetCurrentFilterOption();
95 else if (arg ==
"-remove-search-index"_L1)
96 m_removeSearchIndex =
true;
97 else if (arg ==
"-rebuild-search-index"_L1)
98 m_removeSearchIndex =
true;
99 else if (arg ==
"-help"_L1)
102 m_error = tr(
"Unknown option: %1").arg(arg);
105 if (!m_error.isEmpty()) {
106 showMessage(m_error +
"\n\n\n"_L1 + tr(helpMessage),
true);
108 }
else if (showHelp) {
109 showMessage(tr(helpMessage),
false);
118 return m_pos < m_arguments.size();
124 Q_ASSERT(hasMoreArgs());
125 return m_arguments.at(m_pos++);
132 const QString &fileName = nextArg();
133 m_collectionFile = getFileName(fileName);
134 if (m_collectionFile.isEmpty())
135 m_error = tr(
"The collection file '%1' does not exist.").
138 m_error = tr(
"Missing collection file.");
146 const QString &urlString = nextArg();
151 m_error = tr(
"Invalid URL '%1'.").arg(urlString);
153 m_error = tr(
"Missing URL.");
160 handleShowOrHideOrActivateOption(
Show);
166 handleShowOrHideOrActivateOption(
Hide);
172 handleShowOrHideOrActivateOption(
Activate);
179 const QString &widget = nextArg().toLower();
180 if (widget ==
"contents"_L1)
182 else if (widget ==
"index"_L1)
184 else if (widget ==
"bookmarks"_L1)
186 else if (widget ==
"search"_L1)
189 m_error = tr(
"Unknown widget: %1").arg(widget);
191 m_error = tr(
"Missing widget.");
198 handleRegisterOrUnregisterOption(
Register);
211 const QString &fileName = nextArg();
212 m_helpFile = getFileName(fileName);
213 if (m_helpFile.isEmpty())
214 m_error = tr(
"The Qt help file '%1' does not exist.").arg(fileName);
218 m_error = tr(
"Missing help file.");
226 m_currentFilter = nextArg();
228 m_error = tr(
"Missing filter argument.");
234 QFileInfo fi(fileName);
237 return fi.absoluteFilePath();
246 QString message =
"<pre>"_L1 % msg %
"</pre>"_L1;
248 QMessageBox::critical(0, tr(
"Error"), message);
250 QMessageBox::information(0, tr(
"Notice"), message);
252 fprintf(error ? stderr : stdout,
"%s\n", qPrintable(msg));
259 m_collectionFile = file;
265 return m_collectionFile;
271 return m_arguments.contains(
"-collectionfile"_L1, Qt::CaseInsensitive);
283 return m_enableRemoteControl;
313 return m_currentFilter;
319 return m_removeSearchIndex;
RegisterState registerRequest() const
void showMessage(const QString &msg, bool error)
ShowState contents() const
CmdLineParser(const QStringList &arguments)
QString currentFilter() const
ShowState bookmarks() const
QString collectionFile() const
bool enableRemoteControl() const
void setCollectionFile(const QString &file)
bool collectionFileGiven() const
bool removeSearchIndex() const
static const char helpMessage[]
Combined button and popup list for selecting options.