~flimm/epidermis/icon-theme-bugfix

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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
#!/usr/bin/env python
# David D Lowe (c) 2008-2010
# licensed under GPLv3
"""loadrepo.py provides loading and displaying of the repository's information
in a EpidermisApp's window"""

from __future__ import division
import const
from const import DEBUG, MY_CACHE_HOME, MY_DATA_HOME, PIGMENT_TYPES
import pygtk
pygtk.require("2.0")
import gtk, gobject,os,sys,threading,subprocess, gconf, xml.parsers
import urlparse, urllib, urllib2
import managepigments
from glob import glob
import handy
import ep_exceptions
from gtkcrashhandler import gtkcrashhandler_thread
from gettext import gettext as _
import urllib2
import traceback

_acceptedEpidermisVersions = ("0.1", "0.4")

class _CancelException(Exception):
    """Exception that may be raised by the download_hook function"""
    def __str__(self):
        return "_CancelException"

def load_repo(application, forceReload=False):
    """Load epidermis repository, download and display it.
    
    Keyword arguments:
    application -- the EpidermisApp object
    forceReload -- whether to ignore application.repoLoaded or not, if forceReload 
                   is False, load_repo will will only activate if 
                   application.repoLoaded is False
    
    """    
    handy.require_main_thread()
    
    app = application
    if forceReload:
        app.repoLoaded = False

    if app.repoLoaded:
        # don't load repo if it's loaded already
        return
        
    # set up loading window
    wTree = gtk.glade.XML(app.wTreeFileName)
    fetchWin = wTree.get_widget("winOperations")
    fetchWin.set_destroy_with_parent(True)
    progressbar = wTree.get_widget("progressbarOperations")
    cancelButton = wTree.get_widget("butOperationsCancel")
    progressbar.set_fraction(0)    
    
    app.controls.tvPigments = {}
    for pt in PIGMENT_TYPES:
        app.controls.tvPigments[pt] = app.wTree.get_widget("treeviewRepo" + managepigments.get_pigment_type(pt).directoryName.capitalize())
        if app.controls.tvPigments[pt] is None:
            print >> sys.stderr, "Cannot find app.controls.tvPigments[" + pt + "]"
    
    fetchWin.show_all()
    
    app.currentDownloadRepoThread = _DownloadRepoThread(app.repoURL, app, wTree)
    app.currentDownloadRepoThread.start()

def clear_repo_cache(app):
    """Delete all cached information about the repository"""
    if not DEBUG:
        subprocess.call(["rm", "-r"] + glob(os.path.join(MY_CACHE_HOME, "*")))
    client = gconf.client_get_default()
    app.repoLastModified = ""
    client.set_string("/apps/epidermis/repo_last_modified","")
    app.repoETag = ""
    client.set_string("/apps/epidermis/repo_etag","")
    pass
            
class _DownloadRepoThread(threading.Thread):
    """Thread that downloads the file using urllib.urlretrieve"""
    
    def __init__(self, url, app, wTree):
        """Initialiser
        
        Keyword arguments:
        url -- URL of repo file to download
        app -- EpidermisApp
        wTree --  glade widget tree
        
        """
        self.url = url
        self.parent = app
        self.app = app
        self.wTree = wTree
        self.fetchWin = wTree.get_widget("winOperations")
        self.progressbar = wTree.get_widget("progressbarOperations")
        self.cancelButton = wTree.get_widget("butOperationsCancel")
        threading.Thread.__init__(self)
        self.setName("_DownloadRepoThread")
        self.repoURL = self.parent.repoURL
        
        # cancel the operation if user closes the window
        def fetchWin_delete_callback():
            self._cancel_callback(self.cancelButton)
            return True
        self.handleridDelete = self.fetchWin.connect("delete-event",
            lambda widget, event:fetchWin_delete_callback())
        
        self.app.cancel = False
        self.handleridCancel = self.cancelButton.connect("clicked",self._cancel_callback)
    
    @gtkcrashhandler_thread
    def run(self):
        if self.parent.checkOnlineRepo:
            self.check_repo_online()
        else:
            gtk.gdk.threads_enter()
            self.progressbar.set_text(_("Loading repo"))
            gtk.gdk.threads_leave()
            self.load_cached_repo()
            gobject.idle_add(lambda:self.fetchWin.hide())
        
    def check_repo_online(self):
        """Check to see if the program can connect to the repo."""
        handy.require_non_main_thread()
        gtk.gdk.threads_enter()
        self.progressbar.set_text(_("Checking connection"))
        gtk.gdk.threads_leave()
        
        # Connect to the repo xml file
        req = urllib2.Request(self.repoURL)
        try:
            repoXml = urllib2.urlopen(req)
        except urllib2.HTTPError, ee:
            print >> sys.stderr, "repoUrl: \"%s\"\n%s" % (self.parent.repoURL, str(ee))
            self.display_download_critical_error(_("Connection cannot be made to %s") % self.repoURL, \
                friendlySecondaryText=_("Please make sure that the repo's URL is correct"))
            try:
                repoXml.close()
            except:
                pass
            return False
        except urllib2.URLError, ee:
            print >> sys.stderr, str(ee)
            self.display_download_critical_error(_("Connection cannot be made to %s") % self.repoURL, \
                friendlySecondaryText=_("Please verify your Internet connection"))
            try:
                repoXml.close()
            except:
                pass
            return False
        
        # open downloaded XML file
        from xml.dom import minidom
        thexml = minidom.parse(repoXml)
        root = thexml.getElementsByTagName("ghnsdownload")[0]
        
        # check compatibility with repo's version
        validVer = False
        for ver in root.getAttribute("epidermisVersion").split(";"):
            if ver in _acceptedEpidermisVersions:
                validVer = True
                break
        if not validVer:
            self.display_download_critical_error(_("The repo's version (%s) is not supported by this version of Epidermis. Epidermis may be out of date") % root.getAttribute("epidermisVersion"))
            print >> sys.stderr, str(ep_exceptions.PigmentVersionNotAccepted(root.getAttribute("epidermisVersion"), _acceptedEpidermisVersions))
            return False
        
        # get location of preview_summary tarball
        previewSummaryTgzXml = None
        for item in thexml.getElementsByTagName("stuff"):
            if item.getAttribute("category") == "epidermis/repo_preview_summary":
                previewSummaryTgzXml = handy.get_first_text(item.getElementsByTagName("payload")[0])
        if previewSummaryTgzXml is None:
            print >> sys.stderr, "self.parent.repoURL: \"%s\"" % self.parent.repoURL
            raise(Exception("cannot find stuff element in XML for repo_preview_summary"))
        
        # finished reading repo's xml file
        repoXml.close()
        
        # get the absolute URL of preview summary tarball
        server = urlparse.urlparse(self.parent.repoURL) # eg: server[1] = localhost
        if urlparse.urlparse(previewSummaryTgzXml)[1] == "":
            serverPath = urlparse.urlparse(self.parent.repoURL)[2][: \
                (-1*len(self.parent.repoURL.split("/")[-1]))] # eg: serverPath = /ghns
            previewSummaryTgz = urlparse.urljoin(server[0] + "://", server[1])
            previewSummaryTgz = urlparse.urljoin(previewSummaryTgz.replace("///", "//"), serverPath)
            previewSummaryTgz = urlparse.urljoin(previewSummaryTgz, previewSummaryTgzXml)
        else: # absolute URL
            previewSummaryTgz = previewSummaryTgzXml
        
        # prepare to download preview summary tarball:
        # set If-Modified-Since and If-None-Match headers to check to see
        # if we can use our cached previews
        headers = {}
        if self.parent.repoLastModified != "":
            headers["If-Modified-Since"] = self.parent.repoLastModified
        if self.parent.repoETag != "":
            headers["If-None-Match"] = self.parent.repoETag
        
        
        req = urllib2.Request(previewSummaryTgz, headers=headers)
        usedCachedPreviews = False
        try:
            previewSummaryFile = urllib2.urlopen(req)
        except urllib2.HTTPError, ee:
            if ee.code == 304:
                # we can used cached preview images
                usedCachedPreviews = True
            else:
                print >> sys.stderr, "repoUrl: \"%s\"\n%s" % (self.parent.repoURL, str(ee))
                self.display_download_critical_error(_("Connection cannot be made to %s") % previewSummaryTgz)
                repoXml.close()
                return False
        except urllib2.URLError, ee:
            print >> sys.stderr, str(ee)
            self.display_download_critical_error(_("Connection cannot be made to %s") % previewSummaryTgz, \
                friendlySecondaryText=_("Please verify your Internet connection"))
            repoXml.close()
            raise ee
        else:
            # get expected size of download
            firstSize = int(previewSummaryFile.info().getheader("Content-Length", "-1"))
        
        if usedCachedPreviews or previewSummaryFile.info().status == 304:
            self.load_cached_repo()
            gobject.idle_add(lambda:self.fetchWin.hide())
        else:
            gtk.gdk.threads_enter()
            self.progressbar.set_text(_("Downloading"))
            gtk.gdk.threads_leave()
            previewSummaryFile.close()
            import socket
            try:
                filename, headers = urllib.urlretrieve(previewSummaryTgz, reporthook=self._download_hook)
            except _CancelException, e:
                gobject.idle_add(lambda:self.parent.switch_to_toggled(self.parent.controls.tgInstalled))
            except IOError, err:
                self.display_download_critical_error(_("Error connecting to %(ps)s\n %(err)s") % {'ps':previewSummaryTgz,'err':err})
            except socket.timeout, err:
                self.display_download_critical_error(_("Error connecting to %(ps)s, timed out\n %(err)s") % {'ps':previewSummaryTgz, 'err':err})
            else:
                client = gconf.client_get_default()
                if headers.has_key("Last-Modified"):
                    client.set_string("/apps/epidermis/repo_last_modified", headers["Last-Modified"])
                else:
                    client.set_string("/apps/epidermis/repo_last_modified", "")
                if headers.has_key("ETag"):
                    client.set_string("/apps/epidermis/repo_etag", headers["ETag"])
                else:
                    client.set_string("/apps/epidermis/repo_etag", "")
                self.prepare_downloaded_repo(filename)
                gobject.idle_add(lambda:self.fetchWin.hide())
    
    def display_download_critical_error(self, errorText, friendlyErrorText=None, friendlySecondaryText=""):
        # TODO: make more friendly error displayer
        handy.require_non_main_thread()
        if friendlyErrorText is None:
            friendlyErrorText = errorText
        print >> sys.stderr, errorText
        def dialog_response(dialog, response_id):
            dialog.destroy()
            gobject.idle_add(lambda:self.parent.switch_to_toggled(self.parent.controls.tgInstalled))
        gobject.idle_add(lambda:self.fetchWin.hide())
        downloadDialog = gtk.MessageDialog(None, gtk.DIALOG_MODAL, gtk.MESSAGE_ERROR, gtk.BUTTONS_OK, friendlyErrorText)
        downloadDialog.format_secondary_text(friendlySecondaryText)
        downloadDialog.connect("response", dialog_response)
        downloadDialog.set_skip_taskbar_hint(False)
        downloadDialog.set_title(_("Error"))
        gtk.gdk.threads_enter()
        downloadDialog.run()
        gtk.gdk.threads_leave()
        downloadDialog.destroy()
        
    def _cancel_callback(self, widget):
        """Cancel callback for the cancel button on the download window"""
        handy.require_non_main_thread()
        self.app.cancel = True
        gobject.idle_add(lambda:self.fetchWin.hide())
        gobject.idle_add(lambda:self.app.switch_to_toggled(self.app.controls.tgInstalled))
        gobject.idle_add(lambda:self.app.controls.tgInstalled.set_active(True))
        gobject.idle_add(lambda:self.fetchWin.disconnect(self.handleridDelete))
        gobject.idle_add(lambda:self.cancelButton.disconnect(self.handleridCancel))
        
    def _download_hook(self, blocksDownloaded, blockSize, size):
        """Download_hook for the download thread that displays progress
        in the progressbar
        
        """
        handy.require_non_main_thread()
        downloaded = blocksDownloaded * blockSize
        frac = blocksDownloaded * blockSize/ size
        if frac > 1:
            frac = 1
            downloaded = size
        if frac < 0: frac = 0
        gtk.gdk.threads_enter()
        self.progressbar.set_fraction(frac)
        if size >= 0:
            self.progressbar.set_text(handy.bytes_in_str(downloaded) + "/" + handy.bytes_in_str(size))
        else:
            self.progressbar.set_text(handy.bytes_in_str(downloaded) + "/??")
        gtk.gdk.threads_leave()
        if self.app.cancel:
            raise(_CancelException())

    def load_cached_repo(self):
        """Load repo from cache"""
        
        # read the downloaded preview_summary.xml
        handy.require_non_main_thread()
        filename = os.path.join(MY_CACHE_HOME, "preview_summary.xml")
        if not os.path.exists(filename):
            gobject.idle_add(lambda:self.app.show_error(_("No cached repo information, try again"), terminalText=_("No cached preview_summary.xml"),parent=self.fetchWin))
            clear_repo_cache(self.app)
        else:
            # preview_summary.xml exists
            # try to read and display the information
            try:
                from xml.dom import minidom
                thexml = minidom.parse(filename)
            except xml.parsers.expat.ExpatError:
                self.app.show_error(_("Invalid repo information"), terminalText=_("Invalid xml: %s") % filename, parent=self.fetchWin)
                clear_repo_cache(self.app)
            else:
                # valid XML in preview_summary.xml
                self.app.pigmentsData = {}
                self.app.pigmentsToInstall = {}
                for pt in PIGMENT_TYPES[1:] + ["skin"]:
                    self.app.pigmentsToInstall[pt] = {}
                    self.app.pigmentsData[pt] = self.app.PigmentTypeData(pt, self.app.installed_toggled, self.app.controls.tvPigments[pt])#self.app.controls.tvPigments[pt])
                    items = thexml.getElementsByTagName(managepigments.get_pigment_type(pt).directoryName)[0]
                    for item in items.getElementsByTagName("item"):
                        try:
                            newPigmentInstalled = False
                            newPigmentCodename = ""
                            newPigmentCodename = handy.get_first_text(item.getElementsByTagName("codename")[0])
                            newPigmentInstalled = self.app.is_pigment_installed(pt, newPigmentCodename)
                        except IndexError, ee:
                            self.app.show_error(_("Missing element in item %(id)s in %(filename)s") % {"id":item,
                                "filename":filename})
                        except ep_exceptions.NoFirstTextError, ee:
                            self.app.show_error(_("Missing text in xml of item %(id)s in %(filename)s") % {"id":item,
                                "filename":filename})
                        newPigment = None
                        if newPigmentInstalled:
                            for myPigm in self.app.installedPigments[pt]:
                                if myPigm.codeName == newPigmentCodename:
                                    newPigment = myPigm
                                    break
                            if newPigment is None:
                                print >> sys.stderr, "Cannot find installed pigment: %(newPigmentCodename)s type %(type)s" % \
                                    {'newPigmentCodename':newPigmentCodename, 'type':pt}
                                newPigmentInstalled = False
                            else:
                                self.app.pigmentsData[pt].append_to_listStore(newPigment, installed=True)
                        if not newPigmentInstalled: # pigment is not installed or is badly installed
                            newPigment = managepigments.create_pigment(pt)
                            itemId = item.getAttribute("id")
                            try:
                                newPigment.set_human_name(handy.get_first_text(item.getElementsByTagName("name")[0]))
                                newPigment.codeName = newPigmentCodename
                                newPigment.set_description(handy.get_first_text(item.getElementsByTagName("description")[0]))
                                newPigment.author = handy.get_first_text(item.getElementsByTagName("author")[0])
                            except IndexError, ee:
                                self.app.show_error(_("Missing element in item %(id)s in %(filename)s") % {"id":itemId,
                                    "filename":filename})
                            except ep_exceptions.NoFirstTextError, ee:
                                self.app.show_error(_("Missing text in xml of item %(id)s in %(filename)s") % {"id":itemId,
                                    "filename":filename})
                            if len(item.getElementsByTagName("preview")) > 0:
                                try:
                                    newPigment.set_preview_file(os.path.join(MY_CACHE_HOME, handy.get_first_text(item.getElementsByTagName("preview")[0])))
                                except ep_exceptions.NoFirstTextError:
                                    print >> sys.stderr, "no first text for preview in %s" % filename
                            if pt != "skin":
                                self.app.pigmentsData[pt].append_to_listStore(newPigment, newPigmentInstalled)
                            else:
                                for ptd in PIGMENT_TYPES[1:]:
                                    if len(item.getElementsByTagName(ptd + "Dependancy")) > 0:
                                        try:
                                            linkedCodename = handy.get_first_text(item.getElementsByTagName(ptd + "Dependancy")[0])
                                        except ep_exceptions.NoFirstTextError:
                                            print >> sys.stderr, "no first text for %sDependancy in %s in %s" % (ptd, newPigment.codeName, filename)
                                        else:
                                            foundPigment = None
                                            for pt2 in PIGMENT_TYPES:
                                                for pigm in self.app.pigmentsData[pt2].pigments:
                                                    if pigm.type.codeName == ptd and pigm.codeName == linkedCodename:
                                                        foundPigment = pigm
                                                        break
                                            if foundPigment is None:
                                                print >> sys.stderr, "looking for pigment " + linkedCodename + " of type " + ptd
                                                raise(Exception("can't find pigment"))
                                            newPigment.set_linked_pigment(foundPigment)
                                self.app.pigmentsData[pt].append_to_listStore(newPigment, newPigmentInstalled)

                self.app.originalPigmentsListStore = {}
                for pp in self.app.pigmentsData:
                    self.app.originalPigmentsListStore[self.app.pigmentsData[pp].get_pigmentType()] = self.app.pigmentsData[pp].blank_listStore()
                    ls = self.app.originalPigmentsListStore[self.app.pigmentsData[pp].get_pigmentType()]
                    coun = 0
                    while (coun < self.app.pigmentsData[pp].get_listStore().iter_n_children(None)):
                        self.app.pigmentsData[pp].append_to_listStore(self.app.pigmentsData[pp].get_pigment(coun), listStore=ls, \
                            installed=self.app.pigmentsData[pp].get_installed(coun))    
                        coun += 1
                self.app.repoLoaded = True
                gobject.idle_add(lambda:self.app.controls.tgFindMore.set_active(True))

    def prepare_downloaded_repo(self,filename):
        """Extract the downloaded repo archive in the appropriate places
        in the cache and then run load_cached_repo()
        
        """
        sFilename = filename.split("/")[-1:][0]
        subprocess.call(["rm", os.path.join(MY_CACHE_HOME, "preview_summary.xml")])
        subprocess.call(["mkdir", "-p", os.path.join(MY_CACHE_HOME)])
        subprocess.call(["mv", filename, os.path.join(MY_CACHE_HOME, sFilename)])
        subprocess.call(["gunzip", sFilename], cwd=MY_CACHE_HOME)
        subprocess.call(["tar", "-xf", sFilename[0:-3]], cwd=MY_CACHE_HOME, stdout=os.tmpfile())
        subprocess.call(["rm", os.path.join(MY_CACHE_HOME, sFilename[0:-3])])
        if not os.path.exists(os.path.join(MY_CACHE_HOME, "preview_summary.xml")):
            self.app.show_error(_("Invalid archive downloaded, does not contain preview_summary.xml"))
            clear_repo_cache(self.app)
        else:
            # preview_summary.xml exists
            try:
                from xml.dom import minidom
                thexml = minidom.parse(os.path.join(MY_CACHE_HOME, "preview_summary.xml"))
            except xml.parsers.expat.ExpatError:
                self.app.show_error(terminalText="Invalid xml: " + os.path.join(MY_CACHE_HOME, "preview_summary.xml"), \
                    text="Invalid repo information", parent=self.fetchWin)
                clear_repo_cache(self.app)
            else:
                self.load_cached_repo()
                self.app.checkOnlineRepo = False