代码拉取完成,页面将自动刷新
//xlang Source, Name:PackageViewer.xcsm
//Date: Fri Dec 22:58:50 2018
class PackageViewer : QDialog{
QTreeWidget pcklist;
QPushButton btnInstall, btnDelete, btnAdd, btnExport, loadpack, editsrc;
QLineEdit keyedit;
QLabel labstatus;
QProgressBar progressBar;
String pkgSource = "http://112.124.120.113:8080/dcbinterface/api/getpackages";
PackageManager.PackageInfo [] net_cache_pis;
PackageManager pm = new PackageManager();
bool loadingInProgress = false;
static PackageViewer _viewer = nilptr;
static Object prog_lock = new Object();
static Map<String, int> dllist = new Map<String, int>();
public void updateProgress(){
int total_prog = 0;
int task_count = 0;
synchronized(dllist){
Map.Iterator<String, int> iter = dllist.iterator();
while (iter.hasNext()){
String key = iter.getKey();
total_prog += DownloadCenter.getTaskProgress(key);
task_count++;
iter.next();
}
}
if (task_count != 0){
progressBar.setValue(total_prog / task_count);
}else{
progressBar.setValue(100);
}
if (task_count > 0){
labstatus.setText("共有 " + task_count + " 个任务正在进行中.");
}else{
labstatus.setText("就绪.");
}
}
public void loadPackages(){
JsonObject root;
try{
HttpRequest req = new HttpRequest();
if (req.get(pkgSource, 300000, pkgSource.upper().startsWith("HTTPS://"))){
if (req.getResponseCode() >= 200){
byte [] data = new byte[4096];
String jsondata = "";
while (req.available(true) > 0){
int readlen = req.readData(data,0,data.length);
jsondata = jsondata + new String(data, 0, readlen);
}
root = new JsonObject(jsondata);
}
}
}catch(Exception e){
root = nilptr;
}
if (root == nilptr){
runOnUi(new Runnable(){
void run() override{
QMessageBox.Critical("错误","从网络加载时发生错误!",QMessageBox.Ok,QMessageBox.Ok);
labstatus.setText("加载 " + pkgSource + " 失败!");
loadingInProgress = false;
}
});
return ;
}else{
Vector<PackageManager.PackageInfo> dlpkgs = new Vector<PackageManager.PackageInfo>();
int error = root.getInt("error");
if (error == 0){
JsonArray pkgs = (JsonArray)root.get("data");
if (pkgs != nilptr){
for (int i = 0, c = pkgs.length(); i < c; i++){
JsonObject item = (JsonObject)pkgs.get(i);
if (item != nilptr){
PackageManager.PackageInfo pinfo = new PackageManager.PackageInfo(item);
dlpkgs.add(pinfo);
}
}
runOnUi( new Runnable(){
void run() override{
onloadSuccess(dlpkgs.toArray(new PackageManager.PackageInfo[0]));
}
});
}
runOnUi( new Runnable(){
void run() override{
labstatus.setText("加载 " + pkgSource + " 成功!");
loadingInProgress = false;
}
});
}else{
runOnUi(new Runnable(){
void run() override{
QMessageBox.Critical("错误","包数据错误!",QMessageBox.Ok,QMessageBox.Ok);
labstatus.setText("加载 " + pkgSource + " 失败!");
loadingInProgress = false;
}
});
}
}
}
public void onloadSuccess(@NotNilptr PackageManager.PackageInfo [] pkgs){
loadList();
loadNetList(pkgs);
}
public void onAttach() override {
//setFixedSize(width(), height());
pcklist = (QTreeWidget)attachByName(new QTreeWidget(), "pcklist");
pcklist.setWhatsThis("显示当前开发环境中已安装的包");
btnInstall = (QPushButton)attachByName(new QPushButton(), "btnInstall");
btnInstall.setWhatsThis("从文件安装一个包到开发环境中");
btnDelete = (QPushButton)attachByName(new QPushButton(), "btnDelete");
btnDelete.setWhatsThis("删除选择的包");
btnAdd = (QPushButton)attachByName(new QPushButton(), "btnAdd");
btnAdd.setWhatsThis("将选择的包添加到当前的项目中");
btnExport = (QPushButton)attachByName(new QPushButton(), "btnExport");
btnExport.setWhatsThis("将选择的包导出到文件");
keyedit = (QLineEdit)attachByName(new QLineEdit(), "keyedit");
keyedit.setWhatsThis("输入包名或者描述关键字,支持模糊搜索");
labstatus = (QLabel)attachByName(new QLabel(), "labstatus");
labstatus.setWhatsThis("显示当前状态");
progressBar = (QProgressBar)attachByName(new QProgressBar(), "progressBar");
progressBar.setWhatsThis("显示当前总进度");
btnExport = (QPushButton)attachByName(new QPushButton(), "btnExport");
btnExport.setWhatsThis("将选择的包导出到文件");
loadpack = (QPushButton)attachByName(new QPushButton(), "loadpack");
loadpack.setWhatsThis("加载包");
editsrc = (QPushButton)attachByName(new QPushButton(), "editsrc");
editsrc.setWhatsThis("更改软件源");
String [] columns = {"名称", "版本", "描述", "状态", "发布者"};
pcklist.setColumns(columns);
pcklist.setColumnWidth(0,220);
pcklist.setColumnWidth(1,80);
pcklist.setColumnWidth(2,550);
pcklist.setColumnWidth(3,80);
pcklist.setColumnWidth(4,150);
pcklist.setOnTreeViewItemEvent( new onTreeViewItemEvent(){
void onItemClicked(@NotNilptr QTreeWidget tree,long item, int column){
if (column == 4){
String txt = tree.getItemText(item, column);
if (txt.upper().startsWith("HTTP")){
if (QMessageBox.Question("提示","是否转到发布者网站: <b>" + txt + "</b> ?",QMessageBox.Ok | QMessageBox.No,QMessageBox.Ok) == QMessageBox.Ok){
openUrl(txt);
}
}
}
}
});
if (pm.load() == false){
QMessageBox.Critical("错误", "加载包时出现故障", QMessageBox.Ok, QMessageBox.Ok);
}else{
loadList();
}
if (XWorkspace.workspace != nilptr){
btnAdd.setEnabled(XWorkspace.workspace.isCurrentProjectEmpty() == false);
}else{
btnAdd.setEnabled(false);
}
loadpack.setOnClickListener(new onClickListener(){
void onClick(QObject obj, bool checked)override{
loadNetinfo();
}
});
editsrc.setOnClickListener(new onClickListener(){
void onClick(QObject obj, bool checked)override{
editSrcaddress();
}
});
btnInstall.setOnClickListener(new onClickListener(){
void onClick(QObject obj, bool checked)override{
String file = QFileDialog.getOpenFileName("添加包", nilptr, "X 包 (*.xp)", PackageViewer.this);
if (file != nilptr && file.length() > 0){
install(file);
}
}
});
btnAdd.setOnClickListener(new onClickListener(){
void onClick(QObject obj, bool checked)override{
importToProject();
}
});
btnDelete.setOnClickListener( new onClickListener(){
void onClick(QObject obj,bool checked)override{
deletePackage();
}
});
btnExport.setOnClickListener( new onClickListener(){
void onClick(QObject obj,bool checked)override{
exportPackage();
}
});
setWindowIcon("./res/package64.png");
createContextMenu();
keyedit.setOnKeyEventListener(new onKeyEventListener() {
bool onKeyPress(QObject obj,int key,bool repeat,int count,String text,int scanCode,int virtualKey,int modifier)override {
if (key == Constant.Key_Escape) {
keyedit.setText("");
}
return true;
}
});
keyedit.setOnEditEventListener(new onEditEventListener() {
void onTextChanged(QObject, @NotNilptr String text)override {
String key = text.upper();
long [] items = pcklist.getTopItems();
int c = pcklist.getTopLevelCount();
try{
for (int i = 0; i < c; i++){
bool visible = false;
for (int x = 0; x < 5; x ++){
String itemtext = pcklist.getItemText(items[i],x);
if (Pattern.test(itemtext.upper(), key, 0, false)){
visible = true;
break;
}
}
pcklist.setItemVisible(items[i],visible);
}
}catch(Exception e){
}
}
});
synchronized(prog_lock){
_viewer = this;
}
loadNetinfo();
}
public void loadNetinfo(){
loadingInProgress = true;
labstatus.setText("正在从 " + pkgSource + " 加载包信息...");
new Thread(){
void run()override{
loadPackages();
}
}.start();
}
public void editSrcaddress()
{
InputDialog.requestInput(new InputDialog.onInputListener() {
bool onInputOk(@NotNilptr String text)override {
if (text.length() == 0) {
QMessageBox.Critical("错误", "输入了不正确的地址", QMessageBox.Ok, QMessageBox.Ok);
return false;
}
pkgSource = text;
loadNetinfo();
return true;
}
bool onInputCancel()override {
return true;
}
String getTitle()override {
return "更改源";
}
String getTips()override {
return "输入源地址:";
}
String getDefault()override {
return pkgSource;
}
});
}
public void installSelPkgs(){
long []items = pcklist.getSelectedItems();
if (items != nilptr && items.length > 0){
for (int i =0; i < items.length; i++){
long dlid = pcklist.getItemTag(items[i], 3);
if (dlid > 0 && dlid < net_cache_pis.length){
int nid = dlid - 1;
PackageManager.PackageInfo pinfo = net_cache_pis[nid];
if (pinfo != nilptr && pinfo.detectCache()){
install(pinfo.cacheFile);
}else{
QMessageBox.Critical("注意","未获取完成,无法进行安装",QMessageBox.Ok,QMessageBox.Ok);
}
}
}
}
}
public void downloadSelPkgs(){
long []items = pcklist.getSelectedItems();
if (items != nilptr && items.length > 0){
for (int i =0; i < items.length; i++){
long dlid = pcklist.getItemTag(items[i], 3);
if (dlid > 0){
int nid = dlid - 1;
PackageManager.PackageInfo pinfo = net_cache_pis[nid];
if (pinfo != nilptr && pinfo.detectCache()){
install(pinfo.cacheFile);
}else{
DownloadCenter.createDownload(net_cache_pis[nid].name, net_cache_pis[nid].url, net_cache_pis[nid].cacheFile, net_cache_pis[nid].dlsize, new DownloadListener(){
void onStatus(DownloadStatus status, int prog, Object tag){
synchronized(prog_lock){
if (_viewer != nilptr){
_viewer.runOnUi(new Runnable(){
void run() override{
synchronized(prog_lock){
_viewer.onUpdateProgress(status, prog, tag);
}
}
});
}
}
if ((status == DownloadStatus.DL_DONE) || (status == DownloadStatus.DL_CANCEL)){
synchronized(dllist){
dllist.remove(net_cache_pis[nid].name);
}
}
}
},net_cache_pis[nid]);
synchronized(dllist){
dllist.put(net_cache_pis[nid].name, 0);
}
}
}
}
}
}
public void onUpdateProgress(DownloadStatus status, int prog, Object tag){
long [] items = pcklist.getTopItems();
int c = pcklist.getTopLevelCount();
PackageManager.PackageInfo pinfo = (PackageManager.PackageInfo)tag;
if (pinfo != nilptr){
for (int i = 0; i < c; i++){
bool visible = false;
String itemtext = pcklist.getItemText(items[i],0);
if (pinfo.name.equals(itemtext)){
switch(status){
case DownloadStatus.DL_BEGIN:
pcklist.setItemText(items[i],3,"开始下载...");
break;
case DownloadStatus.DL_INPROCESSING:
pcklist.setItemText(items[i],3,"已下载:" + prog + "%...");
break;
case DownloadStatus.DL_DONE:
if (prog == 100){
pcklist.setItemText(items[i],3,"下载完成");
install(pinfo.cacheFile);
}else{
pcklist.setItemText(items[i],3,"下载失败");
}
break;
}
break;
}
}
}
updateProgress();
}
public void exportPackage(){
long []items = pcklist.getSelectedItems();
if (items != nilptr && items.length > 0){
for (int i = 0; i < items.length; i++){
int id = pcklist.getItemTag(items[i], 0);
PackageManager.PackageInfo pi = pm.get(id);
if (pi != nilptr){
String floder = QFileDialog.getFolderPath("选择导出目录","" ,"", this);
if (floder != nilptr && floder.length() > 0){
floder = String.formatPath(floder.appendPath(pi.name + pi.version + ".xp"), false);
PackageManager.copyFile(floder, pi.filepath);
}
}
}
}
}
public ContextMenu _contextMenu = new ContextMenu();
public void createContextMenu(){
QTreeWidget list = pcklist;
onEventListener menuListener = new onEventListener(){
public void onTrigger(QObject obj) override{
if (obj == _contextMenu.actions[0]){
if (btnInstall._clicklistener != nilptr){
btnInstall._clicklistener.onClick(btnInstall,false);
}
}else
if (obj == _contextMenu.actions[2]){
loadNetinfo();
}else
if (obj == _contextMenu.actions[4]){
importToProject();
}else
if (obj == _contextMenu.actions[6]){
downloadSelPkgs();
}else
if (obj == _contextMenu.actions[7]){
installSelPkgs();
}else
if (obj == _contextMenu.actions[8]){
exportPackage();
}else
if (obj == _contextMenu.actions[10]){
deletePackage();
}
}
};
if (list != nilptr){
String []acts = {"从文件安装...","-", "刷新","-", "添加到当前项目", "-", "获取包", "安装", "导出", "-", "删除"};
_contextMenu.create(list, acts, menuListener, nilptr);
for (int i =0; i < acts.length; i++){
_contextMenu.setEnable(i, true);
}
}
}
public void deletePackage(){
long []items = pcklist.getSelectedItems();
String tips = "将删除包:\n";
if (items != nilptr && items.length > 0){
int count = 0;
for (int i = 0; i < items.length; i++){
int id = pcklist.getItemTag(items[i], 0);
PackageManager.PackageInfo pi = pm.get(id);
if (pi != nilptr){
tips = tips + "\t" + pi.name + " " + pi.version;
count++;
}
}
if (count > 0){
int r = QMessageBox.Question("注意",tips,QMessageBox.Ok | QMessageBox.Cancel ,QMessageBox.Ok );
if (r == QMessageBox.Ok ){
for (int i = 0; i < items.length; i++){
int id = pcklist.getItemTag(items[items.length - (i + 1)], 0);
PackageManager.PackageInfo pi = pm.get(id);
if (pi != nilptr){
pm.deletePackage(pi);
}
}
}
}
pm.save();
reloadList();
}
}
public void install(@NotNilptr String file){
PackageManager.PackageInfo pi = pm.previewPackage(file);
if (pi == nilptr){
QMessageBox.Critical("错误", file + " 不是一个有效的X包.", QMessageBox.Ok, QMessageBox.Ok);
return;
}
long newVersion = Utils.getVersionValue(pi.version);
Vector<PackageManager.PackageInfo> pl = pm.findPackages(pi.name, nilptr);
if (pl.size() == 0){
if (QMessageBox.Information("提示", "将安装包 " + pi.name + " " + pi.version + "?" + "<br /><br />简介:<br /><b>" + pi.descr + "</b><br /><br />发布者:<a href=\"" + pi.author + "\">" + pi.author + "<em>", QMessageBox.Ok | QMessageBox.Cancel, QMessageBox.Ok) == QMessageBox.Ok){
if (pm.installPackage(pi)){
reloadList();
}else{
QMessageBox.Critical("提示", "安装包 " + pi.name + " " + pi.version + "失败!", QMessageBox.Ok, QMessageBox.Ok);
}
}
}else{
bool bExists = false, bUpgrade = false, bDowngrade = false;
for (int i = 0, c = pl.size(); i < c; i ++){
PackageManager.PackageInfo pe = pl.get(i);
if (pe != nilptr ){
long v = Utils.getVersionValue(pe.version);
if (v == newVersion){
bExists = true;
}else
if (v < newVersion){
bUpgrade = true;
}else{
bDowngrade = true;
}
}
}
if (bExists){
if (QMessageBox.Question("包已存在","此包已存在,是否重新安装?",QMessageBox.Yes | QMessageBox.Cancel,QMessageBox.Cancel) == QMessageBox.Cancel){
return ;
}
PackageManager.PackageInfo pe = pm.findPackage(pi.name, pi.version);
while (pe != nilptr){
pm.deletePackage(pe);
pe = pm.findPackage(pi.name, pi.version);
}
if (QMessageBox.Information("提示", "将安装包 " + pi.name + " " + pi.version + "?" + "<br /><br />简介:<br /><b>" + pi.descr + "</b><br /><br />发布者:<a href=\"" + pi.author + "\">" + pi.author + "<em>", QMessageBox.Ok | QMessageBox.Cancel, QMessageBox.Ok) == QMessageBox.Ok){
if (pm.installPackage(pi)){
reloadList();
}else{
QMessageBox.Critical("提示", "安装包 " + pi.name + " " + pi.version + "失败!", QMessageBox.Ok, QMessageBox.Ok);
}
}
}else
if (bUpgrade){
if (QMessageBox.Question("升级包","是否删除旧版包?",QMessageBox.Yes | QMessageBox.Cancel,QMessageBox.Yes) == QMessageBox.Yes){
PackageManager.PackageInfo pe = pm.findPackage(pi.name, nilptr);
while (pe != nilptr){
long v = Utils.getVersionValue(pe.version);
if (v < newVersion){
pm.deletePackage(pe);
}
pe = pm.findPackage(pi.name, pi.version);
}
}
if (QMessageBox.Information("提示", "将安装包 " + pi.name + " " + pi.version + "?" + "<br /><br />简介:<br /><b>" + pi.descr + "</b><br /><br />发布者:<a href=\"" + pi.author + "\">" + pi.author + "<em>", QMessageBox.Ok | QMessageBox.Cancel, QMessageBox.Ok) == QMessageBox.Ok){
if (pm.installPackage(pi)){
reloadList();
}else{
QMessageBox.Critical("提示", "安装包 " + pi.name + " " + pi.version + "失败!", QMessageBox.Ok, QMessageBox.Ok);
}
}
}else{
if (QMessageBox.Question("注意","将安装一个更低版的包,系统中已存在比此包更高版本的包,已经是否继续安装?",QMessageBox.Yes | QMessageBox.Cancel,QMessageBox.Yes) == QMessageBox.Cancel){
return;
}
if (QMessageBox.Information("提示", "将安装包 " + pi.name + " " + pi.version + "?" + "<br /><br />简介:<br /><b>" + pi.descr + "</b><br /><br />发布者:<a href=\"" + pi.author + "\">" + pi.author + "<em>", QMessageBox.Ok | QMessageBox.Cancel, QMessageBox.Ok) == QMessageBox.Ok){
if (pm.installPackage(pi)){
reloadList();
}else{
QMessageBox.Critical("提示", "安装包 " + pi.name + " " + pi.version + "失败!", QMessageBox.Ok, QMessageBox.Ok);
}
}
}
}
}
public void importToProject(){
if (XWorkspace.workspace.isCurrentProjectEmpty()){
QMessageBox.Critical("错误","当前工作区没有载入一个有效的项目",QMessageBox.Ok,QMessageBox.Ok);
return ;
}
ProgressView pv = ProgressView.showWait(-1);
if (pv != nilptr){
pv.setTitleText("正在导入...");
pv.EnableProgress(true , 0);
pv.centerScreen();
long []items = pcklist.getSelectedItems();
if (items != nilptr && items.length > 0){
PackageManager.PackageInfo [] pis = new PackageManager.PackageInfo[items.length];
for (int i = 0; i < items.length; i++){
int id = pcklist.getItemTag(items[i], 0);
pis[i] = pm.get(id);
}
btnAdd.setEnabled(false);
new Thread(){
void run()override{
for (int i = 0; i < pis.length; i++){
pv.setTitleText("正在导入 " + (i + 1) + "/" + pis.length);
PackageManager.PackageInfo pi = pis[i];
if (pi != nilptr){
XWndOutput.Output("\n导入 " + pi.name + " 版本:" + pi.version + " ....\n", 0);
Project cproj = XWorkspace.workspace.getCurrentProject();
if (cproj != nilptr){
cproj.importPkg(pi);
}
}else{
XWndOutput.Output("\n第 " + (i + 1) + " 是无效的包.\n", 0);
}
pv.EnableProgress(true, (double)i + 1 / (double)items.length);
}
pv.EnableProgress(true, 1.f);
pv.showResult(true, true);
pv.setTitleText("已导入");
pv.dismissAfter(2000);
XWndOutput.Output("\n已全部导入完毕\n", 0);
runOnUi(new Runnable(){
void run()override{
btnAdd.setEnabled(true);
XWorkspace.workspace.XIntelliSense();
}
});
}
}.start();
}
}
}
public void reloadList(){
loadList();
if (net_cache_pis != nilptr){
loadNetList(net_cache_pis);
}
}
public void loadList(){
pcklist.clear();
int c = pm.getCount();
for (int i =0; i < c; i++){
PackageManager.PackageInfo pi = pm.get(i);
if (pi != nilptr){
String icon = pi.iconFile;
if (icon == nilptr || icon.length() == 0){
icon = "res/package64.png";
}
long item = pcklist.addItem(icon, pi.name);
pcklist.modifyItemFlags(item, QTreeWidget.ItemIsUserCheckable, 0);
pcklist.setItemText(item, 1, pi.version);
pcklist.setItemText(item, 2, pi.descr);
pcklist.setItemText(item, 3, "已安装");
pcklist.setItemText(item, 4, pi.author);
pcklist.setItemTag(item, 0, i);
}
}
}
public void loadNetList(@NotNilptr PackageManager.PackageInfo [] pis){
int c = pis.length;
net_cache_pis = pis;
for (int i =0; i < c; i++){
PackageManager.PackageInfo pi = pis[i];
if (pi != nilptr){
if (nilptr != pm.findPackage(pi.name, pi.version)){
continue;
}
String icon = pi.iconFile;
if (icon == nilptr || icon.length() == 0){
icon = "res/package64.png";
}
long item = pcklist.addItem(icon, pi.name);
pcklist.setItemTag(item,3,i + 1);
pcklist.modifyItemFlags(item, QTreeWidget.ItemIsUserCheckable, 0);
pcklist.setItemText(item, 1, pi.version);
pcklist.setItemText(item, 2, pi.descr);
if (pi.detectCache()){
pcklist.setItemText(item, 3, String.format("已下载,待安装", pi.dlsize));
}else{
pcklist.setItemText(item, 3, String.format("未安装[大小:%d]", pi.dlsize));
}
pcklist.setItemText(item, 4, pi.author);
pcklist.setItemTag(item, 0, -1);
}
}
}
public bool onClose()override{
synchronized(prog_lock){
_viewer = nilptr;
}
if (loadingInProgress){
QMessageBox.Information("提示", "请稍候, 正在从远程仓库加载可用的包信息...", QMessageBox.Ok, QMessageBox.Ok);
return false;
}
return true;
}
public static bool Show(){
QDialog newDlg = new QDialog();
if (newDlg.load(UIManager.getUIData(__xPackageResource("ui/packagemgr.ui"))) == false){
return false;
}
PackageViewer viewer = new PackageViewer();
viewer.attach(newDlg);
viewer.setModal(true);
viewer.show();
return true;
}
public static bool Install(@NotNilptr String file){
QDialog newDlg = new QDialog();
if (newDlg.load(UIManager.getUIData(__xPackageResource("ui/packagemgr.ui"))) == false){
return false;
}
PackageViewer viewer = new PackageViewer();
viewer.attach(newDlg);
viewer.setModal(true);
viewer.show();
viewer.install(file);
return true;
}
};
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。