configurationengine/source/plugins/symbian/ConeThemePlugin/themeplugin/theme_container.py
changeset 0 2e8eeb919028
child 3 e7e0ae78773e
equal deleted inserted replaced
-1:000000000000 0:2e8eeb919028
       
     1 #
       
     2 # Copyright (c) 2009 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 import os
       
    18 import unzip
       
    19 import shutil
       
    20 import logging
       
    21 from themeplugin import theme_function
       
    22 from theme_resource import ThemeResource
       
    23 from cone.storage import filestorage
       
    24 from cone.public import plugin
       
    25 
       
    26 class ThemeContainer:
       
    27     """
       
    28     This class provides extracts *.tpf files, convertts to *.mbm,*.pkg, ... files and set UID(PID)
       
    29     to the setting values in the model 
       
    30     """
       
    31     
       
    32     def __init__(self, list_tpf, configuration):
       
    33         self.list_tpf=list_tpf
       
    34         self.configuration=configuration
       
    35         self.list_theme=[]
       
    36         self.logger = logging.getLogger('cone.thememl')
       
    37         self.carbide = None
       
    38 
       
    39     def create_themes(self):
       
    40         """
       
    41         extractes tpf file to the temporary directory and creates Theme objects
       
    42         """
       
    43 
       
    44         for tpf in self.list_tpf:
       
    45             logging.getLogger('cone.thememl').info("Creating temp folder for %s" % tpf)
       
    46             theme = Theme()
       
    47             theme.set_tpf_path(tpf)
       
    48             
       
    49             temp_tdf = os.tempnam("Theme")
       
    50             os.mkdir(temp_tdf)
       
    51             temp_theme = os.path.join(temp_tdf,"__temp__")
       
    52             os.mkdir(temp_theme)
       
    53             theme.set_temp_theme(temp_theme)
       
    54             theme.set_temp_tdf(temp_tdf)
       
    55 
       
    56             self.list_theme.append(theme)
       
    57         
       
    58 
       
    59         
       
    60     def build_theme(self, theme_version):
       
    61         """
       
    62         converts *.tpf files to *.mbm, *.skn, ...
       
    63         """
       
    64         for theme in self.list_theme:
       
    65             self.make_theme(theme, theme_version)
       
    66 
       
    67             
       
    68     def prepare_active_themes(self,list_active_theme):
       
    69         """
       
    70         goes through the active themes and sets theme in the list of all themes as active {set the name and
       
    71         the uid number of the platform setting}
       
    72         """
       
    73         default_view = self.configuration.get_default_view()
       
    74         for active_theme in list_active_theme:
       
    75             if active_theme.get_setting_ref():
       
    76                 path=active_theme.get_setting_ref().replace("/",".")
       
    77                 setting = default_view.get_feature(path+".localPath").get_data()
       
    78                 if setting != None and setting.get_value():
       
    79                     setting_value = setting.get_value()
       
    80                     self.set_settinguid_to_theme(active_theme,setting_value)
       
    81 
       
    82     def set_settinguid_to_theme(self,active_theme, path):
       
    83         """
       
    84         finds out the active theme and set the name and the uid of the platform setting
       
    85         """
       
    86         path = "/content/"+path
       
    87         for theme in self.list_theme:
       
    88             tpf_path = theme.get_tpf_path()
       
    89             
       
    90             if tpf_path.endswith(path):
       
    91                  for setting_uid in active_theme.get_setting_uids():
       
    92                     setting_uid_value = setting_uid.replace("/",".")
       
    93                     theme.set_setting_uids(setting_uid_value)
       
    94                     theme.set_uid(active_theme.get_uid())
       
    95                     
       
    96     def set_active_PID_to_model(self):
       
    97         """
       
    98         finds active theme, gets PID from pkg file, convert PID from hexadecimal to decimal formal
       
    99         and set decimal PID to the aknskins setting in the model
       
   100         """
       
   101         l = len (self.list_theme)
       
   102 
       
   103         
       
   104         
       
   105         for theme in self.list_theme:
       
   106             
       
   107             # Make sure autoconfig is the last layer
       
   108             plugin.get_autoconfig(self.configuration)
       
   109 
       
   110             default_view = self.configuration.get_default_view()
       
   111             
       
   112             for setting_uid in theme.get_setting_uids():
       
   113                 aknskins_setting = default_view.get_feature(setting_uid)
       
   114                 if(theme.get_uid()):
       
   115                     uid = int(theme.get_uid(),16)
       
   116                     aknskins_setting.set_value(str(uid))
       
   117                 else:
       
   118                     PID = theme_function.find_text_in_file(os.path.join(theme.get_temp_theme(), "themepackage.pkg"), "!:\\resource\\skins\\", "\\")
       
   119                     dec_PID = theme_function.convert_hexa_to_decimal(PID)
       
   120                     if dec_PID and aknskins_setting:
       
   121                         dec_PID = theme_function.convert_hexa_to_decimal(PID)
       
   122                         aknskins_setting.set_value(str(dec_PID))
       
   123            
       
   124     def make_theme(self, theme, theme_version):
       
   125         """
       
   126         converts the *tdf, *. svg files to *.mbm, *.pkg files, ...
       
   127         The first this method extracts tpf file and then calls carbide.ui command-line
       
   128         which converts theme.
       
   129         """
       
   130         output_path = theme.get_temp_theme()
       
   131         
       
   132         if not os.path.exists(output_path): 
       
   133             os.makedirs(output_path)
       
   134         
       
   135         storagepath = self.configuration.get_storage().get_path()
       
   136 
       
   137         input_path =  theme.get_tpf_path().replace("/","\\")
       
   138         
       
   139         zip_output= theme.get_temp_tdf() + "\\"
       
   140         self.unzip_tpf(theme,zip_output)
       
   141         
       
   142         name_tdf = theme_function.get_tdf_file(zip_output)
       
   143         name_tdf = os.path.join(name_tdf,name_tdf+".tdf")
       
   144         input_tdf = os.path.join(zip_output,name_tdf)
       
   145        
       
   146         command_line = "makepackage -input " + input_tdf + " -output " + output_path
       
   147         
       
   148         if len(theme_version) != 0:
       
   149             command_line = command_line + " -ver "+ theme_version
       
   150         
       
   151         if theme.get_uid() != None:
       
   152             command_line = command_line + " -uid " + theme.get_uid()
       
   153             
       
   154         logging.getLogger('cone.thememl').info("Building theme: %s" % command_line)
       
   155         current_dir = os.getcwd()
       
   156         os.chdir(self.carbide)
       
   157         os.system(command_line)
       
   158         os.chdir(current_dir)
       
   159         
       
   160         
       
   161     def unzip_tpf(self, theme, zip_output):
       
   162         """
       
   163         unzip the tpf file to output directory
       
   164         """
       
   165         f_storage = filestorage.FileStorage(theme.get_temp_tdf(), 'wb')
       
   166         list=[]
       
   167         list.append(theme.get_tpf_path())
       
   168         storage = self.configuration.get_storage()
       
   169         storage.export_resources(list, f_storage)
       
   170         
       
   171         tpf_file = os.path.join(theme.get_temp_tdf(),theme.get_tpf_path().replace("/","\\"))
       
   172         
       
   173         unzip.unzip_file_into_dir(tpf_file,zip_output)
       
   174 
       
   175         
       
   176     def copy_resources_to_output(self,output):
       
   177         """
       
   178         copies *.mbm, *.skn ... to respective directories in the output directory
       
   179         """
       
   180         for theme in self.list_theme:
       
   181             #gets list of path where *.mbm, *.skn, ... will be copied
       
   182             theme_resource = ThemeResource()
       
   183             theme_resource.parse_pkg_file(os.path.join(theme.get_temp_theme(), "themepackage.pkg"))
       
   184                                           
       
   185             # copies *.mbm, *.skn ... to target paths
       
   186             theme_resource.copy_files_from_theme(theme.get_temp_theme(), output)
       
   187       
       
   188         
       
   189     def removeTempDirs(self):
       
   190         """
       
   191         remove temporary directories
       
   192         """
       
   193 
       
   194         for theme in self.list_theme:
       
   195             shutil.rmtree(theme.get_temp_tdf())
       
   196             
       
   197 class Theme:
       
   198     """
       
   199     This class has information about theme. It contains path of tpf file and temporary directories
       
   200     where the theme was extracted and builded
       
   201     Ans also it contains information about the name of setting which has value as UID. The theme 
       
   202     hasn't to have this the name of setting.
       
   203     """
       
   204     def __init__(self):
       
   205         #where the theme was extracted
       
   206         self.temp_tdf = ""
       
   207         #where the theme was builded
       
   208         self.temp_theme = ""
       
   209         #the path of tpf file
       
   210         self.tpf_path = ""
       
   211         # the name of the setting which contains UID
       
   212         self.setting_uids = []
       
   213         self.uids = []
       
   214         self.uid = None
       
   215         
       
   216     def set_tpf_path(self, tpf_path):
       
   217         self.tpf_path = tpf_path
       
   218     
       
   219     def get_tpf_path(self):
       
   220         return self.tpf_path
       
   221     
       
   222     def set_temp_tdf(self, temp_tdf):
       
   223         self.temp_tdf = temp_tdf
       
   224         
       
   225     def get_temp_tdf(self):
       
   226         return self.temp_tdf
       
   227     
       
   228     def set_temp_theme(self, temp_theme):
       
   229         self.temp_theme = temp_theme
       
   230         
       
   231     def get_temp_theme(self):
       
   232         return self.temp_theme
       
   233     
       
   234     def set_setting_uids(self, setting_uid):
       
   235         self.setting_uids.append(setting_uid)
       
   236         
       
   237     def get_setting_uids(self):
       
   238         return self.setting_uids
       
   239     
       
   240     def set_uid(self, uid):
       
   241         self.uid = uid
       
   242         
       
   243     def get_uid(self):
       
   244         return self.uid
       
   245     
       
   246 class ActiveTheme(object):
       
   247     """
       
   248     This class performs information from thememl file. 
       
   249     It contains the name of settig (its value contains the path of tpf file) and
       
   250     the name of setting which contains UID
       
   251      
       
   252     """
       
   253 
       
   254     def __init__(self):
       
   255         self.ref_setting = None
       
   256         self.setting_uids = []
       
   257         self.uid = None
       
   258     
       
   259     def set_setting_ref(self, ref_setting):
       
   260         self.ref_setting  = ref_setting
       
   261         
       
   262     def set_setting_uids(self, setting_uid):
       
   263         self.setting_uids.append(setting_uid)
       
   264         
       
   265     def get_setting_ref(self):
       
   266         return self.ref_setting
       
   267         
       
   268     def get_setting_uids(self):
       
   269         return self.setting_uids
       
   270     
       
   271     def get_uid(self):
       
   272         return self.uid
       
   273     
       
   274     def set_uid(self,uid):
       
   275         self.uid = uid