Skip to content

Commit

Permalink
allow explicitly specified parent for MtpObjectsModel::createDirector…
Browse files Browse the repository at this point in the history
…y/uploadFile
  • Loading branch information
whoozle committed Feb 25, 2017
1 parent 162b3a1 commit 6e75f2f
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 35 deletions.
35 changes: 18 additions & 17 deletions qt/commandqueue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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); }
Expand Down Expand Up @@ -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;
Expand All @@ -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; }
}
Expand Down
7 changes: 3 additions & 4 deletions qt/commandqueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};

Expand Down Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion qt/fileuploader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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())
{
Expand Down
28 changes: 17 additions & 11 deletions qt/mtpobjectsmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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<QtObjectInputStream> object(new QtObjectInputStream(filePath));
Expand All @@ -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;
Expand Down
9 changes: 7 additions & 2 deletions qt/mtpobjectsmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 6e75f2f

Please sign in to comment.