This file is indexed.

/usr/share/pyshared/jarabe/frame/activitiestray.py is in python-jarabe-0.96 0.96.1-2.1git1.

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
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
# Copyright (C) 2006-2007 Red Hat, Inc.
# Copyright (C) 2008 One Laptop Per Child
# Copyright (C) 2010 Collabora Ltd. <http://www.collabora.co.uk/>
#
# 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 logging
from gettext import gettext as _
import tempfile
import os

import gobject
import gconf
import gio
import glib
import gtk

from sugar.graphics import style
from sugar.graphics.tray import HTray
from sugar.graphics.xocolor import XoColor
from sugar.graphics.radiotoolbutton import RadioToolButton
from sugar.graphics.toolbutton import ToolButton
from sugar.graphics.icon import Icon, get_icon_file_name
from sugar.graphics.palette import Palette
from sugar.graphics.menuitem import MenuItem
from sugar.datastore import datastore
from sugar import mime
from sugar import env

from jarabe.model import shell
from jarabe.model import invites
from jarabe.model import bundleregistry
from jarabe.model import filetransfer
from jarabe.view.palettes import JournalPalette, CurrentActivityPalette
from jarabe.view.pulsingicon import PulsingIcon
from jarabe.frame.frameinvoker import FrameWidgetInvoker
from jarabe.frame.notification import NotificationIcon
import jarabe.frame


class ActivityButton(RadioToolButton):
    def __init__(self, home_activity, group):
        RadioToolButton.__init__(self, group=group)

        self.set_palette_invoker(FrameWidgetInvoker(self))
        self.palette_invoker.cache_palette = False

        self._home_activity = home_activity
        self._notify_launch_hid = None

        self._icon = PulsingIcon()
        self._icon.props.base_color = home_activity.get_icon_color()
        self._icon.props.pulse_color = \
                XoColor('%s,%s' % (style.COLOR_BUTTON_GREY.get_svg(),
                                   style.COLOR_TOOLBAR_GREY.get_svg()))
        if home_activity.get_icon_path():
            self._icon.props.file = home_activity.get_icon_path()
        else:
            self._icon.props.icon_name = 'image-missing'
        self.set_icon_widget(self._icon)
        self._icon.show()

        if home_activity.props.launch_status == shell.Activity.LAUNCHING:
            self._icon.props.pulsing = True
            self._notify_launch_hid = home_activity.connect( \
                    'notify::launch-status', self.__notify_launch_status_cb)
        elif home_activity.props.launch_status == shell.Activity.LAUNCH_FAILED:
            self._on_failed_launch()

    def create_palette(self):
        if self._home_activity.is_journal():
            palette = JournalPalette(self._home_activity)
        else:
            palette = CurrentActivityPalette(self._home_activity)
        palette.set_group_id('frame')
        self.set_palette(palette)

    def _on_failed_launch(self):
        # TODO http://bugs.sugarlabs.org/ticket/2007
        pass

    def __notify_launch_status_cb(self, home_activity, pspec):
        home_activity.disconnect(self._notify_launch_hid)
        self._notify_launch_hid = None
        if home_activity.props.launch_status == shell.Activity.LAUNCH_FAILED:
            self._on_failed_launch()
        else:
            self._icon.props.pulsing = False


class InviteButton(ToolButton):
    """Invite to shared activity"""

    __gsignals__ = {
        'remove-invite': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, ([])),
    }

    def __init__(self, invite):
        ToolButton.__init__(self)

        self._invite = invite

        self.connect('clicked', self.__clicked_cb)
        self.connect('destroy', self.__destroy_cb)

        bundle_registry = bundleregistry.get_registry()
        bundle = bundle_registry.get_bundle(invite.get_bundle_id())

        self._icon = Icon()
        self._icon.props.xo_color = invite.get_color()
        if bundle is not None:
            self._icon.props.file = bundle.get_icon()
        else:
            self._icon.props.icon_name = 'image-missing'
        self.set_icon_widget(self._icon)
        self._icon.show()

        palette = InvitePalette(invite)
        palette.props.invoker = FrameWidgetInvoker(self)
        palette.set_group_id('frame')
        palette.connect('remove-invite', self.__remove_invite_cb)
        self.set_palette(palette)

        self._notif_icon = NotificationIcon()
        self._notif_icon.connect('button-release-event',
                                 self.__button_release_event_cb)

        self._notif_icon.props.xo_color = invite.get_color()
        if bundle is not None:
            self._notif_icon.props.icon_filename = bundle.get_icon()
        else:
            self._notif_icon.props.icon_name = 'image-missing'

        frame = jarabe.frame.get_view()
        frame.add_notification(self._notif_icon, gtk.CORNER_TOP_LEFT)

    def __button_release_event_cb(self, icon, event):
        if self._notif_icon is not None:
            frame = jarabe.frame.get_view()
            frame.remove_notification(self._notif_icon)
            self._notif_icon = None
            self._invite.join()
            self.emit('remove-invite')

    def __clicked_cb(self, button):
        self.palette.popup(immediate=True, state=Palette.SECONDARY)

    def __remove_invite_cb(self, palette):
        self.emit('remove-invite')

    def __destroy_cb(self, button):
        if self._notif_icon is not None:
            frame = jarabe.frame.get_view()
            frame.remove_notification(self._notif_icon)
            self._notif_icon = None


class InvitePalette(Palette):
    """Palette for frame or notification icon for invites."""

    __gsignals__ = {
        'remove-invite': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, ([])),
    }

    def __init__(self, invite):
        Palette.__init__(self, '')

        self._invite = invite

        menu_item = MenuItem(_('Join'), icon_name='dialog-ok')
        menu_item.connect('activate', self.__join_activate_cb)
        self.menu.append(menu_item)
        menu_item.show()

        menu_item = MenuItem(_('Decline'), icon_name='dialog-cancel')
        menu_item.connect('activate', self.__decline_activate_cb)
        self.menu.append(menu_item)
        menu_item.show()

        bundle_id = invite.get_bundle_id()

        registry = bundleregistry.get_registry()
        self._bundle = registry.get_bundle(bundle_id)
        if self._bundle:
            name = self._bundle.get_name()
        else:
            name = bundle_id

        self.set_primary_text(glib.markup_escape_text(name))

    def __join_activate_cb(self, menu_item):
        self._invite.join()
        self.emit('remove-invite')

    def __decline_activate_cb(self, menu_item):
        self.emit('remove-invite')


class ActivitiesTray(HTray):
    def __init__(self):
        HTray.__init__(self)

        self._buttons = {}
        self._invite_to_item = {}
        self._freeze_button_clicks = False

        self._home_model = shell.get_model()
        self._home_model.connect('activity-added', self.__activity_added_cb)
        self._home_model.connect('activity-removed',
                                 self.__activity_removed_cb)
        self._home_model.connect('active-activity-changed',
                                 self.__activity_changed_cb)
        self._home_model.connect('tabbing-activity-changed',
                                 self.__tabbing_activity_changed_cb)

        self._invites = invites.get_instance()
        for invite in self._invites:
            self._add_invite(invite)
        self._invites.connect('invite-added', self.__invite_added_cb)
        self._invites.connect('invite-removed', self.__invite_removed_cb)

        filetransfer.new_file_transfer.connect(self.__new_file_transfer_cb)

    def __activity_added_cb(self, home_model, home_activity):
        logging.debug('__activity_added_cb: %r', home_activity)
        if self.get_children():
            group = self.get_children()[0]
        else:
            group = None

        button = ActivityButton(home_activity, group)
        self.add_item(button)
        self._buttons[home_activity] = button
        button.connect('clicked', self.__activity_clicked_cb, home_activity)
        button.show()

    def __activity_removed_cb(self, home_model, home_activity):
        logging.debug('__activity_removed_cb: %r', home_activity)
        button = self._buttons[home_activity]
        self.remove_item(button)
        del self._buttons[home_activity]

    def _activate_activity(self, home_activity):
        button = self._buttons[home_activity]
        self._freeze_button_clicks = True
        button.props.active = True
        self._freeze_button_clicks = False

        self.scroll_to_item(button)
        # Redraw immediately.
        # The widget may not be realized yet, and then there is no window.
        if self.window:
            self.window.process_updates(True)

    def __activity_changed_cb(self, home_model, home_activity):
        logging.debug('__activity_changed_cb: %r', home_activity)

        # Only select the new activity, if there is no tabbing activity.
        if home_model.get_tabbing_activity() is None:
            self._activate_activity(home_activity)

    def __tabbing_activity_changed_cb(self, home_model, home_activity):
        logging.debug('__tabbing_activity_changed_cb: %r', home_activity)
        # If the tabbing_activity is set to None just do nothing.
        # The active activity will be updated a bit later (and it will
        # be set to the activity that is currently selected).
        if home_activity is None:
            return

        self._activate_activity(home_activity)

    def __activity_clicked_cb(self, button, home_activity):
        if not self._freeze_button_clicks and button.props.active:
            logging.debug('ActivitiesTray.__activity_clicked_cb')
            window = home_activity.get_window()
            if window:
                window.activate(gtk.get_current_event_time())

    def __remove_invite_cb(self, icon, invite):
        self._invites.remove_invite(invite)

    def __invite_added_cb(self, invites_model, invite):
        self._add_invite(invite)

    def __invite_removed_cb(self, invites_model, invite):
        self._remove_invite(invite)

    def _add_invite(self, invite):
        """Add an invite"""
        item = InviteButton(invite)
        item.connect('remove-invite', self.__remove_invite_cb, invite)
        self.add_item(item)
        item.show()
        self._invite_to_item[invite] = item

    def _remove_invite(self, invite):
        self.remove_item(self._invite_to_item[invite])
        self._invite_to_item[invite].destroy()
        del self._invite_to_item[invite]

    def __new_file_transfer_cb(self, **kwargs):
        file_transfer = kwargs['file_transfer']
        logging.debug('__new_file_transfer_cb %r', file_transfer)

        if isinstance(file_transfer, filetransfer.IncomingFileTransfer):
            button = IncomingTransferButton(file_transfer)
        elif isinstance(file_transfer, filetransfer.OutgoingFileTransfer):
            button = OutgoingTransferButton(file_transfer)

        self.add_item(button)
        button.show()


class BaseTransferButton(ToolButton):
    """Button with a notification attached
    """
    def __init__(self, file_transfer):
        ToolButton.__init__(self)

        self.file_transfer = file_transfer
        file_transfer.connect('notify::state', self.__notify_state_cb)

        icon = Icon()
        self.props.icon_widget = icon
        icon.show()

        self.notif_icon = NotificationIcon()
        self.notif_icon.connect('button-release-event',
                                 self.__button_release_event_cb)

        self.connect('clicked', self.__button_clicked_cb)

    def __button_release_event_cb(self, icon, event):
        if self.notif_icon is not None:
            frame = jarabe.frame.get_view()
            frame.remove_notification(self.notif_icon)
            self.notif_icon = None

    def __button_clicked_cb(self, button):
        self.palette.popup(immediate=True, state=Palette.SECONDARY)

    def remove(self):
        frame = jarabe.frame.get_view()
        frame.remove_notification(self.notif_icon)
        self.props.parent.remove(self)

    def __notify_state_cb(self, file_transfer, pspec):
        logging.debug('_update state: %r %r', file_transfer.props.state,
                file_transfer.reason_last_change)
        if file_transfer.props.state == filetransfer.FT_STATE_CANCELLED:
            if file_transfer.reason_last_change == \
               filetransfer.FT_REASON_LOCAL_STOPPED:
                self.remove()


class IncomingTransferButton(BaseTransferButton):
    """UI element representing an ongoing incoming file transfer
    """
    def __init__(self, file_transfer):
        BaseTransferButton.__init__(self, file_transfer)

        self._ds_object = datastore.create()

        file_transfer.connect('notify::state', self.__notify_state_cb)
        file_transfer.connect('notify::transferred-bytes',
                              self.__notify_transferred_bytes_cb)

        icons = gio.content_type_get_icon(file_transfer.mime_type).props.names
        icons.append('application-octet-stream')
        for icon_name in icons:
            icon_name = 'transfer-from-%s' % icon_name
            file_name = get_icon_file_name(icon_name)
            if file_name is not None:
                self.props.icon_widget.props.icon_name = icon_name
                self.notif_icon.props.icon_name = icon_name
                break

        icon_color = file_transfer.buddy.props.color
        self.props.icon_widget.props.xo_color = icon_color
        self.notif_icon.props.xo_color = icon_color

        frame = jarabe.frame.get_view()
        frame.add_notification(self.notif_icon,
                               gtk.CORNER_TOP_LEFT)

    def create_palette(self):
        palette = IncomingTransferPalette(self.file_transfer)
        palette.connect('dismiss-clicked', self.__dismiss_clicked_cb)
        palette.props.invoker = FrameWidgetInvoker(self)
        palette.set_group_id('frame')
        return palette

    def __notify_state_cb(self, file_transfer, pspec):
        if file_transfer.props.state == filetransfer.FT_STATE_OPEN:
            logging.debug('__notify_state_cb OPEN')
            self._ds_object.metadata['title'] = file_transfer.title
            self._ds_object.metadata['description'] = file_transfer.description
            self._ds_object.metadata['progress'] = '0'
            self._ds_object.metadata['keep'] = '0'
            self._ds_object.metadata['buddies'] = ''
            self._ds_object.metadata['preview'] = ''
            self._ds_object.metadata['icon-color'] = \
                    file_transfer.buddy.props.color.to_string()
            self._ds_object.metadata['mime_type'] = file_transfer.mime_type
        elif file_transfer.props.state == filetransfer.FT_STATE_COMPLETED:
            logging.debug('__notify_state_cb COMPLETED')
            self._ds_object.metadata['progress'] = '100'
            self._ds_object.file_path = file_transfer.destination_path
            datastore.write(self._ds_object, transfer_ownership=True,
                            reply_handler=self.__reply_handler_cb,
                            error_handler=self.__error_handler_cb)
        elif file_transfer.props.state == filetransfer.FT_STATE_CANCELLED:
            logging.debug('__notify_state_cb CANCELLED')
            object_id = self._ds_object.object_id
            if object_id is not None:
                self._ds_object.destroy()
                datastore.delete(object_id)
                self._ds_object = None

    def __notify_transferred_bytes_cb(self, file_transfer, pspec):
        progress = file_transfer.props.transferred_bytes /      \
                   file_transfer.file_size
        self._ds_object.metadata['progress'] = str(progress * 100)
        datastore.write(self._ds_object, update_mtime=False)

    def __reply_handler_cb(self):
        logging.debug('__reply_handler_cb %r', self._ds_object.object_id)

    def __error_handler_cb(self, error):
        logging.debug('__error_handler_cb %r %s', self._ds_object.object_id,
                      error)

    def __dismiss_clicked_cb(self, palette):
        self.remove()


class OutgoingTransferButton(BaseTransferButton):
    """UI element representing an ongoing outgoing file transfer
    """
    def __init__(self, file_transfer):
        BaseTransferButton.__init__(self, file_transfer)

        icons = gio.content_type_get_icon(file_transfer.mime_type).props.names
        icons.append('application-octet-stream')
        for icon_name in icons:
            icon_name = 'transfer-to-%s' % icon_name
            file_name = get_icon_file_name(icon_name)
            if file_name is not None:
                self.props.icon_widget.props.icon_name = icon_name
                self.notif_icon.props.icon_name = icon_name
                break

        client = gconf.client_get_default()
        icon_color = XoColor(client.get_string('/desktop/sugar/user/color'))
        self.props.icon_widget.props.xo_color = icon_color
        self.notif_icon.props.xo_color = icon_color

        frame = jarabe.frame.get_view()
        frame.add_notification(self.notif_icon,
                               gtk.CORNER_TOP_LEFT)

    def create_palette(self):
        palette = OutgoingTransferPalette(self.file_transfer)
        palette.connect('dismiss-clicked', self.__dismiss_clicked_cb)
        palette.props.invoker = FrameWidgetInvoker(self)
        palette.set_group_id('frame')
        return palette

    def __dismiss_clicked_cb(self, palette):
        self.remove()


class BaseTransferPalette(Palette):
    """Base palette class for frame or notification icon for file transfers
    """
    __gtype_name__ = 'SugarBaseTransferPalette'

    __gsignals__ = {
        'dismiss-clicked': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, ([])),
    }

    def __init__(self, file_transfer):
        Palette.__init__(self, glib.markup_escape_text(file_transfer.title))

        self.file_transfer = file_transfer

        self.progress_bar = None
        self.progress_label = None
        self._notify_transferred_bytes_handler = None

        self.connect('popup', self.__popup_cb)
        self.connect('popdown', self.__popdown_cb)

    def __popup_cb(self, palette):
        self.update_progress()
        self._notify_transferred_bytes_handler = \
                self.file_transfer.connect('notify::transferred_bytes',
                                            self.__notify_transferred_bytes_cb)

    def __popdown_cb(self, palette):
        if self._notify_transferred_bytes_handler is not None:
            self.file_transfer.disconnect(
                    self._notify_transferred_bytes_handler)
            self._notify_transferred_bytes_handler = None

    def __notify_transferred_bytes_cb(self, file_transfer, pspec):
        self.update_progress()

    def _format_size(self, size):
        if size < 1024:
            return _('%dB') % size
        elif size < 1048576:
            return _('%dKB') % (size / 1024)
        else:
            return _('%dMB') % (size / 1048576)

    def update_progress(self):
        logging.debug('update_progress: %r',
                      self.file_transfer.props.transferred_bytes)

        if self.progress_bar is None:
            return

        self.progress_bar.props.fraction = \
                self.file_transfer.props.transferred_bytes / \
                float(self.file_transfer.file_size)
        logging.debug('update_progress: %r', self.progress_bar.props.fraction)

        transferred = self._format_size(
                self.file_transfer.props.transferred_bytes)
        total = self._format_size(self.file_transfer.file_size)
        # TRANS: file transfer, bytes transferred, e.g. 128 of 1024
        self.progress_label.props.label = _('%s of %s') % (transferred, total)


class IncomingTransferPalette(BaseTransferPalette):
    """Palette for frame or notification icon for incoming file transfers
    """
    __gtype_name__ = 'SugarIncomingTransferPalette'

    def __init__(self, file_transfer):
        BaseTransferPalette.__init__(self, file_transfer)

        self.file_transfer.connect('notify::state', self.__notify_state_cb)

        nick = str(self.file_transfer.buddy.props.nick)
        label = glib.markup_escape_text(_('Transfer from %s') % (nick,))
        self.props.secondary_text = label

        self._update()

    def __notify_state_cb(self, file_transfer, pspec):
        self._update()

    def _update(self):
        logging.debug('_update state: %r', self.file_transfer.props.state)
        if self.file_transfer.props.state == filetransfer.FT_STATE_PENDING:
            menu_item = MenuItem(_('Accept'), icon_name='dialog-ok')
            menu_item.connect('activate', self.__accept_activate_cb)
            self.menu.append(menu_item)
            menu_item.show()

            menu_item = MenuItem(_('Decline'), icon_name='dialog-cancel')
            menu_item.connect('activate', self.__decline_activate_cb)
            self.menu.append(menu_item)
            menu_item.show()

            vbox = gtk.VBox()
            self.set_content(vbox)
            vbox.show()

            if self.file_transfer.description:
                label = gtk.Label(self.file_transfer.description)
                vbox.add(label)
                label.show()

            mime_type = self.file_transfer.mime_type
            type_description = mime.get_mime_description(mime_type)

            size = self._format_size(self.file_transfer.file_size)
            label = gtk.Label('%s (%s)' % (size, type_description))
            vbox.add(label)
            label.show()

        elif self.file_transfer.props.state in \
                [filetransfer.FT_STATE_ACCEPTED, filetransfer.FT_STATE_OPEN]:

            for item in self.menu.get_children():
                self.menu.remove(item)

            menu_item = MenuItem(_('Cancel'), icon_name='dialog-cancel')
            menu_item.connect('activate', self.__cancel_activate_cb)
            self.menu.append(menu_item)
            menu_item.show()

            vbox = gtk.VBox()
            self.set_content(vbox)
            vbox.show()

            self.progress_bar = gtk.ProgressBar()
            vbox.add(self.progress_bar)
            self.progress_bar.show()

            self.progress_label = gtk.Label('')
            vbox.add(self.progress_label)
            self.progress_label.show()

            self.update_progress()

        elif self.file_transfer.props.state == filetransfer.FT_STATE_COMPLETED:

            for item in self.menu.get_children():
                self.menu.remove(item)

            menu_item = MenuItem(_('Dismiss'), icon_name='dialog-cancel')
            menu_item.connect('activate', self.__dismiss_activate_cb)
            self.menu.append(menu_item)
            menu_item.show()

            self.update_progress()
        elif self.file_transfer.props.state == filetransfer.FT_STATE_CANCELLED:

            for item in self.menu.get_children():
                self.menu.remove(item)

            if self.file_transfer.reason_last_change == \
                    filetransfer.FT_REASON_REMOTE_STOPPED:
                menu_item = MenuItem(_('Dismiss'), icon_name='dialog-cancel')
                menu_item.connect('activate', self.__dismiss_activate_cb)
                self.menu.append(menu_item)
                menu_item.show()
                text = _('The other participant canceled the file transfer')
                label = gtk.Label(text)
                self.set_content(label)
                label.show()

    def __accept_activate_cb(self, menu_item):
        #TODO: figure out the best place to get rid of that temp file
        extension = mime.get_primary_extension(self.file_transfer.mime_type)
        if extension is None:
            extension = '.bin'
        fd, file_path = tempfile.mkstemp(suffix=extension,
                prefix=self._sanitize(self.file_transfer.title),
                dir=os.path.join(env.get_profile_path(), 'data'))
        os.close(fd)
        os.unlink(file_path)

        self.file_transfer.accept(file_path)

    def _sanitize(self, file_name):
        file_name = file_name.replace('/', '_')
        file_name = file_name.replace('.', '_')
        file_name = file_name.replace('?', '_')
        return file_name

    def __decline_activate_cb(self, menu_item):
        self.file_transfer.cancel()

    def __cancel_activate_cb(self, menu_item):
        self.file_transfer.cancel()

    def __dismiss_activate_cb(self, menu_item):
        self.emit('dismiss-clicked')


class OutgoingTransferPalette(BaseTransferPalette):
    """Palette for frame or notification icon for outgoing file transfers
    """
    __gtype_name__ = 'SugarOutgoingTransferPalette'

    def __init__(self, file_transfer):
        BaseTransferPalette.__init__(self, file_transfer)

        self.progress_bar = None
        self.progress_label = None

        self.file_transfer.connect('notify::state', self.__notify_state_cb)

        nick = str(file_transfer.buddy.props.nick)
        label = glib.markup_escape_text(_('Transfer to %s') % (nick,))
        self.props.secondary_text = label

        self._update()

    def __notify_state_cb(self, file_transfer, pspec):
        self._update()

    def _update(self):
        new_state = self.file_transfer.props.state
        logging.debug('_update state: %r', new_state)
        if new_state == filetransfer.FT_STATE_PENDING:

            menu_item = MenuItem(_('Cancel'), icon_name='dialog-cancel')
            menu_item.connect('activate', self.__cancel_activate_cb)
            self.menu.append(menu_item)
            menu_item.show()

            vbox = gtk.VBox()
            self.set_content(vbox)
            vbox.show()

            if self.file_transfer.description:
                label = gtk.Label(self.file_transfer.description)
                vbox.add(label)
                label.show()

            mime_type = self.file_transfer.mime_type
            type_description = mime.get_mime_description(mime_type)

            size = self._format_size(self.file_transfer.file_size)
            label = gtk.Label('%s (%s)' % (size, type_description))
            vbox.add(label)
            label.show()

        elif new_state in [filetransfer.FT_STATE_ACCEPTED,
                           filetransfer.FT_STATE_OPEN]:

            for item in self.menu.get_children():
                self.menu.remove(item)

            menu_item = MenuItem(_('Cancel'), icon_name='dialog-cancel')
            menu_item.connect('activate', self.__cancel_activate_cb)
            self.menu.append(menu_item)
            menu_item.show()

            vbox = gtk.VBox()
            self.set_content(vbox)
            vbox.show()

            self.progress_bar = gtk.ProgressBar()
            vbox.add(self.progress_bar)
            self.progress_bar.show()

            self.progress_label = gtk.Label('')
            vbox.add(self.progress_label)
            self.progress_label.show()

            self.update_progress()

        elif new_state in [filetransfer.FT_STATE_COMPLETED,
                           filetransfer.FT_STATE_CANCELLED]:

            for item in self.menu.get_children():
                self.menu.remove(item)

            menu_item = MenuItem(_('Dismiss'), icon_name='dialog-cancel')
            menu_item.connect('activate', self.__dismiss_activate_cb)
            self.menu.append(menu_item)
            menu_item.show()

            self.update_progress()

    def __cancel_activate_cb(self, menu_item):
        self.file_transfer.cancel()

    def __dismiss_activate_cb(self, menu_item):
        self.emit('dismiss-clicked')