~speijnik/update-manager/distribution-independent

« back to all changes in this revision

Viewing changes to UpdateManager/Frontend/Newt.py

  • Committer: Stephan Peijnik
  • Date: 2009-05-19 17:17:51 UTC
  • Revision ID: debian@sp.or.at-20090519171751-fyi8dvpm3f5cdz2b
Basic newt/snack-based text-frontend implemented, same functionality as current Gtk frontend.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# UpdateManager/Backend/__init__.py
 
2
#
 
3
#  Copyright (c) 2009 Canonical
 
4
#
 
5
#  Author: Stephan Peijnik <debian@sp.or.at>
 
6
#
 
7
#  This program is free software; you can redistribute it and/or
 
8
#  modify it under the terms of the GNU General Public License as
 
9
#  published by the Free Software Foundation; either version 2 of the
 
10
#  License, or (at your option) any later version.
 
11
#
 
12
#  This program is distributed in the hope that it will be useful,
 
13
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
#  GNU General Public License for more details.
 
16
#
 
17
#  You should have received a copy of the GNU General Public License
 
18
#  along with this program; if not, write to the Free Software
 
19
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
 
20
#  USA.
 
21
 
 
22
""" Newt/snack text interface """
 
23
 
 
24
import logging
 
25
import subprocess
 
26
import sys
 
27
import os
 
28
 
 
29
log = logging.getLogger('UpdateManager.Frontend.Newt')
 
30
 
 
31
from gettext import gettext as _
 
32
from snack import *
 
33
 
 
34
from UpdateManager.Backend import RELOAD_CACHE_STATUS
 
35
from UpdateManager.Frontend import FrontendBase
 
36
 
 
37
class NewtUI(object):
 
38
    def __init__(self, frontend, options):
 
39
        self._frontend = frontend
 
40
        self.screen = SnackScreen()
 
41
        self.options = options
 
42
 
 
43
    def create_ui(self):
 
44
        self.button_bar = ButtonBar(self.screen,
 
45
                                    ( (_("Cancel"), "cancel"),
 
46
                                      (_("Install"), "ok")),
 
47
                                    compact = True)
 
48
        self.textview_changes = Textbox(72, 8, "Changelog", True, True)
 
49
        self.checkbox_tree_updates = CheckboxTree(height=8, width=72, scroll=1)
 
50
        self.checkbox_tree_updates.setCallback(self.checkbox_changed)
 
51
        self.layout = GridForm(self.screen, "Updates", 1, 5)
 
52
        self.layout.add(self.checkbox_tree_updates, 0, 0)
 
53
        # empty line to make it look less crowded
 
54
        self.layout.add(Textbox(60, 1," ",False, False), 0, 1)
 
55
        self.layout.add(self.textview_changes, 0, 2)
 
56
        # empty line to make it look less crowded
 
57
        self.layout.add(Textbox(60, 1," ",False, False), 0, 3)
 
58
        self.layout.add(self.button_bar, 0, 4)
 
59
        # FIXME: better progress than the current suspend/resume screen thing
 
60
        self.screen.suspend()
 
61
 
 
62
    def cache_reload_callback(self, percent, operation):
 
63
        if percent == RELOAD_CACHE_STATUS.DONE:
 
64
            sys.stdout.write(_('Finished loading package cache.') + '\n')
 
65
        elif percent == RELOAD_CACHE_STATUS.BEGIN:
 
66
            sys.stdout.write(_('Loading package cache.') + '\n')
 
67
        else:
 
68
            sys.stdout.write('[%2d] %s\r' % (percent, operation))
 
69
 
 
70
    def main(self, application):
 
71
        self._app = application
 
72
 
 
73
        print _("Building Updates List")
 
74
 
 
75
        packages = application.get_available_updates()
 
76
        download_size = 0
 
77
        categories = {}
 
78
 
 
79
        for pkg in packages:
 
80
            catid = pkg.get_update_category()
 
81
            if not catid in categories.keys():
 
82
                categories[catid] = [pkg,]
 
83
            else:
 
84
                categories[catid].append(pkg)
 
85
 
 
86
        for (i, cat_id) in enumerate(categories.keys()):
 
87
            cat_name = application.get_update_category_name(cat_id)
 
88
            self.checkbox_tree_updates.append(cat_name, selected=True)
 
89
            for pkg in categories[cat_id]:
 
90
                self.checkbox_tree_updates.addItem(pkg.get_package_name(),
 
91
                                                   (i, snackArgs['append']),
 
92
                                                   pkg,
 
93
                                                   selected=True)
 
94
        self.screen.resume()
 
95
        res = self.layout.runOnce()
 
96
        self.screen.finish()
 
97
        button = self.button_bar.buttonPressed(res)
 
98
        if button == "ok":
 
99
            self.screen.suspend()
 
100
            log.debug('TODO: commit')
 
101
 
 
102
    def update_ui(self):
 
103
        self.layout.draw()
 
104
        self.screen.refresh()
 
105
 
 
106
    def checkbox_changed(self):
 
107
        pkg = self.checkbox_tree_updates.getCurrent()
 
108
        descr = ""
 
109
 
 
110
        if hasattr(pkg, "get_description"):
 
111
            if self.options.show_description:
 
112
                descr = pkg.get_description()
 
113
            else:
 
114
                descr = "TODO: changelog handling"
 
115
 
 
116
        self.textview_changes.setText(descr)
 
117
        self.update_ui()
 
118
 
 
119
class NewtFrontend(FrontendBase):
 
120
    """ Newt/Snack text frontend """
 
121
    def init_frontend(self, options):
 
122
        self._ui = NewtUI(self, options)
 
123
        self._ui.create_ui()
 
124
        self.cache_reload_callback = self._ui.cache_reload_callback
 
125
 
 
126
    def init_options(self, option_parser):
 
127
        option_parser.add_option("--show-description", "--show-description",
 
128
                                 action="store_true", dest="show_description",
 
129
                                 default=True,
 
130
                                 help=_("Show description of the package "
 
131
                                        "instead of the changelog"))
 
132
 
 
133
    def main(self, application):
 
134
        return self._ui.main(application)
 
135
 
 
136
    def handle_unprivileged_invocation(self):
 
137
        """ Newt unprivileged user handler. """
 
138
        cmd = ' '.join(sys.argv)
 
139
        if os.path.exists('/usr/bin/sudo'):
 
140
            log.debug('Spwaning new update-manager process via sudo.')
 
141
            return subprocess.call(['/usr/bin/sudo', cmd])
 
142
 
 
143
        # Fall back to su
 
144
        log.debug('Spawning new update-manager process via su.')
 
145
        return subprocess.call(['/bin/su', '-c', cmd])
 
146