File: Screenshots.py

package info (click to toggle)
emesene 1.0-dist-4
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 4,596 kB
  • ctags: 3,006
  • sloc: python: 25,171; makefile: 14; sh: 1
file content (135 lines) | stat: -rw-r--r-- 4,629 bytes parent folder | download
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
# -*- coding: utf-8 -*-

#   This file is part of emesene.
#
#    Emesene 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.
#
#    emesene 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 emesene; if not, write to the Free Software
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

import os
import gtk
import tempfile
import threading
import time

import Plugin

class MainClass(Plugin.Plugin):
    '''Main plugin class'''

    def __init__( self, controller, msn ):
        '''Constructor'''

        Plugin.Plugin.__init__( self, controller, msn )
        self.description = _( 'Take screenshots and send them with ' +
                    '/screenshot [save] [<seconds>]' )
        self.authors = { 'Dx' : 'dx@dxzone.com.ar' }
        self.website = 'http://www.dxzone.com.ar'
        self.displayName = 'Screenshots'
        self.name = 'Screenshots'
        
        self.config = controller.config
        self.config.readPluginConfig(self.name)

        self.controller = controller
        self.slash = controller.Slash
        self.msn = msn
        
        if os.name == 'posix':
            self.root = controller.mainWindow.get_root_window()
        else:
            self.root = None

    def start( self ):
        '''start the plugin'''
        self.slash.register('screenshot', self.screenshot, \
            _('Takes screenshots') )

        self.enabled = True

    def screenshot( self, slashAction ):
        params = slashAction.getParams().split()
        
        save = False
        delay = 0
        
        # parse parameters
        for param in params:
            if param == 'save':
                save = True
            elif param.isdigit():
                delay = int(param)
        
        if delay > 0:
            slashAction.outputText(_('Taking screenshot in %s seconds') % delay)
        
        if not save:
            if self.config.getPluginValue(self.name, 'tip', '0') == '0':
                slashAction.outputText( \
                    _('Tip: Use "/screenshot save <seconds>" to skip the upload '))
                self.config.setPluginValue(self.name, 'tip', '1')
        
        ScreenThread( slashAction, self, save=save, delay=delay ).start()

    def stop( self ):    
        '''stop the plugin'''
        self.slash.unregister('screenshot')
        self.enabled = False

    def check( self ):
        return ( True, 'Ok' )

class ScreenThread(threading.Thread):

    def __init__(self, action, plugin, save=False, delay=0):
        threading.Thread.__init__( self )
        self.slashAction = action
        self.plugin = plugin
        self.save = save
        self.sleepTime = int(delay)

    def run(self):
        print "sleeping: " + str(self.sleepTime)
        time.sleep(self.sleepTime)
        if os.name == 'nt':
            print 'screenshot on windows never tested, check source!'

            # yes, i had to break this link.. 80 cols :P
            ## mail.python.org/pipermail/python-win32/
            ## .../2003-January/000709.html

            # import win32api, win32clipboard
            # win32api.keybd_event(win32con.VK_PRINT, 0)
            # win32clipboard.OpenClipboard(0)
            # data = win32clipboard.GetClipboardData(win32clipboard.CF_DIB)
            # win32clipboard.CloseClipboard()
        elif os.name == 'posix':
            temp = tempfile.mkstemp(prefix='screenshot', suffix='.png')
            os.close(temp[0])
            
            # überfast screenshots! 1.9secs in my pentium II 350mhz
            # er.. yes, that's fast.. try running import
            root = self.plugin.root
            size = root.get_size()
            colormap = root.get_colormap()
            pixmap = gtk.gdk.pixmap_foreign_new(root.xid)
            pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, 0, 8, *size)
            pixbuf.get_from_drawable(pixmap, colormap, 0, 0, 0, 0, *size)
            pixbuf.save(temp[1], 'png')
            
        if not self.save:
            self.slashAction.conversation.sendFile(temp[1])
        else:
            self.slashAction.outputText( _('Temporary file:') + ' ' + \
                temp[1] )