-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilestree.cpp
255 lines (206 loc) · 7.83 KB
/
filestree.cpp
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#include "filestree.h"
#include "ui_filestree.h"
FilesTree::FilesTree(QWidget *parent) :
QDialog(parent),
ui(new Ui::FilesTree)
{
ui->setupUi(this);
loadLocalDirectory();
signalConnection = connect(AppSingleton::instance()->client, &WsClient::onTreeChange, this, &FilesTree::onTreeChangeEvent);
on_syncTree_clicked();
}
FilesTree::~FilesTree()
{
delete ui;
}
void FilesTree::loadLocalDirectory()
{
TreeItem *rootItem = new TreeItem({tr("Name"), tr("Type"), tr("Size")});
QString path = AppSingleton::instance()->getLocalPath();
QDir dir(path);
qDebug() << dir.entryList(QStringList(), QDir::Files);
qDebug() << dir.entryList(QStringList(), QDir::Dirs);
QQueue<QDir> q;
QQueue<TreeItem*> qt;
q.enqueue(dir);
qt.enqueue(rootItem);
while (!q.empty()) {
// this code is mean to work on linux need changes in window and mac
QDir d = q.dequeue();
TreeItem *father = qt.dequeue();
QStringList dirs = d.entryList(QStringList(), QDir::Dirs);
for (int i = 0; i < dirs.size(); i++) {
if (dirs.at(i).compare(".", Qt::CaseInsensitive) == 0 || dirs.at(i).compare("..", Qt::CaseInsensitive) == 0) {
continue;
}
qDebug() << dirs.at(i);
// this code is mean to work on linux need changes in window and mac
QString path = QString("%1/%2").arg(d.absolutePath(), dirs.at(i));
QDir nd(path);
qDebug() << path;
TreeItem *dirItem = new TreeItem({dirs.at(i), tr(""), tr("")}, father, 0, path, "", FOLDER);
father->appendChild(dirItem);
q.enqueue(nd);
qt.enqueue(dirItem);
}
QStringList files = d.entryList(QStringList(), QDir::Files);
for (int i = 0; i < files.size(); i++) {
QString path = QString("%1/%2").arg(d.absolutePath(), files.at(i));
QPair<QString, TreeItemType> type = getFileType(files.at(i));
TreeItem *dirItem = new TreeItem({files.at(i), type.first, tr("")}, father, 0, path, "", type.second);
father->appendChild(dirItem);
}
}
ui->treeView_2->setModel(new ItemModel(rootItem));
}
QPair<QString, TreeItemType> FilesTree::getFileType(QString fileName)
{
QStringList videos = QString("webm mkv flv vob ogv ogg drc mng avi mts m2ts mov qt wmv yuv rm rmvb viv asf amv mp4 m4p m4v mpg mp2 mpeg mpe mpv m2v svi 3gp 3g2 mxf roq nsv flv f4v f4p f4a f4b").split(" ");
for (int i = 0; i < (int) videos.size(); i++) {
if (fileName.endsWith(QString(".%1").arg(videos.at(i)), Qt::CaseInsensitive)) {
return qMakePair("video", VIDEO);
}
}
QStringList images = QString("jpg jpeg jfi jpe jif jfif heif heic png gif svg svgz eps webp tiff tif pnp raw arw cr rw2 nrw k25").split(" ");
for (int i = 0; i < (int) images.size(); i++) {
if (fileName.endsWith(QString(".%1").arg(images.at(i)), Qt::CaseInsensitive)) {
return qMakePair("image", IMAGE);
}
}
return qMakePair("unknow", OTHER);
}
void FilesTree::createParentPaths(TreeItem *treeItem)
{
TreeItem* c=treeItem;
QStack<QDir> paths;
while (c != nullptr) {
QDir dir(QString("%1%2").arg(AppSingleton::instance()->getLocalPath(), c->getPath()));
if (dir.exists()) {
break;
} else {
if (c->getType() == FOLDER || c->getType() == ROOT) {
paths.push(dir);
}
c = c->parentItem();
}
}
while (!paths.empty()) {
QDir dir = paths.pop();
dir.mkpath(".");
}
}
void FilesTree::createChildrenPaths(TreeItem *treeItem)
{
QQueue<TreeItem*> q;
q.enqueue(treeItem);
while (!q.empty()) {
TreeItem *it = q.dequeue();
QDir dir(QString("%1%2").arg(AppSingleton::instance()->getLocalPath(), it->getPath()));
if (!dir.exists()) {
dir.mkpath(".");
}
for (int i = 0; i < it->childCount(); i++) {
TreeItem *child = it->child(i);
if (child->getType() == FOLDER) {
q.enqueue(child);
}
}
}
}
QList<TreeItem *> FilesTree::listOfCopyItems(TreeItem *treeItem)
{
QList<TreeItem*> res;
if (treeItem->getType() == FOLDER) {
QQueue<TreeItem*> q;
q.enqueue(treeItem);
while (!q.empty()) {
TreeItem *it = q.dequeue();
for (int i = 0; i < it->childCount(); i++) {
TreeItem *child = it->child(i);
if (child->getType() == FOLDER) {
q.enqueue(child);
} else if (child->getType() == FOLDER || child->getType() == IMAGE || child->getType() == OTHER) {
res.append(child);
}
}
}
} else if (treeItem->getType() == VIDEO || treeItem->getType() == IMAGE || treeItem->getType() == OTHER) {
qDebug() << treeItem;
res.append(treeItem);
}
return res;
}
void FilesTree::onTreeChangeEvent(QJsonDocument json)
{
qDebug() << "Tree change event";
TreeItem *rootItem = new TreeItem({tr("Name"), tr("Type"), tr("Size")});
QQueue<QJsonObject> q;
QQueue<TreeItem*> qt;
q.enqueue(json.object().value("root").toObject());
qt.enqueue(rootItem);
while (!q.empty()) {
QJsonObject it = q.dequeue();
TreeItem* father = qt.dequeue();
QJsonArray dirs = it.value("dirs").toArray();
if (!dirs.empty()) {
for (int i = 0; i < dirs.size(); i++) {
QJsonObject d = dirs.at(i).toObject();
QString name = d.value("name").toString();
QString path = QString("%1/%2").arg(father->getPath(), name);
TreeItem *dirItem = new TreeItem({name, tr(""), tr("")}, father, 0, path, d.value("hash").toString(), FOLDER);
father->appendChild(dirItem);
q.enqueue(d);
qt.enqueue(dirItem);
}
}
QJsonArray files = it.value("files").toArray();
if (!files.empty()) {
for (int i = 0; i < files.size(); i++) {
QJsonObject f = files.at(i).toObject();
QString name = f.value("name").toString();
QString path = QString("%1/%2").arg(father->getPath(), name);
int size = f.value("size").toInt();
QPair<QString, TreeItemType> type = getFileType(name);
TreeItem *fileItem = new TreeItem({name, type.first, f.value("size").toInteger()}, father, size, path, f.value("hash").toString(), type.second);
father->appendChild(fileItem);
}
}
}
ui->treeView->setModel(new ItemModel(rootItem));
}
void FilesTree::on_syncTree_clicked()
{
AppSingleton::instance()->client->getTree();
}
void FilesTree::on_copyButton_clicked()
{
QModelIndexList selectionList = ui->treeView->selectionModel()->selectedIndexes();
if (!selectionList.empty()) {
QModelIndex item = selectionList.at(0);
TreeItem *treeItem = static_cast<TreeItem*>(item.internalPointer());
qDebug() << treeItem->getPath();
createParentPaths(treeItem);
if (treeItem->getType() == FOLDER) {
createChildrenPaths(treeItem);
}
// add copy files to list
QList<TreeItem*> files = listOfCopyItems(treeItem);
for (int i = 0; i < files.size(); i++) {
// todo: check here if the files was already copied
AppSingleton::instance()->copyThread->appendCopy(files.at(i)->getPath(), files.at(i)->getHash(), files.at(i)->getSize());
}
AppSingleton::instance()->copyThread->reanudateCopies();
}
}
void FilesTree::on_removeButton_clicked()
{
// todo
}
void FilesTree::on_FilesTree_rejected()
{
disconnect(signalConnection);
}
void FilesTree::on_FilesTree_accepted()
{
disconnect(signalConnection);
}