summaryrefslogtreecommitdiff
path: root/examples/open-terminal.py
blob: c80552ab5308145dbbc1658d77098381c880b4ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# This example is contributed by Martin Enlund
import os

try:
    # Python 3.
    from urllib.parse import unquote
except:
    # Python 2.
    from urllib import unquote

from gi.repository import Caja, GObject, Gio

TERMINAL_SCHEMA = 'org.mate.applications-terminal'
TERMINAL_KEY = 'exec'

class OpenTerminalExtension(Caja.MenuProvider, GObject.GObject):
    def __init__(self):
        self.gsettings = Gio.Settings.new(TERMINAL_SCHEMA)
        
    def _open_terminal(self, file):
        filename = unquote(file.get_uri()[7:])
        terminal = self.gsettings[TERMINAL_KEY]

        os.chdir(filename)
        os.system('%s &' % terminal)
        
    def menu_activate_cb(self, menu, file):
        self._open_terminal(file)
        
    def menu_background_activate_cb(self, menu, file): 
        self._open_terminal(file)
       
    def get_file_items(self, window, files):
        if len(files) != 1:
            return
        
        file = files[0]
        if not file.is_directory() or file.get_uri_scheme() != 'file':
            return
        
        item = Caja.MenuItem(name='CajaPython::openterminal_file_item',
                                 label='Open Terminal' ,
                                 tip='Open Terminal In %s' % file.get_name())
        item.connect('activate', self.menu_activate_cb, file)
        return item,

    def get_background_items(self, window, file):
        item = Caja.MenuItem(name='CajaPython::openterminal_item',
                                 label='Open Terminal Here',
                                 tip='Open Terminal In This Directory')
        item.connect('activate', self.menu_background_activate_cb, file)
        return item,