-
Notifications
You must be signed in to change notification settings - Fork 0
/
mover.cpp
205 lines (177 loc) · 6.76 KB
/
mover.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
/*
Copyright 2011 Kapustin Andrey
This file is part of Visual move.
Visual move is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Visual move is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with FileRouter. If not, see <http://www.gnu.org/licenses/>.
Êîä ðàñïðîñòðàíÿåòñÿ ïî ëèöåíçèè GPL 3
Àâòîð: Êàïóñòèí Àíäðåé, 2011 ã.
*/
#include "mover.h"
#include <QApplication>
#include <QFile>
#include <QDir>
#include <QDateTime>
#include <QMessageBox>
Mover::Mover()
{
QSettings *sett=new QSettings(QApplication::applicationDirPath()+"/vmove.ini",QSettings::IniFormat);// (QSettings::IniFormat,QSettings::SystemScope,"ini","vmove");
sett->setIniCodec("Windows-1251");
// Òåñòîâàÿ çàïèñü íàñòðîåê
/*
sett->setValue("/settings/BackupEnabled",true);
sett->setValue("/settings/BackupDir","d:/temp/2");
sett->setValue("/blocks/name0","Test 1");
sett->setValue("/blocks/sourceDir0","d:/temp/0");
sett->setValue("/blocks/destDir0","d:/temp/1");
*/
// Êîíåö òåñòîâîé çàïèñè
// ÷òåíèå áàçîâûõ íàñòðîåê
backupEnabled=sett->value("/settings/BackupEnabled",false).toBool(); // Áýêàï âêëþ÷åí
backupDir=sett->value("/settings/BackupDir","").toString(); // Êàòàëîã áýêàïà
refreshInterval=sett->value("/settings/refreshInterval",0).toInt(); // Ïåðèîä àâòîîîáíîâëåíèÿ ñåê
buttonAllLeft=sett->value("/settings/buttonLeft",true).toBool(); // Êíîïêà âñå íàëåâî
buttonAllRight=sett->value("/settings/buttonRight",true).toBool(); // Êíîïêà âñå íàïðàâî
buttonAllBoth=sett->value("/settings/buttonBoth",true).toBool(); // Êíîïêà âñå âî âñå ñòîðîíû
QString nameb,source,dest,sourcemask,destmask,strDirect;
MoveBlock::MoveDirection direct;
int i=0;
do
//for (int i=0;i<count;i++)
{
nameb=sett->value("/blocks/name"+QString::number(i),"").toString();
source=sett->value("/blocks/sourceDir"+QString::number(i),"").toString();
dest=sett->value("/blocks/destDir"+QString::number(i),"").toString();
sourcemask=sett->value("/blocks/SourceMask"+QString::number(i),"").toString();
destmask=sett->value("/blocks/DestMask"+QString::number(i),"").toString();
strDirect=sett->value("/blocks/direction"+QString::number(i),"right").toString();
direct=MoveBlock::ToRight;
if (strDirect.compare("right",Qt::CaseInsensitive)==0) direct=MoveBlock::ToRight;
if (strDirect.compare("left",Qt::CaseInsensitive)==0) direct=MoveBlock::ToLeft;
if (strDirect.compare("both",Qt::CaseInsensitive)==0) direct=MoveBlock::ToBoth;
if (source=="") break;
//count=i+1;
moveBlocks.append(new MoveBlock(nameb,source,dest,sourcemask,destmask,direct));
i++;
}
while (source!="");
count=moveBlocks.count();
refresh ();
delete sett;
}
Mover::~Mover()
{
while (!moveBlocks.isEmpty())
delete moveBlocks.takeFirst();
}
void Mover::refresh()
{
for (int i=0;i<moveBlocks.count();i++)
{
moveBlocks[i]->refresh();
}
}
// Ïåðåìåùåíèå ôàéëà
void Mover::move(int indexBlock,int indexFile,MoveBlock::MoveDirection movDirect/*=MoveBlock::ToRight*/)
{
QString sfullFileName,dfullFileName,fileName;
switch (movDirect)
{
case MoveBlock::ToRight:
fileName=moveBlocks[indexBlock]->msourceDir.fileNames[indexFile];
sfullFileName=moveBlocks[indexBlock]->msourceDir.mDir+"/"+fileName;
dfullFileName=moveBlocks[indexBlock]->mdestDir.mDir+"/"+fileName;
break;
case MoveBlock::ToLeft:
fileName=moveBlocks[indexBlock]->mdestDir.fileNames[indexFile];
sfullFileName=moveBlocks[indexBlock]->mdestDir.mDir+"/"+fileName;
dfullFileName=moveBlocks[indexBlock]->msourceDir.mDir+"/"+fileName;
}
QFile sourfile(sfullFileName);
if (!sourfile.exists()) return; // Ôàéëà íåò
// Áýêàï ôàéëà åñëè âêëþ÷åí
if (backupEnabled)
{
QString backDir=replDate(backupDir); // Çàìåíÿåì äàòó
QDir dir(backDir);
if (!dir.exists()) dir.mkpath(backDir); // Ñîçäàåì êàòàëîã åñëè åãî íåò
sourfile.copy(backDir+"/"+fileName);
}
// Ïåðåìåùåíèå ôàéëà
if (sourfile.rename(dfullFileName))
{
log.writeMessage("Ïåðåìåùåí ôàéë "+sfullFileName+" â "+dfullFileName);
}
else
{
log.writeMessage("[Îøèáêà] ïåðåìåùåíèÿ ôàéëà "+sfullFileName+" â "+dfullFileName);
}
}
// Ïåðåìåùåíèå ôàéëà
/*
void Mover::move(int index,const QString &fileName,MoveBlock::MoveDirection movDirect)
{
QString sfullFileName,dfullFileName;
switch (movDirect)
{
case MoveBlock::ToRight:
sfullFileName=moveBlocks[index]->msourceDir+"/"+fileName;
dfullFileName=moveBlocks[index]->mdestDir+"/"+fileName;
break;
case MoveBlock::ToLeft:
sfullFileName=moveBlocks[index]->mdestDir+"/"+fileName;
dfullFileName=moveBlocks[index]->msourceDir+"/"+fileName;
}
QFile sourfile(sfullFileName);
if (!sourfile.exists()) return; // Ôàéëà íåò
// Áýêàï ôàéëà åñëè âêëþ÷åí
if (backupEnabled)
{
QString backDir=replDate(backupDir); // Çàìåíÿåì äàòó
QDir dir(backDir);
if (!dir.exists()) dir.mkpath(backDir); // Ñîçäàåì êàòàëîã åñëè åãî íåò
sourfile.copy(backDir+"/"+fileName);
}
// Ïåðåìåùåíèå ôàéëà
if (sourfile.rename(dfullFileName))
{
log.writeMessage("Ïåðåìåùåí ôàéë "+sfullFileName+" â "+dfullFileName);
}
else
{
log.writeMessage("[Îøèáêà] ïåðåìåùåíèÿ ôàéëà "+sfullFileName+" â "+dfullFileName);
}
}
*/
//! Çàìåíÿåò %yymmdd% íà òåêóùóþ äàòó
QString Mover::replDate(const QString &str)
{
QString block,strDate;
QString result=str;
QDateTime nowDate=QDateTime::currentDateTime();
block=getParamBlock(str);
while (block!="")
{
strDate=nowDate.toString(block);
result.replace("%"+block+"%",strDate);
block=getParamBlock(result);
}
return result;
}
//! Âñïîìîãàòåëüíàÿ ôóíêöèÿ äëÿ replDate, âîçâðàùàåò ïåðâóþ íàéäåííóþ ñòðóêòóðó %yymmdd% èëè "" åñëè íå íàéäåíà
QString Mover::getParamBlock(const QString &str)
{
int beg,end;
beg=str.indexOf("%");
if (beg==-1) return "";
end=str.indexOf("%",beg+1);
if (end==-1) return "";
return str.mid(beg+1,end-beg-1);
}