/usr/share/sugar/activities/ImageViewer.activity/ImageViewerActivity.py is in sugar-imageviewer-activity 62-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 | # Copyright (C) 2008, One Laptop per Child
# Author: Sayamindu Dasgupta <sayamindu@laptop.org>
#
# 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
# The sharing bits have been taken from ReadEtexts
from __future__ import division
from sugar3.activity import activity
import logging
from gettext import gettext as _
import time
import os
from gi.repository import Gtk
from gi.repository import Gdk
from gi.repository import GObject
from sugar3.graphics.alert import NotifyAlert
from sugar3.graphics.objectchooser import ObjectChooser
try:
from sugar3.graphics.objectchooser import FILTER_TYPE_GENERIC_MIME
except:
FILTER_TYPE_GENERIC_MIME = 'generic_mime'
from sugar3 import mime
from sugar3.graphics.toolbutton import ToolButton
from sugar3.graphics.toolbarbox import ToolbarBox
from sugar3.graphics.icon import Icon
from sugar3.activity.widgets import ActivityToolbarButton
from sugar3.activity.widgets import StopButton
from sugar3.graphics import style
from sugar3.graphics.alert import Alert
from sugar3 import network
from sugar3.datastore import datastore
try:
from gi.repository import SugarGestures
GESTURES_AVAILABLE = True
except:
GESTURES_AVAILABLE = False
import telepathy
import dbus
import ImageView
class ProgressAlert(Alert):
"""
Progress alert with a progressbar - to show the advance of a task
"""
def __init__(self, timeout=5, **kwargs):
Alert.__init__(self, **kwargs)
self._pb = Gtk.ProgressBar()
self._msg_box.pack_start(self._pb, False, False, 0)
self._pb.set_size_request(int(Gdk.Screen.width() * 9. / 10.), -1)
self._pb.set_fraction(0.0)
self._pb.show()
def set_fraction(self, fraction):
# update only by 10% fractions
if int(fraction * 100) % 10 == 0:
self._pb.set_fraction(fraction)
self._pb.queue_draw()
# force updating the progressbar
while Gtk.events_pending():
Gtk.main_iteration_do(True)
class ImageViewerHTTPRequestHandler(network.ChunkedGlibHTTPRequestHandler):
"""HTTP Request Handler for transferring document while collaborating.
RequestHandler class that integrates with Glib mainloop. It writes
the specified file to the client in chunks, returning control to the
mainloop between chunks.
"""
def translate_path(self, path):
"""Return the filepath to the shared document."""
return self.server.filepath
class ImageViewerHTTPServer(network.GlibTCPServer):
"""HTTP Server for transferring document while collaborating."""
def __init__(self, server_address, filepath):
"""Set up the GlibTCPServer with the ImageViewerHTTPRequestHandler.
filepath -- path to shared document to be served.
"""
self.filepath = filepath
network.GlibTCPServer.__init__(self, server_address,
ImageViewerHTTPRequestHandler)
class ImageViewerURLDownloader(network.GlibURLDownloader):
"""URLDownloader that provides content-length and content-type."""
def get_content_length(self):
"""Return the content-length of the download."""
if self._info is not None:
return int(self._info.headers.get('Content-Length'))
def get_content_type(self):
"""Return the content-type of the download."""
if self._info is not None:
return self._info.headers.get('Content-type')
return None
IMAGEVIEWER_STREAM_SERVICE = 'imageviewer-activity-http'
class ImageViewerActivity(activity.Activity):
def __init__(self, handle):
activity.Activity.__init__(self, handle)
self._object_id = handle.object_id
self._zoom_out_button = None
self._zoom_in_button = None
self.previous_image_button = None
self.next_image_button = None
self._fileserver = None
self._fileserver_tube_id = None
self.scrolled_window = Gtk.ScrolledWindow()
self.scrolled_window.set_policy(Gtk.PolicyType.ALWAYS,
Gtk.PolicyType.ALWAYS)
# disable sharing until a file is opened
self.max_participants = 1
# Don't use the default kinetic scrolling, let the view do the
# drag-by-touch and pinch-to-zoom logic.
self.scrolled_window.set_kinetic_scrolling(False)
self.view = ImageView.ImageViewer()
# Connect to the touch signal for performing drag-by-touch.
self.view.add_events(Gdk.EventMask.TOUCH_MASK)
self._touch_hid = self.view.connect('touch-event',
self.__touch_event_cb)
self.scrolled_window.add(self.view)
self.view.show()
self.connect('key-press-event', self.__key_press_cb)
if GESTURES_AVAILABLE:
# Connect to the zoom signals for performing
# pinch-to-zoom.
zoom_controller = SugarGestures.ZoomController()
zoom_controller.attach(self,
SugarGestures.EventControllerFlags.NONE)
zoom_controller.connect('began', self.__zoomtouch_began_cb)
zoom_controller.connect('scale-changed',
self.__zoomtouch_changed_cb)
zoom_controller.connect('ended', self.__zoomtouch_ended_cb)
self._progress_alert = None
toolbar_box = ToolbarBox()
self._add_toolbar_buttons(toolbar_box)
self.set_toolbar_box(toolbar_box)
toolbar_box.show()
if self._object_id is None or not self._jobject.file_path:
empty_widgets = Gtk.EventBox()
empty_widgets.modify_bg(Gtk.StateType.NORMAL,
style.COLOR_WHITE.get_gdk_color())
vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
mvbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
vbox.pack_start(mvbox, True, False, 0)
image_icon = Icon(pixel_size=style.LARGE_ICON_SIZE,
icon_name='imageviewer',
stroke_color=style.COLOR_BUTTON_GREY.get_svg(),
fill_color=style.COLOR_TRANSPARENT.get_svg())
mvbox.pack_start(image_icon, False, False, style.DEFAULT_PADDING)
label = Gtk.Label('<span foreground="%s"><b>%s</b></span>' %
(style.COLOR_BUTTON_GREY.get_html(),
_('No image')))
label.set_use_markup(True)
mvbox.pack_start(label, False, False, style.DEFAULT_PADDING)
hbox = Gtk.Box()
open_image_btn = Gtk.Button()
open_image_btn.connect('clicked', self._show_picker_cb)
add_image = Gtk.Image.new_from_stock(Gtk.STOCK_ADD,
Gtk.IconSize.BUTTON)
buttonbox = Gtk.Box()
buttonbox.pack_start(add_image, False, True, 0)
buttonbox.pack_end(Gtk.Label(_('Choose an image')), True, True, 5)
open_image_btn.add(buttonbox)
hbox.pack_start(open_image_btn, True, False, 0)
mvbox.pack_start(hbox, False, False, style.DEFAULT_PADDING)
empty_widgets.add(vbox)
empty_widgets.show_all()
self.set_canvas(empty_widgets)
else:
self.set_canvas(self.scrolled_window)
self.scrolled_window.show()
self.unused_download_tubes = set()
self._want_document = True
self._download_content_length = 0
self._download_content_type = None
# Status of temp file used for write_file:
self._tempfile = None
self._close_requested = False
self.connect("shared", self._shared_cb)
h = hash(self._activity_id)
self.port = 1024 + (h % 64511)
self.is_received_document = False
if self.shared_activity:
# We're joining, and we don't already have the document.
if self.get_shared():
# Already joined for some reason, just get the document
self._joined_cb(self)
else:
self._progress_alert = ProgressAlert()
self._progress_alert.props.title = _('Please wait')
self._progress_alert.props.msg = _('Starting connection...')
self.add_alert(self._progress_alert)
# Wait for a successful join before trying to get the document
self.connect("joined", self._joined_cb)
Gdk.Screen.get_default().connect('size-changed', self._configure_cb)
def __touch_event_cb(self, widget, event):
coords = event.get_coords()
if event.type == Gdk.EventType.TOUCH_BEGIN:
self.view.start_dragtouch(coords)
elif event.type == Gdk.EventType.TOUCH_UPDATE:
self.view.update_dragtouch(coords)
elif event.type == Gdk.EventType.TOUCH_END:
self.view.finish_dragtouch(coords)
def __zoomtouch_began_cb(self, controller):
self.view.start_zoomtouch(controller.get_center())
# Don't listen to touch signals until pinch-to-zoom ends.
self.view.disconnect(self._touch_hid)
def __zoomtouch_changed_cb(self, controller, scale):
self.view.update_zoomtouch(controller.get_center(), scale)
def __zoomtouch_ended_cb(self, controller):
self.view.finish_zoomtouch()
self._touch_hid = self.view.connect('touch-event',
self.__touch_event_cb)
def __key_press_cb(self, widget, event):
key_name = Gdk.keyval_name(event.keyval)
if key_name == "Left":
self._change_image(-1)
elif key_name == "Right":
self._change_image(1)
elif event.get_state() & Gdk.ModifierType.CONTROL_MASK:
if key_name == "q":
self.close()
return True
def _get_image_list(self):
value = mime.GENERIC_TYPE_IMAGE
mime_types = mime.get_generic_type(value).mime_types
(self.image_list, self.image_count) = datastore.find({'mime_type':
mime_types})
def _add_toolbar_buttons(self, toolbar_box):
self._seps = []
self.activity_button = ActivityToolbarButton(self)
toolbar_box.toolbar.insert(self.activity_button, 0)
self.activity_button.show()
self._zoom_out_button = ToolButton('zoom-out')
self._zoom_out_button.set_tooltip(_('Zoom out'))
self._zoom_out_button.connect('clicked', self.__zoom_out_cb)
toolbar_box.toolbar.insert(self._zoom_out_button, -1)
self._zoom_out_button.show()
self._zoom_in_button = ToolButton('zoom-in')
self._zoom_in_button.set_tooltip(_('Zoom in'))
self._zoom_in_button.connect('clicked', self.__zoom_in_cb)
toolbar_box.toolbar.insert(self._zoom_in_button, -1)
self._zoom_in_button.show()
zoom_tofit_button = ToolButton('zoom-best-fit')
zoom_tofit_button.set_tooltip(_('Fit to window'))
zoom_tofit_button.connect('clicked', self.__zoom_tofit_cb)
toolbar_box.toolbar.insert(zoom_tofit_button, -1)
zoom_tofit_button.show()
zoom_original_button = ToolButton('zoom-original')
zoom_original_button.set_tooltip(_('Original size'))
zoom_original_button.connect('clicked', self.__zoom_original_cb)
toolbar_box.toolbar.insert(zoom_original_button, -1)
zoom_original_button.show()
fullscreen_button = ToolButton('view-fullscreen')
fullscreen_button.set_tooltip(_('Fullscreen'))
fullscreen_button.connect('clicked', self.__fullscreen_cb)
toolbar_box.toolbar.insert(fullscreen_button, -1)
fullscreen_button.show()
self._seps.append(Gtk.SeparatorToolItem())
toolbar_box.toolbar.insert(self._seps[-1], -1)
self._seps[-1].show()
rotate_anticlockwise_button = ToolButton('rotate_anticlockwise')
rotate_anticlockwise_button.set_tooltip(_('Rotate anticlockwise'))
rotate_anticlockwise_button.connect('clicked',
self.__rotate_anticlockwise_cb)
toolbar_box.toolbar.insert(rotate_anticlockwise_button, -1)
rotate_anticlockwise_button.show()
rotate_clockwise_button = ToolButton('rotate_clockwise')
rotate_clockwise_button.set_tooltip(_('Rotate clockwise'))
rotate_clockwise_button.connect('clicked', self.__rotate_clockwise_cb)
toolbar_box.toolbar.insert(rotate_clockwise_button, -1)
rotate_clockwise_button.show()
if self._object_id is None:
self._seps.append(Gtk.SeparatorToolItem())
toolbar_box.toolbar.insert(self._seps[-1], -1)
self._seps[-1].show()
self.previous_image_button = ToolButton('go-previous-paired')
self.previous_image_button.set_tooltip(_('Previous Image'))
self.previous_image_button.props.sensitive = False
self.previous_image_button.connect('clicked',
self.__previous_image_cb)
toolbar_box.toolbar.insert(self.previous_image_button, -1)
self.previous_image_button.show()
self.next_image_button = ToolButton('go-next-paired')
self.next_image_button.set_tooltip(_('Next Image'))
self.next_image_button.props.sensitive = False
self.next_image_button.connect('clicked', self.__next_image_cb)
toolbar_box.toolbar.insert(self.next_image_button, -1)
self.next_image_button.show()
GObject.idle_add(self._get_image_list)
separator = Gtk.SeparatorToolItem()
separator.props.draw = False
separator.set_expand(True)
toolbar_box.toolbar.insert(separator, -1)
separator.show()
stop_button = StopButton(self)
toolbar_box.toolbar.insert(stop_button, -1)
stop_button.show()
def _configure_cb(self, event=None):
if Gdk.Screen.width() <= style.GRID_CELL_SIZE * 12:
for sep in self._seps:
sep.hide()
else:
for sep in self._seps:
sep.show()
def _update_zoom_buttons(self):
self._zoom_in_button.set_sensitive(self.view.can_zoom_in())
self._zoom_out_button.set_sensitive(self.view.can_zoom_out())
def _change_image(self, delta):
# boundary conditions
if self.current_image_index == 0 and delta == -1:
return
if self.current_image_index == self.image_count - 1 and delta == 1:
return
self.current_image_index += delta
self.make_button_sensitive()
jobject = self.image_list[self.current_image_index]
self._object_id = jobject.object_id
if os.path.exists(self._tempfile):
os.remove(self._tempfile)
self.read_file(jobject.file_path)
def __previous_image_cb(self, button):
if self.current_image_index > 0:
self._change_image(-1)
def __next_image_cb(self, button):
if self.current_image_index < self.image_count:
self._change_image(1)
def __zoom_in_cb(self, button):
self.view.zoom_in()
self._update_zoom_buttons()
def __zoom_out_cb(self, button):
self.view.zoom_out()
self._update_zoom_buttons()
def __zoom_tofit_cb(self, button):
self.view.zoom_to_fit()
self._update_zoom_buttons()
def __zoom_original_cb(self, button):
self.view.zoom_original()
self._update_zoom_buttons()
def __rotate_anticlockwise_cb(self, button):
self.view.rotate_anticlockwise()
def __rotate_clockwise_cb(self, button):
self.view.rotate_clockwise()
def __fullscreen_cb(self, button):
self.fullscreen()
def get_current_image_index(self):
for image in self.image_list:
if image.object_id == self._object_id:
jobject = image
break
self.current_image_index = self.image_list.index(jobject)
def make_button_sensitive(self):
if self.image_count <= 1:
return
if self.current_image_index == 0:
self.next_image_button.props.sensitive = True
self.previous_image_button.props.sensitive = False
elif self.current_image_index == self.image_count - 1:
self.previous_image_button.props.sensitive = True
self.next_image_button.props.sensitive = False
else:
self.next_image_button.props.sensitive = True
self.previous_image_button.props.sensitive = True
def _show_picker_cb(self, button):
if not self._want_document:
return
try:
chooser = ObjectChooser(self, what_filter='Image',
filter_type=FILTER_TYPE_GENERIC_MIME,
show_preview=True)
except:
# for compatibility with older versions
chooser = ObjectChooser(self._activity, what_filter='Image')
try:
result = chooser.run()
if result == Gtk.ResponseType.ACCEPT:
jobject = chooser.get_selected_object()
if jobject and jobject.file_path:
self._object_id = jobject.object_id
self.read_file(jobject.file_path)
self.set_canvas(self.scrolled_window)
self.scrolled_window.show()
finally:
self.get_current_image_index()
self.make_button_sensitive()
chooser.destroy()
del chooser
def read_file(self, file_path):
if self._object_id is None or self.shared_activity:
# read_file is call because the canvas is visible
# but we need check if is not the case of empty file
return
self._want_document = False
# enable collaboration
self.activity_button.page.share.props.sensitive = True
tempfile = os.path.join(self.get_activity_root(), 'instance',
'tmp%i' % time.time())
os.link(file_path, tempfile)
self._tempfile = tempfile
self.view.set_file_location(tempfile)
zoom = self.metadata.get('zoom', None)
if zoom is not None:
self.view.set_zoom(float(zoom))
def write_file(self, file_path):
if self._tempfile:
self.metadata['activity'] = self.get_bundle_id()
self.metadata['zoom'] = str(self.view.get_zoom())
if self._close_requested:
os.link(self._tempfile, file_path)
os.unlink(self._tempfile)
self._tempfile = None
else:
raise NotImplementedError
def can_close(self):
self._close_requested = True
return True
def _download_result_cb(self, getter, tempfile, suggested_name, tube_id):
if self._download_content_type == 'text/html':
# got an error page instead
self._download_error_cb(getter, 'HTTP Error', tube_id)
return
del self.unused_download_tubes
self._tempfile = tempfile
file_path = os.path.join(self.get_activity_root(), 'instance',
'%i' % time.time())
logging.debug("Saving file %s to datastore...", file_path)
os.link(tempfile, file_path)
self._jobject.file_path = file_path
datastore.write(self._jobject, transfer_ownership=True)
logging.debug("Got document %s (%s) from tube %u",
tempfile, suggested_name, tube_id)
if self._progress_alert is not None:
self.remove_alert(self._progress_alert)
self._progress_alert = None
GObject.idle_add(self.__set_file_idle_cb, self._jobject.object_id)
def __set_file_idle_cb(self, object_id):
dsobj = datastore.get(object_id)
self._tempfile = dsobj.file_path
""" This method is used when join a collaboration session """
self.view.set_file_location(self._tempfile)
try:
zoom = int(self.metadata.get('zoom', '0'))
if zoom > 0:
self.view.set_zoom(zoom)
except Exception:
pass
self.set_canvas(self.scrolled_window)
self.scrolled_window.show_all()
return False
def _download_progress_cb(self, getter, bytes_downloaded, tube_id):
if self._download_content_length > 0:
logging.debug("Downloaded %u of %u bytes from tube %u...",
bytes_downloaded, self._download_content_length,
tube_id)
else:
logging.debug("Downloaded %u bytes from tube %u...",
bytes_downloaded, tube_id)
total = self._download_content_length
fraction = bytes_downloaded / total
self._progress_alert.set_fraction(fraction)
def _download_error_cb(self, getter, err, tube_id):
logging.debug("Error getting document from tube %u: %s",
tube_id, err)
self._alert('Failure', 'Error getting document from tube')
self._want_document = True
self._download_content_length = 0
self._download_content_type = None
GObject.idle_add(self._get_document)
def _download_document(self, tube_id, path):
# FIXME: should ideally have the CM listen on a Unix socket
# instead of IPv4 (might be more compatible with Rainbow)
chan = self.shared_activity.telepathy_tubes_chan
iface = chan[telepathy.CHANNEL_TYPE_TUBES]
addr = iface.AcceptStreamTube(
tube_id,
telepathy.SOCKET_ADDRESS_TYPE_IPV4,
telepathy.SOCKET_ACCESS_CONTROL_LOCALHOST, 0,
utf8_strings=True)
logging.debug('Accepted stream tube: listening address is %r', addr)
# SOCKET_ADDRESS_TYPE_IPV4 is defined to have addresses of type '(sq)'
assert isinstance(addr, dbus.Struct)
assert len(addr) == 2
assert isinstance(addr[0], str)
assert isinstance(addr[1], (int, long))
assert addr[1] > 0 and addr[1] < 65536
port = int(addr[1])
getter = ImageViewerURLDownloader("http://%s:%d/document"
% (addr[0], port))
getter.connect("finished", self._download_result_cb, tube_id)
getter.connect("progress", self._download_progress_cb, tube_id)
getter.connect("error", self._download_error_cb, tube_id)
logging.debug("Starting download to %s...", path)
getter.start(path)
self._download_content_length = getter.get_content_length()
self._download_content_type = getter.get_content_type()
return False
def _get_document(self):
if not self._want_document:
return False
# Assign a file path to download if one doesn't exist yet
if not self._jobject.file_path:
path = os.path.join(self.get_activity_root(), 'instance',
'tmp%i' % time.time())
else:
path = self._jobject.file_path
# Pick an arbitrary tube we can try to download the document from
try:
tube_id = self.unused_download_tubes.pop()
except (ValueError, KeyError), e:
logging.debug('No tubes to get the document from right now: %s',
e)
return False
# Avoid trying to download the document multiple times at once
self._want_document = False
GObject.idle_add(self._download_document, tube_id, path)
return False
def _joined_cb(self, also_self):
"""Callback for when a shared activity is joined.
Get the shared document from another participant.
"""
self.watch_for_tubes()
if self._progress_alert is not None:
self._progress_alert.props.msg = _('Receiving image...')
def _share_document(self):
"""Share the document."""
# FIXME: should ideally have the fileserver listen on a Unix socket
# instead of IPv4 (might be more compatible with Rainbow)
logging.debug('Starting HTTP server on port %d', self.port)
self._fileserver = ImageViewerHTTPServer(("", self.port),
self._tempfile)
# Make a tube for it
chan = self.shared_activity.telepathy_tubes_chan
iface = chan[telepathy.CHANNEL_TYPE_TUBES]
self._fileserver_tube_id = \
iface.OfferStreamTube(
IMAGEVIEWER_STREAM_SERVICE,
{},
telepathy.SOCKET_ADDRESS_TYPE_IPV4,
('127.0.0.1', dbus.UInt16(self.port)),
telepathy.SOCKET_ACCESS_CONTROL_LOCALHOST, 0)
def watch_for_tubes(self):
"""Watch for new tubes."""
tubes_chan = self.shared_activity.telepathy_tubes_chan
tubes_chan[telepathy.CHANNEL_TYPE_TUBES].connect_to_signal(
'NewTube', self._new_tube_cb)
tubes_chan[telepathy.CHANNEL_TYPE_TUBES].ListTubes(
reply_handler=self._list_tubes_reply_cb,
error_handler=self._list_tubes_error_cb)
def _new_tube_cb(self, tube_id, initiator, tube_type, service, params,
state):
"""Callback when a new tube becomes available."""
logging.debug('New tube: ID=%d initator=%d type=%d service=%s '
'params=%r state=%d', tube_id, initiator, tube_type,
service, params, state)
if service == IMAGEVIEWER_STREAM_SERVICE:
logging.debug('I could download from that tube')
self.unused_download_tubes.add(tube_id)
# if no download is in progress, let's fetch the document
if self._want_document:
GObject.idle_add(self._get_document)
def _list_tubes_reply_cb(self, tubes):
"""Callback when new tubes are available."""
for tube_info in tubes:
self._new_tube_cb(*tube_info)
def _list_tubes_error_cb(self, e):
"""Handle ListTubes error by logging."""
logging.error('ListTubes() failed: %s', e)
def _shared_cb(self, activityid):
"""Callback when activity shared.
Set up to share the document.
"""
# We initiated this activity and have now shared it, so by
# definition we have the file.
logging.debug('Activity became shared')
self.next_image_button.props.sensitive = False
self.previous_image_button.props.sensitive = False
self.watch_for_tubes()
self._share_document()
def _alert(self, title, text=None):
alert = NotifyAlert(timeout=5)
alert.props.title = title
alert.props.msg = text
self.add_alert(alert)
alert.connect('response', self._alert_cancel_cb)
alert.show()
def _alert_cancel_cb(self, alert, response_id):
self.remove_alert(alert)
|