~pyneighborhood/pyneighborhood/0.5

« back to all changes in this revision

Viewing changes to mainwindow.py

  • Committer: definer
  • Date: 2006-09-16 18:34:46 UTC
  • Revision ID: svn-v3-trunk0:918a6f1d-dd1c-0410-8c33-97c2c1a26c01:trunk:6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
# -*- coding: utf8-*-
 
3
 
 
4
# creates a main window with basic view of a samba tree
 
5
 
 
6
import pygtk
 
7
pygtk.require('2.0')
 
8
import gtk
 
9
import gobject
 
10
import string
 
11
import os
 
12
import time
 
13
from os import system, getenv
 
14
 
 
15
import dialog
 
16
import addconfig
 
17
import config
 
18
import threads
 
19
import monitor
 
20
import bookmarks
 
21
import handlers
 
22
import pyNeighborhood
 
23
 
 
24
class MainWindow:
 
25
 
 
26
    #
 
27
    # close the window and quit
 
28
    #
 
29
    def delete_event(self):
 
30
        home = getenv("HOME")
 
31
        conflocation = string.join([home, "/.pyNeighborhood"], '')
 
32
        os.chdir(conflocation)
 
33
        gtk.main_quit()
 
34
        self.monitor_thr.quit = True
 
35
        os._exit(99)
 
36
        return False
 
37
 
 
38
    #
 
39
    # create a main menu
 
40
    #
 
41
    def get_main_menu(self, window):
 
42
        actions = gtk.ActionGroup("Actions")
 
43
        actions.add_actions(self.menu_entries)
 
44
 
 
45
        ui = gtk.UIManager()
 
46
        ui.insert_action_group(actions, 0)
 
47
        window.add_accel_group(ui.get_accel_group())
 
48
        try:
 
49
            mergeid = ui.add_ui_from_string(self.ui_info)
 
50
        except gobject.GError, msg:
 
51
            print "building menus failed: %s" % msg
 
52
 
 
53
 
 
54
        return ui
 
55
    
 
56
    #
 
57
    # global handlers
 
58
    #
 
59
    def delete_event_handler(self, widget, event, data=None):
 
60
        self.delete_event()
 
61
 
 
62
    def umount(self):
 
63
        #
 
64
        # It is really necessary to move this handler here -
 
65
        # because "monitor" and "bookmarks" objects are to be passed
 
66
        #
 
67
        treeselection = self.bookmarks.treeview.get_selection()
 
68
        (model, iter) = treeselection.get_selected()
 
69
        if iter != None:
 
70
            thr = threads.Umount_Thread(self.notebook, 
 
71
                                        self.treeview, 
 
72
                                        self.treestore, 
 
73
                                        self.treeview_mon, 
 
74
                                     self.treestore_mon)
 
75
            thr.start()
 
76
 
 
77
    #
 
78
        # handlers for running menuitems
 
79
    #
 
80
    def scan_menu_handler(self, action):
 
81
        handlers.scan(self, self)
 
82
 
 
83
    def remove_menu_handler(self, action):
 
84
        handlers.remove(self, self)
 
85
 
 
86
    def filemanager_menu_handler(self, action):
 
87
        handlers.filemanager(self, self)
 
88
                
 
89
    def mount_menu_handler(self, action):
 
90
        handlers.mount(self, self)
 
91
 
 
92
    def umount_menu_handler(self, action):
 
93
        self.umount()
 
94
 
 
95
    def about_menu_handler(self, action):
 
96
        dialog.about(self.window)
 
97
        return
 
98
                
 
99
    def addmach_menu_handler(self, action):
 
100
        newhost = dialog.addmachine(self.window)
 
101
        if newhost != None: 
 
102
            self.treestore.append(None, [newhost, "host", "", ""])
 
103
            addconfig.addhost(newhost)
 
104
        return
 
105
 
 
106
    #
 
107
    # After new interface implementing!!!!!!!!!!!!!!
 
108
    #
 
109
    def preferences_menu_handler(self, action):
 
110
        newoptdict = dialog.options(self.window)
 
111
        if newoptdict != None:
 
112
            pyNeighborhood.parser.write(newoptdict["mount_directory"], newoptdict["file_managers"], 
 
113
                                        newoptdict["smb_mount"], newoptdict["smb_umount"], newoptdict["smb_options"],
 
114
                                        "", "", "", "", "")
 
115
        return
 
116
 
 
117
    def quit_menu_handler(self, action):
 
118
        self.delete_event()
 
119
 
 
120
    #
 
121
    # constructor
 
122
    #
 
123
    def __init__(self):
 
124
        #
 
125
        # create a new window
 
126
        #
 
127
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
 
128
        self.window.set_title("pyNeighborhood")
 
129
        try:
 
130
            self.window.set_icon_from_file("icons/pyneighborhood.png")
 
131
        except:
 
132
            self.window.set_icon_from_file(string.join([config.config(), '/', 'share', '/', 
 
133
                                           'pyNeighborhood', '/', 'icons', '/','pyneighborhood.png'], ''))
 
134
        self.window.set_size_request(600, 400)
 
135
        self.window.connect("delete_event", self.delete_event_handler, self.window)
 
136
        self.window.connect("destroy", self.delete_event_handler, self.window)
 
137
 
 
138
        #
 
139
        # create menu
 
140
        #
 
141
        self.menu_vbox = gtk.VBox(False, 1)
 
142
        self.menu_vbox.set_border_width(1)
 
143
 
 
144
        self.menu_entries = (
 
145
            ("FileMenu", None, "_File"),
 
146
            ("ActionMenu", None, "_Action"),
 
147
            ("EditMenu", None, "_Edit"),
 
148
            ("HelpMenu", None, "_Help"),
 
149
            ("Quit", gtk.STOCK_QUIT,
 
150
             None, "<control>Q", None,
 
151
             self.quit_menu_handler),
 
152
            ("Scan", "SCAN", 
 
153
             "_Scan", None, None,
 
154
             self.scan_menu_handler),
 
155
            ("Mount", "MOUNT", 
 
156
             "_Mount", None, None,
 
157
             self.mount_menu_handler),
 
158
            ("Unmount", "UNMOUNT", 
 
159
             "_Unmount", None, None,
 
160
             self.umount_menu_handler),
 
161
            ("FileManager","FILEMANAGER", 
 
162
             "_File Manager", None, None,
 
163
             self.filemanager_menu_handler),
 
164
            ("AddMachine", gtk.STOCK_ADD, 
 
165
             "_Add Machine...", "<control>A", None,
 
166
             self.addmach_menu_handler),
 
167
            ("Remove", gtk.STOCK_REMOVE, 
 
168
             None, "<control>R", None,
 
169
             self.remove_menu_handler),
 
170
            ("Preferences", gtk.STOCK_PREFERENCES, 
 
171
             None, None, None,
 
172
             self.preferences_menu_handler),
 
173
            ("Help", gtk.STOCK_ABOUT, 
 
174
             None, None, None,
 
175
             self.about_menu_handler)
 
176
        )
 
177
 
 
178
        self.ui_info = \
 
179
        """
 
180
        <ui>
 
181
          <menubar name = "MenuBar">
 
182
            <menu action = "FileMenu">
 
183
              <menuitem action = "Quit"/>
 
184
            </menu>
 
185
            <menu action = "ActionMenu">
 
186
              <menuitem action = "Scan"/>
 
187
              <menuitem action = "Mount"/>
 
188
              <menuitem action = "Unmount"/>
 
189
              <menuitem action = "FileManager"/>
 
190
            </menu>
 
191
            <menu action = "EditMenu">
 
192
              <menuitem action = "AddMachine"/>
 
193
              <menuitem action = "Remove"/>
 
194
              <menuitem action = "Preferences"/>
 
195
            </menu>
 
196
            <menu action = "HelpMenu">
 
197
              <menuitem action = "Help"/>
 
198
            </menu>
 
199
          </menubar>
 
200
        </ui>
 
201
        """
 
202
        menubar = self.get_main_menu(self.window)
 
203
        self.window.add(self.menu_vbox)
 
204
        self.menu_vbox.show()
 
205
        self.menu_vbox.pack_start(menubar.get_widget("/MenuBar"), False, False, 0)
 
206
        
 
207
        #
 
208
        # create a new notebook, place the position of the tabs
 
209
        #
 
210
        self.notebook = gtk.Notebook()
 
211
        label_1 = gtk.Label()
 
212
        label_1.set_text("Bookmarks")
 
213
        label_2 = gtk.Label()
 
214
        label_2.set_text("Mounts")
 
215
        
 
216
        #
 
217
        # vbox with notebook
 
218
        #
 
219
        # Bookmarks
 
220
        self.note_vbox = gtk.VBox(False, 1)
 
221
        self.note_vbox.set_border_width(1)
 
222
        self.note_vbox.show()
 
223
 
 
224
        # Mounts
 
225
        self.note_vbox_mou = gtk.VBox(False, 1)
 
226
        self.note_vbox_mou.set_border_width(1)
 
227
        self.note_vbox_mou.show()
 
228
 
 
229
        #
 
230
        # create "Bookmarks" and "Mounts" widgets
 
231
        #
 
232
        # create bookmarks treeview
 
233
        self.bookmarks = bookmarks.Bookmarks_Widget(self)
 
234
        self.scrolledwindow, self.treestore, self.treeview = self.bookmarks.Bookmarks()
 
235
        # create monitor treeview
 
236
        self.pyn_monitor = monitor.Monitor_Widget(self)
 
237
        self.scrolledwindow_mon, self.treestore_mon, self.treeview_mon = self.pyn_monitor.Monitor()
 
238
 
 
239
        #
 
240
        # add widgets to notebook
 
241
        #
 
242
        # Bookmarks
 
243
        self.note_vbox.pack_start(self.scrolledwindow, True, True, 0)
 
244
        self.notebook.append_page(self.note_vbox, label_1)
 
245
 
 
246
        # Mounts
 
247
        self.note_vbox_mou.pack_start(self.scrolledwindow_mon, True, True, 0)
 
248
        self.notebook.append_page(self.note_vbox_mou, label_2)
 
249
 
 
250
        #
 
251
        # show the notebook
 
252
        #
 
253
        self.notebook.set_show_tabs(True)
 
254
        self.notebook.show()
 
255
 
 
256
        #
 
257
        # start a thread with the mounts monitor
 
258
        #
 
259
        self.monitor_thr = threads.Monitor_Thread(self.treestore_mon)
 
260
        self.monitor_thr.start()
 
261
 
 
262
        #
 
263
        # pack everything into vbox and show all
 
264
        #
 
265
 
 
266
        print pyNeighborhood.parser.optdict 
 
267
        self.menu_vbox.pack_start(self.notebook, True, True, 0)
 
268
        self.window.show_all()
 
269
 
 
270
def main():
 
271
    gobject.threads_init()
 
272
    gtk.main()
 
273