File: DownloadGUI.py

package info (click to toggle)
aptoncd 0.1-1.2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 1,572 kB
  • ctags: 544
  • sloc: python: 3,125; xml: 1,216; makefile: 125; sh: 6
file content (274 lines) | stat: -rw-r--r-- 10,526 bytes parent folder | download
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
#!/usr/bin/python
# -*- coding: iso-8859-15 -*-
######################################################
##
#  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; version 2 only.
#
#  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.
##
######################################################
## 
#  Project: AptOnCd
#  File: DownloadGUI.py
#  Author: Alfredo Jr. <junix>
#  Creation: 18/11/2006
#  Changed: 08/12/2006 by Laudeci Oliveira <laudeci@gmail.com>
#  Purpose: Gui Class to control frmDownload Glade file
##
######################################################

import gtk
from gtk import glade
import string
import xmlfile
import os
import utils
import config
from messageBox import MessageBox
from configDownload import distros

class DownloadGUI:
    def __init__(self, gladefilename):
        self.conf = self.load_conf(config.XML_FILE)
        self.PackFiles = []
        self.gladefile = gladefilename
        self.gui = glade.XML(self.gladefile, "frmDownload")
        self.canceled = False
           
    def run(self):
        
        #Variable to control Download
        self.downloading = True
        # Main form Creation
        self.frmDownload = self.gui.get_widget("frmDownload")
        self.frmDownload.set_position(gtk.WIN_POS_CENTER)
        self.frmDownload.set_modal(True)
        self.frmDownload.connect("delete-event", self.on_btnCancel_clicked)

        #list options
        list_names = [x['name'] for x in distros]
        list_versions = distros[0]['versions']
        list_archs = distros[0]['archs']
        list_urls = distros[0]['urls']
        
        list_countries = self.get_urls(list_urls)

        list_methods = distros[0]['methods']
        list_media = distros[0]['media']
        list_sections = distros[0]['sections']
        
        # Form controls binding
        self.button_cancel = self.gui.get_widget("btnCancel")
        self.button_ok = self.gui.get_widget("btnDownload")
        self.combo_dist = self.gui.get_widget("cboDistribuition")
        self.combo_version = self.gui.get_widget("cboVersion")
        self.combo_arch = self.gui.get_widget("cboArchitecture")
        self.combo_mirror = self.gui.get_widget("cboMirror")
        self.combo_method = self.gui.get_widget("cboMethod")
        self.combo_media = self.gui.get_widget("cboMediaType")
        self.tvSections = self.gui.get_widget("tvSections")
        
        
        self.cboFileLocation = self.gui.get_widget('destinationDownload')
        
        # Events signals declarations
        self.button_cancel.connect("clicked", self.on_btnCancel_clicked)
        self.button_ok.connect("clicked", self.on_btnDownload_clicked)
        
        # fill combo with lists
        self.set_model_from_list(self.combo_dist, list_names)
        self.set_model_from_list(self.combo_version, list_versions)
        self.set_model_from_list(self.combo_arch, list_archs)
        self.set_model_from_list(self.combo_method, list_methods)
        self.set_model_from_list(self.combo_media, list_media)
        self.set_model_from_treelist(self.tvSections, list_sections)
        self.set_model_from_list(self.combo_mirror, list_countries,True)
         
        self.combo_dist.connect("changed", self.on_cboDistribuition_changed)
        
        self.set_saved_itens()
        
        result = self.frmDownload.run()

        #we are done with the dialog, destory it
        self.frmDownload.destroy()

        #return the result
        return result
    
    def get_urls(self,list):
        list_countries = []
        for n in list:
            start = n.find('(')+1
            end = n.find(')')
            country = n[start:end]
            list_countries.append( [country, n[:(start -1) ]])
        return list_countries
            
    def set_saved_itens(self):
        self.selectComboItem(self.combo_dist, self.conf[xmlfile.DISTRIBUTION])
        utils.updateUI()
        self.combo_dist.emit('changed')
        self.selectComboItem(self.combo_version, self.conf[xmlfile.VERSION])
        self.selectComboItem(self.combo_arch, self.conf[xmlfile.ARCHITECTURE])
        self.selectComboItem(self.combo_method, self.conf[xmlfile.METHOD])
        self.selectComboItem(self.combo_media, self.conf[xmlfile.MEDIA])
        self.checkSavedList(self.conf[xmlfile.SECTION])
        self.selectComboItem(self.combo_mirror, self.conf[xmlfile.HOST],True)
        self.cboFileLocation.set_current_folder_uri(self.conf[xmlfile.PATH])                              
    
    def checkSavedList(self,iList):
        for item in iList.split(';'):
            for iter in self.tvSections.get_model():
                if iter[1] == item:
                    iter[0] = True
                    
    def selectComboItem(self,combo, value,isUrl =False):
        model = combo.get_model()
        iter = model.get_iter_first()
        try: 
            if isUrl:
                while model.get_value(iter,1).replace(' ','') != value.replace(' ',''):
                    iter = model.iter_next(iter)
                combo.set_active_iter(iter)
            else:
                while model.get_value(iter,0) != value:
                    iter = model.iter_next(iter)
                combo.set_active_iter(iter)
        except:
            pass
        
        return
    
    def cancel(self):
        return self.canceled
    
    def getWindow(self):
         return self.frmDownload  
     
    def on_cboDistribuition_changed(self, widget):
        model = widget.get_model()
        iter = widget.get_active_iter()
        distro = model.get_value(iter, 0)
        distroIndex = 0
        if distro == 'ubuntu':
            distroIndex = 0
        elif distro == 'debian':
            distroIndex = 1
        list_versions = distros[distroIndex]['versions']
        list_archs = distros[distroIndex]['archs']
        list_urls = distros[distroIndex]['urls']
        list_sections = distros[distroIndex]['sections']
        
        list_countries = self.get_urls(list_urls)
            
        self.set_model_from_list(self.combo_version, list_versions)
        self.set_model_from_list(self.combo_arch, list_archs)
        self.set_model_from_treelist(self.tvSections, list_sections)
        self.set_model_from_list(self.combo_mirror, list_countries, True)
    
    def set_model_from_treelist (self, cb, items):
        if cb.get_model() == None:
            cb.set_model(gtk.ListStore(int,str))
            cell = gtk.CellRendererToggle()
            cell.set_property('activatable', True)
            column1 = gtk.TreeViewColumn(' ', cell)
            column1.add_attribute( cell, "active", 0)
            column1.set_resizable(True)
            cell.connect('toggled',self.on_Column_toggled, cb.get_model())
            self.tvSections.append_column(column1)
            cell1 = gtk.CellRendererText()
            column = gtk.TreeViewColumn(_('Sections'), cell1, markup=1)
            column.set_resizable(True)
            self.tvSections.append_column(column)
            
        model = cb.get_model()
        model.clear()
        
        for i in items:
            value = [False,i.encode("utf-8")]
            model.append(value)
    def on_Column_toggled(self, cell, path, model):
        model[path][config.C_CHECKED] = not model[path][config.C_CHECKED]
    def set_model_from_list (self, cb, items, isUrl = False):
        if cb.get_model() == None:
            if isUrl:
                cb.set_model(gtk.ListStore(str,str))
            else:
                cb.set_model(gtk.ListStore(str))
            cell = gtk.CellRendererText()
            cb.pack_start(cell)
            if isUrl:
                cell2 = gtk.CellRendererText()
                cb.pack_start(cell2)
                cb.add_attribute(cell, 'text',0)
                cb.add_attribute(cell2, 'text',1)
            else:
                cb.add_attribute(cell, 'text',0)
            
        model = cb.get_model()
        model.clear()
        
        for i in items:
            if isUrl:
                model.append(i)
            else:
                model.append([i])
                
    def on_btnDownload_clicked(self, widget):
        self.SaveXmlList()
    
    def on_btnCancel_clicked(self, *widget):
        pass
    
    def load_conf(self, file):
        XMLFile = xmlfile.XMLFile()
        self.node_text = XMLFile.parse(file)
        return XMLFile.load_conf(file)

    def geturl(self,value):
        distro = self.combo_dist.get_active_text() 
        if distro == 'ubuntu':
            distroIndex = 0
        elif distro == 'debian':
            distroIndex = 1
        list_versions = distro
        list_urls = distros[distroIndex]['urls']
        inti = 0
        for n in list_urls:
            if value in n:
                break
            inti+=1
        return list_urls[inti].split(' (')[0]
    
    def get_sections(self):
        list = [sect[1] for sect in self.tvSections.get_model() if sect[config.C_CHECKED]]
        result=''
        for s in list:
            result += s + ';'
        return result[:-1]
    
    def SaveXmlList(self):
        """Persists the server list and its folders to a xml file."""
        aFile = open(config.XML_FILE,"w")
        aFile.write('<?xml version="1.0" encoding="UTF-8" ?>\n')
        aFile.write('<download version="1.0">\n')
        aFile.write('    <settings>\n')
        aFile.write('        <host>'+ self.geturl(self.combo_mirror.get_active_text()) +'</host>\n')
        aFile.write('        <dist>'+ self.combo_dist.get_active_text() +'</dist>\n')
        aFile.write('        <method>'+ self.combo_method.get_active_text() +'</method>\n')
        aFile.write('        <version>'+ self.combo_version.get_active_text() +'</version>\n')
        aFile.write('        <section>%s</section>\n' % self.get_sections())
        aFile.write('        <arch>'+ self.combo_arch.get_active_text() +'</arch>\n')
        sPath = self.cboFileLocation.get_current_folder()
        aFile.write('        <path>'+ sPath +'</path>\n')
        aFile.write('        <media>'+ self.combo_media.get_active_text() +'</media>\n')
        aFile.write('    </settings>\n')
        aFile.write('</download>\n')
        aFile.close()
        return