diff --git a/qt/commandqueue.cpp b/qt/commandqueue.cpp index 624260a7..66d6f441 100644 --- a/qt/commandqueue.cpp +++ b/qt/commandqueue.cpp @@ -33,7 +33,7 @@ void UploadFile::execute(CommandQueue &queue) { queue.uploadFile(Filename); } void MakeDirectory::execute(CommandQueue &queue) -{ queue.createDirectory(Filename, Root); } +{ queue.createDirectory(Filename); } void DownloadFile::execute(CommandQueue &queue) { queue.downloadFile(Filename, ObjectId); } @@ -64,23 +64,26 @@ void CommandQueue::uploadFile(const QString &filename) qDebug() << "uploading file " << filename; QFileInfo fi(filename); + QString parentPath = fi.dir().path(); if (_directories.empty()) - _directories[fi.dir().path()] = _model->parentObjectId(); + _directories[parentPath] = _model->parentObjectId(); start(fi.fileName()); - auto parent = _directories.find(fi.dir().path()); - Q_ASSERT(parent != _directories.end()); + auto parent = _directories.find(parentPath); + if (parent == _directories.end()) + { + qWarning() << "invalid parent " << parentPath; + return; + } try { - if (_model->parentObjectId() != parent.value()) - _model->setParent(parent.value()); - _model->uploadFile(filename); + _model->uploadFile(parent.value(), filename); } catch(const std::exception &ex) { qDebug() << "uploading file " << filename << " failed: " << fromUtf8(ex.what()); } addProgress(fi.size()); } -void CommandQueue::createDirectory(const QString &srcPath, bool root) +void CommandQueue::createDirectory(const QString &srcPath) { if (_aborted) return; @@ -92,22 +95,20 @@ void CommandQueue::createDirectory(const QString &srcPath, bool root) Q_ASSERT(parentDir.cdUp()); QString parentPath = parentDir.path(); qDebug() << "parent: " << parentPath << ", dir: " << dir.dirName(); + if (_directories.empty()) + _directories[parentPath] = _model->parentObjectId(); - if (!root) + auto parent = _directories.find(parentPath); + if (parent == _directories.end()) { - auto parent = _directories.find(parentPath); - Q_ASSERT(parent != _directories.end()); - if (_model->parentObjectId() != parent.value()) - _model->setParent(parent.value()); + qWarning() << "invalid parent " << parentPath; + return; } - else - _directories[parentPath] = _model->parentObjectId(); try { - mtp::ObjectId dirId = _model->createDirectory(dir.dirName()); + mtp::ObjectId dirId = _model->createDirectory(parent.value(), dir.dirName()); _directories[path] = dirId; - _model->setParent(dirId); } catch(const std::exception &ex) { qDebug() << "creating directory" << path << "failed: " << fromUtf8(ex.what()); return; } } diff --git a/qt/commandqueue.h b/qt/commandqueue.h index ccbd5fc1..4f73ba33 100644 --- a/qt/commandqueue.h +++ b/qt/commandqueue.h @@ -51,9 +51,8 @@ struct FileCommand : public Command struct MakeDirectory : public FileCommand { - bool Root; - MakeDirectory(const QString &filename, bool root = false) : - FileCommand(filename), Root(root) { } + MakeDirectory(const QString &filename) : + FileCommand(filename) { } void execute(CommandQueue &queue); }; @@ -88,7 +87,7 @@ class CommandQueue: public QObject MtpObjectsModel *model() const { return _model; } - void createDirectory(const QString &path, bool root); + void createDirectory(const QString &path); void uploadFile(const QString &file); void downloadFile(const QString &filename, mtp::ObjectId objectId); diff --git a/qt/fileuploader.cpp b/qt/fileuploader.cpp index be0f123f..d42c3c30 100644 --- a/qt/fileuploader.cpp +++ b/qt/fileuploader.cpp @@ -83,7 +83,7 @@ void FileUploader::upload(QStringList files) if (currentFileInfo.isDir()) { qDebug() << "adding subdirectory" << currentFile; - commands.push_back(new MakeDirectory(currentFile, true)); + commands.push_back(new MakeDirectory(currentFile)); QDirIterator it(currentFile, QDirIterator::Subdirectories); while(it.hasNext()) { diff --git a/qt/mtpobjectsmodel.cpp b/qt/mtpobjectsmodel.cpp index a98f424d..d71836d4 100644 --- a/qt/mtpobjectsmodel.cpp +++ b/qt/mtpobjectsmodel.cpp @@ -168,21 +168,24 @@ QVariant MtpObjectsModel::data(const QModelIndex &index, int role) const } } -mtp::ObjectId MtpObjectsModel::createDirectory(const QString &name, mtp::AssociationType type) +mtp::ObjectId MtpObjectsModel::createDirectory(mtp::ObjectId parentObjectId, const QString &name, mtp::AssociationType type) { QModelIndex existingDir = findObject(name); if (existingDir.isValid()) return _rows.at(existingDir.row()).ObjectId; mtp::StorageId storageId = _storageId != mtp::Session::AllStorages? _storageId: mtp::Session::AnyStorage; - mtp::Session::NewObjectInfo noi = _session->CreateDirectory(toUtf8(name), _parentObjectId, storageId, type); - beginInsertRows(QModelIndex(), _rows.size(), _rows.size()); - _rows.push_back(Row(noi.ObjectId)); - endInsertRows(); + mtp::Session::NewObjectInfo noi = _session->CreateDirectory(toUtf8(name), parentObjectId, storageId, type); + if (parentObjectId == _parentObjectId) + { + beginInsertRows(QModelIndex(), _rows.size(), _rows.size()); + _rows.push_back(Row(noi.ObjectId)); + endInsertRows(); + } return noi.ObjectId; } -bool MtpObjectsModel::uploadFile(const QString &filePath, QString filename) +bool MtpObjectsModel::uploadFile(mtp::ObjectId parentObjectId, const QString &filePath, QString filename) { QFileInfo fileInfo(filePath); mtp::ObjectFormat objectFormat = mtp::ObjectFormatFromFilename(toUtf8(filePath)); @@ -202,7 +205,7 @@ bool MtpObjectsModel::uploadFile(const QString &filePath, QString filename) return false; } _session->DeleteObject(_rows.at(existingObject.row()).ObjectId); - needReset = true; + needReset = parentObjectId == _parentObjectId; } std::shared_ptr object(new QtObjectInputStream(filePath)); @@ -218,13 +221,16 @@ bool MtpObjectsModel::uploadFile(const QString &filePath, QString filename) oi.Filename = toUtf8(filename); oi.ObjectFormat = objectFormat; oi.SetSize(fileInfo.size()); - mtp::Session::NewObjectInfo noi = _session->SendObjectInfo(oi, _storageId != mtp::Session::AllStorages? _storageId: mtp::Session::AnyStorage, _parentObjectId); + mtp::Session::NewObjectInfo noi = _session->SendObjectInfo(oi, _storageId != mtp::Session::AllStorages? _storageId: mtp::Session::AnyStorage, parentObjectId); qDebug() << "new object id: " << noi.ObjectId << ", sending..."; _session->SendObject(object); qDebug() << "ok"; - beginInsertRows(QModelIndex(), _rows.size(), _rows.size()); - _rows.push_back(Row(noi.ObjectId)); - endInsertRows(); + if (parentObjectId == _parentObjectId) + { + beginInsertRows(QModelIndex(), _rows.size(), _rows.size()); + _rows.push_back(Row(noi.ObjectId)); + endInsertRows(); + } if (needReset) refresh(); return true; diff --git a/qt/mtpobjectsmodel.h b/qt/mtpobjectsmodel.h index e028f5e8..a67db8be 100644 --- a/qt/mtpobjectsmodel.h +++ b/qt/mtpobjectsmodel.h @@ -89,8 +89,13 @@ class MtpObjectsModel : public QAbstractListModel mtp::ObjectId parentObjectId() const { return _parentObjectId; } - mtp::ObjectId createDirectory(const QString &name, mtp::AssociationType type = mtp::AssociationType::GenericFolder); - bool uploadFile(const QString &filePath, QString filename = QString()); + mtp::ObjectId createDirectory(mtp::ObjectId parentObjectId, const QString &name, mtp::AssociationType type = mtp::AssociationType::GenericFolder); + mtp::ObjectId createDirectory(const QString &name, mtp::AssociationType type = mtp::AssociationType::GenericFolder) + { return createDirectory(_parentObjectId, name, type); } + + bool uploadFile(mtp::ObjectId parentObjectId, const QString &filePath, QString filename = QString()); + bool uploadFile(const QString &filePath, QString filename = QString()) + { return uploadFile(_parentObjectId, filePath, filename); } bool downloadFile(const QString &filePath, mtp::ObjectId objectId); void rename(int idx, const QString &fileName); ObjectInfo getInfoById(mtp::ObjectId objectId) const;