VirtualBox

Changeset 71298 in vbox for trunk


Ignore:
Timestamp:
Mar 12, 2018 9:23:17 AM (7 years ago)
Author:
vboxsync
Message:

FE/Qt bugref:6699 Factor-out some utility functions for path string manipulation

Location:
trunk/src/VBox/Frontends/VirtualBox/src/runtime/guestctrl
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Frontends/VirtualBox/src/runtime/guestctrl/UIGuestControlFileManager.cpp

    r71276 r71298  
    261261    if (m_pCopyGuestToHost)
    262262    {
    263         m_pCopyGuestToHost->setText(UIVMInformationDialog::tr("Copy the selected object from guest to host"));
    264         m_pCopyGuestToHost->setToolTip(UIVMInformationDialog::tr("Copy the selected object from guest to host"));
    265         m_pCopyGuestToHost->setStatusTip(UIVMInformationDialog::tr("Copy the selected object from guest to host"));
     263        m_pCopyGuestToHost->setText(UIVMInformationDialog::tr("Copy the selected object(s) from guest to host"));
     264        m_pCopyGuestToHost->setToolTip(UIVMInformationDialog::tr("Copy the selected object(s) from guest to host"));
     265        m_pCopyGuestToHost->setStatusTip(UIVMInformationDialog::tr("Copy the selected object(s) from guest to host"));
    266266    }
    267267
    268268    if (m_pCopyHostToGuest)
    269269    {
    270         m_pCopyHostToGuest->setText(UIVMInformationDialog::tr("Copy the selected object from host to guest"));
    271         m_pCopyHostToGuest->setToolTip(UIVMInformationDialog::tr("Copy the selected object from host to guest"));
    272         m_pCopyHostToGuest->setStatusTip(UIVMInformationDialog::tr("Copy the selected object from host to guest"));
     270        m_pCopyHostToGuest->setText(UIVMInformationDialog::tr("Copy the selected object(s) from host to guest"));
     271        m_pCopyHostToGuest->setToolTip(UIVMInformationDialog::tr("Copy the selected object(s) from host to guest"));
     272        m_pCopyHostToGuest->setStatusTip(UIVMInformationDialog::tr("Copy the selected object(s) from host to guest"));
    273273    }
    274274
     
    485485    if (!m_pGuestFileTable || !m_pHostFileTable)
    486486        return;
    487     QString hostDestinationPath = m_pHostFileTable->currentPath();
     487    QString hostDestinationPath = m_pHostFileTable->currentDirectoryPath();
    488488    printf("current host path %s\n", hostDestinationPath.toStdString().c_str());
    489489    m_pGuestFileTable->copyGuestToHost(hostDestinationPath);
  • trunk/src/VBox/Frontends/VirtualBox/src/runtime/guestctrl/UIGuestControlFileTable.cpp

    r71285 r71298  
    4343/* COM includes: */
    4444# include "CFsObjInfo.h"
     45# include "CGuestFsObjInfo.h"
    4546# include "CGuestDirectory.h"
    4647# include "CProgress.h"
    4748
    4849#endif /* !VBOX_WITH_PRECOMPILED_HEADERS */
     50
     51/** A collection of utility functions for some path string manipulations */
     52class UIPathOperations
     53{
     54public:
     55    static QString removeMultipleDelimiters(const QString &path);
     56    static QString removeTrailingDelimiters(const QString &path);
     57    static QString addStartDelimiter(const QString &path);
     58    static QString removeAllDelimiters(const QString &path);
     59
     60    //    static QString removeTrailingDelimiters(const QString &path);
     61    static QString sanitize(const QString &path);
     62    /** Merge prefix and suffix by making sure they have a single '/' in between */
     63    static QString mergePaths(const QString &path, const QString &baseName);
     64    /** Returns the last part of the @p path. That is the filename or directory name without the path */
     65    static QString getObjectName(const QString &path);
     66    /** Remove the object name and return the path */
     67    static QString getPathExceptObjectName(const QString &path);
     68    /** Replace the last part of the @p previusPath with newBaseName */
     69    static QString constructNewItemPath(const QString &previousPath, const QString &newBaseName);
     70
     71    static const QChar delimiter;
     72};
     73
     74const QChar UIPathOperations::delimiter = QChar('/');
     75
     76QString UIPathOperations::removeMultipleDelimiters(const QString &path)
     77{
     78    QString newPath(path);
     79    QString doubleDelimiter(2, delimiter);
     80
     81    while (newPath.contains(doubleDelimiter) && !newPath.isEmpty())
     82        newPath = newPath.replace(doubleDelimiter, delimiter);
     83    return newPath;
     84}
     85
     86QString UIPathOperations::removeTrailingDelimiters(const QString &path)
     87{
     88    if (path.isNull() || path.isEmpty())
     89        return QString();
     90    QString newPath(path);
     91    /* Make sure for we dont have any trailing slashes: */
     92    while (newPath.length() > 1 && newPath.at(newPath.length() - 1) == UIPathOperations::delimiter)
     93        newPath.chop(1);
     94    return newPath;
     95}
     96
     97QString UIPathOperations::addStartDelimiter(const QString &path)
     98{
     99    if (path.isEmpty())
     100        return QString(path);
     101    QString newPath(path);
     102    if (newPath.at(0) != delimiter)
     103        newPath.insert(0, delimiter);
     104    return newPath;
     105}
     106
     107QString UIPathOperations::sanitize(const QString &path)
     108{
     109    return addStartDelimiter(removeTrailingDelimiters(removeMultipleDelimiters(path)));
     110}
     111
     112QString UIPathOperations::mergePaths(const QString &path, const QString &baseName)
     113{
     114    QString newBase(baseName);
     115    newBase = newBase.remove(delimiter);
     116
     117    /* make sure we have one and only one trailing '/': */
     118    QString newPath(sanitize(path));
     119    if(newPath.isEmpty())
     120        newPath = delimiter;
     121    if(newPath.at(newPath.length() - 1) != delimiter)
     122        newPath += UIPathOperations::delimiter;
     123    newPath += newBase;
     124    return sanitize(newPath);
     125}
     126
     127QString UIPathOperations::getObjectName(const QString &path)
     128{
     129    if (path.length() <= 1)
     130        return QString(path);
     131
     132    QString strTemp(sanitize(path));
     133    if (strTemp.length() < 2)
     134        return strTemp;
     135    int lastSlashPosition = strTemp.lastIndexOf(UIPathOperations::delimiter);
     136    if (lastSlashPosition == -1)
     137        return QString();
     138    return strTemp.right(strTemp.length() - lastSlashPosition - 1);
     139}
     140
     141QString UIPathOperations::getPathExceptObjectName(const QString &path)
     142{
     143    if (path.length() <= 1)
     144        return QString(path);
     145
     146    QString strTemp(sanitize(path));
     147    int lastSlashPosition = strTemp.lastIndexOf(UIPathOperations::delimiter);
     148    if (lastSlashPosition == -1)
     149        return QString();
     150    return strTemp.left(lastSlashPosition + 1);
     151}
     152
     153QString UIPathOperations::constructNewItemPath(const QString &previousPath, const QString &newBaseName)
     154{
     155    if (previousPath.length() <= 1)
     156         return QString(previousPath);
     157    return sanitize(mergePaths(getPathExceptObjectName(previousPath), newBaseName));
     158}
    49159
    50160
     
    128238    , m_bIsOpened(false)
    129239{
    130 
    131240}
    132241
     
    224333        return;
    225334    m_strPath = path;
    226     /* Make sure for we dont have any trailing slashes: */
    227     if (m_strPath.length() > 1 && m_strPath.at(m_strPath.length() - 1) == QChar('/'))
    228         m_strPath.chop(1);
    229 }
    230 
    231 void UIFileTableItem::setPath(const QString &prefix, const QString &suffix)
    232 {
    233     if (prefix.isEmpty())
    234         return;
    235     QString newPath(prefix);
    236     /* Make sure we have a trailing '/' in @p prefix: */
    237     if (prefix.at(newPath.length() - 1) != QChar('/'))
    238         newPath += "/";
    239     newPath += suffix;
    240     setPath(newPath);
     335    UIPathOperations::removeTrailingDelimiters(m_strPath);
    241336}
    242337
     
    785880}
    786881
    787 QString UIGuestControlFileTable::constructNewItemPath(const QString &previousPath, const QString &newBaseName)
    788 {
    789     if (newBaseName.isEmpty() || previousPath.length() <= 1)
    790         return QString();
    791 
    792     QStringList pathList = previousPath.split('/', QString::SkipEmptyParts);
    793     QString newPath("/");
    794     for(int i = 0; i < pathList.size() - 1; ++i)
    795     {
    796         newPath += (pathList.at(i) + "/");
    797     }
    798     newPath += newBaseName;
    799     return newPath;
    800 }
    801 
    802 QString UIGuestControlFileTable::mergePaths(const QString &path, const QString &baseName)
    803 {
    804     QString newPath(path);
    805     /* make sure we have a trailing '/': */
    806     if (newPath.at(newPath.length() - 1) != QChar('/'))
    807         newPath += QChar('/');
    808     newPath += baseName;
    809     return newPath;
    810 }
    811882
    812883QString UIGuestControlFileTable::getNewDirectoryName()
     
    823894}
    824895
    825 QString UIGuestControlFileTable::currentPath() const
     896QString UIGuestControlFileTable::currentDirectoryPath() const
    826897{
    827898    if (!m_pView)
     
    833904    if (!item)
    834905        return QString();
     906    /* be paranoid: */
     907    if (!item->isDirectory())
     908        return QString();
    835909    return item->path();
    836910}
     
    898972    directory = m_comGuestSession.DirectoryOpen(strPath, /*aFilter*/ "", flag);
    899973    parent->setIsOpened(true);
    900 
    901974    if (directory.isOk())
    902975    {
     
    913986            bool isDirectory = (fsInfo.GetType() == KFsObjType_Directory);
    914987            UIFileTableItem *item = new UIFileTableItem(data, isDirectory, parent);
    915             item->setPath(strPath, fsInfo.GetName());
     988            item->setPath(UIPathOperations::mergePaths(strPath, fsInfo.GetName()));
    916989            if (isDirectory)
    917990            {
     
    9301003        updateCurrentLocationEdit(strPath);
    9311004    }
     1005    directory.Close();
    9321006}
    9331007
     
    9631037    if (!item || item->isUpDirectory() || newBaseName.isEmpty() || !m_comGuestSession.isOk())
    9641038        return false;
    965     QString newPath = constructNewItemPath(item->path(), newBaseName);
     1039    QString newPath = UIPathOperations::constructNewItemPath(item->path(), newBaseName);
    9661040    QVector<KFsObjRenameFlag> aFlags(KFsObjRenameFlag_Replace);
    9671041
     
    9841058        return false;
    9851059
    986     QString newDirectoryPath = mergePaths(path, directoryName);
     1060    QString newDirectoryPath = UIPathOperations::mergePaths(path, directoryName);
    9871061    QVector<KDirectoryCreateFlag> flags(KDirectoryCreateFlag_None);
    9881062
     
    9991073void UIGuestFileTable::copyGuestToHost(const QString& hostDestinationPath)
    10001074{
    1001     Q_UNUSED(hostDestinationPath);
     1075    QStringList selectedPathList = selectedItemPathList();
     1076    for (int i = 0; i < selectedPathList.size(); ++i)
     1077        copyGuestToHost(selectedPathList.at(i), hostDestinationPath);
    10021078}
    10031079
    10041080void UIGuestFileTable::copyHostToGuest(const QStringList &hostSourcePathList)
    10051081{
    1006     Q_UNUSED(hostSourcePathList);
     1082    for (int i = 0; i < hostSourcePathList.size(); ++i)
     1083        copyHostToGuest(hostSourcePathList.at(i), currentDirectoryPath());
     1084}
     1085
     1086bool UIGuestFileTable::copyGuestToHost(const QString &guestSourcePath, const QString& hostDestinationPath)
     1087{
     1088    if (m_comGuestSession.isNull())
     1089        return false;
     1090
     1091    /* Currently API expects a path including a file name for file copy*/
     1092    KFsObjType objectType = fsObjectType(guestSourcePath);
     1093    if (objectType == KFsObjType_File)
     1094    {
     1095        // QString destinatioFilePath =  mergePaths(hostDestinationPath, const QString &Path, const QString &baseName);
     1096
     1097    }
     1098
     1099    QVector<KDirectoryCopyFlag> aFlags(KDirectoryCopyFlag_CopyIntoExisting);
     1100    /** @todo listen to CProgress object to monitor copy operation: */
     1101    /*CProgress comProgress = */m_comGuestSession.DirectoryCopyFromGuest(guestSourcePath, hostDestinationPath, aFlags);
     1102    if (!m_comGuestSession.isOk())
     1103        return false;
     1104    return true;
     1105}
     1106
     1107bool UIGuestFileTable::copyHostToGuest(const QString &hostSourcePath, const QString &guestDestinationPath)
     1108{
     1109    if (m_comGuestSession.isNull())
     1110        return false;
     1111    QVector<KDirectoryCopyFlag> aFlags(KDirectoryCopyFlag_CopyIntoExisting);
     1112    /** @todo listen to CProgress object to monitor copy operation: */
     1113    /*CProgress comProgress = */m_comGuestSession.DirectoryCopyToGuest(hostSourcePath, guestDestinationPath, aFlags);
     1114    return true;
     1115}
     1116
     1117KFsObjType UIGuestFileTable::fsObjectType(const QString& path)
     1118{
     1119    if (m_comGuestSession.isNull())
     1120        return KFsObjType_Unknown;
     1121    CGuestFsObjInfo comFsObjInfo = m_comGuestSession.FsObjQueryInfo(path, false /*aFollowSymlinks*/);
     1122    if (!comFsObjInfo.isOk() || m_comGuestSession.isOk())
     1123        return KFsObjType_Unknown;
     1124    return comFsObjInfo.GetType();
    10071125}
    10081126
     
    11081226    if (!item || item->isUpDirectory() || newBaseName.isEmpty())
    11091227        return false;
    1110     QString newPath = constructNewItemPath(item->path(), newBaseName);
     1228    QString newPath = UIPathOperations::constructNewItemPath(item->path(), newBaseName);
    11111229    QDir tempDir;
    11121230    if (tempDir.rename(item->path(), newPath))
     
    11231241    if (!parentDir.mkdir(directoryName))
    11241242    {
    1125         emit sigLogOutput(mergePaths(path, directoryName).append(" could not be created"));
     1243        emit sigLogOutput(UIPathOperations::mergePaths(path, directoryName).append(" could not be created"));
    11261244        return false;
    11271245    }
  • trunk/src/VBox/Frontends/VirtualBox/src/runtime/guestctrl/UIGuestControlFileTable.h

    r71276 r71298  
    6868    const QString  &path() const;
    6969    void setPath(const QString &path);
    70     /** Merge prefix and suffix by making sure they have a single '/' in between */
    71     void setPath(const QString &prexix, const QString &suffix);
    7270
    7371    /** True if this is directory and name is ".." */
     
    8583    /** Full absolute path of the item. Without the trailing '/' */
    8684    QString          m_strPath;
    87     /** For directories base name is the name of the lowest level directory
    88         in strPath. eg. for 'm_strPath = /opt/qt5.6/examples' 'm_strBaseName = examples'
    89         for files it is the name of the file */
    90     QString          m_strBaseName;
     85
    9186};
    9287
     
    112107    void emitLogOutput(const QString& strOutput);
    113108    /** Returns the path of the rootIndex */
    114     QString     currentPath() const;
     109    QString     currentDirectoryPath() const;
    115110    /** Returns the paths of the selected items (if any) as a list */
    116111    QStringList selectedItemPathList();
     
    138133    void keyPressEvent(QKeyEvent * pEvent);
    139134
    140     /** Replace the last part of the @p previusPath with newBaseName */
    141     QString constructNewItemPath(const QString &previousPath, const QString &newBaseName);
    142     QString mergePaths(const QString &Path, const QString &baseName);
     135
    143136
    144137    UIFileTableItem         *m_pRootItem;
     
    212205private:
    213206
     207    KFsObjType fsObjectType(const QString& path);
     208    bool copyGuestToHost(const QString &guestSourcePath, const QString& hostDestinationPath);
     209    bool copyHostToGuest(const QString& hostSourcePath, const QString &guestDestinationPath);
     210
    214211    void configureObjects();
    215212    CGuestSession m_comGuestSession;
Note: See TracChangeset for help on using the changeset viewer.

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