-
Notifications
You must be signed in to change notification settings - Fork 0
/
myframe.cpp
245 lines (173 loc) · 5.39 KB
/
myframe.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
#include "myframe.h"
#include "progressbar.h"
using std::cout;
using std::endl;
MyFrame::MyFrame( wxWindow* parent )
:
MyFrameBase( parent )
{
}
void MyFrame::onInputSelect(wxFileDirPickerEvent& event)
{
inDirName = inDirPicker->GetDirName();
auto count = createFileList(inDirName, *inDirTextCtrl, inFiles);
*debugTextCtrl << _("Input File Count: ") << (int)count << _("\n");
}
void MyFrame::onOutputSelect(wxFileDirPickerEvent& event)
{
outDirName = outDirPicker->GetDirName();
auto count = createFileList(outDirName, *outDirTextCtrl, outFiles);
*debugTextCtrl << _("Output File Count: ") << (int)count << _("\n");
}
void MyFrame::onUnused( wxCommandEvent& event )
{
// TODO: Implement onUnused
std::cout << "On Unused" << std::endl;
// TODO: Implement onUnused
std::cout << "On Unused" << std::endl;
}
void MyFrame::onClose( wxCommandEvent& event )
{
Close(true);
}
void MyFrame::onRun( wxCommandEvent& event )
{
std::cout << "On Run" << std::endl;
if (!outDirName.DirExists() || !inDirName.DirExists()) {
std::cout << "You must select a valid directory" << endl;
return;
}
int addCount = 0, sameCount = 0;
std::vector<FileInfo*> toAddList;
for (auto& iterator : inFiles ) {
FileInfo& cmp1 = iterator.second;
FileInfo& cmp2 = outFiles[cmp1.getShortPath()];
if (cmp1 > cmp2) {
++addCount;
cmp1.setCopytoPath(outDirName.GetFullPath());
toAddList.push_back(&cmp1);
*debugTextCtrl << "Adding: " << cmp1.getShortPath() << "\n";
} else if (cmp1 == cmp2) {
//*debugTextCtrl << "Same: " << cmp1.getShortPath() << "\n";
++sameCount;
}
else {
*debugTextCtrl << "Not Sure What's going on" << cmp1.getShortPath() << "\n";
}
}
*debugTextCtrl << "Files added: " << addCount << "\n";
*debugTextCtrl << "Files same: " << sameCount << "\n";
if (copyFiles() ){
setProgressParams(addCount, "Moving Files");
*debugTextCtrl << "Copy Files\n";
int step = 0;
for (auto copyInfo : toAddList){
// Here is where the magic happens
setProgress(++step);
bool success = false, dirExists = false;
wxString dirPath = wxFileName(copyInfo->getCopyToPath()).GetPath();
dirExists = wxDir::Exists(dirPath);
if (!dirExists){
dirExists = wxFileName::Mkdir(dirPath, wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL);
}
if (dirExists) {
success = wxCopyFile(copyInfo->getFullPath(), copyInfo->getCopyToPath(), true);
*debugTextCtrl << "Copy From " << copyInfo->getFullPath() << "\n to " << copyInfo->getCopyToPath() << " ";
}
else {
*debugTextCtrl << "Error Dir Does not Exist. ";
}
if (success)
*debugTextCtrl << "Successful!\n";
else
*debugTextCtrl << "Failed!!\n";
// Magic over
}
hideProgress();
}
else {
*debugTextCtrl << "No Copy Files\n";
}
}
void MyFrame::onAbout( wxCommandEvent& event )
{
wxMessageBox(_("Back up Program.\n"
"Description:\n"
" -Takes two directories, and input directory and and output directory.\n"
" -The program compares the files between the two directories and copies any files "
"that are either missing from the \"output\" directory or have been modified more recently "
"in the \"input\" directory.\n"
" -This program does not compress any files nor does it back up at regular intervals.\n"
"Notes:\n"
" -This program seems to work pretty well. I have not tested it thorougly however.\n"
" -I did verify that it will create directories in the output folder if they do not exist.\n"
" -There are definately improvements that could be made in terms of efficencies but it works well enough.\n"
),
_("About Backup App"),
wxOK | wxICON_INFORMATION, this);
}
void MyFrame::onClear(wxCommandEvent& event){
inFiles.clear();
outFiles.clear();
inDirPicker->SetPath("");
outDirPicker->SetPath("");
inDirTextCtrl->Clear();
outDirTextCtrl->Clear();
//inDirName
}
MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size )
: MyFrameBase(NULL , wxID_ANY, title , pos , size )
{
progressBar = new ProgressBar(NULL);
SetIcon(wxICON(myicon));
}
unsigned int MyFrame::createFileList(wxFileName& dirName, wxTextCtrl& outputTextCtrl, std::map<wxString, FileInfo>& files )
{
wxArrayString dirList;
wxDir::GetAllFiles(dirName.GetFullPath(), &dirList);
outputTextCtrl << _("Path:\n");
outputTextCtrl << dirName.GetFullPath() << _("\n");
//std::shared_ptr<wxString> parentDirName(new wxString(dirName.GetFullPath()));
unsigned int count = dirList.GetCount();
setProgressParams(count, "Process File List");
for (unsigned int i = 0; i < count; ++i){
setProgress(i + 1);
FileInfo fileInfo(dirName.GetFullPath(), dirList[i]);
files[fileInfo.getShortPath()] = fileInfo;
outputTextCtrl << fileInfo.getShortPath() << "\n";
}
hideProgress();
return count;
}
MyFrame::~MyFrame()
{
restoreStdin();
progressBar->Destroy();
}
void MyFrame::setProgressParams(unsigned int count, wxString title)
{
progressBar->setProgressRange(count);
progressBar->setProgressValue(0);
progressBar->setTitle(title);
progressBar->Show();
}
void MyFrame::stealStdin()
{
sbOld = std::cout.rdbuf();
std::cout.rdbuf(debugTextCtrl);
}
void MyFrame::restoreStdin()
{
if(sbOld)
std::cout.rdbuf(sbOld);
}
bool MyFrame::copyFiles(){
wxMenuItem* copy_files = runMenu->FindItem(COPY_FILE);
if (copy_files){
if (copy_files->IsChecked())
return true;
return false;
}
*debugTextCtrl << "Error copy files menu item not found\n";
return false;
}