zty199 ea511893ba Improve Features
Support CommandLineParsers "--fix-size" and "--hide-buttons".
2020-11-21 22:25:20 +08:00

323 lines
10 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
*
* 如果你想学习更多有关知识,可以访问以下网址:
* DTK相关文档地址https://linuxdeepin.github.io/dtk
* Deepin项目地址https://github.com/linuxdeepin
* 社区项目投递地址https://gitee.com/deepin-opensource
*/
#include "mainwindow.h"
#include <DApplication>
#include <DMainWindow>
#include <QCommandLineParser>
#include <QCommandLineOption>
#include <QFileInfo>
#include <QSettings>
#include "globaldefine.h"
#include "httpd.h"
int main(int argc, char *argv[])
{
DApplication a(argc, argv);
a.loadTranslator();
a.setAttribute(Qt::AA_UseHighDpiPixmaps);
a.setOrganizationName("spark-union"); // 添加组织名称,和商店主体的文件夹同在 ~/.local/share/spark-union 文件夹下
a.setApplicationName("SparkWebAppRuntime"); // 这里不要翻译,否则 ~/.local/share 中文件夹名也会被翻译
DAboutDialog *dialog = new DAboutDialog;
a.setAboutDialog(dialog);
// WindowIcon
dialog->setWindowIcon(QIcon(":/images/spark-webapp-runtime.svg"));
// ProductIcon
dialog->setProductIcon(QIcon(":/images/spark-webapp-runtime.svg"));
// ProductName
dialog->setProductName(QString("<span>%1</span>").arg(DEFAULT_TITLE));
// Version
dialog->setVersion(DApplication::buildVersion(QString("%1 %2").arg(QObject::tr("Version")).arg("1.5")));
// CompanyLogo
dialog->setCompanyLogo(QPixmap(":/images/Logo-Spark.png"));
// Description
QString szDefaultDesc = QString("<a href='https://gitee.com/deepin-community-store/spark-web-app-runtime'><span style='font-size:12pt;font-weight:500;'>%1</span></a><br/>"
"<span style='font-size:12pt;'>%2</span>")
.arg(DEFAULT_TITLE)
.arg(QObject::tr("Presented By Spark developers # HadesStudio"));
dialog->setDescription(szDefaultDesc);
// WebsiteName
dialog->setWebsiteName("spark-app.store");
// WebsiteLink
dialog->setWebsiteLink("https://www.spark-app.store/");
// License
dialog->setLicense(QObject::tr("This program is open source under GPLv3")); // 本程序按GPL第三版开源
QCommandLineParser parser;
parser.setApplicationDescription(QObject::tr("Description: %1").arg(DEFAULT_TITLE));
parser.addHelpOption();
parser.addVersionOption();
QCommandLineOption optParser(QStringList() << "p" << "parser",
QObject::tr("Enable CommandLineParser. Default is false."));
parser.addOption(optParser);
QCommandLineOption optTitle(QStringList() << "t" << "title",
QObject::tr("The Title of Application. Default is %1.").arg(DEFAULT_TITLE),
"title",
DEFAULT_TITLE);
parser.addOption(optTitle);
QCommandLineOption optUrl(QStringList() << "u" << "url",
QObject::tr("The target URL. Default is Blank."),
"url",
DEFAULT_URL);
parser.addOption(optUrl);
QCommandLineOption optWidth(QStringList() << "w" << "width",
QObject::tr("The Width of Application. Default is %1.").arg(DEFAULT_WIDTH),
"width",
QString::number(DEFAULT_WIDTH));
parser.addOption(optWidth);
QCommandLineOption optHeight(QStringList() << "H" << "height",
QObject::tr("The Height of Application. Default is %1.").arg(DEFAULT_HEIGHT),
"height",
QString::number(DEFAULT_HEIGHT));
parser.addOption(optHeight);
QCommandLineOption optFixSize("fix-size",
QObject::tr("Fix Window Size. Default is false."));
parser.addOption(optFixSize);
QCommandLineOption optHideButtons("hide-buttons",
QObject::tr("Hide Control Buttons. Default is false."));
parser.addOption(optHideButtons);
QCommandLineOption optIcon(QStringList() << "i" << "ico",
QObject::tr("The ICON of Application."),
"ico",
DEFAULT_ICON);
parser.addOption(optIcon);
QCommandLineOption optDesc(QStringList() << "d" << "desc",
QObject::tr("The Description of Application."),
"desc",
DEFAULT_DESC);
parser.addOption(optDesc);
QCommandLineOption optCfgFile(QStringList() << "c" << "cfg",
QObject::tr("The Configuration file of Application."),
"cfg",
DEFAULT_CFG);
parser.addOption(optCfgFile);
QCommandLineOption optRootPath(QStringList() << "r" << "root",
QObject::tr("The root path of the program web service."),
"root",
DEFAULT_ROOT);
parser.addOption(optRootPath);
QCommandLineOption optPort(QStringList() << "P" << "port",
QObject::tr("The port number of the program web service."),
"port",
DEFAULT_PORT);
parser.addOption(optPort);
#if SSL_SERVER
QCommandLineOption optSSLPort(QStringList() << "s" << "sslport",
QObject::tr("The ssl port number of the program web service."),
"sslport",
DEFAULT_PORT);
parser.addOption(optSSLPort);
#endif
parser.process(a);
QString szTitle = DEFAULT_TITLE;
QString szUrl = DEFAULT_URL;
int width = DEFAULT_WIDTH;
int height = DEFAULT_HEIGHT;
bool fixSize = false;
bool hideButtons = false;
QString szIcon = DEFAULT_ICON;
QString szDesc = DEFAULT_DESC;
QString szRootPath = DEFAULT_ROOT;
quint16 u16Port = DEFAULT_PORT;
#if SSL_SERVER
quint16 u16sslPort = 0;
#endif
QString szCfgFile = DEFAULT_CFG;
if (parser.isSet(optCfgFile))
{
szCfgFile = parser.value(optCfgFile);
if (!szCfgFile.isEmpty())
{
QFileInfo fi(szCfgFile);
if (fi.exists())
{
QSettings settings(szCfgFile, QSettings::IniFormat);
szTitle = settings.value("SparkWebAppRuntime/Title", DEFAULT_TITLE).toString();
szUrl = settings.value("SparkWebAppRuntime/URL", DEFAULT_TITLE).toString();
width = settings.value("SparkWebAppRuntime/Width", DEFAULT_WIDTH).toUInt();
height = settings.value("SparkWebAppRuntime/Height", DEFAULT_HEIGHT).toUInt();
fixSize = settings.value("SparkWebAppRunTime/FixSize", false).toBool();
hideButtons = settings.value("SparkWebAppRunTime/HideButtons", false).toBool();
szIcon = settings.value("SparkWebAppRuntime/Ico", DEFAULT_ICON).toString();
szDesc = QString("%1<br/><br/>%2")
.arg(settings.value("SparkWebAppRuntime/Desc", QString()).toString())
.arg(szDefaultDesc);
szRootPath = settings.value("SparkWebAppRuntime/RootPath", QString()).toString();
u16Port = settings.value("SparkWebAppRuntime/Port", 0).toUInt();
#if SSL_SERVER
u16sslPort = settings.value("SparkWebAppRuntime/SSLPort", 0).toUInt();
#endif
}
}
}
// 命令行级别优先, 覆盖配置文件的设置
if (parser.isSet(optTitle))
{
szTitle = parser.value(optTitle);
}
if (parser.isSet(optUrl))
{
szUrl = parser.value(optUrl);
}
if (parser.isSet(optWidth))
{
width = parser.value(optWidth).toInt();
}
if (parser.isSet(optHeight))
{
height = parser.value(optHeight).toInt();
}
if (parser.isSet(optFixSize))
{
fixSize = true;
}
if (parser.isSet(optHideButtons))
{
hideButtons = true;
}
if (parser.isSet(optDesc))
{
szDesc = QString("%1<br/><br/>%2").arg(parser.value(optDesc))
.arg(szDefaultDesc);
}
if (parser.isSet(optRootPath))
{
szRootPath = parser.value(optRootPath);
}
if (parser.isSet(optPort))
{
u16Port = parser.value(optPort).toUInt();
}
#if SSL_SERVER
if (parser.isSet(optSSLPort))
{
u16sslPort = parser.value(optSSLPort).toUInt();
}
#endif
// 没设置 -p 并且参数个数>1 并且第一个参数不是-开始的
if (!parser.isSet(optParser) && argc > 1 && !QString(argv[1]).startsWith("-"))
{
// 按照固定顺序级别最优先
if (argc > 1)
{
szTitle = argv[1];
}
if (argc > 2)
{
szUrl = argv[2];
}
if (argc > 3)
{
width = QString(argv[3]).toInt();
}
if (argc > 4)
{
height = QString(argv[4]).toInt();
}
if (argc > 5)
{
fixSize = true;
}
if (argc > 6)
{
hideButtons = true;
}
if (argc > 7)
{
szIcon = QString(argv[7]);
}
if (argc > 8)
{
szDesc = QString("%1<br/><br/>%2").arg(QString(argv[8])).arg(szDefaultDesc);
}
if (argc > 9)
{
szRootPath = QString(argv[9]);
}
if (argc > 10)
{
u16Port = QString(argv[10]).toUInt();
}
#if SSL_SERVER
if (argc > 11)
{
u16sslPort = QString(argv[11]).toUInt();
}
#endif
}
MainWindow w(szTitle, szUrl, width, height, fixSize, hideButtons, dialog);
#if SSL_SERVER
if (!szRootPath.isEmpty() && u16Port > 0 && u16sslPort > 0)
{
HttpD httpd(szRootPath, u16Port, u16sslPort);
httpd.start();
}
#else
if (!szRootPath.isEmpty() && u16Port > 0)
{
static HttpD httpd(szRootPath, u16Port);
QObject::connect(&w, &MainWindow::sigQuit, &httpd, &HttpD::stop);
httpd.start();
}
#endif
if (parser.isSet(optIcon))
{
szIcon = parser.value(optIcon);
}
if (!szIcon.isEmpty())
{
dialog->setWindowIcon(QIcon(szIcon));
dialog->setProductIcon(QIcon(szIcon));
w.setIcon(szIcon);
}
if (!szDesc.isEmpty())
{
dialog->setDescription(szDesc);
}
w.show();
return a.exec();
}