1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
|
/*
This file is part of Android File Transfer For Linux.
Copyright (C) 2015-2020 Vladimir Menshakov
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License,
or (at your option) any later version.
This library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this library; if not, write to the Free Software Foundation,
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef AFTL_QT_MTPOBJECTSMODEL_H
#define AFTL_QT_MTPOBJECTSMODEL_H
#include <qabstractitemmodel.h>
#include <mtp/ptp/Device.h>
#include <QSize>
#include <QVector>
#include <QStringList>
typedef QVector<mtp::ObjectId> MtpObjectList;
class MtpObjectsModel : public QAbstractListModel
{
Q_OBJECT
private:
mtp::SessionPtr _session;
mtp::StorageId _storageId;
mtp::ObjectId _parentObjectId;
bool _enableThumbnails;
QSize _maxThumbnailSize;
using ThumbnailPtr = std::shared_ptr<QPixmap>;
class Row
{
mtp::msg::ObjectInfoPtr _info;
ThumbnailPtr _thumbnail;
public:
mtp::ObjectId ObjectId;
Row(mtp::ObjectId id = mtp::ObjectId()): ObjectId(id) { }
void ResetInfo() { _info.reset(); }
mtp::msg::ObjectInfoPtr GetInfo(mtp::SessionPtr session);
ThumbnailPtr GetThumbnail(mtp::SessionPtr session, QSize maxSize);
bool IsAssociation(mtp::SessionPtr);
};
mutable QVector<Row> _rows;
signals:
void filePositionChanged(qint64, qint64);
void onFilesDropped(QStringList);
bool existingFileOverwrite(QString);
public:
struct ObjectInfo
{
QString Filename;
mtp::ObjectFormat Format;
qint64 Size;
ObjectInfo(): Filename(), Format(), Size(0) { }
ObjectInfo(const QString &fname, mtp::ObjectFormat format, qint64 size): Filename(fname), Format(format), Size(size) { }
};
MtpObjectsModel(QObject *parent = 0);
~MtpObjectsModel();
void enableThumbnail(bool enable, QSize maxSize);
void setSession(mtp::SessionPtr session);
mtp::SessionPtr session() const
{ return _session; }
void setStorageId(mtp::StorageId storageId);
void setParent(mtp::ObjectId parentObjectId);
void refresh()
{ setParent(_parentObjectId); }
bool enter(int idx);
mtp::ObjectId objectIdAt(int idx);
QModelIndex findObject(mtp::ObjectId objectId) const;
QModelIndex findObject(const QString &filename) const;
mtp::ObjectId parentObjectId() const
{ return _parentObjectId; }
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(), mtp::ObjectFormat format = mtp::ObjectFormat::Any);
bool uploadFile(const QString &filePath, QString filename = QString(), mtp::ObjectFormat format = mtp::ObjectFormat::Any)
{ return uploadFile(_parentObjectId, filePath, filename, format); }
bool sendFile(const QString &filePath);
bool downloadFile(const QString &filePath, mtp::ObjectId objectId);
void rename(int idx, const QString &fileName);
ObjectInfo getInfoById(mtp::ObjectId objectId) const;
void deleteObjects(const MtpObjectList &objects);
QStringList extractMimeData(const QMimeData *data);
virtual int rowCount(const QModelIndex &parent = QModelIndex()) const;
virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
virtual Qt::ItemFlags flags(const QModelIndex &index) const;
protected:
virtual QStringList mimeTypes () const;
virtual bool dropMimeData(const QMimeData *data,
Qt::DropAction action, int row, int column, const QModelIndex &parent);
};
#endif // MTPOBJECTSMODEL_H
|