#include "MEGAService.h"

#include "MacXSystemServiceTask.h"
#include "MacXFunctions.h"

#include <QStringList>

@implementation MEGAService

-(id)initWithDelegate:(MacXSystemServiceTask*)delegate_instance {

    self = [super init];
    if (self) {
       delegate = delegate_instance;
     }

    [NSApp setServicesProvider:self];
    NSUpdateDynamicServices();

    return self;
}

- (void)handleItems:(NSPasteboard *)pboard userData:(NSString *)userData error:(NSString **)error {

    if([[pboard types] containsObject:NSFilenamesPboardType]){

        QStringList itemList;
        NSArray* fileArray=[pboard propertyListForType:NSFilenamesPboardType];

        itemList = qt_mac_NSArrayToQStringList(fileArray);
        delegate->processItems(itemList);
    }
}

- (void)handleSyncFolder:(NSPasteboard *)pboard userData:(NSString *)userData error:(NSString **)error {

    if([[pboard types] containsObject:NSFilenamesPboardType]){

        QStringList itemList;
        NSArray* fileArray=[pboard propertyListForType:NSFilenamesPboardType];

        itemList = qt_mac_NSArrayToQStringList(fileArray);
        delegate->processSyncFolder(itemList);
    }
}

// Services handler to open Add Backup dialog with selected local folder
- (void)handleBackupFolder:(NSPasteboard *)pboard userData:(NSString *)userData error:(NSString **)error {

    if([[pboard types] containsObject:NSFilenamesPboardType]){

        QStringList itemList;
        NSArray* fileArray=[pboard propertyListForType:NSFilenamesPboardType];

        itemList = qt_mac_NSArrayToQStringList(fileArray);
        delegate->processBackupFolder(itemList);
    }
}

@end
