Repository navigation
Expand file tree
/
Copy pathtransfermanager.cpp
More file actions
277 lines (247 loc) · 11.4 KB
/
Copy pathtransfermanager.cpp
File metadata and controls
277 lines (247 loc) · 11.4 KB
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
/***********************************************************************
* File Name: transfermanager.cpp
* Author(s): Blake Azuela
* Date Created: 2024-05-19
* Description: Implementation file for the TransferManager class. This file
* includes robust methods for processing and transferring photo
* files, managing duplicates, and updating transfer progress.
* Utilizing a thread seperate from the GUI ensures a centralized and
* consistent transfer process. The class is meticulously designed
* to handle multithreading with atomic operations and integrates
* error handling to provide reliability. Additionally, it uses
* standard filesystem operations and regex for file management.
* License: MIT License
***********************************************************************/
#include <ctime>
#include <sstream>
#include <iostream>
#include <regex>
#include <set>
#include <filesystem>
#include "transfermanager.h"
TransferManager::TransferManager(QObject* parent)
: QObject(parent), progressCounter(0), configManager(AppConfig::get()) {
}
TransferManager::~TransferManager() {}
void TransferManager::processPhotoFiles(std::vector<std::unique_ptr<PhotoFileHandler>> *photoFileHandlers,
std::vector<std::unique_ptr<PhotoFileHandler>> *invalidPhotoFileHandlers,
bool moveFiles){
transferRunning = true;
cancelTransfer = false;
progressCounter = 0; // Reset progress
addDirectoryTransfers(*photoFileHandlers);
processDuplicatePhotoFiles();
if(configManager.config.getMoveInvalidFileMeta()){
addDirectoryTransfers(*invalidPhotoFileHandlers, configManager.config.getInvalidFileMetaDirectory());
}
processFileTransfers(moveFiles);
transferRunning = false;
resetTransferManager();
emit transferComplete(); // Notify that processing is finished
}
void TransferManager::processFileTransfers(bool moveFiles) {
size_t total = directoryTransferMap.size();
size_t current = 0;
for(auto dt = directoryTransferMap.begin(); dt != directoryTransferMap.end(); ++dt){
if(cancelTransfer){
progressCounter = 0;
break;
}
dt->second.transferFiles(moveFiles, configManager.config.getPhotosReplaceDashesWithUnderscores());
current++;
// Calculate progress as a percentage
progressCounter = static_cast<int>((static_cast<double>(current) / total) * 100);
}
}
void TransferManager::resetTransferManager(){
// Cleanup
progressCounter = 0;
directoryTransferMap.clear();
duplicatesTransferMap.clear();
photoTransfers.clear();
invalidPhotoTransfers.clear();
DuplicatePhotoTransfers.clear();
}
void TransferManager::processDuplicatePhotoFiles(){
try {
std::vector<std::unique_ptr<PhotoFileHandler>> duplicatesList;
if(configManager.config.getPhotosDuplicateIdentitySetting() == "File Names Match") {
for(auto dt = directoryTransferMap.begin(); dt != directoryTransferMap.end(); ++dt){
std::vector<std::unique_ptr<PhotoFileHandler>> tempDuplicates = dt->second.getAllPhotoFilenameDuplicates();
for (auto& duplicate : tempDuplicates) {
duplicatesList.push_back(std::move(duplicate));
}
}
}
if(configManager.config.getPhotosDuplicateIdentitySetting() == "All EXIF and Exact File Contents Match") {
for(auto dt = directoryTransferMap.begin(); dt != directoryTransferMap.end(); ++dt){
std::vector<std::unique_ptr<PhotoFileHandler>> tempDuplicates = dt->second.getAllPhotoEXIFDuplicates();
for (auto& duplicate : tempDuplicates) {
duplicatesList.push_back(std::move(duplicate));
}
}
}
addDuplicateTransfers(duplicatesList);
for(auto dt = directoryTransferMap.begin(); dt != directoryTransferMap.end();){
if(dt->second.getFilesToMoveCount() == 0){
dt = directoryTransferMap.erase(dt);
} else {
++dt;
}
}
} catch (const std::exception& e) {
std::cerr << "Exception caught in processDuplicatePhotoFiles: " << e.what() << std::endl;
throw;
} catch (...) {
std::cerr << "Unknown exception caught in processDuplicatePhotoFiles" << std::endl;
throw;
}
}
void TransferManager::addDirectoryTransfers(std::vector<std::unique_ptr<PhotoFileHandler>> &photoFileHandlers,
std::string outputDirectory) {
if(outputDirectory != ""){
directoryTransferMap[outputDirectory].setPhotoFilesToTransfer(photoFileHandlers);
directoryTransferMap[outputDirectory].setTargetDirectory(outputDirectory);
return;
}
for (auto& handler : photoFileHandlers) {
outputDirectory = generateDirectoryPath(handler.get());
directoryTransferMap[outputDirectory].addPhotoFileToTransfer(handler);
directoryTransferMap[outputDirectory].setTargetDirectory(outputDirectory);
}
}
void TransferManager::addDuplicateTransfers(std::vector<std::unique_ptr<PhotoFileHandler>> &photoFileHandlers) {
try {
std::string selection = configManager.config.getDuplicatesFoundSelection();
if(selection == "Add 'Copy##' and Move/Copy") {
for(auto& handler : photoFileHandlers){
std::string outputDirectory = generateDirectoryPath(handler.get());
handler->setTargetFileName(createNumericalFileName(handler->getTargetFileName(), outputDirectory));
directoryTransferMap[outputDirectory].addPhotoFileToTransfer(handler);
directoryTransferMap[outputDirectory].setTargetDirectory(outputDirectory);
}
} else if(selection == "Do Not Move or Copy") {
photoFileHandlers.clear();
} else if(selection == "Overwrite") {
for(auto& handler : photoFileHandlers){
handler->overwriteEnabled = true;
std::string outputDirectory = generateDirectoryPath(handler.get());
directoryTransferMap[outputDirectory].addPhotoFileToTransfer(handler);
directoryTransferMap[outputDirectory].setTargetDirectory(outputDirectory);
}
} else if(selection == "Move To Folder") {
for(auto& handler : photoFileHandlers){
std::string outputDirectory = configManager.config.getDuplicatesDirectory();
handler->setTargetFileName(createNumericalFileName(handler->getTargetFileName(), outputDirectory));
directoryTransferMap[outputDirectory].addPhotoFileToTransfer(handler);
directoryTransferMap[outputDirectory].setTargetDirectory(outputDirectory);
}
}
} catch (const std::exception& e) {
std::cerr << "Exception caught in addDuplicateTransfers: " << e.what() << std::endl;
throw;
} catch (...) {
std::cerr << "Unknown exception caught in addDuplicateTransfers" << std::endl;
throw;
}
}
std::string TransferManager::createNumericalFileName(const std::string& fileName,
const std::string& targetDirectory,
bool forceCopySuffix) {
std::filesystem::path dirPath(targetDirectory);
std::filesystem::path filePath(fileName);
std::string baseFilename = filePath.stem().string();
std::string extension = filePath.extension().string();
std::filesystem::path baseFilePath = dirPath / (baseFilename + extension);
if (!std::filesystem::exists(baseFilePath) && !forceCopySuffix) {
return fileName;
}
std::regex pattern("(_Copy\\d+)$");
std::smatch matches;
if (std::regex_search(baseFilename, matches, pattern)) {
baseFilename = baseFilename.substr(0, matches.position(0));
}
std::set<int> existingNumbers;
std::regex fullPattern(baseFilename + "_Copy(\\d+)" + std::regex_replace(extension, std::regex("\\."), "\\."));
for (const auto& entry : std::filesystem::directory_iterator(dirPath)) {
std::string filename = entry.path().filename().string();
if (std::regex_match(filename, matches, fullPattern) && matches.size() == 2) {
existingNumbers.insert(std::stoi(matches[1].str()));
}
}
std::vector<std::unique_ptr<PhotoFileHandler>> &photoFilesToTransfer = directoryTransferMap[targetDirectory].getPhotoFileToTransfer();
for(const auto& dt : photoFilesToTransfer){
std::string queuedFileName = dt->getTargetFileName();
std::filesystem::path queuedFilePath(queuedFileName);
std::string queuedBaseFilename = queuedFilePath.stem().string();
std::string queuedExtension = queuedFilePath.extension().string();
if (queuedExtension == extension && std::regex_match(queuedBaseFilename, matches, fullPattern) && matches.size() == 2) {
existingNumbers.insert(std::stoi(matches[1].str()));
}
}
int newNumber = 0;
while (existingNumbers.count(newNumber)) {
++newNumber;
}
std::ostringstream oss;
oss << baseFilename << "_Copy" << std::setw(2) << std::setfill('0') << newNumber << extension;
return oss.str();
}
std::string TransferManager::generateDirectoryPath(PhotoFileHandler* handler) {
auto timePoint = handler->getOriginalDateTime();
std::time_t time = std::chrono::system_clock::to_time_t(timePoint);
std::tm dateTime;
#if defined(_WIN32) || defined(_WIN64)
localtime_s(&dateTime, &time);
#else
localtime_r(&time, &dateTime);
#endif
std::vector<std::string> tokens;
std::istringstream iss(configManager.config.getPhotosOutputFolderStructureSelection());
std::string token;
std::string path = configManager.config.getOutputDirectory() + "/";
while (getline(iss, token, ',')) {
std::string trimmed;
std::remove_copy(token.begin(), token.end(), std::back_inserter(trimmed), ' ');
tokens.push_back(trimmed);
}
for (const auto& component : tokens) {
if (component == "CameraModel") {
path += handler->getCameraModel() + "/";
} else if (component == "Year") {
path += std::to_string(dateTime.tm_year + 1900) + "/";
} else if (component == "Month") {
path += getMonthName(dateTime.tm_mon + 1) + "/";
} else if (component == "Day") {
path += std::to_string(dateTime.tm_mday) + "/";
}
}
return QString(QDir::toNativeSeparators(QString::fromStdString(path))).toStdString();
}
std::string TransferManager::getMonthName(int monthNumber) {
// Map of month numbers to their corresponding month names
std::map<int, std::string> monthMap = {
{1, "January"},
{2, "February"},
{3, "March"},
{4, "April"},
{5, "May"},
{6, "June"},
{7, "July"},
{8, "August"},
{9, "September"},
{10, "October"},
{11, "November"},
{12, "December"}
};
// Check if the month number is valid
if (monthMap.find(monthNumber) != monthMap.end()) {
return monthMap[monthNumber];
} else {
// Invalid specification - just return int as string
return std::to_string(monthNumber);
}
}
int const TransferManager::getTransferProgress() {
return progressCounter.load();
}