You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

2545 lines
87 KiB

#include "gPhotoCamera.h"
#define LOG( X ) m_pLog->Log( "GPHO", this, X )
#define LOG_ERROR( X ) m_pLog->Log( "GPHO", this, _LOG_ERROR, X )
struct _CameraWidget{
CameraWidgetType type;
char label [256];
char info [1024];
char name [256];
CameraWidget *parent;
/* Current value of the widget */
char *value_string;
int value_int;
float value_float;
/* For Radio and Menu */
char **choice;
int choice_count;
/* For Range */
float min;
float max;
float increment;
/* Child info */
CameraWidget **children;
int children_count;
/* Widget was changed */
int changed;
/* Widget is read only */
int readonly;
/* Reference count */
int ref_count;
/* Unique identifier */
int id;
/* Callback */
CameraWidgetCallback callback;
};
static void errordumper( GPLogLevel level, const char *domain, const char *str, void *data) {
basicLogCollection::getInstance()->Log( "GPHO_LIB", NULL, str );
};
static void error_func( GPContext *context, const char *format, va_list args, void *data) {
char tBuff[256];
sprintf( tBuff, format, args );
basicLogCollection::getInstance()->Log( "GPHO_CTX_ERR", NULL, tBuff );
};
static void message_func( GPContext *context, const char *format, va_list args, void *data) {
char tBuff[256];
sprintf( tBuff, format, args );
basicLogCollection::getInstance()->Log( "GPHO_CTX_MSG", NULL, tBuff );
};
/*
* This function looks up a label or key entry of
* a configuration widget.
* The functions descend recursively, so you can just
* specify the last component.
*/
int gPhotoCamera::_lookup_widget(CameraWidget*widget, const char *key, CameraWidget **child) {
int ret;
ret = gp_widget_get_child_by_name( widget, key, child );
if( ret < GP_OK ) ret = gp_widget_get_child_by_label( widget, key, child );
return ret;
};
/* Gets a string configuration value.
* This can be:
* - A Text widget
* - The current selection of a Radio Button choice
* - The current selection of a Menu choice
*
* Sample (for Canons eg):
* get_config_value_string (camera, "owner", &ownerstr, context);
*/
int gPhotoCamera::get_config_value_string (Camera *camera, const char *key, char **str, GPContext *context ) {
CameraWidget * widget = NULL, *child = NULL;
CameraWidgetType type;
int ret;
char * val;
ret = gp_camera_get_single_config( camera, key, &child, context );
if (ret == GP_OK) {
if (!child) LOG_ERROR( "child is NULL?");
widget = child;
} else {
ret = gp_camera_get_config( camera, &widget, context );
if (ret < GP_OK) {
LOG_ERROR( "camera_get_config failed" );
return ret;
};
ret = _lookup_widget( widget, key, &child );
if (ret < GP_OK) {
LOG_ERROR( "lookup widget failed" );
goto out;
};
};
/* This type check is optional, if you know what type the label
* has already. If you are not sure, better check. */
ret = gp_widget_get_type( child, &type );
if (ret < GP_OK) {
LOG_ERROR( "Widget get type failed" );
goto out;
}
switch (type) {
case GP_WIDGET_MENU:
case GP_WIDGET_RADIO:
case GP_WIDGET_TEXT:
break;
default:
LOG_ERROR( "widget has bad type!" );
ret = GP_ERROR_BAD_PARAMETERS;
goto out;
}
/* This is the actual query call. Note that we just
* a pointer reference to the string, not a copy... */
ret = gp_widget_get_value( child, &val );
if (ret < GP_OK) {
LOG_ERROR( "could not query widget value" );
goto out;
}
/* Create a new copy for our caller. */
*str = strdup (val);
out:
gp_widget_free (widget);
return ret;
};
gPhotoCamera::gPhotoCamera( config_setting_t *Settings ): basicThread(){
m_pLog = basicLogCollection::getInstance();
m_pSettings = basicSettingsProvider::getInstance()->getSubNode( Settings, "GP_CAMERA" );
m_pBLOG = objectPathfinderBinaryLog::getInstance();
m_nCameraID = 0;
m_nFolderID = 0;
m_ePortType = GP_PORT_NONE;
m_pDBC = NULL;
m_pCamera = NULL;
m_pContext = NULL;
m_pPortInfoList = NULL;
m_pAbilitiesList = NULL;
m_bBurstCapturing = false;
m_pChannelIsBusy = false;
m_bOldWMode =GP_WM_NA;
m_bWMode = GP_WM_CTRL;
#ifdef DEBUG
m_nFlags = FLAGS_NEW;
#else
m_nFlags = FLAGS_NEW | FLAGS_QUIET;
#endif
};
gPhotoCamera::gPhotoCamera( PATHFINDER_DBC *DBC ): basicThread(){
m_pLog = basicLogCollection::getInstance();
m_pSettings = basicSettingsProvider::getInstance()->getSubNode( NULL, "GP_CAMERA" );
m_pBLOG = objectPathfinderBinaryLog::getInstance();
m_nCameraID = 0;
m_nFolderID = 0;
m_ePortType = GP_PORT_NONE;
m_pDBC = DBC;
m_pCamera = NULL;
m_pContext = NULL;
m_pPortInfoList = NULL;
m_pAbilitiesList = NULL;
m_bBurstCapturing = false;
m_pChannelIsBusy = false;
m_bOldWMode =GP_WM_NA;
m_bWMode = GP_WM_CTRL;
#ifdef DEBUG
m_nFlags = FLAGS_NEW;
#else
m_nFlags = FLAGS_NEW | FLAGS_QUIET;
#endif
};
gPhotoCamera::gPhotoCamera( config_setting_t *Settings, PATHFINDER_DBC *DBC ): basicThread(){
m_pLog = basicLogCollection::getInstance();
m_pSettings = basicSettingsProvider::getInstance()->getSubNode( Settings, "GP_CAMERA" );
m_pBLOG = objectPathfinderBinaryLog::getInstance();
m_nCameraID = 0;
m_ePortType = GP_PORT_NONE;
m_pDBC = DBC;
m_pCamera = NULL;
m_pContext = NULL;
m_pPortInfoList = NULL;
m_pAbilitiesList = NULL;
m_bBurstCapturing = false;
m_pChannelIsBusy = false;
m_bOldWMode =GP_WM_NA;
m_bWMode = GP_WM_CTRL;
char tBuffer[1024];
const char * tBuf;
config_setting_t * pSetting;
if( !config_setting_lookup_string( m_pSettings, GPHOTO_TAG_MOUNT_POINT, &tBuf ) ){
sprintf( m_pCamMountPoint, "/srv/pathfinder/data/camera" );
pSetting = config_setting_add( m_pSettings, GPHOTO_TAG_MOUNT_POINT, CONFIG_TYPE_STRING );
config_setting_set_string( pSetting, m_pCamMountPoint );
basicSettingsProvider::getInstance()->compromise();
LOG("Camera Mount Point not defined - generating new!");
} else {
sprintf( m_pCamMountPoint, tBuf );
sprintf( tBuffer, "Camera Mount Point Read From Settings: %s", m_pCamMountPoint );
LOG( tBuffer );
};
pSetting = config_setting_get_member(m_pSettings, GPHOTO_TAG_MOUNT_POINT_UID );
if ( pSetting == NULL ){
m_nAccessUID = 65534;
pSetting = config_setting_add( m_pSettings, GPHOTO_TAG_MOUNT_POINT_UID, CONFIG_TYPE_INT );
config_setting_set_int( pSetting, m_nAccessUID );
basicSettingsProvider::getInstance()->compromise();
LOG("Camera Mount Point UID not defined - generating new!");
} else {
m_nAccessUID = config_setting_get_int( pSetting );
sprintf( tBuffer, "Camera Mount Point UID Read From Settings: %d", m_nAccessUID );
LOG( tBuffer );
};
pSetting = config_setting_get_member(m_pSettings, GPHOTO_TAG_MOUNT_POINT_GID );
if ( pSetting == NULL ){
m_nAccessGID = 65534;
pSetting = config_setting_add( m_pSettings, GPHOTO_TAG_MOUNT_POINT_GID, CONFIG_TYPE_INT );
config_setting_set_int( pSetting, m_nAccessGID );
basicSettingsProvider::getInstance()->compromise();
LOG("Camera Mount Point GID not defined - generating new!");
} else {
m_nAccessGID = config_setting_get_int( pSetting );
sprintf( tBuffer, "Camera Mount Point GID Read From Settings: %d", m_nAccessGID );
LOG( tBuffer );
};
#ifdef DEBUG
m_nFlags = FLAGS_NEW;
#else
m_nFlags = FLAGS_NEW | FLAGS_QUIET;
#endif
};
gPhotoCamera::~gPhotoCamera(){
m_bThreadStatus = TH_ENDED;
m_mCommandLock.unlock();
waitThreadEnd( true );
if( m_pPortInfoList ) gp_port_info_list_free( m_pPortInfoList );
if( m_pAbilitiesList ) gp_abilities_list_free( m_pAbilitiesList );
};
bool gPhotoCamera::create(){
#ifdef DEBUG
LOG("Create()");
#endif
if( m_bThreadStatus > TH_ARMED ) return true;
return basicThread::init();
};
bool gPhotoCamera::makeShot(){
if( m_pCamera == NULL ) return false;
if( m_pChannelIsBusy ) return false;
if( m_bBurstCapturing ) return false;
m_mCommandLock.lock();
GP_CAM_COMMAND * tCMD;
tCMD = (GP_CAM_COMMAND*) malloc(sizeof(GP_CAM_COMMAND));
if( tCMD != NULL ) {
memset( tCMD, 0 , sizeof(GP_CAM_COMMAND) );
tCMD->cmd = GP_SINGLE_SHOT;
m_qCommands.push( tCMD );
};
m_mCommandLock.unlock();
return true;
};
bool gPhotoCamera::makePreview(){
if( m_pCamera == NULL ) return false;
if( m_pChannelIsBusy ) return false;
if( m_bBurstCapturing ) return false;
m_mCommandLock.lock();
GP_CAM_COMMAND * tCMD;
tCMD = (GP_CAM_COMMAND*) malloc(sizeof(GP_CAM_COMMAND));
if( tCMD != NULL ) {
memset( tCMD, 0 , sizeof(GP_CAM_COMMAND) );
tCMD->cmd = GP_PREVIEW_SHOT;
m_qCommands.push( tCMD );
};
m_mCommandLock.unlock();
return true;
};
bool gPhotoCamera::startPreview(){
if( m_pCamera == NULL ) return false;
if( m_pChannelIsBusy ) return false;
if( m_bBurstCapturing ) return false;
m_mCommandLock.lock();
GP_CAM_COMMAND * tCMD;
tCMD = (GP_CAM_COMMAND*) malloc(sizeof(GP_CAM_COMMAND));
if( tCMD != NULL ) {
memset( tCMD, 0 , sizeof(GP_CAM_COMMAND) );
tCMD->cmd = GP_START_PREVIEW;
m_qCommands.push( tCMD );
};
m_mCommandLock.unlock();
return true;
};
bool gPhotoCamera::stopPreview(){
if( m_pCamera == NULL ) return false;
if( m_pChannelIsBusy ) return false;
if( m_bBurstCapturing ) return false;
m_mCommandLock.lock();
GP_CAM_COMMAND * tCMD;
tCMD = (GP_CAM_COMMAND*) malloc(sizeof(GP_CAM_COMMAND));
if( tCMD != NULL ) {
memset( tCMD, 0 , sizeof(GP_CAM_COMMAND) );
tCMD->cmd = GP_STOP_PREVIEW;
m_qCommands.push( tCMD );
};
m_mCommandLock.unlock();
return true;
};
bool gPhotoCamera::setCaptureTarget( GP_CAP_TARGET T ){
if( m_pCamera == NULL ) return false;
if( m_pChannelIsBusy ) return false;
m_mCommandLock.lock();
GP_CAM_COMMAND * tCMD;
tCMD = (GP_CAM_COMMAND*) malloc(sizeof(GP_CAM_COMMAND));
if( tCMD != NULL ) {
memset( tCMD, 0 , sizeof(GP_CAM_COMMAND) );
tCMD->cmd = GP_SET_CONFIG;
sprintf( tCMD->prm,"capturetarget");
if( T == GP_SD_CARD ) {
sprintf( tCMD->var,"Memory card");//"card");// , T );
} else {
sprintf( tCMD->var,"Internal RAM");//"sdram");
};
m_qCommands.push( tCMD );
};
m_mCommandLock.unlock();
return true;
};
bool gPhotoCamera::startBurst(){
if( m_pCamera == NULL ) return false;
if( m_pChannelIsBusy ) return false;
m_mCommandLock.lock();
m_bBurstCapturing = true;
GP_CAM_COMMAND * tCMD;
tCMD = (GP_CAM_COMMAND*) malloc(sizeof(GP_CAM_COMMAND));
if( tCMD != NULL ) {
memset( tCMD, 0 , sizeof(GP_CAM_COMMAND) );
tCMD->cmd = GP_START_BURST;
m_qCommands.push( tCMD );
if( m_pDBC != NULL ) {
m_pDBC->setCameraMode( GP_WM_BURST );
};
};
m_mCommandLock.unlock();
return true;
};
bool gPhotoCamera::stopBurst(){
if( m_pCamera == NULL ) return false;
if( m_pChannelIsBusy ) return false;
m_mCommandLock.lock();
m_bBurstCapturing = false;
GP_CAM_COMMAND * tCMD;
tCMD = (GP_CAM_COMMAND*) malloc(sizeof(GP_CAM_COMMAND));
if( tCMD != NULL ) {
memset( tCMD, 0 , sizeof(GP_CAM_COMMAND) );
tCMD->cmd = GP_STOP_BURST;
m_qCommands.push( tCMD );
};
m_mCommandLock.unlock();
return true;
};
bool gPhotoCamera::validate(){
if( m_pCamera == NULL ) return false;
return true;
};
bool gPhotoCamera::setWM( uint8_t NMode ){
m_bOldWMode = m_bWMode;
m_bWMode = NMode;
if( m_pDBC != NULL ) {
m_pDBC->setCameraMode( m_bWMode );
};
return true;
};
bool gPhotoCamera::cancelWM( uint8_t NMode ){
if( NMode != m_bWMode ) return false;
if( m_bOldWMode == GP_WM_NA ) return false;
if( m_bWMode != m_bOldWMode ) {
m_bWMode == m_bOldWMode;
m_bOldWMode = GP_WM_NA;
};
if( m_pDBC != NULL ) {
m_pDBC->setCameraMode( m_bWMode );
};
return true;
};
void gPhotoCamera::m_fThreadProc( void ){
gp_log_add_func( GP_LOG_ERROR, errordumper, NULL );
//gp_log_add_func( GP_LOG_DEBUG, errordumper, NULL );
CameraFilePath *path;
CameraEventType evtype;
void *data;
int retval;
char tBuff[1024];
// Init Camera!
m_pContext = gp_context_new();
if( m_pContext != NULL ){
LOG("New Context Created");
int ret = GP_OK;
//gp_context_set_error_func( m_pContext, error_func, NULL);
//gp_context_set_message_func( m_pContext, message_func, NULL);
gp_camera_new( &m_pCamera );
LOG("New Camera Created");
ret = gp_camera_init( m_pCamera, m_pContext );
if( ret < GP_OK ){
LOG_ERROR("No camera auto detected!");
gp_camera_free( m_pCamera );
m_pCamera = NULL;
m_bThreadStatus = TH_ENDED;
} else {
LOG("Camera Init Complete");
m_bThreadStatus = TH_WORKING;
if( m_pDBC != NULL){
int StorCNT;
m_pDBC->clenupCameraFS();
CameraStorageInformation *sifs = NULL;
ret = gp_camera_get_storageinfo( m_pCamera, &sifs, &StorCNT, m_pContext );
if( ret == GP_OK ) {
if( StorCNT ) {
for( int dx = 0; dx < StorCNT; dx++ ) {
m_pDBC->updateCAM_STORAGE_INFO( sifs[dx].basedir, sifs[dx].type, sifs[dx].fstype, sifs[dx].access, sifs[dx].capacitykbytes, sifs[dx].freekbytes );
updateFileTree( m_pDBC->getCAM_fileSyetemID( sifs[dx].basedir ), 0, sifs[dx].basedir );
sprintf( tBuff, "Stor INFO: [0x%02X] %s [%s](%s) t:0x%02X fst:0x%02X acc: 0x%02X size: %lld Kb free: %lld Kb for %lld pictures",
sifs[dx].fields,
sifs[dx].basedir,
sifs[dx].label,
sifs[dx].description,
sifs[dx].type,
sifs[dx].fstype,
sifs[dx].access,
sifs[dx].capacitykbytes,
sifs[dx].freekbytes,
sifs[dx].freeimages
);
LOG( tBuff );
};
};
if( sifs != NULL ) free( sifs );
};
m_pDBC->setCameraMode( GP_WM_CTRL );
};
setCaptureTarget( GP_SD_CARD );
data = NULL;
};
#ifdef DEBUG
LOG("Camera Init Done!");
#endif
} else {
LOG_ERROR( "Cannot create GP Context" );
m_bThreadStatus = TH_ENDED;
};
uint8_t mPrevCounter = 0;
while( m_bThreadStatus < TH_ENDED ){
unsigned int timediff;
// Fetch Kernel Commands
m_mCommandLock.lock();
if( !m_qCommands.empty() ) {
GP_CAM_COMMAND * tCMD;
tCMD = m_qCommands.front();
switch( tCMD->cmd ) {
/*case GP_DEL_PHOTOS:{
if( m_pDBC != NULL ) {
char cFile[256];
char cFolder[256];
char cFID;
while( ( cFID = m_pDBC->feitchPhoto( cFolder, cFile ) ) && ( m_bThreadStatus < TH_ENDED ) ){
retval = gp_camera_file_delete( m_pCamera, cFolder, cFile, m_pContext );
if( retval == GP_OK ) {
m_pDBC->removePhoto( cFID );
} else {
sprintf( tBuff, "Error Deleting File [%s] in [%s]", cFile, cFolder );
LOG_ERROR( tBuff );
break;
};
};
};
break;
};*/
case GP_STOP_PREVIEW:{
LOG("Cancel Preview mode");
cancelWM( GP_WM_PREV );
break;
};
case GP_START_PREVIEW:{
LOG("Start Preview mode");
setWM( GP_WM_PREV );
break;
};
case GP_PREVIEW_SHOT:{
// Only in Control mode can be issued manual preview
if( m_bWMode != GP_WM_CTRL ) break;
CameraFile * cameraFile = NULL;
gp_file_new( &cameraFile );
if( cameraFile!= NULL ) {
int retval = gp_camera_capture_preview( m_pCamera, cameraFile, m_pContext );
if( retval != GP_OK){
LOG("Error Ocured!");
switch( retval ) {
case GP_ERROR:
LOG_ERROR( "Triggering capture had error!" );
break;
case GP_ERROR_CAMERA_BUSY:
LOG_ERROR( "Camera is BUSY!" );
break;
default:
LOG_ERROR( "Camera is Not OK!" );
break;
};
} else {
gp_file_save( cameraFile ,"data/capture_preview.jpg");
};
gp_file_free( cameraFile );
};
};
break;
case GP_SINGLE_SHOT:{
LOG("Make Shot!");
// Disable Single Shot while in Burst Mode
if( m_bWMode == GP_WM_BURST ) break;
CameraFilePath camera_file_path;
//strcpy( camera_file_path.folder, "100CANON");
//strcpy( camera_file_path.name, "IMG_1203.JPG");
//int retval = gp_camera_capture( m_pCamera, GP_CAPTURE_IMAGE, &camera_file_path, m_pContext );
int retval = gp_camera_trigger_capture( m_pCamera, m_pContext );
if( retval != GP_OK){
switch( retval ) {
case GP_ERROR:
LOG_ERROR( "Triggering capture had error!" );
break;
case GP_ERROR_CAMERA_BUSY:
LOG_ERROR( "Camera is BUSY!" );
break;
default:
LOG_ERROR( "Camera is Not OK!" );
break;
};
} else m_pChannelIsBusy = true;
};
break;
case GP_START_BURST:{
setWM( GP_WM_BURST );
set_config_action( "viewfinder", "1" );
SLEEP( 500 );
set_config_action( "eosremoterelease", "2" );
//viewfinder=1;eosremoterelease=2;
//m_pChannelIsBusy = true;
};
break;
case GP_STOP_BURST:{
setWM( GP_WM_CTRL );
SLEEP( 300 );
set_config_action( "eosremoterelease", "0" );
SLEEP( 500 );
set_config_action( "viewfinder", "0" );
SLEEP( 300 );
//viewfinder=0;eosremoterelease=0;
//m_pChannelIsBusy = true;
};
break;
case GP_SET_CONFIG:{
CameraWidget *pWidget;
int retVal;
LOG( "Set CFG!" );
m_pChannelIsBusy = true;
//gp_widget_new( GP_WIDGET_MENU, tCMD->prm, &pWidget );
retVal = gp_camera_get_single_config( m_pCamera, (const char *) tCMD->prm, &pWidget, m_pContext );
if( retVal == GP_OK ){
#ifdef DEBUG
char ttt[128];
#endif
switch( pWidget->type ) {
case GP_WIDGET_MENU:
case GP_WIDGET_RADIO:
set_config_action( tCMD->prm, tCMD->var );
/*
int cnt, i;
cnt = gp_widget_count_choices( pWidget );
if( cnt < GP_OK ){
ret = cnt;
break;
};
ret = GP_ERROR_BAD_PARAMETERS;
i = atoi( tCMD->var );
if( (i>= 0) && (i < cnt) ){
const char * choice;
ret = gp_widget_get_choice (pWidget, i, &choice);
if (ret == GP_OK)
ret = gp_widget_set_value (pWidget, choice);
break;
};
//gp_context_error( p->context, _("Choice %s not found within list of choices."), value );
if (ret == GP_OK) {
if (child == rootconfig)
ret = gp_camera_set_single_config (p->camera, name, child, p->context);
else
ret = gp_camera_set_config (p->camera, rootconfig, p->context);
if (ret != GP_OK)
gp_context_error (p->context, _("Failed to set new configuration value %s for configuration entry %s."), value, name);
}
gp_widget_free (rootconfig); */
break;
case GP_WIDGET_TEXT:
#ifdef DEBUG
sprintf( ttt, "Current %s(%s)[string] = %s", pWidget->name, pWidget->label, pWidget->value_string );
LOG( ttt );
#endif
strcpy( pWidget->value_string, tCMD->var );
break;
case GP_WIDGET_DATE:
case GP_WIDGET_TOGGLE:
#ifdef DEBUG
sprintf( ttt, "Current %s(%s)[int] = %d (%s)", pWidget->name, pWidget->label, pWidget->value_int, pWidget->value_string );
LOG( ttt );
#endif
pWidget->value_int = atoi(tCMD->var);
break;
case GP_WIDGET_RANGE:
#ifdef DEBUG
sprintf( ttt, "Current %s(%s)[float] = %.3f (%s)", pWidget->name, pWidget->label, pWidget->value_float, pWidget->value_string );
LOG( ttt );
#endif
pWidget->value_float = (float) atof(tCMD->var);
break;
case GP_WIDGET_BUTTON:
#ifdef DEBUG
sprintf( ttt, "Current %s(%s)[ptr] = %p (%s)", pWidget->name, pWidget->label, pWidget->callback, pWidget->value_string );
LOG( ttt );
#endif
//CameraWidgetCallback
break;
};
retVal = gp_camera_set_single_config( m_pCamera, pWidget->name, pWidget, m_pContext );
gp_widget_free( pWidget );
if( retVal != GP_OK ) {
SLEEP( DEFAULT_THREAD_SLEEP_TIME );
m_mCommandLock.unlock();
LOG_ERROR("CONFIG SET Failure!");
continue;
};
#ifdef DEBUG
LOG("Success!");
#endif
} else {
SLEEP( DEFAULT_THREAD_SLEEP_TIME );
m_mCommandLock.unlock();
continue;
};
};
break;
};
m_qCommands.pop();
free( tCMD );
SLEEP( DEFAULT_THREAD_SLEEP_TIME );
m_mCommandLock.unlock();
continue;
};
m_mCommandLock.unlock();
// Proceed Working Mode BackLogic
if( ( m_bWMode == GP_WM_PREV ) && !m_pChannelIsBusy ) {
mPrevCounter ++;
if( mPrevCounter >= 10 ) {
CameraFile * cameraFile = NULL;
gp_file_new( &cameraFile );
if( cameraFile!= NULL ) {
int retval = gp_camera_capture_preview( m_pCamera, cameraFile, m_pContext );
if( retval != GP_OK){
LOG("Error Ocured!");
switch( retval ) {
case GP_ERROR:
LOG_ERROR( "Triggering capture had error!" );
break;
case GP_ERROR_CAMERA_BUSY:
LOG_ERROR( "Camera is BUSY!" );
break;
default:
LOG_ERROR( "Camera is Not OK!" );
break;
};
} else {
gp_file_save( cameraFile ,"data/capture_preview.jpg");
};
gp_file_free( cameraFile );
};
mPrevCounter = 0;
};
SLEEP( 90 );
} else {
// Fetch Events
retval = gp_camera_wait_for_event( m_pCamera, 10, &evtype, &data, m_pContext ); // 1/10 th second
if( retval != GP_OK ){
LOG_ERROR( "Error Return from waitevent" );
m_bThreadStatus = TH_ENDED;
continue;
};
path = (CameraFilePath*) data;
m_pChannelIsBusy = false;
switch( evtype ){
case GP_EVENT_CAPTURE_COMPLETE:
LOG( "event CAPTURE_COMPLETE" );
break;
case GP_EVENT_UNKNOWN:
case GP_EVENT_TIMEOUT:
break;
case GP_EVENT_FOLDER_ADDED:
LOG( "event FOLDER_ADDED" );
free(data);
break;
case GP_EVENT_FILE_CHANGED:
LOG( "event FILE_CHANGED" );
free(data);
break;
case GP_EVENT_FILE_ADDED:
#ifdef DEBUG
LOG( "event FILE_ADDED" );
#endif
if( m_pBLOG != NULL ){
PF_TIMESTAMP ts;
getPFTS( &ts );
m_pBLOG->registerPIC( &ts, path->name );
} else LOG_ERROR("No Binary Log Connected!");
if( m_pDBC != NULL ) {
m_pDBC->registerCameraPicture( m_nFolderID, path->name );
};
#ifdef DEBUG
//LOG( "event FILE_ADDED" );
//sprintf( tBuff,"File [%s/%s] created.", path->folder, path->name );
//LOG( tBuff );
#endif
/*
retval = gp_camera_file_delete( m_pCamera, path->folder, path->name, m_pContext );
if(retval != GP_OK) {
LOG_ERROR( "Cannot delete picture on camera!" );
sprintf( tBuff, "Retval: %d %s", retval, gp_result_as_string( retval ) );
LOG_ERROR( tBuff );
} else {
LOG("File is Deleted!");
};
*/
free(data);
break;
default:
LOG(" EVT Handler KOKO!");
break;
};
};
};
if( m_pCamera != NULL ) {
gp_camera_exit( m_pCamera, m_pContext );
gp_camera_free( m_pCamera );
};
if( m_pContext != NULL ) gp_context_unref( m_pContext );
};
void gPhotoCamera::updateFileTree( int fsid, int parent, CSTR folder ){
int ret;
int MyID;
char tBuff[256];
CameraList * Folders = NULL;
CameraList * Files = NULL;
if( m_pDBC == NULL ) return;
sprintf( tBuff, "Check Folder: %s", folder );
LOG( tBuff );
MyID = m_pDBC->addCameraFolder( fsid, parent, folder );
if( MyID > 0 ) {
gp_list_new( &Folders );
ret = gp_camera_folder_list_folders( m_pCamera, folder, Folders, m_pContext );
if( ret == GP_OK ) {
for( int i=0; i < gp_list_count( Folders ); i++ ) {
char *name;//, *value;
gp_list_get_name( Folders, i, (const char**)&name );
//gp_list_get_value( Folders, i, (const char**)&value );
sprintf( tBuff, "%s/%s",folder, name );
updateFileTree( fsid, MyID, tBuff );
};
};
gp_list_free( Folders );
gp_list_new( &Files );
ret = gp_camera_folder_list_files( m_pCamera, folder, Files, m_pContext );
if( ret == GP_OK ) {
int i = 0;
for( i=0; i < gp_list_count( Files ); i++ ) {
char *name;//, *value;
gp_list_get_name( Files, i, (const char**)&name);
//gp_list_get_value( Files, i, (const char**)&value);
m_pDBC->addCameraFile( MyID, name );
//sprintf( tBuff, "Files: %s", name );
//LOG( tBuff );
};
if( i > 0 ) {
m_nFolderID = MyID;
sprintf( m_pCapFolder, folder );
sprintf( tBuff, "Save Folder: [%s] id:[%d]", m_pCapFolder, m_nFolderID );
LOG( tBuff );
};
};
gp_list_free( Files );
};
};
bool gPhotoCamera::switchToStorage(){
LOG("To Storage Mode" );
if( m_bWMode != GP_WM_STOR ){
char tBuff[1024];
int RetVal;
m_bWMode = GP_WM_NA;
m_bThreadStatus = TH_ENDED;
m_mCommandLock.unlock();
waitThreadEnd( true );
if( m_pDBC != NULL ) {
m_pDBC->setCameraMode( GP_WM_STOR );
};
sprintf( tBuff, "gphotofs -o uid=%d,gid=%d,allow_other,nonempty,auto_unmount,large_read,default_permissions %s", m_nAccessUID, m_nAccessGID, m_pCamMountPoint );
LOG( tBuff );
RetVal = system( tBuff );
sprintf( tBuff, "Returned Code: %d", RetVal );
LOG( tBuff );
m_bWMode = GP_WM_STOR;
LOG("OK");
return true;
};
LOG("Wrong Mode!");
return false;
};
bool gPhotoCamera::switchToCamera(){
LOG("To Camera Mode" );
if( m_bWMode != GP_WM_CTRL ){
if( m_bWMode == GP_WM_STOR ) {
char tBuff[256];
int RetVal;
m_bWMode = GP_WM_NA;
sprintf( tBuff, "fusermount -u %s ", m_pCamMountPoint );
LOG( tBuff );
RetVal = system( tBuff );
sprintf( tBuff, "Returned Code: %d", RetVal );
LOG( tBuff );
m_bThreadStatus = TH_PRE_INIT;
LOG("From Mount OK");
return create();
} else {
LOG("From Mode OK");
return setWM( GP_WM_CTRL );
};
};
LOG("Wrong Mode!");
return false;
};
bool gPhotoCamera::deleteAllPictures(){
if( m_bWMode == GP_WM_STOR ) {
if( m_pDBC != NULL ) {
char cFile[256];
char cFolder[256];
char cName[1024];
char tBuff[1024];
uint32_t cFID;
while( ( cFID = m_pDBC->feitchPhoto( cFolder, cFile ) ) > 0 ){
sprintf( cName, "%s%s/%s", m_pCamMountPoint, cFolder, cFile );
LOG( cName );
if( 0 == unlink( cName ) ) {
m_pDBC->removePhoto( cFID );
} else {
sprintf( tBuff, "Error Deleting File [%s]", cName );
LOG_ERROR( tBuff );
break;
};
};
};
};
return true;
};
bool gPhotoCamera::fetchFile( uint32_t pictureID ){
unsigned long size;
int fd;
struct stat stbuf;
CameraFile * file;
/*
retval = gp_file_new( &file );
//fprintf(stderr,"camera getfile of %s / %s\n", queue[0].path.folder, queue[0].path.name );
retval = gp_camera_file_get( m_pCamera, queue[0].path.folder, queue[0].path.name, GP_FILE_TYPE_NORMAL, file, m_pContext );
if (retval != GP_OK) {
gp_file_free (file);
LOG_ERROR( "gp_camera_file_get failed" );
m_bThreadStatus = TH_ENDED;
};
// buffer is returned as pointer, not as a copy
retval = gp_file_get_data_and_size( file, &buffer, &size );
if( retval != GP_OK ){
gp_file_free (file);
LOG_ERROR( "gp_file_get_data_and_size failed" );
m_bThreadStatus = TH_ENDED;
};
if( -1 == stat(queue[0].path.name, &stbuf) ){
fd = creat(queue[0].path.name, 0644);
} else{
fd = open(queue[0].path.name, O_RDWR | O_BINARY, 0644);
};
if( fd == -1 ){
LOG_ERROR( queue[0].path.name );
m_bThreadStatus = TH_ENDED;
};
if( -1 == lseek(fd, 0, SEEK_SET) ) LOG_ERROR("lseek");
if( -1 == write (fd, buffer, size) ) LOG_ERROR("write");
close (fd);
gp_file_free (file); // Note: this invalidates buffer.
LOG( "ending download, deleting file." );
retval = gp_camera_file_delete( m_pCamera, queue[0].path.folder, queue[0].path.name, m_pContext );
memmove(&queue[0],&queue[1],sizeof(queue[0])*(nrofqueue-1));
*/
return true;
};
int gPhotoCamera::delete_all_action( char *folder ){
return (gp_camera_folder_delete_all( m_pCamera, folder, m_pContext ));
};
#define CR(result) {int r = (result); if (r < 0) return (r);}
#define CHECK(result) {int r=(result);if(r<0) return(r);}
#define CL(result,list) {int r=(result);if(r<0) {gp_list_free(list);return(r);}}
#define CHECK_CONT(result) \
{ \
int r = (result); \
\
if (r < 0) { \
if (r == GP_ERROR_CANCEL) { \
glob_cancel = 0; \
} else { \
printf (_("*** Error (%i: '%s') ***"), \
r, gp_result_as_string (r)); \
putchar ('\n'); \
} \
} \
}
#define textdomain(String) (String)
#define gettext(String) (String)
#define ngettext(String1,String2,Count) (Count==1?String1:String2)
#define dgettext(Domain,Message) (Message)
#define dcgettext(Domain,Message,Type) (Message)
#define bindtextdomain(Domain,Directory) (Domain)
#define _(String) (String)
#define N_(String) (String)
int gPhotoCamera::action_camera_upload_file( char *folder, char *path){
CameraFile *file;
int res;
char *fn;
LOG( "Uploading file..." );
CR( gp_file_new_from_fd( &file, -1 ));
res = gp_file_open (file, path);
if (res < GP_OK) {
gp_file_unref (file);
return res;
};
//if (p->filename && strcmp (p->filename, ""))
// fn = p->filename;
//else
fn = basename (path);
res = gp_camera_folder_put_file( m_pCamera, folder, fn, GP_FILE_TYPE_NORMAL, file, m_pContext );
gp_file_unref (file);
return (res);
};
int gPhotoCamera::action_camera_upload_metadata( char *folder, char *path ){
CameraFile *file;
int res;
char *fn = NULL;
LOG( "Uploading metadata..." );
CR( gp_file_new( &file ) );
res = gp_file_open (file, path);
if (res < 0) {
gp_file_unref (file);
return (res);
};
/* Check if the user specified a filename */
//if (p->filename && strcmp (p->filename, "")) {
// fn = p->filename;
//} else
if (path == strstr(path, "meta_")) {
fn = path+5;
};
res = gp_camera_folder_put_file( m_pCamera, folder, fn, GP_FILE_TYPE_METADATA, file, m_pContext );
gp_file_unref (file);
return (res);
};
int gPhotoCamera::num_files_action(){
CameraList *list;
int count, filecount;
CR( gp_list_new (&list));
CL( gp_camera_folder_list_files( m_pCamera, m_pCapFolder, list, m_pContext ), list );
CL( count = gp_list_count (list), list);
if( m_nFlags & FLAGS_NEW ){
int i;
const char *name;
filecount = 0;
for (i = 0; i < count; i++) {
CameraFileInfo info;
CL( gp_list_get_name( list, i, &name ), list );
CR( gp_camera_file_get_info( m_pCamera, m_pCapFolder, name, &info, m_pContext ));
if( info.file.fields & GP_FILE_INFO_STATUS && info.file.status != GP_FILE_STATUS_DOWNLOADED ) filecount++;
};
} else filecount = count;
gp_list_free (list);
if( m_nFlags & FLAGS_QUIET ){
char tBuff[256];
sprintf( tBuff, "%i", filecount );
LOG( tBuff );
} else{
char tBuff[256];
sprintf( tBuff, "Number of files in folder '%s': %i", m_pCapFolder, filecount );
LOG( tBuff );
};
return (GP_OK);
};
int gPhotoCamera::list_folders_action(){
CameraList *list;
int count;
const char *name;
int i;
CR( gp_list_new (&list) );
CL( gp_camera_folder_list_folders( m_pCamera, m_pCapFolder, list, m_pContext ), list );
CL( count = gp_list_count(list), list );
if( !(m_nFlags & FLAGS_QUIET) ){
char tBuff[256];
sprintf( tBuff, ngettext(
"There is %d folder in folder '%s'.\n",
"There are %d folders in folder '%s'.\n",
count
), count, m_pCapFolder );
LOG( tBuff );
};
for( i = 0; i < count; i++ ){
char tBuff[256];
CL( gp_list_get_name( list, i, &name ), list );
if( m_nFlags & FLAGS_QUIET) {
if( !strcmp( m_pCapFolder,"/") )
sprintf( tBuff, "/%s", name);
else
sprintf( tBuff, "%s/%s", m_pCapFolder, name );
} else
sprintf( tBuff, " - %s", name);
LOG(tBuff);
};
gp_list_free (list);
return (GP_OK);
};
int gPhotoCamera::list_files_action(){
CameraList *list;
int count, filecount;
char *name;
int i;
CR( gp_list_new(&list) );
CL( gp_camera_folder_list_files( m_pCamera, m_pCapFolder, list, m_pContext ), list );
CL( count = gp_list_count(list), list );
if( m_nFlags & FLAGS_NEW ){
filecount = 0;
for (i = 0; i < count; i++) {
CameraFileInfo info;
CL( gp_list_get_name( list, i, (const char **)&name ), list );
CR( gp_camera_file_get_info( m_pCamera, m_pCapFolder, name, &info, m_pContext ) );
if (info.file.fields & GP_FILE_INFO_STATUS && info.file.status != GP_FILE_STATUS_DOWNLOADED ) filecount++;
};
} else filecount = count;
if (!(m_nFlags & FLAGS_QUIET)) { /* do not print that in quiet mode */
char tBuff[256];
if (filecount == 0) { /* 0 is weird still, despite ngettext() */
sprintf( tBuff, "There is no file in folder '%s'.", m_pCapFolder );
} else {
sprintf( tBuff, ngettext(
"There is %d file in folder '%s'.\n",
"There are %d files in folder '%s'.\n",
filecount
), filecount, m_pCapFolder );
};
LOG(tBuff);
};
for( i = 0; i < count; i++ ){
CL( gp_list_get_name( list, i, (const char **)&name ), list );
CL( print_file_action( m_pCapFolder, name ), list );
};
gp_list_free (list);
return (GP_OK);
};
int gPhotoCamera::print_info_action( char *folder, char *filename ){
CameraFileInfo info;
char tBuf[256];
CR( gp_camera_file_get_info( m_pCamera, folder, filename, &info, m_pContext ) );
sprintf( tBuf, "Information on file '%s' (folder '%s'):", filename, folder );
LOG( tBuf );
LOG( "File:" );
if( info.file.fields == GP_FILE_INFO_NONE ) LOG( " None available." );
else {
memset( tBuf, 0, 256 );
if( info.file.fields & GP_FILE_INFO_TYPE)
sprintf( tBuf, " Mime type: '%s'", info.file.type );
if( info.file.fields & GP_FILE_INFO_SIZE)
sprintf( tBuf, " Size: %lu byte(s)", (unsigned long int)info.file.size );
if( info.file.fields & GP_FILE_INFO_WIDTH)
sprintf( tBuf, " Width: %i pixel(s)", info.file.width );
if( info.file.fields & GP_FILE_INFO_HEIGHT)
sprintf( tBuf, " Height: %i pixel(s)", info.file.height );
if( info.file.fields & GP_FILE_INFO_STATUS)
sprintf( tBuf, " Downloaded: %s", (info.file.status == GP_FILE_STATUS_DOWNLOADED) ? _("yes") : _("no") );
if( info.file.fields & GP_FILE_INFO_PERMISSIONS) {
LOG( " Permissions: " );
if ((info.file.permissions & GP_FILE_PERM_READ) && (info.file.permissions & GP_FILE_PERM_DELETE))
sprintf( tBuf, "read/delete" );
else if (info.file.permissions & GP_FILE_PERM_READ)
sprintf( tBuf, "read" );
else if (info.file.permissions & GP_FILE_PERM_DELETE)
sprintf( tBuf, "delete" );
else
sprintf( tBuf, "none" );
};
LOG( tBuf );
if( info.file.fields & GP_FILE_INFO_MTIME ){
sprintf( tBuf, " Time: %s", asctime (localtime (&info.file.mtime)) );
LOG( tBuf );
};
};
LOG( "Thumbnail:" );
if (info.preview.fields == GP_FILE_INFO_NONE)
LOG( " None available." );
else {
memset( tBuf, 0, 256 );
if (info.preview.fields & GP_FILE_INFO_TYPE)
sprintf( tBuf, " Mime type: '%s'", info.preview.type );
if (info.preview.fields & GP_FILE_INFO_SIZE)
sprintf( tBuf, " Size: %lu byte(s)", (unsigned long int)info.preview.size );
if (info.preview.fields & GP_FILE_INFO_WIDTH)
sprintf( tBuf, " Width: %i pixel(s)", info.preview.width );
if (info.preview.fields & GP_FILE_INFO_HEIGHT)
sprintf( tBuf, " Height: %i pixel(s)", info.preview.height );
if (info.preview.fields & GP_FILE_INFO_STATUS)
sprintf( tBuf, " Downloaded: %s", (info.preview.status == GP_FILE_STATUS_DOWNLOADED) ? _("yes") : _("no") );
LOG( tBuf );
};
LOG( "Audio data:" );
if( info.audio.fields == GP_FILE_INFO_NONE )
LOG( " None available." );
else {
memset( tBuf, 0, 256 );
if (info.audio.fields & GP_FILE_INFO_TYPE)
sprintf( tBuf, " Mime type: '%s'", info.audio.type );
if (info.audio.fields & GP_FILE_INFO_SIZE)
sprintf( tBuf, " Size: %lu byte(s)", (unsigned long int)info.audio.size );
if (info.audio.fields & GP_FILE_INFO_STATUS)
sprintf( tBuf, " Downloaded: %s", (info.audio.status == GP_FILE_STATUS_DOWNLOADED) ? _("yes") : _("no") );
LOG( tBuf );
};
return (GP_OK);
};
int gPhotoCamera::print_file_action( char *folder, char *filename ){
char tBuff[256];
int x = 0;
if( m_nFlags & FLAGS_NEW ){
CameraFileInfo info;
CR( gp_camera_file_get_info( m_pCamera, folder, filename, &info, m_pContext ) );
if( info.file.fields & GP_FILE_INFO_STATUS && info.file.status == GP_FILE_STATUS_DOWNLOADED ){
x++;
return (GP_OK);
};
};
if( m_nFlags & FLAGS_QUIET ){
sprintf( tBuff, "%s/%s", folder, filename );
LOG( tBuff );
} else {
CameraFileInfo info;
if( gp_camera_file_get_info( m_pCamera, folder, filename, &info, NULL) == GP_OK ){
sprintf( tBuff, "#%-5i %-27s", x+1, filename );
LOG( tBuff );
memset( tBuff, 0 , 256 );
if( info.file.fields & GP_FILE_INFO_PERMISSIONS) sprintf( tBuff, "%s%s", (info.file.permissions & GP_FILE_PERM_READ) ? "r" : "-", (info.file.permissions & GP_FILE_PERM_DELETE) ? "d" : "-");
if( info.file.fields & GP_FILE_INFO_SIZE) sprintf( tBuff, " %5ld KB", (unsigned long int)((info.file.size+1023) / 1024));
if( (info.file.fields & GP_FILE_INFO_WIDTH) && + (info.file.fields & GP_FILE_INFO_HEIGHT)) sprintf( tBuff, " %4dx%-4d", info.file.width, info.file.height );
if (info.file.fields & GP_FILE_INFO_TYPE) sprintf( tBuff, " %s", info.file.type );
} else
printf("#%-5i %s\n", x+1, filename);
LOG(tBuff);
};
x++;
return (GP_OK);
};
/*
int gPhotoCamera::save_file_action( char *folder, char *filename ){
return (save_file_to_file( m_pCamera, m_pContext, m_nFlags, folder, filename, GP_FILE_TYPE_NORMAL));
};
int gPhotoCamera::save_exif_action( char *folder, char *filename ){
return (save_file_to_file( m_pCamera, m_pContext, m_nFlags, folder, filename, GP_FILE_TYPE_EXIF));
};
int gPhotoCamera::save_meta_action( char *folder, char *filename ){
return (save_file_to_file( m_pCamera, m_pContext, m_nFlags, folder, filename, GP_FILE_TYPE_METADATA));
};
int gPhotoCamera::save_thumbnail_action( char *folder, char *filename ){
return (save_file_to_file( m_pCamera, m_pContext, m_nFlags, folder, filename, GP_FILE_TYPE_PREVIEW));
};
int gPhotoCamera::save_raw_action( char *folder, char *filename ){
return (save_file_to_file( m_pCamera, m_pContext, m_nFlags, folder, filename, GP_FILE_TYPE_RAW));
};
int gPhotoCamera::save_audio_action( char *folder, char *filename ){
return (save_file_to_file( m_pCamera, m_pContext, m_nFlags, folder, filename, GP_FILE_TYPE_AUDIO));
};
int gPhotoCamera::save_all_audio_action( char *folder, char *filename ){
// not every file has an associated audio file
if (camera_file_exists( m_pCamera, m_pContext, folder, filename, GP_FILE_TYPE_AUDIO))
return (save_file_to_file( m_pCamera, m_pContext, m_nFlags, folder, filename, GP_FILE_TYPE_AUDIO));
return GP_OK;
};
*/
int gPhotoCamera::delete_file_action( char *folder, char *filename ){
if( m_nFlags & FLAGS_NEW ){
CameraFileInfo info;
CR( gp_camera_file_get_info( m_pCamera, folder, filename, &info, m_pContext ) );
if (info.file.fields & GP_FILE_INFO_STATUS && info.file.status == GP_FILE_STATUS_DOWNLOADED) return (GP_OK);
};
return (gp_camera_file_delete( m_pCamera, folder, filename, m_pContext ) );
};
int gPhotoCamera::list_cameras_action(){
int r = GP_OK, n, i;
char tBuff[256];
CameraAbilities a;
r = gp_abilities_list_count( gp_params_abilities_list() );
if( r < 0 ) return (r);
if( m_nFlags & FLAGS_QUIET ){
sprintf( tBuff, "%i", r);
LOG(tBuff);
} else {
sprintf( tBuff, "Number of supported cameras: %i", r);
LOG(tBuff);
LOG( "Supported cameras:" );
};
n = r;
for( i = 0; i < n; i++ ){
r = gp_abilities_list_get_abilities( gp_params_abilities_list(), i, &a );
if (r < 0) break;
if ( m_nFlags & FLAGS_QUIET)
sprintf( tBuff,"%s", a.model);
else
switch (a.status) {
case GP_DRIVER_STATUS_TESTING:
sprintf( tBuff, "\t\"%s\" (TESTING)", a.model );
break;
case GP_DRIVER_STATUS_EXPERIMENTAL:
sprintf( tBuff, "\t\"%s\" (EXPERIMENTAL)", a.model );
break;
case GP_DRIVER_STATUS_PRODUCTION:
default:
sprintf( tBuff, "\t\"%s\"", a.model);
break;
};
LOG(tBuff);
};
return (r);
}
void gPhotoCamera::_get_portinfo_list(){
int count, result;
GPPortInfoList *list = NULL;
if( m_pPortInfoList ) return;
if( gp_port_info_list_new( &list ) < GP_OK ) return;
result = gp_port_info_list_load( list );
if (result < 0) {
gp_port_info_list_free( list );
return;
};
count = gp_port_info_list_count( list );
if( count < 0 ){
gp_port_info_list_free( list );
return;
};
m_pPortInfoList = list;
return;
}
int gPhotoCamera::list_ports_action(){
GPPortInfo info;
int x, count, result = GP_OK;
char tBuff[256];
_get_portinfo_list();
count = gp_port_info_list_count( m_pPortInfoList );
if( m_nFlags & FLAGS_QUIET) sprintf( tBuff, "%i", count );
else sprintf( tBuff, "Devices found: %i", count );
LOG( tBuff );
/* Now list the ports */
for( x = 0; x < count; x++ ){
char *xname, *xpath;
result = gp_port_info_list_get_info( m_pPortInfoList, x, &info );
if( result < 0 ) break;
gp_port_info_get_name( info, &xname);
gp_port_info_get_path( info, &xpath);
sprintf( tBuff, "%-32s %-32s", xpath, xname );
LOG( tBuff );
};
return (result);
};
int gPhotoCamera::auto_detect_action(){
int x, count;
CameraList *list;
const char *name = NULL, *value = NULL;
char tBuff[256];
_get_portinfo_list();
count = gp_port_info_list_count( m_pPortInfoList );
CR( gp_list_new( &list ) );
gp_abilities_list_detect( gp_params_abilities_list(), m_pPortInfoList, list, m_pContext );
CL( count = gp_list_count( list ), list );
sprintf( tBuff, "%-30s %-16s", "Model", "Port" );
LOG(tBuff);
LOG("----------------------------------------------------------");
for (x = 0; x < count; x++) {
CL( gp_list_get_name( list, x, &name), list );
CL( gp_list_get_value( list, x, &value), list );
sprintf( tBuff, "%-30s %-16s", name, value );
LOG(tBuff);
};
gp_list_free (list);
return GP_OK;
};
int gPhotoCamera::action_camera_show_abilities(){
CameraAbilities a;
int i;
int has_capture = 0;
char tBuff[256];
CR( gp_camera_get_abilities( m_pCamera, &a ));
sprintf( tBuff, "Abilities for camera : %s", a.model );
LOG(tBuff);
sprintf( tBuff, "Serial port support : %s", (a.port & GP_PORT_SERIAL) ? _("yes"):_("no") );
LOG(tBuff);
sprintf( tBuff, "USB support : %s", (a.port & GP_PORT_USB) ? _("yes"):_("no") );
LOG(tBuff);
if( a.speed[0] != 0 ){
LOG("Transfer speeds supported :" );
for (i = 0; a.speed[i]; i++){
sprintf( tBuff, " : %i", a.speed[i] );
LOG(tBuff);
};
};
LOG( "Capture choices :" );
if( a.operations & GP_OPERATION_CAPTURE_IMAGE ){
LOG( " : Image" );
has_capture = 1;
};
if( a.operations & GP_OPERATION_CAPTURE_VIDEO ){
LOG( " : Video" );
has_capture = 1;
};
if( a.operations & GP_OPERATION_CAPTURE_AUDIO ){
LOG( " : Audio" );
has_capture = 1;
};
if( a.operations & GP_OPERATION_CAPTURE_PREVIEW ){
LOG( " : Preview" );
has_capture = 1;
};
if (a.operations & GP_OPERATION_TRIGGER_CAPTURE) {
LOG( " : Trigger Capture" );
has_capture = 1;
};
if (has_capture == 0) {
LOG( " : Capture not supported by the driver" );
};
sprintf( tBuff, "Configuration support : %s", (a.operations & GP_OPERATION_CONFIG) ? _("yes"):_("no"));
LOG(tBuff);
sprintf( tBuff, "Delete selected files on camera : %s", (a.file_operations & GP_FILE_OPERATION_DELETE) ? _("yes"):_("no"));
LOG(tBuff);
sprintf( tBuff, "Delete all files on camera : %s", (a.folder_operations & GP_FOLDER_OPERATION_DELETE_ALL) ? _("yes"):_("no"));
LOG(tBuff);
sprintf( tBuff, "File preview (thumbnail) support : %s", (a.file_operations & GP_FILE_OPERATION_PREVIEW) ? _("yes"):_("no"));
LOG(tBuff);
sprintf( tBuff, "File upload support : %s", (a.folder_operations & GP_FOLDER_OPERATION_PUT_FILE) ? _("yes"):_("no"));
LOG(tBuff);
return (GP_OK);
}
int gPhotoCamera::action_camera_set_port( const char *port ){
int p, r;
GPPortInfo info;
char *path;
char tBuff[256];
char verified_port[1024];
verified_port[sizeof (verified_port) - 1] = '\0';
if( !strchr( port, ':' ) ){
sprintf(tBuff, "Ports must look like 'serial:/dev/ttyS0' or 'usb:', but '%s' is missing a colon so I am going to guess what you mean.", port);
if( !strcmp( port, "usb" ) ){
strncpy( verified_port, "usb:", sizeof (verified_port) - 1 );
} else if( strncmp( port, "/dev/", 5) == 0 ){
strncpy( verified_port, "serial:", sizeof (verified_port) - 1);
strncat( verified_port, port, sizeof (verified_port) - strlen (verified_port) - 1 );
} else if( strncmp( port, "/proc/", 6) == 0 ){
strncpy( verified_port, "usb:", sizeof (verified_port) - 1 );
strncat( verified_port, port, sizeof (verified_port) - strlen (verified_port) - 1 );
};
sprintf( tBuff, "Guessed port name. Using port '%s' from now on.", verified_port );
LOG(tBuff);
} else
strncpy ( verified_port, port, sizeof (verified_port) - 1);
/* Create the list of ports and load it. */
_get_portinfo_list();
/* Search our port in the list. */
/* NOTE: This call can modify "il" for regexp matches! */
p = gp_port_info_list_lookup_path( m_pPortInfoList, verified_port );
switch( p ){
case GP_ERROR_UNKNOWN_PORT:
sprintf( tBuff, "The port you specified ('%s') can not be found. Please specify one of the ports found by 'gphoto2 --list-ports' and make sure the spelling is correct (i.e. with prefix 'serial:' or 'usb:').", verified_port );
LOG( tBuff );
break;
default:
break;
};
/* Get info about our port. */
r = gp_port_info_list_get_info( m_pPortInfoList, p, &info );
if( r < 0 )return (r);
/* Set the port of our camera. */
r = gp_camera_set_port_info( m_pCamera, info );
if (r < 0) return (r);
gp_port_info_get_path( info, &path );
//gp_setting_set( "gphoto2", "port", path );
return (GP_OK);
};
int gPhotoCamera::action_camera_about(){
CameraText text;
CR( gp_camera_get_about( m_pCamera, &text, m_pContext ) );
LOG( "About the camera driver:" );
LOG( text.text );
return (GP_OK);
};
int gPhotoCamera::action_camera_summary(){
CameraText text;
CR( gp_camera_get_summary( m_pCamera, &text, m_pContext ) );
LOG( "Camera summary:" );
LOG( text.text );
return (GP_OK);
};
int gPhotoCamera::action_camera_manual(){
CameraText text;
CR( gp_camera_get_manual( m_pCamera, &text, m_pContext ) );
LOG( "Camera manual:" );
LOG( text.text );
return (GP_OK);
}
int gPhotoCamera::action_camera_set_speed( unsigned int speed ){
GPPortInfo info;
GPPortType type;
/* Make sure we've got a serial port. */
CR( gp_camera_get_port_info( m_pCamera, &info ) );
gp_port_info_get_type( info, &type );
if( type != GP_PORT_SERIAL ){
if( (m_nFlags & FLAGS_QUIET) == 0 ) LOG("You can only specify speeds for serial ports.");
return (GP_ERROR_BAD_PARAMETERS);
};
/* Set the speed. */
return gp_camera_set_port_speed( m_pCamera, speed );
};
int gPhotoCamera::action_camera_set_model( const char *model ){
CameraAbilities a;
int m;
CR( m = gp_abilities_list_lookup_model( gp_params_abilities_list(), model ) );
CR( gp_abilities_list_get_abilities( gp_params_abilities_list(), m, &a ) );
CR( gp_camera_set_abilities( m_pCamera, a ) );
//gp_setting_set ("gphoto2", "model", a.model);
return (GP_OK);
};
int gPhotoCamera::set_folder_action( const char *folder ){
sprintf( m_pCapFolder, folder );
return GP_OK;
};
int gPhotoCamera::set_filename_action( const char *filename ){
sprintf( m_pCapFile,filename );
return GP_OK;
};
#define CHECK_NULL(x) { if (x == NULL) { return(-1); /* FIXME: what code? */ } }
enum moviemode { MOVIE_ENDLESS, MOVIE_FRAMES, MOVIE_SECONDS };
int gPhotoCamera::override_usbids_action( int usb_vendor, int usb_product, int usb_vendor_modified, int usb_product_modified ){
CameraAbilitiesList *al = NULL;
int r, n, i;
CameraAbilities a;
CR( gp_abilities_list_new(&al) );
/* The override_usbides_action() function is a notable
* exception to the rule that one is not supposed to use
* p->_abilities_list directly, because it has to and does so
* in a safe way. */
n = gp_abilities_list_count( gp_params_abilities_list() );
for( i = 0; i < n; i++ ){
r = gp_abilities_list_get_abilities( gp_params_abilities_list(), i, &a );
if( r < 0 ) continue;
if( (a.usb_vendor == usb_vendor) && (a.usb_product == usb_product) ){
char tBuff[256];
sprintf( tBuff, "Overriding USB vendor/product id 0x%x/0x%x with 0x%x/0x%x", a.usb_vendor, a.usb_product, usb_vendor_modified, usb_product_modified );
LOG(tBuff);
a.usb_vendor = usb_vendor_modified;
a.usb_product = usb_product_modified;
};
gp_abilities_list_append( al, a );
};
gp_abilities_list_free( m_pAbilitiesList );
m_pAbilitiesList = al;
return (GP_OK);
};
/* time zero for debug log time stamps */
static struct timeval glob_tv_zero = { 0, 0 };
int gPhotoCamera::_find_widget_by_name( const char *name, CameraWidget **child, CameraWidget **rootconfig ){
int ret;
*rootconfig = NULL;
ret = gp_camera_get_single_config( m_pCamera, name, child, m_pContext );
if( ret == GP_OK ){
*rootconfig = *child;
return GP_OK;
};
ret = gp_camera_get_config( m_pCamera, rootconfig, m_pContext );
if( ret != GP_OK ) return ret;
ret = gp_widget_get_child_by_name( *rootconfig, name, child );
if( ret != GP_OK ) ret = gp_widget_get_child_by_label( *rootconfig, name, child );
if (ret != GP_OK) {
char *part, *s, *newname;
newname = strdup( name );
if( !newname ) return GP_ERROR_NO_MEMORY;
*child = *rootconfig;
part = newname;
while( part[0] == '/' ) part++;
while( 1 ){
CameraWidget *tmp;
s = strchr( part, '/' );
if( s ) *s='\0';
ret = gp_widget_get_child_by_name( *child, part, &tmp );
if( ret != GP_OK ) ret = gp_widget_get_child_by_label (*child, part, &tmp);
if( ret != GP_OK ) break;
*child = tmp;
if( !s ){
/* end of path */
free (newname);
return GP_OK;
};
part = s+1;
while( part[0] == '/' ) part++;
};
gp_context_error( m_pContext, _("%s not found in configuration tree."), newname );
free (newname);
gp_widget_free( *rootconfig );
return GP_ERROR;
};
return GP_OK;
};
/* From the strftime(3) man page:
* BUGS
* Some buggy versions of gcc complain about the use of %c: warning:
* %c yields only last 2 digits of year in some locales.
* Of course programmers are encouraged to use %c, it gives the
* preferred date and time representation. One meets all kinds of
* strange obfuscations to circumvent this gcc problem. A relatively
* clean one is to add an intermediate function
*/
size_t my_strftime(char *s, size_t max, const char *fmt, const struct tm *tm){
return strftime(s, max, fmt, tm);
}
int gPhotoCamera::get_config_action( const char *name ){
CameraWidget *rootconfig,*child;
int ret;
ret = _find_widget_by_name ( name, &child, &rootconfig );
if( ret != GP_OK) return ret;
//ret = print_widget (p, name, child);
gp_widget_free (rootconfig);
return ret;
};
int gPhotoCamera::set_config_action( const char *name, const char *value ){
CameraWidget *rootconfig,*child;
int ret, ro;
CameraWidgetType type;
ret = _find_widget_by_name ( name, &child, &rootconfig);
if( ret != GP_OK ) return ret;
ret = gp_widget_get_readonly( child, &ro );
if( ret != GP_OK ){
gp_widget_free (rootconfig);
return ret;
};
if( ro == 1 ){
gp_context_error( m_pContext, _("Property %s is read only."), name );
gp_widget_free( rootconfig );
return GP_ERROR;
};
ret = gp_widget_get_type( child, &type );
if( ret != GP_OK ){
gp_widget_free( rootconfig );
return ret;
};
switch (type) {
case GP_WIDGET_TEXT: { /* char * */
ret = gp_widget_set_value( child, value );
if (ret != GP_OK)
gp_context_error( m_pContext, _("Failed to set the value of text widget %s to %s."), name, value );
break;
}
case GP_WIDGET_RANGE: { /* float */
float f,t,b,s;
ret = gp_widget_get_range( child, &b, &t, &s );
if (ret != GP_OK)
break;
if (!sscanf (value, "%f", &f)) {
gp_context_error( m_pContext, _("The passed value %s is not a floating point value."), value );
ret = GP_ERROR_BAD_PARAMETERS;
break;
}
if ((f < b) || (f > t)) {
gp_context_error( m_pContext, _("The passed value %f is not within the expected range %f - %f."), f, b, t );
ret = GP_ERROR_BAD_PARAMETERS;
break;
}
ret = gp_widget_set_value( child, &f );
if (ret != GP_OK)
gp_context_error( m_pContext, _("Failed to set the value of range widget %s to %f."), name, f );
break;
}
case GP_WIDGET_TOGGLE: { /* int */
int t;
t = 2;
if( !strcasecmp( value, "off") || !strcasecmp (value, "no") ||
!strcasecmp( value, "false") || !strcmp (value, "0") ||
!strcasecmp( value, _("off")) || !strcasecmp (value, _("no")) ||
!strcasecmp( value, _("false"))
)
t = 0;
if( !strcasecmp( value, "on") || !strcasecmp (value, "yes") ||
!strcasecmp( value, "true") || !strcmp (value, "1") ||
!strcasecmp( value, _("on")) || !strcasecmp (value, _("yes")) ||
!strcasecmp( value, _("true"))
)
t = 1;
/*fprintf (stderr," value %s, t %d\n", value, t);*/
if (t == 2) {
gp_context_error( m_pContext, _("The passed value %s is not a valid toggle value."), value );
ret = GP_ERROR_BAD_PARAMETERS;
break;
}
ret = gp_widget_set_value( child, &t );
if (ret != GP_OK)
gp_context_error( m_pContext, _("Failed to set values %s of toggle widget %s."), value, name );
break;
}
case GP_WIDGET_DATE: { /* int */
time_t t = -1;
struct tm xtm;
memset(&xtm,0,sizeof(xtm));
/* We need to set UNIX time in seconds since Epoch */
/* We get ... local time */
if (!strcasecmp (value, "now") || !strcasecmp (value, _("now")))
t = time(NULL);
#ifdef HAVE_STRPTIME
else if( strptime( value, "%c", &xtm) || strptime (value, "%Ec", &xtm) ){
xtm.tm_isdst = -1;
t = mktime (&xtm);
};
#endif
if( t == -1 ){
unsigned long lt;
if( !sscanf(value, "%ld", &lt) ){
gp_context_error( m_pContext, _("The passed value %s is neither a valid time nor an integer."), value );
ret = GP_ERROR_BAD_PARAMETERS;
break;
};
t = lt;
};
ret = gp_widget_set_value( child, &t );
if( ret != GP_OK ) gp_context_error( m_pContext, _("Failed to set new time of date/time widget %s to %s."), name, value );
break;
}
case GP_WIDGET_MENU:
case GP_WIDGET_RADIO: { /* char * */
int cnt, i;
char *endptr;
char tBuff[256];
cnt = gp_widget_count_choices( child );
if( cnt < GP_OK ){
ret = cnt;
break;
};
ret = GP_ERROR_BAD_PARAMETERS;
for( i=0; i<cnt; i++ ){
const char *choice;
ret = gp_widget_get_choice( child, i, &choice );
LOG( choice );
if( ret != GP_OK ) continue;
if( !strcmp( choice, value ) ){
ret = gp_widget_set_value( child, value );
LOG("Test Vals");
LOG( child->value_string );
sprintf( tBuff, "%d", child->value_int );
LOG( tBuff );
sprintf( tBuff, "%0.3f", child->value_float );
LOG( tBuff );
break;
};
};
if( i != cnt ) break;
/* make sure we parse just 1 integer, and there is nothing more.
* sscanf just does not provide this, we need strtol.
*/
i = strtol( value, &endptr, 10 );
if( (value != endptr) && (*endptr == '\0') ){
if( (i>= 0) && (i < cnt) ){
const char *choice;
ret = gp_widget_get_choice( child, i, &choice );
if( ret == GP_OK ) ret = gp_widget_set_value (child, choice);
LOG("Test Vals");
LOG( child->value_string );
sprintf( tBuff, "%d", child->value_int );
LOG( tBuff );
sprintf( tBuff, "%0.3f", child->value_float );
LOG( tBuff );
break;
};
};
/* Lets just try setting the value directly, in case we have flexible setters,
* like PTP shutterspeed. */
ret = gp_widget_set_value( child, value );
LOG("Test Vals");
LOG( child->value_string );
sprintf( tBuff, "%d", child->value_int );
LOG( tBuff );
sprintf( tBuff, "%0.3f", child->value_float );
LOG( tBuff );
if (ret == GP_OK) break;
gp_context_error( m_pContext, _("Choice %s not found within list of choices."), value );
break;
};
/* ignore: */
case GP_WIDGET_WINDOW:
case GP_WIDGET_SECTION:
case GP_WIDGET_BUTTON:
gp_context_error( m_pContext, _("The %s widget is not configurable."), name );
ret = GP_ERROR_BAD_PARAMETERS;
break;
};
if( ret == GP_OK ){
if (child == rootconfig) ret = gp_camera_set_single_config( m_pCamera, name, child, m_pContext );
else ret = gp_camera_set_config( m_pCamera, rootconfig, m_pContext );
if( ret != GP_OK ) gp_context_error( m_pContext, _("Failed to set new configuration value %s for configuration entry %s."), value, name );
};
gp_widget_free( rootconfig );
return (ret);
};
int gPhotoCamera::set_config_index_action( const char *name, const char *value ){
CameraWidget *rootconfig,*child;
int ret;
const char *label;
CameraWidgetType type;
ret = _find_widget_by_name( name, &child, &rootconfig );
if( ret != GP_OK ) return ret;
ret = gp_widget_get_type( child, &type );
if( ret != GP_OK ){
gp_widget_free (rootconfig);
return ret;
};
ret = gp_widget_get_label( child, &label );
if( ret != GP_OK ){
gp_widget_free (rootconfig);
return ret;
};
switch( type ){
case GP_WIDGET_MENU:
case GP_WIDGET_RADIO: { /* char * */
int cnt, i;
cnt = gp_widget_count_choices( child );
if (cnt < GP_OK) {
ret = cnt;
break;
};
ret = GP_ERROR_BAD_PARAMETERS;
if( sscanf (value, "%d", &i) ){
if( (i>= 0) && (i < cnt) ){
const char *choice;
ret = gp_widget_get_choice( child, i, &choice );
if (ret == GP_OK) ret = gp_widget_set_value( child, choice );
break;
};
};
gp_context_error( m_pContext, _("Choice %s not found within list of choices."), value );
break;
}
/* ignore: */
case GP_WIDGET_TOGGLE:
case GP_WIDGET_TEXT:
case GP_WIDGET_RANGE:
case GP_WIDGET_DATE:
case GP_WIDGET_WINDOW:
case GP_WIDGET_SECTION:
case GP_WIDGET_BUTTON:
gp_context_error( m_pContext, _("The %s widget has no indexed list of choices. Use --set-config-value instead."), name );
ret = GP_ERROR_BAD_PARAMETERS;
break;
};
if (ret == GP_OK) {
if( child == rootconfig ) ret = gp_camera_set_single_config( m_pCamera, name, child, m_pContext );
else ret = gp_camera_set_config( m_pCamera, rootconfig, m_pContext );
if (ret != GP_OK) gp_context_error ( m_pContext, _("Failed to set new configuration value %s for configuration entry %s."), value, name);
};
gp_widget_free (rootconfig);
return (ret);
};
int gPhotoCamera::set_config_value_action( const char *name, const char *value ){
CameraWidget *rootconfig,*child;
int ret;
CameraWidgetType type;
ret = _find_widget_by_name( name, &child, &rootconfig );
if( ret != GP_OK ) return ret;
ret = gp_widget_get_type( child, &type );
if( ret != GP_OK ){
gp_widget_free (rootconfig);
return ret;
};
switch (type) {
case GP_WIDGET_TEXT: { /* char * */
ret = gp_widget_set_value( child, value );
if( ret != GP_OK ) gp_context_error( m_pContext, _("Failed to set the value of text widget %s to %s."), name, value );
break;
};
case GP_WIDGET_RANGE: { /* float */
float f,t,b,s;
ret = gp_widget_get_range( child, &b, &t, &s );
if( ret != GP_OK ) break;
if( !sscanf( value, "%f", &f ) ){
gp_context_error( m_pContext, _("The passed value %s is not a floating point value."), value );
ret = GP_ERROR_BAD_PARAMETERS;
break;
};
if( (f < b) || (f > t) ){
gp_context_error( m_pContext, _("The passed value %f is not within the expected range %f - %f."), f, b, t );
ret = GP_ERROR_BAD_PARAMETERS;
break;
};
ret = gp_widget_set_value( child, &f );
if( ret != GP_OK ) gp_context_error( m_pContext, _("Failed to set the value of range widget %s to %f."), name, f );
break;
};
case GP_WIDGET_TOGGLE: { /* int */
int t;
t = 2;
if( !strcasecmp( value, "off") || !strcasecmp( value, "no") ||
!strcasecmp( value, "false") || !strcmp( value, "0") ||
!strcasecmp( value, _("off")) || !strcasecmp( value, _("no")) ||
!strcasecmp( value, _("false"))
)
t = 0;
if( !strcasecmp( value, "on") || !strcasecmp( value, "yes") ||
!strcasecmp( value, "true") || !strcmp( value, "1") ||
!strcasecmp( value, _("on")) || !strcasecmp( value, _("yes")) ||
!strcasecmp( value, _("true"))
)
t = 1;
/*fprintf (stderr," value %s, t %d\n", value, t);*/
if( t == 2 ){
gp_context_error( m_pContext, _("The passed value %s is not a valid toggle value."), value );
ret = GP_ERROR_BAD_PARAMETERS;
break;
};
ret = gp_widget_set_value( child, &t );
if( ret != GP_OK ) gp_context_error( m_pContext, _("Failed to set values %s of toggle widget %s."), value, name );
break;
}
case GP_WIDGET_DATE: { /* int */
int t = -1;
struct tm xtm;
if( !strcasecmp( value, "now" ) || !strcasecmp( value, _("now"))) t = time(NULL);
#ifdef HAVE_STRPTIME
else if( strptime( value, "%c", &xtm ) || strptime( value, "%Ec", &xtm )) t = mktime (&xtm);
#endif
if( t == -1 ){
if( !sscanf( value, "%d", &t) ){
gp_context_error( m_pContext, _("The passed value %s is neither a valid time nor an integer."), value );
ret = GP_ERROR_BAD_PARAMETERS;
break;
};
};
ret = gp_widget_set_value( child, &t );
if( ret != GP_OK ) gp_context_error( m_pContext, _("Failed to set new time of date/time widget %s to %s."), name, value );
break;
}
case GP_WIDGET_MENU:
case GP_WIDGET_RADIO: { /* char * */
int cnt, i;
cnt = gp_widget_count_choices( child );
if( cnt < GP_OK ){
ret = cnt;
break;
};
ret = GP_ERROR_BAD_PARAMETERS;
for( i=0; i<cnt; i++ ){
const char *choice;
ret = gp_widget_get_choice( child, i, &choice );
if( ret != GP_OK ) continue;
if( !strcmp( choice, value) ){
ret = gp_widget_set_value( child, value );
break;
};
};
if( i != cnt ) break;
/* Lets just try setting the value directly, in case we have flexible setters,
* like PTP shutterspeed. */
ret = gp_widget_set_value( child, value );
if( ret == GP_OK ) break;
gp_context_error( m_pContext, _("Choice %s not found within list of choices."), value );
break;
};
/* ignore: */
case GP_WIDGET_WINDOW:
case GP_WIDGET_SECTION:
case GP_WIDGET_BUTTON:
gp_context_error( m_pContext, _("The %s widget is not configurable."), name );
ret = GP_ERROR_BAD_PARAMETERS;
break;
};
if( ret == GP_OK ){
if( child == rootconfig ) ret = gp_camera_set_single_config( m_pCamera, name, child, m_pContext );
else ret = gp_camera_set_config( m_pCamera, rootconfig, m_pContext );
if (ret != GP_OK) gp_context_error ( m_pContext, _("Failed to set new configuration value %s for configuration entry %s."), value, name );
};
gp_widget_free( rootconfig );
return (ret);
};
CameraAbilitiesList *gPhotoCamera::gp_params_abilities_list(){
/* If p == NULL, the behaviour of this function is as undefined as
* the expression p->abilities_list would have been. */
if( m_pAbilitiesList == NULL ){
gp_abilities_list_new( &m_pAbilitiesList );
gp_abilities_list_load( m_pAbilitiesList, m_pContext);
};
return m_pAbilitiesList;
};
/*! \brief Create local filename for CameraFile according to pattern in gp_params
*
* \param folder Name of the folder on the camera the CameraFile is stored in
* \param file CameraFile to find a local name for.
* \param path The pointer to the generated complete path name of the local filename.
* \return GPError code
*
* \warning This function reads the static variable gp_params.
*/
/*
int gPhotoCamera::get_path_for_file (const char *folder, const char *name, CameraFileType type, CameraFile *file, char **path){
unsigned int i, l;
char *s = NULL, b[1024];
time_t t = 0;
struct tm *tm;
int hour12;
static int filenr = 1;
if( !path )return (GP_ERROR_BAD_PARAMETERS);
*path = NULL;
if( file ){
CR( gp_file_get_mtime( file, &t ) );
if (!t) // use the current time as fallback if the camera did not return it.
t = time(NULL);
};
tm = localtime (&t);
hour12 = tm->tm_hour % 12;
if (hour12 == 0) {
hour12 = 12;
};
// If the user didn't specify a filename, use the original name (and prefix).
if( !gp_params.filename || !strcmp( gp_params.filename, "" ) ){
if (file) {
return gp_file_get_name_by_type (file, name, type, path);
} else if ((type == GP_FILE_TYPE_NORMAL) && strchr(name,'.')) {
// return the original name
*path = strdup(name);
return (GP_OK);
} else
// Download required to get the path from CameraFile *file
return (GP_ERROR_BAD_PARAMETERS);
};
// The user did specify a filename. Use it.
b[sizeof (b) - 1] = '\0';
for (i = 0; i < strlen (gp_params.filename); i++) {
if (gp_params.filename[i] == '%') {
char padding = '0'; // default padding character
int precision = 0; // default: no padding
i++;
// determine padding character
switch (gp_params.filename[i]) {
// case ' ':
// spaces are not supported everywhere, so we
// restrict ourselves to padding with zeros.
case '0':
padding = gp_params.filename[i];
precision = 1; // do padding
i++;
break;
}
// determine padding width
if (isdigit((int)gp_params.filename[i])) {
char *cp;
long int _prec;
_prec = strtol(&gp_params.filename[i],
&cp, 10);
if (_prec < 1)
precision = 1;
else if (_prec > 20)
precision = 20;
else
precision = _prec;
if (*cp != 'n') {
// make sure this is %n
gp_context_error (gp_params.context,
_("Zero padding numbers "
"in file names is only "
"possible with %%n."));
return GP_ERROR_BAD_PARAMETERS;
}
// go to first non-digit character
i += (cp - &gp_params.filename[i]);
} else if (precision && ( gp_params.filename[i] != 'n')) {
gp_context_error (gp_params.context,
_("You cannot use %%n "
"zero padding "
"without a "
"precision value!"
));
return GP_ERROR_BAD_PARAMETERS;
}
switch (gp_params.filename[i]) {
case 'n':
// Previously this used an folder index number.
// Now this uses a linear increasing number.
if (precision > 1) {
char padfmt[16];
strcpy(padfmt, "%!.*i");
padfmt[1] = padding;
snprintf (b, sizeof (b), padfmt,
precision, filenr);
} else {
snprintf (b, sizeof (b), "%i",
filenr);
}
filenr++;
break;
case 'C':
// Get the suffix of the original name
s = strrchr (name, '.');
if (!s) {
free (*path);
*path = NULL;
gp_context_error (gp_params.context,
_("The filename provided "
"by the camera ('%s') "
"does not contain a "
"suffix!"), name);
return (GP_ERROR_BAD_PARAMETERS);
}
strncpy (b, s + 1, sizeof (b) - 1);
break;
case 'f':
// Get the file name without suffix
s = strrchr (name, '.');
if (!s)
strncpy (b, name, sizeof (b) - 1);
else {
l = MIN ((unsigned long)(sizeof(b) - 1),
(unsigned long)(s - name));
strncpy (b, name, l);
b[l] = '\0';
}
break;
case 'F':
// Get the folder name
{
const char *f = folder ? folder : "";
if (f[0] == '/') f++; // Skip first '/'
if (!f[0]) f = "."; // replace empty folder name by '.'
strncpy (b, f, sizeof (b) - 1 - strlen(b));
b[sizeof(b) - 1] = '\0';
}
break;
case 'a':
case 'A':
case 'b':
case 'B':
case 'd':
case 'H':
case 'k':
case 'I':
case 'l':
case 'j':
case 'm':
case 'M':
case 'S':
case 'y':
case 'Y':
{
char fmt[3] = { '%', '\0', '\0' };
if (!file) return (GP_ERROR_BAD_PARAMETERS); // mtime unknown
fmt[1] = gp_params.filename[i]; // the letter of this 'case'
strftime(b, sizeof (b), fmt, tm);
break;
}
case '%':
strcpy (b, "%");
break;
case ':':
strncpy_lower(b, name, sizeof(b));
b[sizeof(b)-1] = '\0';
break;
default:
free (*path);
*path = NULL;
gp_context_error (gp_params.context,
_("Invalid format '%s' (error at "
"position %i)."), gp_params.filename,
i + 1);
return (GP_ERROR_BAD_PARAMETERS);
}
} else {
b[0] = gp_params.filename[i];
b[1] = '\0';
}
s = *path ? realloc (*path, strlen (*path) + strlen (b) + 1) :
malloc (strlen (b) + 1);
if (!s) {
free (*path);
*path = NULL;
return (GP_ERROR_NO_MEMORY);
}
if (*path) {
*path = s;
strcat (*path, b);
} else {
*path = s;
strcpy (*path, b);
}
}
// If the file is a capture_preview,
// apply prefix over the calculated basename
if (type == GP_FILE_TYPE_PREVIEW) {
return gp_file_get_name_by_type (file, s, type, path);
}
return (GP_OK);
};
int gPhotoCamera::save_camera_file_to_file ( const char *folder, const char *name, CameraFileType type, CameraFile *file, const char *curname ){
char * path = NULL;
char s[1024];
char c[1024];
int res;
time_t mtime;
struct utimbuf u;
CR (get_path_for_file (folder, name, type, file, &path));
strncpy (s, path, sizeof (s) - 1);
s[sizeof (s) - 1] = '\0';
free (path);
path = NULL;
if( (gp_params.flags & FLAGS_SKIP_EXISTING) && gp_system_is_file(s) ){
#ifdef DEBUG
char tBuff[128];
sprintf( tBuff, "Skip existing file %s", s );
LOG( tBuff );
#endif
if( curname ) unlink( curname );
return( GP_OK );
};
if( (gp_params.flags & FLAGS_QUIET) == 0 ){
while((gp_params.flags & FLAGS_FORCE_OVERWRITE) == 0 && gp_system_is_file (s) ){
do{
putchar ('\007');
printf (_("File %s exists. Overwrite? [y|n] "), s);
fflush (stdout);
if (NULL == fgets (c, sizeof (c) - 1, stdin)) return GP_ERROR;
} while ((c[0]!='y')&&(c[0]!='Y')&& (c[0]!='n')&&(c[0]!='N'));
if ((c[0]=='y') || (c[0]=='Y')) break;
do {
printf (_("Specify new filename? [y|n] "));
fflush (stdout);
if (NULL == fgets (c, sizeof (c) - 1, stdin)) return GP_ERROR;
} while ((c[0]!='y')&&(c[0]!='Y')&& (c[0]!='n')&&(c[0]!='N'));
if (!((c[0]=='y') || (c[0]=='Y'))) {
if (curname) unlink (curname);
return (GP_OK);
};
printf (_("Enter new filename: "));
fflush (stdout);
if (NULL == fgets (s, sizeof (s) - 1, stdin)) return GP_ERROR;
s[strlen (s) - 1] = 0;
};
printf (_("Saving file as %s\n"), s);
fflush (stdout);
};
path = s;
while( (path = strchr(path, gp_system_dir_delim)) ){
*path = '\0';
if(!gp_system_is_dir (s)) gp_system_mkdir (s);
*path++ = gp_system_dir_delim;
};
if (curname) {
int x;
unlink(s);
if (-1 == rename (curname, s)) {
// happens if the user specified a absolute path with --filename
// EPERM happens on windows, see https://github.com/gphoto/libgphoto2/issues/97
if ((errno == EXDEV) || (errno == EPERM)) {
char buf[8192];
int in_fd, out_fd;
in_fd = open(curname, O_RDONLY);
if (in_fd < 0) LOG_ERROR("Can't open file for reading");
out_fd = open(s, O_CREAT | O_WRONLY, 0644);
if (out_fd < 0) LOG_ERROR("Can't open file for writing");
while (1) {
ssize_t result = read(in_fd, buf, sizeof(buf));
if (!result) break;
if (-1 == write(out_fd, buf, result)) {
LOG_ERROR("write");
break;
}
}
close(out_fd);
close(in_fd);
unlink(curname);
} else
perror("rename");
}
x = umask(0022); // get umask
umask(x); // set it back to the old value
chmod(s,0666 & ~x);
}
res = gp_file_get_mtime (file, &mtime);
if ((res == GP_OK) && (mtime)) {
u.actime = mtime;
u.modtime = mtime;
utime (s, &u);
}
gp_params_run_hook(&gp_params, "download", s);
return (GP_OK);
}
int gPhotoCamera::camera_file_exists( Camera *camera, GPContext *context, const char *folder, const char *filename, CameraFileType type ){
CameraFileInfo info;
CR (gp_camera_file_get_info (camera, folder, filename, &info,
context));
switch (type) {
case GP_FILE_TYPE_METADATA:
return TRUE;
case GP_FILE_TYPE_AUDIO:
return (info.audio.fields != 0);
case GP_FILE_TYPE_PREVIEW:
return (info.preview.fields != 0);
case GP_FILE_TYPE_RAW:
case GP_FILE_TYPE_NORMAL:
return (info.file.fields != 0);
default:
gp_context_error (context, "Unknown file type in camera_file_exists: %d", type);
return FALSE;
}
}
static int x_read(void*priv,unsigned char *data, uint64_t *size) {
struct privstr *ps = priv;
int fd = ps->fd;
uint64_t curread = 0, xsize, res;
gp_log (GP_LOG_DEBUG, "x_read", "(%p,%p,%u)", priv, data, (unsigned int)*size);
xsize = *size;
while (curread < xsize) {
res = read (fd, data+curread, xsize-curread);
if (res == -1) return GP_ERROR_IO_READ;
if (!res) break;
curread += res;
}
*size = curread;
return GP_OK;
}
static int x_write(void*priv,unsigned char *data, uint64_t *size) {
struct privstr *ps = priv;
int fd = ps->fd;
uint64_t curwritten = 0, xsize, res;
gp_log (GP_LOG_DEBUG, "x_write","(%p,%p,%u)", priv, data, (unsigned int)*size);
xsize = *size;
while (curwritten < xsize) {
res = write (fd, data+curwritten, xsize-curwritten);
if (res == -1) return GP_ERROR_IO_WRITE;
if (!res) break;
curwritten += res;
}
*size = curwritten;
return GP_OK;
}
static CameraFileHandler xhandler = { x_size, x_read, x_write };
int gPhotoCamera::save_file_to_file( Camera *camera, GPContext *context, Flags flags, const char *folder, const char *filename, CameraFileType type ){
int fd, res;
CameraFile * file;
char tmpname[20], * tmpfilename;
struct privstr * ps = NULL;
if( ( flags & FLAGS_SKIP_EXISTING ) && !(flags & FLAGS_STDOUT) ){
char *path = NULL;
// Check if the file is present before downloading it.
res = get_path_for_file (folder, filename, type, NULL, &path);
if( res == (GP_OK) && gp_system_is_file( path ) ){
// File name pattern do not require CameraFile and target file exists: Skip this file.
if( (gp_params.flags & FLAGS_QUIET) == 0 ){
printf (_("Skip existing file %s\n"), path);
fflush (stdout);
};
return (GP_OK);
};
};
if( flags & FLAGS_NEW ){
CameraFileInfo info;
CR( gp_camera_file_get_info(camera, folder, filename, &info, context) );
switch( type ){
case GP_FILE_TYPE_PREVIEW:
if (info.preview.fields & GP_FILE_INFO_STATUS && info.preview.status == GP_FILE_STATUS_DOWNLOADED) return (GP_OK);
break;
case GP_FILE_TYPE_NORMAL:
case GP_FILE_TYPE_RAW:
case GP_FILE_TYPE_EXIF:
if (info.file.fields & GP_FILE_INFO_STATUS && info.file.status == GP_FILE_STATUS_DOWNLOADED) return (GP_OK);
break;
case GP_FILE_TYPE_AUDIO:
if (info.audio.fields & GP_FILE_INFO_STATUS && info.audio.status == GP_FILE_STATUS_DOWNLOADED) return (GP_OK);
break;
default:
return (GP_ERROR_NOT_SUPPORTED);
};
};
strcpy (tmpname, "tmpfileXXXXXX");
fd = mkstemp(tmpname);
if (fd == -1) {
if (errno == EACCES) {
gp_context_error( context, _("Permission denied") );
return GP_ERROR;
};
CR( gp_file_new (&file) );
tmpfilename = NULL;
} else {
if (time(NULL) & 1) { // to test both methods.
gp_log (GP_LOG_DEBUG, "save_file_to_file","using fd method");
res = gp_file_new_from_fd (&file, fd);
if (res < GP_OK) {
close (fd);
unlink (tmpname);
return res;
}
} else {
gp_log (GP_LOG_DEBUG, "save_file_to_file","using handler method");
ps = malloc (sizeof(*ps));
if (!ps) return GP_ERROR_NO_MEMORY;
ps->fd = fd;
// just pass in the file pointer as private
res = gp_file_new_from_handler (&file, &xhandler, ps);
if (res < GP_OK) {
close (fd);
unlink (tmpname);
return res;
}
}
tmpfilename = tmpname;
}
res = gp_camera_file_get (camera, folder, filename, type,
file, context);
if( res < GP_OK ){
free (ps);
gp_file_unref( file );
if( tmpfilename ) unlink( tmpfilename );
return res;
};
if (flags & FLAGS_STDOUT) {
const char *data;
unsigned long int size;
CR (gp_file_get_data_and_size (file, &data, &size));
if (flags & FLAGS_STDOUT_SIZE) // this will be difficult in fd mode
printf ("%li\n", size);
if (1!=fwrite (data, size, 1, stdout))
fprintf(stderr,"fwrite failed writing to stdout.\n");
if (ps && ps->fd) close (ps->fd);
free (ps);
gp_file_unref (file);
unlink (tmpname);
return (GP_OK);
}
res = save_camera_file_to_file (folder, filename, type, file, tmpfilename);
if (ps && ps->fd) close (ps->fd);
free (ps);
gp_file_unref (file);
if ((res!=GP_OK) && tmpfilename)
unlink (tmpfilename);
return (res);
}
*/
bool gPhotoCamera::isDead(){
return false;
};
#undef LOG
#undef LOG_ERROR