summaryrefslogtreecommitdiffstats
path: root/smb4k/dialogs/smb4ksynchronizationdialog.cpp
blob: 58cf75a0035a00728398bb7d10d05345f48eed92 (plain)
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
/***************************************************************************
    smb4ksynchronizationdialog  -  The synchronization dialog of Smb4K
                             -------------------
    begin                : Sa Mai 19 2007
    copyright            : (C) 2007 by Alexander Reinholdt
    email                : dustpuppy@users.berlios.de
 ***************************************************************************/

/***************************************************************************
 *   This program 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.                                   *
 *                                                                         *
 *   This program 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 this program; if not, write to the                         *
 *   Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,   *
 *   MA  02110-1301 USA                                                    *
 ***************************************************************************/

// TQt includes
#include <tqlayout.h>
#include <tqframe.h>
#include <tqlabel.h>

// KDE includes
#include <tdelocale.h>
#include <kguiitem.h>
#include <kdebug.h>
#include <kurlrequester.h>
#include <klineedit.h>
#include <kprogress.h>

// application specific includes
#include "smb4ksynchronizationdialog.h"
#include "../core/smb4kshare.h"
#include "../core/smb4kcore.h"
#include "../core/smb4ksynchronizationinfo.h"
#include "../core/smb4ksettings.h"

Smb4KSynchronizationDialog::Smb4KSynchronizationDialog( Smb4KShare *share, TQWidget *parent, const char *name )
: KDialogBase( Plain, i18n( "Synchronization" ), User2|User1|Cancel, User1, parent, name, false, true ),
m_share( share )
{
  setWFlags( TQt::WDestructiveClose );

  setButtonGuiItem( User1, KGuiItem( i18n( "Synchronize" ), "go-bottom", i18n( "Synchronize the destination with the source" ) ) );
  setButtonGuiItem( User2, KGuiItem( i18n( "Swap Paths" ), TQString(), i18n( "Swap source and destination" ) ) );

  TQFrame *frame = plainPage();
  TQGridLayout *layout = new TQGridLayout( frame );
  layout->setSpacing( 5 );
  layout->setMargin( 0 );

  TQLabel *source_label =       new TQLabel( i18n( "Source:" ), frame, "SourceURLLabel" );
  KURLRequester *source =      new KURLRequester( m_share->path()+"/", frame, "SourceURL" );
  source->setShowLocalProtocol( false );
  source->setMode( KFile::Directory | KFile::LocalOnly );

  TQLabel *destination_label =  new TQLabel( i18n( "Destination:" ), frame, "DestinationURLLabel" );
  KURLRequester *destination = new KURLRequester( Smb4KSettings::rsyncPrefix(), frame, "DestinationURL" );
  destination->setShowLocalProtocol( false );
  destination->setMode( KFile::Directory | KFile::LocalOnly );

  KLineEdit *current_file =      new KLineEdit( TQString(), frame, "ProgressInfo" );
  current_file->setEnableSqueezedText( true );
  current_file->setReadOnly( true );

  KProgress *individual =         new KProgress( frame, "IndividualProgress", 0 );
  individual->setEnabled( false );

  KProgress *total =           new KProgress( frame, "TotalProgress", 0 );
  total->setEnabled( false );

  TQWidget *transfer_widget =   new TQWidget( frame, "TransferInfoWidget" );
  TQGridLayout *trans_layout =  new TQGridLayout( transfer_widget );
  trans_layout->setSpacing( 5 );
  trans_layout->setMargin( 0 );

  TQLabel *file_label =         new TQLabel( i18n( "Files transferred:" ), transfer_widget,
                               "FilesTransferredLabel" );
  TQLabel *file_trans_label =   new TQLabel( "0 / 0", transfer_widget,
                               "FilesTransferred" );

  TQLabel *rate_label =         new TQLabel( i18n( "Transfer rate:" ), transfer_widget,
                               "TransferRateLabel" );
  TQLabel *trans_rate_label =   new TQLabel( "0.00 kB/s", transfer_widget,
                               "TransferRate" );

  trans_layout->addWidget( file_label, 0, 0, 0 );
  trans_layout->addWidget( file_trans_label, 0, 1, TQt::AlignRight );
  trans_layout->addWidget( rate_label, 1, 0, 0 );
  trans_layout->addWidget( trans_rate_label, 1, 1, TQt::AlignRight );

  transfer_widget->setEnabled( false );

  layout->addWidget( source_label, 0, 0, 0 );
  layout->addWidget( source, 0, 1, 0 );
  layout->addWidget( destination_label, 1, 0, 0 );
  layout->addWidget( destination, 1, 1, 0 );
  layout->addMultiCellWidget( current_file, 2, 2, 0, 1, 0 );
  layout->addMultiCellWidget( individual, 3, 3, 0, 1, 0 );
  layout->addMultiCellWidget( total, 4, 4, 0, 1, 0 );
  layout->addMultiCellWidget( transfer_widget, 5, 6, 0, 1, 0 );

  // Connections
  connect( Smb4KCore::synchronizer(), TQT_SIGNAL( progress( const Smb4KSynchronizationInfo & ) ),
           this,                      TQT_SLOT( slotProgress( const Smb4KSynchronizationInfo & ) ) );

  connect( Smb4KCore::synchronizer(), TQT_SIGNAL( finished() ),
           this,                      TQT_SLOT( slotSynchronizationFinished() ) );


  setFixedSize( (sizeHint().width() > 350 ? sizeHint().width() : 350), sizeHint().height() );
}


Smb4KSynchronizationDialog::~Smb4KSynchronizationDialog()
{
  // Do *not* delete the share object here.
}


/////////////////////////////////////////////////////////////////////////////
//   TQT_SLOT IMPLEMENTATIONS
/////////////////////////////////////////////////////////////////////////////


void Smb4KSynchronizationDialog::slotUser1()
{
  // Synchronize!

  // Disable the URL requesters but in a way, that the information
  // proviced in them is still readable:
  KURLRequester *source = static_cast<KURLRequester *>( child( "SourceURL", "KURLRequester", true ) );
  source->lineEdit()->setReadOnly( true );
  source->button()->setEnabled( false );

  KURLRequester *destination = static_cast<KURLRequester *>( child( "DestinationURL", "KURLRequester", true ) );
  destination->lineEdit()->setReadOnly( true );
  destination->button()->setEnabled( false );

  TQWidget *transfer_widget = static_cast<TQWidget *>( child( "TransferInfoWidget", "TQWidget", true ) );
  transfer_widget->setEnabled( true );

  enableButton( User1, false );
  enableButton( User2, false );

  // Enable the progress bars and the information widgets:
  static_cast<KProgress *>( child( "IndividualProgress", "KProgress", true ) )->setEnabled( true );
  static_cast<KProgress *>( child( "TotalProgress", "KProgress", true ) )->setEnabled( true );

  Smb4KCore::synchronizer()->synchronize( source->url(), destination->url() );
}


void Smb4KSynchronizationDialog::slotUser2()
{
  // Swap URLs.

  KURLRequester *source = static_cast<KURLRequester *>( child( "SourceURL", "KURLRequester", true ) );
  KURLRequester *destination = static_cast<KURLRequester *>( child( "DestinationURL", "KURLRequester", true ) );

  TQString sourceURL = source->url();
  TQString destinationURL = destination->url();

  source->setURL( destinationURL );
  destination->setURL( sourceURL );
}


void Smb4KSynchronizationDialog::slotCancel()
{
  Smb4KCore::synchronizer()->abort();

  KDialogBase::slotCancel();
}


void Smb4KSynchronizationDialog::slotProgress( const Smb4KSynchronizationInfo &info )
{
  KLineEdit *progress =   static_cast<KLineEdit *>( child( "ProgressInfo", "KLineEdit", true ) );
  KProgress *individual = static_cast<KProgress *>( child( "IndividualProgress", "KProgress", true ) );
  KProgress *total =      static_cast<KProgress *>( child( "TotalProgress", "KProgress", true ) );
  TQLabel *transferred =   static_cast<TQLabel *>( child( "FilesTransferred", "TQLabel", true ) );
  TQLabel *rate =          static_cast<TQLabel *>( child( "TransferRate", "TQLabel", true ) );

  if ( !info.text().isEmpty() )
  {
    progress->setSqueezedText( info.text() );
  }

  if ( info.individualProgress() != -1 )
  {
    individual->setProgress( info.individualProgress() );
  }

  if ( info.totalProgress() != -1 )
  {
    total->setProgress( info.totalProgress() );
  }

  if ( info.totalFileNumber() != -1 && info.processedFileNumber() != -1 )
  {
    transferred->setText( TQString( "%1 / %2" ).arg( info.processedFileNumber() ).arg( info.totalFileNumber() ) );
  }

  if ( !info.transferRate().isEmpty() )
  {
    rate->setText( info.transferRate() );
  }
}


void Smb4KSynchronizationDialog::slotSynchronizationFinished()
{
  KProgress *individual = static_cast<KProgress *>( child( "IndividualProgress", "KProgress", true ) );
  KProgress *total =      static_cast<KProgress *>( child( "TotalProgress", "KProgress", true ) );

  if ( individual && individual->progress() != 100 )
  {
    individual->setProgress( 100 );
  }

  if ( total && total->progress() != 100 )
  {
    total->setProgress( 100 );
  }

  // Change the "Cancel" button to a "Close" button.
  setButtonGuiItem( Cancel, KStdGuiItem::close() );
}

#include "smb4ksynchronizationdialog.moc"