VirtualBox

source: vbox/trunk/src/VBox/Frontends/VirtualBox/src/guestctrl/UIGuestControlFileTable.cpp@ 75673

Last change on this file since 75673 was 75673, checked in by vboxsync, 6 years ago

FE/Qt: bugref:6699. Show some warning text in guest file pane when a guest session is not avaible

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 49.1 KB
Line 
1/* $Id: UIGuestControlFileTable.cpp 75673 2018-11-22 15:56:42Z vboxsync $ */
2/** @file
3 * VBox Qt GUI - UIGuestControlFileTable class implementation.
4 */
5
6/*
7 * Copyright (C) 2016-2018 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18#ifdef VBOX_WITH_PRECOMPILED_HEADERS
19# include <precomp.h>
20#else /* !VBOX_WITH_PRECOMPILED_HEADERS */
21
22/* Qt includes: */
23# include <QAction>
24# include <QComboBox>
25# include <QCheckBox>
26# include <QDateTime>
27# include <QDir>
28# include <QHeaderView>
29# include <QItemDelegate>
30# include <QGridLayout>
31# include <QMenu>
32# include <QSortFilterProxyModel>
33# include <QTextEdit>
34# include <QPushButton>
35
36/* GUI includes: */
37# include "QIDialog.h"
38# include "QIDialogButtonBox.h"
39# include "QILabel.h"
40# include "QILineEdit.h"
41# include "QIMessageBox.h"
42# include "UIActionPool.h"
43# include "UIErrorString.h"
44# include "UIGuestFileTable.h"
45# include "UIIconPool.h"
46# include "UIGuestControlFileTable.h"
47# include "UIGuestControlFileManager.h"
48# include "UIGuestControlFileModel.h"
49# include "UIToolBar.h"
50
51/* COM includes: */
52# include "CFsObjInfo.h"
53# include "CGuestFsObjInfo.h"
54# include "CGuestDirectory.h"
55# include "CProgress.h"
56
57#endif /* !VBOX_WITH_PRECOMPILED_HEADERS */
58
59
60
61/*********************************************************************************************************************************
62* UIGuestControlFileView definition. *
63*********************************************************************************************************************************/
64
65/** Using QITableView causes the following problem when I click on the table items
66 Qt WARNING: Cannot creat accessible child interface for object: UIGuestControlFileView.....
67 so for now subclass QTableView */
68class UIGuestControlFileView : public QTableView
69{
70
71 Q_OBJECT;
72
73signals:
74
75 void sigSelectionChanged(const QItemSelection & selected, const QItemSelection & deselected);
76
77public:
78
79 UIGuestControlFileView(QWidget * parent = 0);
80 bool hasSelection() const;
81
82protected:
83
84 virtual void selectionChanged(const QItemSelection & selected, const QItemSelection & deselected) /*override */;
85
86private:
87
88 void configure();
89 QWidget *m_pParent;
90};
91
92
93/*********************************************************************************************************************************
94* UIFileDelegate definition. *
95*********************************************************************************************************************************/
96/** A QItemDelegate child class to disable dashed lines drawn around selected cells in QTableViews */
97class UIFileDelegate : public QItemDelegate
98{
99
100 Q_OBJECT;
101
102protected:
103
104 virtual void drawFocus ( QPainter * /*painter*/, const QStyleOptionViewItem & /*option*/, const QRect & /*rect*/ ) const {}
105
106};
107
108
109/*********************************************************************************************************************************
110* UStringInputDialog definition. *
111*********************************************************************************************************************************/
112
113/** A QIDialog child including a line edit whose text exposed when the dialog is accepted */
114class UIStringInputDialog : public QIDialog
115{
116
117 Q_OBJECT;
118
119public:
120
121 UIStringInputDialog(QWidget *pParent = 0, Qt::WindowFlags flags = 0);
122 QString getString() const;
123
124private:
125
126 QILineEdit *m_pLineEdit;
127
128};
129
130
131/*********************************************************************************************************************************
132* UIFileDeleteConfirmationDialog definition. *
133*********************************************************************************************************************************/
134
135/** A QIDialog child including a line edit whose text exposed when the dialog is accepted */
136class UIFileDeleteConfirmationDialog : public QIDialog
137{
138
139 Q_OBJECT;
140
141public:
142
143 UIFileDeleteConfirmationDialog(QWidget *pParent = 0, Qt::WindowFlags flags = 0);
144 /** Returns whether m_pAskNextTimeCheckBox is checked or not. */
145 bool askDeleteConfirmationNextTime() const;
146
147private:
148
149 QCheckBox *m_pAskNextTimeCheckBox;
150 QILabel *m_pQuestionLabel;
151
152};
153
154
155/*********************************************************************************************************************************
156* UIHostDirectoryDiskUsageComputer implementation. *
157*********************************************************************************************************************************/
158
159UIDirectoryDiskUsageComputer::UIDirectoryDiskUsageComputer(QObject *parent, QStringList pathList)
160 :QThread(parent)
161 , m_pathList(pathList)
162 , m_fOkToContinue(true)
163{
164}
165
166void UIDirectoryDiskUsageComputer::run()
167{
168 for (int i = 0; i < m_pathList.size(); ++i)
169 directoryStatisticsRecursive(m_pathList[i], m_resultStatistics);
170}
171
172void UIDirectoryDiskUsageComputer::stopRecursion()
173{
174 m_mutex.lock();
175 m_fOkToContinue = false;
176 m_mutex.unlock();
177}
178
179bool UIDirectoryDiskUsageComputer::isOkToContinue() const
180{
181 return m_fOkToContinue;
182}
183
184
185/*********************************************************************************************************************************
186* UIPathOperations implementation. *
187*********************************************************************************************************************************/
188
189const QChar UIPathOperations::delimiter = QChar('/');
190const QChar UIPathOperations::dosDelimiter = QChar('\\');
191
192/* static */ QString UIPathOperations::removeMultipleDelimiters(const QString &path)
193{
194 QString newPath(path);
195 QString doubleDelimiter(2, delimiter);
196
197 while (newPath.contains(doubleDelimiter) && !newPath.isEmpty())
198 newPath = newPath.replace(doubleDelimiter, delimiter);
199 return newPath;
200}
201
202/* static */ QString UIPathOperations::removeTrailingDelimiters(const QString &path)
203{
204 if (path.isNull() || path.isEmpty())
205 return QString();
206 QString newPath(path);
207 /* Make sure for we dont have any trailing delimiters: */
208 while (newPath.length() > 1 && newPath.at(newPath.length() - 1) == UIPathOperations::delimiter)
209 newPath.chop(1);
210 return newPath;
211}
212
213/* static */ QString UIPathOperations::addTrailingDelimiters(const QString &path)
214{
215 if (path.isNull() || path.isEmpty())
216 return QString();
217 QString newPath(path);
218 while (newPath.length() > 1 && newPath.at(newPath.length() - 1) != UIPathOperations::delimiter)
219 newPath += UIPathOperations::delimiter;
220 return newPath;
221}
222
223/* static */ QString UIPathOperations::addStartDelimiter(const QString &path)
224{
225 if (path.isEmpty())
226 return QString(path);
227 QString newPath(path);
228
229 if (doesPathStartWithDriveLetter(newPath))
230 {
231 if (newPath.at(newPath.length() - 1) != delimiter)
232 newPath += delimiter;
233 return newPath;
234 }
235 if (newPath.at(0) != delimiter)
236 newPath.insert(0, delimiter);
237 return newPath;
238}
239
240/* static */ QString UIPathOperations::sanitize(const QString &path)
241{
242 //return addStartDelimiter(removeTrailingDelimiters(removeMultipleDelimiters(path)));
243 QString newPath = addStartDelimiter(removeTrailingDelimiters(removeMultipleDelimiters(path))).replace(dosDelimiter, delimiter);
244 return newPath;
245}
246
247/* static */ QString UIPathOperations::mergePaths(const QString &path, const QString &baseName)
248{
249 QString newBase(baseName);
250 newBase = newBase.remove(delimiter);
251
252 /* make sure we have one and only one trailing '/': */
253 QString newPath(sanitize(path));
254 if(newPath.isEmpty())
255 newPath = delimiter;
256 if(newPath.at(newPath.length() - 1) != delimiter)
257 newPath += UIPathOperations::delimiter;
258 newPath += newBase;
259 return sanitize(newPath);
260}
261
262/* static */ QString UIPathOperations::getObjectName(const QString &path)
263{
264 if (path.length() <= 1)
265 return QString(path);
266
267 QString strTemp(sanitize(path));
268 if (strTemp.length() < 2)
269 return strTemp;
270 int lastSlashPosition = strTemp.lastIndexOf(UIPathOperations::delimiter);
271 if (lastSlashPosition == -1)
272 return QString();
273 return strTemp.right(strTemp.length() - lastSlashPosition - 1);
274}
275
276/* static */ QString UIPathOperations::getPathExceptObjectName(const QString &path)
277{
278 if (path.length() <= 1)
279 return QString(path);
280
281 QString strTemp(sanitize(path));
282 int lastSlashPosition = strTemp.lastIndexOf(UIPathOperations::delimiter);
283 if (lastSlashPosition == -1)
284 return QString();
285 return strTemp.left(lastSlashPosition + 1);
286}
287
288/* static */ QString UIPathOperations::constructNewItemPath(const QString &previousPath, const QString &newBaseName)
289{
290 if (previousPath.length() <= 1)
291 return QString(previousPath);
292 return sanitize(mergePaths(getPathExceptObjectName(previousPath), newBaseName));
293}
294
295/* static */ QStringList UIPathOperations::pathTrail(const QString &path)
296{
297 QStringList pathList = path.split(UIPathOperations::delimiter, QString::SkipEmptyParts);
298 if (!pathList.isEmpty() && doesPathStartWithDriveLetter(pathList[0]))
299 {
300 pathList[0] = addTrailingDelimiters(pathList[0]);
301 }
302 return pathList;
303}
304
305/* static */ bool UIPathOperations::doesPathStartWithDriveLetter(const QString &path)
306{
307 if (path.length() < 2)
308 return false;
309 /* search for ':' with the path: */
310 if (!path[0].isLetter())
311 return false;
312 if (path[1] != ':')
313 return false;
314 return true;
315}
316
317
318/*********************************************************************************************************************************
319* UIGuestControlFileView implementation. *
320*********************************************************************************************************************************/
321
322UIGuestControlFileView::UIGuestControlFileView(QWidget *parent)
323 :QTableView(parent)
324 , m_pParent(parent)
325{
326 configure();
327}
328
329void UIGuestControlFileView::configure()
330{
331 setContextMenuPolicy(Qt::CustomContextMenu);
332 setShowGrid(false);
333 setSelectionBehavior(QAbstractItemView::SelectRows);
334 verticalHeader()->setVisible(false);
335 setEditTriggers(QAbstractItemView::NoEditTriggers);
336 /* Minimize the row height: */
337 verticalHeader()->setDefaultSectionSize(verticalHeader()->minimumSectionSize());
338 setAlternatingRowColors(true);
339 installEventFilter(m_pParent);
340}
341
342bool UIGuestControlFileView::hasSelection() const
343{
344 QItemSelectionModel *pSelectionModel = selectionModel();
345 if (!pSelectionModel)
346 return false;
347 return pSelectionModel->hasSelection();
348}
349
350void UIGuestControlFileView::selectionChanged(const QItemSelection & selected, const QItemSelection & deselected)
351{
352 emit sigSelectionChanged(selected, deselected);
353 QTableView::selectionChanged(selected, deselected);
354}
355
356
357/*********************************************************************************************************************************
358* UIFileStringInputDialog implementation. *
359*********************************************************************************************************************************/
360
361UIStringInputDialog::UIStringInputDialog(QWidget *pParent /* = 0 */, Qt::WindowFlags flags /* = 0 */)
362 :QIDialog(pParent, flags)
363{
364 QVBoxLayout *layout = new QVBoxLayout(this);
365 m_pLineEdit = new QILineEdit(this);
366 layout->addWidget(m_pLineEdit);
367
368 QIDialogButtonBox *pButtonBox =
369 new QIDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, this);
370 layout->addWidget(pButtonBox);
371 connect(pButtonBox, &QIDialogButtonBox::accepted, this, &UIStringInputDialog::accept);
372 connect(pButtonBox, &QIDialogButtonBox::rejected, this, &UIStringInputDialog::reject);
373}
374
375QString UIStringInputDialog::getString() const
376{
377 if (!m_pLineEdit)
378 return QString();
379 return m_pLineEdit->text();
380}
381
382
383/*********************************************************************************************************************************
384* UIPropertiesDialog implementation. *
385*********************************************************************************************************************************/
386
387UIPropertiesDialog::UIPropertiesDialog(QWidget *pParent, Qt::WindowFlags flags)
388 :QIDialog(pParent, flags)
389 , m_pMainLayout(new QVBoxLayout)
390 , m_pInfoEdit(new QTextEdit)
391{
392 setLayout(m_pMainLayout);
393
394 if (m_pMainLayout)
395 m_pMainLayout->addWidget(m_pInfoEdit);
396 if (m_pInfoEdit)
397 {
398 m_pInfoEdit->setReadOnly(true);
399 m_pInfoEdit->setFrameStyle(QFrame::NoFrame);
400 }
401 QIDialogButtonBox *pButtonBox =
402 new QIDialogButtonBox(QDialogButtonBox::Ok, Qt::Horizontal, this);
403 m_pMainLayout->addWidget(pButtonBox);
404 connect(pButtonBox, &QIDialogButtonBox::accepted, this, &UIStringInputDialog::accept);
405}
406
407void UIPropertiesDialog::setPropertyText(const QString &strProperty)
408{
409 if (!m_pInfoEdit)
410 return;
411 m_strProperty = strProperty;
412 m_pInfoEdit->setHtml(strProperty);
413}
414
415void UIPropertiesDialog::addDirectoryStatistics(UIDirectoryStatistics directoryStatistics)
416{
417 if (!m_pInfoEdit)
418 return;
419 // QString propertyString = m_pInfoEdit->toHtml();
420 // propertyString += "<b>Total Size:</b> " + QString::number(directoryStatistics.m_totalSize) + QString(" bytes");
421 // if (directoryStatistics.m_totalSize >= UIGuestControlFileTable::m_iKiloByte)
422 // propertyString += " (" + UIGuestControlFileTable::humanReadableSize(directoryStatistics.m_totalSize) + ")";
423 // propertyString += "<br/>";
424 // propertyString += "<b>File Count:</b> " + QString::number(directoryStatistics.m_uFileCount);
425
426 // m_pInfoEdit->setHtml(propertyString);
427
428 QString detailsString(m_strProperty);
429 detailsString += "<br/>";
430 detailsString += "<b>" + UIGuestControlFileManager::tr("Total Size") + "</b> " +
431 QString::number(directoryStatistics.m_totalSize) + UIGuestControlFileManager::tr(" bytes");
432 if (directoryStatistics.m_totalSize >= UIGuestControlFileTable::m_iKiloByte)
433 detailsString += " (" + UIGuestControlFileTable::humanReadableSize(directoryStatistics.m_totalSize) + ")";
434 detailsString += "<br/>";
435
436 detailsString += "<b>" + UIGuestControlFileManager::tr("File Count") + ":</b> " +
437 QString::number(directoryStatistics.m_uFileCount);
438
439 m_pInfoEdit->setHtml(detailsString);
440}
441
442
443/*********************************************************************************************************************************
444* UIDirectoryStatistics implementation. *
445*********************************************************************************************************************************/
446
447UIDirectoryStatistics::UIDirectoryStatistics()
448 : m_totalSize(0)
449 , m_uFileCount(0)
450 , m_uDirectoryCount(0)
451 , m_uSymlinkCount(0)
452{
453}
454
455
456/*********************************************************************************************************************************
457* UIFileTableItem implementation. *
458*********************************************************************************************************************************/
459
460UIFileTableItem::UIFileTableItem(const QVector<QVariant> &data,
461 UIFileTableItem *parent, FileObjectType type)
462 : m_itemData(data)
463 , m_parentItem(parent)
464 , m_bIsOpened(false)
465 , m_isTargetADirectory(false)
466 , m_type(type)
467 , m_isDriveItem(false)
468{
469}
470
471UIFileTableItem::~UIFileTableItem()
472{
473 qDeleteAll(m_childItems);
474 m_childItems.clear();
475}
476
477void UIFileTableItem::appendChild(UIFileTableItem *item)
478{
479 if (!item)
480 return;
481 m_childItems.append(item);
482 m_childMap.insert(item->name(), item);
483}
484
485UIFileTableItem *UIFileTableItem::child(int row) const
486{
487 return m_childItems.value(row);
488}
489
490UIFileTableItem *UIFileTableItem::child(const QString &path) const
491{
492 if (!m_childMap.contains(path))
493 return 0;
494 return m_childMap.value(path);
495}
496
497int UIFileTableItem::childCount() const
498{
499 return m_childItems.count();
500}
501
502int UIFileTableItem::columnCount() const
503{
504 return m_itemData.count();
505}
506
507QVariant UIFileTableItem::data(int column) const
508{
509 return m_itemData.value(column);
510}
511
512QString UIFileTableItem::name() const
513{
514 if (m_itemData.isEmpty() || !m_itemData[0].canConvert(QMetaType::QString))
515 return QString();
516 return m_itemData[0].toString();
517}
518
519void UIFileTableItem::setData(const QVariant &data, int index)
520{
521 if (index >= m_itemData.length())
522 return;
523 m_itemData[index] = data;
524}
525
526UIFileTableItem *UIFileTableItem::parentItem()
527{
528 return m_parentItem;
529}
530
531int UIFileTableItem::row() const
532{
533 if (m_parentItem)
534 return m_parentItem->m_childItems.indexOf(const_cast<UIFileTableItem*>(this));
535 return 0;
536}
537
538bool UIFileTableItem::isDirectory() const
539{
540 return m_type == FileObjectType_Directory;
541}
542
543bool UIFileTableItem::isSymLink() const
544{
545 return m_type == FileObjectType_SymLink;
546}
547
548bool UIFileTableItem::isFile() const
549{
550 return m_type == FileObjectType_File;
551}
552
553void UIFileTableItem::clearChildren()
554{
555 qDeleteAll(m_childItems);
556 m_childItems.clear();
557 m_childMap.clear();
558}
559
560bool UIFileTableItem::isOpened() const
561{
562 return m_bIsOpened;
563}
564
565void UIFileTableItem::setIsOpened(bool flag)
566{
567 m_bIsOpened = flag;
568}
569
570const QString &UIFileTableItem::path() const
571{
572 return m_strPath;
573}
574
575void UIFileTableItem::setPath(const QString &path)
576{
577 if (path.isNull() || path.isEmpty())
578 return;
579 m_strPath = path;
580 UIPathOperations::removeTrailingDelimiters(m_strPath);
581}
582
583bool UIFileTableItem::isUpDirectory() const
584{
585 if (!isDirectory())
586 return false;
587 if (data(0) == UIGuestControlFileModel::strUpDirectoryString)
588 return true;
589 return false;
590}
591
592FileObjectType UIFileTableItem::type() const
593{
594 return m_type;
595}
596
597const QString &UIFileTableItem::targetPath() const
598{
599 return m_strTargetPath;
600}
601
602void UIFileTableItem::setTargetPath(const QString &path)
603{
604 m_strTargetPath = path;
605}
606
607bool UIFileTableItem::isTargetADirectory() const
608{
609 return m_isTargetADirectory;
610}
611
612void UIFileTableItem::setIsTargetADirectory(bool flag)
613{
614 m_isTargetADirectory = flag;
615}
616
617void UIFileTableItem::setIsDriveItem(bool flag)
618{
619 m_isDriveItem = flag;
620}
621
622bool UIFileTableItem::isDriveItem() const
623{
624 return m_isDriveItem;
625}
626
627/*********************************************************************************************************************************
628+* UIFileDeleteConfirmationDialog implementation. *
629+*********************************************************************************************************************************/
630
631UIFileDeleteConfirmationDialog::UIFileDeleteConfirmationDialog(QWidget *pParent /* = 0 */, Qt::WindowFlags flags /* = 0 */)
632 :QIDialog(pParent, flags)
633 , m_pAskNextTimeCheckBox(0)
634 , m_pQuestionLabel(0)
635{
636 QVBoxLayout *pLayout = new QVBoxLayout(this);
637
638 m_pQuestionLabel = new QILabel;
639 if (m_pQuestionLabel)
640 {
641 pLayout->addWidget(m_pQuestionLabel);
642 m_pQuestionLabel->setText(UIGuestControlFileManager::tr("Delete the selected file(s) and/or folder(s)"));
643 }
644
645 QIDialogButtonBox *pButtonBox =
646 new QIDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, this);
647 if (pButtonBox)
648 {
649 pLayout->addWidget(pButtonBox, 0, Qt::AlignCenter);
650 connect(pButtonBox, &QIDialogButtonBox::accepted, this, &UIStringInputDialog::accept);
651 connect(pButtonBox, &QIDialogButtonBox::rejected, this, &UIStringInputDialog::reject);
652 }
653
654 m_pAskNextTimeCheckBox = new QCheckBox;
655
656 if (m_pAskNextTimeCheckBox)
657 {
658 UIGuestControlFileManagerSettings *pFileManagerSettings = UIGuestControlFileManagerSettings::instance();
659 if (pFileManagerSettings)
660 m_pAskNextTimeCheckBox->setChecked(pFileManagerSettings->bAskDeleteConfirmation);
661
662 pLayout->addWidget(m_pAskNextTimeCheckBox);
663 m_pAskNextTimeCheckBox->setText(UIGuestControlFileManager::tr("Ask for this confirmation next time"));
664 m_pAskNextTimeCheckBox->setToolTip(UIGuestControlFileManager::tr("Delete confirmation can be "
665 "disabled/enabled also from the Options panel."));
666 }
667}
668
669bool UIFileDeleteConfirmationDialog::askDeleteConfirmationNextTime() const
670{
671 if (!m_pAskNextTimeCheckBox)
672 return true;
673 return m_pAskNextTimeCheckBox->isChecked();
674}
675
676
677/*********************************************************************************************************************************
678* UIGuestControlFileTable implementation. *
679*********************************************************************************************************************************/
680const unsigned UIGuestControlFileTable::m_iKiloByte = 1000;
681UIGuestControlFileTable::UIGuestControlFileTable(UIActionPool *pActionPool, QWidget *pParent /* = 0 */)
682 :QIWithRetranslateUI<QWidget>(pParent)
683 , m_pRootItem(0)
684 , m_pLocationLabel(0)
685 , m_pPropertiesDialog(0)
686 , m_pActionPool(pActionPool)
687 , m_pToolBar(0)
688 , m_pModel(0)
689 , m_pView(0)
690 , m_pProxyModel(0)
691 , m_pMainLayout(0)
692 , m_pLocationComboBox(0)
693 , m_pWarningLabel(0)
694{
695 prepareObjects();
696}
697
698UIGuestControlFileTable::~UIGuestControlFileTable()
699{
700 delete m_pRootItem;
701}
702
703void UIGuestControlFileTable::reset()
704{
705 if (m_pModel)
706 m_pModel->beginReset();
707 delete m_pRootItem;
708 m_pRootItem = 0;
709 if (m_pModel)
710 m_pModel->endReset();
711 if (m_pLocationComboBox)
712 {
713 disconnect(m_pLocationComboBox, static_cast<void(QComboBox::*)(const QString&)>(&QComboBox::currentIndexChanged),
714 this, &UIGuestControlFileTable::sltLocationComboCurrentChange);
715 m_pLocationComboBox->clear();
716 connect(m_pLocationComboBox, static_cast<void(QComboBox::*)(const QString&)>(&QComboBox::currentIndexChanged),
717 this, &UIGuestControlFileTable::sltLocationComboCurrentChange);
718 }
719}
720
721void UIGuestControlFileTable::emitLogOutput(const QString& strOutput, FileManagerLogType eLogType)
722{
723 emit sigLogOutput(strOutput, eLogType);
724}
725
726void UIGuestControlFileTable::prepareObjects()
727{
728 m_pMainLayout = new QGridLayout();
729 if (!m_pMainLayout)
730 return;
731 m_pMainLayout->setSpacing(0);
732 m_pMainLayout->setContentsMargins(0, 0, 0, 0);
733 setLayout(m_pMainLayout);
734
735 m_pToolBar = new UIToolBar;
736 if (m_pToolBar)
737 {
738 m_pMainLayout->addWidget(m_pToolBar, 0, 0, 1, 5);
739 }
740
741 m_pLocationLabel = new QILabel;
742 if (m_pLocationLabel)
743 {
744 m_pMainLayout->addWidget(m_pLocationLabel, 1, 0, 1, 1);
745 }
746
747 m_pLocationComboBox = new QComboBox;
748 if (m_pLocationComboBox)
749 {
750 m_pMainLayout->addWidget(m_pLocationComboBox, 1, 1, 1, 4);
751 m_pLocationComboBox->setEditable(false);
752 connect(m_pLocationComboBox, static_cast<void(QComboBox::*)(const QString&)>(&QComboBox::currentIndexChanged),
753 this, &UIGuestControlFileTable::sltLocationComboCurrentChange);
754 }
755
756
757 m_pModel = new UIGuestControlFileModel(this);
758 if (!m_pModel)
759 return;
760
761 m_pProxyModel = new UIGuestControlFileProxyModel(this);
762 if (!m_pProxyModel)
763 return;
764 m_pProxyModel->setSourceModel(m_pModel);
765
766 m_pView = new UIGuestControlFileView(this);
767 if (m_pView)
768 {
769 m_pMainLayout->addWidget(m_pView, 2, 0, 5, 5);
770 m_pView->setModel(m_pProxyModel);
771 m_pView->setItemDelegate(new UIFileDelegate);
772 m_pView->setSortingEnabled(true);
773 m_pView->sortByColumn(0, Qt::AscendingOrder);
774
775 connect(m_pView, &UIGuestControlFileView::doubleClicked,
776 this, &UIGuestControlFileTable::sltItemDoubleClicked);
777 connect(m_pView, &UIGuestControlFileView::clicked,
778 this, &UIGuestControlFileTable::sltItemClicked);
779 connect(m_pView, &UIGuestControlFileView::sigSelectionChanged,
780 this, &UIGuestControlFileTable::sltSelectionChanged);
781 connect(m_pView, &UIGuestControlFileView::customContextMenuRequested,
782 this, &UIGuestControlFileTable::sltCreateFileViewContextMenu);
783
784 }
785 m_pWarningLabel = new QILabel(this);
786 if (m_pWarningLabel)
787 {
788 m_pMainLayout->addWidget(m_pWarningLabel, 2, 0, 5, 5);
789 m_pWarningLabel->setStyleSheet("font: 24pt;");
790 m_pWarningLabel->setAlignment(Qt::AlignCenter | Qt::AlignVCenter);
791 }
792 m_pWarningLabel->setVisible(!isEnabled());
793 m_pView->setVisible(isEnabled());
794
795 m_pSearchLineEdit = new QILineEdit;
796 if (m_pSearchLineEdit)
797 {
798 m_pMainLayout->addWidget(m_pSearchLineEdit, 8, 0, 1, 5);
799 m_pSearchLineEdit->hide();
800 m_pSearchLineEdit->setClearButtonEnabled(true);
801 connect(m_pSearchLineEdit, &QLineEdit::textChanged,
802 this, &UIGuestControlFileTable::sltSearchTextChanged);
803 }
804}
805
806void UIGuestControlFileTable::updateCurrentLocationEdit(const QString& strLocation)
807{
808 if (!m_pLocationComboBox)
809 return;
810 int itemIndex = m_pLocationComboBox->findText(strLocation,
811 Qt::MatchExactly | Qt::MatchCaseSensitive);
812 if (itemIndex == -1)
813 {
814 m_pLocationComboBox->insertItem(m_pLocationComboBox->count(), strLocation);
815 itemIndex = m_pLocationComboBox->count() - 1;
816 }
817 m_pLocationComboBox->setCurrentIndex(itemIndex);
818}
819
820void UIGuestControlFileTable::changeLocation(const QModelIndex &index)
821{
822 if (!index.isValid() || !m_pView)
823 return;
824 m_pView->setRootIndex(m_pProxyModel->mapFromSource(index));
825 m_pView->clearSelection();
826
827 UIFileTableItem *item = static_cast<UIFileTableItem*>(index.internalPointer());
828 if (item)
829 {
830 updateCurrentLocationEdit(item->path());
831 }
832 /** @todo check if we really need this and if not remove it */
833 //m_pModel->signalUpdate();
834}
835
836void UIGuestControlFileTable::initializeFileTree()
837{
838 if (m_pRootItem)
839 reset();
840
841 /* Root item: */
842 const QString startPath("/");
843 QVector<QVariant> headData;
844 headData.resize(UIGuestControlFileModelColumn_Max);
845 m_pRootItem = new UIFileTableItem(headData, 0, FileObjectType_Directory);
846 UIFileTableItem* startItem = new UIFileTableItem(createTreeItemData(startPath, 4096, QDateTime(),
847 "" /* owner */, "" /* permissions */),
848 m_pRootItem, FileObjectType_Directory);
849 startItem->setPath(startPath);
850 m_pRootItem->appendChild(startItem);
851 startItem->setIsOpened(false);
852 populateStartDirectory(startItem);
853
854 m_pModel->signalUpdate();
855 updateCurrentLocationEdit(startPath);
856 m_pView->setRootIndex(m_pProxyModel->mapFromSource(m_pModel->rootIndex()));
857}
858
859void UIGuestControlFileTable::populateStartDirectory(UIFileTableItem *startItem)
860{
861 determineDriveLetters();
862 if (m_driveLetterList.isEmpty())
863 {
864 /* Read the root directory and get the list: */
865 readDirectory(startItem->path(), startItem, true);
866 }
867 else
868 {
869 for (int i = 0; i < m_driveLetterList.size(); ++i)
870 {
871 UIFileTableItem* driveItem = new UIFileTableItem(createTreeItemData(m_driveLetterList[i], 4096,
872 QDateTime(), QString(), QString()),
873 startItem, FileObjectType_Directory);
874 driveItem->setPath(m_driveLetterList[i]);
875 startItem->appendChild(driveItem);
876 driveItem->setIsOpened(false);
877 driveItem->setIsDriveItem(true);
878 startItem->setIsOpened(true);
879 }
880 }
881}
882
883void UIGuestControlFileTable::insertItemsToTree(QMap<QString,UIFileTableItem*> &map,
884 UIFileTableItem *parent, bool isDirectoryMap, bool isStartDir)
885{
886 if (parent)
887
888 /* Make sure we have an item representing up directory, and make sure it is not there for the start dir: */
889 if (isDirectoryMap)
890 {
891 if (!map.contains(UIGuestControlFileModel::strUpDirectoryString) && !isStartDir)
892 {
893 QVector<QVariant> data;
894 UIFileTableItem *item = new UIFileTableItem(createTreeItemData(UIGuestControlFileModel::strUpDirectoryString, 4096,
895 QDateTime(), QString(), QString())
896 , parent, FileObjectType_Directory);
897 item->setIsOpened(false);
898 map.insert(UIGuestControlFileModel::strUpDirectoryString, item);
899 }
900 else if (map.contains(UIGuestControlFileModel::strUpDirectoryString) && isStartDir)
901 {
902 map.remove(UIGuestControlFileModel::strUpDirectoryString);
903 }
904 }
905 for (QMap<QString,UIFileTableItem*>::const_iterator iterator = map.begin();
906 iterator != map.end(); ++iterator)
907 {
908 if (iterator.key() == "." || iterator.key().isEmpty())
909 continue;
910 parent->appendChild(iterator.value());
911 }
912}
913
914void UIGuestControlFileTable::sltItemDoubleClicked(const QModelIndex &index)
915{
916 if (!index.isValid() || !m_pModel || !m_pView)
917 return;
918 QModelIndex nIndex = m_pProxyModel ? m_pProxyModel->mapToSource(index) : index;
919 goIntoDirectory(nIndex);
920}
921
922void UIGuestControlFileTable::sltItemClicked(const QModelIndex &index)
923{
924 Q_UNUSED(index);
925 disableSelectionSearch();
926}
927
928void UIGuestControlFileTable::sltGoUp()
929{
930 if (!m_pView || !m_pModel)
931 return;
932 QModelIndex currentRoot = currentRootIndex();
933
934 if (!currentRoot.isValid())
935 return;
936 if (currentRoot != m_pModel->rootIndex())
937 {
938 QModelIndex parentIndex = currentRoot.parent();
939 if (parentIndex.isValid())
940 {
941 changeLocation(currentRoot.parent());
942 m_pView->selectRow(currentRoot.row());
943 }
944 }
945}
946
947void UIGuestControlFileTable::sltGoHome()
948{
949 goToHomeDirectory();
950}
951
952void UIGuestControlFileTable::sltRefresh()
953{
954 refresh();
955}
956
957void UIGuestControlFileTable::goIntoDirectory(const QModelIndex &itemIndex)
958{
959 if (!m_pModel)
960 return;
961
962 /* Make sure the colum is 0: */
963 QModelIndex index = m_pModel->index(itemIndex.row(), 0, itemIndex.parent());
964 if (!index.isValid())
965 return;
966
967 UIFileTableItem *item = static_cast<UIFileTableItem*>(index.internalPointer());
968 if (!item)
969 return;
970
971 /* check if we need to go up: */
972 if (item->isUpDirectory())
973 {
974 QModelIndex parentIndex = m_pModel->parent(m_pModel->parent(index));
975 if (parentIndex.isValid())
976 changeLocation(parentIndex);
977 return;
978 }
979
980 if (!item->isDirectory())
981 return;
982 if (!item->isOpened())
983 readDirectory(item->path(),item);
984 changeLocation(index);
985}
986
987void UIGuestControlFileTable::goIntoDirectory(const QStringList &pathTrail)
988{
989 UIFileTableItem *parent = getStartDirectoryItem();
990
991 for(int i = 0; i < pathTrail.size(); ++i)
992 {
993 if (!parent)
994 return;
995 /* Make sure parent is already opened: */
996 if (!parent->isOpened())
997 readDirectory(parent->path(), parent, parent == getStartDirectoryItem());
998 /* search the current path item among the parent's children: */
999 UIFileTableItem *item = parent->child(pathTrail.at(i));
1000 if (!item)
1001 return;
1002 parent = item;
1003 }
1004 if (!parent)
1005 return;
1006 if (!parent->isOpened())
1007 readDirectory(parent->path(), parent, parent == getStartDirectoryItem());
1008 goIntoDirectory(parent);
1009}
1010
1011void UIGuestControlFileTable::goIntoDirectory(UIFileTableItem *item)
1012{
1013 if (!item || !m_pModel)
1014 return;
1015 goIntoDirectory(m_pModel->index(item));
1016}
1017
1018UIFileTableItem* UIGuestControlFileTable::indexData(const QModelIndex &index) const
1019{
1020 if (!index.isValid())
1021 return 0;
1022 return static_cast<UIFileTableItem*>(index.internalPointer());
1023}
1024
1025void UIGuestControlFileTable::refresh()
1026{
1027 if (!m_pView || !m_pModel)
1028 return;
1029 QModelIndex currentIndex = currentRootIndex();
1030
1031 UIFileTableItem *treeItem = indexData(currentIndex);
1032 if (!treeItem)
1033 return;
1034 bool isRootDir = (m_pModel->rootIndex() == currentIndex);
1035 m_pModel->beginReset();
1036 /* For now we clear the whole subtree (that isrecursively) which is an overkill: */
1037 treeItem->clearChildren();
1038 if (isRootDir)
1039 populateStartDirectory(treeItem);
1040 else
1041 readDirectory(treeItem->path(), treeItem, isRootDir);
1042 m_pModel->endReset();
1043 m_pView->setRootIndex(m_pProxyModel->mapFromSource(currentIndex));
1044 setSelectionDependentActionsEnabled(m_pView->hasSelection());
1045}
1046
1047void UIGuestControlFileTable::relist()
1048{
1049 if (!m_pProxyModel)
1050 return;
1051 m_pProxyModel->invalidate();
1052}
1053
1054void UIGuestControlFileTable::sltDelete()
1055{
1056 if (!checkIfDeleteOK())
1057 return;
1058
1059 if (!m_pView || !m_pModel)
1060 return;
1061
1062 if (!m_pView || !m_pModel)
1063 return;
1064 QItemSelectionModel *selectionModel = m_pView->selectionModel();
1065 if (!selectionModel)
1066 return;
1067
1068 QModelIndexList selectedItemIndices = selectionModel->selectedRows();
1069 for(int i = 0; i < selectedItemIndices.size(); ++i)
1070 {
1071 QModelIndex index =
1072 m_pProxyModel ? m_pProxyModel->mapToSource(selectedItemIndices.at(i)) : selectedItemIndices.at(i);
1073 deleteByIndex(index);
1074 }
1075 /** @todo dont refresh here, just delete the rows and update the table view: */
1076 refresh();
1077}
1078
1079void UIGuestControlFileTable::sltRename()
1080{
1081 if (!m_pView)
1082 return;
1083 QItemSelectionModel *selectionModel = m_pView->selectionModel();
1084 if (!selectionModel)
1085 return;
1086
1087 QModelIndexList selectedItemIndices = selectionModel->selectedRows();
1088 if (selectedItemIndices.size() == 0)
1089 return;
1090 QModelIndex modelIndex =
1091 m_pProxyModel ? m_pProxyModel->mapToSource(selectedItemIndices.at(0)) : selectedItemIndices.at(0);
1092 UIFileTableItem *item = indexData(modelIndex);
1093 if (!item || item->isUpDirectory())
1094 return;
1095 m_pView->edit(selectedItemIndices.at(0));
1096}
1097
1098void UIGuestControlFileTable::sltCreateNewDirectory()
1099{
1100 if (!m_pModel || !m_pView)
1101 return;
1102 QModelIndex currentIndex = currentRootIndex();
1103 if (!currentIndex.isValid())
1104 return;
1105 UIFileTableItem *item = static_cast<UIFileTableItem*>(currentIndex.internalPointer());
1106 if (!item)
1107 return;
1108
1109 QString newDirectoryName = getNewDirectoryName();
1110 if (newDirectoryName.isEmpty())
1111 return;
1112
1113 if (createDirectory(item->path(), newDirectoryName))
1114 {
1115 /** @todo instead of refreshing here (an overkill) just add the
1116 rows and update the tabel view: */
1117 sltRefresh();
1118 }
1119}
1120
1121void UIGuestControlFileTable::sltCopy()
1122{
1123
1124 m_copyCutBuffer = selectedItemPathList();
1125 // if (!m_copyCutBuffer.isEmpty())
1126 // m_pPaste->setEnabled(true);
1127 // else
1128 // m_pPaste->setEnabled(false);
1129}
1130
1131void UIGuestControlFileTable::sltCut()
1132{
1133 m_copyCutBuffer = selectedItemPathList();
1134 // if (!m_copyCutBuffer.isEmpty())
1135 // m_pPaste->setEnabled(true);
1136 // else
1137 // m_pPaste->setEnabled(false);
1138}
1139
1140void UIGuestControlFileTable::sltPaste()
1141{
1142 // paste them
1143 m_copyCutBuffer.clear();
1144 //m_pPaste->setEnabled(false);
1145}
1146
1147void UIGuestControlFileTable::sltShowProperties()
1148{
1149 showProperties();
1150}
1151
1152void UIGuestControlFileTable::sltSelectionChanged(const QItemSelection & selected, const QItemSelection & deselected)
1153{
1154 Q_UNUSED(selected);
1155 Q_UNUSED(deselected);
1156 setSelectionDependentActionsEnabled(m_pView->hasSelection());
1157}
1158
1159void UIGuestControlFileTable::sltLocationComboCurrentChange(const QString &strLocation)
1160{
1161 QString comboLocation(UIPathOperations::sanitize(strLocation));
1162 if (comboLocation == currentDirectoryPath())
1163 return;
1164 goIntoDirectory(UIPathOperations::pathTrail(comboLocation));
1165}
1166
1167void UIGuestControlFileTable::sltSelectAll()
1168{
1169 if (!m_pModel || !m_pView)
1170 return;
1171 m_pView->selectAll();
1172 deSelectUpDirectoryItem();
1173}
1174
1175void UIGuestControlFileTable::sltInvertSelection()
1176{
1177 setSelectionForAll(QItemSelectionModel::Toggle | QItemSelectionModel::Rows);
1178 deSelectUpDirectoryItem();
1179}
1180
1181void UIGuestControlFileTable::sltSearchTextChanged(const QString &strText)
1182{
1183 performSelectionSearch(strText);
1184}
1185
1186void UIGuestControlFileTable::sltCreateFileViewContextMenu(const QPoint &point)
1187{
1188 QWidget *pSender = qobject_cast<QWidget*>(sender());
1189 if (!pSender)
1190 return;
1191 createFileViewContextMenu(pSender, point);
1192}
1193
1194void UIGuestControlFileTable::deSelectUpDirectoryItem()
1195{
1196 if (!m_pView)
1197 return;
1198 QItemSelectionModel *pSelectionModel = m_pView->selectionModel();
1199 if (!pSelectionModel)
1200 return;
1201 QModelIndex currentRoot = currentRootIndex();
1202 if (!currentRoot.isValid())
1203 return;
1204
1205 /* Make sure that "up directory item" (if exists) is deselected: */
1206 for (int i = 0; i < m_pModel->rowCount(currentRoot); ++i)
1207 {
1208 QModelIndex index = m_pModel->index(i, 0, currentRoot);
1209 if (!index.isValid())
1210 continue;
1211
1212 UIFileTableItem *item = static_cast<UIFileTableItem*>(index.internalPointer());
1213 if (item && item->isUpDirectory())
1214 {
1215 QModelIndex indexToDeselect = m_pProxyModel ? m_pProxyModel->mapFromSource(index) : index;
1216 pSelectionModel->select(indexToDeselect, QItemSelectionModel::Deselect | QItemSelectionModel::Rows);
1217 }
1218 }
1219}
1220
1221void UIGuestControlFileTable::setSelectionForAll(QItemSelectionModel::SelectionFlags flags)
1222{
1223 if (!m_pView)
1224 return;
1225 QItemSelectionModel *pSelectionModel = m_pView->selectionModel();
1226 if (!pSelectionModel)
1227 return;
1228 QModelIndex currentRoot = currentRootIndex();
1229 if (!currentRoot.isValid())
1230 return;
1231
1232 for (int i = 0; i < m_pModel->rowCount(currentRoot); ++i)
1233 {
1234 QModelIndex index = m_pModel->index(i, 0, currentRoot);
1235 if (!index.isValid())
1236 continue;
1237 QModelIndex indexToSelect = m_pProxyModel ? m_pProxyModel->mapFromSource(index) : index;
1238 pSelectionModel->select(indexToSelect, flags);
1239 }
1240}
1241
1242void UIGuestControlFileTable::setSelection(const QModelIndex &indexInProxyModel)
1243{
1244 if (!m_pView)
1245 return;
1246 QItemSelectionModel *selectionModel = m_pView->selectionModel();
1247 if (!selectionModel)
1248 return;
1249 selectionModel->select(indexInProxyModel, QItemSelectionModel::Current | QItemSelectionModel::Rows | QItemSelectionModel::Select);
1250 m_pView->scrollTo(indexInProxyModel, QAbstractItemView::EnsureVisible);
1251}
1252
1253void UIGuestControlFileTable::deleteByIndex(const QModelIndex &itemIndex)
1254{
1255 UIFileTableItem *treeItem = indexData(itemIndex);
1256 if (!treeItem)
1257 return;
1258 deleteByItem(treeItem);
1259}
1260
1261void UIGuestControlFileTable::retranslateUi()
1262{
1263 if (m_pRootItem)
1264 {
1265 m_pRootItem->setData(UIGuestControlFileManager::tr("Name"), UIGuestControlFileModelColumn_Name);
1266 m_pRootItem->setData(UIGuestControlFileManager::tr("Size"), UIGuestControlFileModelColumn_Size);
1267 m_pRootItem->setData(UIGuestControlFileManager::tr("Change Time"), UIGuestControlFileModelColumn_ChangeTime);
1268 m_pRootItem->setData(UIGuestControlFileManager::tr("Owner"), UIGuestControlFileModelColumn_Owner);
1269 m_pRootItem->setData(UIGuestControlFileManager::tr("Permissions"), UIGuestControlFileModelColumn_Permissions);
1270 }
1271 if (m_pWarningLabel)
1272 m_pWarningLabel->setText(UIGuestControlFileManager::tr("No Guest Session"));
1273}
1274
1275bool UIGuestControlFileTable::eventFilter(QObject *pObject, QEvent *pEvent) /* override */
1276{
1277 Q_UNUSED(pObject);
1278 if (pEvent->type() == QEvent::KeyPress)
1279 {
1280 QKeyEvent *pKeyEvent = dynamic_cast<QKeyEvent*>(pEvent);
1281 if (pKeyEvent)
1282 {
1283 if (pKeyEvent->key() == Qt::Key_Enter || pKeyEvent->key() == Qt::Key_Return)
1284 {
1285 if (m_pView && m_pModel)
1286 {
1287 /* Get the selected item. If there are 0 or more than 1 selection do nothing: */
1288 QItemSelectionModel *selectionModel = m_pView->selectionModel();
1289 if (selectionModel)
1290 {
1291 QModelIndexList selectedItemIndices = selectionModel->selectedRows();
1292 if (selectedItemIndices.size() == 1 && m_pModel)
1293 goIntoDirectory( m_pProxyModel->mapToSource(selectedItemIndices.at(0)));
1294 }
1295 }
1296 return true;
1297 }
1298 else if (pKeyEvent->key() == Qt::Key_Delete)
1299 {
1300 sltDelete();
1301 return true;
1302 }
1303 else if (pKeyEvent->key() == Qt::Key_Backspace)
1304 {
1305 sltGoUp();
1306 return true;
1307 }
1308 else if (pKeyEvent->text().length() == 1 && pKeyEvent->text().at(0).unicode() <= 127)
1309 {
1310 if (m_pSearchLineEdit)
1311 {
1312 m_pSearchLineEdit->show();
1313 QString strText = m_pSearchLineEdit->text();
1314 strText.append(pKeyEvent->text());
1315 m_pSearchLineEdit->setText(strText);
1316 }
1317 }
1318 }
1319 }
1320
1321 return false;
1322}
1323
1324UIFileTableItem *UIGuestControlFileTable::getStartDirectoryItem()
1325{
1326 if (!m_pRootItem)
1327 return 0;
1328 if (m_pRootItem->childCount() <= 0)
1329 return 0;
1330 return m_pRootItem->child(0);
1331}
1332
1333
1334QString UIGuestControlFileTable::getNewDirectoryName()
1335{
1336 UIStringInputDialog *dialog = new UIStringInputDialog();
1337 if (dialog->execute())
1338 {
1339 QString strDialog = dialog->getString();
1340 delete dialog;
1341 return strDialog;
1342 }
1343 delete dialog;
1344 return QString();
1345}
1346
1347QString UIGuestControlFileTable::currentDirectoryPath() const
1348{
1349 if (!m_pView)
1350 return QString();
1351 QModelIndex currentRoot = currentRootIndex();
1352 if (!currentRoot.isValid())
1353 return QString();
1354 UIFileTableItem *item = static_cast<UIFileTableItem*>(currentRoot.internalPointer());
1355 if (!item)
1356 return QString();
1357 /* be paranoid: */
1358 if (!item->isDirectory())
1359 return QString();
1360 return item->path();
1361}
1362
1363QStringList UIGuestControlFileTable::selectedItemPathList()
1364{
1365 QItemSelectionModel *selectionModel = m_pView->selectionModel();
1366 if (!selectionModel)
1367 return QStringList();
1368
1369 QStringList pathList;
1370 QModelIndexList selectedItemIndices = selectionModel->selectedRows();
1371 for(int i = 0; i < selectedItemIndices.size(); ++i)
1372 {
1373 QModelIndex index =
1374 m_pProxyModel ? m_pProxyModel->mapToSource(selectedItemIndices.at(i)) : selectedItemIndices.at(i);
1375 UIFileTableItem *item = static_cast<UIFileTableItem*>(index.internalPointer());
1376 if (!item)
1377 continue;
1378 pathList.push_back(item->path());
1379 }
1380 return pathList;
1381}
1382
1383CGuestFsObjInfo UIGuestControlFileTable::guestFsObjectInfo(const QString& path, CGuestSession &comGuestSession) const
1384{
1385 if (comGuestSession.isNull())
1386 return CGuestFsObjInfo();
1387 CGuestFsObjInfo comFsObjInfo = comGuestSession.FsObjQueryInfo(path, true /*aFollowSymlinks*/);
1388 if (!comFsObjInfo.isOk())
1389 return CGuestFsObjInfo();
1390 return comFsObjInfo;
1391}
1392
1393void UIGuestControlFileTable::setSelectionDependentActionsEnabled(bool fIsEnabled)
1394{
1395 foreach (QAction *pAction, m_selectionDependentActions)
1396 {
1397 pAction->setEnabled(fIsEnabled);
1398 }
1399}
1400
1401
1402QVector<QVariant> UIGuestControlFileTable::createTreeItemData(const QString &strName, ULONG64 size, const QDateTime &changeTime,
1403 const QString &strOwner, const QString &strPermissions)
1404{
1405 QVector<QVariant> data;
1406 data.resize(UIGuestControlFileModelColumn_Max);
1407 data[UIGuestControlFileModelColumn_Name] = strName;
1408 data[UIGuestControlFileModelColumn_Size] = (qulonglong)size;
1409 data[UIGuestControlFileModelColumn_ChangeTime] = changeTime;
1410 data[UIGuestControlFileModelColumn_Owner] = strOwner;
1411 data[UIGuestControlFileModelColumn_Permissions] = strPermissions;
1412 return data;
1413}
1414
1415bool UIGuestControlFileTable::event(QEvent *pEvent)
1416{
1417 if (pEvent->type() == QEvent::EnabledChange)
1418 {
1419 m_pWarningLabel->setVisible(!isEnabled());
1420 m_pView->setVisible(isEnabled());
1421 }
1422 return QIWithRetranslateUI<QWidget>::event(pEvent);
1423}
1424
1425
1426QString UIGuestControlFileTable::fileTypeString(FileObjectType type)
1427{
1428 QString strType = UIGuestControlFileManager::tr("Unknown");
1429 switch(type)
1430 {
1431 case FileObjectType_File:
1432 strType = UIGuestControlFileManager::tr("File");
1433 break;
1434 case FileObjectType_Directory:
1435 strType = UIGuestControlFileManager::tr("Directory");
1436 break;
1437 case FileObjectType_SymLink:
1438 strType = UIGuestControlFileManager::tr("Symbolic Link");
1439 break;
1440 case FileObjectType_Other:
1441 strType = UIGuestControlFileManager::tr("Other");
1442 break;
1443
1444 case FileObjectType_Unknown:
1445 default:
1446 break;
1447 }
1448 return strType;
1449}
1450
1451/* static */ QString UIGuestControlFileTable::humanReadableSize(ULONG64 size)
1452{
1453 int i = 0;
1454 double dSize = size;
1455 const char* units[] = {" B", " kB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB"};
1456 while (size > m_iKiloByte) {
1457 size /= m_iKiloByte;
1458 dSize /= (double) m_iKiloByte;
1459 i++;
1460 }
1461 if (i > 8)
1462 return QString();
1463
1464 QString strResult(QString::number(dSize, 'f', 2));
1465 strResult += units[i];
1466 return strResult;
1467}
1468
1469void UIGuestControlFileTable::continueWithMove(const QUuid &progressId)
1470{
1471 deleteByPath(m_deleteAfterCopyCache.value(progressId, QStringList()));
1472}
1473
1474void UIGuestControlFileTable::sltReceiveDirectoryStatistics(UIDirectoryStatistics statistics)
1475{
1476 if (!m_pPropertiesDialog)
1477 return;
1478 m_pPropertiesDialog->addDirectoryStatistics(statistics);
1479}
1480
1481QModelIndex UIGuestControlFileTable::currentRootIndex() const
1482{
1483 if (!m_pView)
1484 return QModelIndex();
1485 if (!m_pProxyModel)
1486 return m_pView->rootIndex();
1487 return m_pProxyModel->mapToSource(m_pView->rootIndex());
1488}
1489
1490void UIGuestControlFileTable::performSelectionSearch(const QString &strSearchText)
1491{
1492 if (!m_pProxyModel | !m_pView || strSearchText.isEmpty())
1493 return;
1494
1495 int rowCount = m_pProxyModel->rowCount(m_pView->rootIndex());
1496 UIFileTableItem *pFoundItem = 0;
1497 QModelIndex index;
1498 for (int i = 0; i < rowCount && !pFoundItem; ++i)
1499 {
1500 index = m_pProxyModel->index(i, 0, m_pView->rootIndex());
1501 if (!index.isValid())
1502 continue;
1503 pFoundItem = static_cast<UIFileTableItem*>(m_pProxyModel->mapToSource(index).internalPointer());
1504 if (!pFoundItem)
1505 continue;
1506 const QString &strName = pFoundItem->name();
1507 if (!strName.startsWith(m_pSearchLineEdit->text(), Qt::CaseInsensitive))
1508 pFoundItem = 0;
1509 }
1510 if (pFoundItem)
1511 {
1512 /* Deselect anything that is already selected: */
1513 m_pView->clearSelection();
1514 setSelection(index);
1515 }
1516}
1517
1518void UIGuestControlFileTable::disableSelectionSearch()
1519{
1520 if (!m_pSearchLineEdit)
1521 return;
1522 m_pSearchLineEdit->blockSignals(true);
1523 m_pSearchLineEdit->clear();
1524 m_pSearchLineEdit->hide();
1525 m_pSearchLineEdit->blockSignals(false);
1526}
1527
1528bool UIGuestControlFileTable::checkIfDeleteOK()
1529{
1530 UIGuestControlFileManagerSettings *pFileManagerSettings = UIGuestControlFileManagerSettings::instance();
1531 if (!pFileManagerSettings)
1532 return true;
1533 if (!pFileManagerSettings->bAskDeleteConfirmation)
1534 return true;
1535 UIFileDeleteConfirmationDialog *pDialog =
1536 new UIFileDeleteConfirmationDialog(this);
1537
1538
1539 bool fContinueWithDelete = (pDialog->execute() == QDialog::Accepted);
1540
1541 bool bAskNextTime = pDialog->askDeleteConfirmationNextTime();
1542
1543 /* Update the file manager settings only if it is necessary: */
1544 if (pFileManagerSettings->bAskDeleteConfirmation != bAskNextTime)
1545 {
1546 pFileManagerSettings->bAskDeleteConfirmation = bAskNextTime;
1547 /* Notify file manager settings panel so that the check box there is updated: */
1548 emit sigDeleteConfirmationOptionChanged();
1549 }
1550
1551 delete pDialog;
1552
1553 return fContinueWithDelete;
1554
1555}
1556
1557#include "UIGuestControlFileTable.moc"
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette