File: DirSynch.cpp

package info (click to toggle)
bsc 2.27-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,640 kB
  • ctags: 2,610
  • sloc: cpp: 14,100; perl: 114; makefile: 46
file content (268 lines) | stat: -rw-r--r-- 9,220 bytes parent folder | download | duplicates (2)
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
/********************************************************************
 * Copyright (C) 2005, 2006 Piotr Pszczolkowski
 *-------------------------------------------------------------------
 * This file is part of BSCommander (Beesoft Commander).
 *
 * BSCommander 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 2 of the License, or
 * (at your option) any later version.
 *
 * BSCommander 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 BsC; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *******************************************************************/
 
/*------- include files:
-------------------------------------------------------------------*/
#include "DirSynch.h"
#include "CmpFiles.h"
#include "Shared.h"
#include <qmessagebox.h>

/*------- local constants:
-------------------------------------------------------------------*/
const QString DirSynch::Caption          = QT_TR_NOOP( "Directories synchronisation" );
const QString DirSynch::InfoCaption      = QT_TR_NOOP( "Synchronised directories" );
const QString DirSynch::NotReadableFile  = QT_TR_NOOP( "You can't read from this file: %1.\nCheck permission access." );
const QString DirSynch::OpenForReadError = QT_TR_NOOP( "Can't open this file for reading: %1." );
const QString DirSynch::OpenForWriteError= QT_TR_NOOP( "Can't open this file for writing; %1." );


//*******************************************************************
// DirSynch                                              CONSTRUCTOR
//*******************************************************************
DirSynch::DirSynch( QWidget* const in_parent, const QString& in_src_dir, const QString& in_dst_dir )
: DirsDialog      ( in_parent, in_src_dir, in_dst_dir, Synchronize )
{
	setCaption( tr(Caption) );
	set_info_title( tr(InfoCaption) );

	connect( this, SIGNAL( file_do_it( const QString&, bool, bool, const QString&, const QString& ) ),
				this, SLOT  ( file_do_it( const QString&, bool, bool, const QString&, const QString& ) ));
	connect( this, SIGNAL( dir_do_it ( const QString&, bool, bool, const QString&, const QString& ) ),
				this, SLOT  ( dir_do_it ( const QString&, bool, bool, const QString&, const QString& ) ));
}
// end of DirSynch

//*******************************************************************
// dir_do_it                                            PRIVATE slot
//-------------------------------------------------------------------
// Funkcja jest wywolywana tylko i wylacznie jesli podkatalog o
// podanej nazwie nie istnieje we wskazanych katalogach.
// Nalezy nie istniejacy podkatalog utworzyc i zsynchronizowac.
//*******************************************************************
void DirSynch::dir_do_it(	const QString& in_name,
									const bool     in_lft_flag,
									const bool     in_rgt_flag,
									const QString& in_lft_dir,
									const QString& in_rgt_dir )
{
	Shared::idle();
	if( d_break ) return;
	
	const QString lft_path = in_lft_dir + "/" + in_name;
	const QString rgt_path = in_rgt_dir + "/" + in_name;
	
	QString lft_disp_name = "";
	QString rgt_disp_name = "";
	QString mark     = "";
	
	// ####### KOPIOWANIE Z LEWEJ NA PRAWO #######
	if( in_lft_flag && !in_rgt_flag ) {
		lft_disp_name = Shared::get_subpath( d_src_dir, lft_path ).remove( 0, 1 );
		mark = DirMark + " " + L2R;
		//.............................................
		if( rgt_is_blocked() ) {
			rgt_disp_name = tr(BlockMark);
		}
		else {
			if( create_dir( lft_path, in_rgt_dir  ) ) {
				rgt_disp_name = tr(CopyMark);
				scanning( lft_path, rgt_path );
			}
			else {
				rgt_disp_name = tr(ErrorMark);
			}
		}
		//.............................................
		add_item( lft_disp_name, mark, rgt_disp_name, lft_path, rgt_path );
	}
	// ####### KOPIOWANIE Z PRAWEJ NA LEWO #######
	else if( !in_lft_flag && in_rgt_flag ) {
		rgt_disp_name = Shared::get_subpath( d_dst_dir, rgt_path ).remove( 0, 1 );
		mark = R2L + "DIR";
		//.............................................
		if( lft_is_blocked() ) {
			lft_disp_name = tr(BlockMark);
		}
		else {
			if( create_dir( rgt_path, in_lft_dir ) ) {
				lft_disp_name =  tr(CopyMark);
				scanning( lft_path, rgt_path );
			}
			else {
				lft_disp_name = tr(ErrorMark);
			}
		}
		//.............................................
		add_item( lft_disp_name, mark, rgt_disp_name, lft_path, rgt_path );
	}	
}
// end of dir_do_it

//*******************************************************************
// create_dir                                                PRIVATE
//-------------------------------------------------------------------
// Tworzy brakujacy podkatalog.
//*******************************************************************
bool DirSynch::create_dir( const QString& in_src_dir, const QString& in_dst_dir )
{
	Shared::idle();
	if( d_break ) return FALSE;
	
	bool retval = FALSE;
	
	const QFileInfo fi( in_src_dir );
	if( Shared::is_regular_file( fi.fileName() ) ) {
		if( fi.isDir() ) {
			const QDir dir( in_src_dir );
			if( dir.isReadable() ) {
				QDir dst( in_dst_dir );
				dst.mkdir( fi.fileName(), FALSE );
				retval = dst.cd( fi.fileName(), FALSE );
			}
		}
	}

	return retval;
}
// end of create_dir

//*******************************************************************
// file_do_it                                           PRIVATE slot
//*******************************************************************
void DirSynch::file_do_it( const QString& in_name,
									const bool     in_lft_flag,
									const bool     in_rgt_flag,
									const QString& in_lft_dir,
									const QString& in_rgt_dir )
{
	Shared::idle();
	if( d_break ) return;
	
	const QString lft_path = in_lft_dir + "/" + in_name;
	const QString rgt_path = in_rgt_dir + "/" + in_name;
	
	QString mark     = "";
	QString lft_name = "";
	QString rgt_name = "";
	QString lft_disp_name = "";
	QString rgt_disp_name = "";

	// ####### USTALAMY KIERUNEK KOPIOWANIA #######

	// ------- oba pliki istnieja - sprawdz czy sa takie same -------
	if( in_lft_flag && in_rgt_flag ) {
		CmpFiles cmp( this, lft_path, rgt_path );
		if( CmpFiles::NotEqual == cmp.compare() ) {
			// Pliki sie roznia zawartoscia - sa rozne
			const QFileInfo lfi( lft_path );
			const QFileInfo rfi( rgt_path );
			if     ( lfi.lastModified() == rfi.lastModified() ) mark = QuestMark;
			else if( lfi.lastModified() >  rfi.lastModified() ) mark = L2R;
			else                                                mark = R2L;
		}
		else {
			// Pliki maja taka sama zawartosc - sa takie same
			mark = Equal;
		}
	}
	// ------- kopiuj z lewej na prawo ------
	else if( in_lft_flag ) {
		mark = L2R;
	}
	// ------- kopiuj z prawej na lewo -------
	else if( in_rgt_flag ) {
		mark = R2L;
	}

	// ####### KOPIOWANIE (JESLI NIE MA BLOKADY) #######
	
	if( L2R == mark ) {
		lft_disp_name = Shared::get_subpath( d_src_dir, lft_path ).remove( 0, 1 );
		if( rgt_is_blocked() ) {
			rgt_disp_name = tr(BlockMark);
		}
		else {
			rgt_disp_name = copy_file( lft_path, rgt_path ) ? tr(CopyMark) : tr(ErrorMark);
		}
	}
	else if( R2L == mark ) {
		rgt_disp_name = Shared::get_subpath( d_dst_dir, rgt_path ).remove( 0, 1 );
		if( lft_is_blocked() ) {
			lft_disp_name = tr(BlockMark);
		}
		else {
			lft_disp_name = copy_file( rgt_path, lft_path ) ? tr(CopyMark) : tr(ErrorMark);
		}
	}
	else {
		lft_disp_name = Shared::get_subpath( d_src_dir, lft_path ).remove( 0, 1 );
		rgt_disp_name = Shared::get_subpath( d_dst_dir, rgt_path ).remove( 0, 1 );
	}

	add_item( lft_disp_name, mark, rgt_disp_name, lft_path, rgt_path );
}
// end of file_do_it

//*******************************************************************
// copy_file                                                 PRIVATE
//*******************************************************************
bool DirSynch::copy_file( const QString& in_src_path, const QString& in_dst_path )
{
	Shared::idle();
	if( d_break ) return FALSE;
	
	bool result = FALSE;
	
	const QFileInfo fi( in_src_path );
	QFile in ( in_src_path );
	QFile out( in_dst_path );
	
	if( FALSE == fi.isReadable() ) {
		QMessageBox::warning( this, Shared::ProgramName, tr(NotReadableFile).arg( in_src_path ));
	}
	else {
		if( in.open( IO_ReadOnly ) ) {
			if( out.open( IO_WriteOnly | IO_Truncate ) ) {
				result = TRUE;
				while( ( FALSE == d_break ) && ( FALSE == in.atEnd() )) {
					const Q_LONG n = in.readBlock( d_buffer, BufferSize );
					if( -1 == n ) {
						result = FALSE;
						break;
					}
					out.writeBlock( d_buffer, n );
					Shared::idle();
				}
				out.close();
			}
			else {
				QMessageBox::warning( this, Shared::ProgramName, tr(OpenForWriteError).arg( in_dst_path ) );
			}
			in.close();
		}
		else {
			QMessageBox::warning( this, Shared::ProgramName, tr(OpenForReadError).arg( in_src_path ) );
		}
	}

	return result;
}
// end of copy_file