1 Star 0 Fork 18

陈永权/ZenShot

forked from Gitee 极速下载/ZenShot 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
zentaologic.cpp 15.13 KB
一键复制 编辑 原始数据 按行查看 历史
leexu007 提交于 2022-10-17 15:42 . + Center setting dlgs
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
#include "starterui.h"
#include "starter.h"
#include "config/xmlconfig.h"
#include "config/configvalue.h"
#include "spdlogwrapper.hpp"
#include "usrmetatype.h"
#include <QMenu>
#include <QDir>
#include <QMessageBox>
#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonObject>
#include <QBuffer>
#include <QDesktopServices>
#if !NZENTAO_VER_
void StarterUI::OnShowZenTaoSetting()
{
if (!m_ZTSettingDlg.isVisible())
{
m_ZTSettingDlg.showNormal();
CenterDlg(&m_ZTSettingDlg);
}
m_ZTSettingDlg.raise();
m_ZTSettingDlg.activateWindow();
}
void StarterUI::OnShowPreview(Workspace* w)
{
bool has_config = false;
auto cb = [&](rapidxml::xml_node<>*& root, rapidxml::xml_node<>*& node)
{
(void)root;
(void)node;
has_config = true;
return true;
};
GetXMLConfig().FindAllNode("config", "zentao", cb);
if (!has_config)
{
m_ZTSettingDlg.showNormal();
CenterDlg(&m_ZTSettingDlg);
m_ZTSettingDlg.raise();
m_ZTSettingDlg.activateWindow();
return;
}
if (!m_ZTSubmitDlg.isVisible())
{
m_CurrentShot = w->result();
m_ZTSubmitDlg.showNormal();
CenterDlg(&m_ZTSubmitDlg);
emit Thumbnail(m_CurrentShot);
}
m_ZTSubmitDlg.raise();
m_ZTSubmitDlg.activateWindow();
}
void StarterUI::OnLogin(string_ptr url, string_ptr usr, string_ptr pass)
{
if (IsSameUsr(*usr, *url))
{
L_TRACE("same usr && url ..........");
return;
}
QString err_token;
if (UsrLogin(url, usr, pass, err_token))
{
QMessageBox::information(NULL, tr("Title"), err_token);
return;
}
L_DEBUG("usr [{0}] token = <<{1}>>", usr->c_str(), err_token.toStdString().c_str());
m_HttpReq.SetToken(err_token.toStdString().c_str());
}
void StarterUI::OnSubmitLogin(string_ptr name)
{
string_ptr url, usr, pass;
auto cb = [&](rapidxml::xml_node<>*& root, rapidxml::xml_node<>*& node)
{
(void)root;
if (GetConfigAttrString(node, "name") == *name)
{
url.reset(new std::string(GetConfigAttrString(node, "url")));
usr.reset(new std::string(GetConfigAttrString(node, "usr")));
pass.reset(new std::string(GetConfigAttrString(node, "pass")));
return true;
}
return false;
};
GetXMLConfig().FindAllNode("config", "zentao", cb);
if (url == nullptr || usr == nullptr || pass == nullptr)
{
emit SubmitLoginResult(false);
return;
}
if (url->empty() || usr->empty() || pass->empty())
{
emit SubmitLoginResult(false);
return;
}
// if (IsSameUsr(*usr, *url))
// {
// SubmitLoginResult(true);
// return;
// }
QString err_token;
if (UsrLogin(url, usr, pass, err_token))
{
QMessageBox::information(NULL, tr("Title"), err_token);
emit SubmitLoginResult(false);
return;
}
L_DEBUG("usr [{0}] token = <<{1}>>", usr->c_str(), err_token.toStdString().c_str());
m_HttpReq.SetToken(err_token.toStdString().c_str());
emit SubmitLoginResult(true);
}
void StarterUI::OnHttpProduct()
{
zproduct_item_vec_ptr items(new std::vector<zproduct_item>);
std::string orig_uri = build_uri(m_CurrentUrl.c_str(), "/products");
for (int i = 1; i; i++)
{
std::string uri = orig_uri;
uri.append("?")
.append("limit=50")
.append("&")
.append("page=").append(std::to_string(i));
m_HttpReq.SetUrl(uri.c_str());
m_HttpReq.SetTokenHeader();
std::string data;
if (!m_HttpReq.Exec(data))
{
return;
}
QJsonParseError e;
QJsonDocument doc = QJsonDocument::fromJson(data.c_str(), &e);
if (doc.isNull() || e.error != QJsonParseError::NoError)
{
break;
}
auto products = doc["products"].toArray();
for (const auto& item : products)
{
auto obj = item.toObject();
zproduct_item product;
product.id_ = static_cast<uint32_t>(obj["id"].toInt());
product.name_ = obj["name"].toString().toStdString();
items->push_back(product);
}
int total = doc["total"].toInt();
int limit = doc["limit"].toInt();
if (!limit)
break;
int max_page = total / limit;
if (total % limit) max_page++;
if (i >= max_page)
break;
}
emit ProductItems(items);
}
void StarterUI::OnHttpModule(uint32_t product_id, string_ptr view_type)
{
zmodule_item_vec_ptr items(new std::vector<zmodule_item>);
std::string orig_uri = build_uri(m_CurrentUrl.c_str(), "/modules");
std::string uri = orig_uri;
uri.append("?")
.append("id=").append(std::to_string(product_id))
.append("&")
.append("type=").append(*view_type);
m_HttpReq.SetUrl(uri.c_str());
m_HttpReq.SetTokenHeader();
std::string data;
if (!m_HttpReq.Exec(data))
{
emit ModuleItems(items);
return;
}
QJsonParseError e;
QJsonDocument doc = QJsonDocument::fromJson(data.c_str(), &e);
if (doc.isNull() || e.error != QJsonParseError::NoError)
{
emit ModuleItems(items);
return;
}
auto modules = doc["modules"].toArray();
for (const auto& item : modules)
{
auto obj = item.toObject();
zmodule_item product;
product.id_ = static_cast<uint32_t>(obj["id"].toInt());
product.name_ = obj["name"].toString().toStdString();
items->push_back(product);
}
emit ModuleItems(items);
}
void StarterUI::OnHttpVersion(uint32_t product_id, string_ptr type)
{
zversion_item_vec_ptr items(new std::vector<zversion_item>);
std::string uri = build_uri(m_CurrentUrl.c_str(), "/projects");
uri.append("/").append(std::to_string(product_id))
.append("/builds");
m_HttpReq.SetUrl(uri.c_str());
m_HttpReq.SetTokenHeader();
std::string data;
if (!m_HttpReq.Exec(data))
{
emit VersionItems(items);
return;
}
QJsonParseError e;
QJsonDocument doc = QJsonDocument::fromJson(data.c_str(), &e);
if (doc.isNull() || e.error != QJsonParseError::NoError)
{
emit VersionItems(items);
return;
}
auto modules = doc["builds"].toArray();
for (const auto& item : modules)
{
auto obj = item.toObject();
zversion_item product;
product.id_ = obj["branchName"].toString().toStdString();
product.name_ = obj["name"].toString().toStdString();
items->push_back(product);
}
emit VersionItems(items);
}
void StarterUI::OnHttpModules(string_ptr type)
{
zpri_item_vec_ptr pris(new std::vector<zpri_item>);
zseverity_item_vec_ptr serveritys(new std::vector<zseverity_item>);
zos_item_vec_ptr oss(new std::vector<zos_item>);
zbrowser_item_vec_ptr browers(new std::vector<zbrowser_item>);
ztype_item_vec_ptr types(new std::vector<ztype_item>);
std::string uri = build_uri(m_CurrentUrl.c_str(), "/langs");
uri.append("?")
.append("modules=").append(*type);
m_HttpReq.SetUrl(uri.c_str());
m_HttpReq.SetTokenHeader();
std::string data;
if (!m_HttpReq.Exec(data))
{
emit ModulesItems(pris, serveritys, oss, browers, types);
return;
}
QJsonParseError e;
QJsonDocument doc = QJsonDocument::fromJson(data.c_str(), &e);
if (doc.isNull() || e.error != QJsonParseError::NoError)
{
emit ModulesItems(pris, serveritys, oss, browers, types);
return;
}
auto modules = doc[type->c_str()].toObject();
auto priList = modules["priList"].toArray();
auto severityList = modules["severityList"].toObject();
auto osList = modules["osList"].toObject();
auto browserList = modules["browserList"].toObject();
auto versionList = modules["versionList"].toArray();
auto typeList = modules["typeList"].toObject();
auto categoryList = modules["categoryList"].toObject();
for (const auto& pri : priList)
{
zpri_item item;
item.name_ = pri.toString().toStdString();
item.id_ = static_cast<uint32_t>(atoi(item.name_.c_str()));
pris->push_back(item);
}
for (auto it = severityList.begin(); it != severityList.end(); it++)
{
zseverity_item item;
item.id_ = static_cast<uint32_t>(it.key().toInt());
item.name_ = it.value().toString().toStdString();
serveritys->push_back(item);
}
for (auto it = osList.begin(); it != osList.end(); it++)
{
zos_item item;
item.id_ = it.key().toStdString();
item.name_ = it.value().toString().toStdString();
oss->push_back(item);
}
for (auto it = browserList.begin(); it != browserList.end(); it++)
{
zbrowser_item item;
item.id_ = it.key().toStdString();
item.name_ = it.value().toString().toStdString();
browers->push_back(item);
}
for (auto it = typeList.begin(); it != typeList.end(); it++)
{
ztype_item item;
item.id_ = it.key().toStdString();
item.name_ = it.value().toString().toStdString();
types->push_back(item);
}
for (auto it = categoryList.begin(); it != categoryList.end(); it++)
{
ztype_item item;
item.id_ = it.key().toStdString();
item.name_ = it.value().toString().toStdString();
types->push_back(item);
}
emit ModulesItems(pris, serveritys, oss, browers, types);
}
void StarterUI::OnSubmitDemandJson(uint32_t product_id, string_ptr json)
{
std::string demand_uri;
demand_uri.append("/products/").append(std::to_string(product_id))
.append("/stories");
std::string uri = build_uri(m_CurrentUrl.c_str(), demand_uri.c_str());
m_HttpReq.SetUrl(uri.c_str());
m_HttpReq.SetTokenHeader();
m_HttpReq.SetPost(json->c_str());
std::string data;
if (!m_HttpReq.Exec(data)) // http error
{
return;
}
bool success = false;
QJsonParseError e;
QJsonDocument doc = QJsonDocument::fromJson(data.c_str(), &e);
if (doc.isNull() || e.error != QJsonParseError::NoError)
{
m_ZTTipsDlg.SetContent(false, tr("demandunknown"));
}
else if (doc["id"].isUndefined() || doc["id"].isNull())
{
auto title = doc["error"].toObject();
auto arr = title["title"].toArray();
if (!arr.isEmpty())
{
m_ZTTipsDlg.SetContent(false, arr[0].toString());
}
else
{
m_ZTTipsDlg.SetContent(false, tr("demandunknown"));
}
}
else
{
m_LastSubmitUrl = "story-view-";
m_LastSubmitUrl.append(std::to_string(doc["id"].toInt())).append(".html");
m_ZTTipsDlg.SetContent(true, tr("demandsuccess"));
success = true;
}
CenterDlg(&m_ZTTipsDlg);
int ret = m_ZTTipsDlg.exec();
if (success)
{
m_ZTSubmitDlg.hide();
emit StopShot(m_CurrentStarter);
return;
}
if (ret != 1)
{
m_ZTSubmitDlg.hide();
return;
}
}
void StarterUI::OnSubmitBugJson(uint32_t product_id, string_ptr json)
{
std::string bug_uri;
bug_uri.append("/products/").append(std::to_string(product_id))
.append("/bugs");
std::string uri = build_uri(m_CurrentUrl.c_str(), bug_uri.c_str());
m_HttpReq.SetUrl(uri.c_str());
m_HttpReq.SetTokenHeader();
m_HttpReq.SetPost(json->c_str());
L_TRACE(json->c_str());
std::string data;
if (!m_HttpReq.Exec(data)) // http error
{
return;
}
bool success = false;
QJsonParseError e;
QJsonDocument doc = QJsonDocument::fromJson(data.c_str(), &e);
if (doc.isNull() || e.error != QJsonParseError::NoError)
{
m_ZTTipsDlg.SetContent(false, tr("bugunknown"));
}
else if (doc["id"].isUndefined() || doc["id"].isNull())
{
auto title = doc["error"].toObject();
auto arr = title["title"].toArray();
if (!arr.isEmpty())
{
m_ZTTipsDlg.SetContent(false, arr[0].toString());
}
else
{
m_ZTTipsDlg.SetContent(false, tr("bugunknown"));
}
}
else
{
m_LastSubmitUrl = "bug-view-";
m_LastSubmitUrl.append(std::to_string(doc["id"].toInt())).append(".html");
m_ZTTipsDlg.SetContent(true, tr("bugsuccess"));
success = true;
}
CenterDlg(&m_ZTTipsDlg);
int ret = m_ZTTipsDlg.exec();
if (success)
{
m_ZTSubmitDlg.hide();
emit StopShot(m_CurrentStarter);
return;
}
if (ret != 1)
{
m_ZTSubmitDlg.hide();
return;
}
}
void StarterUI::OnUploadImage()
{
if (m_CurrentShot == nullptr)
{
emit UploadImageDone(false, nullptr);
return;
}
QByteArray pix_bytes;
QBuffer buf(&pix_bytes);
if (!m_CurrentShot->save(&buf, "png") || pix_bytes.isEmpty())
{
emit UploadImageDone(false, nullptr);
return;
}
std::string uri = build_uri(m_CurrentUrl.c_str(), "/files");
m_HttpReq.SetUrl(uri.c_str());
m_HttpReq.SetTokenHeader();
std::string data;
if (!m_HttpReq.UploadFormFile(data, pix_bytes)) // http error
{
emit UploadImageDone(false, nullptr);
return;
}
QJsonParseError e;
QJsonDocument doc = QJsonDocument::fromJson(data.c_str(), &e);
if (doc.isNull() || e.error != QJsonParseError::NoError)
{
emit UploadImageDone(false, nullptr);
return;
}
if (doc["error"].isString())
{
QMessageBox::information(NULL, tr("Title"), doc["error"].toString().toUtf8());
emit UploadImageDone(false, nullptr);
return;
}
string_ptr img_url(new std::string(doc["url"].toString().toStdString()));
emit UploadImageDone(true, img_url);
}
void StarterUI::OnOpenZentaoUrl()
{
QString url = m_CurrentUrl.c_str();
if (*m_CurrentUrl.rbegin() != '/')
{
url += "/";
}
url += m_LastSubmitUrl.c_str();
QDesktopServices::openUrl(QUrl(url));
}
void StarterUI::OnSubmitZentaoHide()
{
if (m_CurrentStarter == nullptr)
return;
m_CurrentStarter->rasie();
}
void StarterUI::OnTipZentaoHide()
{
m_ZTSubmitDlg.raise();
m_ZTSubmitDlg.activateWindow();
}
int StarterUI::UsrLogin(string_ptr url, string_ptr usr, string_ptr pass, QString& err_token)
{
std::string uri = build_uri(url->c_str(), "/tokens");
m_HttpReq.SetUrl(uri.c_str());
m_HttpReq.SetPost("{\"account\": \"%s\", \"password\": \"%s\"}", usr->c_str(), pass->c_str());
L_DEBUG("{0} & {1} LOGIN -> {2}", usr->c_str(), pass->c_str(), uri.c_str());
std::string data;
if (!m_HttpReq.Exec(data)) // http error
{
err_token = tr("httptimeout");
return -1;
}
QJsonParseError e;
QJsonDocument doc = QJsonDocument::fromJson(data.c_str(), &e);
if (doc.isNull() || e.error != QJsonParseError::NoError)
{
err_token = tr("invalidjson");
return -2;
}
if (doc["error"].isString())
{
err_token = doc["error"].toString().toUtf8();
return -3;
}
if (doc["errmsg"].isString())
{
err_token = doc["errmsg"].toString().toUtf8();
return -3;
}
auto token = doc["token"].toString();
if (token.isEmpty())
{
err_token = tr("invalidtoken");
return -4;
}
m_CurrentUsr = *usr;
m_CurrentUrl = *url;
err_token = token;
return 0;
}
#endif // NZENTAO_VER_
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C/C++
1
https://gitee.com/chen_yong_quan/ZenShot.git
git@gitee.com:chen_yong_quan/ZenShot.git
chen_yong_quan
ZenShot
ZenShot
main

搜索帮助