File: AutoUpdate.tcl

package info (click to toggle)
coccinella 0.96.20-9
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 13,184 kB
  • sloc: tcl: 124,744; xml: 206; makefile: 66; sh: 62
file content (295 lines) | stat: -rw-r--r-- 8,870 bytes parent folder | download | duplicates (4)
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
#  AutoUpdate.tcl ---
#  
#      This file is part of The Coccinella application. It implements 
#      methods to query for new versions.
#
#  Copyright (c) 2003-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: AutoUpdate.tcl,v 1.30 2008-04-27 13:33:38 matben Exp $

package require tinydom
package require http 2.3

namespace eval ::AutoUpdate {
    
    component::define AutoUpdate \
      "Automatically checks for new version of this application."

    # Allow the update url to be set via the option database.
    set urlEN     "http://coccinella.sourceforge.net/updates/update_en.xml"
    set urlFormat "http://coccinella.sourceforge.net/updates/update_%s.xml"
#     set urlEN     "http://coccinella.sourceforge.net/updates/update_en_0.96.8.xml"
#     set urlFormat "http://coccinella.sourceforge.net/updates/update_test_%s.xml"

    set ::config(autoupdate,do)        1
    set ::config(autoupdate,url)       $urlEN
    set ::config(autoupdate,urlFormat) $urlFormat
    set ::config(autoupdate,interval) "1 day ago"

    variable newVersion 1.0
}

proc ::AutoUpdate::Init {} {

    ::Debug 2 "::AutoUpdate::Init"
        
    ::hooks::register prefsInitHook   ::AutoUpdate::InitPrefsHook
    ::hooks::register launchFinalHook ::AutoUpdate::LaunchHook

    # TRANSLATORS: checking for new versions of Coccinella; see Info menu
    set menuDef {command  mUpdateCheck  {[mc "&Update Check"]} {::AutoUpdate::Get -silent 0}  {}}
    ::JUI::RegisterMenuEntry info $menuDef

    component::register AutoUpdate
}

proc ::AutoUpdate::InitPrefsHook {} {
    global  prefs

    # Auto update mechanism: if lastVersion < run version => autoupdate
    set prefs(autoupdate,lastVersion) 0.0
    set prefs(autoupdate,lastTime)    [clock scan "1 year ago"]
    
    ::PrefUtils::Add [list  \
      [list prefs(autoupdate,lastVersion) prefs_autoupdate_lastVersion $prefs(autoupdate,lastVersion)] \
      [list prefs(autoupdate,lastTime)    prefs_autoupdate_lastTime    $prefs(autoupdate,lastTime)] \
      ]
}

proc ::AutoUpdate::LaunchHook {} {
    global  prefs this config
    
    if {$config(autoupdate,do)} {
	
	# The 'notLater' gives seconds for now minus interval.
	set notLater [clock scan $config(autoupdate,interval)]
	if {$prefs(autoupdate,lastTime) < $notLater} {
	    if {[package vcompare $this(vers,full) $prefs(autoupdate,lastVersion)] > 0} {
		after 10000 [namespace code Get]
	    }
	}
    }
}

# AutoUpdate::Get --
# 
#       Tries to get the update xml files, first the localized one, 
#       and if that fails, the english one.

proc ::AutoUpdate::Get {args} {
    global  this prefs config
    variable opts
    
    ::Debug 2 "::AutoUpdate::Get"
    
    array set opts {
	-silent 1
	-locale 1
    }
    array set opts $args
    eval {GetURL} [array get opts]
}

proc ::AutoUpdate::GetURL {args} {
    global  this prefs config
    variable opts

    array set opts $args
    if {$opts(-locale)} {
	set url [format $config(autoupdate,urlFormat) [::msgcat::mclocale]]
    } else {
	set url $config(autoupdate,url)
    }
    ::Debug 2 "\t url=$url"
    set tmopts [list -timeout $prefs(timeoutMillis)]
    if {[catch {eval {
	::httpex::get $url -command [namespace code [list Command $opts(-locale)]]
    } $tmopts} token]} {
	if {!$opts(-silent)} {
	    ::UI::MessageBox -title [mc "Error"] -icon error -type ok \
	      -message [mc "Cannot connect to server %s to download update details: %s" $url $token]
	}
    }
}

proc ::AutoUpdate::Command {locale token} {
    global  prefs this
    upvar #0 $token state
    variable opts
    variable newVersion

    # Investigate 'state' for any exceptions.
    set hstate [::httpex::state $token]
    set status [::httpex::status $token]
    set ncode  [::httpex::ncode $token]
    if {$hstate ne "final"} {
	return
    }

    set prefs(autoupdate,lastTime) [clock seconds]
    
    ::Debug 2 "::AutoUpdate::Command status=$status, ncode=$ncode, locale=$locale"
    
    if {($status eq "ok") && ($ncode eq "200")} {

	# Get and parse xml.
	set xml [::httpex::data $token]   
	set token [tinydom::parse $xml]
	set xmllist [tinydom::documentElement $token]
	set releaseElem [lindex [tinydom::children $xmllist] 0]
	set releaseAttr [tinydom::attrlist $releaseElem]
	array set releaseA $releaseAttr
	set message ""
	set changesL [list]
	
	foreach elem [tinydom::children $releaseElem] {
	    switch -- [tinydom::tagname $elem] {
		message {
		    set message [tinydom::chdata $elem]
		}
		changes {
		    foreach item [tinydom::children $elem] {
			lappend changesL [tinydom::chdata $item]
		    }
		}
	    }
	}
	set newVersion $releaseA(version)
	
	# Show dialog if newer version available.
	if {[package vcompare $this(vers,full) $releaseA(version)] == -1} {
	    Dialog $releaseAttr $message $changesL
	} elseif {!$opts(-silent)} {
	    ::UI::MessageBox -title [mc "Update Check"] -icon info -type ok \
	      -message [mc "You already have the latest version available (%s)." $this(vers,full)]
	}
	tinydom::cleanup $token
    } elseif {$locale} {

	# Try get the English catalog as a fallback.
	GetURL -locale 0
    }
    ::httpex::cleanup $token
}

proc ::AutoUpdate::Dialog {releaseAttr message changesL} {
    global  this prefs
    variable autocheck
    variable newVersion
    
    set w .aupdate
    if {[winfo exists $w]} {
	return
    }
    ::UI::Toplevel $w -macstyle documentProc -usemacmainmenu 1 \
      -closecommand [namespace current]::Destroy
    wm title $w [mc "New Version"]
    
    # Global frame.
    ttk::frame $w.frall
    pack $w.frall -fill both -expand 1
    
    set wbox $w.frall.f
    ttk::frame $wbox -padding [option get . dialogPadding {}]
    pack $wbox -fill both -expand 1

    # Text.
    set wtext $wbox.text
    text $wtext -width 60 -height 16 -wrap word \
      -borderwidth 1 -relief sunken -background white
    pack $wtext
    
    $wtext tag configure msgtag -lmargin1 10 -spacing1 4 -spacing3 4 \
      -font CociSmallBoldFont
    $wtext tag configure attrtag -lmargin1 10 -spacing1 2 -spacing3 2
    $wtext tag configure changestag -lmargin1 10 -spacing1 4 -spacing3 4 \
      -font CociSmallBoldFont
    $wtext tag configure itemtag -lmargin1 20 -lmargin2 30 \
      -spacing1 2 -spacing3 2
    $wtext configure -tabs {100 right 110 left}
        
    $wtext mark set insert end
    $wtext configure -state normal
    $wtext insert end $message msgtag
    $wtext insert end "\n"
    
    foreach {name value} $releaseAttr {
	$wtext insert end "\t[mc [string totitle $name]]:" attrtag
	
	switch -- $name {
	    url {
		$wtext insert end "\t"
		::Text::ParseURI $wtext $value
	    }
	    date {
		set date [clock format [clock scan $value]  \
		  -format "%A %d %B %Y"]
		$wtext insert end "\t$date" attrtag
	    }
	    default {
		$wtext insert end "\t$value" attrtag
	    }
	}
	$wtext insert end "\n"
    }
    $wtext insert end [mc "Changes since previous release"]:\n changestag
    foreach item $changesL {
	$wtext insert end "o $item\n" itemtag
    }    
    $wtext configure -state disabled
    
    set autocheck 1
    if {[package vcompare $this(vers,full) $prefs(autoupdate,lastVersion)] <= 0} {
    	set autocheck 0
    }
    ttk::checkbutton $wbox.ch -text [mc "Automatically check for updates"] \
      -variable [namespace current]::autocheck
    pack $wbox.ch -side top -anchor w -pady 8
    
    # Button part.
    set frbot $wbox.b
    ttk::frame $frbot
    ttk::button $frbot.btok -text [mc "Close"] \
      -command [list destroy $w]
    pack $frbot.btok -side right
    pack $frbot -side top -fill x
    
    # Configure text widget height to fit all.
    bind $wtext <Map> {
	set ylines [%W count -displaylines 1.0 end]
	set spacing [expr {2+2}]
	set font [%W cget -font]
	array set fontA [font metrics [%W cget -font]]
	set add [expr {int($ylines*$spacing/($fontA(-linespace) + 0.0))}]
	%W configure -height [expr {$ylines + $add + 1}]
    }
    
    wm resizable $w 0 0
}

proc ::AutoUpdate::Destroy {w} {
    global  prefs
    variable autocheck
    variable newVersion
    
    if {$autocheck} {
	set prefs(autoupdate,lastVersion) 0.0
    } else {
	set prefs(autoupdate,lastVersion) $newVersion
    }
}
    
#-------------------------------------------------------------------------------