#include <QDebug>
#include <QFileOpenEvent>


#include "qiosapplicationdelegate.h"

/// \category QIOSApplicationDelegate(CmsApplicationDelegate)
/// \abstract A category on QIOSApplicationDelegate to override openURL


/// Use the Objective-C category feature to override the openURL method
@implementation QIOSApplicationDelegate (CmsApplicationDelegate)

/// Posts a QFileOpenEvent to QCoreApplication when iOS calls openURL.
///
/// This allows userto open a TSR file from Apple Mail,
/// select "Open in TSR", to launch TSR and pass openURL the
/// file to open.
///
/// Must add UTExportedTypeDeclarations and CFBundleDocumentTypes to your
/// Info.plist for this to work.
///
/// Example: Info.plist
/// \verbatim
///    <key>UTExportedTypeDeclarations</key>
///    <array>
///        <dict>
///            <key>UTTypeConformsTo</key>
///            <array>
///                <string>public.data</string>
///            </array>
///            <key>UTTypeDescription</key>
///            <string>Tracking Status Report</string>
///            <key>UTTypeIdentifier</key>
///            <string>com.ditchwitch.tsr</string>
///            <key>UTTypeTagSpecification</key>
///            <dict>
///                <key>public.filename-extension</key>
///                <string>tsr</string>
///                <key>public.mime-type</key>
///                <string>public.data</string>
///            </dict>
///        </dict>
///    </array>
///    <key>CFBundleDocumentTypes</key>
///    <array>
///        <dict>
///            <key>CFBundleTypeIconFiles</key>
///            <array>
///                <string>Icon.png</string>
///            </array>
///            <key>CFBundleTypeName</key>
///            <string>Tracking Status Report</string>
///            <key>CFBundleTypeRole</key>
///            <string>Editor</string>
///            <key>LSHandlerRank</key>
///            <string>Owner</string>
///            <key>LSItemContentTypes</key>
///            <array>
///                <string>com.ditchwitch.tsr</string>
///                <string>public.data</string>
///            </array>
///        </dict>
///    </array>
/// \endverbatim
///
/// See file: cmsapplication.cpp
/// \see cmsapplication.cpp
///
/// \see https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/CustomizingExistingClasses/CustomizingExistingClasses.html
///
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
                                       sourceApplication:(NSString *)sourceApplication
                                              annotation:(id)annotation
{
    Q_UNUSED(application);
    Q_UNUSED(annotation);

    NSLog(@"sourceApplication: %@",sourceApplication);
    NSLog(@"url: %@",url);

    QString filePath = QString::fromNSString(url.path);
    qDebug("filePath %s", filePath.toLatin1().data());

    QFileOpenEvent *fileOpenEvent = new QFileOpenEvent(filePath);
    QCoreApplication::postEvent(QCoreApplication::instance(), fileOpenEvent);

    return YES;
}

@end

