creatorextension/com.nokia.s60tools.creator/src/com/nokia/s60tools/creator/job/RunInDeviceJob.java
author dpodwall
Tue, 12 Jan 2010 13:17:53 -0600
changeset 0 61163b28edca
permissions -rw-r--r--
initial EPL conversion
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
     1
/*
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
     2
* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). 
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
     3
* All rights reserved.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
     4
* This component and the accompanying materials are made available
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
     5
* under the terms of "Eclipse Public License v1.0"
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
     6
* which accompanies this distribution, and is available
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
     8
*
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
     9
* Initial Contributors:
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    10
* Nokia Corporation - initial contribution.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    11
*
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    12
* Contributors:
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    13
*
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    14
* Description: 
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    15
*
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    16
*/
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    17
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    18
package com.nokia.s60tools.creator.job;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    19
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    20
import java.io.File;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    21
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    22
import org.eclipse.core.runtime.IProgressMonitor;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    23
import org.eclipse.core.runtime.IStatus;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    24
import org.eclipse.core.runtime.Status;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    25
import org.eclipse.core.runtime.jobs.Job;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    26
import org.eclipse.jface.dialogs.IDialogConstants;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    27
import org.eclipse.swt.widgets.Display;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    28
import org.eclipse.swt.widgets.Shell;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    29
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    30
import com.nokia.s60tools.creator.CreatorActivator;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    31
import com.nokia.s60tools.creator.common.ProductInfoRegistry;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    32
import com.nokia.s60tools.creator.preferences.CreatorPreferences;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    33
import com.nokia.s60tools.creator.resources.Messages;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    34
import com.nokia.s60tools.creator.util.CreatorEditorConsole;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    35
import com.nokia.s60tools.hticonnection.services.AppStatus;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    36
import com.nokia.s60tools.hticonnection.services.HTIServiceFactory;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    37
import com.nokia.s60tools.hticonnection.services.IApplicationControlService;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    38
import com.nokia.s60tools.hticonnection.services.IConnectionTestService;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    39
import com.nokia.s60tools.hticonnection.services.IFTPListener;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    40
import com.nokia.s60tools.hticonnection.services.IFTPRequestManager;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    41
import com.nokia.s60tools.hticonnection.services.IFTPService;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    42
import com.nokia.s60tools.util.exceptions.JobCancelledByUserException;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    43
import com.nokia.s60tools.util.resource.FileUtils;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    44
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    45
/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    46
 * Job for uploading files
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    47
 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    48
public class RunInDeviceJob extends Job implements IManageableJob, IFTPListener {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    49
	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    50
	private static final String CREATOR_EXE_NAME = ProductInfoRegistry.getCreatorSymbianExcecutableName();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    51
	private String srcFilePath;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    52
	private String destFilePath;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    53
	private static int timeout = 30*1000; // Default timeout 30 s.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    54
	private IProgressMonitor monitor;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    55
	private String destFileName;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    56
	private int confirmDialogSelection = -1;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    57
	private IFTPRequestManager FTPReguestManager;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    58
	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    59
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    60
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    61
	 * Constructor
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    62
	 * @param name Name for job
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    63
	 * @param srcFilePath Source file path
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    64
	 * @param destFilePath Destination file path
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    65
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    66
	public RunInDeviceJob(String name, String srcFilePath, String destFilePath, String destFileName ) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    67
		super(name);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    68
		this.srcFilePath = srcFilePath;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    69
		this.destFilePath = destFilePath;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    70
		this.destFileName = destFileName;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    71
		setUser(true);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    72
		setTimeOut();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    73
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    74
	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    75
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    76
	 * Setting the timeout for operation.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    77
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    78
	private void setTimeOut() {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    79
		File srcFile = new File(srcFilePath);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    80
		Long bytes = new Long( srcFile.length());
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    81
		//Setting time out to be 10 ms for byte, e.g script with few element is 300 bytes long, then timeout will be 30s
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    82
		int countTimeOut = bytes.intValue() * 10;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    83
		//
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    84
		int maxTimeOut = 2*60*1000;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    85
		//If we count longer than default time out, and it does not reach maximum time out, we set new timeout
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    86
		if(countTimeOut > timeout && countTimeOut < maxTimeOut ){
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    87
			timeout = countTimeOut;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    88
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    89
		//if we count longer than max timeout, using max timeout
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    90
		else if(countTimeOut > maxTimeOut){
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    91
			timeout = maxTimeOut;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    92
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    93
		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    94
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    95
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    96
	/* (non-Javadoc)
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    97
	 * @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor)
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    98
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    99
	protected IStatus run(IProgressMonitor monitor) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   100
		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   101
		this.monitor = monitor;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   102
		try{
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   103
		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   104
			CreatorJobManager.getInstance().registerJob(this);			
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   105
			
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   106
			//First check if HTI is running
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   107
			IFTPService service = HTIServiceFactory
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   108
					.createFTPService(CreatorEditorConsole.getInstance());			
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   109
			IConnectionTestService connService = HTIServiceFactory
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   110
				.createConnectionTestService(CreatorEditorConsole.getInstance());
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   111
			checkCancel();			
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   112
			//If HTI is not running, returning error
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   113
			if(!connService.isReady()){
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   114
				String msg = Messages.getString("RunInDeviceJob.HTINotRunning_ErrMsg");
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   115
				CreatorEditorConsole.getInstance().println(
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   116
						msg, CreatorEditorConsole.MSG_ERROR); //$NON-NLS-1$
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   117
				
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   118
				return new Status(
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   119
						Status.ERROR,
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   120
						CreatorActivator.PLUGIN_ID,
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   121
						msg); //$NON-NLS-1$				
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   122
			}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   123
			
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   124
			checkCancel();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   125
			
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   126
			//Then upload file
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   127
			try {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   128
				monitor.beginTask(getName(), IProgressMonitor.UNKNOWN); //$NON-NLS-1$
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   129
				
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   130
				boolean replace = CreatorPreferences.getDontAskFileReplaceInDevice();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   131
				if(!replace){
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   132
					monitor.subTask(Messages.getString("RunInDeviceJob.CheckFileExistence_TaskName")); //$NON-NLS-1$
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   133
					replace = true;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   134
					//Before load, check if there is file allready
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   135
					checkCancel();					
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   136
					String deviceSaveFolded = CreatorPreferences.getDeviceSaveFolded();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   137
					String[] files = service.listFiles(deviceSaveFolded, 0);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   138
					if(files != null && files.length > 0){
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   139
						for (int i = 0; i < files.length; i++) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   140
							if(destFileName.equalsIgnoreCase(files[i])){
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   141
								//File allready exist
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   142
								replace = showReplaceFileDialog();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   143
								break;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   144
							}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   145
						}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   146
					}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   147
				}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   148
				
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   149
				
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   150
				//If replacing (or file does not exist) uploading file to device, 
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   151
				//if user does not upload file, existing file will be executed in device.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   152
				if(replace){
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   153
										
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   154
					checkCancel();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   155
					monitor.subTask(Messages.getString("RunInDeviceJob.Upload_File_SubTaskName")); //$NON-NLS-1$
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   156
					
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   157
					StringBuffer buf = FileUtils.loadDataFromFile(srcFilePath);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   158
					if (buf != null) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   159
						String fileString = buf.toString();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   160
		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   161
						checkCancel();	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   162
						service.uploadFile(fileString.getBytes(), destFilePath, this, timeout);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   163
						
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   164
		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   165
					}					
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   166
				}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   167
				
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   168
			}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   169
			catch(JobCancelledByUserException e){
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   170
				return Status.CANCEL_STATUS;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   171
			}			
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   172
			catch (Exception e) {			
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   173
				String msg = Messages.getString("RunInDeviceJob.Upload_Failed_ConsoleErrorMsg");
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   174
				return returnErrorStatus(e, msg);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   175
			} 
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   176
			
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   177
			checkCancel();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   178
			
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   179
			try {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   180
				
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   181
				IApplicationControlService appService = HTIServiceFactory.createApplicationControlService(CreatorEditorConsole.getInstance());
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   182
				String programName = CREATOR_EXE_NAME;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   183
				String parameters = destFilePath;							
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   184
				
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   185
				//Checking Creator application status in device
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   186
				checkCancel();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   187
				monitor.subTask(Messages.getString("RunInDeviceJob.CheckIfRunning_SubTaskName")); //$NON-NLS-1$							
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   188
				AppStatus status = appService.getApplicationStatusByName(programName, timeout);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   189
				
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   190
				//If creator is running, stopping it
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   191
				checkCancel();				
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   192
				if(status.getStatus() == AppStatus.RUNNING){
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   193
					
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   194
					//Showing dialog to as if user wants to shutdown creator
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   195
					//If user selects Cancel, JobCancelledByUserException is thrown
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   196
					showShutdownDialog();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   197
					monitor.subTask(Messages.getString("RunInDeviceJob.StopCreator_SubTaskName")); //$NON-NLS-1$					
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   198
					appService.stopApplicationByName(programName, timeout);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   199
				}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   200
				
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   201
				//Showing user that he/she should follow progress in device/emulator
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   202
				showInformation();																													
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   203
				
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   204
				//Start to run script
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   205
				checkCancel();				
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   206
				monitor.subTask(Messages.getString("RunInDeviceJob.Run_File_SubTaskName")); //$NON-NLS-1$							
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   207
				appService.startProcess(programName, parameters, timeout);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   208
			}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   209
			catch(JobCancelledByUserException e){
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   210
				return Status.CANCEL_STATUS;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   211
			}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   212
			catch (Exception e){
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   213
				String msg = Messages.getString("RunInDeviceJob.Run_Failed_ConsoleErrorMsg");
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   214
				return returnErrorStatus(e, msg);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   215
				
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   216
			}		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   217
		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   218
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   219
		catch(JobCancelledByUserException e){
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   220
			return Status.CANCEL_STATUS;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   221
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   222
		finally {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   223
			finished();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   224
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   225
		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   226
		return Status.OK_STATUS;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   227
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   228
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   229
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   230
	 * Showing user a dialog to shutdown Creator in device or not,
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   231
	 * @throws JobCancelledByUserException if user selects Cancel
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   232
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   233
	private void showShutdownDialog() throws JobCancelledByUserException {		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   234
		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   235
		if(!CreatorPreferences.getDontAskShutdownCreator()){
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   236
		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   237
			Runnable runDlg = new Runnable(){
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   238
	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   239
				public void run() {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   240
					//
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   241
					ShutdownCreatorInDeviceDialog dlg = new ShutdownCreatorInDeviceDialog(CreatorActivator.getCurrentlyActiveWbWindowShell());
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   242
		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   243
					dlg.open();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   244
					confirmDialogSelection = dlg.getSelection();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   245
				}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   246
			};
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   247
			
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   248
			Display.getDefault().syncExec(runDlg);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   249
			
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   250
			if(confirmDialogSelection == IDialogConstants.CANCEL_ID){
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   251
				throwJobCancelledException();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   252
			}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   253
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   254
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   255
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   256
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   257
	 * Shows user an information dialog to follow execution in device.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   258
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   259
	private void showInformation() {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   260
		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   261
		if(!CreatorPreferences.getDontAskShowInformation()){
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   262
		Runnable showInfo = new Runnable(){
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   263
					
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   264
			public void run() {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   265
				WatchDeviceInformationDialog dlg = new WatchDeviceInformationDialog(
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   266
						CreatorActivator.getCurrentlyActiveWbWindowShell(),
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   267
						destFileName);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   268
				dlg.open();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   269
				
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   270
			}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   271
		};
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   272
		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   273
		Display.getDefault().asyncExec(showInfo);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   274
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   275
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   276
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   277
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   278
	 * Check if job is canceled, and throws an exception if it's.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   279
	 * @throws JobCancelledByUserException
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   280
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   281
	private void checkCancel() throws JobCancelledByUserException {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   282
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   283
		if(monitor.isCanceled()){
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   284
			if(FTPReguestManager != null){
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   285
				FTPReguestManager.cancel();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   286
			}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   287
			throwJobCancelledException();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   288
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   289
		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   290
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   291
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   292
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   293
	 * Throw cancel exception
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   294
	 * @throws JobCancelledByUserException
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   295
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   296
	private void throwJobCancelledException() throws JobCancelledByUserException {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   297
		throw new JobCancelledByUserException(Messages.getString("RunInDeviceJob.JobCanceledByUser_Msg"));
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   298
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   299
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   300
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   301
	 * Check if user wants to replace existing file
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   302
	 * @return <code>true</code> if replace, <code>false</code> otherwise.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   303
	 * @throws JobCancelledByUserException 
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   304
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   305
	private boolean showReplaceFileDialog() throws JobCancelledByUserException {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   306
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   307
		Runnable runDlg = new Runnable(){
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   308
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   309
			public void run() {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   310
				//
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   311
				Shell shell = CreatorActivator.getCurrentlyActiveWbWindowShell();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   312
				ConfirmFileReplaceDialog dlg = new ConfirmFileReplaceDialog(
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   313
						shell, destFilePath);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   314
	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   315
				dlg.open();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   316
				confirmDialogSelection = dlg.getSelection();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   317
			}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   318
		};
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   319
		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   320
		Display.getDefault().syncExec(runDlg);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   321
		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   322
		if(confirmDialogSelection == IDialogConstants.CANCEL_ID){
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   323
			throwJobCancelledException();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   324
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   325
		boolean replace = confirmDialogSelection == IDialogConstants.YES_ID ? true : false;		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   326
		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   327
		return replace;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   328
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   329
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   330
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   331
	 * Returns Error status
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   332
	 * @param e
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   333
	 * @param msg
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   334
	 * @return {@link Status} with {@link Status#ERROR}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   335
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   336
	private IStatus returnErrorStatus(Exception e, String msg) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   337
		CreatorEditorConsole.getInstance().println(
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   338
				msg, CreatorEditorConsole.MSG_ERROR); //$NON-NLS-1$
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   339
		e.printStackTrace();			
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   340
		return new Status(
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   341
				Status.ERROR,
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   342
				CreatorActivator.PLUGIN_ID,
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   343
				msg, e); //$NON-NLS-1$
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   344
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   345
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   346
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   347
	 * Done and unregister job
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   348
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   349
	private void finished() {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   350
		monitor.done();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   351
		CreatorJobManager.getInstance().unregisterJob(this);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   352
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   353
	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   354
	/* (non-Javadoc)
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   355
	 * @see com.nokia.s60tools.remotecontrol.job.IManageableJob#forcedShutdown()
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   356
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   357
	public void forcedShutdown() {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   358
		cancel();
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   359
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   360
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   361
	/* (non-Javadoc)
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   362
	 * @see com.nokia.s60tools.hticonnection.services.IFTPListener#requestEnded(com.nokia.s60tools.hticonnection.services.IFTPRequestManager)
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   363
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   364
	public void requestEnded(IFTPRequestManager manager) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   365
		// not needed		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   366
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   367
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   368
	/* (non-Javadoc)
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   369
	 * @see com.nokia.s60tools.hticonnection.services.IFTPListener#requestInQueue(com.nokia.s60tools.hticonnection.services.IFTPRequestManager)
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   370
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   371
	public void requestInQueue(IFTPRequestManager manager) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   372
		// not needed
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   373
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   374
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   375
	/* (non-Javadoc)
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   376
	 * @see com.nokia.s60tools.hticonnection.services.IFTPListener#requestStarted(com.nokia.s60tools.hticonnection.services.IFTPRequestManager)
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   377
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   378
	public void requestStarted(IFTPRequestManager manager) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   379
		FTPReguestManager = manager;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   380
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   381
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   382
}