File indexing completed on 2024-04-21 04:47:49

0001 /****************************************************************************************
0002  * Copyright (c) 2007 Benjamin Reed <ranger@befunk.com>                                 *
0003  *                                                                                      *
0004  * This program is free software; you can redistribute it and/or modify it under        *
0005  * the terms of the GNU General Public License as published by the Free Software        *
0006  * Foundation; either version 2 of the License, or (at your option) any later           *
0007  * version.                                                                             *
0008  *                                                                                      *
0009  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0010  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0011  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0012  *                                                                                      *
0013  * You should have received a copy of the GNU General Public License along with         *
0014  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0015  ****************************************************************************************/
0016 
0017 #include <Carbon/Carbon.h>
0018 
0019 #include "amarokurls/AmarokUrl.h"
0020 #include "core-impl/collections/support/CollectionManager.h"
0021 #include "core/support/Debug.h"
0022 #include "core-impl/support/TrackLoader.h"
0023 #include "core/meta/Meta.h"
0024 #include "core/playlists/Playlist.h"
0025 #include "core-impl/playlists/types/file/PlaylistFileSupport.h"
0026 #include "playlist/PlaylistController.h"
0027 
0028 
0029 
0030 #include <QByteArray>
0031 
0032 #include <QUrl>
0033 
0034 static AEEventHandlerUPP appleEventProcessorUPP = 0;
0035 static AEEventHandlerUPP macCallbackUrlHandlerUPP = 0;
0036 
0037 OSStatus
0038 appleEventProcessor(const AppleEvent *ae, AppleEvent *, long /*handlerRefCon*/)
0039 {
0040     OSType aeID = typeWildCard;
0041     OSType aeClass = typeWildCard;
0042     AEGetAttributePtr(ae, keyEventClassAttr, typeType, 0, &aeClass, sizeof(aeClass), 0);
0043     AEGetAttributePtr(ae, keyEventIDAttr, typeType, 0, &aeID, sizeof(aeID), 0);
0044 
0045     if(aeClass == kCoreEventClass)
0046     {
0047         if(aeID == kAEReopenApplication)
0048         {
0049 #if 0
0050             if( PlaylistWindow::self() )
0051                 PlaylistWindow::self()->show();
0052 #endif
0053         }
0054         return noErr;
0055     }
0056     return eventNotHandledErr;
0057 }
0058 
0059 OSStatus
0060 macCallbackUrlHandler( const AppleEvent *ae, AppleEvent *, long /*handlerRefCon*/)
0061 {
0062     DEBUG_BLOCK
0063     OSErr error = noErr;
0064     Size actualSize = 0;
0065     DescType descType = typeUTF8Text;
0066     if( ( error = AESizeOfParam( ae, keyDirectObject, &descType, &actualSize ) ) == noErr )
0067     {
0068         QByteArray ba;
0069         ba.resize( actualSize + 1 );
0070         error = AEGetParamPtr( ae, keyDirectObject, typeUTF8Text, 0, ba.data(), actualSize, &actualSize );
0071         if( error == noErr )
0072         {
0073             QUrl url( QString::fromUtf8( ba.data() ) );
0074             if( url.scheme() == "amarok" )
0075             {
0076                 AmarokUrl aUrl( url.url() );
0077                 aUrl.run();
0078             } else
0079             {
0080                 TrackLoader *loader = new TrackLoader();
0081                 // FIXME: this has no effect, one has to connect to finished() signal
0082                 loader->init( url );
0083             }
0084         }
0085     }
0086     return error;
0087 }
0088 
0089 void
0090 setupEventHandler_mac(SRefCon handlerRef)
0091 {
0092     appleEventProcessorUPP = AEEventHandlerUPP(appleEventProcessor);
0093     AEInstallEventHandler(kCoreEventClass, kAEReopenApplication, appleEventProcessorUPP, handlerRef, true);
0094     macCallbackUrlHandlerUPP = AEEventHandlerUPP(macCallbackUrlHandler);
0095     AEInstallEventHandler(kInternetEventClass, kAEGetURL, macCallbackUrlHandlerUPP, handlerRef, false);
0096 }
0097