2
# -*- coding: utf-8 -*-
3
# nautilus-pastebin - Nautilus extension to paste a file to a pastebin service
5
# Alessio Treglia <quadrispro@ubuntu.com>
6
# Copyright (C) 2009, Alessio Treglia
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.
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.
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
23
MAINTAINER_MODE = False
32
sys.path.append(os.path.expanduser(os.path.join("~",".nautilus-pastebin")))
36
from ConfigParser import ConfigParser
37
from threading import Thread
38
from pastebin.core import *
40
gettext.install("nautilus-pastebin")
43
NAME_APP = "nautilus-pastebin"
45
class ConfigFileException(Exception):
48
pynotify.init("Nautilus pastebin extension")
50
class GlobalConfigurationValidator(GlobalConfigurationValidator):
51
NEEDED_OPTS = GlobalConfigurationValidator.NEEDED_OPTS.extend(['enable_confirmation', 'enable_notification','enable_open_browser'])
55
"""Log almost whatever happens."""
57
if sys.version.split()[0] < '3':
62
class ConfirmDialog(gtk.MessageDialog):
63
"""Ask user to confirm before submitting the file"""
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)
74
def _handle_click(self, widget, data):
75
self.confirm = (data == gtk.RESPONSE_YES)
77
def get_confirm(self):
80
class PastebinThread(Thread):
81
def __init__(self, pasteconf):
82
self.pasteconf = pasteconf
83
self.paster = Paster(pasteconf)
87
log ("PastebinThread started!")
89
# Ask user to confirm the action
90
if self.pasteconf.enable_confirmation:
93
if not md.get_confirm():
95
pasteurl = self.paster.paste()
100
helper = gtk.Button()
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)
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)
112
cb = gtk.clipboard_get('CLIPBOARD')
114
cb.set_text(pasteurl)
116
# Open a browser window
117
if self.pasteconf.enable_open_browser:
118
webbrowser.open(pasteurl)
121
if self.pasteconf.enable_notification:
122
n = pynotify.Notification(summary, message)
123
n.set_icon_from_pixbuf(icon)
126
class PastebinitExtension(nautilus.MenuProvider):
128
log ("Intializing nautilus-pastebin extension...")
130
def get_file_items(self, window, files):
135
if filename.get_uri_scheme() != 'file' or filename.is_directory():
139
#Called when the user selects a file in Nautilus.
140
item = nautilus.MenuItem("NautilusPython::pastebin_item",
142
_("Send this file to a pastebin"))
143
item.set_property('icon', "nautilus-pastebin")
144
item.connect("activate", self.menu_activate_cb, files)
148
def menu_activate_cb(self, menu, files):
153
validator = GlobalConfigurationValidator()
155
pasteconf = validator.getPastebinConfiguration()
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)
165
pastefile = urllib.unquote(filename.get_uri()[7:])
166
if not pasteconf.setContent(pastefile):
169
thread = PastebinThread(pasteconf)
171
while (thread.isAlive()):
173
while gtk.events_pending():