forked from linuxdeepin/deepin-compressor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
3561 lines (3169 loc) · 135 KB
/
mainwindow.cpp
File metadata and controls
3561 lines (3169 loc) · 135 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (C) 2019 ~ 2020 Uniontech Software Technology Co.,Ltd.
// SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later
#include "mainwindow.h"
#include "loadingpage.h"
#include "homepage.h"
#include "compresspage.h"
#include "compresssettingpage.h"
#include "uncompresspage.h"
#include "progresspage.h"
#include "successpage.h"
#include "failurepage.h"
#include "pluginmanager.h"
#include "settingdialog.h"
#include "archivemanager.h"
#include "DebugTimeManager.h"
#include "popupdialog.h"
#include "progressdialog.h"
#include "datamanager.h"
#include "ddesktopservicesthread.h"
#include "openFileWatcher.h"
#include "mimetypes.h"
#include "customwidget.h"
#include "treeheaderview.h"
#include "compressview.h"
#include "uncompressview.h"
#include "uitools.h"
#include "calculatesizethread.h"
#include "eventlogutils.h"
#include <DFileDialog>
#include <DTitlebar>
#include <DWindowCloseButton>
#include <DWindowOptionButton>
#include <DArrowLineDrawer>
#include <DFontSizeManager>
#include <denhancedwidget.h>
#include <DSysInfo>
#include <QStackedWidget>
#include <QKeyEvent>
#include <QSettings>
#include <QDebug>
#include <QThreadPool>
#include <QtConcurrent/QtConcurrent>
#include <QScreen>
#include <QFormLayout>
#include <QShortcut>
#include <QJsonObject>
#include <QTimer>
#ifdef DTKCORE_CLASS_DConfigFile
#include <DConfig>
DCORE_USE_NAMESPACE
#endif
static QMutex mutex; // 静态全局变量只在定义该变量的源文件内有效
#define FILE_TRUNCATION_LENGTH 70
MainWindow::MainWindow(QWidget *parent)
: DMainWindow(parent)
, m_strProcessID(QString::number(QCoreApplication::applicationPid())) // 获取应用进程号
{
setWindowTitle(tr("Archive Manager"));
// 先构建初始界面
m_pMainWidget = new QStackedWidget(this); // 中心面板
m_pHomePage = new HomePage(this); // 首页
m_pMainWidget->addWidget(m_pHomePage);
setCentralWidget(m_pMainWidget); // 设置中心面板
m_pMainWidget->setCurrentIndex(0);
m_openkey = new QShortcut(QKeySequence(Qt::Key_Slash + Qt::CTRL + Qt::SHIFT), this); // Ctrl+Shift+/
m_openkey->setContext(Qt::ApplicationShortcut);
// 初始化标题栏
initTitleBar();
initData();
// 开启定时器刷新界面
m_iInitUITimer = startTimer(500);
QJsonObject obj{
{"tid", EventLogUtils::Start},
{"version", QCoreApplication::applicationVersion()},
{"mode", 1}
};
EventLogUtils::get().writeLogs(obj);
}
MainWindow::~MainWindow()
{
// 保存窗口大小状态
saveConfigWinState();
ArchiveManager::get_instance()->destory_instance();
// 清除缓存数据
QProcess p;
QString command = "rm";
QStringList args;
args.append("-rf");
args.append(TEMPPATH + QDir::separator() + m_strProcessID);
p.execute(command, args);
p.waitForFinished();
if (nullptr != m_mywork && m_mywork->isRunning()) {
m_mywork->set_thread_stop(true); // 结束计算大小线程
m_mywork->wait(); //必须等待线程结束
}
if (nullptr != m_mywork) {
m_mywork->deleteLater();
m_mywork = nullptr;
}
qInfo() << "应用正常退出";
}
bool MainWindow::checkHerePath(const QString &strPath)
{
QFileInfo info(strPath);
if (!(info.isWritable() && info.isExecutable())) { // 检查一选择保存路径是否有权限
TipDialog dialog(this);
// 屏幕居中显示
moveDialogToCenter(&dialog);
dialog.showDialog(tr("You do not have permission to save files here, please change and retry"), tr("OK", "button"), DDialog::ButtonNormal);
return false;
}
return true;
}
void MainWindow::initUI()
{
// 初始化界面
m_pLoadingPage = new LoadingPage(this); // 加载界面
m_pCompressPage = new CompressPage(this); // 压缩列表界面
m_pCompressSettingPage = new CompressSettingPage(this); // 压缩设置界面
m_pUnCompressPage = new UnCompressPage(this); // 解压列表界面
m_pProgressPage = new ProgressPage(this); // 进度界面
m_pSuccessPage = new SuccessPage(this); // 成功界面
m_pFailurePage = new FailurePage(this); // 失败界面
m_pProgressdialog = new ProgressDialog(this); //进度弹窗
m_commentProgressDialog = new CommentProgressDialog(this); // 更新注释进度弹窗
m_pSettingDlg = new SettingDialog(this);
m_pSettingDlg->setMinimumWidth(750);
// 添加界面至主界面
m_pMainWidget->addWidget(m_pCompressPage);
m_pMainWidget->addWidget(m_pCompressSettingPage);
m_pMainWidget->addWidget(m_pUnCompressPage);
m_pMainWidget->addWidget(m_pProgressPage);
m_pMainWidget->addWidget(m_pSuccessPage);
m_pMainWidget->addWidget(m_pFailurePage);
m_pMainWidget->addWidget(m_pLoadingPage);
// 创建打开文件监控
m_pOpenFileWatcher = new OpenFileWatcher(this);
// 刷新压缩设置界面格式选项
m_pCompressSettingPage->refreshMenu();
}
void MainWindow::initTitleBar()
{
// 创建菜单
QMenu *menu = new QMenu(this);
m_pOpenAction = menu->addAction(tr("Open file"), this, &MainWindow::slotChoosefiles);
menu->addAction(tr("Settings"), this, [ = ] { m_pSettingDlg->exec(); });
menu->addSeparator();
// 初始化标题栏菜单
titlebar()->setMenu(menu);
titlebar()->setTitle("");
// 设置应用程序图标
QIcon icon = QIcon::fromTheme("deepin-compressor");
titlebar()->setIcon(icon);
// titlebar widget
m_pTitleWidget = new TitleWidget(this);
m_pTitleWidget->setFocusPolicy(Qt::TabFocus);
this->titlebar()->addWidget(m_pTitleWidget, Qt::AlignLeft);
setTabOrder(this->titlebar(), m_pTitleWidget);
m_pTitleWidget->setVisible(false);
this->titlebar()->setFocusProxy(nullptr);
}
void MainWindow::initData()
{
// 初始化数据配置
m_pSettings = new QSettings(QDir(UiTools::getConfigPath()).filePath("config.conf"), QSettings::IniFormat, this);
if (m_pSettings->value("dir").toString().isEmpty()) {
m_pSettings->setValue("dir", "");
}
restoreConfigWinState(); // 恢复窗口状态(大小和最大化状态)
setMinimumSize(620, 300); // task 16309调整最小大小
}
void MainWindow::initConnections()
{
connect(DGuiApplicationHelper::instance(), &DGuiApplicationHelper::themeTypeChanged, this, &MainWindow::slotThemeChanged);
connect(m_pTitleWidget, &TitleWidget::sigTitleClicked, this, &MainWindow::slotTitleBtnClicked);
connect(m_pTitleWidget, &TitleWidget::sigCommentClicked, this, &MainWindow::slotTitleCommentButtonPressed);
connect(m_pHomePage, &HomePage::signalFileChoose, this, &MainWindow::slotChoosefiles);
connect(m_pHomePage, &HomePage::signalDragFiles, this, &MainWindow::slotDragSelectedFiles);
connect(m_pCompressPage, &CompressPage::signalLevelChanged, this, &MainWindow::slotCompressLevelChanged);
connect(m_pCompressPage, &CompressPage::signalCompressNextClicked, this, &MainWindow::slotCompressNext);
connect(m_pCompressPage, &CompressPage::signalFileChoose, this, &MainWindow::slotChoosefiles);
connect(m_pCompressSettingPage, &CompressSettingPage::signalCompressClicked, this, &MainWindow::slotCompress);
connect(m_pUnCompressPage, &UnCompressPage::signalUncompress, this, &MainWindow::slotUncompressClicked);
connect(m_pUnCompressPage, &UnCompressPage::signalExtract2Path, this, &MainWindow::slotExtract2Path);
connect(m_pUnCompressPage, &UnCompressPage::signalDelFiles, this, &MainWindow::slotDelFiles);
connect(m_pUnCompressPage, &UnCompressPage::signalRenameFile, this, &MainWindow::slotRenameFile);
connect(this, &MainWindow::sigRenameFile, m_pUnCompressPage, &UnCompressPage::sigRenameFile);
connect(m_pUnCompressPage, &UnCompressPage::signalOpenFile, this, &MainWindow::slotOpenFile);
connect(m_pUnCompressPage, &UnCompressPage::signalAddFiles2Archive, this, &MainWindow::slotAddFiles);
connect(m_pUnCompressPage, &UnCompressPage::signalFileChoose, this, &MainWindow::slotChoosefiles);
connect(m_pProgressPage, &ProgressPage::signalPause, this, &MainWindow::slotPause);
connect(m_pProgressPage, &ProgressPage::signalContinue, this, &MainWindow::slotContinue);
connect(m_pProgressPage, &ProgressPage::signalCancel, this, &MainWindow::slotCancel);
connect(m_pProgressdialog, &ProgressDialog::signalPause, this, &MainWindow::slotPause);
connect(m_pProgressdialog, &ProgressDialog::signalContinue, this, &MainWindow::slotContinue);
connect(m_pProgressdialog, &ProgressDialog::signalCancel, this, &MainWindow::slotCancel);
connect(m_pSuccessPage, &SuccessPage::sigBackButtonClicked, this, &MainWindow::slotSuccessReturn);
connect(m_pSuccessPage, &SuccessPage::signalViewFile, this, &MainWindow::slotSuccessView);
connect(m_pFailurePage, &FailurePage::sigFailRetry, this, &MainWindow::slotFailureRetry);
connect(m_pFailurePage, &FailurePage::sigBackButtonClickedOnFail, this, &MainWindow::slotFailureReturn);
connect(ArchiveManager::get_instance(), &ArchiveManager::signalJobFinished, this, &MainWindow::slotJobFinished);
connect(ArchiveManager::get_instance(), &ArchiveManager::signalprogress, this, &MainWindow::slotReceiveProgress);
connect(ArchiveManager::get_instance(), &ArchiveManager::signalCurFileName, this, &MainWindow::slotReceiveCurFileName);
connect(ArchiveManager::get_instance(), &ArchiveManager::signalFileWriteErrorName, this, &MainWindow::slotReceiveFileWriteErrorName);
connect(ArchiveManager::get_instance(), &ArchiveManager::signalCurArchiveName, this, &MainWindow::slotReceiveCurArchiveName);
connect(ArchiveManager::get_instance(), &ArchiveManager::signalQuery, this, &MainWindow::slotQuery);
connect(m_pOpenFileWatcher, &OpenFileWatcher::fileChanged, this, &MainWindow::slotOpenFileChanged);
//定制需求不显示ctrl+shift+?快捷键菜单
if(!property(ORDER_JSON).isValid()) {
connect(m_openkey, &QShortcut::activated, this, &MainWindow::slotShowShortcutTip);
}
}
void MainWindow::refreshPage()
{
qInfo() << "refreshPage()";
switch (m_ePageID) {
case PI_Home: {
resetMainwindow();
m_pMainWidget->setCurrentIndex(0);
setTitleButtonStyle(false, false);
titlebar()->setTitle("");
}
break;
case PI_Compress: {
m_pMainWidget->setCurrentIndex(1);
setTitleButtonStyle(true, false, DStyle::StandardPixmap::SP_IncreaseElement);
if (0 == m_iCompressedWatchTimerID) {
m_iCompressedWatchTimerID = startTimer(1);
}
titlebar()->setTitle(tr("Create New Archive"));
}
break;
case PI_CompressSetting: {
m_pMainWidget->setCurrentIndex(2);
setTitleButtonStyle(true, false, DStyle::StandardPixmap::SP_ArrowLeave);
if (0 == m_iCompressedWatchTimerID) {
m_iCompressedWatchTimerID = startTimer(1);
}
titlebar()->setTitle(tr("Create New Archive"));
}
break;
case PI_UnCompress: {
m_pMainWidget->setCurrentIndex(3);
bool bShowAddBtn = true;
if(property(ORDER_JSON).isValid()) {
if(m_pUnCompressPage) {
QVariantMap mapdata = m_pUnCompressPage->mapOrderJson();
if(mapdata.contains(ORDER_EDIT)) {
bShowAddBtn = mapdata.value(ORDER_EDIT).toBool();
m_pOpenAction->setEnabled(bShowAddBtn);
}
}
}
setTitleButtonStyle(bShowAddBtn, true, DStyle::StandardPixmap::SP_IncreaseElement);
titlebar()->setTitle(QFileInfo(m_pUnCompressPage->archiveFullPath()).fileName());
}
break;
case PI_AddCompressProgress: {
m_pMainWidget->setCurrentIndex(4);
setTitleButtonStyle(false, false);
m_pProgressPage->resetProgress();
titlebar()->setTitle(tr("Adding files to %1").arg(m_pProgressPage->archiveName()));
}
break;
case PI_CompressProgress: {
m_pMainWidget->setCurrentIndex(4);
setTitleButtonStyle(false, false);
m_pProgressPage->resetProgress();
titlebar()->setTitle(tr("Compressing"));
}
break;
case PI_UnCompressProgress: {
m_pMainWidget->setCurrentIndex(4);
setTitleButtonStyle(false, false);
m_pProgressPage->resetProgress();
titlebar()->setTitle(tr("Extracting"));
}
break;
case PI_DeleteProgress: {
m_pMainWidget->setCurrentIndex(4);
setTitleButtonStyle(false, false);
m_pProgressPage->resetProgress();
titlebar()->setTitle(tr("Deleting"));
}
break;
case PI_RenameProgress: {
m_pMainWidget->setCurrentIndex(4);
setTitleButtonStyle(false, false);
m_pProgressPage->resetProgress();
titlebar()->setTitle(tr("Renaming"));
}
break;
case PI_ConvertProgress: {
m_pMainWidget->setCurrentIndex(4);
setTitleButtonStyle(false, false);
m_pProgressPage->resetProgress();
titlebar()->setTitle(tr("Converting"));
}
break;
case PI_CommentProgress: {
m_pMainWidget->setCurrentIndex(4);
setTitleButtonStyle(false, false);
m_pProgressPage->resetProgress();
titlebar()->setTitle(tr("Updating comments")); // 正在更新注释
}
break;
case PI_Success: {
m_pMainWidget->setCurrentIndex(5);
setTitleButtonStyle(false, false);
titlebar()->setTitle("");
}
break;
case PI_Failure: {
m_pMainWidget->setCurrentIndex(6);
setTitleButtonStyle(false, false);
titlebar()->setTitle("");
}
break;
case PI_Loading: {
m_pMainWidget->setCurrentIndex(7);
setTitleButtonStyle(false, false);
titlebar()->setTitle("");
}
break;
}
//压缩文件焦点需压缩文件名上
if(m_ePageID == PI_CompressSetting) {
return;
}
//切换界面后焦点默认在标题栏上
this->titlebar()->setFocus();
}
qint64 MainWindow::calSelectedTotalFileSize(const QStringList &files)
{
QElapsedTimer time1;
time1.start();
m_stCompressParameter.qSize = 0;
foreach (QString file, files) {
QFileInfo fi(file);
if (fi.isFile()) { // 如果为文件,直接获取大小
qint64 curFileSize = fi.size();
#ifdef __aarch64__
if (maxFileSize_ < curFileSize) {
maxFileSize_ = curFileSize;
}
#endif
m_stCompressParameter.qSize += curFileSize;
} else if (fi.isDir()) { // 如果是文件夹,递归获取所有子文件大小总和
QtConcurrent::run(this, &MainWindow::calFileSizeByThread, file);
}
}
// 等待线程池结束
QThreadPool::globalInstance()->waitForDone();
qInfo() << QString("计算大小线程结束,耗时:%1ms,文件总大小:%2B").arg(time1.elapsed()).arg(m_stCompressParameter.qSize);
return m_stCompressParameter.qSize;
}
void MainWindow::calFileSizeByThread(const QString &path)
{
QDir dir(path);
if (!dir.exists())
return;
// 获得文件夹中的文件列表
QFileInfoList list = dir.entryInfoList(QDir::AllEntries | QDir::System
| QDir::NoDotAndDotDot | QDir::Hidden);
for (int i = 0; i < list.count(); ++i) {
QFileInfo fileInfo = list.at(i);
if (fileInfo.isDir()) {
// 如果是文件夹 则将此文件夹放入线程池中进行计算
QtConcurrent::run(this, &MainWindow::calFileSizeByThread, fileInfo.filePath());
} else {
mutex.lock();
// 如果是文件则直接计算大小
qint64 curFileSize = fileInfo.size();
#ifdef __aarch64__
if (maxFileSize_ < curFileSize) {
maxFileSize_ = curFileSize;
}
#endif
m_stCompressParameter.qSize += curFileSize;
mutex.unlock();
}
}
}
void MainWindow::setTitleButtonStyle(bool bVisible, bool bVisible2, DStyle::StandardPixmap pixmap)
{
m_pTitleWidget->setVisible(bVisible || bVisible2);
m_pTitleWidget->setTitleButtonStyle(bVisible, bVisible2, pixmap);
}
void MainWindow::loadArchive(const QString &strArchiveFullPath)
{
if (!QFileInfo(strArchiveFullPath).isReadable()) {
TipDialog dialog(this);
dialog.showDialog(tr("You do not have permission to load %1").arg(strArchiveFullPath), tr("OK", "button"), DDialog::ButtonNormal);
return;
}
PERF_PRINT_BEGIN("POINT-05", "加载时间");
m_operationtype = Operation_Load;
//处理分卷包名称
QString transFile = strArchiveFullPath;
QStringList listSupportedMimeTypes = PluginManager::get_instance().supportedWriteMimeTypes(PluginManager::SortByComment); // 获取支持的压缩格式
CustomMimeType mimeType = determineMimeType(transFile);
// 构建压缩包加载之后的数据
m_stUnCompressParameter.strFullPath = strArchiveFullPath;
UiTools::transSplitFileName(transFile, m_stUnCompressParameter.eSplitVolume);
QFileInfo fileinfo(transFile);
if (!fileinfo.exists()) {
// 分卷不完整(损坏)
// 比如打开1.7z.002时,1.7z.001不存在
m_ePageID = PI_Failure;
showErrorMessage(FI_Load, EI_ArchiveMissingVolume);
return;
}
m_stUnCompressParameter.bCommentModifiable = (mimeType.name() == "application/zip") ? true : false;
m_stUnCompressParameter.bMultiplePassword = (mimeType.name() == "application/zip") ? true : false;
m_stUnCompressParameter.bModifiable = (listSupportedMimeTypes.contains(mimeType.name()) && fileinfo.isWritable()
&& m_stUnCompressParameter.eSplitVolume == UnCompressParameter::ST_No); // 支持压缩且文件可写的非分卷格式才能修改数据
// 监听压缩包
watcherArchiveFile(transFile);
if(property(ORDER_JSON).isValid()) {
if(m_pUnCompressPage)
m_pUnCompressPage->setProperty(ORDER_JSON, property(ORDER_JSON));
}
m_pUnCompressPage->setArchiveFullPath(transFile, m_stUnCompressParameter); // 设置压缩包全路径和是否分卷
// 根据是否可修改压缩包标志位设置打开文件选项是否可用
m_pTitleWidget->setTitleButtonEnable(m_stUnCompressParameter.bModifiable);
m_pOpenAction->setEnabled(m_stUnCompressParameter.bModifiable);
// 设置默认解压路径
if (m_pSettingDlg->getDefaultExtractPath().isEmpty()) {
// 若默认为空,即设置默认解压路径为压缩包所在位置
m_pUnCompressPage->setDefaultUncompressPath(fileinfo.absolutePath()); // 设置默认解压路径
} else {
// 否则,使用设置选项中路径
m_pUnCompressPage->setDefaultUncompressPath(m_pSettingDlg->getDefaultExtractPath()); // 设置默认解压路径
}
// zip分卷指定使用cli7zplugin
UiTools::AssignPluginType eType = (UnCompressParameter::ST_Zip == m_stUnCompressParameter.eSplitVolume) ?
(UiTools::AssignPluginType::APT_Cli7z) : (UiTools::AssignPluginType::APT_Auto);
// 加载操作
if (ArchiveManager::get_instance()->loadArchive(transFile, eType)) {
m_pLoadingPage->setDes(tr("Loading, please wait..."));
m_pLoadingPage->startLoading(); // 开始加载
m_ePageID = PI_Loading;
} else {
// 无可用插件,回到首页
m_ePageID = PI_Home;
refreshPage();
show();
// 提示无插件
TipDialog dialog(this);
dialog.showDialog(tr("Plugin error"), tr("OK", "button"), DDialog::ButtonNormal);
}
}
void MainWindow::timerEvent(QTimerEvent *event)
{
if (m_iInitUITimer == event->timerId()) {
if (!m_initFlag) {
// 初始化界面
qInfo() << "初始化界面";
initUI();
initConnections();
m_initFlag = true;
}
killTimer(m_iInitUITimer);
m_iInitUITimer = 0;
} else if (m_iCompressedWatchTimerID == event->timerId()) {
// 对压缩文件的监控
QStringList listFiles = m_pCompressPage->compressFiles();
for (int i = 0; i < listFiles.count(); i++) {
QFileInfo info(listFiles[i]);
if (!info.exists()) {
// 先暂停操作
ArchiveManager::get_instance()->pauseOperation();
QString displayName = UiTools::toShortString(info.fileName());
QString strTips = tr("%1 was changed on the disk, please import it again.").arg(displayName);
TipDialog dialog(this);
dialog.showDialog(strTips, tr("OK", "button"), DDialog::ButtonNormal);
m_pCompressPage->refreshCompressedFiles(true, listFiles[i]);
ArchiveManager::get_instance()->cancelOperation();
// 返回到列表界面
if (m_ePageID != PI_Compress) {
m_ePageID = PI_Compress;
refreshPage();
}
// 如果待压缩文件列表数目为空,回到首页
if (m_pCompressPage->compressFiles().count() == 0) {
m_ePageID = PI_Home;
refreshPage();
}
}
}
}
}
void MainWindow::closeEvent(QCloseEvent *event)
{
if (m_operationtype != Operation_NULL) {
// 保存当前操作的状态以便还原操作
bool isPause = m_pProgressPage->isPause();
qInfo() << "点击x按钮之前的操作" << isPause;
// 当前还有操作正在进行
slotPause(); // 先暂停当前操作
// 创建询问关闭对话框
SimpleQueryDialog dialog(this);
int iResult = dialog.showDialog(tr("Are you sure you want to stop the ongoing task?"), tr("Cancel", "button"), DDialog::ButtonNormal, tr("Confirm", "button"), DDialog::ButtonRecommend);
// 点击确认时,停止当前操作,关闭应用
if (QDialog::Accepted == iResult) {
slotCancel(); // 执行取消操作
event->accept();
} else {
if (!isPause) { // 之前未暂停
slotContinue(); // 继续之前的操作
}
event->ignore(); // 忽略退出
}
} else {
event->accept(); // 忽略退出
}
}
bool MainWindow::checkSettings(const QString &file)
{
// QString strTransFileName = file;
// UnCompressParameter::SplitType type;
// UiTools::transSplitFileName(strTransFileName, type);
QFileInfo info(file);
if (!info.exists()) {
// 文件不存在
TipDialog dialog(this);
moveDialogToCenter(&dialog);
dialog.showDialog(tr("No such file or directory"), tr("OK", "button"), DDialog::ButtonNormal);
return false;
} else {
if (!info.isReadable()) {
TipDialog dialog(this);
dialog.showDialog(tr("You do not have permission to load %1").arg(file), tr("OK", "button"), DDialog::ButtonNormal);
return false;
}
if (info.isDir()) {
// 选择打开的是文件夹
TipDialog dialog(this);
moveDialogToCenter(&dialog);
dialog.showDialog(tr("The file format is not supported by Archive Manager"), tr("OK", "button"), DDialog::ButtonNormal);
return false;
} else {
// 文件判断
QString fileMime;
bool existMime = false; // 在设置界面是否被勾选
bool bArchive = false; // 是否是应用支持解压的格式
bool mimeIsChecked = true; // 默认该格式被勾选
// 判断内容
if (file.isEmpty()) {
existMime = true;
} else {
fileMime = determineMimeType(file).name();
if (fileMime.contains("application/"))
fileMime = fileMime.remove("application/");
if (fileMime.size() > 0) {
existMime = UiTools::isExistMimeType(fileMime, bArchive);
} else {
existMime = false;
}
}
// 若在关联类型中没有找到勾选的此格式
if (!existMime) {
QString str;
if (bArchive) {
// 如果是压缩包,提示勾选关联类型
str = tr("Please check the file association type in the settings of Archive Manager");
} else {
// 如果不是压缩包,提示非支持的压缩格式
str = tr("The file format is not supported by Archive Manager");
}
// 弹出提示对话框
TipDialog dialog;
moveDialogToCenter(&dialog);
int re = dialog.showDialog(str, tr("OK", "button"), DDialog::ButtonNormal);
if (re != 1) { // ?
mimeIsChecked = false;
}
}
return mimeIsChecked;
}
}
}
bool MainWindow::handleApplicationTabEventNotify(QObject *obj, QKeyEvent *evt)
{
#if 0
if (!m_pUnCompressPage || !m_pCompressPage /*|| !m_pCompressSetting*/) {
return false;
}
QObject *pLastObj = nullptr;
if (UiTools::isWayland()) {
pLastObj = titlebar()->findChild<DWindowOptionButton *>("DTitlebarDWindowOptionButton");
} else {
pLastObj = titlebar()->findChild<DWindowCloseButton *>("DTitlebarDWindowCloseButton");
}
int keyOfEvent = evt->key();
if (Qt::Key_Tab == keyOfEvent) { //tab焦点顺序:从上到下,从左到右
if (obj == titlebar()) { //焦点顺序:标题栏设置按钮->标题栏按钮
if (m_pTitleButton->isVisible() && m_pTitleButton->isEnabled()) {
m_pTitleButton->setFocus(Qt::TabFocusReason);
return true;
} if (m_pTitleCommentButton->isVisible() && m_pTitleCommentButton->isEnabled()) {
m_pTitleCommentButton->setFocus(Qt::TabFocusReason);
return true;
} else {
return false;
}
} else if (obj->objectName() == "CommentButton") { //焦点顺序:标题栏按钮->标题栏设置按钮
titlebar()->setFocus(Qt::TabFocusReason);
//titlebar不截获屏蔽掉,因为让他继续往下一menubutton发送tab
// return true;
} else if (obj->objectName() == "TitleButton" && !(m_pTitleCommentButton->isVisible() && m_pTitleCommentButton->isEnabled())) { //焦点顺序:标题栏按钮->标题栏设置按钮
titlebar()->setFocus(Qt::TabFocusReason);
//titlebar不截获屏蔽掉,因为让他继续往下一menubutton发送tab
// return true;
} else if (obj->objectName() == "gotoPreviousLabel") { //焦点顺序:返回上一页->文件列表
switch (m_ePageID) {
case PI_UnCompress:
m_pUnCompressPage->getUnCompressView()->setFocus(Qt::TabFocusReason);
break;
case PI_Compress:
m_pCompressPage->getCompressView()->setFocus(Qt::TabFocusReason);
break;
default:
return false;
}
return true;
} else if (obj->objectName() == "TableViewFile") { //焦点顺序:文件列表->解压路径按钮/下一步按钮
switch (m_ePageID) {
case PI_UnCompress:
m_pUnCompressPage->getUncompressPathBtn()->setFocus(Qt::TabFocusReason);
break;
case PI_Compress:
m_pCompressPage->getNextBtn()->setFocus(Qt::TabFocusReason);
break;
default:
return false;
}
return true;
} else if (obj == pLastObj) { //焦点顺序:关闭应用按钮->返回上一页/文件列表/压缩类型选择
switch (m_ePageID) {
case PI_UnCompress:
if (m_pUnCompressPage->getUnCompressView()->getHeaderView()->getpreLbl()->isVisible()) {
m_pUnCompressPage->getUnCompressView()->getHeaderView()->getpreLbl()->setFocus(Qt::TabFocusReason);
} else {
m_pUnCompressPage->getUnCompressView()->setFocus(Qt::TabFocusReason);
}
break;
case PI_Compress:
if (m_pCompressPage->getCompressView()->getHeaderView()->getpreLbl()->isVisible()) {
m_pCompressPage->getCompressView()->getHeaderView()->getpreLbl()->setFocus(Qt::TabFocusReason);
} else {
m_pCompressPage->getCompressView()->setFocus(Qt::TabFocusReason);
}
break;
case PI_CompressSetting:
m_pCompressSettingPage->getClickLbl()->setFocus(Qt::TabFocusReason);
break;
default:
return false;
}
return true;
}
} else if (Qt::Key_Backtab == keyOfEvent) { //shift+tab 焦点顺序,与tab焦点顺序相反
if (obj == pLastObj) {
if (m_pTitleCommentButton->isVisible() && m_pTitleCommentButton->isEnabled()) {
m_pTitleCommentButton->setFocus(Qt::BacktabFocusReason);
return true;
} else if (m_pTitleButton->isVisible() && m_pTitleButton->isEnabled()) {
m_pTitleButton->setFocus(Qt::BacktabFocusReason);
return true;
} else {
return false;
}
} else if (obj->objectName() == "TitleButton") {
switch (m_ePageID) {
case PI_UnCompress:
m_pUnCompressPage->getUnCompressBtn()->setFocus(Qt::BacktabFocusReason);
break;
case PI_Compress:
m_pCompressPage->getNextBtn()->setFocus(Qt::TabFocusReason);
break;
case PI_CompressSetting:
m_pCompressSettingPage->getCompressBtn()->setFocus(Qt::BacktabFocusReason);
break;
default:
return false;
}
return true;
} else if (obj->objectName() == "TableViewFile") {
if (nullptr != qobject_cast<DataTreeView *>(obj) && qobject_cast<DataTreeView *>(obj)->getHeaderView()->getpreLbl()->isVisible()) {
qobject_cast<DataTreeView *>(obj)->getHeaderView()->getpreLbl()->setFocus(Qt::BacktabFocusReason);
} else {
DWindowCloseButton *closeButton = titlebar()->findChild<DWindowCloseButton *>("DTitlebarDWindowCloseButton");
if (nullptr != closeButton) {
closeButton->setFocus(Qt::BacktabFocusReason);
}
}
return true;
} else if (obj->objectName() == "ClickTypeLabel" || obj->objectName() == "gotoPreviousLabel") {
DWindowCloseButton *closeButton = titlebar()->findChild<DWindowCloseButton *>("DTitlebarDWindowCloseButton");
if (closeButton) {
closeButton->setFocus(Qt::BacktabFocusReason);
}
return true;
} else if (obj == m_pCompressPage->getNextBtn()) {
m_pCompressPage->getCompressView()->setFocus(Qt::BacktabFocusReason);
return true;
} /*else if (obj == m_pUnCompressPage->getNextbutton()) {
m_pUnCompressPage->getPathCommandLinkButton()->setFocus(Qt::BacktabFocusReason);
return true;
}*/ else if (obj == m_pUnCompressPage->getUncompressPathBtn()) {
m_pUnCompressPage->getUnCompressView()->setFocus(Qt::BacktabFocusReason);
return true;
}
} else if (Qt::Key_Left == keyOfEvent || Qt::Key_Up == keyOfEvent) { //Key_Left、Key_Up几处顺序特殊处理*/
if (obj == m_pCompressPage->getNextBtn()) {
m_pCompressPage->getCompressView()->setFocus(Qt::BacktabFocusReason);
return true;
} else if (obj == m_pUnCompressPage->getUnCompressBtn()) {
m_pUnCompressPage->getUncompressPathBtn()->setFocus(Qt::BacktabFocusReason);
return true;
} else if (obj == m_pUnCompressPage->getUncompressPathBtn()) {
m_pUnCompressPage->getUnCompressView()->setFocus(Qt::BacktabFocusReason);
return true;
}
}
#endif
return false;
}
void MainWindow::handleQuit()
{
// 关闭处理
close();
}
void MainWindow::slotHandleArguments(const QStringList &listParam, MainWindow::ArgumentType eType)
{
qInfo() << listParam;
if (!m_initFlag) {
// 初始化界面
qInfo() << "初始化界面";
initUI();
initConnections();
m_initFlag = true;
}
if (listParam.count() == 0) {
delayQuitApp();
return;
}
qInfo() << "处理传入参数";
switch (eType) {
case AT_Open: { // 打开操作
if (!handleArguments_Open(listParam)) {
delayQuitApp();
return;
}
}
break;
case AT_RightMenu: { // 右键操作
if (!handleArguments_RightMenu(listParam)) {
delayQuitApp();
return;
}
}
break;
case AT_DragDropAdd: { // 拖拽追加操作
if (!handleArguments_Append(listParam)) {
delayQuitApp();
return;
}
}
break;
}
// 刷新界面并显示
refreshPage();
show();
}
void MainWindow::slotTitleBtnClicked()
{
if (PI_Home == m_ePageID || PI_Compress == m_ePageID || PI_UnCompress == m_ePageID) {
// 通过文件选择对话框选择文件进行操作
slotChoosefiles();
} else if (PI_CompressSetting == m_ePageID) {
// 压缩设置界面点击 返回
m_ePageID = PI_Compress;
refreshPage();
}
}
void MainWindow::slotChoosefiles()
{
if(property(ORDER_JSON).isValid()) {
if(m_pUnCompressPage) {
QVariantMap mapdata = m_pUnCompressPage->mapOrderJson();
if(mapdata.contains(ORDER_EDIT)) {
if(!mapdata.value(ORDER_EDIT).toBool()) return;
}
}
}
// 创建文件选择对话框
DFileDialog dialog(this);
dialog.setAcceptMode(DFileDialog::AcceptOpen);
dialog.setFileMode(DFileDialog::ExistingFiles);
dialog.setAllowMixedSelection(true);
// 获取配置中历史选择路径
QString historyDir = m_pSettings->value("dir").toString();
// 若历史选择路径为空,则默认为主目录
if (historyDir.isEmpty()) {
historyDir = QDir::homePath();
}
dialog.setDirectory(historyDir); // 设置对话框打开时的路径
const int mode = dialog.exec();
// 保存选中路径至配置中
m_pSettings->setValue("dir", dialog.directoryUrl().toLocalFile());
// 关闭或取消不处理
if (mode != QDialog::Accepted) {
return;
}
QStringList listSelFiles = dialog.selectedFiles();
if (listSelFiles.count() == 0)
return;
// 判断是否是本地设备文件,过滤 手机 网络 ftp smb 等
for (const auto &url : listSelFiles) {
if (!UiTools::isLocalDeviceFile(url)) {
return;
}
}
if (PI_Home == m_ePageID) {
if (listSelFiles.count() == 1 && UiTools::isArchiveFile(listSelFiles[0])) {
// 压缩包加载
loadArchive(listSelFiles[0]);
} else {
// 添加压缩文件
m_pCompressPage->addCompressFiles(listSelFiles);
m_ePageID = PI_Compress;
}
refreshPage();
} else if (PI_Compress == m_ePageID) {
// 添加压缩文件
m_pCompressPage->addCompressFiles(listSelFiles);
} else if (PI_UnCompress == m_ePageID) {
// 追加压缩
m_pUnCompressPage->addNewFiles(listSelFiles);
} else if (PI_CompressSetting == m_ePageID) {
// 追加压缩
m_pCompressPage->addCompressFiles(listSelFiles);
m_ePageID = PI_Compress;
refreshPage();
}
}
void MainWindow::slotDragSelectedFiles(const QStringList &listFiles)
{
// 未选择任何文件
if (listFiles.count() == 0) {
return;
}
/* 当选择的文件数目大于1时,执行压缩流程
* 若数目为1,根据文件格式判断是压缩还是需要解压
*/
if (listFiles.count() > 1) {
m_ePageID = PI_Compress;
m_pCompressPage->addCompressFiles(listFiles); // 添加压缩文件
} else {
if (UiTools::isArchiveFile(listFiles[0])) { // 压缩文件处理
loadArchive(listFiles[0]);
} else { // 普通文件处理
m_ePageID = PI_Compress;
m_pCompressPage->addCompressFiles(listFiles); // 添加压缩文件
}
}
refreshPage(); // 刷新界面
}
void MainWindow::slotCompressLevelChanged(bool bRootIndex)
{
m_pOpenAction->setEnabled(bRootIndex);
m_pTitleWidget->setTitleButtonVisible(bRootIndex);
}
void MainWindow::slotCompressNext()
{
QStringList listCompressFiles = m_pCompressPage->compressFiles(); // 获取待压缩的文件
m_pCompressSettingPage->setFileSize(listCompressFiles, calSelectedTotalFileSize(listCompressFiles));
//m_pCompressSettingPage->refreshMenu();
// 刷新界面 切换到压缩设置界面
m_ePageID = PI_CompressSetting;
refreshPage();
}
void MainWindow::slotCompress(const QVariant &val)
{
qInfo() << "点击了压缩按钮";