~ubuntu-branches/ubuntu/vivid/nautilus-pastebin/vivid

« back to all changes in this revision

Viewing changes to scripts/nautilus-pastebin.py

  • Committer: Bazaar Package Importer
  • Author(s): Alessio Treglia
  • Date: 2009-10-02 14:46:39 UTC
  • Revision ID: james.westby@ubuntu.com-20091002144639-9qh33a65nbox0deq
Tags: upstream-0.2.1
ImportĀ upstreamĀ versionĀ 0.2.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
# -*- coding: utf-8 -*-
 
3
# nautilus-pastebin - Nautilus extension to paste a file to a pastebin service
 
4
# Written by:
 
5
#    Alessio Treglia <quadrispro@ubuntu.com>
 
6
# Copyright (C) 2009, Alessio Treglia
 
7
#
 
8
# This package is free software; you can redistribute it and/or modify
 
9
# it under the terms of the GNU General Public License as published by
 
10
# the Free Software Foundation; either version 2 of the License, or
 
11
# (at your option) any later version.
 
12
#
 
13
# This package is distributed in the hope that it will be useful,
 
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
# GNU General Public License for more details.
 
17
#
 
18
# You should have received a copy of the GNU General Public License
 
19
# along with this package; if not, write to the Free Software
 
20
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 
21
#
 
22
 
 
23
MAINTAINER_MODE = False
 
24
LOGGING = True
 
25
 
 
26
import urllib
 
27
import gtk
 
28
import nautilus
 
29
import pynotify
 
30
import os, sys
 
31
if MAINTAINER_MODE:
 
32
    sys.path.append(os.path.expanduser(os.path.join("~",".nautilus-pastebin")))
 
33
import time
 
34
import gettext
 
35
import webbrowser
 
36
from ConfigParser import ConfigParser
 
37
from threading import Thread
 
38
from pastebin.core import *
 
39
 
 
40
gettext.install("nautilus-pastebin")
 
41
 
 
42
# Globals
 
43
NAME_APP = "nautilus-pastebin"
 
44
 
 
45
class ConfigFileException(Exception):
 
46
    pass
 
47
 
 
48
pynotify.init("Nautilus pastebin extension")
 
49
 
 
50
class GlobalConfigurationValidator(GlobalConfigurationValidator):
 
51
    NEEDED_OPTS = GlobalConfigurationValidator.NEEDED_OPTS.extend(['enable_confirmation', 'enable_notification','enable_open_browser'])
 
52
    pass
 
53
 
 
54
def log(message):
 
55
    """Log almost whatever happens."""
 
56
    if LOGGING:
 
57
        if sys.version.split()[0] < '3':
 
58
            print message
 
59
        else:
 
60
            print (message)
 
61
 
 
62
class ConfirmDialog(gtk.MessageDialog):
 
63
    """Ask user to confirm before submitting the file"""
 
64
    def __init__(self):
 
65
        gtk.MessageDialog.__init__(self,
 
66
            message_format=_("The file will be sent to the selected pastebin, \
 
67
are you sure to continue?"),
 
68
            flags=gtk.DIALOG_MODAL,
 
69
            buttons=gtk.BUTTONS_YES_NO,
 
70
            type=gtk.MESSAGE_QUESTION)
 
71
        self.set_default_response(gtk.RESPONSE_NO)
 
72
        self.connect('response', self._handle_click)
 
73
        self.confirm = False
 
74
    def _handle_click(self, widget, data):
 
75
        self.confirm = (data == gtk.RESPONSE_YES)
 
76
        self.destroy()
 
77
    def get_confirm(self):
 
78
        return self.confirm
 
79
 
 
80
class PastebinThread(Thread):
 
81
    def __init__(self, pasteconf):
 
82
        self.pasteconf = pasteconf
 
83
        self.paster = Paster(pasteconf)
 
84
        Thread.__init__(self)
 
85
 
 
86
    def run(self):
 
87
        log ("PastebinThread started!")
 
88
 
 
89
        # Ask user to confirm the action
 
90
        if self.pasteconf.enable_confirmation:
 
91
            md = ConfirmDialog()
 
92
            md.run()
 
93
            if not md.get_confirm():
 
94
                return
 
95
        pasteurl = self.paster.paste()
 
96
        
 
97
        summary = ''
 
98
        message = ''
 
99
        icon = None
 
100
        helper = gtk.Button()
 
101
        
 
102
        if not pasteurl:
 
103
            summary = _("Unable to read or parse the result page")
 
104
            message = _("It could be a server timeout or a change server side, try later.")
 
105
            icon = helper.render_icon(gtk.STOCK_DIALOG_ERROR, gtk.ICON_SIZE_DIALOG)
 
106
        else:
 
107
            summary = 'File pasted to: '
 
108
            URLmarkup = '<a href="%(pasteurl)s">%(pasteurl)s</a>'
 
109
            message = URLmarkup % {'pasteurl':pasteurl}
 
110
            icon = helper.render_icon(gtk.STOCK_PASTE, gtk.ICON_SIZE_DIALOG)
 
111
            
 
112
            cb = gtk.clipboard_get('CLIPBOARD')
 
113
            cb.clear()
 
114
            cb.set_text(pasteurl)
 
115
            
 
116
            # Open a browser window
 
117
            if self.pasteconf.enable_open_browser:
 
118
                webbrowser.open(pasteurl)
 
119
            
 
120
        # Show a bubble
 
121
        if self.pasteconf.enable_notification:
 
122
            n = pynotify.Notification(summary, message)
 
123
            n.set_icon_from_pixbuf(icon)
 
124
            n.show()
 
125
 
 
126
class PastebinitExtension(nautilus.MenuProvider):
 
127
    def __init__(self):
 
128
        log ("Intializing nautilus-pastebin extension...")
 
129
    
 
130
    def get_file_items(self, window, files):
 
131
        if len(files)!=1:
 
132
            return
 
133
        filename = files[0]
 
134
 
 
135
        if filename.get_uri_scheme() != 'file' or filename.is_directory():
 
136
            return
 
137
 
 
138
        items = []
 
139
        #Called when the user selects a file in Nautilus.
 
140
        item = nautilus.MenuItem("NautilusPython::pastebin_item",
 
141
                                 _("Pastebin"),
 
142
                                 _("Send this file to a pastebin"))
 
143
        item.set_property('icon', "nautilus-pastebin")
 
144
        item.connect("activate", self.menu_activate_cb, files)
 
145
        items.append(item)
 
146
        return items
 
147
 
 
148
    def menu_activate_cb(self, menu, files):
 
149
        if len(files) != 1:
 
150
            return
 
151
        filename = files[0]
 
152
 
 
153
        validator = GlobalConfigurationValidator()
 
154
        validator.validate()
 
155
        pasteconf = validator.getPastebinConfiguration()
 
156
 
 
157
        if not pasteconf.setFormatByMimeType(filename.get_mime_type()):
 
158
            n= pynotify.Notification(_("Unable to send the file"), _("This file type cannot be pasted."))
 
159
            helper = gtk.Button()
 
160
            icon = helper.render_icon(gtk.STOCK_DIALOG_ERROR, gtk.ICON_SIZE_DIALOG)
 
161
            n.set_icon_from_pixbuf(icon)
 
162
            n.show()
 
163
            return
 
164
 
 
165
        pastefile = urllib.unquote(filename.get_uri()[7:])
 
166
        if not pasteconf.setContent(pastefile):
 
167
            return
 
168
        
 
169
        thread = PastebinThread(pasteconf)
 
170
        thread.start()
 
171
        while (thread.isAlive()):
 
172
            time.sleep(0.09)
 
173
            while gtk.events_pending():
 
174
                gtk.main_iteration()
 
175