Changeset 71255 in vbox
- Timestamp:
- Mar 7, 2018 12:02:39 PM (7 years ago)
- Location:
- trunk/src/VBox/Frontends/VirtualBox/src/runtime/information/guestctrl
- Files:
-
- 3 edited
-
UIGuestControlFileManager.cpp (modified) (1 diff)
-
UIGuestControlFileTable.cpp (modified) (32 diffs)
-
UIGuestControlFileTable.h (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Frontends/VirtualBox/src/runtime/information/guestctrl/UIGuestControlFileManager.cpp
r71234 r71255 447 447 void UIGuestControlFileManager::sltGuestSessionStateChanged(const CGuestSessionStateChangedEvent &cEvent) 448 448 { 449 if (cEvent.isOk() && m_comGuestSession.isOk())449 if (cEvent.isOk() /*&& m_comGuestSession.isOk()*/) 450 450 { 451 451 CVirtualBoxErrorInfo cErrorInfo = cEvent.GetError(); -
trunk/src/VBox/Frontends/VirtualBox/src/runtime/information/guestctrl/UIGuestControlFileTable.cpp
r71234 r71255 30 30 # include "QILabel.h" 31 31 # include "QILineEdit.h" 32 # include "UIErrorString.h" 32 33 # include "UIIconPool.h" 33 34 # include "UIGuestControlFileTable.h" … … 59 60 { 60 61 public: 61 explicit UIFileTableItem(const QList<QVariant> &data, UIFileTableItem *parentItem = 0);62 explicit UIFileTableItem(const QList<QVariant> &data, bool isDirectory, UIFileTableItem *parentItem); 62 63 ~UIFileTableItem(); 63 64 64 65 void appendChild(UIFileTableItem *child); 65 66 66 UIFileTableItem *child(int row); 67 UIFileTableItem *child(int row) const; 68 /** Return a child (if possible) by path */ 69 UIFileTableItem *child(const QString &path) const; 67 70 int childCount() const; 68 71 int columnCount() const; 69 72 QVariant data(int column) const; 73 void setData(const QVariant &data, int index); 70 74 int row() const; 71 75 UIFileTableItem *parentItem(); … … 73 77 bool isDirectory() const; 74 78 bool isOpened() const; 75 76 void setIsDirectory(bool flag);77 79 void setIsOpened(bool flag); 78 80 79 81 const QString &path() const; 80 82 void setPath(const QString &path); 81 82 const QString &name() const; 83 void setName(const QString &name); 83 /** Merge prefix and suffix by making sure they have a single '/' in between */ 84 void setPath(const QString &prexix, const QString &suffix); 84 85 85 86 /** True if this is directory and name is ".." */ … … 88 89 89 90 private: 90 QList<UIFileTableItem*> m_childItems; 91 QList<UIFileTableItem*> m_childItems; 92 /** Used to find children by path */ 93 QMap<QString, UIFileTableItem*> m_childMap; 91 94 QList<QVariant> m_itemData; 92 95 UIFileTableItem *m_parentItem; 93 96 bool m_bIsDirectory; 94 97 bool m_bIsOpened; 98 /** Full absolute path of the item. Without the trailing '/' */ 95 99 QString m_strPath; 96 QString m_strName; 100 /** For directories base name is the name of the lowest level directory 101 in strPath. eg. for 'm_strPath = /opt/qt5.6/examples' 'm_strBaseName = examples' 102 for files it is the name of the file */ 103 QString m_strBaseName; 97 104 }; 98 105 99 106 100 UIFileTableItem::UIFileTableItem(const QList<QVariant> &data, UIFileTableItem *parent)107 UIFileTableItem::UIFileTableItem(const QList<QVariant> &data, bool isDirectory, UIFileTableItem *parent) 101 108 : m_itemData(data) 102 109 , m_parentItem(parent) 103 , m_bIsDirectory( false)110 , m_bIsDirectory(isDirectory) 104 111 , m_bIsOpened(false) 105 112 { … … 115 122 void UIFileTableItem::appendChild(UIFileTableItem *item) 116 123 { 124 if (!item) 125 return; 117 126 m_childItems.append(item); 118 } 119 120 UIFileTableItem *UIFileTableItem::child(int row) 127 m_childMap.insert(item->path(), item); 128 } 129 130 UIFileTableItem *UIFileTableItem::child(int row) const 121 131 { 122 132 return m_childItems.value(row); 123 133 } 124 134 135 UIFileTableItem *UIFileTableItem::child(const QString &path) const 136 { 137 if (!m_childMap.contains(path)) 138 return 0; 139 return m_childMap.value(path); 140 } 141 125 142 int UIFileTableItem::childCount() const 126 143 { … … 136 153 { 137 154 return m_itemData.value(column); 155 } 156 157 void UIFileTableItem::setData(const QVariant &data, int index) 158 { 159 if (index >= m_itemData.length()) 160 return; 161 m_itemData[index] = data; 138 162 } 139 163 … … 160 184 qDeleteAll(m_childItems); 161 185 m_childItems.clear(); 186 m_childMap.clear(); 162 187 } 163 188 … … 167 192 } 168 193 169 void UIFileTableItem::setIsDirectory(bool flag)170 {171 m_bIsDirectory = flag;172 }173 174 194 void UIFileTableItem::setIsOpened(bool flag) 175 195 { … … 184 204 void UIFileTableItem::setPath(const QString &path) 185 205 { 206 if (path.isNull() || path.isEmpty()) 207 return; 186 208 m_strPath = path; 187 } 188 189 const QString &UIFileTableItem::name() const 190 { 191 return m_strName; 192 } 193 194 void UIFileTableItem::setName(const QString &name) 195 { 196 m_strName = name; 209 /* Make sure for we dont have any trailing slashes: */ 210 if (m_strPath.length() > 1 && m_strPath.at(m_strPath.length() - 1) == QChar('/')) 211 m_strPath.chop(1); 212 } 213 214 void UIFileTableItem::setPath(const QString &prefix, const QString &suffix) 215 { 216 if (prefix.isEmpty()) 217 return; 218 QString newPath(prefix); 219 /* Make sure we have a trailing '/' in @p prefix: */ 220 if (prefix.at(newPath.length() - 1) != QChar('/')) 221 newPath += "/"; 222 newPath += suffix; 223 setPath(newPath); 197 224 } 198 225 … … 243 270 } 244 271 272 bool UIGuestControlFileModel::setData(const QModelIndex &index, const QVariant &value, int role) 273 { 274 if (index.isValid() && role == Qt::EditRole) 275 { 276 UIFileTableItem *item = static_cast<UIFileTableItem*>(index.internalPointer()); 277 if (!item || !m_pParent) 278 return false; 279 if (m_pParent->renameItem(item, value.toString())) 280 { 281 item->setData(value, index.column()); 282 emit dataChanged(index, index); 283 } 284 else 285 { 286 if (m_pParent) 287 m_pParent->emitLogOutput(QString("cannot rename %1").arg(item->path())); 288 } 289 return true; 290 } 291 return false; 292 } 293 245 294 QVariant UIGuestControlFileModel::data(const QModelIndex &index, int role) const 246 295 { … … 251 300 return QVariant(); 252 301 253 if (role == Qt::DisplayRole )302 if (role == Qt::DisplayRole || role == Qt::EditRole) 254 303 { 255 304 /* dont show anything but the name for up directories: */ … … 279 328 if (!index.isValid()) 280 329 return 0; 281 330 UIFileTableItem *item = static_cast<UIFileTableItem*>(index.internalPointer()); 331 if (!item) 332 return QAbstractItemModel::flags(index); 333 334 if (!item->isUpDirectory() && index.column() == 0) 335 return QAbstractItemModel::flags(index) | Qt::ItemIsEditable; 282 336 return QAbstractItemModel::flags(index); 283 337 } … … 294 348 } 295 349 return QVariant(); 350 } 351 352 QModelIndex UIGuestControlFileModel::index(UIFileTableItem* item) 353 { 354 if (!item) 355 return QModelIndex(); 356 return createIndex(item->row(), 0, item); 296 357 } 297 358 … … 382 443 , m_pTree(0) 383 444 , m_pLocationLabel(0) 445 , m_pGoHome(0) 384 446 , m_pMainLayout(0) 385 447 , m_pCurrentLocationEdit(0) 386 448 , m_pToolBar(0) 387 449 , m_pGoUp(0) 388 , m_pGoHome(0)389 450 , m_pRefresh(0) 390 451 , m_pDelete(0) … … 417 478 } 418 479 480 void UIGuestControlFileTable::emitLogOutput(const QString& strOutput) 481 { 482 emit sigLogOutput(strOutput); 483 } 484 419 485 void UIGuestControlFileTable::prepareObjects() 420 486 { … … 460 526 m_pView->setModel(m_pModel); 461 527 m_pView->setItemDelegate(new UIFileDelegate); 528 m_pView->setEditTriggers(QAbstractItemView::NoEditTriggers); 462 529 /* Minimize the row height: */ 463 530 m_pView->verticalHeader()->setDefaultSectionSize(m_pView->verticalHeader()->minimumSectionSize()); … … 565 632 QList<QVariant> headData; 566 633 headData << "Name" << "Size"; 567 m_pRootItem = new UIFileTableItem(headData );634 m_pRootItem = new UIFileTableItem(headData, true, 0); 568 635 569 636 QList<QVariant> startDirData; 570 637 startDirData << "/" << 4096; 571 UIFileTableItem* startItem = new UIFileTableItem(startDirData, m_pRootItem);638 UIFileTableItem* startItem = new UIFileTableItem(startDirData, true, m_pRootItem); 572 639 startItem->setPath("/"); 573 startItem->setName("/");574 640 m_pRootItem->appendChild(startItem); 575 641 576 startItem->setIsDirectory(true);577 642 startItem->setIsOpened(false); 578 643 /* Read the root directory and get the list: */ … … 594 659 QList<QVariant> data; 595 660 data << ".." << 4096; 596 UIFileTableItem *item = new UIFileTableItem(data, parent); 597 item->setIsDirectory(true); 661 UIFileTableItem *item = new UIFileTableItem(data, isDirectoryMap, parent); 598 662 item->setIsOpened(false); 599 663 map.insert("..", item); … … 628 692 return; 629 693 if (currentRoot != m_pModel->rootIndex()) 630 m_pView->setRootIndex(currentRoot.parent());694 changeLocation(currentRoot.parent()); 631 695 } 632 696 633 697 void UIGuestControlFileTable::sltGoHome() 634 698 { 699 goToHomeDirectory(); 635 700 } 636 701 … … 645 710 if (!item) 646 711 return; 712 647 713 /* check if we need to go up: */ 648 714 if (item->isUpDirectory()) … … 658 724 if (!item->isOpened()) 659 725 readDirectory(item->path(),item); 660 661 726 changeLocation(itemIndex); 662 727 } 663 728 729 void UIGuestControlFileTable::goIntoDirectory(const QVector<QString> &pathTrail) 730 { 731 UIFileTableItem *parent = getStartDirectoryItem(); 732 733 for(int i = 0; i < pathTrail.size(); ++i) 734 { 735 if (!parent) 736 return; 737 /* Make sure parent is already opened: */ 738 if (!parent->isOpened()) 739 readDirectory(parent->path(), parent, parent == getStartDirectoryItem()); 740 /* search the current path item among the parent's children: */ 741 UIFileTableItem *item = parent->child(pathTrail.at(i)); 742 if (!item) 743 return; 744 parent = item; 745 } 746 if (!parent) 747 return; 748 if (!parent->isOpened()) 749 readDirectory(parent->path(), parent, parent == getStartDirectoryItem()); 750 goIntoDirectory(parent); 751 } 752 753 void UIGuestControlFileTable::goIntoDirectory(UIFileTableItem *item) 754 { 755 if (!item || !m_pModel) 756 return; 757 goIntoDirectory(m_pModel->index(item)); 758 } 759 760 UIFileTableItem* UIGuestControlFileTable::indexData(const QModelIndex &index) const 761 { 762 if (!index.isValid()) 763 return 0; 764 return static_cast<UIFileTableItem*>(index.internalPointer()); 765 } 766 664 767 void UIGuestControlFileTable::refresh() 665 768 { … … 667 770 return; 668 771 QModelIndex currentIndex = m_pView->rootIndex(); 669 if (!currentIndex.isValid()) 670 return; 671 UIFileTableItem *treeItem = 672 static_cast<UIFileTableItem*>(currentIndex.internalPointer()); 772 773 UIFileTableItem *treeItem = indexData(currentIndex); 673 774 if (!treeItem) 674 775 return; … … 700 801 void UIGuestControlFileTable::sltRename() 701 802 { 803 if (!m_pView) 804 return; 805 QItemSelectionModel *selectionModel = m_pView->selectionModel(); 806 if (!selectionModel) 807 return; 808 809 QModelIndexList selectedItemIndices = selectionModel->selectedRows(); 810 if (selectedItemIndices.size() == 0) 811 return; 812 UIFileTableItem *item = indexData(selectedItemIndices.at(0)); 813 if (!item || item->isUpDirectory()) 814 return; 815 m_pView->edit(selectedItemIndices.at(0)); 816 702 817 } 703 818 704 819 void UIGuestControlFileTable::deleteByIndex(const QModelIndex &itemIndex) 705 820 { 706 if (!itemIndex.isValid()) 707 return; 708 UIFileTableItem *treeItem = static_cast<UIFileTableItem*>(itemIndex.internalPointer()); 821 UIFileTableItem *treeItem = indexData(itemIndex); 709 822 if (!treeItem) 710 823 return; … … 723 836 if (m_pGoHome) 724 837 { 725 m_pGoHome->setText(UIVMInformationDialog::tr("Go to home folder"));726 m_pGoHome->setToolTip(UIVMInformationDialog::tr("Go to home folder"));727 m_pGoHome->setStatusTip(UIVMInformationDialog::tr("Go to home folder"));838 m_pGoHome->setText(UIVMInformationDialog::tr("Go to home folder")); 839 m_pGoHome->setToolTip(UIVMInformationDialog::tr("Go to home folder")); 840 m_pGoHome->setStatusTip(UIVMInformationDialog::tr("Go to home folder")); 728 841 } 729 842 … … 805 918 } 806 919 807 920 UIFileTableItem *UIGuestControlFileTable::getStartDirectoryItem() 921 { 922 if (!m_pRootItem) 923 return 0; 924 if (m_pRootItem->childCount() <= 0) 925 return 0; 926 return m_pRootItem->child(0); 927 } 928 929 QString UIGuestControlFileTable::constructNewItemPath(const QString &previousPath, const QString &newBaseName) 930 { 931 if (newBaseName.isEmpty() || previousPath.length() <= 1) 932 return QString(); 933 934 QStringList pathList = previousPath.split('/', QString::SkipEmptyParts); 935 QString newPath("/"); 936 for(int i = 0; i < pathList.size() - 1; ++i) 937 { 938 newPath += (pathList.at(i) + "/"); 939 } 940 newPath += newBaseName; 941 return newPath; 942 } 808 943 /********************************************************************************************************************************* 809 944 * UIGuestFileTable implementation. * … … 813 948 :UIGuestControlFileTable(pParent) 814 949 { 950 configureObjects(); 815 951 retranslateUi(); 816 952 } … … 859 995 QList<QVariant> data; 860 996 data << fsInfo.GetName() << static_cast<qulonglong>(fsInfo.GetObjectSize()); 861 UIFileTableItem *item = new UIFileTableItem(data, parent); 862 item->setPath(QString("%1%3%2").arg(strPath).arg("/").arg(fsInfo.GetName())); 863 if (fsInfo.GetType() == KFsObjType_Directory) 997 bool isDirectory = (fsInfo.GetType() == KFsObjType_Directory); 998 UIFileTableItem *item = new UIFileTableItem(data, isDirectory, parent); 999 1000 item->setPath(strPath, fsInfo.GetName()); 1001 if (isDirectory) 864 1002 { 865 1003 directories.insert(fsInfo.GetName(), item); 866 item->setIsDirectory(true);867 1004 item->setIsOpened(false); 868 1005 } … … 870 1007 { 871 1008 files.insert(fsInfo.GetName(), item); 872 item->setIsDirectory(false);873 1009 item->setIsOpened(false); 874 1010 } … … 896 1032 } 897 1033 else 898 {899 /** @todo this is not working: */900 1034 m_comGuestSession.FsObjRemove(item->path()); 901 } 902 } 903 1035 } 1036 1037 void UIGuestFileTable::goToHomeDirectory() 1038 { 1039 /* TODO: not implemented in guest control yet: */ 1040 } 1041 1042 bool UIGuestFileTable::renameItem(UIFileTableItem *item, QString newBaseName) 1043 { 1044 1045 if (!item || item->isUpDirectory() || newBaseName.isEmpty() || !m_comGuestSession.isOk()) 1046 return false; 1047 QString newPath = constructNewItemPath(item->path(), newBaseName); 1048 QVector<KFsObjRenameFlag> aFlags(KFsObjRenameFlag_Replace); 1049 1050 m_comGuestSession.FsObjRename(item->path(), newPath, aFlags); 1051 if (m_comGuestSession.isOk()) 1052 return false; 1053 return true; 1054 } 1055 1056 void UIGuestFileTable::configureObjects() 1057 { 1058 if (m_pGoHome) 1059 m_pGoHome->setEnabled(false); 1060 } 904 1061 905 1062 /********************************************************************************************************************************* … … 940 1097 QList<QVariant> data; 941 1098 data << fileInfo.baseName() << fileInfo.size(); 942 UIFileTableItem *item = new UIFileTableItem(data, parent);1099 UIFileTableItem *item = new UIFileTableItem(data, fileInfo.isDir(), parent); 943 1100 item->setPath(fileInfo.absoluteFilePath()); 944 1101 if (fileInfo.isDir()) 945 1102 { 946 1103 directories.insert(fileInfo.baseName(), item); 947 item->setIsDirectory(true);948 1104 item->setIsOpened(false); 949 1105 } … … 951 1107 { 952 1108 files.insert(fileInfo.baseName(), item); 953 item->setIsDirectory(false);954 1109 item->setIsOpened(false); 955 1110 } … … 981 1136 } 982 1137 1138 void UIHostFileTable::goToHomeDirectory() 1139 { 1140 if(!m_pRootItem || m_pRootItem->childCount() <= 0) 1141 return; 1142 UIFileTableItem *startDirItem = m_pRootItem->child(0); 1143 if (!startDirItem) 1144 return; 1145 1146 // UIFileTableItem *rootDirectoryItem 1147 QDir homeDirectory(QDir::homePath()); 1148 QVector<QString> pathTrail;//(QDir::rootPath()); 1149 do{ 1150 1151 pathTrail.push_front(homeDirectory.absolutePath()); 1152 homeDirectory.cdUp(); 1153 }while(!homeDirectory.isRoot()); 1154 1155 goIntoDirectory(pathTrail); 1156 } 1157 1158 bool UIHostFileTable::renameItem(UIFileTableItem *item, QString newPath) 1159 { 1160 Q_UNUSED(item); 1161 return true; 1162 } 983 1163 #include "UIGuestControlFileTable.moc" -
trunk/src/VBox/Frontends/VirtualBox/src/runtime/information/guestctrl/UIGuestControlFileTable.h
r71234 r71255 57 57 58 58 QVariant data(const QModelIndex &index, int role) const /* override */; 59 bool setData(const QModelIndex &index, const QVariant &value, int role); 60 59 61 Qt::ItemFlags flags(const QModelIndex &index) const /* override */; 60 62 QVariant headerData(int section, Qt::Orientation orientation, 61 63 int role = Qt::DisplayRole) const /* override */; 62 64 QModelIndex index(int row, int column, 63 const QModelIndex &parent = QModelIndex()) const /* override */; 65 const QModelIndex &parent = QModelIndex()) const /* override */; 66 QModelIndex index(UIFileTableItem* item); 64 67 QModelIndex parent(const QModelIndex &index) const /* override */; 65 68 int rowCount(const QModelIndex &parent = QModelIndex()) const /* override */; … … 97 100 /** Delete all the tree nodes */ 98 101 void reset(); 102 void emitLogOutput(const QString& strOutput); 99 103 100 104 protected: … … 109 113 virtual void refresh(); 110 114 virtual void deleteByItem(UIFileTableItem *item) = 0; 115 virtual void goToHomeDirectory() = 0; 116 virtual bool renameItem(UIFileTableItem *item, QString newPath) = 0; 117 void goIntoDirectory(const QModelIndex &itemIndex); 118 /** Follow the path trail, open directories as we go and descend */ 119 void goIntoDirectory(const QVector<QString> &pathTrail); 120 /** Go into directory pointed by the @p item */ 121 void goIntoDirectory(UIFileTableItem *item); 122 UIFileTableItem* indexData(const QModelIndex &index) const; 111 123 void keyPressEvent(QKeyEvent * pEvent); 124 125 /** Replace the last part of the @p previusPath with newBaseName */ 126 QString constructNewItemPath(const QString &previousPath, const QString &newBaseName); 127 112 128 UIFileTableItem *m_pRootItem; 113 129 … … 119 135 QTreeView *m_pTree; 120 136 QILabel *m_pLocationLabel; 121 137 QAction *m_pGoHome; 122 138 protected slots: 123 139 … … 131 147 private: 132 148 133 void prepareObjects(); 134 void prepareActions(); 135 void deleteByIndex(const QModelIndex &itemIndex); 136 void goIntoDirectory(const QModelIndex &itemIndex); 137 QGridLayout *m_pMainLayout; 138 QILineEdit *m_pCurrentLocationEdit; 139 UIToolBar *m_pToolBar; 140 QAction *m_pGoUp; 141 QAction *m_pGoHome; 142 QAction *m_pRefresh; 143 QAction *m_pDelete; 144 QAction *m_pRename; 145 QAction *m_pNewFolder; 146 147 QAction *m_pCopy; 148 QAction *m_pCut; 149 QAction *m_pPaste; 149 void prepareObjects(); 150 void prepareActions(); 151 void deleteByIndex(const QModelIndex &itemIndex); 152 /** Return the UIFileTableItem for path / which is a direct (and single) child of m_pRootItem */ 153 UIFileTableItem *getStartDirectoryItem(); 154 155 QGridLayout *m_pMainLayout; 156 QILineEdit *m_pCurrentLocationEdit; 157 UIToolBar *m_pToolBar; 158 QAction *m_pGoUp; 159 160 QAction *m_pRefresh; 161 QAction *m_pDelete; 162 QAction *m_pRename; 163 QAction *m_pNewFolder; 164 165 QAction *m_pCopy; 166 QAction *m_pCut; 167 QAction *m_pPaste; 150 168 151 169 friend class UIGuestControlFileModel; … … 168 186 virtual void readDirectory(const QString& strPath, UIFileTableItem *parent, bool isStartDir = false) /* override */; 169 187 virtual void deleteByItem(UIFileTableItem *item) /* override */; 188 virtual void goToHomeDirectory() /* override */; 189 virtual bool renameItem(UIFileTableItem *item, QString newPath); 170 190 171 191 private: 172 192 193 void configureObjects(); 173 194 CGuestSession m_comGuestSession; 195 174 196 }; 175 197 … … 189 211 virtual void readDirectory(const QString& strPath, UIFileTableItem *parent, bool isStartDir = false) /* override */; 190 212 virtual void deleteByItem(UIFileTableItem *item) /* override */; 213 virtual void goToHomeDirectory() /* override */; 214 virtual bool renameItem(UIFileTableItem *item, QString newPath); 191 215 }; 192 216
Note:
See TracChangeset
for help on using the changeset viewer.

