diff options
author | Anne Jan Brouwer <brouwer@annejan.com> | 2016-12-14 22:56:38 +0100 |
---|---|---|
committer | Anne Jan Brouwer <brouwer@annejan.com> | 2016-12-14 22:56:38 +0100 |
commit | b42bb45e6074b3213aaf752e7dc906e95e01790a (patch) | |
tree | bd694541f8a32ec6163aef278bb9b614acc0643f /src/storemodel.cpp | |
parent | 816e5aac5030c3f3dd5ae8ae11eb3c97e034f68c (diff) | |
parent | fdb5c50ee7b89d9eaf1e2bf08e802996edc58131 (diff) |
Updated from master
Diffstat (limited to 'src/storemodel.cpp')
-rw-r--r-- | src/storemodel.cpp | 179 |
1 files changed, 174 insertions, 5 deletions
diff --git a/src/storemodel.cpp b/src/storemodel.cpp index 0b45eb10..a1f4609d 100644 --- a/src/storemodel.cpp +++ b/src/storemodel.cpp @@ -1,4 +1,28 @@ #include "storemodel.h" +#include <QDebug> +#include <QStringListModel> +#include <QMimeData> +#include <QMessageBox> + + +QDataStream &operator<<(QDataStream &out, const dragAndDropInfoPasswordStore &dragAndDropInfoPasswordStore) +{ + out << dragAndDropInfoPasswordStore.isDir + << dragAndDropInfoPasswordStore.isFile + << dragAndDropInfoPasswordStore.path; + return out; +} + + + +QDataStream &operator>>(QDataStream &in, dragAndDropInfoPasswordStore &dragAndDropInfoPasswordStore) +{ + in >> dragAndDropInfoPasswordStore.isDir + >> dragAndDropInfoPasswordStore.isFile + >> dragAndDropInfoPasswordStore.path; + return in; +} + /** * @brief StoreModel::StoreModel @@ -41,11 +65,11 @@ bool StoreModel::ShowThis(const QModelIndex index) const { break; } } else { - QModelIndex useIndex = sourceModel()->index(index.row(), 0, index.parent()); - QString path = fs->filePath(useIndex); - path = QDir(store).relativeFilePath(path); - path.replace(QRegExp("\\.gpg$"), ""); - retVal = path.contains(filterRegExp()); + QModelIndex useIndex = sourceModel()->index(index.row(), 0, index.parent()); + QString path = fs->filePath(useIndex); + path = QDir(store).relativeFilePath(path); + path.replace(QRegExp("\\.gpg$"), ""); + retVal = path.contains(filterRegExp()); } return retVal; } @@ -82,3 +106,148 @@ QVariant StoreModel::data(const QModelIndex &index, int role) const { return initial_value; } + +/** + * @brief StoreModel::supportedDropActions enable drop. + * @return + */ +Qt::DropActions StoreModel::supportedDropActions() const +{ + return Qt::CopyAction | Qt::MoveAction; +} +/** + * @brief StoreModel::supportedDragActions enable drag. + * @return + */ +Qt::DropActions StoreModel::supportedDragActions() const +{ + return Qt::CopyAction | Qt::MoveAction; +} + +Qt::ItemFlags StoreModel::flags(const QModelIndex &index) const +{ + Qt::ItemFlags defaultFlags = QSortFilterProxyModel::flags(index); + + if (index.isValid()){ + return Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | defaultFlags; + }else{ + return Qt::ItemIsDropEnabled | defaultFlags; + } +} + +QStringList StoreModel::mimeTypes() const +{ + QStringList types; + types << "application/vnd+qtpass.dragAndDropInfoPasswordStore"; + return types; +} + +QMimeData *StoreModel::mimeData(const QModelIndexList &indexes) const +{ + dragAndDropInfoPasswordStore info; + + QByteArray encodedData; + // only use the first, otherwise we should enable multiselection + QModelIndex index = indexes.at(0); + if (index.isValid()) { + QModelIndex useIndex = mapToSource(index); + + info.isDir = fs->fileInfo(useIndex).isDir(); + info.isFile = fs->fileInfo(useIndex).isFile(); + info.path = fs->fileInfo(useIndex).absoluteFilePath(); + QDataStream stream(&encodedData, QIODevice::WriteOnly); + stream << info; + } + + QMimeData *mimeData = new QMimeData(); + mimeData->setData("application/vnd+qtpass.dragAndDropInfoPasswordStore", encodedData); + return mimeData; +} + + + + +bool StoreModel::canDropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) const +{ + QModelIndex useIndex = this->index(parent.row(), parent.column(), parent.parent()); + QByteArray encodedData = data->data("application/vnd+qtpass.dragAndDropInfoPasswordStore"); + QDataStream stream(&encodedData, QIODevice::ReadOnly); + dragAndDropInfoPasswordStore info; + stream >> info; + if (data->hasFormat("application/vnd+qtpass.dragAndDropInfoPasswordStore") == false) + return false; + + + if (column > 0){ + return false; + } + + // you can drop a folder on a folder + if (fs->fileInfo(mapToSource(useIndex)).isDir() && info.isDir){ + return true; + } + // you can drop a file on a folder + if (fs->fileInfo(mapToSource(useIndex)).isDir() && info.isFile){ + return true; + } + // you can drop a file on a file + if (fs->fileInfo(mapToSource(useIndex)).isFile() && info.isFile){ + return true; + } + + return false; +} + +bool StoreModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) +{ + if (!canDropMimeData(data, action, row, column, parent)) + return false; + + if (action == Qt::IgnoreAction){ + return true; + } + QByteArray encodedData = data->data("application/vnd+qtpass.dragAndDropInfoPasswordStore"); + + QDataStream stream(&encodedData, QIODevice::ReadOnly); + dragAndDropInfoPasswordStore info; + stream >> info; + QModelIndex destIndex = this->index(parent.row(), parent.column(), parent.parent()); + QFileInfo destFileinfo = fs->fileInfo(mapToSource(destIndex)); + QFileInfo srcFileInfo = QFileInfo(info.path); + QDir qdir; + QString cleanedSrc = qdir.cleanPath(srcFileInfo.absoluteFilePath()); + QString cleanedDest = qdir.cleanPath(destFileinfo.absoluteFilePath()); + if(info.isDir){ + QDir srcDir = QDir(info.path); + // dropped dir onto dir + if(destFileinfo.isDir()){ + QDir destDir = QDir(cleanedDest).filePath(srcFileInfo.fileName()); + QString cleanedDestDir = qdir.cleanPath(destDir.absolutePath()); + if(action == Qt::MoveAction){ + QtPassSettings::getPass()->Move(cleanedSrc, cleanedDestDir); + }else if(action == Qt::CopyAction){ + QtPassSettings::getPass()->Copy(cleanedSrc, cleanedDestDir); + } + } + }else if(info.isFile){ + // dropped file onto a directory + if(destFileinfo.isDir()){ + if(action == Qt::MoveAction){ + QtPassSettings::getPass()->Move(cleanedSrc, cleanedDest); + }else if(action == Qt::CopyAction){ + QtPassSettings::getPass()->Copy(cleanedSrc, cleanedDest); + } + }else if(destFileinfo.isFile()){ + // dropped file onto a file + int answer = QMessageBox::question(0, tr("force overwrite?"), tr("overwrite %1 with %2?").arg(cleanedDest).arg(cleanedSrc), QMessageBox::Yes | QMessageBox::No); + bool force = answer==QMessageBox::Yes; + if(action == Qt::MoveAction){ + QtPassSettings::getPass()->Move(cleanedSrc, cleanedDest, force); + }else if(action == Qt::CopyAction){ + QtPassSettings::getPass()->Copy(cleanedSrc, cleanedDest, force); + } + } + + } + return true; +} |