bool CanonTaskDownload::execute() {
  if(canon->isLegacy()) {
    canon->lockUI();
  }

  EdsError err = EDS_ERR_OK;
  EdsStreamRef stream = NULL;
  EdsDirectoryItemInfo dir_item_info;
  bool result = true;
  
  err = EdsGetDirectoryItemInfo(item, &dir_item_info);
  if(err != EDS_ERR_OK) {
    CANON_ERROR(err);
    result = false;
    goto done;
  }

  err = EdsCreateFileStream(dir_item_info.szFileName, kEdsFileCreateDisposition_CreateAlways, kEdsAccess_ReadWrite, &stream);
  if(err != EDS_ERR_OK) {
    CANON_ERROR(err);
    result = false;
    goto done;
  }

  err = EdsSetProgressCallback(stream, canon_download_progress, kEdsProgressOption_Periodically, (EdsVoid*) this);
  if(err != EDS_ERR_OK) {
    CANON_ERROR(err);
    result = false;
    goto done;
  }

  err = EdsDownload(item, dir_item_info.size, stream);
  if(err == EDS_ERR_OK) {
    err = EdsDownloadComplete(item);
    if(err != EDS_ERR_OK) {
      CANON_ERROR(err);
      result = false;
      goto done;
    }
  }
  else {
    CANON_ERROR(err);
  }

 done:
  if(canon->isLegacy()) {
    canon->unlockUI();
  }

  if(stream) {
    EdsRelease(stream);
    stream = NULL;
  }
  return result;
}
EdsError downloadImage(EdsDirectoryItemRef directoryItem, char* filename)
{
	EdsError err = EDS_ERR_OK;
	EdsStreamRef stream = NULL;
	// Get directory item information
	EdsDirectoryItemInfo dirItemInfo;
	err = EdsGetDirectoryItemInfo(directoryItem, & dirItemInfo);
	//printf("err EdsGetDirectoryItemInfo: %d\n", err);
	// Create file stream for transfer destination
	if(err == EDS_ERR_OK)
	{
		err = EdsCreateFileStream( theFileName,
			kEdsFileCreateDisposition_CreateAlways,
			kEdsAccess_ReadWrite, &stream);
	   //printf("err EdsCreateFileStream: %X\n", err);
	}
	// Download image
	if(err == EDS_ERR_OK)
	{
		err = EdsDownload( directoryItem, dirItemInfo.size, stream);
	//printf("err EdsDownload: %d\n", err);

	}
	// Issue notification that download is complete
	char* buf = NULL;
	if(err == EDS_ERR_OK)
	{
		err = EdsDownloadComplete(directoryItem);
		//printf("err EdsDownloadComplete: %d\n", err);
		buf = new char [strlen(theFileName)+1];
		strcpy_s(buf, strlen(theFileName)+1, theFileName);
		theFileName[0] = '\0';
	}
	// Release stream
	if( stream != NULL)
	{
		EdsRelease(stream);
		stream = NULL;
	}
	pictureProgress = CAMERA_COMPLETE;
	resetPictureTaking();

	// Notify done downloading
	if (buf) {
		/*std::cout << "snapped " << buf << std::endl;
		std::flush(std::cout);*/
		delete [] buf; buf = NULL;
	}
	return err;
}
    //---------------------------------------------------------------------
    bool CanonCameraWrapper::downloadImage(EdsDirectoryItemRef directoryItem){
        if( !downloadEnabled ) return false;

        EdsError err = EDS_ERR_OK;
        EdsStreamRef stream = NULL;
        EdsDirectoryItemInfo dirItemInfo;

        bool success = false;
        string imageName;
        string imagePath;

        int timeStart = ofGetElapsedTimeMillis();

        err = EdsGetDirectoryItemInfo(directoryItem, &dirItemInfo);
        if(err == EDS_ERR_OK){

            imageName = dirItemInfo.szFileName;
            imagePath = downloadPath + imageName;

            printf("Downloading image %s to %s\n", imageName.c_str(), imagePath.c_str());
            err = EdsCreateFileStream( ofToDataPath( imagePath ).c_str(), kEdsFileCreateDisposition_CreateAlways, kEdsAccess_ReadWrite, &stream);
        }

        if(err == EDS_ERR_OK){
            err = EdsDownload( directoryItem, dirItemInfo.size, stream);
        }

        if(err == EDS_ERR_OK){

            lastImageName = imageName;
            lastImagePath = imagePath;

            printf("Image downloaded in %ims\n", ofGetElapsedTimeMillis()-timeStart);

            err = EdsDownloadComplete(directoryItem);
            if( bDeleteAfterDownload ){
                printf("Image deleted\n");
                EdsDeleteDirectoryItem(directoryItem);
            }
            success = true;
        }

        easyRelease(stream);

		if (success)
			evtImageDownloaded.notify(this, getLastImagePath());

        return success;
    }
 void Camera::requestReadFile(const CameraFileRef& file, const std::function<void(EdsError error, QImage img)>& callback) {
    QImage img;
 
    EdsStreamRef stream = NULL;
    EdsError error = EdsCreateMemoryStream(0, &stream);
    if (error != EDS_ERR_OK) {
        std::cerr << "ERROR - failed to create memory stream" << std::endl;
        goto read_cleanup;
    }
 
    error = EdsDownload(file->mDirectoryItem, file->getSize(), stream);
    if (error != EDS_ERR_OK) {
        std::cerr << "ERROR - failed to download" << std::endl;
        goto read_cleanup;
    }
 
    error = EdsDownloadComplete(file->mDirectoryItem);
    if (error != EDS_ERR_OK) {
        std::cerr << "ERROR - failed to mark download as complete" << std::endl;
        goto read_cleanup;
    }
 
    unsigned char* data;
    error = EdsGetPointer(stream, (EdsVoid**)&data);
    if (error != EDS_ERR_OK) {
        std::cerr << "ERROR - failed to get pointer from stream" << std::endl;
        goto read_cleanup;
    }
 
    EdsUInt32 length;
    error = EdsGetLength(stream, &length);
    if (error != EDS_ERR_OK) {
        std::cerr << "ERROR - failed to get stream length" << std::endl;
        goto read_cleanup;
    }
 
    img = QImage::fromData(data, length, "JPG");
 
 read_cleanup:
    if (stream) {
        EdsRelease(stream);
    }
 
    callback(error, img);
 }
 void Camera::requestDownloadFile(const CameraFileRef& file, const QDir& destinationFolderPath, const std::function<void(EdsError error, QString outputFilePath)>& callback) {
     // check if destination exists and create if not
     if ( !destinationFolderPath.exists() ) {
         bool status = destinationFolderPath.mkpath(".");
         if (!status) {
             std::cerr << "ERROR - failed to create destination folder path '" << destinationFolderPath.dirName().toStdString() << "'" << std::endl;
             return callback(EDS_ERR_INTERNAL_ERROR, NULL);
         }
     }
     
     QString filePath = destinationFolderPath.filePath(QString::fromStdString(file->getName()));
     
     EdsStreamRef stream = NULL;
     EdsError error = EdsCreateFileStream(filePath.toStdString().c_str(), kEdsFileCreateDisposition_CreateAlways, kEdsAccess_ReadWrite, &stream);
     if (error != EDS_ERR_OK) {
         std::cerr << "ERROR - failed to create file stream" << std::endl;
         goto download_cleanup;
     }
     
     error = EdsDownload(file->mDirectoryItem, file->getSize(), stream);
     if (error != EDS_ERR_OK) {
         std::cerr << "ERROR - failed to download" << std::endl;
         goto download_cleanup;
     }
     
     error = EdsDownloadComplete(file->mDirectoryItem);
     if (error != EDS_ERR_OK) {
         std::cerr << "ERROR - failed to mark download as complete" << std::endl;
         goto download_cleanup;
     }
     
 download_cleanup:
     if (stream) {
         EdsRelease(stream);
     }
     
     callback(error, filePath);
 }
EdsError EDSCALLBACK CanonEDCamera::handleObjectEvent( EdsObjectEvent event, EdsBaseRef  object, EdsVoid * context) 
{ 
   printf ("Object Event triggered\n");

   CanonEDCamera* mmCanon = (CanonEDCamera*) g_Self;
  
   switch(event) 
   { 
      case kEdsObjectEvent_DirItemRequestTransfer: 
         {
            EdsError err = EDS_ERR_OK;
            EdsStreamRef stream = NULL;

            EdsDirectoryItemInfo dirItemInfo;
            err = EdsGetDirectoryItemInfo(object, &dirItemInfo);

            // do we need to notify the camera?
            /*
             if (err == EDS_ERR_OK)
             {
             CameraEvent e("DownloadStart");
             _model->notifyObservers(&e);
             }
             */

            if (err == EDS_ERR_OK)
            {
               //err = EdsCreateFileStream(dirItemInfo.szFileName, kEdsFileCreateDisposition_CreateAlways, kEdsAccess_ReadWrite, &stream);
               err = EdsCreateMemoryStream(0, &stream);
            }

            // Set Progress Callback???

            // Download Image
            if (err == EDS_ERR_OK)
            {
               err = EdsDownload(object,  dirItemInfo.size, stream);
            }

            if (err == EDS_ERR_OK)
            {
               err = EdsDownloadComplete(object);
            }

            EdsImageRef imageRef = NULL;

            if (err == EDS_ERR_OK)
            {
               err = EdsCreateImageRef(stream, &imageRef);
            }

            EdsImageInfo imageInfo;

            if (err == EDS_ERR_OK)
            {
               err = EdsGetImageInfo(imageRef, kEdsImageSrc_FullView, &imageInfo);
            }
            if (err == EDS_ERR_OK)
            {
               printf ("Image Width: %d\n", imageInfo.width);
            }


         }
         break; 
        
         default: 
          
         break; 
      } 
    
 
   // Object must be released 
   if(object) 
   { 
      EdsRelease(object); 
   } 
}