crashanalysis/crashanalyser/com.nokia.s60tools.crashanalyser/src/com/nokia/s60tools/crashanalyser/model/FileExportManager.java
changeset 0 5ad7ad99af01
equal deleted inserted replaced
-1:000000000000 0:5ad7ad99af01
       
     1 /*
       
     2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 
       
    18 package com.nokia.s60tools.crashanalyser.model;
       
    19 
       
    20 import java.io.File;
       
    21 import java.util.ArrayList;
       
    22 import java.util.Iterator;
       
    23 import java.util.List;
       
    24 import org.eclipse.swt.widgets.Shell;
       
    25 import org.eclipse.jface.dialogs.MessageDialog;
       
    26 import org.eclipse.jface.viewers.ISelection;
       
    27 import org.eclipse.jface.viewers.IStructuredSelection;
       
    28 import com.nokia.s60tools.crashanalyser.files.*;
       
    29 
       
    30 /**
       
    31  * This class handles all file export tasks.  
       
    32  *
       
    33  */
       
    34 public final class FileExportManager {
       
    35 	public final static String EXTENSION_ZIP = ".zip";
       
    36 	
       
    37 	private FileExportManager() {
       
    38 		// not to be implemented
       
    39 	}
       
    40 	
       
    41 	private static void showMessage(String message, Shell shell) {
       
    42 		MessageDialog.openInformation(
       
    43 				shell,
       
    44 				"Crash Analyser",
       
    45 				message);
       
    46 	}
       
    47 
       
    48 	/**
       
    49 	 * Exports all selected files to Zip as Html format
       
    50 	 * @param selection  IStructuredSelection of MainView's selected rows
       
    51 	 * @param shell shell for window
       
    52 	 */
       
    53 	public static void ExportSelectedFilesAsHtmlToZip(ISelection selection, Shell shell) {
       
    54 		// return if nothing is selected
       
    55 		if (selection == null || selection.isEmpty()) {
       
    56 			showMessage("Please select first the files you want to export", shell);
       
    57 			return;
       
    58 		}
       
    59 
       
    60 		List<CrashFileBundle> files = new ArrayList<CrashFileBundle>();
       
    61 		@SuppressWarnings("unchecked")
       
    62 		Iterator i = ((IStructuredSelection)selection).iterator();
       
    63 		// go through all selected files
       
    64 		while (i.hasNext()) {
       
    65 			CrashFileBundle cFileBundle = (CrashFileBundle)i.next();
       
    66 			// handle only files which contains xml file. only files which contain
       
    67 			// xml can be saved as html
       
    68 			if (cFileBundle.hasXml()) {
       
    69 				files.add(cFileBundle);						
       
    70 			}
       
    71 		}
       
    72 		// no files contained xml, return
       
    73 		if (files.isEmpty()) {
       
    74 			showMessage("Only decoded files can be exported as HTML", shell);
       
    75 			return;
       
    76 		}
       
    77 		
       
    78 		String saveFilePath = FileOperations.saveAsDialog("Crash Analyser - Select zip location", new String[] {"*.zip"}, shell);
       
    79 		if (saveFilePath != null) {
       
    80 			List<String> htmlFiles = new ArrayList<String>();
       
    81 			// get all html files
       
    82 			for (int j = 0; j < files.size(); j++) {
       
    83 				CrashFileBundle cfb = files.get(j);
       
    84 				File htmlFile = cfb.getHtmlFile(true);
       
    85 				if (htmlFile != null)
       
    86 					htmlFiles.add(htmlFile.getAbsolutePath());
       
    87 			}
       
    88 			 
       
    89 			FileOperations.zipFiles(htmlFiles.toArray(new String[htmlFiles.size()]), saveFilePath);
       
    90 		}		
       
    91 	}
       
    92 	
       
    93 	/**
       
    94 	 * Exports all selected files to Zip as Xml format (.crashxml or .xml)
       
    95 	 * @param selection  IStructuredSelection of MainView's selected rows
       
    96 	 * @param shell shell for window
       
    97 	 */
       
    98 	public static void ExportSelectedFilesAsXmlToZip(ISelection selection, Shell shell) {
       
    99 		// return if nothing is selected
       
   100 		if (selection == null || selection.isEmpty()) {
       
   101 			showMessage("Please select first the files you want to export", shell);
       
   102 			return;
       
   103 		}
       
   104 
       
   105 		ArrayList<String> files = new ArrayList<String>();
       
   106 		@SuppressWarnings("unchecked")
       
   107 		Iterator i = ((IStructuredSelection)selection).iterator();
       
   108 		// go through all selected files
       
   109 		while (i.hasNext()) {
       
   110 			CrashFileBundle cFileBundle = (CrashFileBundle)i.next();
       
   111 			// handle only files which contains xml file
       
   112 			if (cFileBundle.hasXml()) {
       
   113 				files.add(cFileBundle.getXmlFilePath());						
       
   114 			}
       
   115 		}
       
   116 		// no files contained xml, return
       
   117 		if (files.isEmpty()) {
       
   118 			showMessage("Only decoded files can be exported as XML", shell);
       
   119 			return;
       
   120 		}
       
   121 		
       
   122 		String saveFilePath = FileOperations.saveAsDialog("Crash Analyser - Select zip location", new String[] {"*.zip"}, shell);
       
   123 		if (saveFilePath != null) {
       
   124 			FileOperations.zipFiles(files.toArray(new String[files.size()]), saveFilePath);
       
   125 		}
       
   126 	}
       
   127 	
       
   128 	/**
       
   129 	 * Exports a selected file to Html format. If more than one file is selected, does nothing
       
   130 	 * @param selection  IStructuredSelection of MainView's selected rows
       
   131 	 * @param shell shell for window
       
   132 	 */
       
   133 	public static void ExportSelectedFileToHtml(ISelection selection, Shell shell) {
       
   134 		// return if nothing is selected or if more than one items are selected
       
   135 		if (selection == null || 
       
   136 			selection.isEmpty()) {
       
   137 			showMessage("Please select first a file you want to export", shell);
       
   138 			return;
       
   139 		}
       
   140 			
       
   141 		if (((IStructuredSelection)selection).size() > 1) {
       
   142 			showMessage("Please select a single file", shell);
       
   143 			return;
       
   144 		}
       
   145 		
       
   146 		List<CrashFileBundle> files = new ArrayList<CrashFileBundle>();
       
   147 		@SuppressWarnings("unchecked")
       
   148 		Iterator i = ((IStructuredSelection)selection).iterator();
       
   149 		// go through all selected files
       
   150 		while (i.hasNext()) {
       
   151 			CrashFileBundle cFileBundle = (CrashFileBundle)i.next();
       
   152 			// handle only files which contains xml file. only files which contain
       
   153 			// xml can be saved as html
       
   154 			if (cFileBundle.hasXml()) {
       
   155 				files.add(cFileBundle);
       
   156 			}
       
   157 		}
       
   158 		
       
   159 		// return if no xml files were found or if more than one file was found
       
   160 		if (files.isEmpty() || files.size() > 1) {
       
   161 			showMessage("Only decoded files can be exported as HTML", shell);
       
   162 			return;
       
   163 		}
       
   164 
       
   165 		String saveFilePath = FileOperations.saveAsDialog("Crash Analyser - Select XML location", 
       
   166 															new String[] {"*.htm"}, 
       
   167 															shell);
       
   168 		if (saveFilePath != null) {
       
   169 			files.get(0).saveAsHtml(new File(saveFilePath));
       
   170 		}
       
   171 	}
       
   172 	
       
   173 	/**
       
   174 	 * Exports a selected file to Xml format (.crashxml or .xml). If more than one 
       
   175 	 * file is selected, does nothing.
       
   176 	 * @param selection  IStructuredSelection of MainView's selected rows
       
   177 	 * @param shell shell for window
       
   178 	 */
       
   179 	public static void ExportSelectedFileToXml(ISelection selection, Shell shell) {
       
   180 		// return if nothing is selected or if more than one items are selected
       
   181 		if (selection == null || 
       
   182 			selection.isEmpty()) {
       
   183 			showMessage("Please select first a file you want to export", shell);
       
   184 			return;
       
   185 		}
       
   186 		if (((IStructuredSelection)selection).size() > 1) {
       
   187 			showMessage("Please select a single file", shell);
       
   188 			return;
       
   189 		}
       
   190 		
       
   191 		boolean crashXml = false;
       
   192 		ArrayList<String> files = new ArrayList<String>();
       
   193 		@SuppressWarnings("unchecked")
       
   194 		Iterator i = ((IStructuredSelection)selection).iterator();
       
   195 		// go through all selected files
       
   196 		while (i.hasNext()) {
       
   197 			CrashFileBundle cFileBundle = (CrashFileBundle)i.next();
       
   198 			// handle only files which contains xml file
       
   199 			if (cFileBundle.hasXml()) {
       
   200 				files.add(cFileBundle.getXmlFilePath());
       
   201 				crashXml = cFileBundle.isFullyDecoded();
       
   202 			}
       
   203 		}
       
   204 		
       
   205 		// return if no xml files were found or if more than one file was found
       
   206 		if (files.isEmpty() || files.size() > 1) {
       
   207 			showMessage("Only decoded files can be exported as XML", shell);
       
   208 			return;
       
   209 		}
       
   210 
       
   211 		String[] extension = {"*." + CrashAnalyserFile.SUMMARY_FILE_EXTENSION};
       
   212 		if (crashXml)
       
   213 			extension = new String[] {"*." + CrashAnalyserFile.OUTPUT_FILE_EXTENSION};
       
   214 		
       
   215 		String saveFilePath = FileOperations.saveAsDialog("Crash Analyser - Select XML location", extension, shell);
       
   216 		if (saveFilePath != null) {
       
   217 			FileOperations.copyFile(new File(files.get(0)), new File(saveFilePath), true);
       
   218 		}
       
   219 	}
       
   220 	
       
   221 	/**
       
   222 	 * A single row in selection can have multiple files. This method first zips all files found
       
   223 	 * in a single row in to a zip and then finally zips all "row zips" into one zip.
       
   224 	 * @param selection  IStructuredSelection of MainView's selected rows
       
   225 	 * @param shell shell for window
       
   226 	 */
       
   227 	public static void ExportSelectedFilesToZipInAllFormats(ISelection selection, Shell shell) {
       
   228 		// return if nothing is selected
       
   229 		if (selection == null || selection.isEmpty()) {
       
   230 			showMessage("Please select first the files you want to export", shell);
       
   231 			return;
       
   232 		}
       
   233 
       
   234 		List<CrashFileBundle> files = new ArrayList<CrashFileBundle>();
       
   235 		@SuppressWarnings("unchecked")
       
   236 		Iterator i = ((IStructuredSelection)selection).iterator();
       
   237 		// go through all selected files
       
   238 		while (i.hasNext()) {
       
   239 			CrashFileBundle cFileBundle = (CrashFileBundle)i.next();
       
   240 			// handle only files which are not empty nor emulator panics
       
   241 			if (!cFileBundle.isEmpty() && !cFileBundle.isEmulatorPanic()) {
       
   242 				files.add(cFileBundle);						
       
   243 			}
       
   244 		}
       
   245 		// no files, return
       
   246 		if (files.isEmpty())
       
   247 			return;
       
   248 		
       
   249 		String saveFilePath = FileOperations.saveAsDialog("Crash Analyser - Select zip location", new String[] {"*.zip"}, shell);
       
   250 		if (saveFilePath != null) {
       
   251 			List<String> zipFiles = new ArrayList<String>();
       
   252 			List<String> bundleFiles = new ArrayList<String>();
       
   253 			// zip all bundle files to zips
       
   254 			for (int j = 0; j < files.size(); j++) {
       
   255 				boolean nameSet = false;
       
   256 				CrashFileBundle cfb = files.get(j);
       
   257 				String zipFileName = FileOperations.addSlashToEnd(cfb.getBundleFolder());
       
   258 				// zip binary file
       
   259 				UndecodedFile uf = cfb.getUndecodedFile();
       
   260 				if (uf != null) {
       
   261 					bundleFiles.add(uf.getFilePath());
       
   262 					zipFileName += uf.getFileName() + EXTENSION_ZIP;
       
   263 					nameSet = true;
       
   264 				}
       
   265 				// zip .crashxml file
       
   266 				CrashFile cf = cfb.getCrashFile();
       
   267 				if (cf != null) {
       
   268 					bundleFiles.add(cf.getFilePath());
       
   269 					if (!nameSet) {
       
   270 						zipFileName += cf.getFileName() + EXTENSION_ZIP;
       
   271 						nameSet = true;
       
   272 					}
       
   273 				}
       
   274 				// zip .xml file
       
   275 				SummaryFile sf = cfb.getSummaryFile();
       
   276 				if (sf != null) {
       
   277 					bundleFiles.add(sf.getFilePath());
       
   278 					if (!nameSet) {
       
   279 						zipFileName += sf.getFileName() + EXTENSION_ZIP;
       
   280 						nameSet = true;
       
   281 					}
       
   282 				}
       
   283 				// zip html file
       
   284 				File htmlFile = cfb.getHtmlFile(true);
       
   285 				if (htmlFile != null) {
       
   286 					bundleFiles.add(htmlFile.getAbsolutePath());
       
   287 					if (!nameSet) {
       
   288 						zipFileName += htmlFile.getName() + EXTENSION_ZIP;
       
   289 					}
       
   290 				}
       
   291 				
       
   292 				if (!bundleFiles.isEmpty()) {
       
   293 					FileOperations.zipFiles(bundleFiles.toArray(new String[bundleFiles.size()]), zipFileName);
       
   294 					zipFiles.add(zipFileName);
       
   295 				}
       
   296 			}
       
   297 			
       
   298 			if (!zipFiles.isEmpty())
       
   299 				FileOperations.zipFiles(zipFiles.toArray(new String[zipFiles.size()]), saveFilePath);
       
   300 		}		
       
   301 	}
       
   302 }