[4] | 1 | ############################################################################ |
---|
| 2 | # |
---|
| 3 | # SAGE UI - A Graphical User Interface for SAGE |
---|
| 4 | # Copyright (C) 2005 Electronic Visualization Laboratory, |
---|
| 5 | # University of Illinois at Chicago |
---|
| 6 | # |
---|
| 7 | # All rights reserved. |
---|
| 8 | # |
---|
| 9 | # Redistribution and use in source and binary forms, with or without |
---|
| 10 | # modification, are permitted provided that the following conditions are met: |
---|
| 11 | # |
---|
| 12 | # * Redistributions of source code must retain the above copyright |
---|
| 13 | # notice, this list of conditions and the following disclaimer. |
---|
| 14 | # * Redistributions in binary form must reproduce the above |
---|
| 15 | # copyright notice, this list of conditions and the following disclaimer |
---|
| 16 | # in the documentation and/or other materials provided with the distribution. |
---|
| 17 | # * Neither the name of the University of Illinois at Chicago nor |
---|
| 18 | # the names of its contributors may be used to endorse or promote |
---|
| 19 | # products derived from this software without specific prior written permission. |
---|
| 20 | # |
---|
| 21 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
---|
| 22 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
---|
| 23 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
---|
| 24 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
---|
| 25 | # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
---|
| 26 | # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
---|
| 27 | # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
---|
| 28 | # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
---|
| 29 | # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
---|
| 30 | # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
---|
| 31 | # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
| 32 | # |
---|
| 33 | # Direct questions, comments etc about SAGE UI to www.evl.uic.edu/cavern/forum |
---|
| 34 | # |
---|
| 35 | # Author: Ratko Jagodic |
---|
| 36 | # |
---|
| 37 | ############################################################################ |
---|
| 38 | |
---|
| 39 | |
---|
| 40 | ### MODULE FOR SAVING SAGE UI PREFERENCES ### |
---|
| 41 | |
---|
| 42 | import os, os.path, cPickle, copy, sys |
---|
| 43 | |
---|
| 44 | from globals import * |
---|
| 45 | from sagePath import getUserPath |
---|
| 46 | |
---|
| 47 | # where we store all the prefs |
---|
| 48 | PREFS_DIR = getUserPath("sageui", "prefs") |
---|
| 49 | FILE_LIB_FILE = opj(PREFS_DIR, "file_libs.pickle") |
---|
| 50 | USERNAMES_FILE = opj(PREFS_DIR, "usernames.pickle") |
---|
| 51 | VISUAL_FILE = opj(PREFS_DIR, "visuals.pickle") |
---|
| 52 | FAVORITES_FILE = opj(PREFS_DIR, "favorite_files.pickle") |
---|
| 53 | MACHINES_FILE = opj(PREFS_DIR, "machines.pickle") |
---|
| 54 | VNC_FILE = opj(PREFS_DIR, "vnc.pickle") |
---|
| 55 | |
---|
| 56 | global fileLib |
---|
| 57 | global usernames |
---|
| 58 | global visual |
---|
| 59 | global favorite_files |
---|
| 60 | global machines |
---|
| 61 | global vnc |
---|
| 62 | |
---|
| 63 | |
---|
| 64 | #------------------------------------------------------- |
---|
| 65 | ### for saving and loading preferences |
---|
| 66 | #------------------------------------------------------- |
---|
| 67 | |
---|
| 68 | def readAllPreferences(): |
---|
| 69 | global fileLib |
---|
| 70 | global usernames |
---|
| 71 | global visual |
---|
| 72 | global favorite_files |
---|
| 73 | global machines |
---|
| 74 | global vnc |
---|
| 75 | |
---|
| 76 | |
---|
| 77 | p = readPreferences(FILE_LIB_FILE) |
---|
| 78 | if not p: fileLib = FileLibraryPrefs() |
---|
| 79 | else: fileLib = p |
---|
| 80 | |
---|
| 81 | p = readPreferences(USERNAMES_FILE) |
---|
| 82 | if not p: usernames = Usernames() |
---|
| 83 | else: usernames = p |
---|
| 84 | |
---|
| 85 | p = readPreferences(VISUAL_FILE) |
---|
| 86 | if not p: visual = VisualPrefs() |
---|
| 87 | else: visual = p |
---|
| 88 | |
---|
| 89 | p = readPreferences(FAVORITES_FILE) |
---|
| 90 | if not p: favorite_files = FavoriteFiles() |
---|
| 91 | else: favorite_files = p |
---|
| 92 | |
---|
| 93 | p = readPreferences(MACHINES_FILE) |
---|
| 94 | if not p: machines = MachinePrefs() |
---|
| 95 | else: machines = p |
---|
| 96 | |
---|
| 97 | p = readPreferences(VNC_FILE) |
---|
| 98 | if not p: vnc = VNCPrefs() |
---|
| 99 | else: vnc = p |
---|
| 100 | |
---|
| 101 | |
---|
| 102 | |
---|
| 103 | ### reads the preferences from a file |
---|
| 104 | def readPreferences(sourceFile): |
---|
| 105 | try: |
---|
| 106 | f = open(sourceFile, "rb") |
---|
| 107 | (objToLoad) = cPickle.Unpickler(f).load() |
---|
| 108 | f.close() |
---|
| 109 | return objToLoad |
---|
| 110 | except: |
---|
| 111 | #print sys.exc_info()[0], sys.exc_info()[1] |
---|
| 112 | return False |
---|
| 113 | |
---|
| 114 | |
---|
| 115 | ### saves the provided object into a specified file |
---|
| 116 | def savePreferences(objToSave, targetFile): |
---|
| 117 | try: |
---|
| 118 | f = open(targetFile, "wb") |
---|
| 119 | cPickle.Pickler(f, cPickle.HIGHEST_PROTOCOL).dump(objToSave) |
---|
| 120 | f.close() |
---|
| 121 | except: |
---|
| 122 | #print sys.exc_info()[0], sys.exc_info()[1] |
---|
| 123 | return False |
---|
| 124 | |
---|
| 125 | |
---|
| 126 | |
---|
| 127 | |
---|
| 128 | #------------------------------------------------------- |
---|
| 129 | # datastructures for preferences |
---|
| 130 | #------------------------------------------------------- |
---|
| 131 | |
---|
| 132 | |
---|
| 133 | class VNCPrefs: |
---|
| 134 | def __init__(self): |
---|
| 135 | self.__profiles = {} #keyed by profile name |
---|
| 136 | |
---|
| 137 | def AddProfile(self, profileName, ip, displayNum, size, passwd): |
---|
| 138 | self.__profiles[profileName] = (ip, displayNum, size, passwd) |
---|
| 139 | self.__Save() |
---|
| 140 | |
---|
| 141 | def DeleteProfile(self, profileName): |
---|
| 142 | if self.__profiles.has_key(profileName): |
---|
| 143 | del self.__profiles[profileName] |
---|
| 144 | self.__Save() |
---|
| 145 | |
---|
| 146 | def ProfileExists(self, profileName): |
---|
| 147 | return self.__profiles.has_key(profileName) |
---|
| 148 | |
---|
| 149 | def GetProfileList(self): |
---|
| 150 | return copy.deepcopy(self.__profiles.keys()) |
---|
| 151 | |
---|
| 152 | def GetProfile(self, profileName): |
---|
| 153 | return self.__profiles[profileName] |
---|
| 154 | |
---|
| 155 | def __Save(self): |
---|
| 156 | savePreferences(self, VNC_FILE) |
---|
| 157 | |
---|
| 158 | |
---|
| 159 | |
---|
| 160 | class Usernames: |
---|
| 161 | def __init__(self): |
---|
| 162 | self.__usernames = ["New-user"] |
---|
| 163 | self.__default = "New-user" |
---|
| 164 | |
---|
| 165 | def AddUsername(self, username): |
---|
| 166 | if not username in self.__usernames: |
---|
| 167 | self.__usernames.append(str(username)) |
---|
| 168 | self.__default = str(username) |
---|
| 169 | self.__Save() |
---|
| 170 | |
---|
| 171 | def GetDefault(self): |
---|
| 172 | return self.__default |
---|
| 173 | |
---|
| 174 | def GetUsernames(self): |
---|
| 175 | return copy.deepcopy(self.__usernames) |
---|
| 176 | |
---|
| 177 | def __Save(self): |
---|
| 178 | savePreferences(self, USERNAMES_FILE) |
---|
| 179 | |
---|
| 180 | |
---|
| 181 | |
---|
| 182 | class MachinePrefs: |
---|
| 183 | def __init__(self): |
---|
| 184 | self.__machines = {} |
---|
| 185 | |
---|
| 186 | def GetMachineHash(self): |
---|
| 187 | return copy.deepcopy(self.__machines) |
---|
| 188 | |
---|
| 189 | def AddMachine(self, sageMachine): |
---|
| 190 | self.__machines[sageMachine.GetId()] = sageMachine |
---|
| 191 | self.__Save() |
---|
| 192 | |
---|
| 193 | def RemoveMachine(self, sageMachine): |
---|
| 194 | if self.__machines.has_key(sageMachine.GetId()): |
---|
| 195 | del self.__machines[sageMachine.GetId()] |
---|
| 196 | self.__Save() |
---|
| 197 | |
---|
| 198 | def __Save(self): |
---|
| 199 | savePreferences(self, MACHINES_FILE) |
---|
| 200 | |
---|
| 201 | |
---|
| 202 | |
---|
| 203 | class VisualPrefs: |
---|
| 204 | def __init__(self): |
---|
| 205 | self.__frameSize = None |
---|
| 206 | self.__framePos = None |
---|
| 207 | self.__chatShown = None |
---|
| 208 | self.__receivePerfData = True |
---|
| 209 | self.__keepAspectRatio = True |
---|
| 210 | |
---|
| 211 | def SetFrameSize(self, size): |
---|
| 212 | self.__frameSize = size |
---|
| 213 | self.__Save() |
---|
| 214 | |
---|
| 215 | def SetFramePos(self, pos): |
---|
| 216 | self.__framePos = pos |
---|
| 217 | self.__Save() |
---|
| 218 | |
---|
| 219 | def ShowChat(self, doShow): |
---|
| 220 | self.__chatShown = doShow |
---|
| 221 | self.__Save() |
---|
| 222 | |
---|
| 223 | def SetReceivePerformanceData(self, doReceive): |
---|
| 224 | self.__receivePerfData = doReceive |
---|
| 225 | self.__Save() |
---|
| 226 | |
---|
| 227 | def SetKeepAspectRatio(self, doKeep): |
---|
| 228 | self.__keepAspectRatio = doKeep |
---|
| 229 | self.__Save() |
---|
| 230 | |
---|
| 231 | def GetKeepAspectRatio(self): |
---|
| 232 | return self.__keepAspectRatio |
---|
| 233 | |
---|
| 234 | def GetReceivePerformanceData(self): |
---|
| 235 | return self.__receivePerfData |
---|
| 236 | |
---|
| 237 | def GetFramePos(self): |
---|
| 238 | return self.__framePos |
---|
| 239 | |
---|
| 240 | def GetFrameSize(self): |
---|
| 241 | return self.__frameSize |
---|
| 242 | |
---|
| 243 | def IsChatShown(self): |
---|
| 244 | return self.__chatShown |
---|
| 245 | |
---|
| 246 | def SetAll(self, size, pos, chatShow): |
---|
| 247 | self.__frameSize = size |
---|
| 248 | self.__framePos = pos |
---|
| 249 | self.__chatShown = chatShow |
---|
| 250 | self.__Save() |
---|
| 251 | |
---|
| 252 | def __Save(self): |
---|
| 253 | savePreferences(self, VISUAL_FILE) |
---|
| 254 | |
---|
| 255 | |
---|
| 256 | |
---|
| 257 | class FavoriteFiles: |
---|
| 258 | def __init__(self): |
---|
| 259 | self.__favorites = {} |
---|
| 260 | |
---|
| 261 | def AddFavorite(self, itemData): |
---|
| 262 | type = itemData.GetType() |
---|
| 263 | if not self.__favorites.has_key(type): |
---|
| 264 | self.__favorites[type] = [] |
---|
| 265 | self.__favorites[type].append(itemData) |
---|
| 266 | self.__Save() |
---|
| 267 | |
---|
| 268 | def RemoveFavorite(self, itemData): |
---|
| 269 | type = itemData.GetType() |
---|
| 270 | if self.AlreadyFavorite(itemData): |
---|
| 271 | self.__favorites[type].remove(itemData) |
---|
| 272 | self.__Save() |
---|
| 273 | |
---|
| 274 | def AlreadyFavorite(self, itemData): |
---|
| 275 | type = itemData.GetType() |
---|
| 276 | if self.__favorites.has_key(type) and (itemData in self.__favorites[type]): |
---|
| 277 | return True |
---|
| 278 | else: |
---|
| 279 | return False |
---|
| 280 | |
---|
| 281 | def GetFavorites(self): |
---|
| 282 | return copy.deepcopy(self.__favorites) |
---|
| 283 | |
---|
| 284 | def __Save(self): |
---|
| 285 | savePreferences(self, FAVORITES_FILE) |
---|
| 286 | |
---|
| 287 | |
---|
| 288 | |
---|
| 289 | class FileLibraryPrefs: |
---|
| 290 | def __init__(self): |
---|
| 291 | self.__libs = {"local":"localhost"} #keyed by name |
---|
| 292 | self.__default = ("local", "localhost") |
---|
| 293 | |
---|
| 294 | def AddLibrary(self, ip, name): |
---|
| 295 | if self.__libs.has_key(name): |
---|
| 296 | return False |
---|
| 297 | else: |
---|
| 298 | self.__libs[name] = ip |
---|
| 299 | self.__Save() |
---|
| 300 | |
---|
| 301 | def RemoveLibrary(self, name): |
---|
| 302 | if self.__libs.has_key(name): |
---|
| 303 | del self.__libs[name] |
---|
| 304 | self.__Save() |
---|
| 305 | |
---|
| 306 | def GetLibraries(self): |
---|
| 307 | return copy.deepcopy(self.__libs) #prevent the user from modifying it directly |
---|
| 308 | |
---|
| 309 | def GetLibraryIP(self, name): |
---|
| 310 | if self.__libs.has_key(name): |
---|
| 311 | return self.__libs[name] |
---|
| 312 | else: |
---|
| 313 | return False |
---|
| 314 | |
---|
| 315 | def GetLibraryList(self): |
---|
| 316 | return self.__libs.keys() |
---|
| 317 | |
---|
| 318 | def SetDefault(self, name, ip): |
---|
| 319 | if not ip: |
---|
| 320 | return |
---|
| 321 | self.__default = (str(name), str(ip)) |
---|
| 322 | self.__Save() |
---|
| 323 | |
---|
| 324 | def GetDefault(self): |
---|
| 325 | return self.__default |
---|
| 326 | |
---|
| 327 | def __Save(self): |
---|
| 328 | savePreferences(self, FILE_LIB_FILE) |
---|
| 329 | |
---|
| 330 | |
---|
| 331 | |
---|
| 332 | |
---|
| 333 | |
---|