-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_manager.cpp
124 lines (109 loc) · 3.34 KB
/
file_manager.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
#include "file_manager.hpp"
#include <QDirIterator>
#include <QDateTime>
QStringList file_manager::list_files(const QDir& dir)
{
QStringList files;
QDirIterator it(dir, QDirIterator::Subdirectories);
while(it.hasNext())
{
files << it.next().replace(dir.path(), "");
}
return files;
}
file_manager::file_manager(QObject *parent) : QObject(parent) { }
void file_manager::set_source(const QString& path)
{
_source.setPath(path);
emit source_exists(_source.exists());
}
void file_manager::set_destination(const QString& path)
{
_destination.setPath(path);
emit destinations_exists(_destination.exists());
}
void file_manager::build_lists()
{
_source.setFilter(QDir::Files | QDir::Dirs | QDir::NoDot | QDir::NoDotDot);
_destination.setFilter(QDir::Files | QDir::Dirs | QDir::NoDot | QDir::NoDotDot);
QStringList source_files = list_files(_source);
QStringList destination_files = list_files(_destination);
{
QStringList new_files_list;
QStringList update_files_list;
for(const QString& file : source_files)
{
if(!destination_files.contains(file))
{
new_files_list << file;
}
else if(QFileInfo(_source.path()+file).lastModified() >
QFileInfo(_destination.path()+file).lastModified() && QFileInfo(_destination.path()+file).isFile())
{
update_files_list << file;
}
}
emit new_files(new_files_list);
emit update_files(update_files_list);
}
QStringList deleted_files_list;
for(const QString& file : destination_files)
{
if(!source_files.contains(file))
{
deleted_files_list << file;
}
}
emit delete_files(deleted_files_list);
}
void file_manager::synchronise(const QStringList& new_files, const QStringList& updated_files,
const QStringList& deleted_files)
{
emit total_progress(new_files.count() + updated_files.count() + deleted_files.count());
int progress = 1;
for(auto& file : new_files)
{
emit current_progress(progress, "Copying " + file);
if(QFileInfo(_source.path()+file).isFile() && !QFile::copy(_source.path() + file, _destination.path()+file))
{
emit error("Unable to copy " + file + " to destination directory!");
}
else if(QFileInfo(_source.path()+file).isDir() && !QDir().mkdir(_destination.path()+file))
{
emit error("Unable to copy " + file + " to destination directory!");
}
++progress;
}
for(auto it = updated_files.rbegin(); it != updated_files.rend(); ++it)
{
emit current_progress(progress, "Updating " + *it);
if(QFileInfo(_destination.path()+*it).isDir())
{
continue;
}
if(!QFile::remove(_destination.path()+*it))
{
emit error("Unable to override " + *it + " in destination directory!");
continue;
}
if(!QFile::copy(_source.path() + *it, _destination.path()+*it))
{
emit error("Unable to copy " + *it + " to destination directory!");
}
++progress;
}
for(auto it = deleted_files.rbegin(); it != deleted_files.rend(); ++it)
{
emit current_progress(progress, "Deleting " + *it);
if(QFileInfo(_destination.path()+*it).isFile() && !QFile::remove(_destination.path()+*it))
{
emit error("Unable to delete " + *it + " in destination directory!");
}
else if(QFileInfo(_destination.path()+*it).isDir() && !QDir(_destination.path()+*it).removeRecursively())
{
emit error("Unable to delete flder " + *it + " in destination directory!");
}
++progress;
}
emit done();
}