Debian Codegraph @

src/RsyncDirModel_p.cpp

raw symbols SCIP
1 /****************************************************************************
2 Copyright (C) VCreate Logic Private Limited, Bangalore
3 Contact: [email protected]
4
5 This file is developed by VCreate Logic Private Limited and used as part of the "luckyBackup" project
6 It is distributed under the terms of the GNU General Public License
7 It is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 It is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with the luckyBackup project. If not, see <http://www.gnu.org/licenses/>.
19
20 ****************************************************************************/
21 // modified on 22 May 2016
22 // by luckyb after Lothar's fixes
23
24 #include "RsyncDirModel_p.h"
25
26 #include <QFileIconProvider>
27 #include <QPixmapCache>
28 #include <QDesktopServices>
29 #include <QDir>
30 #include <QIcon>
31 #include <QPixmap>
32 #include <QTextCodec>
33
34 ///////////////////////////////////////////////////////////////////////////////
35 // RsyncEntry
36 ///////////////////////////////////////////////////////////////////////////////
37
38 struct RsyncEntryData
39 {
40 RsyncEntryData() : parent(0),
41 childrenFetched(false),
42 fetching(false) { }
43
44 QUrlInfo urlInfo;
45
46 RsyncEntry* parent;
47 QList<RsyncEntry*> children;
48 bool childrenFetched;
49 bool fetching;
50 QString path;
51 QIcon icon;
52
53 };
54
55
56
57 RsyncEntry::RsyncEntry(const QUrlInfo& info)
58 {
59 d = new RsyncEntryData;
60 d->urlInfo = info;
61 if(info.isDir())
62 d->childrenFetched = false;
63 else
64 d->childrenFetched = true;
65
66 if(info.isValid())
67 d->icon = FileIconCache::instance().icon(info);
68 }
69
70 RsyncEntry::~RsyncEntry()
71 {
72 qDeleteAll(d->children);
73 delete d;
74 }
75
76 RsyncEntry* RsyncEntry::parentEntry() const
77 {
78 return d->parent;
79 }
80
81 QList<RsyncEntry*> RsyncEntry::childEntries() const
82 {
83 return d->children;
84 }
85
86 bool RsyncEntry::isChildrenFetched() const
87 {
88 return d->childrenFetched;
89 }
90
91 bool RsyncEntry::isFetching() const
92 {
93 return d->fetching;
94 }
95
96 QString RsyncEntry::path() const
97 {
98 if(!d->path.isEmpty())
99 return d->path;
100
101 if(d->parent)
102 d->path = d->parent->path();
103 else
104 d->path = "/";
105
106 if(d->urlInfo.isDir())
107 d->path += d->urlInfo.name() + "/";
108 else
109 d->path += d->urlInfo.name();
110
111 return d->path;
112 }
113
114 QUrlInfo RsyncEntry::urlInfo() const
115 {
116 return d->urlInfo;
117 }
118
119 QIcon RsyncEntry::icon() const
120 {
121 return d->icon;
122 }
123
124 RsyncEntry* RsyncEntry::addUrlInfo(const QUrlInfo& urlInfo)
125 {
126 if(d->children.count())
127 {
128 RsyncEntry* firstEntry = d->children.at(0);
129 if(!firstEntry->urlInfo().isValid())
130 {
131 d->children.removeAt(0);
132 delete firstEntry;
133 }
134 }
135
136 RsyncEntry* childEntry = new RsyncEntry(urlInfo);
137 d->children.append( childEntry );
138 childEntry->d->parent = this;
139 return childEntry;
140 }
141
142 void RsyncEntry::removeUrlInfo(const QUrlInfo& urlInfo)
143 {
144 for(int i=0; i<d->children.count(); i++)
145 {
146 RsyncEntry* entry = d->children.at(i);
147 if(entry->urlInfo() == urlInfo)
148 {
149 d->children.removeAt(i);
150 delete entry;
151 return;
152 }
153 }
154 }
155
156 void RsyncEntry::setChildrenFetched(bool val)
157 {
158 if(!val && d->children.count())
159 return;
160
161 d->childrenFetched = val;
162 if(val)
163 d->fetching = false;
164 }
165
166 void RsyncEntry::clear()
167 {
168 d->children.clear();
169 }
170
171 void RsyncEntry::setFetching(bool val)
172 {
173 d->fetching = val;
174
175 // Add a dummy URL info, until
176 // children are fetched.
177 addUrlInfo(QUrlInfo());
178 }
179
180 ///////////////////////////////////////////////////////////////////////////////
181 // RsyncDirListerOperation
182 ///////////////////////////////////////////////////////////////////////////////
183
184 struct RsyncDirListerOperationData
185 {
186 RsyncDirListerOperationData()
187 : parentEntry(0),
188 executing(false),
189 finished(false),
190 success(false),
191 RsyncCmdId(0) { }
192
193 QProcess* syncProcess;
194 bool ProcReportedError;
195 bool abort;
196 QString errorMessage;
197 RsyncDirLister* parent;
198 RsyncEntry* parentEntry;
199 bool executing;
200 bool finished;
201 bool success;
202 int id;
203 int RsyncCmdId;
204 QUrl url;
205
206 QList<QUrlInfo> urlInfoList;
207 };
208
209 static int UniqueId()
210 {
211 static int id;
212 return ++id;
213 }
214
215 RsyncDirListerOperation::RsyncDirListerOperation(RsyncDirLister* parent)
216 : QObject(parent)
217 {
218 d = new RsyncDirListerOperationData;
219 d->parent = parent;
220 d->id = UniqueId();
221 d->url = QUrl(d->parent->url().path());
222 d->syncProcess = new QProcess(this);
223 connect(d->syncProcess, SIGNAL (finished(int, QProcess::ExitStatus)), this, SLOT(procFinished()));
224 connect(d->syncProcess, SIGNAL (error(QProcess::ProcessError)), this, SLOT(procError()));
225 }
226
227 RsyncDirListerOperation::RsyncDirListerOperation(RsyncDirLister* parent,RsyncEntry* entry)
228 : QObject(parent)
229 {
230 d = new RsyncDirListerOperationData;
231 d->parent = parent;
232 d->parentEntry = entry;
233 d->url = QUrl(d->parent->url().path()+d->parentEntry->path());
234 d->parentEntry->setFetching(true);
235 d->id = UniqueId();
236 d->syncProcess = new QProcess(this);
237 connect(d->syncProcess, SIGNAL (finished(int, QProcess::ExitStatus)), this, SLOT(procFinished()));
238 connect(d->syncProcess, SIGNAL (error(QProcess::ProcessError)), this, SLOT(procError()));
239 }
240
241 RsyncDirListerOperation::~RsyncDirListerOperation()
242 {
243 delete d;
244 }
245
246 void RsyncDirListerOperation::setParentEntry(RsyncEntry* entry)
247 {
248
249 d->parentEntry = entry;
250 d->url = QUrl(d->parent->url().path()+d->parentEntry->path());
251 d->parentEntry->setFetching(true);
252 }
253
254 RsyncEntry* RsyncDirListerOperation::parentEntry() const
255 {
256 return d->parentEntry;
257 }
258
259 int RsyncDirListerOperation::id() const
260 {
261 return d->id;
262 }
263
264 bool RsyncDirListerOperation::isExecuting() const
265 {
266 return d->executing;
267 }
268
269 bool RsyncDirListerOperation::isFinished() const
270 {
271 return d->finished;
272 }
273
274 bool RsyncDirListerOperation::isSuccessfullyFinished() const
275 {
276 return d->success;
277 }
278 QString RsyncDirListerOperation::path() const
279 {
280 return d->url.path();
281 }
282 void RsyncDirListerOperation::execute()
283 {
284 d->executing = true;
285 QStringList tempArgs=d->parent->arguments();
286 tempArgs << path();
287 d->syncProcess -> start (d->parent->command(),tempArgs); // execute rsync command with rsyncArguments
288 if (!d->syncProcess->waitForStarted())
289 abort();
290 }
291
292 void RsyncDirListerOperation::abort()
293 {
294 if(d->executing)
295 {
296 d->syncProcess->kill();
297 d->executing = false;
298 d->finished = true;
299 d->success = false;
300 d->urlInfoList.clear();
301 // emit aborted();
302 }
303 }
304
305
306 void RsyncDirListerOperation::procFinished()
307 {
308 QTextCodec *codec = QTextCodec::codecForName("UTF-8");
309 QTextStream outputStream (new QString(codec->toUnicode(d->syncProcess -> readAllStandardOutput())));
310 QString line;
311 QList<RsyncEntry*> parentEntries;
312 do {
313 line =outputStream.readLine();
314 QStringList data= line.split(" ",QString::SkipEmptyParts);
315 if (data.isEmpty())
316 continue;
317 bool isDir=false;
318 bool isFile=false;
319 bool isLink=false;
320 bool isExec=false;
321 QString d1=data[0];
322 //in data list 0: permisions, 1: size, 2: date, 3: hour, 4: name
323 if (d1.at(0)=='d'){
324 isExec=true;
325 isDir=true;
326 }
327 else if(d1.at(0)=='l')
328 isLink=true;
329 else if(d1.at(0)=='-')
330 isFile=true;
331 else
332 continue;
333 d1=data[4];
334 for (int i=5;i<data.size();i++)
335 d1+=" "+data[i];
336 if (d1[0]=='.')
337 continue;
338 if (d->parentEntry){
339 d->parentEntry->addUrlInfo(QUrlInfo(d1,777,"root","root",data[1].toLong(), QDateTime::fromString(data[2]+" "+data[3],"yyyy/MM/dd HH:mm:ss"),
340 QDateTime::fromString(data[2]+" "+data[3],"yyyy/MM/dd HH:mm:ss"),
341 isDir,isFile,isLink,false,true,isExec));
342 }else{
343 parentEntries.append(new RsyncEntry(QUrlInfo(d1,777,"root","root",data[1].toLong(), QDateTime::fromString(data[2]+" "+data[3],"yyyy/MM/dd HH:mm:ss"),
344 QDateTime::fromString(data[2]+" "+data[3],"yyyy/MM/dd HH:mm:ss"),
345 isDir,isFile,isLink,false,true,isExec)));
346
347 }
348
349
350 }while (!line.isNull());
351 d->executing = false;
352 d->finished = true;
353 d->success = true;
354 if (d->parentEntry)
355 d->parent->setChildrenFetched(d->parentEntry);
356 else
357 d->parent->setTopLevelEntries(parentEntries);
358 }
359
360 void RsyncDirListerOperation::procError()
361 {
362 QTextCodec *codec = QTextCodec::codecForName("UTF-8");
363 d->errorMessage = codec->toUnicode(d->syncProcess -> readAllStandardError());
364 d->executing = false;
365 d->finished = true;
366 d->success = false;
367 }
368
369 ///////////////////////////////////////////////////////////////////////////////
370 // RsyncDirLister
371 ///////////////////////////////////////////////////////////////////////////////
372
373 struct RsyncDirListerData
374 {
375 RsyncDirListerData() :
376 topLevelEntriesFetched(false),
377 currentParent(0) { }
378
379 // QProcess *syncProcess;
380 // bool ProcReportedError;
381 // bool abort;
382 QString errorMessage;
383 QList<RsyncEntry*> topLevelEntries;
384 bool topLevelEntriesFetched;
385 QUrl url;
386
387 RsyncEntry* currentParent;
388 QString command; //command to be executed. Normally this is "rsync"
389 QStringList rsyncArguments; // This stringList holds all arguments for the rsync command
390 QList<RsyncDirListerOperation*> operationQueue;
391
392 };
393
394 RsyncDirLister::RsyncDirLister(QObject* parent)
395 :QObject(parent)
396 {
397 d = new RsyncDirListerData;
398 d->command="rsync";
399 d->rsyncArguments << "--list-only";
400
401 }
402
403 RsyncDirLister::~RsyncDirLister()
404 {
405 delete d;
406 }
407
408 void RsyncDirLister::setUrl(const QUrl& url)
409 {
410 d->url = url;
411 }
412 void RsyncDirLister::setUrl(const QUrl& url,const QString& cpath)
413 {
414 d->rsyncArguments.clear();
415 d->url=url;
416 d->command=cpath;
417
418 d->rsyncArguments << "--list-only";
419 }
420 void RsyncDirLister::setUrl(const QUrl& url,const QString& cpath, const QStringList& cargs)
421 {
422 d->rsyncArguments.clear();
423 d->url=url;
424 d->command=cpath;
425
426 if (!cargs.isEmpty())
427 d->rsyncArguments=cargs;
428 d->rsyncArguments << "--list-only";
429
430 }
431
432 QUrl RsyncDirLister::url() const
433 {
434 return d->url;
435 }
436
437 QString RsyncDirLister::command() const
438 {
439 return d->command;
440 }
441
442 QStringList RsyncDirLister::arguments() const
443 {
444 return d->rsyncArguments;
445 }
446
447 QList<RsyncEntry*> RsyncDirLister::topLevelEntries() const
448 {
449 return d->topLevelEntries;
450 }
451
452 bool RsyncDirLister::topLevelEntriesFetched() const
453 {
454 return d->topLevelEntriesFetched;
455 }
456
457 QString RsyncDirLister::lastError() const
458 {
459 return d->errorMessage;
460 }
461
462 void RsyncDirLister::list()
463 {
464 cleanOperations();
465 // Create a list operation and queue it.
466 RsyncDirListerOperation* listOper
467 = new RsyncDirListerOperation(this);
468 d->operationQueue.append(listOper);
469 d->topLevelEntriesFetched = false;
470 listOper->execute();
471
472 }
473
474 void RsyncDirLister::list(RsyncEntry* entry)
475 {
476 // If the entry is already being fetched,
477 // dont sweat
478 cleanOperations();
479 if(entry->isChildrenFetched() || entry->isFetching())
480 return;
481
482 // Verify whether entry belongs to us
483 RsyncEntry* e2 = entry;
484 while(e2->parentEntry())
485 e2 = e2->parentEntry();
486 if(!d->topLevelEntries.contains(e2))
487 return;
488
489 // Create a list operation and queue it.
490 RsyncDirListerOperation* listOper
491 = new RsyncDirListerOperation(this,entry);
492 // listOper->setParentEntry(entry);
493 d->operationQueue.append(listOper);
494 listOper->execute();
495 }
496
497 void RsyncDirLister::refresh()
498 {
499 abort();
500 qDeleteAll(d->topLevelEntries);
501 d->topLevelEntries.clear();
502 list();
503 }
504
505 void RsyncDirLister::refresh(RsyncEntry* entry)
506 {
507 if(entry->isFetching())
508 return;
509
510 entry->setChildrenFetched(false);
511 list(entry);
512 }
513
514 void RsyncDirLister::cleanOperations()
515 {
516 Q_FOREACH(RsyncDirListerOperation* oper, d->operationQueue)
517 {
518 if(oper->isFinished())
519 d->operationQueue.removeOne(oper);
520 }
521 }
522
523 void RsyncDirLister::abort()
524 {
525 QList<RsyncDirListerOperation*> queue = d->operationQueue;
526 d->operationQueue.clear();
527
528 Q_FOREACH(RsyncDirListerOperation* oper, queue)
529 {
530 if(oper->isExecuting())
531 oper->abort();
532 }
533
534 qDeleteAll(queue);
535 }
536
537 void RsyncDirLister::setTopLevelEntries(const QList<RsyncEntry*>& entries)
538 {
539 d->topLevelEntries = entries;
540 d->topLevelEntriesFetched = true;
541 emit listingDone(NULL);
542 }
543
544 void RsyncDirLister::setChildrenFetched(RsyncEntry* entry)
545 {
546 entry->setChildrenFetched(true);
547 emit listingDone(entry);
548 }
549
550
551 ///////////////////////////////////////////////////////////////////////////////
552 // RsyncDirLister
553 ///////////////////////////////////////////////////////////////////////////////
554
555
556
557 struct FileIconCacheData
558 {
559 QFileIconProvider iconProvider;
560 QString tempDir;
561
562 QString suffix(const QString& fileName);
563 QString createTempFile(const QString& name);
564 };
565
566 FileIconCache& FileIconCache::instance()
567 {
568 static FileIconCache theInstance;
569 return theInstance;
570 }
571
572 FileIconCache::FileIconCache()
573 {
574 d = new FileIconCacheData;
575
576 QDir tempDir = QDir::tempPath();
577 tempDir.mkdir("FileIconCache");
578 d->tempDir = tempDir.absoluteFilePath("FileIconCache");
579 }
580
581 FileIconCache::~FileIconCache()
582 {
583 delete d;
584 }
585
586 QIcon FileIconCache::icon(const QUrlInfo& info) const
587 {
588 QIcon icon;
589
590 if(info.isDir())
591 icon = d->iconProvider.icon(QFileIconProvider::Folder);
592 else
593 {
594 QString suffix = d->suffix(info.name());
595 QPixmap pixmap;
596 if(QPixmapCache::find(suffix, pixmap))
597 return QIcon(pixmap);
598
599 if(suffix.isEmpty())
600 icon = d->iconProvider.icon(QFileIconProvider::File);
601 else
602 {
603 // Create an empty file with the same name as info.name()
604 // in the temporary folder
605 QString file = d->createTempFile(info.name());
606 if(file.isEmpty())
607 icon = d->iconProvider.icon(QFileIconProvider::File);
608 else
609 icon = d->iconProvider.icon(QFileInfo(file));
610 QFile::remove(file);
611 }
612
613 if(icon.isNull())
614 icon = d->iconProvider.icon(QFileIconProvider::File);
615
616 QPixmapCache::insert(suffix, icon.pixmap(32,32));
617 }
618
619 return icon;
620 }
621
622 QString FileIconCacheData::suffix(const QString& fileName)
623 {
624 QStringList comps = fileName.split(".");
625 comps.removeFirst();
626
627 QString suffix = comps.join(".");
628 return suffix;
629 }
630
631 QString FileIconCacheData::createTempFile(const QString& name)
632 {
633 QString completeName = QString("%1/%2").arg(tempDir).arg(name);
634
635 QFile file(completeName);
636 if(!file.open(QFile::WriteOnly))
637 return QString();
638
639 file.write(completeName.toLatin1());
640 file.close();
641
642 return completeName;
643 }
644