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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
|
/* $Id$ */
/** @file
* VBox Qt GUI - UIMediumManager class implementation.
*/
/*
* Copyright (C) 2006-2019 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* you can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
/* Qt includes: */
#include <QFrame>
#include <QHBoxLayout>
#include <QHeaderView>
#include <QLabel>
#include <QMenuBar>
#include <QProgressBar>
#include <QPushButton>
/* GUI includes: */
#include "QIDialogButtonBox.h"
#include "QIFileDialog.h"
#include "QILabel.h"
#include "QIMessageBox.h"
#include "QITabWidget.h"
#include "VBoxGlobal.h"
#include "UIActionPoolManager.h"
#include "UIExtraDataManager.h"
#include "UIMediumDetailsWidget.h"
#include "UIMediumItem.h"
#include "UIMediumManager.h"
#include "UIMediumSearchWidget.h"
#include "UIWizardCloneVD.h"
#include "UIMessageCenter.h"
#include "UIToolBar.h"
#include "UIIconPool.h"
#include "UIMedium.h"
/* COM includes: */
#include "COMEnums.h"
#include "CMachine.h"
#include "CMediumAttachment.h"
#include "CMediumFormat.h"
#include "CStorageController.h"
#include "CSystemProperties.h"
#ifdef VBOX_WS_MAC
# include "UIWindowMenuManager.h"
#endif /* VBOX_WS_MAC */
/** Functor allowing to check if passed UIMediumItem is suitable by @a uID. */
class CheckIfSuitableByID : public CheckIfSuitableBy
{
public:
/** Constructor accepting @a uID to compare with. */
CheckIfSuitableByID(const QUuid &uID) : m_uID(uID) {}
private:
/** Determines whether passed UIMediumItem is suitable by @a uID. */
bool isItSuitable(UIMediumItem *pItem) const { return pItem->id() == m_uID; }
/** Holds the @a uID to compare to. */
QUuid m_uID;
};
/** Functor allowing to check if passed UIMediumItem is suitable by @a state. */
class CheckIfSuitableByState : public CheckIfSuitableBy
{
public:
/** Constructor accepting @a state to compare with. */
CheckIfSuitableByState(KMediumState state) : m_state(state) {}
private:
/** Determines whether passed UIMediumItem is suitable by @a state. */
bool isItSuitable(UIMediumItem *pItem) const { return pItem->state() == m_state; }
/** Holds the @a state to compare to. */
KMediumState m_state;
};
/*********************************************************************************************************************************
* Class UIEnumerationProgressBar implementation. *
*********************************************************************************************************************************/
UIEnumerationProgressBar::UIEnumerationProgressBar(QWidget *pParent /* = 0 */)
: QWidget(pParent)
{
/* Prepare: */
prepare();
}
void UIEnumerationProgressBar::setText(const QString &strText)
{
m_pLabel->setText(strText);
}
int UIEnumerationProgressBar::value() const
{
return m_pProgressBar->value();
}
void UIEnumerationProgressBar::setValue(int iValue)
{
m_pProgressBar->setValue(iValue);
}
void UIEnumerationProgressBar::setMaximum(int iValue)
{
m_pProgressBar->setMaximum(iValue);
}
void UIEnumerationProgressBar::prepare()
{
/* Create layout: */
QHBoxLayout *pLayout = new QHBoxLayout(this);
{
/* Configure layout: */
pLayout->setContentsMargins(0, 0, 0, 0);
/* Create label: */
m_pLabel = new QLabel;
/* Create progress-bar: */
m_pProgressBar = new QProgressBar;
{
/* Configure progress-bar: */
m_pProgressBar->setTextVisible(false);
}
/* Add widgets into layout: */
pLayout->addWidget(m_pLabel);
pLayout->addWidget(m_pProgressBar);
}
}
/*********************************************************************************************************************************
* Class UIMediumManagerWidget implementation. *
*********************************************************************************************************************************/
UIMediumManagerWidget::UIMediumManagerWidget(EmbedTo enmEmbedding, UIActionPool *pActionPool,
bool fShowToolbar /* = true */, QWidget *pParent /* = 0 */)
: QIWithRetranslateUI<QWidget>(pParent)
, m_enmEmbedding(enmEmbedding)
, m_pActionPool(pActionPool)
, m_fShowToolbar(fShowToolbar)
, m_fPreventChangeCurrentItem(false)
, m_pTabWidget(0)
, m_iTabCount(3)
, m_fInaccessibleHD(false)
, m_fInaccessibleCD(false)
, m_fInaccessibleFD(false)
, m_iconHD(UIIconPool::iconSet(":/hd_16px.png", ":/hd_disabled_16px.png"))
, m_iconCD(UIIconPool::iconSet(":/cd_16px.png", ":/cd_disabled_16px.png"))
, m_iconFD(UIIconPool::iconSet(":/fd_16px.png", ":/fd_disabled_16px.png"))
, m_pDetailsWidget(0)
, m_pToolBar(0)
, m_pProgressBar(0)
, m_pSearchWidget(0)
{
/* Prepare: */
prepare();
}
QMenu *UIMediumManagerWidget::menu() const
{
return m_pActionPool->action(UIActionIndexST_M_MediumWindow)->menu();
}
void UIMediumManagerWidget::setProgressBar(UIEnumerationProgressBar *pProgressBar)
{
/* Cache progress-bar reference:*/
m_pProgressBar = pProgressBar;
/* Update translation: */
retranslateUi();
}
void UIMediumManagerWidget::retranslateUi()
{
/* Adjust toolbar: */
#ifdef VBOX_WS_MAC
// WORKAROUND:
// There is a bug in Qt Cocoa which result in showing a "more arrow" when
// the necessary size of the toolbar is increased. Also for some languages
// the with doesn't match if the text increase. So manually adjust the size
// after changing the text. */
if (m_pToolBar)
m_pToolBar->updateLayout();
#endif
/* Translate tab-widget: */
if (m_pTabWidget)
{
m_pTabWidget->setTabText(tabIndex(UIMediumDeviceType_HardDisk), UIMediumManager::tr("&Hard disks"));
m_pTabWidget->setTabText(tabIndex(UIMediumDeviceType_DVD), UIMediumManager::tr("&Optical disks"));
m_pTabWidget->setTabText(tabIndex(UIMediumDeviceType_Floppy), UIMediumManager::tr("&Floppy disks"));
}
/* Translate HD tree-widget: */
QITreeWidget *pTreeWidgetHD = treeWidget(UIMediumDeviceType_HardDisk);
if (pTreeWidgetHD)
{
pTreeWidgetHD->headerItem()->setText(0, UIMediumManager::tr("Name"));
pTreeWidgetHD->headerItem()->setText(1, UIMediumManager::tr("Virtual Size"));
pTreeWidgetHD->headerItem()->setText(2, UIMediumManager::tr("Actual Size"));
}
/* Translate CD tree-widget: */
QITreeWidget *pTreeWidgetCD = treeWidget(UIMediumDeviceType_DVD);
if (pTreeWidgetCD)
{
pTreeWidgetCD->headerItem()->setText(0, UIMediumManager::tr("Name"));
pTreeWidgetCD->headerItem()->setText(1, UIMediumManager::tr("Size"));
}
/* Translate FD tree-widget: */
QITreeWidget *pTreeWidgetFD = treeWidget(UIMediumDeviceType_Floppy);
if (pTreeWidgetFD)
{
pTreeWidgetFD->headerItem()->setText(0, UIMediumManager::tr("Name"));
pTreeWidgetFD->headerItem()->setText(1, UIMediumManager::tr("Size"));
}
/* Translate progress-bar: */
if (m_pProgressBar)
{
m_pProgressBar->setText(UIMediumManager::tr("Checking accessibility"));
#ifdef VBOX_WS_MAC
/* Make sure that the widgets aren't jumping around
* while the progress-bar get visible. */
m_pProgressBar->adjustSize();
//int h = m_pProgressBar->height();
//if (m_pButtonBox)
// m_pButtonBox->setMinimumHeight(h + 12);
#endif
}
/* Full refresh if there is at least one item present: */
if ( (pTreeWidgetHD && pTreeWidgetHD->topLevelItemCount())
|| (pTreeWidgetCD && pTreeWidgetCD->topLevelItemCount())
|| (pTreeWidgetFD && pTreeWidgetFD->topLevelItemCount()))
sltRefreshAll();
}
void UIMediumManagerWidget::sltResetMediumDetailsChanges()
{
/* Push the current item data into details-widget: */
sltHandleCurrentTabChanged();
}
void UIMediumManagerWidget::sltApplyMediumDetailsChanges()
{
/* Get current medium-item: */
UIMediumItem *pMediumItem = currentMediumItem();
AssertMsgReturnVoid(pMediumItem, ("Current item must not be null"));
AssertReturnVoid(!pMediumItem->id().isNull());
/* Get item data: */
UIDataMedium oldData = *pMediumItem;
UIDataMedium newData = m_pDetailsWidget->data();
/* Search for corresponding medium: */
CMedium comMedium = vboxGlobal().medium(pMediumItem->id()).medium();
/* Try to assign new medium type: */
if ( comMedium.isOk()
&& newData.m_options.m_enmType != oldData.m_options.m_enmType)
{
/* Check if we need to release medium first: */
bool fDo = true;
if ( pMediumItem->machineIds().size() > 1
|| ( ( newData.m_options.m_enmType == KMediumType_Immutable
|| newData.m_options.m_enmType == KMediumType_MultiAttach)
&& pMediumItem->machineIds().size() > 0))
fDo = pMediumItem->release(true);
if (fDo)
{
comMedium.SetType(newData.m_options.m_enmType);
/* Show error message if necessary: */
if (!comMedium.isOk())
msgCenter().cannotChangeMediumType(comMedium, oldData.m_options.m_enmType, newData.m_options.m_enmType, this);
}
}
/* Try to assign new medium location: */
if ( comMedium.isOk()
&& newData.m_options.m_strLocation != oldData.m_options.m_strLocation)
{
/* Prepare move storage progress: */
CProgress comProgress = comMedium.MoveTo(newData.m_options.m_strLocation);
/* Show error message if necessary: */
if (!comMedium.isOk())
msgCenter().cannotMoveMediumStorage(comMedium,
oldData.m_options.m_strLocation,
newData.m_options.m_strLocation,
this);
else
{
/* Show move storage progress: */
msgCenter().showModalProgressDialog(comProgress, UIMediumManager::tr("Moving medium..."),
":/progress_media_move_90px.png", this);
/* Show error message if necessary: */
if (!comProgress.isOk() || comProgress.GetResultCode() != 0)
msgCenter().cannotMoveMediumStorage(comProgress,
oldData.m_options.m_strLocation,
newData.m_options.m_strLocation,
this);
}
}
/* Try to assign new medium size: */
if ( comMedium.isOk()
&& newData.m_options.m_uLogicalSize != oldData.m_options.m_uLogicalSize)
{
/* Prepare resize storage progress: */
CProgress comProgress = comMedium.Resize(newData.m_options.m_uLogicalSize);
/* Show error message if necessary: */
if (!comMedium.isOk())
msgCenter().cannotResizeHardDiskStorage(comMedium,
oldData.m_options.m_strLocation,
vboxGlobal().formatSize(oldData.m_options.m_uLogicalSize),
vboxGlobal().formatSize(newData.m_options.m_uLogicalSize),
this);
else
{
/* Show resize storage progress: */
msgCenter().showModalProgressDialog(comProgress, UIMediumManager::tr("Resizing medium..."),
":/progress_media_resize_90px.png", this);
/* Show error message if necessary: */
if (!comProgress.isOk() || comProgress.GetResultCode() != 0)
msgCenter().cannotResizeHardDiskStorage(comProgress,
oldData.m_options.m_strLocation,
vboxGlobal().formatSize(oldData.m_options.m_uLogicalSize),
vboxGlobal().formatSize(newData.m_options.m_uLogicalSize),
this);
}
}
/* Try to assign new medium description: */
if ( comMedium.isOk()
&& newData.m_options.m_strDescription != oldData.m_options.m_strDescription)
{
comMedium.SetDescription(newData.m_options.m_strDescription);
/* Show error message if necessary: */
if (!comMedium.isOk())
msgCenter().cannotChangeMediumDescription(comMedium,
oldData.m_options.m_strLocation,
this);
}
/* Recache current item: */
pMediumItem->refreshAll();
/* Push the current item data into details-widget: */
sltHandleCurrentTabChanged();
}
void UIMediumManagerWidget::sltHandleMediumCreated(const QUuid &uMediumID)
{
/* Search for corresponding medium: */
UIMedium medium = vboxGlobal().medium(uMediumID);
/* Ignore non-interesting media: */
if (medium.isNull() || medium.isHostDrive())
return;
/* Ignore media (and their children) which are
* marked as hidden or attached to hidden machines only: */
if (UIMedium::isMediumAttachedToHiddenMachinesOnly(medium))
return;
/* Create medium-item for corresponding medium: */
UIMediumItem *pMediumItem = createMediumItem(medium);
/* Make sure medium-item was created: */
if (!pMediumItem)
return;
/* If medium-item change allowed and
* 1. medium-enumeration is not currently in progress or
* 2. if there is no currently medium-item selected
* we have to choose newly added medium-item as current one: */
if ( !m_fPreventChangeCurrentItem
&& ( !vboxGlobal().isMediumEnumerationInProgress()
|| !mediumItem(medium.type())))
setCurrentItem(treeWidget(medium.type()), pMediumItem);
}
void UIMediumManagerWidget::sltHandleMediumDeleted(const QUuid &uMediumID)
{
/* Make sure corresponding medium-item deleted: */
deleteMediumItem(uMediumID);
}
void UIMediumManagerWidget::sltHandleMediumEnumerationStart()
{
/* Disable 'refresh' action: */
if (m_pActionPool->action(UIActionIndexST_M_Medium_S_Refresh))
m_pActionPool->action(UIActionIndexST_M_Medium_S_Refresh)->setEnabled(false);
/* Disable details-widget: */
if (m_pDetailsWidget)
m_pDetailsWidget->setOptionsEnabled(false);
/* Reset and show progress-bar: */
if (m_pProgressBar)
{
m_pProgressBar->setMaximum(vboxGlobal().mediumIDs().size());
m_pProgressBar->setValue(0);
m_pProgressBar->show();
}
/* Reset inaccessibility flags: */
m_fInaccessibleHD =
m_fInaccessibleCD =
m_fInaccessibleFD = false;
/* Reset tab-widget icons: */
if (m_pTabWidget)
{
m_pTabWidget->setTabIcon(tabIndex(UIMediumDeviceType_HardDisk), m_iconHD);
m_pTabWidget->setTabIcon(tabIndex(UIMediumDeviceType_DVD), m_iconCD);
m_pTabWidget->setTabIcon(tabIndex(UIMediumDeviceType_Floppy), m_iconFD);
}
/* Repopulate tree-widgets content: */
repopulateTreeWidgets();
/* Re-fetch all current medium-items: */
refetchCurrentMediumItems();
refetchCurrentChosenMediumItem();
}
void UIMediumManagerWidget::sltHandleMediumEnumerated(const QUuid &uMediumID)
{
/* Search for corresponding medium: */
UIMedium medium = vboxGlobal().medium(uMediumID);
/* Ignore non-interesting media: */
if (medium.isNull() || medium.isHostDrive())
return;
/* Ignore media (and their children) which are
* marked as hidden or attached to hidden machines only: */
if (UIMedium::isMediumAttachedToHiddenMachinesOnly(medium))
return;
/* Update medium-item for corresponding medium: */
updateMediumItem(medium);
/* Advance progress-bar: */
if (m_pProgressBar)
m_pProgressBar->setValue(m_pProgressBar->value() + 1);
}
void UIMediumManagerWidget::sltHandleMediumEnumerationFinish()
{
/* Hide progress-bar: */
if (m_pProgressBar)
m_pProgressBar->hide();
/* Enable details-widget: */
if (m_pDetailsWidget)
m_pDetailsWidget->setOptionsEnabled(true);
/* Enable 'refresh' action: */
if (m_pActionPool->action(UIActionIndexST_M_Medium_S_Refresh))
m_pActionPool->action(UIActionIndexST_M_Medium_S_Refresh)->setEnabled(true);
/* Re-fetch all current medium-items: */
refetchCurrentMediumItems();
refetchCurrentChosenMediumItem();
}
void UIMediumManagerWidget::sltAddMedium()
{
QString strDefaultMachineFolder = vboxGlobal().virtualBox().GetSystemProperties().GetDefaultMachineFolder();
vboxGlobal().openMediumWithFileOpenDialog(currentMediumType(), this, strDefaultMachineFolder);
}
void UIMediumManagerWidget::sltCopyMedium()
{
/* Get current medium-item: */
UIMediumItem *pMediumItem = currentMediumItem();
AssertMsgReturnVoid(pMediumItem, ("Current item must not be null"));
AssertReturnVoid(!pMediumItem->id().isNull());
/* Copy current medium-item: */
//pMediumItem->copy();
/* Show Clone VD wizard: */
UIMedium medium = pMediumItem->medium();
UISafePointerWizard pWizard = new UIWizardCloneVD(currentTreeWidget(), medium.medium());
pWizard->prepare();
pWizard->exec();
/* Delete if still exists: */
if (pWizard)
delete pWizard;
}
void UIMediumManagerWidget::sltMoveMedium()
{
/* Get current medium-item: */
UIMediumItem *pMediumItem = currentMediumItem();
AssertMsgReturnVoid(pMediumItem, ("Current item must not be null"));
AssertReturnVoid(!pMediumItem->id().isNull());
/* Copy current medium-item: */
pMediumItem->move();
/* Push the current item data into details-widget: */
sltHandleCurrentTabChanged();
}
void UIMediumManagerWidget::sltRemoveMedium()
{
/* Get current medium-item: */
UIMediumItem *pMediumItem = currentMediumItem();
AssertMsgReturnVoid(pMediumItem, ("Current item must not be null"));
AssertReturnVoid(!pMediumItem->id().isNull());
/* Remove current medium-item: */
pMediumItem->remove();
}
void UIMediumManagerWidget::sltReleaseMedium()
{
/* Get current medium-item: */
UIMediumItem *pMediumItem = currentMediumItem();
AssertMsgReturnVoid(pMediumItem, ("Current item must not be null"));
AssertReturnVoid(!pMediumItem->id().isNull());
/* Remove current medium-item: */
bool fResult = pMediumItem->release();
/* Refetch currently chosen medium-item: */
if (fResult)
refetchCurrentChosenMediumItem();
}
void UIMediumManagerWidget::sltToggleMediumDetailsVisibility(bool fVisible)
{
/* Save the setting: */
gEDataManager->setVirtualMediaManagerDetailsExpanded(fVisible);
/* Toggle medium details visibility: */
if (m_pDetailsWidget)
m_pDetailsWidget->setVisible(fVisible);
/* Notify external lsiteners: */
emit sigMediumDetailsVisibilityChanged(fVisible);
}
void UIMediumManagerWidget::sltToggleMediumSearchVisibility(bool fVisible)
{
/* Save the setting: */
gEDataManager->setVirtualMediaManagerSearchWidgetExpanded(fVisible);
/* Toggle medium details visibility: */
if (m_pSearchWidget)
m_pSearchWidget->setVisible(fVisible);
}
void UIMediumManagerWidget::sltRefreshAll()
{
/* Start medium-enumeration: */
vboxGlobal().startMediumEnumeration();
}
void UIMediumManagerWidget::sltHandleCurrentTabChanged()
{
/* Get current tree-widget: */
QITreeWidget *pTreeWidget = currentTreeWidget();
if (pTreeWidget)
{
/* If another tree-widget was focused before,
* move focus to current tree-widget: */
if (qobject_cast<QITreeWidget*>(focusWidget()))
pTreeWidget->setFocus();
}
/* Update action icons: */
updateActionIcons();
/* Raise the required information-container: */
if (m_pDetailsWidget)
m_pDetailsWidget->setCurrentType(currentMediumType());
/* Re-fetch currently chosen medium-item: */
refetchCurrentChosenMediumItem();
sltHandlePerformSearch();
}
void UIMediumManagerWidget::sltHandleCurrentItemChanged()
{
/* Get sender() tree-widget: */
QITreeWidget *pTreeWidget = qobject_cast<QITreeWidget*>(sender());
AssertMsgReturnVoid(pTreeWidget, ("This slot should be called by tree-widget only!\n"));
/* Re-fetch current medium-item of required type: */
refetchCurrentMediumItem(mediumType(pTreeWidget));
}
void UIMediumManagerWidget::sltHandleContextMenuRequest(const QPoint &position)
{
/* Get current tree-widget: */
QITreeWidget *pTreeWidget = currentTreeWidget();
AssertPtrReturnVoid(pTreeWidget);
/* If underlaying item was found => make sure that item is current one: */
QTreeWidgetItem *pItem = pTreeWidget->itemAt(position);
if (pItem)
setCurrentItem(pTreeWidget, pItem);
/* Compose temporary context-menu: */
QMenu menu;
if (pTreeWidget->itemAt(position))
{
menu.addAction(m_pActionPool->action(UIActionIndexST_M_Medium_S_Copy));
menu.addAction(m_pActionPool->action(UIActionIndexST_M_Medium_S_Move));
menu.addAction(m_pActionPool->action(UIActionIndexST_M_Medium_S_Remove));
menu.addAction(m_pActionPool->action(UIActionIndexST_M_Medium_S_Release));
menu.addAction(m_pActionPool->action(UIActionIndexST_M_Medium_T_Details));
menu.addAction(m_pActionPool->action(UIActionIndexST_M_Medium_T_Search));
}
else
{
menu.addAction(m_pActionPool->action(UIActionIndexST_M_Medium_S_Add));
menu.addAction(m_pActionPool->action(UIActionIndexST_M_Medium_T_Search));
menu.addAction(m_pActionPool->action(UIActionIndexST_M_Medium_S_Refresh));
}
/* And show it: */
menu.exec(pTreeWidget->viewport()->mapToGlobal(position));
}
void UIMediumManagerWidget::sltPerformTablesAdjustment()
{
/* Get all the tree-widgets: */
const QList<QITreeWidget*> trees = m_trees.values();
/* Calculate deduction for every header: */
QList<int> deductions;
foreach (QITreeWidget *pTreeWidget, trees)
{
int iDeduction = 0;
for (int iHeaderIndex = 1; iHeaderIndex < pTreeWidget->header()->count(); ++iHeaderIndex)
iDeduction += pTreeWidget->header()->sectionSize(iHeaderIndex);
deductions << iDeduction;
}
/* Adjust the table's first column: */
for (int iTreeIndex = 0; iTreeIndex < trees.size(); ++iTreeIndex)
{
QITreeWidget *pTreeWidget = trees[iTreeIndex];
int iSize0 = pTreeWidget->viewport()->width() - deductions[iTreeIndex];
if (pTreeWidget->header()->sectionSize(0) != iSize0)
pTreeWidget->header()->resizeSection(0, iSize0);
}
}
void UIMediumManagerWidget::sltHandlePerformSearch()
{
if (!m_pSearchWidget || !m_pTabWidget)
return;
QITreeWidget *pTreeWidget = treeWidget(static_cast<UIMediumDeviceType>(m_pTabWidget->currentIndex()));
if (!pTreeWidget)
return;
m_pSearchWidget->search(pTreeWidget);
}
void UIMediumManagerWidget::prepare()
{
/* Prepare connections: */
prepareConnections();
/* Prepare actions: */
prepareActions();
/* Prepare widgets: */
prepareWidgets();
/* Load settings: */
loadSettings();
/* Apply language settings: */
retranslateUi();
/* Start medium-enumeration (if necessary): */
if (!vboxGlobal().isMediumEnumerationInProgress())
vboxGlobal().startMediumEnumeration();
/* Emulate medium-enumeration otherwise: */
else
{
/* Start medium-enumeration: */
sltHandleMediumEnumerationStart();
/* Finish medium-enumeration (if necessary): */
if (!vboxGlobal().isMediumEnumerationInProgress())
sltHandleMediumEnumerationFinish();
}
}
void UIMediumManagerWidget::prepareConnections()
{
/* Configure medium-processing connections: */
connect(&vboxGlobal(), &VBoxGlobal::sigMediumCreated,
this, &UIMediumManagerWidget::sltHandleMediumCreated);
connect(&vboxGlobal(), &VBoxGlobal::sigMediumDeleted,
this, &UIMediumManagerWidget::sltHandleMediumDeleted);
/* Configure medium-enumeration connections: */
connect(&vboxGlobal(), &VBoxGlobal::sigMediumEnumerationStarted,
this, &UIMediumManagerWidget::sltHandleMediumEnumerationStart);
connect(&vboxGlobal(), &VBoxGlobal::sigMediumEnumerated,
this, &UIMediumManagerWidget::sltHandleMediumEnumerated);
connect(&vboxGlobal(), &VBoxGlobal::sigMediumEnumerationFinished,
this, &UIMediumManagerWidget::sltHandleMediumEnumerationFinish);
}
void UIMediumManagerWidget::prepareActions()
{
/* First of all, add actions which has smaller shortcut scope: */
addAction(m_pActionPool->action(UIActionIndexST_M_Medium_S_Add));
addAction(m_pActionPool->action(UIActionIndexST_M_Medium_S_Copy));
addAction(m_pActionPool->action(UIActionIndexST_M_Medium_S_Move));
addAction(m_pActionPool->action(UIActionIndexST_M_Medium_S_Remove));
addAction(m_pActionPool->action(UIActionIndexST_M_Medium_S_Release));
addAction(m_pActionPool->action(UIActionIndexST_M_Medium_T_Details));
addAction(m_pActionPool->action(UIActionIndexST_M_Medium_T_Search));
addAction(m_pActionPool->action(UIActionIndexST_M_Medium_S_Refresh));
/* Connect actions: */
connect(m_pActionPool->action(UIActionIndexST_M_Medium_S_Add), &QAction::triggered,
this, &UIMediumManagerWidget::sltAddMedium);
connect(m_pActionPool->action(UIActionIndexST_M_Medium_S_Copy), &QAction::triggered,
this, &UIMediumManagerWidget::sltCopyMedium);
connect(m_pActionPool->action(UIActionIndexST_M_Medium_S_Move), &QAction::triggered,
this, &UIMediumManagerWidget::sltMoveMedium);
connect(m_pActionPool->action(UIActionIndexST_M_Medium_S_Remove), &QAction::triggered,
this, &UIMediumManagerWidget::sltRemoveMedium);
connect(m_pActionPool->action(UIActionIndexST_M_Medium_S_Release), &QAction::triggered,
this, &UIMediumManagerWidget::sltReleaseMedium);
connect(m_pActionPool->action(UIActionIndexST_M_Medium_T_Details), &QAction::toggled,
this, &UIMediumManagerWidget::sltToggleMediumDetailsVisibility);
connect(m_pActionPool->action(UIActionIndexST_M_Medium_T_Search), &QAction::toggled,
this, &UIMediumManagerWidget::sltToggleMediumSearchVisibility);
connect(m_pActionPool->action(UIActionIndexST_M_Medium_S_Refresh), &QAction::triggered,
this, &UIMediumManagerWidget::sltRefreshAll);
/* Update action icons: */
updateActionIcons();
}
void UIMediumManagerWidget::prepareWidgets()
{
/* Create main-layout: */
new QVBoxLayout(this);
AssertPtrReturnVoid(layout());
{
/* Configure layout: */
layout()->setContentsMargins(0, 0, 0, 0);
#ifdef VBOX_WS_MAC
layout()->setSpacing(10);
#else
layout()->setSpacing(qApp->style()->pixelMetric(QStyle::PM_LayoutVerticalSpacing) / 2);
#endif
/* Prepare toolbar, if requested: */
if (m_fShowToolbar)
prepareToolBar();
/* Prepare tab-widget: */
prepareTabWidget();
/* Prepare details-widget: */
prepareDetailsWidget();
/* Prepare search-widget: */
prepareSearchWidget();
}
}
void UIMediumManagerWidget::prepareToolBar()
{
/* Create toolbar: */
m_pToolBar = new UIToolBar(parentWidget());
AssertPtrReturnVoid(m_pToolBar);
{
/* Configure toolbar: */
const int iIconMetric = (int)(QApplication::style()->pixelMetric(QStyle::PM_LargeIconSize));
m_pToolBar->setIconSize(QSize(iIconMetric, iIconMetric));
m_pToolBar->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
/* Add toolbar actions: */
m_pToolBar->addAction(m_pActionPool->action(UIActionIndexST_M_Medium_S_Add));
m_pToolBar->addSeparator();
m_pToolBar->addAction(m_pActionPool->action(UIActionIndexST_M_Medium_S_Copy));
m_pToolBar->addAction(m_pActionPool->action(UIActionIndexST_M_Medium_S_Move));
m_pToolBar->addAction(m_pActionPool->action(UIActionIndexST_M_Medium_S_Remove));
m_pToolBar->addAction(m_pActionPool->action(UIActionIndexST_M_Medium_S_Release));
m_pToolBar->addAction(m_pActionPool->action(UIActionIndexST_M_Medium_T_Details));
m_pToolBar->addAction(m_pActionPool->action(UIActionIndexST_M_Medium_T_Search));
m_pToolBar->addSeparator();
m_pToolBar->addAction(m_pActionPool->action(UIActionIndexST_M_Medium_S_Refresh));
#ifdef VBOX_WS_MAC
/* Check whether we are embedded into a stack: */
if (m_enmEmbedding == EmbedTo_Stack)
{
/* Add into layout: */
layout()->addWidget(m_pToolBar);
}
#else
/* Add into layout: */
layout()->addWidget(m_pToolBar);
#endif
}
}
void UIMediumManagerWidget::prepareTabWidget()
{
/* Create tab-widget: */
m_pTabWidget = new QITabWidget;
AssertPtrReturnVoid(m_pTabWidget);
{
/* Create tabs: */
for (int i = 0; i < m_iTabCount; ++i)
prepareTab((UIMediumDeviceType)i);
/* Configure tab-widget: */
m_pTabWidget->setFocusPolicy(Qt::TabFocus);
m_pTabWidget->setTabIcon(tabIndex(UIMediumDeviceType_HardDisk), m_iconHD);
m_pTabWidget->setTabIcon(tabIndex(UIMediumDeviceType_DVD), m_iconCD);
m_pTabWidget->setTabIcon(tabIndex(UIMediumDeviceType_Floppy), m_iconFD);
connect(m_pTabWidget, &QITabWidget::currentChanged, this, &UIMediumManagerWidget::sltHandleCurrentTabChanged);
/* Add tab-widget into central layout: */
layout()->addWidget(m_pTabWidget);
/* Update other widgets according chosen tab: */
sltHandleCurrentTabChanged();
}
}
void UIMediumManagerWidget::prepareTab(UIMediumDeviceType type)
{
/* Create tab: */
m_pTabWidget->addTab(new QWidget, QString());
QWidget *pTab = tab(type);
AssertPtrReturnVoid(pTab);
{
/* Create tab layout: */
QVBoxLayout *pLayout = new QVBoxLayout(pTab);
AssertPtrReturnVoid(pLayout);
{
#ifdef VBOX_WS_MAC
/* Configure layout: */
pLayout->setContentsMargins(10, 10, 10, 10);
#endif
/* Prepare tree-widget: */
prepareTreeWidget(type, type == UIMediumDeviceType_HardDisk ? 3 : 2);
}
}
}
void UIMediumManagerWidget::prepareTreeWidget(UIMediumDeviceType type, int iColumns)
{
/* Create tree-widget: */
m_trees.insert(tabIndex(type), new QITreeWidget);
QITreeWidget *pTreeWidget = treeWidget(type);
AssertPtrReturnVoid(pTreeWidget);
{
/* Configure tree-widget: */
pTreeWidget->setExpandsOnDoubleClick(false);
pTreeWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
pTreeWidget->setContextMenuPolicy(Qt::CustomContextMenu);
pTreeWidget->setAlternatingRowColors(true);
pTreeWidget->setAllColumnsShowFocus(true);
pTreeWidget->setAcceptDrops(true);
pTreeWidget->setColumnCount(iColumns);
pTreeWidget->sortItems(0, Qt::AscendingOrder);
if (iColumns > 0)
pTreeWidget->header()->setSectionResizeMode(0, QHeaderView::Fixed);
if (iColumns > 1)
pTreeWidget->header()->setSectionResizeMode(1, QHeaderView::ResizeToContents);
if (iColumns > 2)
pTreeWidget->header()->setSectionResizeMode(2, QHeaderView::ResizeToContents);
pTreeWidget->header()->setStretchLastSection(false);
pTreeWidget->setSortingEnabled(true);
connect(pTreeWidget, &QITreeWidget::currentItemChanged,
this, &UIMediumManagerWidget::sltHandleCurrentItemChanged);
connect(pTreeWidget, &QITreeWidget::itemDoubleClicked,
m_pActionPool->action(UIActionIndexST_M_Medium_T_Details), &QAction::setChecked);
connect(pTreeWidget, &QITreeWidget::customContextMenuRequested,
this, &UIMediumManagerWidget::sltHandleContextMenuRequest);
connect(pTreeWidget, &QITreeWidget::resized,
this, &UIMediumManagerWidget::sltPerformTablesAdjustment, Qt::QueuedConnection);
connect(pTreeWidget->header(), &QHeaderView::sectionResized,
this, &UIMediumManagerWidget::sltPerformTablesAdjustment, Qt::QueuedConnection);
/* Add tree-widget into tab layout: */
tab(type)->layout()->addWidget(pTreeWidget);
}
}
void UIMediumManagerWidget::prepareDetailsWidget()
{
/* Create details-widget: */
m_pDetailsWidget = new UIMediumDetailsWidget(this, m_enmEmbedding);
AssertPtrReturnVoid(m_pDetailsWidget);
{
/* Configure details-widget: */
m_pDetailsWidget->setVisible(false);
m_pDetailsWidget->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);
connect(m_pDetailsWidget, &UIMediumDetailsWidget::sigAcceptAllowed,
this, &UIMediumManagerWidget::sigAcceptAllowed);
connect(m_pDetailsWidget, &UIMediumDetailsWidget::sigRejectAllowed,
this, &UIMediumManagerWidget::sigRejectAllowed);
connect(m_pDetailsWidget, &UIMediumDetailsWidget::sigDataChangeRejected,
this, &UIMediumManagerWidget::sltResetMediumDetailsChanges);
connect(m_pDetailsWidget, &UIMediumDetailsWidget::sigDataChangeAccepted,
this, &UIMediumManagerWidget::sltApplyMediumDetailsChanges);
/* Add into layout: */
layout()->addWidget(m_pDetailsWidget);
}
}
void UIMediumManagerWidget::prepareSearchWidget()
{
m_pSearchWidget = new UIMediumSearchWidget(this);
AssertPtrReturnVoid(m_pSearchWidget);
{
m_pSearchWidget->setVisible(false);
m_pSearchWidget->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);
connect(m_pSearchWidget, &UIMediumSearchWidget::sigPerformSearch,
this, &UIMediumManagerWidget::sltHandlePerformSearch);
/* Add into layout: */
layout()->addWidget(m_pSearchWidget);
}
}
void UIMediumManagerWidget::loadSettings()
{
/* Details action/widget: */
m_pActionPool->action(UIActionIndexST_M_Medium_T_Details)->setChecked(gEDataManager->virtualMediaManagerDetailsExpanded());
sltToggleMediumDetailsVisibility(m_pActionPool->action(UIActionIndexST_M_Medium_T_Details)->isChecked());
/* Search action/widget: */
m_pActionPool->action(UIActionIndexST_M_Medium_T_Search)->setChecked(gEDataManager->virtualMediaManagerSearchWidgetExpanded());
sltToggleMediumSearchVisibility(m_pActionPool->action(UIActionIndexST_M_Medium_T_Search)->isChecked());
}
void UIMediumManagerWidget::repopulateTreeWidgets()
{
/* Remember current medium-items: */
if (UIMediumItem *pMediumItem = mediumItem(UIMediumDeviceType_HardDisk))
m_uCurrentIdHD = pMediumItem->id();
if (UIMediumItem *pMediumItem = mediumItem(UIMediumDeviceType_DVD))
m_uCurrentIdCD = pMediumItem->id();
if (UIMediumItem *pMediumItem = mediumItem(UIMediumDeviceType_Floppy))
m_uCurrentIdFD = pMediumItem->id();
/* Clear tree-widgets: */
QITreeWidget *pTreeWidgetHD = treeWidget(UIMediumDeviceType_HardDisk);
if (pTreeWidgetHD)
{
setCurrentItem(pTreeWidgetHD, 0);
pTreeWidgetHD->clear();
}
QITreeWidget *pTreeWidgetCD = treeWidget(UIMediumDeviceType_DVD);
if (pTreeWidgetCD)
{
setCurrentItem(pTreeWidgetCD, 0);
pTreeWidgetCD->clear();
}
QITreeWidget *pTreeWidgetFD = treeWidget(UIMediumDeviceType_Floppy);
if (pTreeWidgetFD)
{
setCurrentItem(pTreeWidgetFD, 0);
pTreeWidgetFD->clear();
}
/* Create medium-items (do not change current one): */
m_fPreventChangeCurrentItem = true;
foreach (const QUuid &uMediumID, vboxGlobal().mediumIDs())
sltHandleMediumCreated(uMediumID);
m_fPreventChangeCurrentItem = false;
/* Select first item as current one if nothing selected: */
if (pTreeWidgetHD && !mediumItem(UIMediumDeviceType_HardDisk))
if (QTreeWidgetItem *pItem = pTreeWidgetHD->topLevelItem(0))
setCurrentItem(pTreeWidgetHD, pItem);
if (pTreeWidgetCD && !mediumItem(UIMediumDeviceType_DVD))
if (QTreeWidgetItem *pItem = pTreeWidgetCD->topLevelItem(0))
setCurrentItem(pTreeWidgetCD, pItem);
if (pTreeWidgetFD && !mediumItem(UIMediumDeviceType_Floppy))
if (QTreeWidgetItem *pItem = pTreeWidgetFD->topLevelItem(0))
setCurrentItem(pTreeWidgetFD, pItem);
sltHandlePerformSearch();
}
void UIMediumManagerWidget::refetchCurrentMediumItem(UIMediumDeviceType type)
{
/* Get corresponding medium-item: */
UIMediumItem *pMediumItem = mediumItem(type);
#ifdef VBOX_WS_MAC
/* Set the file for the proxy icon: */
if (pMediumItem == currentMediumItem())
setWindowFilePath(pMediumItem ? pMediumItem->location() : QString());
#endif /* VBOX_WS_MAC */
/* Make sure current medium-item visible: */
if (pMediumItem)
treeWidget(type)->scrollToItem(pMediumItem, QAbstractItemView::EnsureVisible);
/* Update actions: */
updateActions();
/* Update details-widget: */
if (m_pDetailsWidget)
m_pDetailsWidget->setData(pMediumItem ? *pMediumItem : UIDataMedium(type));
}
void UIMediumManagerWidget::refetchCurrentChosenMediumItem()
{
refetchCurrentMediumItem(currentMediumType());
}
void UIMediumManagerWidget::refetchCurrentMediumItems()
{
refetchCurrentMediumItem(UIMediumDeviceType_HardDisk);
refetchCurrentMediumItem(UIMediumDeviceType_DVD);
refetchCurrentMediumItem(UIMediumDeviceType_Floppy);
}
void UIMediumManagerWidget::updateActions()
{
/* Get current medium-item: */
UIMediumItem *pMediumItem = currentMediumItem();
/* Calculate actions accessibility: */
bool fNotInEnumeration = !vboxGlobal().isMediumEnumerationInProgress();
/* Apply actions accessibility: */
bool fActionEnabledCopy = fNotInEnumeration && pMediumItem && checkMediumFor(pMediumItem, Action_Copy);
m_pActionPool->action(UIActionIndexST_M_Medium_S_Copy)->setEnabled(fActionEnabledCopy);
bool fActionEnabledMove = fNotInEnumeration && pMediumItem && checkMediumFor(pMediumItem, Action_Edit);
m_pActionPool->action(UIActionIndexST_M_Medium_S_Move)->setEnabled(fActionEnabledMove);
bool fActionEnabledRemove = fNotInEnumeration && pMediumItem && checkMediumFor(pMediumItem, Action_Remove);
m_pActionPool->action(UIActionIndexST_M_Medium_S_Remove)->setEnabled(fActionEnabledRemove);
bool fActionEnabledRelease = fNotInEnumeration && pMediumItem && checkMediumFor(pMediumItem, Action_Release);
m_pActionPool->action(UIActionIndexST_M_Medium_S_Release)->setEnabled(fActionEnabledRelease);
bool fActionEnabledDetails = true;
m_pActionPool->action(UIActionIndexST_M_Medium_T_Details)->setEnabled(fActionEnabledDetails);
}
void UIMediumManagerWidget::updateActionIcons()
{
const UIMediumDeviceType enmCurrentMediumType = currentMediumType();
if (enmCurrentMediumType != UIMediumDeviceType_Invalid)
{
m_pActionPool->action(UIActionIndexST_M_Medium_S_Add)->setState((int)enmCurrentMediumType);
m_pActionPool->action(UIActionIndexST_M_Medium_S_Copy)->setState((int)enmCurrentMediumType);
m_pActionPool->action(UIActionIndexST_M_Medium_S_Move)->setState((int)enmCurrentMediumType);
m_pActionPool->action(UIActionIndexST_M_Medium_S_Remove)->setState((int)enmCurrentMediumType);
m_pActionPool->action(UIActionIndexST_M_Medium_S_Release)->setState((int)enmCurrentMediumType);
m_pActionPool->action(UIActionIndexST_M_Medium_T_Details)->setState((int)enmCurrentMediumType);
}
}
void UIMediumManagerWidget::updateTabIcons(UIMediumItem *pMediumItem, Action action)
{
/* Make sure medium-item is valid: */
AssertReturnVoid(pMediumItem);
/* Prepare data for tab: */
const QIcon *pIcon = 0;
bool *pfInaccessible = 0;
const UIMediumDeviceType mediumType = pMediumItem->mediumType();
switch (mediumType)
{
case UIMediumDeviceType_HardDisk:
pIcon = &m_iconHD;
pfInaccessible = &m_fInaccessibleHD;
break;
case UIMediumDeviceType_DVD:
pIcon = &m_iconCD;
pfInaccessible = &m_fInaccessibleCD;
break;
case UIMediumDeviceType_Floppy:
pIcon = &m_iconFD;
pfInaccessible = &m_fInaccessibleFD;
break;
default:
AssertFailed();
}
AssertReturnVoid(pIcon && pfInaccessible);
switch (action)
{
case Action_Add:
{
/* Does it change the overall state? */
if (*pfInaccessible || pMediumItem->state() != KMediumState_Inaccessible)
break; /* no */
*pfInaccessible = true;
if (m_pTabWidget)
m_pTabWidget->setTabIcon(tabIndex(mediumType), vboxGlobal().warningIcon());
break;
}
case Action_Edit:
case Action_Remove:
{
bool fCheckRest = false;
if (action == Action_Edit)
{
/* Does it change the overall state? */
if ((*pfInaccessible && pMediumItem->state() == KMediumState_Inaccessible) ||
(!*pfInaccessible && pMediumItem->state() != KMediumState_Inaccessible))
break; /* no */
/* Is the given item in charge? */
if (!*pfInaccessible && pMediumItem->state() == KMediumState_Inaccessible)
*pfInaccessible = true; /* yes */
else
fCheckRest = true; /* no */
}
else
fCheckRest = true;
if (fCheckRest)
{
/* Find the first KMediumState_Inaccessible item to be in charge: */
CheckIfSuitableByState lookForState(KMediumState_Inaccessible);
CheckIfSuitableByID ignoreID(pMediumItem->id());
UIMediumItem *pInaccessibleMediumItem = searchItem(pMediumItem->parentTree(), lookForState, &ignoreID);
*pfInaccessible = !!pInaccessibleMediumItem;
}
if (m_pTabWidget)
{
if (*pfInaccessible)
m_pTabWidget->setTabIcon(tabIndex(mediumType), vboxGlobal().warningIcon());
else
m_pTabWidget->setTabIcon(tabIndex(mediumType), *pIcon);
}
break;
}
default:
break;
}
}
UIMediumItem* UIMediumManagerWidget::createMediumItem(const UIMedium &medium)
{
/* Get medium type: */
UIMediumDeviceType type = medium.type();
/* Create medium-item: */
UIMediumItem *pMediumItem = 0;
switch (type)
{
/* Of hard-drive type: */
case UIMediumDeviceType_HardDisk:
{
/* Make sure corresponding tree-widget exists: */
QITreeWidget *pTreeWidget = treeWidget(UIMediumDeviceType_HardDisk);
if (pTreeWidget)
{
/* Recursively create hard-drive item: */
pMediumItem = createHardDiskItem(medium);
/* Make sure item was created: */
if (!pMediumItem)
break;
if (pMediumItem->id() == m_uCurrentIdHD)
{
setCurrentItem(pTreeWidget, pMediumItem);
m_uCurrentIdHD = QUuid();
}
}
break;
}
/* Of optical-image type: */
case UIMediumDeviceType_DVD:
{
/* Make sure corresponding tree-widget exists: */
QITreeWidget *pTreeWidget = treeWidget(UIMediumDeviceType_DVD);
if (pTreeWidget)
{
/* Create optical-disk item: */
pMediumItem = new UIMediumItemCD(medium, pTreeWidget);
/* Make sure item was created: */
if (!pMediumItem)
break;
LogRel2(("UIMediumManager: Optical medium-item with ID={%s} created.\n", medium.id().toString().toUtf8().constData()));
if (pMediumItem->id() == m_uCurrentIdCD)
{
setCurrentItem(pTreeWidget, pMediumItem);
m_uCurrentIdCD = QUuid();
}
}
break;
}
/* Of floppy-image type: */
case UIMediumDeviceType_Floppy:
{
/* Make sure corresponding tree-widget exists: */
QITreeWidget *pTreeWidget = treeWidget(UIMediumDeviceType_Floppy);
if (pTreeWidget)
{
/* Create floppy-disk item: */
pMediumItem = new UIMediumItemFD(medium, pTreeWidget);
/* Make sure item was created: */
if (!pMediumItem)
break;
LogRel2(("UIMediumManager: Floppy medium-item with ID={%s} created.\n", medium.id().toString().toUtf8().constData()));
if (pMediumItem->id() == m_uCurrentIdFD)
{
setCurrentItem(pTreeWidget, pMediumItem);
m_uCurrentIdFD = QUuid();
}
}
break;
}
default: AssertMsgFailed(("Medium-type unknown: %d\n", type)); break;
}
/* Make sure item was created: */
if (!pMediumItem)
return 0;
/* Update tab-icons: */
updateTabIcons(pMediumItem, Action_Add);
/* Re-fetch medium-item if it is current one created: */
if (pMediumItem == mediumItem(type))
refetchCurrentMediumItem(type);
/* Return created medium-item: */
return pMediumItem;
}
UIMediumItem* UIMediumManagerWidget::createHardDiskItem(const UIMedium &medium)
{
/* Make sure passed medium is valid: */
AssertReturn(!medium.medium().isNull(), 0);
/* Make sure corresponding tree-widget exists: */
QITreeWidget *pTreeWidget = treeWidget(UIMediumDeviceType_HardDisk);
if (pTreeWidget)
{
/* Search for existing medium-item: */
UIMediumItem *pMediumItem = searchItem(pTreeWidget, CheckIfSuitableByID(medium.id()));
/* If medium-item do not exists: */
if (!pMediumItem)
{
/* If medium have a parent: */
if (medium.parentID() != UIMedium::nullID())
{
/* Try to find parent medium-item: */
UIMediumItem *pParentMediumItem = searchItem(pTreeWidget, CheckIfSuitableByID(medium.parentID()));
/* If parent medium-item was not found: */
if (!pParentMediumItem)
{
/* Make sure corresponding parent medium is already cached! */
UIMedium parentMedium = vboxGlobal().medium(medium.parentID());
if (parentMedium.isNull())
AssertMsgFailed(("Parent medium with ID={%s} was not found!\n", medium.parentID().toString().toUtf8().constData()));
/* Try to create parent medium-item: */
else
pParentMediumItem = createHardDiskItem(parentMedium);
}
/* If parent medium-item was found: */
if (pParentMediumItem)
{
pMediumItem = new UIMediumItemHD(medium, pParentMediumItem);
LogRel2(("UIMediumManager: Child hard-disk medium-item with ID={%s} created.\n", medium.id().toString().toUtf8().constData()));
}
}
/* Else just create item as top-level one: */
if (!pMediumItem)
{
pMediumItem = new UIMediumItemHD(medium, pTreeWidget);
LogRel2(("UIMediumManager: Root hard-disk medium-item with ID={%s} created.\n", medium.id().toString().toUtf8().constData()));
}
}
/* Return created medium-item: */
return pMediumItem;
}
/* Return null by default: */
return 0;
}
void UIMediumManagerWidget::updateMediumItem(const UIMedium &medium)
{
/* Get medium type: */
UIMediumDeviceType type = medium.type();
/* Search for existing medium-item: */
UIMediumItem *pMediumItem = searchItem(treeWidget(type), CheckIfSuitableByID(medium.id()));
/* Create item if doesn't exists: */
if (!pMediumItem)
pMediumItem = createMediumItem(medium);
/* Make sure item was created: */
if (!pMediumItem)
return;
/* Update medium-item: */
pMediumItem->setMedium(medium);
LogRel2(("UIMediumManager: Medium-item with ID={%s} updated.\n", medium.id().toString().toUtf8().constData()));
/* Update tab-icons: */
updateTabIcons(pMediumItem, Action_Edit);
/* Re-fetch medium-item if it is current one updated: */
if (pMediumItem == mediumItem(type))
refetchCurrentMediumItem(type);
}
void UIMediumManagerWidget::deleteMediumItem(const QUuid &uMediumID)
{
/* Search for corresponding tree-widget: */
QList<UIMediumDeviceType> types;
types << UIMediumDeviceType_HardDisk << UIMediumDeviceType_DVD << UIMediumDeviceType_Floppy;
QITreeWidget *pTreeWidget = 0;
UIMediumItem *pMediumItem = 0;
foreach (UIMediumDeviceType type, types)
{
/* Get iterated tree-widget: */
pTreeWidget = treeWidget(type);
/* Search for existing medium-item: */
pMediumItem = searchItem(pTreeWidget, CheckIfSuitableByID(uMediumID));
if (pMediumItem)
break;
}
/* Make sure item was found: */
if (!pMediumItem)
return;
/* Update tab-icons: */
updateTabIcons(pMediumItem, Action_Remove);
/* Delete medium-item: */
delete pMediumItem;
LogRel2(("UIMediumManager: Medium-item with ID={%s} deleted.\n", uMediumID.toString().toUtf8().constData()));
/* If there is no current medium-item now selected
* we have to choose first-available medium-item as current one: */
if (!pTreeWidget->currentItem())
setCurrentItem(pTreeWidget, pTreeWidget->topLevelItem(0));
}
QWidget* UIMediumManagerWidget::tab(UIMediumDeviceType type) const
{
/* Determine tab index for passed medium type: */
int iIndex = tabIndex(type);
/* Return tab for known tab index: */
if (iIndex >= 0 && iIndex < m_iTabCount)
return iIndex < m_pTabWidget->count() ? m_pTabWidget->widget(iIndex) : 0;
/* Null by default: */
return 0;
}
QITreeWidget* UIMediumManagerWidget::treeWidget(UIMediumDeviceType type) const
{
/* Determine tab index for passed medium type: */
int iIndex = tabIndex(type);
/* Return tree-widget for known tab index: */
if (iIndex >= 0 && iIndex < m_iTabCount)
return m_trees.value(iIndex, 0);
/* Null by default: */
return 0;
}
UIMediumItem* UIMediumManagerWidget::mediumItem(UIMediumDeviceType type) const
{
/* Get corresponding tree-widget: */
QITreeWidget *pTreeWidget = treeWidget(type);
/* Return corresponding medium-item: */
return pTreeWidget ? toMediumItem(pTreeWidget->currentItem()) : 0;
}
UIMediumDeviceType UIMediumManagerWidget::mediumType(QITreeWidget *pTreeWidget) const
{
/* Determine tab index of passed tree-widget: */
int iIndex = m_trees.key(pTreeWidget, -1);
/* Return medium type for known tab index: */
if (iIndex >= 0 && iIndex < m_iTabCount)
return (UIMediumDeviceType)iIndex;
/* Invalid by default: */
AssertFailedReturn(UIMediumDeviceType_Invalid);
}
UIMediumDeviceType UIMediumManagerWidget::currentMediumType() const
{
/* Invalid if tab-widget doesn't exists: */
if (!m_pTabWidget)
return UIMediumDeviceType_Invalid;
/* Return current medium type: */
return (UIMediumDeviceType)m_pTabWidget->currentIndex();
}
QITreeWidget* UIMediumManagerWidget::currentTreeWidget() const
{
/* Return current tree-widget: */
return treeWidget(currentMediumType());
}
UIMediumItem* UIMediumManagerWidget::currentMediumItem() const
{
/* Return current medium-item: */
return mediumItem(currentMediumType());
}
void UIMediumManagerWidget::setCurrentItem(QITreeWidget *pTreeWidget, QTreeWidgetItem *pItem)
{
/* Make sure passed tree-widget is valid: */
AssertPtrReturnVoid(pTreeWidget);
/* Make passed item current for passed tree-widget: */
pTreeWidget->setCurrentItem(pItem);
/* If non NULL item was passed: */
if (pItem)
{
/* Make sure it's also selected, and visible: */
pItem->setSelected(true);
pTreeWidget->scrollToItem(pItem, QAbstractItemView::EnsureVisible);
}
/* Re-fetch currently chosen medium-item: */
refetchCurrentChosenMediumItem();
}
/* static */
int UIMediumManagerWidget::tabIndex(UIMediumDeviceType type)
{
/* Return tab index corresponding to known medium type: */
switch (type)
{
case UIMediumDeviceType_HardDisk: return 0;
case UIMediumDeviceType_DVD: return 1;
case UIMediumDeviceType_Floppy: return 2;
default: break;
}
/* -1 by default: */
return -1;
}
/* static */
UIMediumItem* UIMediumManagerWidget::searchItem(QITreeWidget *pTreeWidget, const CheckIfSuitableBy &condition, CheckIfSuitableBy *pException)
{
/* Make sure argument is valid: */
if (!pTreeWidget)
return 0;
/* Return wrapper: */
return searchItem(pTreeWidget->invisibleRootItem(), condition, pException);
}
/* static */
UIMediumItem* UIMediumManagerWidget::searchItem(QTreeWidgetItem *pParentItem, const CheckIfSuitableBy &condition, CheckIfSuitableBy *pException)
{
/* Make sure argument is valid: */
if (!pParentItem)
return 0;
/* Verify passed item if it is of 'medium' type too: */
if (UIMediumItem *pMediumParentItem = toMediumItem(pParentItem))
if ( condition.isItSuitable(pMediumParentItem)
&& (!pException || !pException->isItSuitable(pMediumParentItem)))
return pMediumParentItem;
/* Iterate other all the children: */
for (int iChildIndex = 0; iChildIndex < pParentItem->childCount(); ++iChildIndex)
if (UIMediumItem *pMediumChildItem = toMediumItem(pParentItem->child(iChildIndex)))
if (UIMediumItem *pRequiredMediumChildItem = searchItem(pMediumChildItem, condition, pException))
return pRequiredMediumChildItem;
/* Null by default: */
return 0;
}
/* static */
bool UIMediumManagerWidget::checkMediumFor(UIMediumItem *pItem, Action action)
{
/* Make sure passed ID is valid: */
AssertReturn(pItem, false);
switch (action)
{
case Action_Edit:
{
/* Edit means changing the description and alike; any media that is
* not being read to or written from can be altered in these terms. */
switch (pItem->state())
{
case KMediumState_NotCreated:
case KMediumState_Inaccessible:
case KMediumState_LockedRead:
case KMediumState_LockedWrite:
return false;
default:
break;
}
return true;
}
case Action_Copy:
{
/* False for children: */
return pItem->medium().parentID() == UIMedium::nullID();
}
case Action_Remove:
{
/* Removable if not attached to anything: */
return !pItem->isUsed();
}
case Action_Release:
{
/* Releasable if attached but not in snapshots: */
return pItem->isUsed() && !pItem->isUsedInSnapshots();
}
default:
break;
}
AssertFailedReturn(false);
}
/* static */
UIMediumItem* UIMediumManagerWidget::toMediumItem(QTreeWidgetItem *pItem)
{
/* Cast passed QTreeWidgetItem to UIMediumItem if possible: */
return pItem && pItem->type() == QITreeWidgetItem::ItemType ? static_cast<UIMediumItem*>(pItem) : 0;
}
/*********************************************************************************************************************************
* Class UIMediumManagerFactory implementation. *
*********************************************************************************************************************************/
UIMediumManagerFactory::UIMediumManagerFactory(UIActionPool *pActionPool /* = 0 */)
: m_pActionPool(pActionPool)
{
}
void UIMediumManagerFactory::create(QIManagerDialog *&pDialog, QWidget *pCenterWidget)
{
pDialog = new UIMediumManager(pCenterWidget, m_pActionPool);
}
/*********************************************************************************************************************************
* Class UIMediumManagerFactory implementation. *
*********************************************************************************************************************************/
UIMediumManager::UIMediumManager(QWidget *pCenterWidget, UIActionPool *pActionPool)
: QIWithRetranslateUI<QIManagerDialog>(pCenterWidget)
, m_pActionPool(pActionPool)
, m_pProgressBar(0)
{
}
void UIMediumManager::sltHandleButtonBoxClick(QAbstractButton *pButton)
{
/* Disable buttons first of all: */
button(ButtonType_Reset)->setEnabled(false);
button(ButtonType_Apply)->setEnabled(false);
/* Compare with known buttons: */
if (pButton == button(ButtonType_Reset))
emit sigDataChangeRejected();
else
if (pButton == button(ButtonType_Apply))
emit sigDataChangeAccepted();
}
void UIMediumManager::retranslateUi()
{
/* Translate window title: */
setWindowTitle(tr("Virtual Media Manager"));
/* Translate buttons: */
button(ButtonType_Reset)->setText(tr("Reset"));
button(ButtonType_Apply)->setText(tr("Apply"));
button(ButtonType_Close)->setText(tr("Close"));
button(ButtonType_Reset)->setStatusTip(tr("Reset changes in current medium details"));
button(ButtonType_Apply)->setStatusTip(tr("Apply changes in current medium details"));
button(ButtonType_Close)->setStatusTip(tr("Close dialog without saving"));
button(ButtonType_Reset)->setShortcut(QString("Ctrl+Backspace"));
button(ButtonType_Apply)->setShortcut(QString("Ctrl+Return"));
button(ButtonType_Close)->setShortcut(Qt::Key_Escape);
button(ButtonType_Reset)->setToolTip(tr("Reset Changes (%1)").arg(button(ButtonType_Reset)->shortcut().toString()));
button(ButtonType_Apply)->setToolTip(tr("Apply Changes (%1)").arg(button(ButtonType_Apply)->shortcut().toString()));
button(ButtonType_Close)->setToolTip(tr("Close Window (%1)").arg(button(ButtonType_Close)->shortcut().toString()));
}
void UIMediumManager::configure()
{
/* Apply window icons: */
setWindowIcon(UIIconPool::iconSetFull(":/media_manager_32px.png", ":/media_manager_16px.png"));
}
void UIMediumManager::configureCentralWidget()
{
/* Create widget: */
UIMediumManagerWidget *pWidget = new UIMediumManagerWidget(EmbedTo_Dialog, m_pActionPool, true, this);
AssertPtrReturnVoid(pWidget);
{
/* Configure widget: */
setWidget(pWidget);
setWidgetMenu(pWidget->menu());
#ifdef VBOX_WS_MAC
setWidgetToolbar(pWidget->toolbar());
#endif
connect(this, &UIMediumManager::sigDataChangeRejected,
pWidget, &UIMediumManagerWidget::sltResetMediumDetailsChanges);
connect(this, &UIMediumManager::sigDataChangeAccepted,
pWidget, &UIMediumManagerWidget::sltApplyMediumDetailsChanges);
/* Add into layout: */
centralWidget()->layout()->addWidget(pWidget);
}
}
void UIMediumManager::configureButtonBox()
{
/* Configure button-box: */
connect(widget(), &UIMediumManagerWidget::sigMediumDetailsVisibilityChanged,
button(ButtonType_Apply), &QPushButton::setVisible);
connect(widget(), &UIMediumManagerWidget::sigMediumDetailsVisibilityChanged,
button(ButtonType_Reset), &QPushButton::setVisible);
connect(widget(), &UIMediumManagerWidget::sigAcceptAllowed,
button(ButtonType_Apply), &QPushButton::setEnabled);
connect(widget(), &UIMediumManagerWidget::sigRejectAllowed,
button(ButtonType_Reset), &QPushButton::setEnabled);
connect(buttonBox(), &QIDialogButtonBox::clicked,
this, &UIMediumManager::sltHandleButtonBoxClick);
// WORKAROUND:
// Since we connected signals later than extra-data loaded
// for signals above, we should handle that stuff here again:
button(ButtonType_Apply)->setVisible(gEDataManager->virtualMediaManagerDetailsExpanded());
button(ButtonType_Reset)->setVisible(gEDataManager->virtualMediaManagerDetailsExpanded());
/* Create progress-bar: */
m_pProgressBar = new UIEnumerationProgressBar;
AssertPtrReturnVoid(m_pProgressBar);
{
/* Configure progress-bar: */
m_pProgressBar->hide();
/* Add progress-bar into button-box layout: */
buttonBox()->addExtraWidget(m_pProgressBar);
/* Notify widget it has progress-bar: */
widget()->setProgressBar(m_pProgressBar);
}
}
void UIMediumManager::finalize()
{
/* Apply language settings: */
retranslateUi();
}
UIMediumManagerWidget *UIMediumManager::widget()
{
return qobject_cast<UIMediumManagerWidget*>(QIManagerDialog::widget());
}
|