summaryrefslogtreecommitdiff
path: root/src/gnome-software-properties.in
blob: 6c9b8d3bb80b7ebc697b5d68a7a03ab8f89041ab (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
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
#!/usr/bin/python2.4
# gnome-software-properties.in - edit /etc/apt/sources.list
#  
#  Copyright (c) 2004 Canonical
#                2004 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 pygtk
import sys
pygtk.require('2.0')
import gtk
import gtk.glade
import gconf
import gobject
import os
import gettext
import re
import string
import time
import tempfile
import subprocess
from optparse import OptionParser
import apt_pkg
        
sys.path.append("@prefix@/share/update-manager/python")

import aptsources
import dialog_add
import dialog_edit
import dialog_apt_key
from dialog_settings import dialog_settings
import shutil


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

class SoftwareConfigurator:
    
  def on_button_ok_clicked(self, widget, data):
    #self.save_periodic_config(periodicAptConfFile)
    
    location = "/etc/apt/sources.list"
    #backup first
    shutil.copy(location,location+".save")
    # write
    self.sourceslist.save(location)

    # write the source.list first, even if dirty=False, because
    # e.g. CD-ROM add does not set dirty variable even if it adds
    # something to the sources.list (but no reload needed)
    if self.dirty == False:
      gtk.main_quit()
      sys.exit(0)
    
    primary = "<span weight=\"bold\" size=\"larger\">" + _("Repositories "
              "changed") + "</span>"
    secondary = _("The repository information has changes. A backup copy of "
                  "your sources.list is stored in %s.save. "
                  "\n\n"
                  "You need to reload the package list from the servers "
                  "for your changes to take effect. Do you want to do this "
                  "now?") % location
    dialog = gtk.MessageDialog(self.main_window,gtk.DIALOG_MODAL,
                               gtk.MESSAGE_INFO,gtk.BUTTONS_YES_NO,"")
    dialog.set_markup(primary);
    dialog.format_secondary_text(secondary);
    #textview = gtk.TextView()
    #textview.set_editable(gtk.FALSE)
    #textbuffer = textview.get_buffer()
    #f = os.popen("/usr/bin/diff -u %s.save %s" % (location,location))
    #diff = f.read()
    #textbuffer.set_text(diff)
    #if f.close() != None:
    #  win = gtk.ScrolledWindow()
    #  win.set_policy(gtk.POLICY_AUTOMATIC,gtk.POLICY_AUTOMATIC)
    #  win.add(textview)
    #  win.set_size_request(400,300)
    #  win.show()
    #  dialog.vbox.pack_start(child = win, padding = 6)
    #  textview.show()
    res = dialog.run()
    dialog.destroy()
    if res == gtk.RESPONSE_YES:
      self.main_window.hide()
      # we are in "no-update" mode, this is used when the calling application
      # wants to deal with this itself
      if options.no_update:
        gtk.main_quit()
        sys.exit(1)
      child = subprocess.Popen(["/usr/sbin/synaptic", "--update-at-startup",
                                "--hide-main-window","--non-interactive"],
                               close_fds=True)
      # wait for the child to finish
      while child.poll() == None:
        time.sleep(0.05)
        while gtk.events_pending():
          gtk.main_iteration()
        gtk.main_quit()
        sys.exit(1)
    gtk.main_quit()
    sys.exit(0)

  def on_button_edit_clicked(self, widget, data):
    selection = self.sourceslist_view.get_selection()
    (model, iter) = selection.get_selected()
    source_entry = model.get_value(iter, LIST_ENTRY_OBJ)
    edit = dialog_edit.dialog_edit(self.main_window,self.sourceslist,
                                   source_entry)
    if edit.run() == gtk.RESPONSE_OK:
      self.reloadsources(self.sourceslist,self.matcher)
      self.dirty = True

  def on_sourceslist_selection_changed(self, selection, data):
    (model, iter) = selection.get_selected()
    if iter != None:
      self.button_del.set_sensitive(True)
      self.button_edit.set_sensitive(True)
    else:
      self.button_del.set_sensitive(False)
      self.button_edit.set_sensitive(False)

  #def on_sourceslist_row_activated(self, treeview, path, column, data):
  #  #print "on_row_activated()"
  #  model = treeview.get_model()
  #  source_entry = model.get_value(model.get_iter(path), LIST_ENTRY_OBJ)
  #  edit = dialog_edit.dialog_edit(self.main_window,self.sourceslist,
  #                                 source_entry)
  #  edit.run()
  #  self.reloadsources(self.sourceslist,self.matcher)

  def on_button_remove_clicked(self, widget, data):
    selection = self.sourceslist_view.get_selection()
    (model,a_iter) = selection.get_selected()
    if a_iter == None:
      return
    source = model.get_value(a_iter, LIST_ENTRY_OBJ)
    self.sourceslist.remove(source)
    self.reloadsources(self.sourceslist,self.matcher)
    self.dirty=True

  def on_button_add_clicked(self, widget, data):
    add = dialog_add.dialog_add(self.main_window,self.sourceslist)
    if add.run() == gtk.RESPONSE_OK:
      self.reloadsources(self.sourceslist,self.matcher)
      self.dirty=True

  def on_button_settings_clicked(self, widget, data):
    settings = dialog_settings(self.main_window, self.gladexml)
    settings.run()
    self.show_disabled = self.gconfclient.get_bool("/apps/gnome-software-properties/show_disabled")
    self.c_enabled.set_property("visible", self.show_disabled)
    self.reloadsources(self.sourceslist, self.matcher)

  def on_button_add_cdrom_clicked(self, widget, data):
    tmp = tempfile.NamedTemporaryFile()
    cmd = ["/usr/sbin/synaptic", "--hide-main-window",  "--non-interactive",
           "-o","Dir::Etc::sourcelist=%s" % tmp.name,"--ask-cdrom" ]
    self.main_window.set_sensitive(False)
    proc = subprocess.Popen(cmd)
    # wait for process to finish
    while proc.poll() == None:
      while gtk.events_pending():
        gtk.main_iteration()
      time.sleep(0.05)
    self.main_window.set_sensitive(True)
    # read tmp file with source name
    line = ""
    for x in open(tmp.name):
      line = x
    if line != "":
      self.sourceslist.list.append(aptsources.SourceEntry(line))
      self.reloadsources(self.sourceslist,self.matcher)
      #self.dirty=True # no need here

  def on_button_authentication_clicked(self, widget, data):
    auth = dialog_apt_key.dialog_apt_key(self.main_window)
    auth.run()

  def reloadsources(self, sourceslist, matcher):
    #self.meta = meta_data()
    self.sourcesstore.clear()
    for source in sourceslist.list:
      if source.invalid or (source.disabled and not self.show_disabled):
        continue
      (a_type,dist,comps) = 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)
      iter = self.sourcesstore.append([contents, not source.disabled, source])

  def show_help(self, widget, data):
    print "self.show_help() called"

  # toggled on/off a source in the listview
  def toggled_enabled(self, renderer, path_string):
    iter = self.sourcesstore.get_iter_from_string(path_string)
    source = self.sourcesstore.get_value(iter, LIST_ENTRY_OBJ)
    self.dirty=True
    if self.sourcesstore.get_value(iter, LIST_ENABLED):
      self.sourcesstore.set_value(iter, LIST_ENABLED, False)
      source.set_enabled(False)
    else:
      self.sourcesstore.set_value(iter, LIST_ENABLED, True)
      source.set_enabled(True)

  def __init__(self):
    self.gconfclient = gconf.client_get_default()

    if os.path.exists("../data/gnome-software-properties.glade"):
      self.gladexml = gtk.glade.XML("../data/gnome-software-properties.glade")
    else:
      self.gladexml = gtk.glade.XML("@prefix@/share/update-manager/gnome-software-properties.glade")

    # do we show disabled sources?
    self.show_disabled = self.gconfclient.get_bool("/apps/gnome-software-properties/show_disabled")
    self.main_window = self.gladexml.get_widget("SoftwareConfigurator")
    # button on the right
    self.gladexml.signal_connect("on_button_edit_clicked",
                                 self.on_button_edit_clicked, None)
    self.button_edit = self.gladexml.get_widget("button_edit")
    self.button_del = self.gladexml.get_widget("button_remove")

    # Gets the treeview and creates a store for it.
    self.sourceslist_view = self.gladexml.get_widget("sourceslist")
    self.sourcesstore = gtk.ListStore(str, bool,gobject.TYPE_PYOBJECT)
    self.sourceslist_view.set_model(self.sourcesstore)
    #self.gladexml.signal_connect("on_sourceslist_row_activated",
    #                             self.on_sourceslist_row_activated, None)
    self.sourceslist_view.get_selection().connect("changed", self.on_sourceslist_selection_changed, None)

    # was something modified
    self.dirty=False

    # Create columns and append them.
    cr = gtk.CellRendererToggle()
    cr.set_property("activatable", True)
    cr.set_property("xpad", 10)
    cr.set_property("ypad", 10)
    cr.connect("toggled", self.toggled_enabled)
    self.c_enabled = gtk.TreeViewColumn("Enabled", cr, active=LIST_ENABLED)
    self.sourceslist_view.append_column(self.c_enabled)
    self.c_enabled.set_property("visible", self.show_disabled)

    tr = gtk.CellRendererText()
    tr.set_property("xpad", 10)
    tr.set_property("ypad", 10)
    c0 = gtk.TreeViewColumn("Entry", tr, markup=LIST_MARKUP)
    self.sourceslist_view.append_column(c0)

    self.sourceslist = aptsources.SourcesList("@sysconfdir@/apt/sources.list")
    self.matcher = aptsources.SourceEntryMatcher()
    # Empty and fill the sources store.
    self.reloadsources(self.sourceslist,self.matcher)

    self.main_window = self.gladexml.get_widget("SoftwareConfigurator")
    self.main_window.connect("delete_event", lambda widget,ev: gtk.main_quit())

    okbutton = self.gladexml.get_widget("button_ok")
    okbutton.connect("clicked", self.on_button_ok_clicked, None)

    cancelbutton = self.gladexml.get_widget("button_cancel")
    cancelbutton.connect("clicked", lambda w,v: gtk.main_quit(), None)

    self.gladexml.signal_connect("on_button_add_clicked", self.on_button_add_clicked, None)
    self.gladexml.signal_connect("on_button_add_cdrom_clicked", self.on_button_add_cdrom_clicked, None)
    self.gladexml.signal_connect("on_button_remove_clicked", self.on_button_remove_clicked, None)
    self.gladexml.signal_connect("on_button_authentication_clicked", self.on_button_authentication_clicked, None)

    # settings
    self.gladexml.signal_connect("on_button_settings_clicked", self.on_button_settings_clicked, None)

    self.main_window.show()
    if options.toplevel != None:
      # don't show the add-cdrom button for now
      # FIXME: on the long run interface with apt-pkg/cdrom.h
      b = self.gladexml.get_widget("button_add_cdrom")
      b.hide()
      toplevel = gtk.gdk.window_foreign_new(int(options.toplevel))
      self.main_window.window.set_transient_for(toplevel)

      
    # init the config
    apt_pkg.InitConfig()

  def main(self):
    gtk.main()


if __name__ == "__main__":
  APP="update-manager"
  DIR="@prefix@/share/locale"
  gettext.bindtextdomain(APP, DIR)
  gettext.textdomain(APP)
  gtk.glade.bindtextdomain(APP, DIR)
  gtk.glade.textdomain(APP)
  _ = gettext.gettext
  #print "Software Configurator started..."

  # add option parser
  parser = OptionParser()
  parser.add_option("-n", "--no-update", action="store_true",
                    dest="no_update", default=False,
                    help="No update on repository change (usefull if called "\
                         "from a external program).")
  parser.add_option("-t", "--toplevel", 
                    action="store", type="string", dest="toplevel",
                    help="Set x-window-id of the toplevel parent for the "\
                         "dialog (usefull for embedding)")
  

  (options, args) = parser.parse_args()
  ui = SoftwareConfigurator()
  ui.main()
  #print "Software Configurator ended..."