1 /*
2     This file is part of Android File Transfer For Linux.
3     Copyright (C) 2015-2020  Vladimir Menshakov
4 
5     This library is free software; you can redistribute it and/or modify it
6     under the terms of the GNU Lesser General Public License as published by
7     the Free Software Foundation; either version 2.1 of the License,
8     or (at your option) any later version.
9 
10     This library is distributed in the hope that it will be useful, but
11     WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13     Lesser General Public License for more details.
14 
15     You should have received a copy of the GNU Lesser General Public License
16     along with this library; if not, write to the Free Software Foundation,
17     Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 
20 #ifndef AFTL_QT_COMMANDQUEUE_H
21 #define AFTL_QT_COMMANDQUEUE_H
22 
23 #include <QObject>
24 #include <QQueue>
25 #include <QMap>
26 #include <mtp/ptp/ObjectId.h>
27 #include <mtp/ptp/ObjectFormat.h>
28 #include <mtp/metadata/Library.h>
29 
30 class MtpObjectsModel;
31 class CommandQueue;
32 
33 namespace mtp
34 {
35 	class Library;
36 	DECLARE_PTR(Library);
37 }
38 
39 struct Command
40 {
41 	virtual ~Command() = default;
42 	virtual void execute(CommandQueue &queue) = 0;
43 };
44 
45 struct FinishQueue : public Command
46 {
47 	mtp::ObjectId DirectoryId; //return id
FinishQueueFinishQueue48 	FinishQueue(mtp::ObjectId id): DirectoryId(id) { }
49 	virtual void execute(CommandQueue &queue);
50 };
51 
52 struct FileCommand : public Command
53 {
54 	QString		Filename;
55 
FileCommandFileCommand56 	FileCommand(const QString &filename) : Filename(filename) { }
57 };
58 
59 struct MakeDirectory : public FileCommand
60 {
MakeDirectoryMakeDirectory61 	MakeDirectory(const QString &filename) :
62 		FileCommand(filename) { }
63 	void execute(CommandQueue &queue);
64 };
65 
66 struct UploadFile : public FileCommand
67 {
UploadFileUploadFile68 	UploadFile(const QString &filename) : FileCommand(filename) { }
69 	void execute(CommandQueue &queue);
70 };
71 
72 struct ImportFile : public FileCommand
73 {
ImportFileImportFile74 	ImportFile(const QString &filename) : FileCommand(filename) { }
75 	void execute(CommandQueue &queue);
76 };
77 
78 struct DownloadFile : public FileCommand
79 {
80 	mtp::ObjectId			ObjectId;
81 
DownloadFileDownloadFile82 	DownloadFile(const QString &filename, mtp::ObjectId objectId) : FileCommand(filename), ObjectId(objectId) { }
83 	void execute(CommandQueue &queue);
84 };
85 
86 struct LoadLibrary : public Command
87 {
88 	void execute(CommandQueue &queue);
89 };
90 
91 class CommandQueue: public QObject
92 {
93 	Q_OBJECT
94 
95 private:
96 	MtpObjectsModel *				_model;
97 	qint64							_completedFilesSize;
98 	QMap<QString, mtp::ObjectId>	_directories;
99 	std::map<QString, mtp::Library::AlbumPtr> _albums;
100 
101 	struct Cover
102 	{
103 		QString 	Path;
104 		int			Score;
105 	};
106 
107 	std::map<QString, Cover> 		_covers;
108 	mtp::LibraryPtr					_library;
109 	volatile bool					_aborted;
110 
111 public:
112 	CommandQueue(MtpObjectsModel *model);
113 	~CommandQueue();
114 
model()115 	MtpObjectsModel *model() const
116 	{ return _model; }
117 
118 	mtp::LibraryPtr library() const;
119 
120 	void loadLibrary();
121 	void createDirectory(const QString &path);
122 	void uploadFile(const QString &file);
123 	void downloadFile(const QString &filename, mtp::ObjectId objectId);
124 	void importFile(const QString &file);
125 
126 public slots:
127 	void onFileProgress(qint64, qint64);
128 	void execute(Command *cmd);
129 	void start(const QString &filename);
130 	void finish(mtp::ObjectId directoryId);
131 	void addProgress(qint64);
132 	void abort();
133 
134 signals:
135 	void started(QString);
136 	void progress(qint64 bytes);
137 	void total(qint64 bytes);
138 	void finished();
139 };
140 
141 #endif // COMMANDQUEUE_H
142