Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/vnclauncher.py
blob: c580cc48fbaab60f11154401d449e0882ffc1012 (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (C) 2013, SomosAzucar
# Version especial de VncLauncher con nuevos cambios.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

import gtk
import time
import gobject
import os
import commands
import platform
from threading import Thread

from gettext import gettext as _

from sugar.activity import activity
from sugar.graphics.radiotoolbutton import RadioToolButton
from sugar.graphics.toolbutton import ToolButton
from sugar.graphics.toolbarbox import ToolbarBox
from sugar.activity.widgets import StopButton
from sugar.activity.widgets import ActivityToolbarButton


class VncLauncherActivity(activity.Activity):

    def __init__(self, handle):
        activity.Activity.__init__(self, handle)

        self.max_participants = 1

        self.toolbarbox = ToolbarBox()
        self.toolbar = self.toolbarbox.toolbar

        self.start_vnc = RadioToolButton(icon_name="start_vnc",
                                         tooltip=_("Iniciar servidor VNC"))

        self.stop_vnc = RadioToolButton(icon_name="stop_vnc",
                                        tooltip=_("Detener servidor VNC"),
                                        group=self.start_vnc)

        self.stop_vnc.set_active(True)

        self.get_ipbutton = ToolButton(icon_name="get_ip",
                                       tooltip=_("Obtener la IP actual"))
        
 
        ##
        self.messages = gtk.TreeView()
        self.messages.set_rules_hint(True)
        modelo = gtk.ListStore(str, str, gtk.gdk.Color)
        self.messages.set_model(modelo)
        render = gtk.CellRendererText()
        render1 = gtk.CellRendererText()
        
        column1 = gtk.TreeViewColumn(_("Hora"), render, markup=0)
        column2 = gtk.TreeViewColumn(_("Mensaje"), render1, markup=1) 
        column1.add_attribute(render, 'foreground-gdk', 2)  
        column2.add_attribute(render1, 'foreground-gdk', 2)  
            
        
        self.messages.append_column(column1)
        self.messages.append_column(column2)
        color = gtk.gdk.color_parse("dark blue")
        modelo.insert(0, [time.strftime("\n<b>%H:%M:%S</b>\n"), _("\n<b>Inicio de actividad.</b>\n"), color])

        self.showed_message_stop = True
        self.showed_message_start = False

        self.stop_vnc.connect("clicked", self.__stop_vnc)
        self.start_vnc.connect("clicked", self.__start_vnc)
        self.clear_model = ToolButton(icon_name="clear_console",
                                      tooltip=_("Eliminar mensajes"))
        self.clear_model.connect("clicked", lambda x: modelo.clear())
        self.get_ipbutton.connect("clicked", self.__get_ip)
        self.last_message = 1

        self.__get_x11vnc_path()
        ##
        separator = gtk.SeparatorToolItem()
        separator.props.draw = False
        separator.set_expand(True)

        self.stop_activity = StopButton(self)
        
        self.toolbar.insert(ActivityToolbarButton(self), -1)
        self.toolbar.insert(gtk.SeparatorToolItem(), -1)
        self.toolbar.insert(self.start_vnc, -1)
        self.toolbar.insert(self.stop_vnc, -1)
        self.toolbar.insert(gtk.SeparatorToolItem(), -1)
        self.toolbar.insert(self.get_ipbutton, -1)
        self.toolbar.insert(self.clear_model, -1)
        self.toolbar.insert(separator, -1)
        self.toolbar.insert(self.stop_activity, -1)
       
        self.messages_scroll = gtk.ScrolledWindow()
        self.messages_scroll.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
        self.messages_scroll.add_with_viewport(self.messages)

        self.set_toolbar_box(self.toolbarbox)
        self.set_canvas(self.messages_scroll)

        self.show_all()
        gobject.timeout_add(100, self.__check_is_on)
        
    def __check_is_on(self):
        pid = commands.getoutput("pidof x11vnc")
        if self.start_vnc.get_active() and pid == "":
            self.showed_message_stop = True
            self.stop_vnc.set_active(True)
            self.start_vnc.set_active(False)
            self.showed_message_start = False

            color = gtk.gdk.color_parse("dark red")
            self.messages.get_model().insert(self.last_message, [time.strftime("\n<b>%H:%M:%S</b>\n"), _("\n<b>Se ha detenido inhesperadamente el servidor.</b>\n"), color])
            self.last_message += 1

        return True
    
    def __get_x11vnc_path(self):
        system = platform.machine()
        color = gtk.gdk.color_parse("dark red")
        if os.path.exists("/usr/bin/x11vnc"):
            self.path = "/usr/bin/x11vnc"
            message = _("PATH: %s") % self.path
        else:
            if "arm" in system:
                self.path = os.path.join(activity.get_bundle_path(), "bin", "arm", "x11vnc")
                os.environ["LD_LIBRARY_PATH"] = self.path.replace("x11vnc", "lib/")
            elif "64" in system:
                self.path = os.path.join(activity.get_bundle_path(), "bin", "x64", "x11vnc")
                os.environ["LD_LIBRARY_PATH"] = self.path.replace("x11vnc", "lib/")
            else:
                self.path = os.path.join(activity.get_bundle_path(), "bin", "i586", "x11vnc")
                os.environ["LD_LIBRARY_PATH"] = self.path.replace("x11vnc", "lib/")
            message = _("PATH: %s") % self.path
        
        self.messages.get_model().insert(self.last_message, [time.strftime("\n<b>%H:%M:%S</b>\n"), "<b>" + message + "</b>", color])
        self.last_message += 1
        
    def __start_vnc(self, widget):

        def servidor():
            commands.getoutput(self.path)
        Thread(target=servidor).start()

        if not self.showed_message_start:
            self.showed_message_start = True
            pass
        else:
            return

        self.showed_message_stop = False
        self.isrunning = True
        color = gtk.gdk.color_parse("green")
        self.messages.get_model().insert(self.last_message, [time.strftime("\n<b>%H:%M:%S</b>\n"), _("\n<b>Se inició el servidor VNC</b>\n"), color])
        self.last_message += 1
    
    def __stop_vnc(self, widget):
    
        if not self.showed_message_stop:
            self.showed_message_stop = True
            pass
        else:
            return

        self.showed_message_start = False
        self.pid_nuevo = commands.getoutput("pidof x11vnc")
        color = gtk.gdk.color_parse('red')

        os.system("kill " + self.pid_nuevo)
        
        self.messages.get_model().insert(self.last_message, [time.strftime("\n<b>%H:%M:%S</b>\n"), _("\n<b>El servidor VNC está ahora detenido.</b>\n"), color])
        self.last_message += 1
    
    
    def close(self):
        pid = commands.getoutput("pidof x11vnc")
        commands.getoutput("kill " + pid)
        self.destroy()
    
    def __get_ip(self, widget):
        system = platform.platform()
        if "olpc" in system:
            target = "eth0"
        else:
            target = "wlan0"
        
        ifconfig = "/sbin/ifconfig"
        cmd = "%s %s" % (ifconfig, target)
        output = commands.getoutput(cmd)
        error = _("Sin conexión inalámbrica.")
        ip = error
        inet = output.find('inet')
        if inet >= 0:
            start = inet + len('inet')
            end = output.find(" ",start + 1)
            ip = output[start:end]
        else:
            ip = error
        
        ip = ip.replace(":", "") 
        if error == "Sin conexión inalámbrica.":
            mensaje = error
        else:
            mensaje = "IP: " + ip  
        color = gtk.gdk.color_parse("dark blue")
        self.messages.get_model().insert(self.last_message, [time.strftime("\n<b>%H:%M:%S</b>\n"),  "\n<b>" + mensaje + "</b>\n", color])
        
        self.last_message += 1