/
MainWindow.cpp
185 lines (154 loc) · 4.53 KB
/
MainWindow.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
#include "MainWindow.h"
#include <QtWidgets>
#include "SystemTrayIcon.h"
#include "Common.h"
MainWindow::MainWindow(QWidget *parent)
: QDialog(parent), isStart_(false)
{
this->setWindowFlags(Qt::WindowMinimizeButtonHint | Qt::WindowCloseButtonHint);
QLabel *fromLabel = new QLabel(tr("From:"), this);
QLabel *toLabel = new QLabel(tr("To :"), this);
leFrom_ = new QLineEdit(this);
leTo_ = new QLineEdit(this);
pbFromBrowser_ = new QPushButton(tr("Browser"), this);
pbToBrowser_ = new QPushButton(tr("Browser"), this);
pbStart_ = new QPushButton(tr("Start"), this);
tray_ = new SystemTrayIcon(this);
leFrom_->setReadOnly(true);
leTo_->setReadOnly(true);
connect(pbFromBrowser_, SIGNAL(clicked()), this, SLOT(slotFromBrowser()));
connect(pbToBrowser_, SIGNAL(clicked()), this, SLOT(slotToBrowser()));
connect(pbStart_, SIGNAL(clicked()), this, SLOT(slotStart()));
connect(tray_, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(iconIsActived(QSystemTrayIcon::ActivationReason)));
tray_->show();
QHBoxLayout *fromLayout = new QHBoxLayout;
fromLayout->addWidget(fromLabel);
fromLayout->addWidget(leFrom_);
fromLayout->addWidget(pbFromBrowser_);
QHBoxLayout *toLayout = new QHBoxLayout;
toLayout->addWidget(toLabel);
toLayout->addWidget(leTo_);
toLayout->addWidget(pbToBrowser_);
QHBoxLayout *startLayout = new QHBoxLayout;
startLayout->addWidget(pbStart_);
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addLayout(fromLayout);
mainLayout->addLayout(toLayout);
mainLayout->addLayout(startLayout);
this->setLayout(mainLayout);
watcher_ = new QFileSystemWatcher(this);
}
MainWindow::~MainWindow(void)
{
}
QString MainWindow::oppositeSide(QString path, bool isFrom)
{
QString fromDir = leFrom_->text();
QString toDir = leTo_->text();
if (fromDir.isEmpty() || toDir.isEmpty() || path.isEmpty()) {
return "";
}
if (isFrom) {
return path.replace(fromDir, toDir);
} else {
return path.replace(toDir, fromDir);
}
}
bool MainWindow::isOppositeSideExist(QString path, bool isFrom)
{
QFileInfo info(oppositeSide(path, isFrom));
return info.exists();
}
void MainWindow::slotFromBrowser()
{
QString curDir = currentDirFromDlg();
leFrom_->setText(curDir);
if (!watcher_->files().empty()) {
watcher_->removePaths(watcher_->files());
}
if (!watcher_->directories().empty()) {
watcher_->removePaths(watcher_->directories());
}
QStringList files, subdirs;
Common::foreachDir(leFrom_->text(), files, subdirs, true);
watcher_->addPaths(files);
watcher_->addPath(leFrom_->text());
watcher_->addPaths(subdirs);
}
void MainWindow::slotToBrowser()
{
QString curDir = currentDirFromDlg();
leTo_->setText(curDir);
start(false);
}
void MainWindow::slotStart()
{
if (leFrom_->text().isEmpty() || leTo_->text().isEmpty()) {
return;
}
start(!isStart_);
}
QString MainWindow::currentDirFromDlg()
{
QFileDialog dlg(this);
dlg.setFileMode(QFileDialog::Directory);
if (dlg.exec()) {
return dlg.directory().path();
}
return "";
}
void MainWindow::start(bool isTrue)
{
if (isTrue) {
isStart_ = true;
pbStart_->setText(tr("Stop"));
connect(watcher_, SIGNAL(fileChanged(QString)), this, SLOT(slotFileChanged(QString)));
connect(watcher_, SIGNAL(directoryChanged(QString)), this, SLOT(slotDirectoryChanged(QString)));
Common::asyncCopy(leFrom_->text(), leTo_->text());
} else {
isStart_ = false;
pbStart_->setText(tr("Start"));
disconnect(watcher_, SIGNAL(fileChanged(QString)), this, SLOT(slotFileChanged(QString)));
disconnect(watcher_, SIGNAL(directoryChanged(QString)), this, SLOT(slotDirectoryChanged(QString)));
}
}
void MainWindow::slotFileChanged(const QString &path)
{
QFileInfo info(path);
QString dstDir;
if (info.isFile()) {
dstDir = oppositeSide(info.absolutePath());
} else if (info.isDir()) {
dstDir = oppositeSide(path);
} else {
return;
}
Common::asyncCopy(path, dstDir);
}
void MainWindow::slotDirectoryChanged(const QString &dir)
{
QStringList files, subdirs;
Common::foreachDir(dir, files, subdirs);
foreach (const QString &file, files) {
if (!watcher_->files().contains(file)) {
slotFileChanged(file);
}
}
foreach (const QString &subdir, subdirs) {
if (!watcher_->directories().contains(subdir)) {
slotFileChanged(subdir);
}
}
QStringList dstFiles, dstSubdis;
Common::foreachDir(oppositeSide(dir), dstFiles, dstSubdis);
foreach (const QString &file, dstFiles) {
if (!isOppositeSideExist(file, false)) {
Common::deleteFile(file);
}
}
foreach (const QString &subdir, dstSubdis) {
if (!isOppositeSideExist(subdir, false)) {
Common::deleteDir(subdir);
}
}
}