themeinstaller/source/src/com/nokia/tools/themeinstaller/defrep/DefinitionRepository.java
branchRCL_3
changeset 32 fe49e33862e2
parent 31 b685c59de105
child 33 04b7640f6fb5
equal deleted inserted replaced
31:b685c59de105 32:fe49e33862e2
     1 /*
       
     2  * Copyright (c) 2007 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:  Definition Repository implementation
       
    15  *
       
    16  */
       
    17 
       
    18 package com.nokia.tools.themeinstaller.defrep;
       
    19 
       
    20 import java.io.File;
       
    21 import java.io.IOException;
       
    22 import java.io.InputStream;
       
    23 import java.util.Observer;
       
    24 
       
    25 import com.nokia.tools.themeinstaller.odtconverter.ConverterProperties;
       
    26 import com.nokia.tools.themeinstaller.odtconverter.ODTHeader;
       
    27 import com.nokia.tools.themeinstaller.odtconverter.ThemeStatusResolver;
       
    28 import com.nokia.tools.themeinstaller.defrep.operations.CopyOperation;
       
    29 import com.nokia.tools.themeinstaller.defrep.operations.FileOperation;
       
    30 import com.nokia.tools.themeinstaller.defrep.operations.StoreOperation;
       
    31 
       
    32 /**
       
    33  * Definition repository class.
       
    34  */
       
    35 public class DefinitionRepository implements IDefinitionRepository {
       
    36 
       
    37 	// CONSTANTS
       
    38 	// Property keys
       
    39 	private static final String THEMES_ROOT_ROM = "themes_datacage_rom";
       
    40 	private static final String THEMES_ROOT_USER_DISC = "themes_datacage_user_disc";
       
    41 	private static final String ODT_FILE_EXT_FORMAT = "odt_file_ext_format";
       
    42 	private static final String ODT_RESOURCES_FOLDER = "odt_resources_folder";
       
    43 	private static final String ODT_LANG_RESOURCES_FOLDER = "odt_lang_resources_folder";
       
    44 
       
    45 	// Singleton instance
       
    46 	private static DefinitionRepository sInstance = null;
       
    47 
       
    48 	/**
       
    49 	 * Definition repository private constructor.
       
    50 	 */
       
    51 	private DefinitionRepository() {
       
    52 	}
       
    53 
       
    54 	/**
       
    55 	 * Get a Definition Repository instance (singleton).
       
    56 	 * 
       
    57 	 * @return Definition Repository instance
       
    58 	 */
       
    59 	public static DefinitionRepository getInstance() {
       
    60 		if (sInstance == null) {
       
    61 			sInstance = new DefinitionRepository();
       
    62 		}
       
    63 
       
    64 		return sInstance;
       
    65 	}
       
    66 
       
    67 	/*
       
    68 	 * (non-Javadoc)
       
    69 	 * 
       
    70 	 * @see
       
    71 	 * com.nokia.tools.odtconverter.defrep.IDefinitionRepository#copy(java.lang
       
    72 	 * .String, java.util.Observer)
       
    73 	 */
       
    74 	public void copy(File aSource, File aDestination, boolean aAppend,
       
    75 			Observer aListener) {
       
    76 		CopyOperation operation = new CopyOperation(aSource, aDestination,
       
    77 				aAppend);
       
    78 		operation.addObserver(aListener);
       
    79 		performOperation(operation);
       
    80 	}
       
    81 
       
    82 	/*
       
    83 	 * (non-Javadoc)
       
    84 	 * 
       
    85 	 * @see
       
    86 	 * com.nokia.tools.odtconverter.defrep.IDefinitionRepository#store(java.
       
    87 	 * lang.String, java.io.InputStream, java.util.Observer)
       
    88 	 */
       
    89 	public void store(File aDestination, InputStream aStream, Observer aListener) {
       
    90 		StoreOperation operation = new StoreOperation(aDestination, aStream);
       
    91 		operation.addObserver(aListener);
       
    92 		performOperation(operation);
       
    93 	}
       
    94 
       
    95 	/*
       
    96 	 * (non-Javadoc)
       
    97 	 * 
       
    98 	 * @see
       
    99 	 * com.nokia.tools.odtconverter.defrep.IDefinitionRepository#storeODT(java
       
   100 	 * .io.File, com.nokia.tools.odtconverter.ODTDocument, java.util.Observer)
       
   101 	 */
       
   102 	public void storeODT(File aDestination, ODTHeader aHeader,
       
   103 			InputStream aStream, Observer aListener) throws IOException {
       
   104 		File destination = new File(createODTPath(aDestination, aHeader));
       
   105 		store(destination, aStream, aListener);
       
   106 	}
       
   107 
       
   108 	/*
       
   109 	 * (non-Javadoc)
       
   110 	 * 
       
   111 	 * @see
       
   112 	 * com.nokia.tools.odtconverter.defrep.IDefinitionRepository#copyResource
       
   113 	 * (java.io.File, java.io.File, com.nokia.tools.odtconverter.ODTHeader,
       
   114 	 * java.util.Observer)
       
   115 	 */
       
   116 	public void copyResource(File aSource, File aDestination,
       
   117 			ODTHeader aHeader, Observer aListener) throws IOException {
       
   118 		ConverterProperties properties = ConverterProperties.getInstance();
       
   119 		StringBuffer odtPath = combineDirectory(aDestination, aHeader,
       
   120 				properties);
       
   121 
       
   122 		// Get the language to resolve the destination directory
       
   123 		Integer language = (Integer) aHeader.get(ODTHeader.Language);
       
   124 		if (language != null) {
       
   125 			// Location for language specific resources
       
   126 			String template = properties.getProperty(ODT_LANG_RESOURCES_FOLDER);
       
   127 
       
   128 			Integer[] formatArguments = { language };
       
   129 			odtPath.append(String.format(template, formatArguments));
       
   130 		} else {
       
   131 			// Location for language independent resources
       
   132 			odtPath.append(properties.get(ODT_RESOURCES_FOLDER));
       
   133 		}
       
   134 
       
   135 		odtPath.append(aSource.getName());
       
   136 		File destFile = new File(odtPath.toString());
       
   137 		copy(aSource, destFile, false, aListener);
       
   138 	}
       
   139 
       
   140 	/*
       
   141 	 * (non-Javadoc)
       
   142 	 * 
       
   143 	 * @see
       
   144 	 * com.nokia.tools.odtconverter.defrep.IDefinitionRepository#createODTPath
       
   145 	 * (java.lang.String, com.nokia.tools.odtconverter.ODTHeader,
       
   146 	 * com.nokia.tools.odtconverter.ConverterProperties)
       
   147 	 */
       
   148 	public String createODTPath(File aDestination, ODTHeader aHeader) {
       
   149 		// Get themes installation directory from the properties
       
   150 		ConverterProperties properties = ConverterProperties.getInstance();
       
   151 
       
   152 		StringBuffer odtPath = combineDirectory(aDestination, aHeader,
       
   153 				properties);
       
   154 
       
   155 		// Append ODT file name
       
   156 		odtPath.append(aHeader.get(ODTHeader.ThemeShortName));
       
   157 
       
   158 		// Append extension according to the language
       
   159 		String format = properties.getProperty(ODT_FILE_EXT_FORMAT);
       
   160 		Integer[] formatArguments = { (Integer) aHeader.get(ODTHeader.Language) };
       
   161 		odtPath.append(String.format(format, formatArguments));
       
   162 
       
   163 		return odtPath.toString();
       
   164 	}
       
   165 
       
   166 	/**
       
   167 	 * Combine the installation directory structure. According to the Xuikon,
       
   168 	 * the destination directory for locked themes is ROM location. Other themes
       
   169 	 * are stored to the user disk. Rest of the path is determined by theme
       
   170 	 * details: Application UID, Provider UID, Theme UID and Theme Version.
       
   171 	 * 
       
   172 	 * @param aDestination
       
   173 	 *            destination root directory, usually the epoc root
       
   174 	 * @param aHeader
       
   175 	 *            ODT file header
       
   176 	 * @param aProperties
       
   177 	 *            converter properties instance
       
   178 	 * @return complete destination path name for the installed theme
       
   179 	 */
       
   180 	private StringBuffer combineDirectory(File aDestination, ODTHeader aHeader,
       
   181 			ConverterProperties aProperties) {
       
   182 		StringBuffer odtPath = new StringBuffer();
       
   183 		odtPath.append(aDestination.getPath());
       
   184 
       
   185 		// Add file separator only if the destination directory does not
       
   186 		// already end with the separator
       
   187 		if (!aDestination.getPath().endsWith(File.separator)) {
       
   188 			odtPath.append(File.separator);
       
   189 		}
       
   190 
       
   191 		// if EXnThemeStatusLicenceeDefault flag is set,
       
   192 		// theme is installed to ROM (z:\) otherwise to user disc (c:\)
       
   193 		if ((((Integer) aHeader.get(ODTHeader.Flags)).intValue() & ThemeStatusResolver.E_XN_THEME_STATUS_LICENCEE_DEFAULT) != 0) {
       
   194 			odtPath.append(aProperties.getProperty(THEMES_ROOT_ROM));
       
   195 		} else {
       
   196 			odtPath.append(aProperties.getProperty(THEMES_ROOT_USER_DISC));
       
   197 		}
       
   198 
       
   199 		//Bypass adding appuid etc, if we don't have these properties
       
   200 		if (aHeader.get(ODTHeader.ApplicationUID) != null && aHeader.get(ODTHeader.ProviderUID)!= null
       
   201 				&&  aHeader.get(ODTHeader.ThemeUID) != null && aHeader.get(ODTHeader.ThemeVersion) != null){
       
   202 			// Append Application UID
       
   203 			odtPath.append(((Long) aHeader.get(ODTHeader.ApplicationUID))
       
   204 					.longValue());
       
   205 		
       
   206 			odtPath.append(File.separator);
       
   207 
       
   208 			// Append Provider UID
       
   209 			odtPath.append(((Long) aHeader.get(ODTHeader.ProviderUID))
       
   210 					.longValue());
       
   211 			odtPath.append(File.separator);
       
   212 
       
   213 			// Append Theme UID
       
   214 			odtPath
       
   215 					.append(((Long) aHeader.get(ODTHeader.ThemeUID))
       
   216 							.longValue());
       
   217 			odtPath.append(File.separator);
       
   218 
       
   219 			// Append Theme version
       
   220 			odtPath.append(aHeader.get(ODTHeader.ThemeVersion));
       
   221 			odtPath.append(File.separator);
       
   222 		}
       
   223 		return odtPath;
       
   224 	}
       
   225 
       
   226 	/**
       
   227 	 * Start a file operation in a new thread.
       
   228 	 * 
       
   229 	 * @param aOperation
       
   230 	 *            The operation to start.
       
   231 	 */
       
   232 	private void performOperation(FileOperation aOperation) {
       
   233 		Thread thread = new Thread(aOperation);
       
   234 		thread.start();
       
   235 	}
       
   236 
       
   237 }