/*
- * Copyright (C) 2009 The Android Open Source Project
+ * Common
+ *
+ * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact:
+ * Kangho Kim <kh5325.kim@samsung.com>
+ * Shingil kang <shingil.kang@samsung.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
+ *
+ * Contributors:
+ * - S-Core Co., Ltd
+ *
*/
-
package org.tizen.common.connection.ui;
import org.eclipse.core.runtime.IProgressMonitor;
* Implementation of the {@link ISyncProgressMonitor} wrapping an Eclipse
* {@link IProgressMonitor}.
*/
-public class
-SyncProgressMonitor
-implements ISyncProgressMonitor
+public class SyncProgressMonitor implements ISyncProgressMonitor
{
protected static String[] UNITS = new String[] { "B", "KB", "MB", "GB" };
private String labelFrom = "From: ";
private String labelTo = "To: ";
private String labelSize = "Size: ";
-
+
protected long totalWork;
-
+
protected long worked;
-
+
protected int estimated;
-
+
protected int rate;
- protected static
- String
- getSizeAndUnit(
- final long size
- )
+ protected static String getSizeAndUnit(final long size)
{
int index = 0;
double value = size;
-
- while ( index < UNITS.length - 1 && value >= 1000 )
+
+ while (index < UNITS.length - 1 && value >= 1000)
{
- value /= 1000.00;
- ++index;
+ value /= 1000.00;
+ ++index;
}
-
- return String.format( FORMATS[index], value, UNITS[index] );
+
+ return String.format(FORMATS[index], value, UNITS[index]);
}
- public
- SyncProgressMonitor(
- final TransferProgressMonitorDialog tmd,
- final IProgressMonitor monitor,
- final String name
- )
+ public SyncProgressMonitor(final TransferProgressMonitorDialog tmd, final IProgressMonitor monitor, final String name)
{
this.dialog = tmd;
this.monitor = monitor;
public void start(long totalWork)
{
- this.totalWork = totalWork;
- this.rate = 1;
- this.estimated = 0;
- while( totalWork > Integer.MAX_VALUE )
+ this.totalWork = totalWork;
+ this.rate = 1;
+ this.estimated = 0;
+
+ while (totalWork > Integer.MAX_VALUE)
{
totalWork /= 10;
this.rate *= 10;
}
- monitor.beginTask(name, (int) (totalWork / this.rate) );
- apply();
+ monitor.beginTask(name, (int) (totalWork));
+ apply();
}
public void stop()
monitor.done();
}
- public void advance( long work )
+ public void advance(long work)
{
- worked += work;
- int difference = (int) (estimated - ( worked / this.rate ));
- estimated += difference;
- monitor.worked( difference );
- apply();
+ worked += work;
+ monitor.worked((int) work / rate);
+ apply();
}
public boolean isCanceled()
return monitor.isCanceled();
}
- public void startSubTask( String name )
+ public void startSubTask(String name)
{
- this.name = name;
- apply();
+ this.name = name;
+ apply();
}
-
+
protected void apply()
{
final Label fromLabel = dialog.getFromLabel();
final String sizeStr;
String[] subTasks = name.split("\t");
- if (name == null || subTasks.length != 3)
+ if (name == null || subTasks.length != 2)
{
fromStr = subTasks[0];
toStr = "";
{
fromStr = labelFrom + subTasks[0];
toStr = labelTo + subTasks[1];
- sizeStr = labelSize + String.format( "( %s / %s )", getSizeAndUnit( getWorked() ), getSizeAndUnit( getTotal() ) );
+ sizeStr = labelSize + String.format("( %s / %s )", getSizeAndUnit(getWorked()), getSizeAndUnit(getTotal()));
}
- SWTUtil.asyncExec(new Runnable()
+ SWTUtil.syncExec(new Runnable()
{
@Override
public void run()
});
}
- @Override
+ @Override
public long getTotal()
{
- return totalWork;
+ return totalWork;
}
- @Override
+ @Override
public long getWorked()
{
- return worked;
+ return worked;
}
}
package org.tizen.sdblib.service;
import static org.tizen.sdblib.service.SyncServiceConstants.MESSAGE_SIZECHECKING;
+import static org.tizen.sdblib.service.SyncServiceConstants.MESSAGE_PULL_FILES;
+import static org.tizen.sdblib.service.SyncServiceConstants.MESSAGE_PUSH_FILES;
import static org.tizen.sdblib.service.SyncServiceConstants.RESULT_CANCELED;
import static org.tizen.sdblib.service.SyncServiceConstants.RESULT_CONNECTION_ERROR;
import static org.tizen.sdblib.service.SyncServiceConstants.RESULT_FILE_READ_ERROR;
monitor.start( -1 );
monitor.startSubTask( MESSAGE_SIZECHECKING );
+ SyncResult result;
+
// compute the number of file to move
try {
long total = getTotalSize(entries, monitor);
+ monitor.startSubTask( MESSAGE_PULL_FILES );
monitor.stop();
// start the monitor
monitor.start(total);
+ result= doPull(entries, localPath, monitor, timeOut);
+ }
+ catch ( final InterruptedException e )
+ {
+ return new SyncResult( RESULT_CANCELED );
+ }
+ finally
+ {
+ monitor.stop();
+ }
+
+ return result;
+ }
+ public SyncResult
+ doPull(
+ final FileEntry[] entries,
+ final String localPath,
+ ISyncProgressMonitor monitor,
+ final int timeOut
+ )
+ {
+ monitor = nvl( monitor, NullSyncProgressMonitor.getInstance() );
+
+ try {
for ( final FileEntry entry : entries )
{
- final String path = addTailingPath( localPath, entry.getName() );
- if ( entry.isDirectory() )
- {
- new File( path ).mkdir();
+ final String path = addTailingPath( localPath, entry.getName() );
+ if ( entry.isDirectory() )
+ {
+ new File( path ).mkdir();
FileEntry[] children = entry.getChildren();
- pull(children, path, monitor, timeOut);
- }
- else
- {
+ doPull(children, path, monitor, timeOut);
+ }
+ else
+ {
monitor.startSubTask( String.format("%s\t%s", entry.getFullPath(), path ) );
- FileOutputStream fileOut = null;
- try
- {
- fileOut = new FileOutputStream( path );
- SyncResult result = doPull( entry, fileOut, monitor, timeOut );
- if ( RESULT_OK != result.getCode() )
- {
- return result;
- }
- }
- finally
- {
- tryClose( fileOut );
- }
- }
+ FileOutputStream fileOut = null;
+ try
+ {
+ fileOut = new FileOutputStream( path );
+ SyncResult result = doPull( entry, fileOut, monitor, timeOut );
+ if ( RESULT_OK != result.getCode() )
+ {
+ return result;
+ }
+ }
+ finally
+ {
+ tryClose( fileOut );
+ }
+ }
}
return new SyncResult( RESULT_OK );
}
- catch ( final InterruptedException e )
- {
- return new SyncResult( RESULT_CANCELED );
- }
- catch ( final IOException e )
- {
- return new SyncResult( RESULT_CONNECTION_ERROR, e );
- }
- finally
+ catch ( final IOException e )
{
- monitor.stop();
+ return new SyncResult( RESULT_CONNECTION_ERROR, e );
}
}
// write the content in the file
out.write( data, 0, nBytes );
- monitor.advance( nBytes );
+ monitor.advance( nBytes );
}
return new SyncResult( RESULT_OK );
try {
// get the total count of the bytes to transfer
long total = getTotalLocalFileSizeLong( locals, monitor );
+ monitor.startSubTask( MESSAGE_PUSH_FILES );
monitor.stop();
-
+
monitor.start( total );
-
+
final String remotePath = remote.getFullPath();