1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
|
# PrefUtils.tcl ---
#
# This file is part of The Coccinella application. It defines some
# utilities for keeping the user preferences.
#
# Copyright (c) 1999-2007 Mats Bengtsson
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# $Id: PrefUtils.tcl,v 1.19 2008-08-20 13:04:05 matben Exp $
#
################################################################################
#
# The priority arguments to the option command are normally specified
# symbolically using one of the following values:
#
# factoryDefault
# Level 20. Used for default values hard-coded into application.
#
# appDefault
# Level 40. Used for options specified in application-specific
# startup files.
#
# userDefault
# Level 60. Used for options describing customization, such as
# shortcuts etc.
#
# interactive
# Level 80. Used for options specified interactively after the
# application starts running.
#
# absolute
# Level 100. Cannot be overridden.
#
################################################################################
package provide PrefUtils 1.0
namespace eval ::PrefUtils:: {
variable priNameToNum
array set priNameToNum {
0 0 20 20 40 40 60 60 80 80 100 100
factoryDefault 20
appDefault 40
userDefault 60
interactive 80
absolute 100
}
}
# PrefUtils::Init --
#
# Reads the preference file into the internal option database.
# Use pre 0.94.2 prefs file as a fallback.
#
# Arguments:
#
# Results:
# updates the internal option database.
proc ::PrefUtils::Init {} {
global this prefs
set prefs(master,default) {}
set prefs(master,mustSave) {}
set prefs(master) {}
set prefsFilePath $this(userPrefsFilePath)
# Pre prefs file if any. Lower priority!
if {[file exists $this(prePrefsFile)]} {
catch {option readfile $this(prePrefsFile) startupFile} err
}
set appName [option get . appName {}]
set theAppName [option get . theAppName {}]
if {[file exists $prefsFilePath]} {
if {[catch {option readfile $prefsFilePath} err]} {
tk_messageBox -type ok -title [::msgcat::mc "Error"] -icon error \
-message "Error reading preference file: $prefsFilePath."
}
}
# Post prefs file if any.
if {[file exists $this(postPrefsFile)]} {
catch {option readfile $this(postPrefsFile)} err
}
}
# PrefUtils::Add --
#
# Set the user preferences from the preferences file if they are there,
# else take the hardcoded defaults.
# Take care of the priority for each variable.
#
# Arguments:
# prefsL a list of lists where each sublist defines an item in the
# following way:
# {varName resourceName defaultValue ?priority?}
#
# Results:
# none
proc ::PrefUtils::Add {prefsL} {
AddToMaster $prefsL
}
proc ::PrefUtils::AddMustSave {prefsL} {
AddToMaster $prefsL mustSave
}
proc ::PrefUtils::AddToMaster {prefsL {key ""}} {
global prefs
variable priNameToNum
foreach item $prefsL {
lassign $item varName resName defaultValue
# The default priority for hardcoded values are 20 (factoryDefault).
if {[llength $item] >= 4} {
set priority $priNameToNum([lindex $item 3])
} else {
set priority 20
}
if {$key eq ""} {
lappend prefs(master,default) \
[list $varName $resName $defaultValue $priority]
} else {
lappend prefs(master,$key) \
[list $varName $resName $defaultValue $priority]
}
# Override options that should be write only:
# for instance, version numbers.
if {$priority <= 60} {
set value [GetValue $varName $resName $defaultValue]
} else {
set value $defaultValue
}
# All names must be fully qualified. Therefore #0.
upvar #0 $varName var
# Treat arrays specially.
if {[string match "*_array" $resName]} {
array set var $value
} else {
set var $value
}
}
# Keep syncd.
set prefs(master) [concat $prefs(master,default) $prefs(master,mustSave)]
}
# PrefUtils::GetValue --
#
# Returns the preference variables value, either from the preference
# file, or if there is no value for it there, return the default
# value.
#
# Arguments:
# varName the actual name of the preference variable.
# resName the resource name of the preference variable.
# defValue the default value of the preference name.
#
# Results:
# a value for the preference with the given name.
proc ::PrefUtils::GetValue {varName resName defValue} {
set value [option get . $resName {}]
# If not there {} then take the itsHardCodedDefaultValue.
if {$value == {}} {
set value $defValue
}
return $value
}
# PrefUtils::SaveToFile --
#
# Saves the preferences to a file. Preferences must be stored in
# the master copy 'prefs(master)' in the corresponding list format.
# We do not save options if equal to default values.
# This has the benefit that we may change hardcoded defaults without
# changing resource names or other tricks. It is also useful if a
# default value depends on an dynamic OS thing, like if QuickTime
# is installed or not.
#
# Arguments:
#
# Results:
# preference file written.
proc ::PrefUtils::SaveToFile {} {
global prefs this
set userPrefsFile $this(userPrefsFilePath)
# Work on a temporary file and switch later.
set tmpFile $userPrefsFile.tmp
if {[catch {open $tmpFile w} fid]} {
::UI::MessageBox -icon error -title [mc "Error"] -type ok \
-message [mc "Cannot open file %s." $tmpFile]
return
}
fconfigure $fid -encoding utf-8
# Header information.
puts $fid "!\n! User preferences for the Coccinella application."
puts $fid "! It may be edited if you know what you are doing."
puts $fid "! The data written at: [clock format [clock seconds]]\n!"
# Only preferences indicated in the master copy are saved.
foreach item $prefs(master,default) {
PutsItem $item $fid 0
}
foreach item $prefs(master,mustSave) {
PutsItem $item $fid 1
}
close $fid
if {[catch {file rename -force $tmpFile $userPrefsFile} err]} {
set msg "Error renaming preferences file: $err"
::UI::MessageBox -type ok -message $msg -icon error
return
}
if {$this(platform) eq "windows"} {
file attributes $userPrefsFile -hidden 1
}
}
proc ::PrefUtils::PutsItem {item fid ignoreDefault} {
lassign $item varName resName defVal
# All names must be fully qualified. Therefore #0.
upvar #0 $varName var
# Treat arrays specially.
if {[string match "*_array" $resName]} {
array set tmpArr $defVal
if {$ignoreDefault || ![arraysequal $varName tmpArr]} {
puts $fid [format "%-24s\t%s" *${resName}: [array get var]]
}
} else {
if {$ignoreDefault || ![string equal $var $defVal]} {
# Protect for buggy code that is trying to use multiline values.
regsub -all "\n" $var {\\n} var
puts $fid [format "%-24s\t%s" *${resName}: $var]
}
}
}
# PrefUtils::ResetToFactoryDefaults --
#
# Resets the preferences in 'prefs(master)' to their hardcoded values.
#
# Arguments:
# maxPriority 0 20 40 60 80 100, or equivalent description.
# Pick only values with lower priority than maxPriority.
#
# Results:
# prefs values may change, and user interface stuff updated.
proc ::PrefUtils::ResetToFactoryDefaults {maxPriority} {
global prefs
variable priNameToNum
set maxPriorityNum $priNameToNum($maxPriority)
foreach item $prefs(master) {
set varName [lindex $item 0]
set resName [lindex $item 1]
set defaultValue [lindex $item 2]
set varPriority [lindex $item 3]
if {$varPriority < $maxPriorityNum} {
upvar #0 $varName var
# Treat arrays specially.
if {[string match "*_array" $resName]} {
array set var $defaultValue
} else {
set var $defaultValue
}
}
}
}
# PrefUtils::ResetToUserDefaults --
#
# Resets the applications state to correspond to the existing
# preference file.
#
# Arguments:
#
# Results:
# prefs values may change, and user interface stuff updated.
proc ::PrefUtils::ResetToUserDefaults {} {
global prefs
# Need to make a temporary storage in order not to duplicate items.
set defaultList $prefs(master,default)
set mustSaveList $prefs(master,mustSave)
# Read the user option database file once again.
Init
Add $defaultList
AddMustSave $mustSaveList
}
# PrefUtils::ParseCommandLineOptions --
#
# Process command line options for preferences.
proc ::PrefUtils::ParseCommandLineOptions {cargv} {
global prefs argvArr jprefs
if {![llength $cargv]} {
return
}
upvar ::Jabber::jstate jstate
# Skip anything that does not start with "-".
if {![regexp {(-[a-z].+$)} $cargv - optList]} {
return
}
foreach {key value} $optList {
switch -glob -- $key {
-debugLevel {
set name [string trimleft $key -]
uplevel #0 set $name $value
}
-prefs_* {
if {[regexp {^-prefs_(.+)$} $key match index]} {
set prefs($index) $value
}
}
-jprefs_* {
if {[regexp {^-jprefs_(.+)$} $key match index]} {
set jprefs($index) $value
}
}
-jstate_debug {
if {[regexp {^-jstate_(.+)$} $key match index]} {
set jstate($index) $value
}
}
}
}
}
#-------------------------------------------------------------------------------
|