00001 ############################################################################## 00002 # 00003 # Copyright (c) 2007-2008 Albert Cervera i Areny <albert@nan-tic.com> 00004 # 00005 # WARNING: This program as such is intended to be used by professional 00006 # programmers who take the whole responsability of assessing all potential 00007 # consequences resulting from its eventual inadequacies and bugs 00008 # End users who are looking for a ready-to-use solution with commercial 00009 # garantees and support are strongly adviced to contract a Free Software 00010 # Service Company 00011 # 00012 # This program is Free Software; you can redistribute it and/or 00013 # modify it under the terms of the GNU General Public License 00014 # as published by the Free Software Foundation; either version 2 00015 # of the License, or (at your option) any later version. 00016 # 00017 # This program is distributed in the hope that it will be useful, 00018 # but WITHOUT ANY WARRANTY; without even the implied warranty of 00019 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00020 # GNU General Public License for more details. 00021 # 00022 # You should have received a copy of the GNU General Public License 00023 # along with this program; if not, write to the Free Software 00024 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00025 # 00026 ############################################################################## 00027 00028 from Koo import Rpc 00029 00030 ## @brief ViewSettings class allows storing and retrieving of view state 00031 # information such as column size and ordering in QListViews and the such. 00032 # 00033 # Settings are stored as a string (not unicode) and in most cases 00034 # end up converted to/from a QByteArray; hence the need of ensuring 00035 # we use str instead of unicode. That's why we enforce str() in a 00036 # couple of places. 00037 class ViewSettings: 00038 cache = {} 00039 databaseName = None 00040 uid = None 00041 hasSettingsModule = True 00042 00043 ## @brief Stores settings for the given view id. 00044 @staticmethod 00045 def store( id, settings ): 00046 if not id: 00047 return 00048 00049 ViewSettings.checkConnection() 00050 00051 if settings: 00052 # Ensure it's a string and not unicode 00053 settings = str(settings) 00054 00055 # Do not update store data in the server if it settings have not changed 00056 # from the ones in cache. 00057 if id in ViewSettings.cache and ViewSettings.cache[id] == settings: 00058 return 00059 00060 # Add settings in the cache. Note that even if the required koo 00061 # module is not installed in the server view settings will be kept 00062 # during user session. 00063 ViewSettings.cache[id] = settings 00064 00065 if not ViewSettings.hasSettingsModule: 00066 return 00067 00068 try: 00069 # We don't want to crash if the koo module is not installed on the server 00070 # but we do want to crash if there are mistakes in setViewSettings() code. 00071 ids = Rpc.session.call( '/object', 'execute', 'nan.koo.view.settings', 'search', [ 00072 ('user','=',Rpc.session.uid),('view','=',id) 00073 ]) 00074 except: 00075 ViewSettings.hasSettingsModule = False 00076 return 00077 # As 'nan.koo.view.settings' is proved to exist we don't need try-except here. And we 00078 # can use execute() instead of call(). 00079 if ids: 00080 Rpc.session.execute( '/object', 'execute', 'nan.koo.view.settings', 'write', ids, { 00081 'data': settings 00082 }) 00083 else: 00084 Rpc.session.execute( '/object', 'execute', 'nan.koo.view.settings', 'create', { 00085 'user': Rpc.session.uid, 00086 'view': id, 00087 'data': settings 00088 }) 00089 00090 ## @brief Loads information for the given view id. 00091 @staticmethod 00092 def load( id ): 00093 if not id: 00094 return None 00095 00096 ViewSettings.checkConnection() 00097 00098 if id in ViewSettings.cache: 00099 # Restore settings from the cache. Note that even if the required koo 00100 # module is not installed in the server view settings will be kept 00101 # during user session. 00102 return ViewSettings.cache[id] 00103 00104 if not ViewSettings.hasSettingsModule: 00105 return None 00106 00107 try: 00108 # We don't want to crash if the koo module is not installed on the server 00109 # but we do want to crash if there are mistakes in setViewSettings() code. 00110 ids = Rpc.session.call( '/object', 'execute', 'nan.koo.view.settings', 'search', [ 00111 ('user','=',Rpc.session.uid),('view','=',id) 00112 ]) 00113 except: 00114 ViewSettings.hasSettingsModule = False 00115 return None 00116 # As 'nan.koo.view.settings' is proved to exist we don't need try-except here. 00117 if not ids: 00118 return None 00119 settings = Rpc.session.execute( '/object', 'execute', 'nan.koo.view.settings', 'read', ids, ['data'] )[0]['data'] 00120 00121 if settings: 00122 # Ensure it's a string and not unicode 00123 settings = str(settings) 00124 00125 ViewSettings.cache[id] = settings 00126 00127 return settings 00128 00129 ## @brief Checks if connection has changed and clears cache and hasSettingsModule flag 00130 @staticmethod 00131 def checkConnection(): 00132 if ViewSettings.databaseName != Rpc.session.databaseName or ViewSettings.uid != Rpc.session.uid: 00133 ViewSettings.clear() 00134 00135 ## @brief Clears cache and resets state. This means that after installing the koo 00136 # module you don't have to close session and login again because 00137 # hasSettingsModule is reset to True. 00138 @staticmethod 00139 def clear(): 00140 ViewSettings.databaseName = Rpc.session.databaseName 00141 ViewSettings.uid = Rpc.session.uid 00142 ViewSettings.hasSettingsModule = True 00143 ViewSettings.cache = {} 00144