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