gPodder 3.5.2 for Harmattan
[gpodder.git] / src / gpodder / gtkui / macosx.py
blob75b26cd90c87088b19a80f0c49c3ac5caf76306e
1 # -*- coding: utf-8 -*-
3 # gPodder - A media aggregator and podcast client
4 # Copyright (c) 2005-2013 Thomas Perl and the gPodder Team
6 # gPodder is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
11 # gPodder is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 import struct
21 import sys
23 from gpodder import util
25 def aeKeyword(fourCharCode):
26 """transform four character code into a long"""
27 return struct.unpack('I', fourCharCode)[0]
30 # for the kCoreEventClass, kAEOpenDocuments, ... constants
31 # comes with macpython
32 from Carbon.AppleEvents import *
34 # all this depends on pyObjc (http://pyobjc.sourceforge.net/).
35 # There may be a way to achieve something equivalent with only
36 # what's in MacPython (see for instance http://mail.python.org/pipermail/pythonmac-sig/2006-May/017373.html)
37 # but I couldn't achieve this !
39 # Also note that it only works when gPodder is not running !
40 # For some reason I don't get the events afterwards...
41 try:
42 from AppKit import NSObject
43 from AppKit import NSAppleEventManager
44 from AppKit import NSAppleEventDescriptor
46 class gPodderEventHandler(NSObject):
47 """ handles Apple Events for :
48 - Open With... (and dropping a file on the icon)
49 - "subscribe to podcast" from firefox
50 The code was largely inspired by gedit-osx-delegate.m, from the
51 gedit project
52 (see http://git.gnome.org/browse/gedit/tree/gedit/osx/gedit-osx-delegate.m?id=GEDIT_2_28_3).
53 """
55 # keeps a reference to the gui.gPodder class
56 gp = None
58 def register(self, gp):
59 """ register all handlers with NSAppleEventManager """
60 self.gp = gp
61 aem = NSAppleEventManager.sharedAppleEventManager()
62 aem.setEventHandler_andSelector_forEventClass_andEventID_(
63 self, 'openFileEvent:reply:', aeKeyword(kCoreEventClass), aeKeyword(kAEOpenDocuments))
64 aem.setEventHandler_andSelector_forEventClass_andEventID_(
65 self, 'subscribeEvent:reply:', aeKeyword('GURL'), aeKeyword('GURL'))
67 def openFileEvent_reply_(self, event, reply):
68 """ handles an 'Open With...' event"""
69 urls = []
70 filelist = event.paramDescriptorForKeyword_(aeKeyword(keyDirectObject))
71 numberOfItems = filelist.numberOfItems()
72 for i in range(1,numberOfItems+1):
73 fileAliasDesc = filelist.descriptorAtIndex_(i)
74 fileURLDesc = fileAliasDesc.coerceToDescriptorType_(aeKeyword(typeFileURL))
75 fileURLData = fileURLDesc.data()
76 url = buffer(fileURLData.bytes(),0,fileURLData.length())
77 url = str(url)
78 util.idle_add(self.gp.on_item_import_from_file_activate, None,url)
79 urls.append(str(url))
81 print >>sys.stderr,("open Files :",urls)
82 result = NSAppleEventDescriptor.descriptorWithInt32_(42)
83 reply.setParamDescriptor_forKeyword_(result, aeKeyword('----'))
85 def subscribeEvent_reply_(self, event, reply):
86 """ handles a 'Subscribe to...' event"""
87 filelist = event.paramDescriptorForKeyword_(aeKeyword(keyDirectObject))
88 fileURLData = filelist.data()
89 url = buffer(fileURLData.bytes(),0,fileURLData.length())
90 url = str(url)
91 print >>sys.stderr,("Subscribe to :"+url)
92 util.idle_add(self.gp.subscribe_to_url, url)
94 result = NSAppleEventDescriptor.descriptorWithInt32_(42)
95 reply.setParamDescriptor_forKeyword_(result, aeKeyword('----'))
97 # global reference to the handler (mustn't be destroyed)
98 handler = gPodderEventHandler.alloc().init()
99 except ImportError:
100 print >> sys.stderr, """
101 Warning: pyobjc not found. Disabling "Subscribe with" events handling
103 handler = None
105 def register_handlers(gp):
106 """ register the events handlers (and keep a reference to gPodder's instance)"""
107 if handler is not None:
108 handler.register(gp)