Repaired files.out_table Cli_Presentaion
[findit] / src / files / out_table.py
1 #!/usr/bin/env python
2 # -*-coding: utf-8 -*-
3 # vim: sw=4 ts=4 expandtab ai
4
5 from os.path import abspath
6
7 from misc import _
8
9 #==============================================================================
10
11 class Cli_Presentation(object):
12     def __init__(self, results):   ###
13         filelist, start_path = results
14         self.toplevel = self.print_results(filelist)
15
16     def print_results(self, filelist):
17         for bsize, path, size in filelist:
18             print '%10d' % bsize, path
19
20 #==============================================================================
21
22 class Gtk_Presentation(object):
23     def __init__(self, results):   ###
24         import gtk
25         global gtk  # for save_results and copy_results
26         import gobject
27
28         self.filelist, self.start_path = results
29
30         #====================
31         # Treeview
32         #====================
33
34         # Store results
35         self.liststore = gtk.ListStore(str, str, gobject.TYPE_INT64)
36         for bsize, path, size in self.filelist:
37             self.liststore.append([size,
38                                    path.replace(self.start_path,'', 1),
39                                    bsize])
40
41         treeview = gtk.TreeView(self.liststore)
42
43         # 'Size' column
44         size_col = gtk.TreeViewColumn(_('Size'))
45         cell1 = gtk.CellRendererText()
46         cell1.set_property('width', 90)
47         size_col.pack_start(cell1, True)
48         size_col.add_attribute(cell1, 'text', 0)
49         treeview.append_column(size_col)
50
51         # 'Path' column
52         path_col = gtk.TreeViewColumn(_('Path'))
53         cell2 = gtk.CellRendererText()
54         path_col.pack_start(cell2, True)
55         path_col.add_attribute(cell2, 'markup', 1)
56         treeview.append_column(path_col)
57
58         # Column sorting
59         treeview.set_search_column(1)
60         path_col.set_sort_column_id(1)
61         size_col.set_sort_column_id(2)
62
63         # Add treeview to scrolled window
64         swin = gtk.ScrolledWindow()
65         swin.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
66         swin.add(treeview)
67
68         #====================
69         # Toolbar
70         #====================
71
72         toolbar = gtk.Toolbar()
73         toolbar.set_property('icon-size', 'small-toolbar')
74
75         abs_paths_tbtn = gtk.ToggleToolButton()
76         abs_paths_tbtn.set_label(_('Absolute paths'))
77         abs_paths_tbtn.connect('clicked', self._show_abspaths)
78
79         bitesizes_tbtn = gtk.ToggleToolButton()
80         bitesizes_tbtn.set_label(_('Sizes in bytes'))
81         bitesizes_tbtn.connect('clicked', self._show_bitesizes)
82
83         saveresults_tbtn = gtk.ToolButton('gtk-save')
84         saveresults_tbtn.connect('clicked', self.save_results)
85
86         copyresults_tbtn = gtk.ToolButton('gtk-copy')
87         copyresults_tbtn.connect('clicked', self.copy_results)
88
89         toolbar.insert(abs_paths_tbtn, -1)
90         toolbar.insert(bitesizes_tbtn, -1)
91         toolbar.insert(saveresults_tbtn, -1)
92         toolbar.insert(copyresults_tbtn, -1)
93
94         #====================
95         # Others
96         #====================
97
98         vbox = gtk.VBox(False, 0)
99         vbox.pack_start(swin, True, True, 0)
100         vbox.pack_start(toolbar, False, False, 0)
101
102         self.toplevel = vbox
103
104     #=== Functions ============================================================
105
106     def _show_abspaths(self, btn):
107         # Toggled mean 'absolute paths'
108         if btn.get_active():
109             # Mark absolute part of path with color 
110             for i, (bsize, path, size) in enumerate(self.filelist):
111                 self.liststore[i][1] = '<span background="lawngreen">' + \
112                                        abspath(self.start_path) + '</span>' + \
113                                        path.replace(self.start_path,'', 1)
114         else:
115             for i, (bsize, path, size) in enumerate(self.filelist):
116                 self.liststore[i][1] = path.replace(self.start_path,'', 1)
117
118     def _show_bitesizes(self, btn):
119         if btn.get_active():
120             for i, (bsize, path, size) in enumerate(self.filelist):
121                 self.liststore[i][0] = bsize
122         else:
123             for i, (bsize, path, size) in enumerate(self.filelist):
124                 self.liststore[i][0] = size
125
126     def save_results(self, btn):
127         """Show 'Save to file' dialog."""
128         dialog = gtk.FileChooserDialog(title='Save to...',
129                                        action='save',
130                                        buttons=(gtk.STOCK_OK, gtk.RESPONSE_OK,
131                                                 gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL))
132         dialog.show_all()
133         response = dialog.run()
134         if response == gtk.RESPONSE_OK:
135             filepath = dialog.get_filename()
136             outfile = open(filepath, 'w')
137             # Saving results as "bite size\tabsolute path"
138             for bsize, path, size in self.filelist:
139                 outfile.write(`bsize` + '\t' + abspath(path) + '\n')
140             outfile.close()
141         dialog.destroy()
142
143     def copy_results(self, btn):
144         """Copy results to clipboard."""
145         # Form list
146         filelist = ''
147         for bsize, path, size in self.filelist:
148             filelist += `bsize` + '\t' + abspath(path) + '\n'
149
150         # Store in clipboard
151         cb = gtk.Clipboard()
152         cb.set_text(filelist)
153         cb.store()
154
155 #==============================================================================
156
157 class Hildon_Presentation(object):
158     def __init__(self, filelist):   ###
159         import gtk
160         import gobject
161         import hildon
162
163         # На таблетке не отображаються заголовки столбцов по умолчанию -
164         # след строка заставляет их отображаться принудительно
165         treeview.set_headers_visible(1)