File: FtpServer.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 (276 lines) | stat: -rw-r--r-- 10,209 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
269
270
271
272
273
274
275
276
/********************************************************************
 * 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
 *-------------------------------------------------------------------
 * FILE   : FtpServer.cpp
 * OBJECT : FtpServer
 * INFO   : Dialog umozliwiajacy wprowadzanie/modyfikacje danych
 *          opisujacych polaczenie z konkretnym serwerem S/FTP.
 *******************************************************************/

/*------- include files:
-------------------------------------------------------------------*/
#include "FtpServer.h"
#include "Shared.h"
#include "Config.h"
#include <qlayout.h>
#include <qpushbutton.h>
#include <qgroupbox.h>
#include <qlabel.h>
#include <qlineedit.h>
#include <qcheckbox.h>
#include <qvalidator.h>

/*------- local constants:
-------------------------------------------------------------------*/
const int     FtpServer::OVER_STRETCH  = 100;
const int     FtpServer::SPACING_WIDTH = 2;
const int     FtpServer::MARGIN_WIDTH  = 2;
const QString FtpServer::ANONYM_NAME   = "anonymous";
const QString FtpServer::ANONYM_PASS   = "bsc@beesoft.org";
//...................................................................
const QString FtpServer::CAPTION       = QT_TR_NOOP( "FTP server data" );
const QString FtpServer::BTN_OK        = QT_TR_NOOP( "&Ok" );
const QString FtpServer::SRV_SETTINGS  = QT_TR_NOOP( "Server settings" );
const QString FtpServer::NAME_LABEL    = QT_TR_NOOP( "&Name:" );
const QString FtpServer::PORT_LABEL    = QT_TR_NOOP( "&Port" );
const QString FtpServer::ADDR_LABEL    = QT_TR_NOOP( "&Address:" );
const QString FtpServer::DIR_LABEL     = QT_TR_NOOP( "&Directory:" );
const QString FtpServer::USR_SETTINGS  = QT_TR_NOOP( "User settings" );
const QString FtpServer::USER_LABEL    = QT_TR_NOOP( "&User name:" );
const QString FtpServer::PASS_LABEL    = QT_TR_NOOP( "&Password:" );
const QString FtpServer::ANONYM_CB     = QT_TR_NOOP( "Anonymou&s user" );
const QString FtpServer::SSL_CB        = QT_TR_NOOP( "&Secure FTP (sftp)" );

//*******************************************************************
// FtpServer                                             CONSTRUCTOR
//*******************************************************************
FtpServer::FtpServer( QWidget* const in_parent, FtpServerInfo& in_fsi )
: QDialog      ( in_parent )
, d_main_layout( new QHBoxLayout( this ))
//.....................................
, d_data_layout( new QVBoxLayout )
, d_data_box   ( new QGroupBox( 2, Qt::Horizontal, tr(SRV_SETTINGS), this ))
, d_name_lbl   ( new QLabel   ( tr(NAME_LABEL), d_data_box ))
, d_name_le    ( new QLineEdit( d_data_box ) )
, d_addr_lbl   ( new QLabel   ( tr(ADDR_LABEL), d_data_box ))
, d_addr_le    ( new QLineEdit( d_data_box ) )
, d_port_lbl   ( new QLabel   ( tr(PORT_LABEL), d_data_box ))
, d_port_le    ( new QLineEdit( d_data_box ) )
, d_dir_lbl    ( new QLabel   ( tr(DIR_LABEL), d_data_box ))
, d_dir_le     ( new QLineEdit( d_data_box ) )
, d_ssl_cb     ( new QCheckBox( tr(SSL_CB), this ))
, d_anonym_cb  ( new QCheckBox( tr(ANONYM_CB), this ))
, d_user_box   ( new QGroupBox( 2, Qt::Horizontal, tr(USR_SETTINGS), this ))
, d_user_lbl   ( new QLabel   ( tr(USER_LABEL), d_user_box ))
, d_user_le    ( new QLineEdit( d_user_box ))
, d_pass_lbl   ( new QLabel   ( tr(PASS_LABEL), d_user_box ))
, d_pass_le    ( new QLineEdit( d_user_box ))
//.....................................
, d_btn_layout ( new QVBoxLayout )
, d_ok_btn     ( new QPushButton( tr(BTN_OK), this ))
, d_cancel_btn ( new QPushButton( tr(Shared::CancelBtnLabel), this ))
//.....................................
, d_validator  ( new QIntValidator( 1, 100000, this ))
, d_fsi        ( in_fsi )
{
	setCaption( tr(CAPTION) );
	setFont( Config::instance()->lfs_default_font()  );
	d_ssl_cb->hide();

	d_main_layout->setSpacing( SPACING_WIDTH );
	d_main_layout->setMargin( MARGIN_WIDTH );
	
	d_name_lbl->setBuddy( d_name_le );
	d_addr_lbl->setBuddy( d_addr_le );
	d_port_lbl->setBuddy( d_port_le );
	d_dir_lbl->setBuddy( d_dir_le );
	d_user_lbl->setBuddy( d_user_le );
	d_pass_lbl->setBuddy( d_pass_le );
	
	d_port_le->setValidator( d_validator );
	d_pass_le->setEchoMode( QLineEdit::Password );
			
	d_data_layout->addWidget( d_data_box );
	d_data_layout->addWidget( d_ssl_cb );
	d_data_layout->addWidget( d_anonym_cb );
	d_data_layout->addWidget( d_user_box );
	d_main_layout->addLayout( d_data_layout );

	d_btn_layout->addStretch( OVER_STRETCH );
	d_btn_layout->addWidget( d_ok_btn );
	d_btn_layout->addWidget( d_cancel_btn );
	d_main_layout->addLayout( d_btn_layout );
	
	d_main_layout->setStretchFactor( d_data_layout, OVER_STRETCH );

	connect( d_ok_btn    , SIGNAL( clicked()     ), this, SLOT( slot_ok()         ));
	connect( d_cancel_btn, SIGNAL( clicked()     ), this, SLOT( slot_cancel()     ));
	connect( d_anonym_cb , SIGNAL( toggled(bool) ), this, SLOT( slot_anonym(bool) ));
	connect( d_ssl_cb    , SIGNAL( toggled(bool) ), this, SLOT( slot_ssl(bool)    ));
}
// end of FtpServer

//*******************************************************************
// ~FtpServer                                             DESTRUCTOR
//*******************************************************************
FtpServer::~FtpServer()
{
	delete d_validator;
	d_validator = 0;
}
// end of ~FtpServer

//*******************************************************************
// polish                                          PRIVATE inherited
//*******************************************************************
void FtpServer::show()
{
	d_name_le->setText( d_fsi.name() );
	d_addr_le->setText( d_fsi.addr() );
	d_port_le->setText( QString::number( d_fsi.port() ));
	d_dir_le->setText( d_fsi.dir() );
	d_user_le->setText( d_fsi.user() );
	d_pass_le->setText( d_fsi.pass() );

	if( ANONYM_NAME == d_fsi.user() ) {
		d_anonym_cb->setChecked( TRUE );
	}
	d_ssl_cb->setChecked( d_fsi.ssl() );
	QDialog::show();
}
// end of polish

//*******************************************************************
// check                                                     PRIVATE
//-------------------------------------------------------------------
// Kontrola czy wprowadzono wszystkie dane.
//*******************************************************************
bool FtpServer::check()
{
	bool retval = TRUE;
	
	if( d_name_le->text().isEmpty() ) {
		retval = FALSE;
		d_name_le->setFocus();
	}
	else if( d_addr_le->text().isEmpty() ) {
		retval = FALSE;
		d_addr_le->setFocus();
	}
	else if( FALSE == d_ssl_cb->isChecked() ) {
		if( d_port_le->text().isEmpty() ) {
			retval = FALSE;
			d_port_le->setText( "21" );
			d_port_le->setFocus();
		}
		else if( d_dir_le->text().isEmpty() ) {
			retval = FALSE;
			d_dir_le->setText( "/" );
			d_dir_le->setFocus();
		}
	}
	else if( d_user_le->text().isEmpty() ) {
		retval = FALSE;
		d_user_le->setFocus();
	}
	else if( d_pass_le->text().isEmpty() ) {
		retval = FALSE;
		d_pass_le->setFocus();
	}

	return retval;
}
// end of check

//*******************************************************************
// slot_ok                                              PRIVATE slot
//-------------------------------------------------------------------
// Uzytkownik zaakceptowal aktualnie wprowadzone dane.
// Wracamy do listy serwerow.
//*******************************************************************
void FtpServer::slot_ok()
{
	if( check() ) {
		d_fsi.set_name( d_name_le->text() );
		d_fsi.set_ssl ( d_ssl_cb->isChecked() );
		d_fsi.set_addr( d_addr_le->text() );
		d_fsi.set_port( d_port_le->text().toInt() );
		d_fsi.set_dir ( d_dir_le->text()  );
		d_fsi.set_user( d_user_le->text() );
		d_fsi.set_pass( d_pass_le->text() );
		accept();
	}
}
// end of slot_ok

//*******************************************************************
// slot_cancel                                          PRIVATE slot
//-------------------------------------------------------------------
// Uzytkownik zrezygnowal ze wszystkich zmian danych.
//*******************************************************************
void FtpServer::slot_cancel()
{
	d_fsi.clear();
	reject();
}
// end of slot_cancel

//*******************************************************************
// slot_anonym                                          PRIVATE slot
//*******************************************************************
void FtpServer::slot_anonym( bool in_on )
{
	if( in_on ) {
		d_user_le->setEnabled( FALSE );
		d_pass_le->setEnabled( FALSE );
		d_user_le->setText( ANONYM_NAME );
		d_pass_le->setText( ANONYM_PASS );
	}
	else {
		d_user_le->setEnabled( TRUE );
		d_pass_le->setEnabled( TRUE );
		d_user_le->clear();
		d_pass_le->clear();		
	}
}
// end of slot_anonym

//*******************************************************************
// slot_ssl                                             PRIVATE slot
//-------------------------------------------------------------------
// Uzytkownik zdecydowal, ze chce skorzystac z SFTP.
//*******************************************************************
void FtpServer::slot_ssl( bool in_on )
{
	if( in_on ) {
		d_anonym_cb->setEnabled( FALSE );
		d_port_le->setEnabled( FALSE );
		d_dir_le->setEnabled( FALSE );
		d_user_le->setEnabled( TRUE );
		d_pass_le->setEnabled( TRUE );
		d_user_le->clear();
		d_pass_le->clear();
	}
	else {
		d_anonym_cb->setEnabled( TRUE );
		d_port_le->setEnabled( TRUE );
		d_dir_le->setEnabled( TRUE );
	}
}
// end of slot_anonym