1
/*
2
    Copyright (c) 2007 Benjamin Reed <ranger@befunk.com>
3
4
    This program is free software: you can redistribute it and/or modify
5
    it under the terms of the GNU General Public License as published by
6
    the Free Software Foundation, either version 2 of the License, or
7
    (at your option) any later version.
8
9
    This program is distributed in the hope that it will be useful,
10
    but WITHOUT ANY WARRANTY; without even the implied warranty of
11
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
    GNU General Public License for more details.
13
14
    You should have received a copy of the GNU General Public License
15
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
*/
17
18
#include <Carbon/Carbon.h>
19
20
#include "amarokurls/AmarokUrl.h"
21
#include "CollectionManager.h"
22
#include "Debug.h"
23
#include "Meta.h"
24
#include "Playlist.h"
25
#include "PlaylistFileSupport.h"
26
#include "playlist/PlaylistController.h"
27
#include "playlistmanager/PlaylistManager.h"
28
29
30
#include <QByteArray>
31
32
#include <KUrl>
33
34
static AEEventHandlerUPP appleEventProcessorUPP = 0;
35
static AEEventHandlerUPP macCallbackUrlHandlerUPP = 0;
36
37
OSStatus
38
appleEventProcessor(const AppleEvent *ae, AppleEvent *, long /*handlerRefCon*/)
39
{
40
    OSType aeID = typeWildCard;
41
    OSType aeClass = typeWildCard;
42
    AEGetAttributePtr(ae, keyEventClassAttr, typeType, 0, &aeClass, sizeof(aeClass), 0);
43
    AEGetAttributePtr(ae, keyEventIDAttr, typeType, 0, &aeID, sizeof(aeID), 0);
44
45
    if(aeClass == kCoreEventClass)
46
    {
47
        if(aeID == kAEReopenApplication)
48
        {
49
#if 0
50
            if( PlaylistWindow::self() )
51
                PlaylistWindow::self()->show();
52
#endif
53
        }
54
        return noErr;
55
    }
56
    return eventNotHandledErr;
57
}
58
59
OSStatus
60
macCallbackUrlHandler( const AppleEvent *ae, AppleEvent *, long /*handlerRefCon*/)
61
{
62
    DEBUG_BLOCK
63
    OSErr error = noErr;
64
    Size actualSize = 0;
65
    DescType descType = typeUTF8Text;
66
    if( ( error = AESizeOfParam( ae, keyDirectObject, &descType, &actualSize ) ) == noErr )
67
    {
68
        QByteArray ba;
69
        ba.resize( actualSize + 1 );
70
        error = AEGetParamPtr( ae, keyDirectObject, typeUTF8Text, 0, ba.data(), actualSize, &actualSize );
71
        if( error == noErr )
72
        {
73
            KUrl url( QString::fromUtf8( ba.data() ) );
74
            if( url.protocol() == "amarok" )
75
            {
76
                AmarokUrl aUrl( url.url() );
77
                aUrl.run();
78
            }
79
            else if( PlaylistManager::instance()->isPlaylist( url ) )
80
            {
81
                Meta::PlaylistPtr playlist = Meta::loadPlaylist( url );
82
                The::playlistController()->insertOptioned( playlist, Playlist::AppendAndPlay );
83
            }
84
            else
85
            {
86
                Meta::TrackPtr track = CollectionManager::instance()->trackForUrl( url );
87
                The::playlistController()->insertOptioned( track, Playlist::AppendAndPlay );
88
            }
89
        }
90
    }
91
    return error;
92
}
93
94
void
95
setupEventHandler_mac(long handlerRef)
96
{
97
    appleEventProcessorUPP = AEEventHandlerUPP(appleEventProcessor);
98
    AEInstallEventHandler(kCoreEventClass, kAEReopenApplication, appleEventProcessorUPP, handlerRef, true);
99
    macCallbackUrlHandlerUPP = AEEventHandlerUPP(macCallbackUrlHandler);
100
    AEInstallEventHandler(kInternetEventClass, kAEGetURL, macCallbackUrlHandlerUPP, handlerRef, false);
101
}