src/modules/gui/cache.py
changeset 2991 75e616731cc3
parent 2990 2cc6693a7d83
child 2992 e48a94cff862
equal deleted inserted replaced
2990:2cc6693a7d83 2991:75e616731cc3
     1 #!/usr/bin/python
       
     2 #
       
     3 # CDDL HEADER START
       
     4 #
       
     5 # The contents of this file are subject to the terms of the
       
     6 # Common Development and Distribution License (the "License").
       
     7 # You may not use this file except in compliance with the License.
       
     8 #
       
     9 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
       
    10 # or http://www.opensolaris.org/os/licensing.
       
    11 # See the License for the specific language governing permissions
       
    12 # and limitations under the License.
       
    13 #
       
    14 # When distributing Covered Code, include this CDDL HEADER in each
       
    15 # file and include the License file at usr/src/OPENSOLARIS.LICENSE.
       
    16 # If applicable, add the following below this CDDL HEADER, with the
       
    17 # fields enclosed by brackets "[]" replaced with your own identifying
       
    18 # information: Portions Copyright [yyyy] [name of copyright owner]
       
    19 #
       
    20 # CDDL HEADER END
       
    21 #
       
    22 # Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
       
    23 # Use is subject to license terms.
       
    24 #
       
    25 
       
    26 import os
       
    27 import sys
       
    28 try:
       
    29         import gtk
       
    30 except ImportError:
       
    31         sys.exit(1)
       
    32 from threading import Thread
       
    33 import pkg.gui.misc_non_gui as nongui_misc
       
    34 
       
    35 CACHE_VERSION = 11
       
    36 
       
    37 class CacheListStores:
       
    38         def __init__(self, api_o):
       
    39                 self.api_o = api_o
       
    40 
       
    41         def __get_cache_dir(self):
       
    42                 return nongui_misc.get_cache_dir(self.api_o)
       
    43 
       
    44         def get_index_timestamp(self):
       
    45                 return self.api_o.last_modified
       
    46 
       
    47         def __dump_categories_expanded_dict(self, cat_exp_dict):
       
    48                 #CED entry: {('opensolaris.org', (6,)): True}
       
    49                 cache_dir = self.__get_cache_dir()
       
    50                 if not cache_dir:
       
    51                         return
       
    52                 catexs = []
       
    53                 for key, val in cat_exp_dict.iteritems():
       
    54                         if not val:
       
    55                                 continue
       
    56                         name, path = key
       
    57                         path1 = -1
       
    58                         if len(path) > 0:
       
    59                                 path1 = path[0]
       
    60                         catex = {}
       
    61                         catex["name"] = name
       
    62                         catex["path1"] = path1
       
    63                         catexs.append(catex)                
       
    64                 
       
    65                 nongui_misc.dump_cache_file(
       
    66                     os.path.join(cache_dir, "pm_cat_exp.cpl"),
       
    67                     catexs)
       
    68                     
       
    69         def __load_categories_expanded_dict(self, cat_exp_dict):
       
    70                 cache_dir = self.__get_cache_dir()
       
    71                 if not cache_dir:
       
    72                         return
       
    73                 catexs = nongui_misc.read_cache_file(
       
    74                     os.path.join(cache_dir, "pm_cat_exp.cpl"))
       
    75                 for catex in catexs:
       
    76                         name = catex.get("name")
       
    77                         path1 = catex.get("path1")
       
    78                         if path1 != -1:
       
    79                                 cat_exp_dict[name, (path1,)] = True
       
    80 
       
    81         def dump_categories_expanded_dict(self, cat_exp_dict):
       
    82                 Thread(target = self.__dump_categories_expanded_dict,
       
    83                     args = (cat_exp_dict, )).start()
       
    84 
       
    85         def load_categories_expanded_dict(self, cat_exp_dict):
       
    86                 Thread(target = self.__load_categories_expanded_dict,
       
    87                     args = (cat_exp_dict, )).start()
       
    88 
       
    89         def __dump_categories_active_dict(self, cat_ac_dict):
       
    90                 cache_dir = self.__get_cache_dir()
       
    91                 if not cache_dir:
       
    92                         return
       
    93                 catacs = []
       
    94                 for name, path in cat_ac_dict.iteritems():
       
    95                         path1 = -1
       
    96                         path2 = -1
       
    97                         if len(path) == 1:
       
    98                                 path1 = path[0]
       
    99                         elif len(path) > 1:
       
   100                                 path1 = path[0]
       
   101                                 path2 = path[1]                        
       
   102                         catac = {}
       
   103                         catac["name"] = name
       
   104                         catac["path1"] = path1
       
   105                         catac["path2"] = path2
       
   106                         catacs.append(catac)
       
   107                 
       
   108                 nongui_misc.dump_cache_file(
       
   109                     os.path.join(cache_dir, "pm_cat_ac.cpl"),
       
   110                     catacs)
       
   111                     
       
   112         def __load_categories_active_dict(self, cat_ac_dict):
       
   113                 cache_dir = self.__get_cache_dir()
       
   114                 if not cache_dir:
       
   115                         return
       
   116                 catacs = nongui_misc.read_cache_file(
       
   117                     os.path.join(cache_dir, "pm_cat_ac.cpl"))
       
   118                 for catac in catacs:
       
   119                         name = catac.get("name")
       
   120                         path1 = catac.get("path1")
       
   121                         path2 = catac.get("path2")
       
   122                         if path1 != -1 and path2 != -1:
       
   123                                 cat_ac_dict[name] = (path1, path2)
       
   124                         elif path1 != -1:
       
   125                                 cat_ac_dict[name] = (path1,)
       
   126 
       
   127         def dump_categories_active_dict(self, cat_ac_dict):
       
   128                 Thread(target = self.__dump_categories_active_dict,
       
   129                     args = (cat_ac_dict, )).start()
       
   130 
       
   131         def load_categories_active_dict(self, cat_ac_dict):
       
   132                 Thread(target = self.__load_categories_active_dict,
       
   133                     args = (cat_ac_dict, )).start()
       
   134 
       
   135         def __dump_search_completion_info(self, completion_list):
       
   136                 cache_dir = self.__get_cache_dir()
       
   137                 if not cache_dir:
       
   138                         return
       
   139                 texts = []
       
   140                 for text in completion_list:
       
   141                         txt = {}
       
   142                         txt["text"] = text[0]
       
   143                         texts.append(txt)
       
   144                 try:
       
   145                         nongui_misc.dump_cache_file(
       
   146                             os.path.join(cache_dir, ".__search__completion.cpl"), texts)
       
   147                 except IOError:
       
   148                         return
       
   149 
       
   150         def __load_search_completion_info(self, completion_list):
       
   151                 cache_dir = self.__get_cache_dir()
       
   152                 if not cache_dir:
       
   153                         return
       
   154                 texts = []
       
   155                 try:
       
   156                         texts = nongui_misc.read_cache_file(
       
   157                             os.path.join(cache_dir, ".__search__completion.cpl"))
       
   158                 except IOError:
       
   159                         return gtk.ListStore(str)
       
   160 
       
   161                 txt_count = 0
       
   162                 for txt in texts:
       
   163                         txt_val = txt.get("text")
       
   164                         text = [ txt_val ]
       
   165                         completion_list.insert(txt_count, text)
       
   166                         txt_count += 1
       
   167 
       
   168         def dump_search_completion_info(self, completion_list):
       
   169                 Thread(target = self.__dump_search_completion_info,
       
   170                     args = (completion_list, )).start()
       
   171 
       
   172         def load_search_completion_info(self, completion_list):
       
   173                 Thread(target = self.__load_search_completion_info,
       
   174                     args = (completion_list, )).start()