File: postrExtension.py

package info (click to toggle)
postr 0.13.1-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch
  • size: 2,160 kB
  • ctags: 485
  • sloc: sh: 4,139; python: 4,058; makefile: 141
file content (87 lines) | stat: -rw-r--r-- 3,129 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
# Postr's Nautilus Extension, an extension to upload to Flickr using Postr
#
# Copyright (C) 2007 German Poo-Caaman~o <gpoo@gnome.org>
#
# 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, 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 gettext
gettext.install('postr')

import gobject, nautilus
import os, os.path
from urllib import unquote

PROGRAM_NAME = 'postr'

class PostrExtension(nautilus.MenuProvider):
    def __init__(self):
        # The constructor must be exists, even if there is nothing
        # to initialize (See Bug #374958)
        #self.program = None
        pass
    
    def locate_program(self, program_name):
        path_list = os.environ['PATH']
        for d in path_list.split(os.path.pathsep):
            try:
                if program_name in os.listdir(d):
                    return os.path.sep.join([d, program_name])
            except OSError:
                # Normally is a bad idea use 'pass' in a exception,
                # but in this case we don't care if the directory
                # in path exists or not.
                pass

        return None

    def upload_files(self, menu, files):
        # This is the method invoked when our extension is activated
        # Do whatever you want to do with the files selected
        if len(files) == 0:
            return

        names = [ unquote(file.get_uri()[7:]) for file in files ]

        argv = [ PROGRAM_NAME ] + names

        # TODO: use startup notification
        gobject.spawn_async(argv, flags=gobject.SPAWN_SEARCH_PATH)

    def get_file_items(self, window, files):
        # Show the menu iif:
        # - There is at least on file selected
        # - All the selected files are images
        # - All selected images are locals (currently Postr doesn't have
        #   support for gnome-vfs
        # - Postr is installed (is in PATH)
        if len(files) == 0:
            return
        
        for file in files:
            if file.is_directory() or file.get_uri_scheme() != 'file':
                return
            if not file.is_mime_type("image/*"):
                return

        #self.program = self.locate_program(PROGRAM_NAME)
        #if not self.program:
        #    return

        item = nautilus.MenuItem('PostrExtension::upload_files',
                                 _('Upload to Flickr...'),
                                 _('Upload the selected files into Flickr'),
                                 "postr")
        item.connect('activate', self.upload_files, files)

        return item,