creating one window with toolbar presentation
[findit] / src / pkgsearch.py
1 #!/usr/bin/python
2 # -*-coding: utf-8 -*-
3 # vim: sw=4 ts=4 expandtab ai
4
5 import gtk
6 import apt_pkg
7 from heapq import nlargest
8 from misc import size_hum_read
9
10 class Search_Pkg_Control(object):
11
12     def __init__(self, config, show_funct):
13         count = config.get('default_pkg_count')
14         self.show_func = show_funct
15         self.abstr = Search_Pkg_Abstraction()
16         self.present = Search_Pkg_Presentation(count, self.start_search)
17
18     def start_search(self, count, show_type):
19         pkglist = []
20         for psize, packg in nlargest(count, self.abstr.pkggetter()):
21             pkglist.append([packg, size_hum_read(psize), psize])
22         
23         if show_type is 'table_view':
24             from outtable import Out_Table_Control
25             out = Out_Table_Control()
26         #elif show_type is 'diag_view':
27         #    from outdiag import Out_Diag_Control
28         #    out = Out_Diag_Control(self.cfg)
29         out.show(pkglist, self.abstr.full())
30         self.show_func(None, out.get_ui())
31
32     def get_ui(self):
33         return self.present.get_ui()
34
35 class Search_Pkg_Abstraction(object):
36
37     def __init__(self):
38         apt_pkg.InitConfig()
39         apt_pkg.InitSystem()
40         self.cache = apt_pkg.GetCache()
41
42     def pkggetter(self):
43         self.fullsize = 0
44         for pkg in self.cache.Packages:
45             # pkg is from a list of packages, sorted by name.
46             if pkg.CurrentState == apt_pkg.CurStateInstalled:
47                 pkgsize = [version.InstalledSize for version in pkg.VersionList][0]
48                 self.fullsize = self.fullsize + pkgsize
49                 yield pkgsize, pkg.Name
50
51     def full(self):
52         return self.fullsize
53
54
55 class Search_Pkg_Presentation(object):
56
57     def __init__(self, count, st_func):
58         
59         label1 = gtk.Label('Files quantity')
60         # Окошко ввода количества файлов, мин значение=1 макс=65536 по умолчанию 10
61         adj = gtk.Adjustment(count, 1, 65536, 1, 5, 0)
62         self.file_cnt = gtk.SpinButton(adj, 0, 0)
63
64         hbox = gtk.HBox(False, 0)
65         hbox.pack_start(label1, False, False, 5)
66         hbox.pack_start(self.file_cnt, False, False, 0)
67         
68         label2 = gtk.Label('Choice show type')
69         show_butt1 = gtk.RadioButton(None, 'Show in Table view')
70         show_butt1.connect('toggled', self.show_type, 'table_view')
71         show_butt1.set_active(True)
72         show_butt2 = gtk.RadioButton(show_butt1, 'Show in Diagram view')
73         show_butt2.connect('toggled', self.show_type, 'table_view')
74         self.show = 'table_view'
75
76         butt_start = gtk.Button('START')
77         butt_start.connect('released', self.start_srch, st_func)
78         
79         vbox1 = gtk.VBox(False, 0)
80         vbox1.pack_start(label2, False, False, 5)
81         vbox1.pack_start(show_butt1, False, False, 5)
82         vbox1.pack_start(show_butt2, False, False, 5)
83
84         self.vbox = gtk.VBox(False, 0)
85         self.vbox.pack_start(hbox, False, False, 5)
86         self.vbox.pack_start(vbox1, False, False, 5)
87         self.vbox.pack_start(butt_start, False, False, 5)
88
89     def show_type(self, widget, data):
90         self.show = data
91
92     def get_data(self):
93         return int( self.file_cnt.get_value() )
94     
95     def get_show_type(self):
96         return self.show
97
98     def get_ui(self):
99         return self.vbox
100
101     def start_srch(self, widget, start_func):
102         start_func(self.get_data(), self.get_show_type())