source: projects/pygtk-rpminstall/trunk/src/rpminstall.py @ 5093

Revision 5093, 5.5 KB checked in by yasumichi, 13 years ago (diff)

fixed gettext related.

Line 
1#!/usr/bin/python
2#
3# main.py
4# Copyright (C) Yasumichi Akahoshi 2011 <yasumichi@vinelinux.org>
5#
6# pygtk-rpminstall is free software: you can redistribute it and/or modify it
7# under the terms of the GNU General Public License as published by the
8# Free Software Foundation, either version 3 of the License, or
9# (at your option) any later version.
10#
11# pygtk-rpminstall is distributed in the hope that it will be useful, but
12# WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14# See the GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License along
17# with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19
20from gi.repository import Gtk, GdkPixbuf, Gdk
21import os, sys, fnmatch
22import rpm
23import progress, dependdialog
24
25#Comment the first line and uncomment the second before installing
26#or making the tarball (alternatively, use project variables)
27UI_FILE = "ui/rpminstall.ui"
28RPM_ICON = "ui/icons/rpm.png"
29
30rpmtsCallback_fd = None
31
32#
33# Class for main window
34#
35class GUI:
36        def __init__(self):
37                # Build user interface
38                self.builder = Gtk.Builder()
39                self.builder.set_translation_domain('rpminstall')
40                self.builder.add_from_file(os.path.join (os.path.dirname(__file__), UI_FILE))
41                self.builder.connect_signals(self)
42                self.build_file_list_view()
43
44                # add packages from command line arguments
45                self.add_packages_from_argv ()
46
47                # Show window
48                self.window = self.builder.get_object('window')
49                self.window.show_all()
50
51        #
52        # Build file list view
53        #
54        def build_file_list_view(self):
55                # Build Column
56                column_file = Gtk.TreeViewColumn(_("RPM Package"))
57               
58                renderer_pixbuf = Gtk.CellRendererPixbuf()
59                column_file.pack_start(renderer_pixbuf,False)
60                column_file.add_attribute(renderer_pixbuf,"pixbuf",0)
61               
62                renderer_text = Gtk.CellRendererText()
63                column_file.pack_start(renderer_text, True)
64                column_file.add_attribute(renderer_text, "text", 1)
65               
66                file_list_view = self.builder.get_object('file_list_view')
67                file_list_view.append_column(column_file)
68
69                # Enable multi select
70                selection = file_list_view.get_selection()
71                selection.set_mode(Gtk.SelectionMode.MULTIPLE)
72
73                self.progress = 0
74
75        #
76        # add packages from command line arguments
77        #
78        def add_packages_from_argv(self):
79                file_liststore = self.builder.get_object('file_liststore')
80                pixbuf = GdkPixbuf.Pixbuf.new_from_file(os.path.join(os.path.dirname(__file__), RPM_ICON))
81
82                for arg in sys.argv:
83                        for pattern in self.get_installable_pattern_list():
84                                if fnmatch.fnmatchcase(arg, pattern):
85                                        file_liststore.append([pixbuf, arg])
86                                        break
87
88        #
89        # Callback when add button is clicked
90        #
91        def add_button_clicked(self, window):
92                dialog = Gtk.FileChooserDialog(_("Please choose a file"), window,
93                        Gtk.FileChooserAction.OPEN,
94                        (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
95                        Gtk.STOCK_OPEN, Gtk.ResponseType.OK))
96                dialog.set_select_multiple(True)
97
98                filter_text = Gtk.FileFilter()
99                filter_text.set_name(_("RPM Binary Package"))
100                for pattern in self.get_installable_pattern_list():
101                        filter_text.add_pattern(pattern)
102               
103                dialog.add_filter(filter_text)
104                if dialog.run() == Gtk.ResponseType.OK:
105                        file_liststore = self.builder.get_object('file_liststore')
106                        pixbuf = GdkPixbuf.Pixbuf.new_from_file(os.path.join(os.path.dirname(__file__), RPM_ICON))
107                        for fname in dialog.get_filenames():
108                                file_liststore.append([pixbuf, fname])
109
110                dialog.destroy()
111
112        #
113        # Callback when delete button is clicked
114        #
115        def delete_button_clicked(self, button):
116                file_list_view = self.builder.get_object('file_list_view')
117                file_liststore = self.builder.get_object('file_liststore')
118                selection = file_list_view.get_selection()
119                while True:
120                        (unused, paths) = selection.get_selected_rows()
121                        if paths == []:
122                                break
123                        file_liststore.remove( file_liststore.get_iter(paths[0]) )
124               
125        #
126        # Callback when cancel button is clicked
127        #
128        def cancel_button_clicked(self,button):
129                Gtk.main_quit()
130
131        #
132        # Callback when cancel button is clicked
133        #
134        def execute_button_clicked(self,widget):
135                count = 0
136                file_liststore = self.builder.get_object('file_liststore')
137               
138                ts = rpm.TransactionSet()
139
140                treeiter = file_liststore.get_iter_first()
141                while treeiter != None:
142                        filename = str(file_liststore[treeiter][1])
143                        fd = os.open(filename, os.O_RDONLY)
144                        h = None
145                        try:
146                                h = ts.hdrFromFdno(fd)
147                                ts.addInstall(h, filename, 'u')
148                                h = None
149                                os.close(fd)
150                                count = count + 1
151                        except rpm.error, e:
152                                print str(e)
153
154
155                        treeiter = file_liststore.iter_next(treeiter)
156
157                if count > 0:
158                        if self.possibility_of_installation(ts):
159                                ts.order()
160                                dialog = progress.ProgressDialog(self.window, ts)
161                                response = dialog.run()
162                else:
163                        dialog = Gtk.MessageDialog(self.window, 0, Gtk.MessageType.INFO,
164                                Gtk.ButtonsType.OK, _("No package is selected."))
165                        dialog.run()
166                        dialog.destroy()
167
168        #
169        # check dependency
170        #
171        def possibility_of_installation(self, ts):
172                unresolved_dependencies = ts.check()
173                if not unresolved_dependencies:
174                        return  True
175                else:
176                        dialog = dependdialog.DependDialog(unresolved_dependencies)
177                        dialog.run ()
178                        return False
179
180        #
181        # get installable pattern list
182        #
183        def get_installable_pattern_list(self):
184                pattern_list = [ '*.noarch.rpm' ]
185
186                arch = rpm.expandMacro('%{_arch}')
187                if arch == 'i386':
188                        pattern_list.append('*.i?86.rpm')
189                elif arch == 'x86_64':
190                        pattern_list.append('*.i?86.rpm')
191                        pattern_list.append('*.x86_64.rpm')
192                else:
193                        pattern_list.append('*.' + arch + '.rpm')
194               
195                return  pattern_list
196                       
197        #
198        # Callback when Gui is destroied
199        #
200        def destroy(window, self):
201                Gtk.main_quit()
202
Note: See TracBrowser for help on using the repository browser.