summaryrefslogtreecommitdiff
path: root/SoftwareProperties/SoftwareProperties.py
blob: 1752f79fc5660163c9e2fe79521aceb787720fc4 (plain)
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
#!/usr/bin/env python
# gnome-software-properties.in - edit /etc/apt/sources.list
#
#  Copyright (c) 2004 Canonical
#                2004-2005 Michiel Sikkes
#
#  Author: Michiel Sikkes <michiel@eyesopened.nl>
#          Michael Vogt <mvo@debian.org>
#
#  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 2 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, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
#  USA

import sys
import gnome
import gconf
import apt_pkg
import gobject
import shutil
import gettext
from gettext import gettext as _
import os

#sys.path.append("@prefix/share/update-manager/python")

from UpdateManager.Common.SimpleGladeApp import SimpleGladeApp
import aptsources
import dialog_add
import dialog_edit
from dialog_apt_key import apt_key
from utils import *

(LIST_MARKUP, LIST_ENABLED, LIST_ENTRY_OBJ) = range(3)

CONF_MAP = {
  "autoupdate"   : "APT::Periodic::Update-Package-Lists",
  "autodownload" : "APT::Periodic::Download-Upgradeable-Packages",
  "autoclean"    : "APT::Periodic::AutocleanInterval",
  "max_size"     : "APT::Archives::MaxSize",
  "max_age"      : "APT::Archives::MaxAge"
}


class SoftwareProperties(SimpleGladeApp):

  def __init__(self, datadir, options):

    self.datadir = datadir
    SimpleGladeApp.__init__(self, datadir+"glade/SoftwareProperties.glade",
                            None, domain="update-manager")
    apt_pkg.InitConfig()
   
    self.modified = False
		  
    self.gnome_program = gnome.init("Software Properties", "0.41")
    self.gconfclient = gconf.client_get_default()

    self.window_main.hide() 
                                   
    # If externally called, reparent to external application.
    if (options.socket != None):
      plug = gtk.Plug(long(options.socket))
      self.vbox1.reparent(plug)
      plug.show_all()
    else:
      self.window_main.show()
      
    self.init_sourceslist()
    self.reload_sourceslist()
      
    update_days = apt_pkg.Config.FindI(CONF_MAP["autoupdate"])
    
    self.spinbutton_update_interval.set_value(update_days)
    
    if update_days >= 1:
      self.checkbutton_auto_update.set_active(True)
    else:
      self.checkbutton_auto_update.set_active(False)
      
    self.apt_key = apt_key()
    
    self.init_keyslist()
    self.reload_keyslist()
    
  def init_sourceslist(self):
    self.source_store = gtk.ListStore(str, bool, gobject.TYPE_PYOBJECT)
    self.treeview1.set_model(self.source_store)
    
    tr = gtk.CellRendererText()
    tr.set_property("xpad", 10)
    tr.set_property("ypad", 10)
    
    source_col = gtk.TreeViewColumn("Description", tr, markup=LIST_MARKUP)
    source_col.set_max_width(500)

    self.treeview1.append_column(source_col)
    
    self.sourceslist = aptsources.SourcesList()
    self.matcher = aptsources.SourceEntryMatcher()
    
  def init_keyslist(self):
    self.keys_store = gtk.ListStore(str)
    self.treeview2.set_model(self.keys_store)
    
    tr = gtk.CellRendererText()
    
    keys_col = gtk.TreeViewColumn("Key", tr, text=0)
    self.treeview2.append_column(keys_col)
    
  def reload_sourceslist(self):
    self.source_store.clear()
    for source in self.sourceslist.list:
      if source.invalid or source.disabled:
        continue
      (a_type, dist, comps) = self.matcher.match(source)
      
      contents = ""
      if source.comment != "":
        contents += "<i>%s</i>\n\n" % (source.comment)
      contents +="<big><b>%s </b></big> (%s) <small>\n%s</small>" % (dist,a_type, comps)

      self.source_store.append([contents, not source.disabled, source])
      
  def reload_keyslist(self):
    self.keys_store.clear()
    for key in self.apt_key.list():
      self.keys_store.append([key])
  
  def opt_autoupdate_toggled(self, widget):  
    if self.checkbutton_auto_update.get_active():
      if self.spinbutton_update_interval.get_value() == 0:
        self.spinbutton_update_interval.set_value(1)
        value = "1"
      else:
        value = str(self.spinbutton_update_interval.get_value()) 
    else:
      value = "0"
    
    apt_pkg.Config.Set(CONF_MAP["autoupdate"], str(value))
    
    # FIXME: Write config options, apt_pkg should be able to do this.
    self.write_config()
    
  def write_config(self):
    periodic = "/etc/apt/apt.conf.d/10periodic"
    
    content = []
    
    if os.path.isfile(periodic):
      content = open(periodic, "r").readlines()
      
    cnf = apt_pkg.Config.SubTree("APT::Periodic")
    
    f = open(periodic, "w+")
    for line in content:
      found = False
      for key in cnf.List():
        if line.find("APT::Periodic::%s" % (key)) >= 0:
          found = True
          break
        if not found:
          f.write(line)
          
    for i in cnf.List():
      f.write("APT::Periodic::%s \"%s\";\n" % (i, cnf.FindI(i)))
    f.close()    
    
  def save_sourceslist(self):
    location = "/etc/apt/sources.list"
    shutil.copy(location, location + ".save")
    self.sourceslist.save(location)
    
  def on_add_clicked(self, widget):
    dialog = dialog_add.dialog_add(self.window_main, self.sourceslist,
                                   self.datadir)
    if dialog.run() == gtk.RESPONSE_OK:
      self.reload_sourceslist()
    
    self.modified = True
      
  def on_edit_clicked(self, widget):
    sel = self.treeview1.get_selection()
    (model, iter) = sel.get_selected()
    source_entry = model.get_value(iter, LIST_ENTRY_OBJ)
    
    dialog = dialog_edit.dialog_edit(self.window_main, self.sourceslist,
                                     source_entry, self.datadir)
                                     
    if dialog.run() == gtk.RESPONSE_OK:
      self.reload_sourceslist()
      
    self.modified = True
      
  def on_remove_clicked(self, widget):
    sel = self.treeview1.get_selection()
    (model, iter) = sel.get_selected()
    
    if iter:
      source = model.get_value(iter, LIST_ENTRY_OBJ)
      self.sourceslist.remove(source)
      self.reload_sourceslist()  
    
    self.modified = True
    
  def add_key_clicked(self, widget):
    _ = gettext.gettext
    chooser = gtk.FileChooserDialog(title=_("Choose a key-file"),
                                    parent=self.window_main,
                                    buttons=(gtk.STOCK_CANCEL,gtk.RESPONSE_REJECT,
                                             gtk.STOCK_OK,gtk.RESPONSE_ACCEPT))
    res = chooser.run()
    chooser.hide()
    if res == gtk.RESPONSE_ACCEPT:
      if not self.apt_key.add(chooser.get_filename()):
        error(self.window_main,
              _("Error importing selected file"),
              _("The selected file may not be a GPG key file " \
                "or it might be corrupt."))
        self.reload_keyslist()
        
  def remove_key_clicked(self, widget):
    selection = self.treeview2.get_selection()
    (model,a_iter) = selection.get_selected()
    if a_iter == None:
        return
    key = model.get_value(a_iter,0)
    if not self.apt_key.rm(key[:8]):
      error(self.main,
        _("Error removing the key"),
        _("The key you selected could not be removed. "
          "Please report this as a bug."))
    self.reload_keyslist()
    
  def on_restore_clicked(self, widget):
    self.apt_key.update()
    self.reload_keyslist()
    
  def on_delete_event(self, widget, args):
    self.save_sourceslist()
    self.quit()
    
  def on_close_button(self, widget):
    self.save_sourceslist()
    self.quit()
    
  def on_help_button(self, widget):
    gnome.help_display_desktop(self.gnome_program, 
                               "update-manager", "update-manager", 
                               "setting-preferences")