-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPhotos_Copy.cpp
296 lines (260 loc) · 9.34 KB
/
Photos_Copy.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#include "Photos_Copy.h"
#include "ui_Photos_Copy.h"
#include <QDir>
#include "ExivInfo.h"
#include <QFileDialog>
#include <QStandardPaths>
#include <QDebug>
#include <QDateTime>
#include <QTimer>
#include "LICENSE.h"
Photos_Copy::Photos_Copy(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Photos_Copy)
{
ui->setupUi(this);
m_IndexThreadRunning = false;
m_CopyThreadRunning = false;
connect(ui->pushButton_Stop, &QPushButton::clicked, this, &Photos_Copy::Slot_BtnClick);
connect(ui->pushButton_Start, &QPushButton::clicked, this, &Photos_Copy::Slot_BtnClick);
connect(ui->pushButton_License, &QPushButton::clicked, this, &Photos_Copy::Slot_BtnClick);
connect(ui->pushButton_RefreshIndex, &QPushButton::clicked, this, &Photos_Copy::Slot_BtnClick);
connect(ui->pushButton_SelectTargetPath, &QPushButton::clicked, this, &Photos_Copy::Slot_BtnClick);
connect(ui->pushButton_SlelctSourcePath, &QPushButton::clicked, this, &Photos_Copy::Slot_BtnClick);
connect(this, &Photos_Copy::Sign_IndexInformation, this, &Photos_Copy::Slot_IndexInformation);
connect(this, &Photos_Copy::Sign_CopyInformation, this, &Photos_Copy::Slot_CopyInformation);
ui->progressBar_Task->setVisible(false);
ui->progressBar_File->setVisible(false);
ui->pushButton_Stop->setEnabled(false);
}
Photos_Copy::~Photos_Copy()
{
delete ui;
}
void Photos_Copy::Slot_BtnClick()
{
QPushButton *Btn = static_cast<QPushButton*>(sender());
if (Btn == nullptr){
return;
}
if (Btn == ui->pushButton_Start){
m_CopyThreadRunning = true;
auto ThreadPtr = new std::thread(&Photos_Copy::CopyThread, this);
ThreadPtr->detach();
return;
}
if (Btn == ui->pushButton_License){
LICENSE License(this);
License.exec();
return;
}
if (Btn == ui->pushButton_Stop){
m_CopyThreadRunning = false;
return;
}
if (Btn == ui->pushButton_RefreshIndex){
SourceRootPath = ui->lineEdit_SourcePath->text();
auto Thread_Ptr = new std::thread(&Photos_Copy::IndexThread, this);
Thread_Ptr->detach();
return;
}
// QString HomePath =
auto DirPath = QFileDialog::getExistingDirectory(this, "choose src Directory", QStandardPaths::standardLocations(QStandardPaths::HomeLocation).first());
if (DirPath.isEmpty()){
return;
}
if (Btn == ui->pushButton_SlelctSourcePath){
ui->lineEdit_SourcePath->setText(DirPath);
SourceRootPath = DirPath;
ui->treeWidget_Source->clear();
auto Thread_Ptr = new std::thread(&Photos_Copy::IndexThread, this);
Thread_Ptr->detach();
}
if (Btn == ui->pushButton_SelectTargetPath){
ui->LineEdit_TargetPath->setText(DirPath);
}
}
void Photos_Copy::Slot_IndexInformation(int state, QString FilePath)
{
/*
* state:
* 0 : Start
* 1 : FindFile
* 2 : Stop
*/
if (state == 0){
ui->treeWidget_Source->clear();
ui->treeWidget_Target->clear();
m_SrcFileIndex.clear();
m_TarFileIndex.clear();
return;
}
if (state == 1){
QFileInfo FileInfo(FilePath);
QTreeWidgetItem *Item = new QTreeWidgetItem;
Item->setText(0, FileInfo.fileName());
Item->setText(1, QString::number(FileInfo.size()));
Item->setText(2, FileInfo.absolutePath());
ui->treeWidget_Source->addTopLevelItem(Item);
m_SrcFileIndex[FileInfo.absoluteFilePath()] = Item;
}
}
void Photos_Copy::Slot_CopyInformation(int state, unsigned int FullLenth, unsigned int CopyLenth, QString FileName, QString SrcDir, QString TarDir)
{
if (state == 0){
ui->pushButton_Stop->setEnabled(true);
ui->pushButton_Start->setEnabled(false);
ui->pushButton_RefreshIndex->setEnabled(false);
ui->pushButton_SelectTargetPath->setEnabled(false);
ui->pushButton_SlelctSourcePath->setEnabled(false);
ui->progressBar_Task->setVisible(true);
ui->progressBar_File->setVisible(true);
ui->lineEdit_SourcePath->setEnabled(false);
ui->LineEdit_TargetPath->setEnabled(false);
ui->lineEdit_TargetDirectroFormat->setEnabled(false);
ui->progressBar_Task->setMaximum(m_SrcFileIndex.count());
ui->progressBar_Task->setValue(0);
return;
}
if (state == 1){
ui->label_FileName->setText("Copying " + FileName);
ui->progressBar_File->setMaximum(FullLenth);
return;
}
if (state == 2){
ui->progressBar_File->setValue(CopyLenth);
return;
}
if (state == 3){
QTreeWidgetItem *Item = new QTreeWidgetItem;
QFileInfo FileInfo(TarDir + "/" + FileName);
Item->setText(0, FileInfo.fileName());
Item->setText(1, QString::number(FileInfo.size()));
Item->setText(2, FileInfo.absolutePath());
ui->treeWidget_Target->addTopLevelItem(Item);
auto absoluteFilePath = SrcDir + "/" + FileName;
delete m_SrcFileIndex[absoluteFilePath];
m_SrcFileIndex.remove(absoluteFilePath);
m_TarFileIndex[absoluteFilePath] = Item;
ui->progressBar_Task->setValue(m_TarFileIndex.count());
return;
}
if (state == 4){
ui->pushButton_Stop->setEnabled(false);
ui->pushButton_Start->setEnabled(true);
ui->pushButton_RefreshIndex->setEnabled(true);
ui->pushButton_SelectTargetPath->setEnabled(true);
ui->pushButton_SlelctSourcePath->setEnabled(true);
ui->lineEdit_SourcePath->setEnabled(true);
ui->LineEdit_TargetPath->setEnabled(true);
ui->lineEdit_TargetDirectroFormat->setEnabled(true);
ui->label_FileName->setText("Copy end.");
QTimer::singleShot(5000, [&]{
ui->label_FileName->setText("");
ui->progressBar_Task->setVisible(false);
ui->progressBar_Task->setValue(0);
ui->progressBar_Task->setMaximum(1);
ui->progressBar_File->setVisible(false);
ui->progressBar_File->setValue(0);
ui->progressBar_File->setMaximum(1);
});
return;
}
}
void Photos_Copy::Index(QString RootPath)
{
QDir Directory(RootPath);
if(!Directory.exists())
{
return;
}
Directory.setFilter(QDir::AllEntries | QDir::NoSymLinks);
QFileInfoList FIleList = Directory.entryInfoList();
foreach(auto var, FIleList){
if (var.isDir()){
if (var.fileName() == "." || var.fileName() == ".."){
continue;
}
Index(var.absoluteFilePath());
}
if (var.isSymLink()){
continue;
}
if (var.isFile()){
if (var.suffix().toUpper() == "DB"){
continue;
}
if (var.suffix().toUpper() == "XML"){
continue;
}
emit Sign_IndexInformation(1, var.absoluteFilePath());
}
}
}
void Photos_Copy::IndexThread()
{
emit Sign_IndexInformation(0, "");
Index(SourceRootPath);
emit Sign_IndexInformation(2, "");
}
void Photos_Copy::CopyThread()
{
emit Sign_CopyInformation(0, 0, 0, "", "", "");
QList<QString> Files;
QTreeWidgetItemIterator it(ui->treeWidget_Source);
while (*it) {
Files.append((*it)->text(2) + "/" + (*it)->text(0));
++it;
}
QByteArray FileData;
std::string ExifValue;
QString TargetDirectroFormat = ui->lineEdit_TargetDirectroFormat->text();
foreach(auto var, Files){
if (m_CopyThreadRunning == false){
break;
}
ExivInfo SrcExifInfo(var.toStdString());
SrcExifInfo.GetExifKey("Exif.Image.DateTime", ExifValue);
QString DirectroPath = ui->LineEdit_TargetPath->text() + "/";
DirectroPath += QDateTime::fromString(QString::fromStdString(ExifValue), "yyyy:MM:dd hh:mm:ss").toString(TargetDirectroFormat);
QDir TargetDirectro;
if (TargetDirectro.exists(DirectroPath) == false){
TargetDirectro.mkpath(DirectroPath);
}
QFileInfo SrcFileInfo(var);
QFile SrcFile(var);
if (SrcFile.open(QIODevice::ReadOnly) == false){
continue;
}
QFile TargetFile(DirectroPath + "/" + SrcFileInfo.fileName());
if (TargetFile.open(QIODevice::WriteOnly) == false){
SrcFile.close();
continue;
}
FileData.clear();
unsigned int CopyedLenth = 0;
emit Sign_CopyInformation(1, SrcFileInfo.size(), CopyedLenth, SrcFileInfo.fileName(), SrcFileInfo.absolutePath(), DirectroPath);
do{
FileData = SrcFile.read(4096);
CopyedLenth += FileData.length();
TargetFile.write(FileData);
emit Sign_CopyInformation(2, SrcFileInfo.size(), CopyedLenth, SrcFileInfo.fileName(), SrcFileInfo.absolutePath(), DirectroPath);
}while(FileData.size() == 4096);
SrcFile.close();
if (ui->checkBox_DeleteDourceFile->checkState() == Qt::Checked){
SrcFile.remove();
}
TargetFile.close();
emit Sign_CopyInformation(3, SrcFileInfo.size(), CopyedLenth, SrcFileInfo.fileName(), SrcFileInfo.absolutePath(), DirectroPath);
}
emit Sign_CopyInformation(4, 0, 0, "", "", "");
m_CopyThreadRunning = false;
}
bool Photos_Copy::ReadExifInfo(QString Path)
{
ExivInfo Info(Path.toStdString());
if (Info.IsEmpty() == true){
return false;
}
return true;
}