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