⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 updaterclient.cpp.svn-base

📁 QT写的自动更新程序 自认为不错 请大家参考
💻 SVN-BASE
📖 第 1 页 / 共 2 页
字号:
#include <QtGui>
#include <QCloseEvent>
#include <QtCore>
#include "updaterclient.h"
#include "downloadprogress.h"
#include "aboutdlg.h"
#include "historyshow.h"
#include "IClientCenter.h"
#include "../UpdateXmlPlist.h"
#include "pubfuncs.h"
#include "CommonFtpOperFunction.h"
#include "CommonFileOperFunction.h"
#include "InstallThread.h"

UpdaterClient::UpdaterClient(QWidget *parent, Qt::WFlags flags)
	: wTrayWindow(parent, flags), wWindowPos(this, WidgetWidth, WidgetHeigth),
	pmodelSortFilterProxy(0),
	pStdItemModel(0),
	opeFtp(0),
	opeFile(0),
	filecount(0),
	btnMode(BM_DownLoad),
	pPacksInstallThread(0),
	pRefreshTimer(0),
	directQuit(false)
{
	setupUi();

	loadDownloadProgress();
}

UpdaterClient::~UpdaterClient()
{
	pubfunc::release(pmodelSortFilterProxy);
	pubfunc::release(pStdItemModel);
	pubfunc::release(opeFtp);
	pubfunc::release(opeFile);
	pubfunc::release(pPacksInstallThread);
	pubfunc::release(pRefreshTimer);
}

void UpdaterClient::setupUi()
{
	ui.setupUi(this);

	/* 重新采用奇趣提供的plugintest工程的设计方法构建treeview显示
	pStdItemModel = new QStandardItemModel(0, 5, this);
	pStdItemModel->setHeaderData(0, Qt::Horizontal, tr("check"));
	pStdItemModel->setHeaderData(1, Qt::Horizontal, tr("name"));
	pStdItemModel->setHeaderData(2, Qt::Horizontal, tr("date"));
	pStdItemModel->setHeaderData(3, Qt::Horizontal, tr("size"));
	pStdItemModel->setHeaderData(4, Qt::Horizontal, tr("soft"));
	*/
	pStdItemModel = new QStandardItemModel();
	pStdItemModel->clear();
	pStdItemModel->setHorizontalHeaderLabels(QStringList() << tr("name") << tr("date") << tr("size") << tr("soft"));
	//

	pmodelSortFilterProxy = new QSortFilterProxyModel();
	pmodelSortFilterProxy->setDynamicSortFilter(true);
	pmodelSortFilterProxy->setSourceModel(pStdItemModel);
	QTreeView* pviewPacks = ui.Packs;
	pviewPacks->setRootIsDecorated(false);
	pviewPacks->setAlternatingRowColors(true);
	pviewPacks->setModel(pmodelSortFilterProxy);
	pviewPacks->setSortingEnabled(true);
	pviewPacks->sortByColumn(2, Qt::AscendingOrder);

	//
	QLabel* plabelStatus = ui.statusLabel;
	plabelStatus->setVisible(false);

	//tray
	trayparam.icon = QIcon(":/icon.png");
	wTrayWindow::createTrayIcon(trayparam);

	//menu
	//启动时最小化
	{
		QAction* pStartMinimal = ui.actionStart_minimal;
		pStartMinimal->setCheckable(true);
		connect(pStartMinimal, SIGNAL(toggled(bool)), this, SLOT(on_menu_startminimal(bool)));
		pStartMinimal->setChecked(getCConfig()->getValue("StartMinimal").toBool());
	}

	//是否自动刷新更新信息
	{
		QAction* pAutoRefresh = ui.actionAuto_refresh;
		pAutoRefresh->setCheckable(true);
		connect(pAutoRefresh, SIGNAL(toggled(bool)), this, SLOT(on_menu_autorefresh(bool)));
		pAutoRefresh->setChecked(getCConfig()->getValue("AutoRefresh").toBool());
		on_menu_autorefresh(getCConfig()->getValue("AutoRefresh").toBool());
	}

	//是否自动下载
	{
		QAction* pAutoDownload = ui.actionQuto_download;
		pAutoDownload->setCheckable(true);
		connect(pAutoDownload, SIGNAL(toggled(bool)), this, SLOT(on_menu_autodownload(bool)));
		pAutoDownload->setChecked(getCConfig()->getValue("AutoDownload").toBool());
	}

	//是否自动安装
	{
		QAction* pAutoInstall = ui.actionAuto_install;
		pAutoInstall->setCheckable(true);
		connect(pAutoInstall, SIGNAL(toggled(bool)), this, SLOT(on_menu_autoinstall(bool)));
		pAutoInstall->setChecked(getCConfig()->getValue("AutoInstall").toBool());
	}

	//其他菜单事件
	{
		QAction* pHelp = ui.actionHelp;
		connect(pHelp, SIGNAL(triggered()), this, SLOT(on_menu_help()));

		QAction* pHistory = ui.actionInstallhistory;
		connect(pHistory, SIGNAL(triggered()), this, SLOT(on_menu_history()));
	}

}

void UpdaterClient::on_menu_help()
{
	AboutDlg aboutdlg;
	aboutdlg.exec();
	this->setWindowState(Qt::WindowActive);
}

void UpdaterClient::on_menu_history()
{
	HistoryShow hisdlg;
	hisdlg.exec();
	this->setWindowState(Qt::WindowActive);
}

void UpdaterClient::on_menu_startminimal(bool checked)
{
	getCConfig()->setValue("StartMinimal", checked);
}

void UpdaterClient::on_menu_autodownload(bool checked)
{
	getCConfig()->setValue("AutoDownload", checked);
}

void UpdaterClient::on_menu_autoinstall(bool checked)
{
	getCConfig()->setValue("AutoInstall", checked);
}

void UpdaterClient::on_menu_autorefresh(bool checked)
{
	getCConfig()->setValue("AutoRefresh", checked);

	if (checked)
	{
		pRefreshTimer = new QTimer();
		pRefreshTimer->start(getCConfig()->getValue("DefaultAutoRefreshTime").toLongLong());

		connect(pRefreshTimer, SIGNAL(timeout()), this, SLOT(on_refreshautotimerout()));
	}
	else
	{
		pRefreshTimer->stop();
		pubfunc::release(pRefreshTimer);
	}
}

void UpdaterClient::on_refreshautotimerout()
{
	if (this->isHidden())
		loadDownloadProgress(false);
}

void UpdaterClient::on_proxyClose()
{
	//! 如果还存在没有结束的下载,则跳出提示
	//! 如果用户执意退出,则保存断点续传
	if (filecount != SIZE(mapReadyPacks))
	{
		this->show();

		//! 存在没有下载好的包
		QMessageBox msg(QMessageBox::Warning, tr("warning"), tr("have some packs in downloading queue"),
			QMessageBox::Yes | QMessageBox::No, this);
		msg.button(QMessageBox::Yes)->setText(tr("Yes"));
		msg.button(QMessageBox::No)->setText(tr("No"));
		if (msg.exec() != QMessageBox::Yes)
		{
			//如果用户不点击退出
			directQuit = false;
			return;
		}
		else
		{
			//需要暂停下载
			if (!IsNull(opeFtp) && !IsNull(opeFile))
			{
				opeFtp->abort();
				opeFile->close();
			}

			directQuit = true;
		}
	}
	else
	{//不存在更新包,则允许退出
		directQuit = true;
	}

	QMetaObject::invokeMethod(this, "close");
}

void UpdaterClient::closeEvent(QCloseEvent* pevent)
{
	if (!directQuit)
	{
		this->hide();
		pevent->ignore();
		return;
	}
}

void UpdaterClient::loadDownloadProgress(bool useui)
{
	//初始化按钮状态
	ui.btndownloadall->setDisabled(false);

	//清理
	filecount = 0;
	mapReadyPacks.clear();
	QStandardItemModel* pStandardItemModel = (QStandardItemModel*)pmodelSortFilterProxy->sourceModel();
	//while (pStandardItemModel->rowCount())
	{
		pStandardItemModel->removeRows(0, pStandardItemModel->rowCount());
	}

	//开始流程
	DownloadProgress download;
	if (useui)
		download.exec();
	else
	{
		download.processClient2Server();
		download.processXml();
	}

	//根据计算的结构进行显示
	if (!download.sucess)
		return;

	CUpdateXmlPlistTree* pdataServerData = static_cast<CUpdateXmlPlistTree*>(getUpdateXmlPlistServerTree());
	CUpdateXmlPlistTree::TIterEachSoftware iterEachSoftware = pdataServerData->begin();
	//遍历所有软件
	while(iterEachSoftware != pdataServerData->end())
	{
		CUpdateXmlPlistTree::EachSoftUpdate* pEachSoft = &iterEachSoftware->second;
		CUpdateXmlPlistTree::EachSoftUpdate::TIterEachPack iterPack = pEachSoft->begin();
		//遍历每一个补丁包
		while(iterPack != pEachSoft->end())
		{
			CUpdatePack* pPack = &*iterPack;
			if (pPack->needupdate 
#ifndef _DEBUG
				&& !pPack->debugmode
#endif
				)
			{
				//addPackShow((QStandardItemModel*)pmodelSortFilterProxy->sourceModel(), pPack);
				mapReadyPacks[pPack->name] = pPack;
			}
			++iterPack;
		}
		++iterEachSoftware;
	}

	//显示所有可用的补丁
	TMapReadyPacks::iterator iterReadyPack = mapReadyPacks.begin();
	while(iterReadyPack != mapReadyPacks.end())
	{
		const IUpdatePack* pPack = iterReadyPack->second;
		//addPackShow((QStandardItemModel*)pmodelSortFilterProxy->sourceModel(), pPack);
		addPackShow(pStdItemModel, pPack);

		++iterReadyPack;
	}

	//显示其他信息
	QTextBrowser* packInfo = ui.packInfo;
	QString restr = QString("一共找到%1个更新需要执行。").arg(SIZE(mapReadyPacks));
	packInfo->setHtml(restr);

	//没有更新
	if (SIZE(mapReadyPacks) == 0)
	{
		QPushButton* pbtnDownAll = ui.btndownloadall;
		pbtnDownAll->setDisabled(false);//允许退出

		pbtnDownAll->setText(tr("EXIT"));
		packInfo->setHtml(tr("NONUPDATE"));
		btnMode = BM_Complete;
		return;
	}

	//如果有更新才进行如下显示
	if (SIZE(mapReadyPacks))
	{
		//显示气泡提示
		wTrayWindow::showTrayMessage(tr("info"), restr);

		//判断是否需要自动下载
		if (getCConfig()->getValue("AutoDownload").toBool())
		{
			//如果是自动安装,此时禁用安装按钮
			ui.btndownloadall->setDisabled(true);

			downloadPacks();
		}
	}
}

void UpdaterClient::addPackShow(QStandardItemModel* pModel, const IUpdatePack* pPack)
{
	/* same reason as initial procession
	pItems->insertRow(0);
	pItems->setData(pItems->index(0, 0), Qt::Checked, Qt::CheckStateRole);
	pItems->setData(pItems->index(0, 1), pPack->name, Qt::DisplayRole);
	QString datestr = QString::number(pPack->publishTime);
	QString dateyear, datemonth, dateday;
	datestr.remove(dateyear = datestr.left(4));
	datestr.remove(datemonth = datestr.left(2));
	datestr.remove(dateday = datestr.left(2));
	pItems->setData(pItems->index(0, 2), QDate(dateyear.toInt(), datemonth.toInt(), dateday.toInt()), Qt::DisplayRole);
	pItems->setData(pItems->index(0, 3), pPack->packSize, Qt::DisplayRole);
	pItems->setData(pItems->index(0, 4), pPack->softname, Qt::DisplayRole);
	*/

	QList<QStandardItem*> columns;
	QStandardItem* name = new QStandardItem(pPack->name);
	name->setEditable(false);
	
	if (!pPack->mustdownload)//如果是可选补丁
	{
		name->setCheckable(true);
		name->setCheckState(Qt::Checked);
	}

	QString datestr = QString::number(pPack->publishTime);
	QStandardItem* date = new QStandardItem(datestr);
	QString dateyear, datemonth, dateday;
	date->setText(datestr);
	datestr.remove(dateyear = datestr.left(4));
	datestr.remove(datemonth = datestr.left(2));
	datestr.remove(dateday = datestr.left(2));
	date->setData(QDate(dateyear.toInt(), datemonth.toInt(), dateday.toInt()));
	date->setEditable(false);

	QStandardItem* size = new QStandardItem(pPack->packSize);
	size->setEditable(false);

	QStandardItem* softname = new QStandardItem(pPack->softname);
	softname->setEditable(false);

	columns << name << date << size << softname;
	pModel->appendRow(columns);
}

//树形结构被点击
void UpdaterClient::on_Packs_clicked(const QModelIndex & qindex)
{
	if (!qindex.isValid())
		return;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -