fb8657874ee165f85c8c4010c6b8c2cd43b42170
[findit] / src / main.py
1 #!/usr/bin/env python
2 # -*-coding: utf-8 -*-
3 # vim: sw=4 ts=4 expandtab ai
4 # main.py --search files -o outtable -p ". 3"
5
6 import sys
7
8 from config import Config
9
10 __progname__ = 'FindIT'
11 __version__ = '0.2.0'
12
13 #==============================================================================
14
15 class Control(object):
16     def __init__(self):
17         config = Config ###()
18
19         self.abstrac = Abstraction()
20
21         if(len(sys.argv) > 1):
22             Cli_Presentation(self.abstrac)  ###
23         else:
24             Gtk_Presentation(config, self.abstrac)  ###
25
26 #==============================================================================
27
28 class Abstraction(object):
29     def __init__(self):
30         self.progname = __progname__
31         self.version = __version__
32         self.authors = [    'Alex Taker\n   * Email: alteker@gmail.com\n',
33                         'Eugene Gagarin\n   * Email: mosfet07@ya.ru\n',
34                         'Alexandr Popov\n   * Email: popov2al@gmail.com' ]
35         self.comments = 'Tool for find some information on computer.'
36         self.license = \
37 'This program is free software; you can redistribute it and/or\nmodify it \
38 under the terms of the GNU General Public License\nas published by the Free \
39 Software Foundation; either version 3\nof the License, or (at your option) \
40 any later version.'
41
42         import gettext
43         try:
44             # Meaning ru/LC_MESSAGES/program.mo in current dir (sys.path[0])
45             # For /usr/share/locale uncomment next string:
46             #LANGRU = gettext.translation('findit')
47             LANGRU = gettext.translation('findit', sys.path[0], languages=['ru'])
48             LANGRU.install()
49         except IOError:
50             # Comment out before use pygettext
51             def _(text): 
52                 return text
53
54 #==============================================================================
55
56 class Cli_Presentation(object):
57
58     def __init__(self, abstrac):
59         from optparse import OptionParser
60         import sys
61
62         self.abstrac = abstrac
63
64         parser = OptionParser(version=__progname__ + ' ' + __version__)
65         parser.add_option('--search', '-s', dest='search', type='string')
66         parser.add_option('--output', '-o', dest='output', type='string')
67         parser.add_option('--params', '-p', dest='params', type='string')
68         parser.add_option('--about',   action='callback', callback=self._about)
69         parser.add_option('--license', action='callback', callback=self._license)
70         (options, args) = parser.parse_args()
71
72         config = {}
73         config['search'] = options.search
74         config['outtype'] = options.output
75         config['ignore_dirs'] = ['/dev', '/proc', '/sys', '/mnt']
76         config['start_path'], config['count'] = options.params.split()
77
78         self.show_search_toplevel(config)
79
80     def _about(self, *a):
81         print self.abstrac.comments
82         sys.exit()
83
84     def _license(self, *a):
85         print self.abstrac.license
86         sys.exit()
87
88     def show_search_toplevel(self, config):
89         search_module = __import__(config['search'] + '.search')
90         search_toplevel = search_module.search.Control('cli', config).run()
91
92 #==============================================================================
93
94 class Gtk_Presentation(object):
95     """Main window class."""
96
97     def __init__(self, config, abstrac):
98         import gtk
99
100         self.config = config
101
102         def _create_menu():
103             """Create main menu."""
104             menubar = gtk.MenuBar()
105             fileitem = gtk.MenuItem(_('_File'))
106             viewitem = gtk.MenuItem(_('_View'))
107             helpitem = gtk.MenuItem(_('_Help'))
108             helpitem.connect('activate', about_dialog)
109             menubar.add(fileitem)
110             menubar.add(viewitem)
111             menubar.add(helpitem)
112             return menubar
113
114         def _create_toolbar():
115             """Create toolbar."""
116             toolbar = gtk.Toolbar()
117             filesearch_tbtn = gtk.RadioToolButton(None)
118             debsearch_tbtn = gtk.RadioToolButton(filesearch_tbtn)
119
120             filesearch_tbtn.set_name('files')
121             debsearch_tbtn.set_name('debs')
122
123             filesearch_tbtn.set_label(_('Files search'))
124             debsearch_tbtn.set_label(_('Debs search'))
125
126             filesearch_tbtn.connect('clicked', self.show_search_toplevel, 'files')
127             debsearch_tbtn.connect('clicked', self.show_search_toplevel, 'debs')
128
129             toolbar.insert(filesearch_tbtn, -1)
130             toolbar.insert(debsearch_tbtn, -1)
131
132             search_tbtns = [filesearch_tbtn, debsearch_tbtn]
133
134             # Activate radio tool button
135             for btn in search_tbtns:
136                 if btn.get_name() == self.config['search']:
137                     btn.set_active(True)
138
139             return toolbar
140
141         def about_dialog(widget):
142             """About dialog window."""
143             dialog = gtk.AboutDialog()
144             dialog.set_name(abstrac.progname)
145             dialog.set_version(abstrac.version)
146             dialog.set_authors(abstrac.authors)
147             dialog.set_comments(abstrac.comments)
148             dialog.set_license(abstrac.license)
149             dialog.show_all()
150             dialog.run()
151             dialog.destroy()
152
153         window = gtk.Window()
154         window.set_default_size(600, 400)
155         window.set_geometry_hints(None, 600, 400)
156         window.set_border_width(4)
157         window.set_wmclass('MainWindow', 'FindIT')
158         window.connect('destroy', gtk.main_quit)
159
160         menu = _create_menu()
161         toolbar = _create_toolbar()
162
163         self.vbox = gtk.VBox(False, 4)
164         self.vbox.pack_start(menu, False, False, 0)
165         self.vbox.pack_start(toolbar, False, False, 0)
166         self.show_search_toplevel(None, self.config['search'])
167
168         window.add(self.vbox)
169         window.show_all()
170         gtk.main()
171
172     #=== Search selecting =====================================================
173     def show_search_toplevel(self, btn, searchtype):
174         print 'Entering <' + searchtype + '> search mode...'
175
176         search_module = __import__(searchtype + '.search')
177         search_toplevel = search_module.search.Control('gtk', self.config).run()
178
179         try:
180             self.vbox.remove(self.vbox.get_children()[2])
181         except:
182             pass
183         self.vbox.pack_start(search_toplevel, True, True, 0)
184         search_toplevel.show_all()
185
186 #==============================================================================
187
188 class Hildon_Presentation(object):
189     """Main window class."""
190
191     def __init__(self, config):
192         import gtk
193         import hildon
194
195         self.config = config
196
197 #==============================================================================
198
199 if __name__ == '__main__':
200     Control()