2
2
#include " ui_mainwindow.h"
3
3
// #include "QDebug"
4
4
5
+ MainWindow *singleton;
6
+
5
7
MainWindow::MainWindow (QWidget *parent) :
6
8
QMainWindow(parent),
7
9
ui(new Ui::MainWindow)
8
10
{
9
11
ui->setupUi (this );
12
+ singleton = this ;
10
13
setWindowFlags (Qt::FramelessWindowHint);
11
14
setAttribute (Qt::WA_TranslucentBackground);
12
15
mLocation = this ->geometry ();
@@ -51,6 +54,7 @@ MainWindow::MainWindow(QWidget *parent) :
51
54
ui->usernameLineEdit ->setAttribute (Qt::WA_InputMethodEnabled, false );
52
55
ui->passwordLineEdit ->setAttribute (Qt::WA_InputMethodEnabled, false );
53
56
ui->stackedWidget ->setCurrentIndex (2 );
57
+ ui->stackedWidgetBottom ->setCurrentIndex (0 );
54
58
AboutButton->hide ();// 在获取公告前隐藏这两个按钮
55
59
AdvancedButton->hide ();
56
60
n=new network ();
@@ -91,7 +95,6 @@ MainWindow::MainWindow(QWidget *parent) :
91
95
ui->autoLoginCheckBox ->setCheckState (Qt::Checked);
92
96
}
93
97
}
94
-
95
98
}
96
99
97
100
void MainWindow::showMessage (void )
@@ -434,14 +437,13 @@ void MainWindow::on_checkUpdateButton_clicked()
434
437
{
435
438
ui->enterButtonInAboutPage ->setEnabled (false );
436
439
ui->checkUpdateButton ->setEnabled (false );
437
- QStringList list;
438
440
ui->statusBar ->setText (" 检查更新中..." );
439
441
setStyleSheet (" QWidget#centralWidget{color:black;background:white;border:1px solid #3498DB;}" );
440
442
ui->widgetBottom ->setStyleSheet (" QLabel#statusBar{color:white;padding:5px 0px 5px;}"
441
443
" QWidget#widgetBottom{background:#3498DB;}" );
442
444
if (isOnline)
443
445
{
444
- int c=checkVersion (list ,n);
446
+ int c=checkVersion (updateInfo ,n);
445
447
if (c!=-1 )
446
448
{
447
449
ui->statusBar ->setText (" 检查更新中...成功!" );
@@ -475,12 +477,10 @@ void MainWindow::on_checkUpdateButton_clicked()
475
477
QString v;
476
478
v.sprintf (" %s" ,version);
477
479
QString ehaut=ui->eHautIco ->text ();
478
- QString url=QString (" <a href = \" %1\" >%2</a>" ).arg (list.at (4 )).arg (ehaut);
479
- s=s+" " +v+" 。最新版本 " +list.at (0 )+" ,发布于 " +list.at (1 )+" 。<br><br> <===================== <br> 点击左边蜗牛下载最新版!<br><br>备用地址:" +QString (" <a href = \" %1\" >%2</a>" ).arg (list.at (4 )).arg (" 点我下载!" );
480
-
481
- ui->eHautIco ->setText (url);
482
- ui->eHautIco ->setOpenExternalLinks (true );
483
- ui->aboutBox ->setHtml (s);
480
+ QString url=QString (" <a href = \" %1\" >%2</a>" ).arg (updateInfo.at (4 )).arg (ehaut);
481
+ s=s+" " +v+" 。最新版本 " +updateInfo.at (0 )+" ,发布于 " +updateInfo.at (1 )+" 。<br><br> <===================== <br> 点击左边蜗牛下载最新版!<br><br>备用地址:" +QString (" <a href = \" %1\" >%2</a>" ).arg (updateInfo.at (4 )).arg (" 点我下载!" );
482
+ ui->aboutBox ->setText (s);
483
+ ui->eHautIco ->setEnabled (true );
484
484
}
485
485
}
486
486
else
@@ -737,8 +737,7 @@ void MainWindow::on_loginButton_clicked(bool showmode)
737
737
}
738
738
ui->stackedWidget ->setCurrentIndex (3 );
739
739
getUserInfo (false );
740
- QStringList list;
741
- int c=checkVersion (list,n);
740
+ int c=checkVersion (updateInfo,n);
742
741
if (c==1 )
743
742
{
744
743
@@ -749,20 +748,18 @@ void MainWindow::on_loginButton_clicked(bool showmode)
749
748
list[3] sha1
750
749
list[4] url
751
750
*/
752
-
753
751
QString s=" 当前版本" ;
754
752
QString v;
755
753
v.sprintf (" %s" ,version);
756
754
QString ehaut=ui->eHautIco ->text ();
757
- QString url=QString (" <a href = \" %1\" >%2</a>" ).arg (list.at (4 )).arg (ehaut);
758
- s=s+" " +v+" 。最新版本 " +list.at (0 )+" ,发布于 " +list.at (1 )+" 。<br><br> <===================== <br> 点击左边蜗牛下载最新版!<br><br>备用地址:" +QString (" <a href = \" %1\" >%2</a>" ).arg (list.at (4 )).arg (" 点我下载!" );
755
+ s=s+" " +v+" 。最新版本 " +updateInfo.at (0 )+" ,发布于 " +updateInfo.at (1 )+" 。<br><br> <===================== <br> 点击左边蜗牛下载最新版!<br><br>备用地址:" +QString (" <a href = \" %1\" >%2</a>" ).arg (updateInfo.at (4 )).arg (" 点我下载!" );
759
756
760
757
AboutButton->setStyleSheet (" QPushButton {border-image: url(:/titleButtons/about_alert);}"
761
758
" QPushButton:hover {border-image: url(:/titleButtons/about_alert_hover);}"
762
759
" QPushButton:pressed {border-image: url(:/titleButtons/about_alert_pressed);}" );
763
- ui->eHautIco ->setText (url);
764
- ui->eHautIco ->setOpenExternalLinks (true );
760
+
765
761
ui->aboutBox ->setHtml (s);
762
+ ui->eHautIco ->setEnabled (true );
766
763
}
767
764
768
765
}
@@ -814,3 +811,156 @@ void MainWindow::on_loginButton_clicked(bool showmode)
814
811
}
815
812
ui->loginButton ->setEnabled (true );
816
813
}
814
+
815
+ void MainWindow::on_eHautIco_clicked ()
816
+ {
817
+ ui->eHautIco ->setEnabled (false );
818
+ ui->enterButtonInAboutPage ->setEnabled (false );
819
+ ui->checkUpdateButton ->setEnabled (false );
820
+ setStyleSheet (" QWidget#centralWidget{color:black;background:white;border:1px solid #1aad18;}" );
821
+ ui->widgetBottom ->setStyleSheet (" QLabel#statusBar{color:white;padding:5px 0px 5px;}"
822
+ " QWidget#widgetBottom{background:#1aad18;}" );
823
+ info=" 检查是否存在老版本更新包..." ;
824
+ ui->aboutBox ->setHtml (info);
825
+ bool isUpdateProgramExist=s->checkUpdateProgramisExist (" update.exe" );
826
+ if (isUpdateProgramExist)
827
+ {
828
+ info=info+" 删除成功!<br>" ;
829
+ }
830
+ else
831
+ {
832
+ info=info+" 无需删除!<br>" ;
833
+ }
834
+ info=info+" 下载更新包中..." ;
835
+ ui->aboutBox ->setHtml (info);
836
+ ui->stackedWidgetBottom ->setCurrentIndex (1 );
837
+ ui->downloadBar ->setMaximum (100 );
838
+ ui->downloadBar ->setValue (0 );
839
+ QString downloadUrl=updateInfo.at (4 );
840
+ static int retry=1 ;
841
+ static bool isTrue=false ;
842
+ QString path=QCoreApplication::applicationDirPath ()+" /update.exe" ;
843
+ FILE* fp = nullptr ;
844
+ while (retry<=3 )
845
+ {
846
+ if (retry>1 )
847
+ {
848
+ Sleep (5000 );
849
+ info=info+" 尝试第 " +QString::number (retry)+" 次下载:<br>" ;
850
+ info=info+" 下载更新包中..." ;
851
+ ui->aboutBox ->setHtml (info);
852
+ }
853
+ CURL* curl = curl_easy_init ();
854
+ if (!(curl))
855
+ {// 如果非正常初始化
856
+ exit (-1 );
857
+ }
858
+ fp=fopen (path.toStdString ().c_str (), " wb" );
859
+ rewind (fp);
860
+ curl_easy_setopt (curl, CURLOPT_URL,downloadUrl.toStdString ().c_str ());
861
+ // 设置接收数据的回调
862
+ curl_easy_setopt (curl,CURLOPT_SSL_VERIFYPEER,false );// 设定为不验证证书和HOST
863
+ curl_easy_setopt (curl,CURLOPT_SSL_VERIFYHOST,false );
864
+ curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, storage::saveDataTodisk);
865
+ curl_easy_setopt (curl, CURLOPT_WRITEDATA, fp);
866
+ // 设置重定向的最大次数
867
+ curl_easy_setopt (curl, CURLOPT_MAXREDIRS, 5 );
868
+ // 设置301、302跳转跟随location
869
+ // curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
870
+ curl_easy_setopt (curl, CURLOPT_NOPROGRESS, 0L );
871
+ // 设置进度回调函数
872
+ curl_easy_setopt (curl, CURLOPT_PROGRESSFUNCTION, MainWindow::progressUpdate);
873
+ // 设置超时
874
+ curl_easy_setopt (curl,CURLOPT_CONNECTTIMEOUT,3L );
875
+ // 开始执行请求
876
+ CURLcode retcCode = curl_easy_perform (curl);
877
+ if (retcCode!=CURLE_OK)
878
+ {
879
+ info=info+" 下载失败!<br>" ;
880
+ ui->aboutBox ->setHtml (info);
881
+ retry++;
882
+ }
883
+ else
884
+ {
885
+ info=info+" 下载成功!<br>" ;
886
+ isTrue=true ;
887
+ ui->aboutBox ->setHtml (info);
888
+ break ;
889
+ }
890
+ curl_easy_cleanup (curl);
891
+ }
892
+ if (fp!=nullptr )
893
+ fclose (fp);
894
+ if (isTrue)
895
+ {
896
+ info=info+" 校验下载值中..." ;
897
+ ui->aboutBox ->setHtml (info);
898
+ QString sha1;
899
+ sha1.prepend (s->fileChecksum (" update.exe" ));
900
+ // qDebug()<<sha1<<" "<<updateInfo.at(3);
901
+ if (updateInfo.at (3 ).compare (sha1)==0 )
902
+ {
903
+ info=info+" 校验成功!<br>" ;
904
+ ui->aboutBox ->setHtml (info);
905
+ ui->downloadBar ->setMaximum (100 );
906
+ ui->downloadBar ->setValue (100 );
907
+ info=info+" 5s后尝试启动自动更新程序!<br>" ;
908
+ ui->aboutBox ->setHtml (info);
909
+ QTimer::singleShot (5000 , this , SLOT (startUpdate ()));
910
+ }
911
+ else
912
+ {
913
+ info=info+" 校验失败!<br>" ;
914
+ ui->statusBar ->setText (" 自动更新失败!请重试!" );
915
+ setStyleSheet (" QWidget#centralWidget{color:black;background:white;border:1px solid #E05D6F;}" );
916
+ ui->widgetBottom ->setStyleSheet (" QLabel#statusBar{color:white;padding:5px 0px 5px;}"
917
+ " QWidget#widgetBottom{background:#E05D6F;}" );
918
+ ui->stackedWidgetBottom ->setCurrentIndex (0 );
919
+ info=info+" 正在删除更新包..." ;
920
+ ui->aboutBox ->setHtml (info);
921
+ bool isDelete=s->checkUpdateProgramisExist (" update.exe" );
922
+ if (isDelete)
923
+ {
924
+ info=info+" 删除成功!<br>请您重试自动更新!" ;
925
+ ui->aboutBox ->setHtml (info);
926
+ }
927
+ else
928
+ {
929
+ info=info+" 不可预知的错误!<br>" ;
930
+ ui->aboutBox ->setHtml (info);
931
+ }
932
+ }
933
+ }
934
+
935
+ ui->enterButtonInAboutPage ->setEnabled (true );
936
+ ui->checkUpdateButton ->setEnabled (true );
937
+ ui->eHautIco ->setEnabled (true );
938
+ }
939
+
940
+ void MainWindow::startUpdate ()
941
+ {
942
+ QString path=QCoreApplication::applicationDirPath ()+" /update.exe" ;
943
+ QProcess process (this );
944
+ bool isStart=process.startDetached (path);\
945
+ if (isStart)
946
+ qApp->exit (0 );
947
+ else
948
+ {
949
+ info=info+" 调用失败请重试!<br>" ;
950
+ ui->statusBar ->setText (" 自动更新失败!请重试!" );
951
+ setStyleSheet (" QWidget#centralWidget{color:black;background:white;border:1px solid #E05D6F;}" );
952
+ ui->widgetBottom ->setStyleSheet (" QLabel#statusBar{color:white;padding:5px 0px 5px;}"
953
+ " QWidget#widgetBottom{background:#E05D6F;}" );
954
+ ui->stackedWidgetBottom ->setCurrentIndex (0 );
955
+ }
956
+ }
957
+
958
+ int MainWindow::progressUpdate (void *clientp, double dltotal, double dlnow, double ultotal, double ulnow)
959
+ {
960
+ if ( dltotal > -0.1 && dltotal < 0.1 )
961
+ return 0 ;
962
+ int nPos = (int ) ( (dlnow/dltotal)*90.0 );
963
+ // ui->downloadBar->setValue(nPos);
964
+ singleton->ui ->downloadBar ->setValue (nPos);
965
+ return 0 ;
966
+ }
0 commit comments