Logo Search packages:      
Sourcecode: nautilus-pastebin version File versions

nautilus-pastebin.py

#!/usr/bin/python
# -*- coding: utf-8 -*-
# nautilus-pastebin - Nautilus extension to paste a file to a pastebin service
# Written by:
#    Alessio Treglia <quadrispro@ubuntu.com>
# Copyright (C) 2009, Alessio Treglia
#
# This package 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 package 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 package; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
#

MAINTAINER_MODE = False
LOGGING = True

import urllib
import gtk
import nautilus
import pynotify
import os, sys
if MAINTAINER_MODE:
    sys.path.append(os.path.expanduser(os.path.join("~",".nautilus-pastebin")))
import time
import gettext
import webbrowser
from ConfigParser import ConfigParser
from threading import Thread
from pastebin.core import *

gettext.install("nautilus-pastebin")

# Globals
NAME_APP = "nautilus-pastebin"

class ConfigFileException(Exception):
    pass

pynotify.init("Nautilus pastebin extension")

class GlobalConfigurationValidator(GlobalConfigurationValidator):
    NEEDED_OPTS = GlobalConfigurationValidator.NEEDED_OPTS.extend(['enable_confirmation', 'enable_notification','enable_open_browser'])
    pass

def log(message):
    """Log almost whatever happens."""
    if LOGGING:
        if sys.version.split()[0] < '3':
            print message
        else:
            print (message)

00062 class ConfirmDialog(gtk.MessageDialog):
    """Ask user to confirm before submitting the file"""
    def __init__(self):
        gtk.MessageDialog.__init__(self,
            message_format=_("The file will be sent to the selected pastebin, \
are you sure to continue?"),
            flags=gtk.DIALOG_MODAL,
            buttons=gtk.BUTTONS_YES_NO,
            type=gtk.MESSAGE_QUESTION)
        self.set_default_response(gtk.RESPONSE_NO)
        self.connect('response', self._handle_click)
        self.confirm = False
    def _handle_click(self, widget, data):
        self.confirm = (data == gtk.RESPONSE_YES)
        self.destroy()
    def get_confirm(self):
        return self.confirm

class PastebinThread(Thread):
    def __init__(self, pasteconf):
        self.pasteconf = pasteconf
        self.paster = Paster(pasteconf)
        Thread.__init__(self)

    def run(self):
        log ("PastebinThread started!")

        # Ask user to confirm the action
        if self.pasteconf.enable_confirmation:
            md = ConfirmDialog()
            md.run()
            if not md.get_confirm():
                return
        pasteurl = self.paster.paste()
        
        summary = ''
        message = ''
        icon = None
        helper = gtk.Button()
        
        if not pasteurl:
            summary = _("Unable to read or parse the result page")
            message = _("It could be a server timeout or a change server side, try later.")
            icon = helper.render_icon(gtk.STOCK_DIALOG_ERROR, gtk.ICON_SIZE_DIALOG)
        else:
            summary = 'File pasted to: '
            URLmarkup = '<a href="%(pasteurl)s">%(pasteurl)s</a>'
            message = URLmarkup % {'pasteurl':pasteurl}
            icon = helper.render_icon(gtk.STOCK_PASTE, gtk.ICON_SIZE_DIALOG)
            
            cb = gtk.clipboard_get('CLIPBOARD')
            cb.clear()
            cb.set_text(pasteurl)
            
            # Open a browser window
            if self.pasteconf.enable_open_browser:
                webbrowser.open(pasteurl)
            
        # Show a bubble
        if self.pasteconf.enable_notification:
            n = pynotify.Notification(summary, message)
            n.set_icon_from_pixbuf(icon)
            n.show()

class PastebinitExtension(nautilus.MenuProvider):
    def __init__(self):
        log ("Intializing nautilus-pastebin extension...")
    
    def get_file_items(self, window, files):
        if len(files)!=1:
            return
        filename = files[0]

        if filename.get_uri_scheme() != 'file' or filename.is_directory():
            return

        items = []
        #Called when the user selects a file in Nautilus.
        item = nautilus.MenuItem("NautilusPython::pastebin_item",
                                 _("Pastebin"),
                                 _("Send this file to a pastebin"))
        item.set_property('icon', "nautilus-pastebin")
        item.connect("activate", self.menu_activate_cb, files)
        items.append(item)
        return items

    def menu_activate_cb(self, menu, files):
        if len(files) != 1:
            return
        filename = files[0]

        validator = GlobalConfigurationValidator()
        validator.validate()
        pasteconf = validator.getPastebinConfiguration()

        if not pasteconf.setFormatByMimeType(filename.get_mime_type()):
            n= pynotify.Notification(_("Unable to send the file"), _("This file type cannot be pasted."))
            helper = gtk.Button()
            icon = helper.render_icon(gtk.STOCK_DIALOG_ERROR, gtk.ICON_SIZE_DIALOG)
            n.set_icon_from_pixbuf(icon)
            n.show()
            return

        pastefile = urllib.unquote(filename.get_uri()[7:])
        if not pasteconf.setContent(pastefile):
            return
        
        thread = PastebinThread(pasteconf)
        thread.start()
        while (thread.isAlive()):
            time.sleep(0.09)
            while gtk.events_pending():
                gtk.main_iteration()


Generated by  Doxygen 1.6.0   Back to index