-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainframe.cpp
More file actions
265 lines (221 loc) · 7.08 KB
/
mainframe.cpp
File metadata and controls
265 lines (221 loc) · 7.08 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
#include "mainframe.hpp"
#include <wx/dnd.h>
BEGIN_EVENT_TABLE(MainFrame, wxFrame)
EVT_BUTTON(ID_BTN_RENAME, MainFrame::OnBtnRename)
EVT_BUTTON(ID_BTN_HELP, MainFrame::OnBtnHelp)
END_EVENT_TABLE()
wxArrayString& split(const wxString &s, wxChar delim, wxArrayString &elems)
{
wxString item;
for(size_t i = 0; i < s.size(); ++i)
{
if(s[i] == delim)
{
if(!item.IsEmpty()) elems.Add(item);
item.Clear();
}
else item += s[i];
}
if(!item.IsEmpty()) elems.Add(item);
return elems;
}
wxString replace(const wxString& s, const wxChar& c, const wxString& r)
{
wxString result;
for(size_t i = 0; i < s.size(); ++i)
{
if(s[i] == c) result.Append(r);
else result.Append(s[i]);
}
return result;
}
wxArrayString separatePath(const wxString& s)
{
wxArrayString result;
result.Add(wxString());
result.Add(wxString());
result.Add(wxString());
int pos = -1;
for(int i = 0; i < s.size(); ++i)
if(s[i] == (wxChar)'\\') pos = i;
if(pos == -1)
{
result[1] = s;
}
else
{
result[0] = s.SubString(0, pos);
result[1] = s.SubString(pos+1, s.size());
}
// extension
pos = -1;
for(int i = 0; i < result[1].size(); ++i)
if(result[1][i] == (wxChar)'.') pos = i;
if(pos != -1)
{
result[2] = result[1].SubString(pos, s.size());
result[1] = result[1].SubString(0, pos-1);
}
return result;
}
wxString getNumber(int current, int max)
{
int a = 0;
int b = 0;
int c = current;
while(max >= 10)
{
a++;
max /= 10;
}
while(c >= 10)
{
b++;
c /= 10;
}
wxString result = wxString::Format(wxT("%i"),current);
for(int i = 0 ; i < a - b; ++i)
result.insert(result.begin(), '0');
return result;
}
class DnDFile : public wxFileDropTarget
{
public:
DnDFile(MainFrame *pOwner = NULL) { m_pOwner = pOwner; }
virtual bool OnDropFiles(wxCoord x, wxCoord y,const wxArrayString& filenames)
{
if(filenames.size() > 0)
{
m_pOwner->OnDropFiles(filenames);
return true;
}
return false;
};
private:
MainFrame *m_pOwner;
};
MainFrame::MainFrame(const wxString& title) : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(500, 500), wxDEFAULT_FRAME_STYLE)
{
wxFrame::SetIcon(wxIcon(_T("MAINICON")));
SetMinSize(wxSize(500, 500));
wxPanel *panel = new wxPanel(this, -1);
wxBoxSizer *vbox = new wxBoxSizer(wxVERTICAL);
wxBoxSizer *hbox1 = new wxBoxSizer(wxHORIZONTAL);
wxBoxSizer *hbox2 = new wxBoxSizer(wxHORIZONTAL);
wxBoxSizer *hbox3 = new wxBoxSizer(wxHORIZONTAL);
wxBoxSizer *hbox4 = new wxBoxSizer(wxHORIZONTAL);
vbox->Add(hbox1, 0, wxEXPAND);
vbox->Add(hbox2, 0, wxALIGN_LEFT);
vbox->Add(hbox3, 0, wxALIGN_LEFT | wxEXPAND);
vbox->Add(hbox4, 0, wxEXPAND);
m_rename = new wxButton(panel, ID_BTN_RENAME, _T("Rename with :"));
hbox1->Add(m_rename);
m_output = new wxTextCtrl(panel, -1, _T(""));
hbox1->Add(m_output, 20);
m_help = new wxButton(panel, ID_BTN_HELP, _T("?"));
hbox1->Add(m_help, 1);
hbox2->Add(new wxStaticText(panel, -1, _T("Start from number :")));
m_start = new wxTextCtrl(panel, -1, _T("1"));
hbox2->Add(m_start);
m_extension = new wxCheckBox(panel, -1, _T("keep extension"));
hbox2->Add(m_extension);
m_clear = new wxCheckBox(panel, -1, _T("clear list after renaming"));
hbox2->Add(m_clear);
hbox3->Add(new wxStaticLine(panel, -1, wxPoint(0, 5), wxSize(1, 1)), 1);
hbox4->Add(new wxStaticText(panel, -1, _T("File list")));
m_list = new wxTextCtrl(panel, -1, _T(""), wxPoint(0, 0), wxSize(0, 0), wxTE_MULTILINE);
vbox->Add(m_list, 1, wxEXPAND);
panel->SetSizer(vbox);
this->SetDropTarget((wxDropTarget*)new DnDFile(this));
}
MainFrame::~MainFrame()
{
}
void MainFrame::OnDropFiles(const wxArrayString& filenames)
{
for(size_t i = 0; i < filenames.size(); ++i)
{
if((i == 0 && !m_list->GetValue().IsEmpty()) || i != 0) m_list->AppendText(wxString(_T("\n")));
m_list->AppendText(filenames[i]);
}
}
void MainFrame::OnBtnRename(wxCommandEvent &event)
{
wxString out = m_output->GetValue();
if(out.IsEmpty())
{
wxMessageBox(_T("Please specify the output name"));
return;
}
int joker = 0;
for(size_t i = 0; i < out.size(); ++i)
{
if(out[i] == '*') joker++;
}
if(joker == 0)
{
wxMessageBox(_T("Joker '*' is missing in the output name"));
return;
}
long lstart;
if(!m_start->GetValue().ToLong(&lstart))
{
wxMessageBox(_T("Invalid starting number"));
return;
}
int istart = (int)lstart;
if(istart < 0)
{
wxMessageBox(_T("Starting number must be positive"));
return;
}
joker = 0; // will be used to count errors
wxArrayString files;
split(m_list->GetValue(), '\n', files);
wxLogNull *log = new wxLogNull();
// remove empty string
for(size_t i = 0; i < files.Count(); ++i)
{
if(files[i].IsEmpty())
{
files.erase(files.begin()+i);
i--;
}
}
if(files.GetCount() == 0) return;
bool hasExt = m_extension->GetValue();
wxArrayString exts;
// security
for(size_t i = 0; i < files.Count(); ++i)
{
wxArrayString f = separatePath(files[i]);
wxString newname = replace(_T("renamer_temporary_file_security_*"), '*', getNumber(i+lstart, files.Count()+lstart-1));
exts.Add(_T(""));
if(!wxRenameFile(f[0] + f[1] + f[2], f[0] + newname, true))
{
joker++;
files[i].Clear();
}
else
{
files[i] = f[0] + newname;
if(hasExt) exts[i] = f[2];
}
}
for(size_t i = 0; i < files.Count(); ++i)
{
if(!files[i].IsEmpty())
{
wxArrayString f = separatePath(files[i]);
wxString newname = replace(out, '*', getNumber(i+lstart, files.Count()+lstart-1));
if(!wxRenameFile(f[0] + f[1], f[0] + newname + exts[i], true)) joker++;
}
}
delete log;
wxMessageBox(_T("Done\n") + wxString::Format(wxT("%i"),files.Count()) + _T(" file(s)\n") + wxString::Format(wxT("%i"),joker) + _T(" error(s)\n"));
if(m_clear->GetValue()) m_list->Clear();
}
void MainFrame::OnBtnHelp(wxCommandEvent &event)
{
wxMessageBox(_T("Usage\n- Add your files in the order you want them renamed.\nYou can use the full path or relative path.\nDrag and drop also works.\n\n- Set the output name. It MUST contain the '*' character.\nExample :\t\"example*.txt\"\nResult :\t\"example1.txt\"\n\t\"example2.txt\"\n\tetc...\n\n- Press the rename button"), _T("Help"), wxICON_QUESTION);
}