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

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "gPhotoCamera.h"
  2. #define LOG( X ) m_pLog->Log( "GPHO", this, X )
  3. #define LOG_ERROR( X ) m_pLog->Log( "GPHO", this, _LOG_ERROR, X )
  4. struct _CameraWidget{
  5. CameraWidgetType type;
  6. char label [256];
  7. char info [1024];
  8. char name [256];
  9. CameraWidget *parent;
  10. /* Current value of the widget */
  11. char *value_string;
  12. int value_int;
  13. float value_float;
  14. /* For Radio and Menu */
  15. char **choice;
  16. int choice_count;
  17. /* For Range */
  18. float min;
  19. float max;
  20. float increment;
  21. /* Child info */
  22. CameraWidget **children;
  23. int children_count;
  24. /* Widget was changed */
  25. int changed;
  26. /* Widget is read only */
  27. int readonly;
  28. /* Reference count */
  29. int ref_count;
  30. /* Unique identifier */
  31. int id;
  32. /* Callback */
  33. CameraWidgetCallback callback;
  34. };
  35. static void errordumper( GPLogLevel level, const char *domain, const char *str, void *data) {
  36. basicLogCollection::getInstance()->Log( "GPHO_LIB", NULL, str );
  37. };
  38. static void error_func( GPContext *context, const char *format, va_list args, void *data) {
  39. char tBuff[256];
  40. sprintf( tBuff, format, args );
  41. basicLogCollection::getInstance()->Log( "GPHO_CTX_ERR", NULL, tBuff );
  42. };
  43. static void message_func( GPContext *context, const char *format, va_list args, void *data) {
  44. char tBuff[256];
  45. sprintf( tBuff, format, args );
  46. basicLogCollection::getInstance()->Log( "GPHO_CTX_MSG", NULL, tBuff );
  47. };
  48. /*
  49. * This function looks up a label or key entry of
  50. * a configuration widget.
  51. * The functions descend recursively, so you can just
  52. * specify the last component.
  53. */
  54. int gPhotoCamera::_lookup_widget(CameraWidget*widget, const char *key, CameraWidget **child) {
  55. int ret;
  56. ret = gp_widget_get_child_by_name( widget, key, child );
  57. if( ret < GP_OK ) ret = gp_widget_get_child_by_label( widget, key, child );
  58. return ret;
  59. };
  60. /* Gets a string configuration value.
  61. * This can be:
  62. * - A Text widget
  63. * - The current selection of a Radio Button choice
  64. * - The current selection of a Menu choice
  65. *
  66. * Sample (for Canons eg):
  67. * get_config_value_string (camera, "owner", &ownerstr, context);
  68. */
  69. int gPhotoCamera::get_config_value_string (Camera *camera, const char *key, char **str, GPContext *context ) {
  70. CameraWidget * widget = NULL, *child = NULL;
  71. CameraWidgetType type;
  72. int ret;
  73. char * val;
  74. ret = gp_camera_get_single_config( camera, key, &child, context );
  75. if (ret == GP_OK) {
  76. if (!child) LOG_ERROR( "child is NULL?");
  77. widget = child;
  78. } else {
  79. ret = gp_camera_get_config( camera, &widget, context );
  80. if (ret < GP_OK) {
  81. LOG_ERROR( "camera_get_config failed" );
  82. return ret;
  83. };
  84. ret = _lookup_widget( widget, key, &child );
  85. if (ret < GP_OK) {
  86. LOG_ERROR( "lookup widget failed" );
  87. goto out;
  88. };
  89. };
  90. /* This type check is optional, if you know what type the label
  91. * has already. If you are not sure, better check. */
  92. ret = gp_widget_get_type( child, &type );
  93. if (ret < GP_OK) {
  94. LOG_ERROR( "Widget get type failed" );
  95. goto out;
  96. }
  97. switch (type) {
  98. case GP_WIDGET_MENU:
  99. case GP_WIDGET_RADIO:
  100. case GP_WIDGET_TEXT:
  101. break;
  102. default:
  103. LOG_ERROR( "widget has bad type!" );
  104. ret = GP_ERROR_BAD_PARAMETERS;
  105. goto out;
  106. }
  107. /* This is the actual query call. Note that we just
  108. * a pointer reference to the string, not a copy... */
  109. ret = gp_widget_get_value( child, &val );
  110. if (ret < GP_OK) {
  111. LOG_ERROR( "could not query widget value" );
  112. goto out;
  113. }
  114. /* Create a new copy for our caller. */
  115. *str = strdup (val);
  116. out:
  117. gp_widget_free (widget);
  118. return ret;
  119. };
  120. gPhotoCamera::gPhotoCamera( config_setting_t *Settings ): basicThread(){
  121. m_pLog = basicLogCollection::getInstance();
  122. m_pSettings = basicSettingsProvider::getInstance()->getSubNode( Settings, "GP_CAMERA" );
  123. m_pBLOG = objectPathfinderBinaryLog::getInstance();
  124. m_nCameraID = 0;
  125. m_nFolderID = 0;
  126. m_ePortType = GP_PORT_NONE;
  127. m_pDBC = NULL;
  128. m_pCamera = NULL;
  129. m_pContext = NULL;
  130. m_pPortInfoList = NULL;
  131. m_pAbilitiesList = NULL;
  132. m_bBurstCapturing = false;
  133. m_pChannelIsBusy = false;
  134. m_bOldWMode =GP_WM_NA;
  135. m_bWMode = GP_WM_CTRL;
  136. #ifdef DEBUG
  137. m_nFlags = FLAGS_NEW;
  138. #else
  139. m_nFlags = FLAGS_NEW | FLAGS_QUIET;
  140. #endif
  141. };
  142. gPhotoCamera::gPhotoCamera( PATHFINDER_DBC *DBC ): basicThread(){
  143. m_pLog = basicLogCollection::getInstance();
  144. m_pSettings = basicSettingsProvider::getInstance()->getSubNode( NULL, "GP_CAMERA" );
  145. m_pBLOG = objectPathfinderBinaryLog::getInstance();
  146. m_nCameraID = 0;
  147. m_nFolderID = 0;
  148. m_ePortType = GP_PORT_NONE;
  149. m_pDBC = DBC;
  150. m_pCamera = NULL;
  151. m_pContext = NULL;
  152. m_pPortInfoList = NULL;
  153. m_pAbilitiesList = NULL;
  154. m_bBurstCapturing = false;
  155. m_pChannelIsBusy = false;
  156. m_bOldWMode =GP_WM_NA;
  157. m_bWMode = GP_WM_CTRL;
  158. #ifdef DEBUG
  159. m_nFlags = FLAGS_NEW;
  160. #else
  161. m_nFlags = FLAGS_NEW | FLAGS_QUIET;
  162. #endif
  163. };
  164. gPhotoCamera::gPhotoCamera( config_setting_t *Settings, PATHFINDER_DBC *DBC ): basicThread(){
  165. m_pLog = basicLogCollection::getInstance();
  166. m_pSettings = basicSettingsProvider::getInstance()->getSubNode( Settings, "GP_CAMERA" );
  167. m_pBLOG = objectPathfinderBinaryLog::getInstance();
  168. m_nCameraID = 0;
  169. m_ePortType = GP_PORT_NONE;
  170. m_pDBC = DBC;
  171. m_pCamera = NULL;
  172. m_pContext = NULL;
  173. m_pPortInfoList = NULL;
  174. m_pAbilitiesList = NULL;
  175. m_bBurstCapturing = false;
  176. m_pChannelIsBusy = false;
  177. m_bOldWMode =GP_WM_NA;
  178. m_bWMode = GP_WM_CTRL;
  179. char tBuffer[1024];
  180. const char * tBuf;
  181. config_setting_t * pSetting;
  182. if( !config_setting_lookup_string( m_pSettings, GPHOTO_TAG_MOUNT_POINT, &tBuf ) ){
  183. sprintf( m_pCamMountPoint, "/srv/pathfinder/data/camera" );
  184. pSetting = config_setting_add( m_pSettings, GPHOTO_TAG_MOUNT_POINT, CONFIG_TYPE_STRING );
  185. config_setting_set_string( pSetting, m_pCamMountPoint );
  186. basicSettingsProvider::getInstance()->compromise();
  187. LOG("Camera Mount Point not defined - generating new!");
  188. } else {
  189. sprintf( m_pCamMountPoint, tBuf );
  190. sprintf( tBuffer, "Camera Mount Point Read From Settings: %s", m_pCamMountPoint );
  191. LOG( tBuffer );
  192. };
  193. pSetting = config_setting_get_member(m_pSettings, GPHOTO_TAG_MOUNT_POINT_UID );
  194. if ( pSetting == NULL ){
  195. m_nAccessUID = 65534;
  196. pSetting = config_setting_add( m_pSettings, GPHOTO_TAG_MOUNT_POINT_UID, CONFIG_TYPE_INT );
  197. config_setting_set_int( pSetting, m_nAccessUID );
  198. basicSettingsProvider::getInstance()->compromise();
  199. LOG("Camera Mount Point UID not defined - generating new!");
  200. } else {
  201. m_nAccessUID = config_setting_get_int( pSetting );
  202. sprintf( tBuffer, "Camera Mount Point UID Read From Settings: %d", m_nAccessUID );
  203. LOG( tBuffer );
  204. };
  205. pSetting = config_setting_get_member(m_pSettings, GPHOTO_TAG_MOUNT_POINT_GID );
  206. if ( pSetting == NULL ){
  207. m_nAccessGID = 65534;
  208. pSetting = config_setting_add( m_pSettings, GPHOTO_TAG_MOUNT_POINT_GID, CONFIG_TYPE_INT );
  209. config_setting_set_int( pSetting, m_nAccessGID );
  210. basicSettingsProvider::getInstance()->compromise();
  211. LOG("Camera Mount Point GID not defined - generating new!");
  212. } else {
  213. m_nAccessGID = config_setting_get_int( pSetting );
  214. sprintf( tBuffer, "Camera Mount Point GID Read From Settings: %d", m_nAccessGID );
  215. LOG( tBuffer );
  216. };
  217. #ifdef DEBUG
  218. m_nFlags = FLAGS_NEW;
  219. #else
  220. m_nFlags = FLAGS_NEW | FLAGS_QUIET;
  221. #endif
  222. };
  223. gPhotoCamera::~gPhotoCamera(){
  224. m_bThreadStatus = TH_ENDED;
  225. m_mCommandLock.unlock();
  226. waitThreadEnd( true );
  227. if( m_pPortInfoList ) gp_port_info_list_free( m_pPortInfoList );
  228. if( m_pAbilitiesList ) gp_abilities_list_free( m_pAbilitiesList );
  229. };
  230. bool gPhotoCamera::create(){
  231. #ifdef DEBUG
  232. LOG("Create()");
  233. #endif
  234. if( m_bThreadStatus > TH_ARMED ) return true;
  235. return basicThread::init();
  236. };
  237. bool gPhotoCamera::makeShot(){
  238. if( m_pCamera == NULL ) return false;
  239. if( m_pChannelIsBusy ) return false;
  240. if( m_bBurstCapturing ) return false;
  241. m_mCommandLock.lock();
  242. GP_CAM_COMMAND * tCMD;
  243. tCMD = (GP_CAM_COMMAND*) malloc(sizeof(GP_CAM_COMMAND));
  244. if( tCMD != NULL ) {
  245. memset( tCMD, 0 , sizeof(GP_CAM_COMMAND) );
  246. tCMD->cmd = GP_SINGLE_SHOT;
  247. m_qCommands.push( tCMD );
  248. };
  249. m_mCommandLock.unlock();
  250. return true;
  251. };
  252. bool gPhotoCamera::makePreview(){
  253. if( m_pCamera == NULL ) return false;
  254. if( m_pChannelIsBusy ) return false;
  255. if( m_bBurstCapturing ) return false;
  256. m_mCommandLock.lock();
  257. GP_CAM_COMMAND * tCMD;
  258. tCMD = (GP_CAM_COMMAND*) malloc(sizeof(GP_CAM_COMMAND));
  259. if( tCMD != NULL ) {
  260. memset( tCMD, 0 , sizeof(GP_CAM_COMMAND) );
  261. tCMD->cmd = GP_PREVIEW_SHOT;
  262. m_qCommands.push( tCMD );
  263. };
  264. m_mCommandLock.unlock();
  265. return true;
  266. };
  267. bool gPhotoCamera::startPreview(){
  268. if( m_pCamera == NULL ) return false;
  269. if( m_pChannelIsBusy ) return false;
  270. if( m_bBurstCapturing ) return false;
  271. m_mCommandLock.lock();
  272. GP_CAM_COMMAND * tCMD;
  273. tCMD = (GP_CAM_COMMAND*) malloc(sizeof(GP_CAM_COMMAND));
  274. if( tCMD != NULL ) {
  275. memset( tCMD, 0 , sizeof(GP_CAM_COMMAND) );
  276. tCMD->cmd = GP_START_PREVIEW;
  277. m_qCommands.push( tCMD );
  278. };
  279. m_mCommandLock.unlock();
  280. return true;
  281. };
  282. bool gPhotoCamera::stopPreview(){
  283. if( m_pCamera == NULL ) return false;
  284. if( m_pChannelIsBusy ) return false;
  285. if( m_bBurstCapturing ) return false;
  286. m_mCommandLock.lock();
  287. GP_CAM_COMMAND * tCMD;
  288. tCMD = (GP_CAM_COMMAND*) malloc(sizeof(GP_CAM_COMMAND));
  289. if( tCMD != NULL ) {
  290. memset( tCMD, 0 , sizeof(GP_CAM_COMMAND) );
  291. tCMD->cmd = GP_STOP_PREVIEW;
  292. m_qCommands.push( tCMD );
  293. };
  294. m_mCommandLock.unlock();
  295. return true;
  296. };
  297. bool gPhotoCamera::setCaptureTarget( GP_CAP_TARGET T ){
  298. if( m_pCamera == NULL ) return false;
  299. if( m_pChannelIsBusy ) return false;
  300. m_mCommandLock.lock();
  301. GP_CAM_COMMAND * tCMD;
  302. tCMD = (GP_CAM_COMMAND*) malloc(sizeof(GP_CAM_COMMAND));
  303. if( tCMD != NULL ) {
  304. memset( tCMD, 0 , sizeof(GP_CAM_COMMAND) );
  305. tCMD->cmd = GP_SET_CONFIG;
  306. sprintf( tCMD->prm,"capturetarget");
  307. if( T == GP_SD_CARD ) {
  308. sprintf( tCMD->var,"Memory card");//"card");// , T );
  309. } else {
  310. sprintf( tCMD->var,"Internal RAM");//"sdram");
  311. };
  312. m_qCommands.push( tCMD );
  313. };
  314. m_mCommandLock.unlock();
  315. return true;
  316. };
  317. bool gPhotoCamera::startBurst(){
  318. if( m_pCamera == NULL ) return false;
  319. if( m_pChannelIsBusy ) return false;
  320. m_mCommandLock.lock();
  321. m_bBurstCapturing = true;
  322. GP_CAM_COMMAND * tCMD;
  323. tCMD = (GP_CAM_COMMAND*) malloc(sizeof(GP_CAM_COMMAND));
  324. if( tCMD != NULL ) {
  325. memset( tCMD, 0 , sizeof(GP_CAM_COMMAND) );
  326. tCMD->cmd = GP_START_BURST;
  327. m_qCommands.push( tCMD );
  328. if( m_pDBC != NULL ) {
  329. m_pDBC->setCameraMode( GP_WM_BURST );
  330. };
  331. };
  332. m_mCommandLock.unlock();
  333. return true;
  334. };
  335. bool gPhotoCamera::stopBurst(){
  336. if( m_pCamera == NULL ) return false;
  337. if( m_pChannelIsBusy ) return false;
  338. m_mCommandLock.lock();
  339. m_bBurstCapturing = false;
  340. GP_CAM_COMMAND * tCMD;
  341. tCMD = (GP_CAM_COMMAND*) malloc(sizeof(GP_CAM_COMMAND));
  342. if( tCMD != NULL ) {
  343. memset( tCMD, 0 , sizeof(GP_CAM_COMMAND) );
  344. tCMD->cmd = GP_STOP_BURST;
  345. m_qCommands.push( tCMD );
  346. };
  347. m_mCommandLock.unlock();
  348. return true;
  349. };
  350. bool gPhotoCamera::validate(){
  351. if( m_pCamera == NULL ) return false;
  352. return true;
  353. };
  354. bool gPhotoCamera::setWM( uint8_t NMode ){
  355. m_bOldWMode = m_bWMode;
  356. m_bWMode = NMode;
  357. if( m_pDBC != NULL ) {
  358. m_pDBC->setCameraMode( m_bWMode );
  359. };
  360. return true;
  361. };
  362. bool gPhotoCamera::cancelWM( uint8_t NMode ){
  363. if( NMode != m_bWMode ) return false;
  364. if( m_bOldWMode == GP_WM_NA ) return false;
  365. if( m_bWMode != m_bOldWMode ) {
  366. m_bWMode == m_bOldWMode;
  367. m_bOldWMode = GP_WM_NA;
  368. };
  369. if( m_pDBC != NULL ) {
  370. m_pDBC->setCameraMode( m_bWMode );
  371. };
  372. return true;
  373. };
  374. void gPhotoCamera::m_fThreadProc( void ){
  375. gp_log_add_func( GP_LOG_ERROR, errordumper, NULL );
  376. //gp_log_add_func( GP_LOG_DEBUG, errordumper, NULL );
  377. CameraFilePath *path;
  378. CameraEventType evtype;
  379. void *data;
  380. int retval;
  381. char tBuff[1024];
  382. // Init Camera!
  383. m_pContext = gp_context_new();
  384. if( m_pContext != NULL ){
  385. LOG("New Context Created");
  386. int ret = GP_OK;
  387. //gp_context_set_error_func( m_pContext, error_func, NULL);
  388. //gp_context_set_message_func( m_pContext, message_func, NULL);
  389. gp_camera_new( &m_pCamera );
  390. LOG("New Camera Created");
  391. ret = gp_camera_init( m_pCamera, m_pContext );
  392. if( ret < GP_OK ){
  393. LOG_ERROR("No camera auto detected!");
  394. gp_camera_free( m_pCamera );
  395. m_pCamera = NULL;
  396. m_bThreadStatus = TH_ENDED;
  397. } else {
  398. LOG("Camera Init Complete");
  399. m_bThreadStatus = TH_WORKING;
  400. if( m_pDBC != NULL){
  401. int StorCNT;
  402. m_pDBC->clenupCameraFS();
  403. CameraStorageInformation *sifs = NULL;
  404. ret = gp_camera_get_storageinfo( m_pCamera, &sifs, &StorCNT, m_pContext );
  405. if( ret == GP_OK ) {
  406. if( StorCNT ) {
  407. for( int dx = 0; dx < StorCNT; dx++ ) {
  408. m_pDBC->updateCAM_STORAGE_INFO( sifs[dx].basedir, sifs[dx].type, sifs[dx].fstype, sifs[dx].access, sifs[dx].capacitykbytes, sifs[dx].freekbytes );
  409. updateFileTree( m_pDBC->getCAM_fileSyetemID( sifs[dx].basedir ), 0, sifs[dx].basedir );
  410. 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",
  411. sifs[dx].fields,
  412. sifs[dx].basedir,
  413. sifs[dx].label,
  414. sifs[dx].description,
  415. sifs[dx].type,
  416. sifs[dx].fstype,
  417. sifs[dx].access,
  418. sifs[dx].capacitykbytes,
  419. sifs[dx].freekbytes,
  420. sifs[dx].freeimages
  421. );
  422. LOG( tBuff );
  423. };
  424. };
  425. if( sifs != NULL ) free( sifs );
  426. };
  427. m_pDBC->setCameraMode( GP_WM_CTRL );
  428. };
  429. setCaptureTarget( GP_SD_CARD );
  430. data = NULL;
  431. };
  432. #ifdef DEBUG
  433. LOG("Camera Init Done!");
  434. #endif
  435. } else {
  436. LOG_ERROR( "Cannot create GP Context" );
  437. m_bThreadStatus = TH_ENDED;
  438. };
  439. uint8_t mPrevCounter = 0;
  440. while( m_bThreadStatus < TH_ENDED ){
  441. unsigned int timediff;
  442. // Fetch Kernel Commands
  443. m_mCommandLock.lock();
  444. if( !m_qCommands.empty() ) {
  445. GP_CAM_COMMAND * tCMD;
  446. tCMD = m_qCommands.front();
  447. switch( tCMD->cmd ) {
  448. /*case GP_DEL_PHOTOS:{
  449. if( m_pDBC != NULL ) {
  450. char cFile[256];
  451. char cFolder[256];
  452. char cFID;
  453. while( ( cFID = m_pDBC->feitchPhoto( cFolder, cFile ) ) && ( m_bThreadStatus < TH_ENDED ) ){
  454. retval = gp_camera_file_delete( m_pCamera, cFolder, cFile, m_pContext );
  455. if( retval == GP_OK ) {
  456. m_pDBC->removePhoto( cFID );
  457. } else {
  458. sprintf( tBuff, "Error Deleting File [%s] in [%s]", cFile, cFolder );
  459. LOG_ERROR( tBuff );
  460. break;
  461. };
  462. };
  463. };
  464. break;
  465. };*/
  466. case GP_STOP_PREVIEW:{
  467. LOG("Cancel Preview mode");
  468. cancelWM( GP_WM_PREV );
  469. break;
  470. };
  471. case GP_START_PREVIEW:{
  472. LOG("Start Preview mode");
  473. setWM( GP_WM_PREV );
  474. break;
  475. };
  476. case GP_PREVIEW_SHOT:{
  477. // Only in Control mode can be issued manual preview
  478. if( m_bWMode != GP_WM_CTRL ) break;
  479. CameraFile * cameraFile = NULL;
  480. gp_file_new( &cameraFile );
  481. if( cameraFile!= NULL ) {
  482. int retval = gp_camera_capture_preview( m_pCamera, cameraFile, m_pContext );
  483. if( retval != GP_OK){
  484. LOG("Error Ocured!");
  485. switch( retval ) {
  486. case GP_ERROR:
  487. LOG_ERROR( "Triggering capture had error!" );
  488. break;
  489. case GP_ERROR_CAMERA_BUSY:
  490. LOG_ERROR( "Camera is BUSY!" );
  491. break;
  492. default:
  493. LOG_ERROR( "Camera is Not OK!" );
  494. break;
  495. };
  496. } else {
  497. gp_file_save( cameraFile ,"data/capture_preview.jpg");
  498. };
  499. gp_file_free( cameraFile );
  500. };
  501. };
  502. break;
  503. case GP_SINGLE_SHOT:{
  504. LOG("Make Shot!");
  505. // Disable Single Shot while in Burst Mode
  506. if( m_bWMode == GP_WM_BURST ) break;
  507. CameraFilePath camera_file_path;
  508. //strcpy( camera_file_path.folder, "100CANON");
  509. //strcpy( camera_file_path.name, "IMG_1203.JPG");
  510. //int retval = gp_camera_capture( m_pCamera, GP_CAPTURE_IMAGE, &camera_file_path, m_pContext );
  511. int retval = gp_camera_trigger_capture( m_pCamera, m_pContext );
  512. if( retval != GP_OK){
  513. switch( retval ) {
  514. case GP_ERROR:
  515. LOG_ERROR( "Triggering capture had error!" );
  516. break;
  517. case GP_ERROR_CAMERA_BUSY:
  518. LOG_ERROR( "Camera is BUSY!" );
  519. break;
  520. default:
  521. LOG_ERROR( "Camera is Not OK!" );
  522. break;
  523. };
  524. } else m_pChannelIsBusy = true;
  525. };
  526. break;
  527. case GP_START_BURST:{
  528. setWM( GP_WM_BURST );
  529. set_config_action( "viewfinder", "1" );
  530. SLEEP( 500 );
  531. set_config_action( "eosremoterelease", "2" );
  532. //viewfinder=1;eosremoterelease=2;
  533. //m_pChannelIsBusy = true;
  534. };
  535. break;
  536. case GP_STOP_BURST:{
  537. setWM( GP_WM_CTRL );
  538. SLEEP( 300 );
  539. set_config_action( "eosremoterelease", "0" );
  540. SLEEP( 500 );
  541. set_config_action( "viewfinder", "0" );
  542. SLEEP( 300 );
  543. //viewfinder=0;eosremoterelease=0;
  544. //m_pChannelIsBusy = true;
  545. };
  546. break;
  547. case GP_SET_CONFIG:{
  548. CameraWidget *pWidget;
  549. int retVal;
  550. LOG( "Set CFG!" );
  551. m_pChannelIsBusy = true;
  552. //gp_widget_new( GP_WIDGET_MENU, tCMD->prm, &pWidget );
  553. retVal = gp_camera_get_single_config( m_pCamera, (const char *) tCMD->prm, &pWidget, m_pContext );
  554. if( retVal == GP_OK ){
  555. #ifdef DEBUG
  556. char ttt[128];
  557. #endif
  558. switch( pWidget->type ) {
  559. case GP_WIDGET_MENU:
  560. case GP_WIDGET_RADIO:
  561. set_config_action( tCMD->prm, tCMD->var );
  562. /*
  563. int cnt, i;
  564. cnt = gp_widget_count_choices( pWidget );
  565. if( cnt < GP_OK ){
  566. ret = cnt;
  567. break;
  568. };
  569. ret = GP_ERROR_BAD_PARAMETERS;
  570. i = atoi( tCMD->var );
  571. if( (i>= 0) && (i < cnt) ){
  572. const char * choice;
  573. ret = gp_widget_get_choice (pWidget, i, &choice);
  574. if (ret == GP_OK)
  575. ret = gp_widget_set_value (pWidget, choice);
  576. break;
  577. };
  578. //gp_context_error( p->context, _("Choice %s not found within list of choices."), value );
  579. if (ret == GP_OK) {
  580. if (child == rootconfig)
  581. ret = gp_camera_set_single_config (p->camera, name, child, p->context);
  582. else
  583. ret = gp_camera_set_config (p->camera, rootconfig, p->context);
  584. if (ret != GP_OK)
  585. gp_context_error (p->context, _("Failed to set new configuration value %s for configuration entry %s."), value, name);
  586. }
  587. gp_widget_free (rootconfig); */
  588. break;
  589. case GP_WIDGET_TEXT:
  590. #ifdef DEBUG
  591. sprintf( ttt, "Current %s(%s)[string] = %s", pWidget->name, pWidget->label, pWidget->value_string );
  592. LOG( ttt );
  593. #endif
  594. strcpy( pWidget->value_string, tCMD->var );
  595. break;
  596. case GP_WIDGET_DATE:
  597. case GP_WIDGET_TOGGLE:
  598. #ifdef DEBUG
  599. sprintf( ttt, "Current %s(%s)[int] = %d (%s)", pWidget->name, pWidget->label, pWidget->value_int, pWidget->value_string );
  600. LOG( ttt );
  601. #endif
  602. pWidget->value_int = atoi(tCMD->var);
  603. break;
  604. case GP_WIDGET_RANGE:
  605. #ifdef DEBUG
  606. sprintf( ttt, "Current %s(%s)[float] = %.3f (%s)", pWidget->name, pWidget->label, pWidget->value_float, pWidget->value_string );
  607. LOG( ttt );
  608. #endif
  609. pWidget->value_float = (float) atof(tCMD->var);
  610. break;
  611. case GP_WIDGET_BUTTON:
  612. #ifdef DEBUG
  613. sprintf( ttt, "Current %s(%s)[ptr] = %p (%s)", pWidget->name, pWidget->label, pWidget->callback, pWidget->value_string );
  614. LOG( ttt );
  615. #endif
  616. //CameraWidgetCallback
  617. break;
  618. };
  619. retVal = gp_camera_set_single_config( m_pCamera, pWidget->name, pWidget, m_pContext );
  620. gp_widget_free( pWidget );
  621. if( retVal != GP_OK ) {
  622. SLEEP( DEFAULT_THREAD_SLEEP_TIME );
  623. m_mCommandLock.unlock();
  624. LOG_ERROR("CONFIG SET Failure!");
  625. continue;
  626. };
  627. #ifdef DEBUG
  628. LOG("Success!");
  629. #endif
  630. } else {
  631. SLEEP( DEFAULT_THREAD_SLEEP_TIME );
  632. m_mCommandLock.unlock();
  633. continue;
  634. };
  635. };
  636. break;
  637. };
  638. m_qCommands.pop();
  639. free( tCMD );
  640. SLEEP( DEFAULT_THREAD_SLEEP_TIME );
  641. m_mCommandLock.unlock();
  642. continue;
  643. };
  644. m_mCommandLock.unlock();
  645. // Proceed Working Mode BackLogic
  646. if( ( m_bWMode == GP_WM_PREV ) && !m_pChannelIsBusy ) {
  647. mPrevCounter ++;
  648. if( mPrevCounter >= 10 ) {
  649. CameraFile * cameraFile = NULL;
  650. gp_file_new( &cameraFile );
  651. if( cameraFile!= NULL ) {
  652. int retval = gp_camera_capture_preview( m_pCamera, cameraFile, m_pContext );
  653. if( retval != GP_OK){
  654. LOG("Error Ocured!");
  655. switch( retval ) {
  656. case GP_ERROR:
  657. LOG_ERROR( "Triggering capture had error!" );
  658. break;
  659. case GP_ERROR_CAMERA_BUSY:
  660. LOG_ERROR( "Camera is BUSY!" );
  661. break;
  662. default:
  663. LOG_ERROR( "Camera is Not OK!" );
  664. break;
  665. };
  666. } else {
  667. gp_file_save( cameraFile ,"data/capture_preview.jpg");
  668. };
  669. gp_file_free( cameraFile );
  670. };
  671. mPrevCounter = 0;
  672. };
  673. SLEEP( 90 );
  674. } else {
  675. // Fetch Events
  676. retval = gp_camera_wait_for_event( m_pCamera, 10, &evtype, &data, m_pContext ); // 1/10 th second
  677. if( retval != GP_OK ){
  678. LOG_ERROR( "Error Return from waitevent" );
  679. m_bThreadStatus = TH_ENDED;
  680. continue;
  681. };
  682. path = (CameraFilePath*) data;
  683. m_pChannelIsBusy = false;
  684. switch( evtype ){
  685. case GP_EVENT_CAPTURE_COMPLETE:
  686. LOG( "event CAPTURE_COMPLETE" );
  687. break;
  688. case GP_EVENT_UNKNOWN:
  689. case GP_EVENT_TIMEOUT:
  690. break;
  691. case GP_EVENT_FOLDER_ADDED:
  692. LOG( "event FOLDER_ADDED" );
  693. free(data);
  694. break;
  695. case GP_EVENT_FILE_CHANGED:
  696. LOG( "event FILE_CHANGED" );
  697. free(data);
  698. break;
  699. case GP_EVENT_FILE_ADDED:
  700. #ifdef DEBUG
  701. LOG( "event FILE_ADDED" );
  702. #endif
  703. if( m_pBLOG != NULL ){
  704. PF_TIMESTAMP ts;
  705. getPFTS( &ts );
  706. m_pBLOG->registerPIC( &ts, path->name );
  707. } else LOG_ERROR("No Binary Log Connected!");
  708. if( m_pDBC != NULL ) {
  709. m_pDBC->registerCameraPicture( m_nFolderID, path->name );
  710. };
  711. #ifdef DEBUG
  712. //LOG( "event FILE_ADDED" );
  713. //sprintf( tBuff,"File [%s/%s] created.", path->folder, path->name );
  714. //LOG( tBuff );
  715. #endif
  716. /*
  717. retval = gp_camera_file_delete( m_pCamera, path->folder, path->name, m_pContext );
  718. if(retval != GP_OK) {
  719. LOG_ERROR( "Cannot delete picture on camera!" );
  720. sprintf( tBuff, "Retval: %d %s", retval, gp_result_as_string( retval ) );
  721. LOG_ERROR( tBuff );
  722. } else {
  723. LOG("File is Deleted!");
  724. };
  725. */
  726. free(data);
  727. break;
  728. default:
  729. LOG(" EVT Handler KOKO!");
  730. break;
  731. };
  732. };
  733. };
  734. if( m_pCamera != NULL ) {
  735. gp_camera_exit( m_pCamera, m_pContext );
  736. gp_camera_free( m_pCamera );
  737. };
  738. if( m_pContext != NULL ) gp_context_unref( m_pContext );
  739. };
  740. void gPhotoCamera::updateFileTree( int fsid, int parent, CSTR folder ){
  741. int ret;
  742. int MyID;
  743. char tBuff[256];
  744. CameraList * Folders = NULL;
  745. CameraList * Files = NULL;
  746. if( m_pDBC == NULL ) return;
  747. sprintf( tBuff, "Check Folder: %s", folder );
  748. LOG( tBuff );
  749. MyID = m_pDBC->addCameraFolder( fsid, parent, folder );
  750. if( MyID > 0 ) {
  751. gp_list_new( &Folders );
  752. ret = gp_camera_folder_list_folders( m_pCamera, folder, Folders, m_pContext );
  753. if( ret == GP_OK ) {
  754. for( int i=0; i < gp_list_count( Folders ); i++ ) {
  755. char *name;//, *value;
  756. gp_list_get_name( Folders, i, (const char**)&name );
  757. //gp_list_get_value( Folders, i, (const char**)&value );
  758. sprintf( tBuff, "%s/%s",folder, name );
  759. updateFileTree( fsid, MyID, tBuff );
  760. };
  761. };
  762. gp_list_free( Folders );
  763. gp_list_new( &Files );
  764. ret = gp_camera_folder_list_files( m_pCamera, folder, Files, m_pContext );
  765. if( ret == GP_OK ) {
  766. int i = 0;
  767. for( i=0; i < gp_list_count( Files ); i++ ) {
  768. char *name;//, *value;
  769. gp_list_get_name( Files, i, (const char**)&name);
  770. //gp_list_get_value( Files, i, (const char**)&value);
  771. m_pDBC->addCameraFile( MyID, name );
  772. //sprintf( tBuff, "Files: %s", name );
  773. //LOG( tBuff );
  774. };
  775. if( i > 0 ) {
  776. m_nFolderID = MyID;
  777. sprintf( m_pCapFolder, folder );
  778. sprintf( tBuff, "Save Folder: [%s] id:[%d]", m_pCapFolder, m_nFolderID );
  779. LOG( tBuff );
  780. };
  781. };
  782. gp_list_free( Files );
  783. };
  784. };
  785. bool gPhotoCamera::switchToStorage(){
  786. LOG("To Storage Mode" );
  787. if( m_bWMode != GP_WM_STOR ){
  788. char tBuff[1024];
  789. int RetVal;
  790. m_bWMode = GP_WM_NA;
  791. m_bThreadStatus = TH_ENDED;
  792. m_mCommandLock.unlock();
  793. waitThreadEnd( true );
  794. if( m_pDBC != NULL ) {
  795. m_pDBC->setCameraMode( GP_WM_STOR );
  796. };
  797. sprintf( tBuff, "gphotofs -o uid=%d,gid=%d,allow_other,nonempty,auto_unmount,large_read,default_permissions %s", m_nAccessUID, m_nAccessGID, m_pCamMountPoint );
  798. LOG( tBuff );
  799. RetVal = system( tBuff );
  800. sprintf( tBuff, "Returned Code: %d", RetVal );
  801. LOG( tBuff );
  802. m_bWMode = GP_WM_STOR;
  803. LOG("OK");
  804. return true;
  805. };
  806. LOG("Wrong Mode!");
  807. return false;
  808. };
  809. bool gPhotoCamera::switchToCamera(){
  810. LOG("To Camera Mode" );
  811. if( m_bWMode != GP_WM_CTRL ){
  812. if( m_bWMode == GP_WM_STOR ) {
  813. char tBuff[256];
  814. int RetVal;
  815. m_bWMode = GP_WM_NA;
  816. sprintf( tBuff, "fusermount -u %s ", m_pCamMountPoint );
  817. LOG( tBuff );
  818. RetVal = system( tBuff );
  819. sprintf( tBuff, "Returned Code: %d", RetVal );
  820. LOG( tBuff );
  821. m_bThreadStatus = TH_PRE_INIT;
  822. LOG("From Mount OK");
  823. return create();
  824. } else {
  825. LOG("From Mode OK");
  826. return setWM( GP_WM_CTRL );
  827. };
  828. };
  829. LOG("Wrong Mode!");
  830. return false;
  831. };
  832. bool gPhotoCamera::deleteAllPictures(){
  833. if( m_bWMode == GP_WM_STOR ) {
  834. if( m_pDBC != NULL ) {
  835. char cFile[256];
  836. char cFolder[256];
  837. char cName[1024];
  838. char tBuff[1024];
  839. uint32_t cFID;
  840. while( ( cFID = m_pDBC->feitchPhoto( cFolder, cFile ) ) > 0 ){
  841. sprintf( cName, "%s%s/%s", m_pCamMountPoint, cFolder, cFile );
  842. LOG( cName );
  843. if( 0 == unlink( cName ) ) {
  844. m_pDBC->removePhoto( cFID );
  845. } else {
  846. sprintf( tBuff, "Error Deleting File [%s]", cName );
  847. LOG_ERROR( tBuff );
  848. break;
  849. };
  850. };
  851. };
  852. };
  853. return true;
  854. };
  855. bool gPhotoCamera::fetchFile( uint32_t pictureID ){
  856. unsigned long size;
  857. int fd;
  858. struct stat stbuf;
  859. CameraFile * file;
  860. /*
  861. retval = gp_file_new( &file );
  862. //fprintf(stderr,"camera getfile of %s / %s\n", queue[0].path.folder, queue[0].path.name );
  863. retval = gp_camera_file_get( m_pCamera, queue[0].path.folder, queue[0].path.name, GP_FILE_TYPE_NORMAL, file, m_pContext );
  864. if (retval != GP_OK) {
  865. gp_file_free (file);
  866. LOG_ERROR( "gp_camera_file_get failed" );
  867. m_bThreadStatus = TH_ENDED;
  868. };
  869. // buffer is returned as pointer, not as a copy
  870. retval = gp_file_get_data_and_size( file, &buffer, &size );
  871. if( retval != GP_OK ){
  872. gp_file_free (file);
  873. LOG_ERROR( "gp_file_get_data_and_size failed" );
  874. m_bThreadStatus = TH_ENDED;
  875. };
  876. if( -1 == stat(queue[0].path.name, &stbuf) ){
  877. fd = creat(queue[0].path.name, 0644);
  878. } else{
  879. fd = open(queue[0].path.name, O_RDWR | O_BINARY, 0644);
  880. };
  881. if( fd == -1 ){
  882. LOG_ERROR( queue[0].path.name );
  883. m_bThreadStatus = TH_ENDED;
  884. };
  885. if( -1 == lseek(fd, 0, SEEK_SET) ) LOG_ERROR("lseek");
  886. if( -1 == write (fd, buffer, size) ) LOG_ERROR("write");
  887. close (fd);
  888. gp_file_free (file); // Note: this invalidates buffer.
  889. LOG( "ending download, deleting file." );
  890. retval = gp_camera_file_delete( m_pCamera, queue[0].path.folder, queue[0].path.name, m_pContext );
  891. memmove(&queue[0],&queue[1],sizeof(queue[0])*(nrofqueue-1));
  892. */
  893. return true;
  894. };
  895. int gPhotoCamera::delete_all_action( char *folder ){
  896. return (gp_camera_folder_delete_all( m_pCamera, folder, m_pContext ));
  897. };
  898. #define CR(result) {int r = (result); if (r < 0) return (r);}
  899. #define CHECK(result) {int r=(result);if(r<0) return(r);}
  900. #define CL(result,list) {int r=(result);if(r<0) {gp_list_free(list);return(r);}}
  901. #define CHECK_CONT(result) \
  902. { \
  903. int r = (result); \
  904. \
  905. if (r < 0) { \
  906. if (r == GP_ERROR_CANCEL) { \
  907. glob_cancel = 0; \
  908. } else { \
  909. printf (_("*** Error (%i: '%s') ***"), \
  910. r, gp_result_as_string (r)); \
  911. putchar ('\n'); \
  912. } \
  913. } \
  914. }
  915. #define textdomain(String) (String)
  916. #define gettext(String) (String)
  917. #define ngettext(String1,String2,Count) (Count==1?String1:String2)
  918. #define dgettext(Domain,Message) (Message)
  919. #define dcgettext(Domain,Message,Type) (Message)
  920. #define bindtextdomain(Domain,Directory) (Domain)
  921. #define _(String) (String)
  922. #define N_(String) (String)
  923. int gPhotoCamera::action_camera_upload_file( char *folder, char *path){
  924. CameraFile *file;
  925. int res;
  926. char *fn;
  927. LOG( "Uploading file..." );
  928. CR( gp_file_new_from_fd( &file, -1 ));
  929. res = gp_file_open (file, path);
  930. if (res < GP_OK) {
  931. gp_file_unref (file);
  932. return res;
  933. };
  934. //if (p->filename && strcmp (p->filename, ""))
  935. // fn = p->filename;
  936. //else
  937. fn = basename (path);
  938. res = gp_camera_folder_put_file( m_pCamera, folder, fn, GP_FILE_TYPE_NORMAL, file, m_pContext );
  939. gp_file_unref (file);
  940. return (res);
  941. };
  942. int gPhotoCamera::action_camera_upload_metadata( char *folder, char *path ){
  943. CameraFile *file;
  944. int res;
  945. char *fn = NULL;
  946. LOG( "Uploading metadata..." );
  947. CR( gp_file_new( &file ) );
  948. res = gp_file_open (file, path);
  949. if (res < 0) {
  950. gp_file_unref (file);
  951. return (res);
  952. };
  953. /* Check if the user specified a filename */
  954. //if (p->filename && strcmp (p->filename, "")) {
  955. // fn = p->filename;
  956. //} else
  957. if (path == strstr(path, "meta_")) {
  958. fn = path+5;
  959. };
  960. res = gp_camera_folder_put_file( m_pCamera, folder, fn, GP_FILE_TYPE_METADATA, file, m_pContext );
  961. gp_file_unref (file);
  962. return (res);
  963. };
  964. int gPhotoCamera::num_files_action(){
  965. CameraList *list;
  966. int count, filecount;
  967. CR( gp_list_new (&list));
  968. CL( gp_camera_folder_list_files( m_pCamera, m_pCapFolder, list, m_pContext ), list );
  969. CL( count = gp_list_count (list), list);
  970. if( m_nFlags & FLAGS_NEW ){
  971. int i;
  972. const char *name;
  973. filecount = 0;
  974. for (i = 0; i < count; i++) {
  975. CameraFileInfo info;
  976. CL( gp_list_get_name( list, i, &name ), list );
  977. CR( gp_camera_file_get_info( m_pCamera, m_pCapFolder, name, &info, m_pContext ));
  978. if( info.file.fields & GP_FILE_INFO_STATUS && info.file.status != GP_FILE_STATUS_DOWNLOADED ) filecount++;
  979. };
  980. } else filecount = count;
  981. gp_list_free (list);
  982. if( m_nFlags & FLAGS_QUIET ){
  983. char tBuff[256];
  984. sprintf( tBuff, "%i", filecount );
  985. LOG( tBuff );
  986. } else{
  987. char tBuff[256];
  988. sprintf( tBuff, "Number of files in folder '%s': %i", m_pCapFolder, filecount );
  989. LOG( tBuff );
  990. };
  991. return (GP_OK);
  992. };
  993. int gPhotoCamera::list_folders_action(){
  994. CameraList *list;
  995. int count;
  996. const char *name;
  997. int i;
  998. CR( gp_list_new (&list) );
  999. CL( gp_camera_folder_list_folders( m_pCamera, m_pCapFolder, list, m_pContext ), list );
  1000. CL( count = gp_list_count(list), list );
  1001. if( !(m_nFlags & FLAGS_QUIET) ){
  1002. char tBuff[256];
  1003. sprintf( tBuff, ngettext(
  1004. "There is %d folder in folder '%s'.\n",
  1005. "There are %d folders in folder '%s'.\n",
  1006. count
  1007. ), count, m_pCapFolder );
  1008. LOG( tBuff );
  1009. };
  1010. for( i = 0; i < count; i++ ){
  1011. char tBuff[256];
  1012. CL( gp_list_get_name( list, i, &name ), list );
  1013. if( m_nFlags & FLAGS_QUIET) {
  1014. if( !strcmp( m_pCapFolder,"/") )
  1015. sprintf( tBuff, "/%s", name);
  1016. else
  1017. sprintf( tBuff, "%s/%s", m_pCapFolder, name );
  1018. } else
  1019. sprintf( tBuff, " - %s", name);
  1020. LOG(tBuff);
  1021. };
  1022. gp_list_free (list);
  1023. return (GP_OK);
  1024. };
  1025. int gPhotoCamera::list_files_action(){
  1026. CameraList *list;
  1027. int count, filecount;
  1028. char *name;
  1029. int i;
  1030. CR( gp_list_new(&list) );
  1031. CL( gp_camera_folder_list_files( m_pCamera, m_pCapFolder, list, m_pContext ), list );
  1032. CL( count = gp_list_count(list), list );
  1033. if( m_nFlags & FLAGS_NEW ){
  1034. filecount = 0;
  1035. for (i = 0; i < count; i++) {
  1036. CameraFileInfo info;
  1037. CL( gp_list_get_name( list, i, (const char **)&name ), list );
  1038. CR( gp_camera_file_get_info( m_pCamera, m_pCapFolder, name, &info, m_pContext ) );
  1039. if (info.file.fields & GP_FILE_INFO_STATUS && info.file.status != GP_FILE_STATUS_DOWNLOADED ) filecount++;
  1040. };
  1041. } else filecount = count;
  1042. if (!(m_nFlags & FLAGS_QUIET)) { /* do not print that in quiet mode */
  1043. char tBuff[256];
  1044. if (filecount == 0) { /* 0 is weird still, despite ngettext() */
  1045. sprintf( tBuff, "There is no file in folder '%s'.", m_pCapFolder );
  1046. } else {
  1047. sprintf( tBuff, ngettext(
  1048. "There is %d file in folder '%s'.\n",
  1049. "There are %d files in folder '%s'.\n",
  1050. filecount
  1051. ), filecount, m_pCapFolder );
  1052. };
  1053. LOG(tBuff);
  1054. };
  1055. for( i = 0; i < count; i++ ){
  1056. CL( gp_list_get_name( list, i, (const char **)&name ), list );
  1057. CL( print_file_action( m_pCapFolder, name ), list );
  1058. };
  1059. gp_list_free (list);
  1060. return (GP_OK);
  1061. };
  1062. int gPhotoCamera::print_info_action( char *folder, char *filename ){
  1063. CameraFileInfo info;
  1064. char tBuf[256];
  1065. CR( gp_camera_file_get_info( m_pCamera, folder, filename, &info, m_pContext ) );
  1066. sprintf( tBuf, "Information on file '%s' (folder '%s'):", filename, folder );
  1067. LOG( tBuf );
  1068. LOG( "File:" );
  1069. if( info.file.fields == GP_FILE_INFO_NONE ) LOG( " None available." );
  1070. else {
  1071. memset( tBuf, 0, 256 );
  1072. if( info.file.fields & GP_FILE_INFO_TYPE)
  1073. sprintf( tBuf, " Mime type: '%s'", info.file.type );
  1074. if( info.file.fields & GP_FILE_INFO_SIZE)
  1075. sprintf( tBuf, " Size: %lu byte(s)", (unsigned long int)info.file.size );
  1076. if( info.file.fields & GP_FILE_INFO_WIDTH)
  1077. sprintf( tBuf, " Width: %i pixel(s)", info.file.width );
  1078. if( info.file.fields & GP_FILE_INFO_HEIGHT)
  1079. sprintf( tBuf, " Height: %i pixel(s)", info.file.height );
  1080. if( info.file.fields & GP_FILE_INFO_STATUS)
  1081. sprintf( tBuf, " Downloaded: %s", (info.file.status == GP_FILE_STATUS_DOWNLOADED) ? _("yes") : _("no") );
  1082. if( info.file.fields & GP_FILE_INFO_PERMISSIONS) {
  1083. LOG( " Permissions: " );
  1084. if ((info.file.permissions & GP_FILE_PERM_READ) && (info.file.permissions & GP_FILE_PERM_DELETE))
  1085. sprintf( tBuf, "read/delete" );
  1086. else if (info.file.permissions & GP_FILE_PERM_READ)
  1087. sprintf( tBuf, "read" );
  1088. else if (info.file.permissions & GP_FILE_PERM_DELETE)
  1089. sprintf( tBuf, "delete" );
  1090. else
  1091. sprintf( tBuf, "none" );
  1092. };
  1093. LOG( tBuf );
  1094. if( info.file.fields & GP_FILE_INFO_MTIME ){
  1095. sprintf( tBuf, " Time: %s", asctime (localtime (&info.file.mtime)) );
  1096. LOG( tBuf );
  1097. };
  1098. };
  1099. LOG( "Thumbnail:" );
  1100. if (info.preview.fields == GP_FILE_INFO_NONE)
  1101. LOG( " None available." );
  1102. else {
  1103. memset( tBuf, 0, 256 );
  1104. if (info.preview.fields & GP_FILE_INFO_TYPE)
  1105. sprintf( tBuf, " Mime type: '%s'", info.preview.type );
  1106. if (info.preview.fields & GP_FILE_INFO_SIZE)
  1107. sprintf( tBuf, " Size: %lu byte(s)", (unsigned long int)info.preview.size );
  1108. if (info.preview.fields & GP_FILE_INFO_WIDTH)
  1109. sprintf( tBuf, " Width: %i pixel(s)", info.preview.width );
  1110. if (info.preview.fields & GP_FILE_INFO_HEIGHT)
  1111. sprintf( tBuf, " Height: %i pixel(s)", info.preview.height );
  1112. if (info.preview.fields & GP_FILE_INFO_STATUS)
  1113. sprintf( tBuf, " Downloaded: %s", (info.preview.status == GP_FILE_STATUS_DOWNLOADED) ? _("yes") : _("no") );
  1114. LOG( tBuf );
  1115. };
  1116. LOG( "Audio data:" );
  1117. if( info.audio.fields == GP_FILE_INFO_NONE )
  1118. LOG( " None available." );
  1119. else {
  1120. memset( tBuf, 0, 256 );
  1121. if (info.audio.fields & GP_FILE_INFO_TYPE)
  1122. sprintf( tBuf, " Mime type: '%s'", info.audio.type );
  1123. if (info.audio.fields & GP_FILE_INFO_SIZE)
  1124. sprintf( tBuf, " Size: %lu byte(s)", (unsigned long int)info.audio.size );
  1125. if (info.audio.fields & GP_FILE_INFO_STATUS)
  1126. sprintf( tBuf, " Downloaded: %s", (info.audio.status == GP_FILE_STATUS_DOWNLOADED) ? _("yes") : _("no") );
  1127. LOG( tBuf );
  1128. };
  1129. return (GP_OK);
  1130. };
  1131. int gPhotoCamera::print_file_action( char *folder, char *filename ){
  1132. char tBuff[256];
  1133. int x = 0;
  1134. if( m_nFlags & FLAGS_NEW ){
  1135. CameraFileInfo info;
  1136. CR( gp_camera_file_get_info( m_pCamera, folder, filename, &info, m_pContext ) );
  1137. if( info.file.fields & GP_FILE_INFO_STATUS && info.file.status == GP_FILE_STATUS_DOWNLOADED ){
  1138. x++;
  1139. return (GP_OK);
  1140. };
  1141. };
  1142. if( m_nFlags & FLAGS_QUIET ){
  1143. sprintf( tBuff, "%s/%s", folder, filename );
  1144. LOG( tBuff );
  1145. } else {
  1146. CameraFileInfo info;
  1147. if( gp_camera_file_get_info( m_pCamera, folder, filename, &info, NULL) == GP_OK ){
  1148. sprintf( tBuff, "#%-5i %-27s", x+1, filename );
  1149. LOG( tBuff );
  1150. memset( tBuff, 0 , 256 );
  1151. 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" : "-");
  1152. if( info.file.fields & GP_FILE_INFO_SIZE) sprintf( tBuff, " %5ld KB", (unsigned long int)((info.file.size+1023) / 1024));
  1153. 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 );
  1154. if (info.file.fields & GP_FILE_INFO_TYPE) sprintf( tBuff, " %s", info.file.type );
  1155. } else
  1156. printf("#%-5i %s\n", x+1, filename);
  1157. LOG(tBuff);
  1158. };
  1159. x++;
  1160. return (GP_OK);
  1161. };
  1162. /*
  1163. int gPhotoCamera::save_file_action( char *folder, char *filename ){
  1164. return (save_file_to_file( m_pCamera, m_pContext, m_nFlags, folder, filename, GP_FILE_TYPE_NORMAL));
  1165. };
  1166. int gPhotoCamera::save_exif_action( char *folder, char *filename ){
  1167. return (save_file_to_file( m_pCamera, m_pContext, m_nFlags, folder, filename, GP_FILE_TYPE_EXIF));
  1168. };
  1169. int gPhotoCamera::save_meta_action( char *folder, char *filename ){
  1170. return (save_file_to_file( m_pCamera, m_pContext, m_nFlags, folder, filename, GP_FILE_TYPE_METADATA));
  1171. };
  1172. int gPhotoCamera::save_thumbnail_action( char *folder, char *filename ){
  1173. return (save_file_to_file( m_pCamera, m_pContext, m_nFlags, folder, filename, GP_FILE_TYPE_PREVIEW));
  1174. };
  1175. int gPhotoCamera::save_raw_action( char *folder, char *filename ){
  1176. return (save_file_to_file( m_pCamera, m_pContext, m_nFlags, folder, filename, GP_FILE_TYPE_RAW));
  1177. };
  1178. int gPhotoCamera::save_audio_action( char *folder, char *filename ){
  1179. return (save_file_to_file( m_pCamera, m_pContext, m_nFlags, folder, filename, GP_FILE_TYPE_AUDIO));
  1180. };
  1181. int gPhotoCamera::save_all_audio_action( char *folder, char *filename ){
  1182. // not every file has an associated audio file
  1183. if (camera_file_exists( m_pCamera, m_pContext, folder, filename, GP_FILE_TYPE_AUDIO))
  1184. return (save_file_to_file( m_pCamera, m_pContext, m_nFlags, folder, filename, GP_FILE_TYPE_AUDIO));
  1185. return GP_OK;
  1186. };
  1187. */
  1188. int gPhotoCamera::delete_file_action( char *folder, char *filename ){
  1189. if( m_nFlags & FLAGS_NEW ){
  1190. CameraFileInfo info;
  1191. CR( gp_camera_file_get_info( m_pCamera, folder, filename, &info, m_pContext ) );
  1192. if (info.file.fields & GP_FILE_INFO_STATUS && info.file.status == GP_FILE_STATUS_DOWNLOADED) return (GP_OK);
  1193. };
  1194. return (gp_camera_file_delete( m_pCamera, folder, filename, m_pContext ) );
  1195. };
  1196. int gPhotoCamera::list_cameras_action(){
  1197. int r = GP_OK, n, i;
  1198. char tBuff[256];
  1199. CameraAbilities a;
  1200. r = gp_abilities_list_count( gp_params_abilities_list() );
  1201. if( r < 0 ) return (r);
  1202. if( m_nFlags & FLAGS_QUIET ){
  1203. sprintf( tBuff, "%i", r);
  1204. LOG(tBuff);
  1205. } else {
  1206. sprintf( tBuff, "Number of supported cameras: %i", r);
  1207. LOG(tBuff);
  1208. LOG( "Supported cameras:" );
  1209. };
  1210. n = r;
  1211. for( i = 0; i < n; i++ ){
  1212. r = gp_abilities_list_get_abilities( gp_params_abilities_list(), i, &a );
  1213. if (r < 0) break;
  1214. if ( m_nFlags & FLAGS_QUIET)
  1215. sprintf( tBuff,"%s", a.model);
  1216. else
  1217. switch (a.status) {
  1218. case GP_DRIVER_STATUS_TESTING:
  1219. sprintf( tBuff, "\t\"%s\" (TESTING)", a.model );
  1220. break;
  1221. case GP_DRIVER_STATUS_EXPERIMENTAL:
  1222. sprintf( tBuff, "\t\"%s\" (EXPERIMENTAL)", a.model );
  1223. break;
  1224. case GP_DRIVER_STATUS_PRODUCTION:
  1225. default:
  1226. sprintf( tBuff, "\t\"%s\"", a.model);
  1227. break;
  1228. };
  1229. LOG(tBuff);
  1230. };
  1231. return (r);
  1232. }
  1233. void gPhotoCamera::_get_portinfo_list(){
  1234. int count, result;
  1235. GPPortInfoList *list = NULL;
  1236. if( m_pPortInfoList ) return;
  1237. if( gp_port_info_list_new( &list ) < GP_OK ) return;
  1238. result = gp_port_info_list_load( list );
  1239. if (result < 0) {
  1240. gp_port_info_list_free( list );
  1241. return;
  1242. };
  1243. count = gp_port_info_list_count( list );
  1244. if( count < 0 ){
  1245. gp_port_info_list_free( list );
  1246. return;
  1247. };
  1248. m_pPortInfoList = list;
  1249. return;
  1250. }
  1251. int gPhotoCamera::list_ports_action(){
  1252. GPPortInfo info;
  1253. int x, count, result = GP_OK;
  1254. char tBuff[256];
  1255. _get_portinfo_list();
  1256. count = gp_port_info_list_count( m_pPortInfoList );
  1257. if( m_nFlags & FLAGS_QUIET) sprintf( tBuff, "%i", count );
  1258. else sprintf( tBuff, "Devices found: %i", count );
  1259. LOG( tBuff );
  1260. /* Now list the ports */
  1261. for( x = 0; x < count; x++ ){
  1262. char *xname, *xpath;
  1263. result = gp_port_info_list_get_info( m_pPortInfoList, x, &info );
  1264. if( result < 0 ) break;
  1265. gp_port_info_get_name( info, &xname);
  1266. gp_port_info_get_path( info, &xpath);
  1267. sprintf( tBuff, "%-32s %-32s", xpath, xname );
  1268. LOG( tBuff );
  1269. };
  1270. return (result);
  1271. };
  1272. int gPhotoCamera::auto_detect_action(){
  1273. int x, count;
  1274. CameraList *list;
  1275. const char *name = NULL, *value = NULL;
  1276. char tBuff[256];
  1277. _get_portinfo_list();
  1278. count = gp_port_info_list_count( m_pPortInfoList );
  1279. CR( gp_list_new( &list ) );
  1280. gp_abilities_list_detect( gp_params_abilities_list(), m_pPortInfoList, list, m_pContext );
  1281. CL( count = gp_list_count( list ), list );
  1282. sprintf( tBuff, "%-30s %-16s", "Model", "Port" );
  1283. LOG(tBuff);
  1284. LOG("----------------------------------------------------------");
  1285. for (x = 0; x < count; x++) {
  1286. CL( gp_list_get_name( list, x, &name), list );
  1287. CL( gp_list_get_value( list, x, &value), list );
  1288. sprintf( tBuff, "%-30s %-16s", name, value );
  1289. LOG(tBuff);
  1290. };
  1291. gp_list_free (list);
  1292. return GP_OK;
  1293. };
  1294. int gPhotoCamera::action_camera_show_abilities(){
  1295. CameraAbilities a;
  1296. int i;
  1297. int has_capture = 0;
  1298. char tBuff[256];
  1299. CR( gp_camera_get_abilities( m_pCamera, &a ));
  1300. sprintf( tBuff, "Abilities for camera : %s", a.model );
  1301. LOG(tBuff);
  1302. sprintf( tBuff, "Serial port support : %s", (a.port & GP_PORT_SERIAL) ? _("yes"):_("no") );
  1303. LOG(tBuff);
  1304. sprintf( tBuff, "USB support : %s", (a.port & GP_PORT_USB) ? _("yes"):_("no") );
  1305. LOG(tBuff);
  1306. if( a.speed[0] != 0 ){
  1307. LOG("Transfer speeds supported :" );
  1308. for (i = 0; a.speed[i]; i++){
  1309. sprintf( tBuff, " : %i", a.speed[i] );
  1310. LOG(tBuff);
  1311. };
  1312. };
  1313. LOG( "Capture choices :" );
  1314. if( a.operations & GP_OPERATION_CAPTURE_IMAGE ){
  1315. LOG( " : Image" );
  1316. has_capture = 1;
  1317. };
  1318. if( a.operations & GP_OPERATION_CAPTURE_VIDEO ){
  1319. LOG( " : Video" );
  1320. has_capture = 1;
  1321. };
  1322. if( a.operations & GP_OPERATION_CAPTURE_AUDIO ){
  1323. LOG( " : Audio" );
  1324. has_capture = 1;
  1325. };
  1326. if( a.operations & GP_OPERATION_CAPTURE_PREVIEW ){
  1327. LOG( " : Preview" );
  1328. has_capture = 1;
  1329. };
  1330. if (a.operations & GP_OPERATION_TRIGGER_CAPTURE) {
  1331. LOG( " : Trigger Capture" );
  1332. has_capture = 1;
  1333. };
  1334. if (has_capture == 0) {
  1335. LOG( " : Capture not supported by the driver" );
  1336. };
  1337. sprintf( tBuff, "Configuration support : %s", (a.operations & GP_OPERATION_CONFIG) ? _("yes"):_("no"));
  1338. LOG(tBuff);
  1339. sprintf( tBuff, "Delete selected files on camera : %s", (a.file_operations & GP_FILE_OPERATION_DELETE) ? _("yes"):_("no"));
  1340. LOG(tBuff);
  1341. sprintf( tBuff, "Delete all files on camera : %s", (a.folder_operations & GP_FOLDER_OPERATION_DELETE_ALL) ? _("yes"):_("no"));
  1342. LOG(tBuff);
  1343. sprintf( tBuff, "File preview (thumbnail) support : %s", (a.file_operations & GP_FILE_OPERATION_PREVIEW) ? _("yes"):_("no"));
  1344. LOG(tBuff);
  1345. sprintf( tBuff, "File upload support : %s", (a.folder_operations & GP_FOLDER_OPERATION_PUT_FILE) ? _("yes"):_("no"));
  1346. LOG(tBuff);
  1347. return (GP_OK);
  1348. }
  1349. int gPhotoCamera::action_camera_set_port( const char *port ){
  1350. int p, r;
  1351. GPPortInfo info;
  1352. char *path;
  1353. char tBuff[256];
  1354. char verified_port[1024];
  1355. verified_port[sizeof (verified_port) - 1] = '\0';
  1356. if( !strchr( port, ':' ) ){
  1357. 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);
  1358. if( !strcmp( port, "usb" ) ){
  1359. strncpy( verified_port, "usb:", sizeof (verified_port) - 1 );
  1360. } else if( strncmp( port, "/dev/", 5) == 0 ){
  1361. strncpy( verified_port, "serial:", sizeof (verified_port) - 1);
  1362. strncat( verified_port, port, sizeof (verified_port) - strlen (verified_port) - 1 );
  1363. } else if( strncmp( port, "/proc/", 6) == 0 ){
  1364. strncpy( verified_port, "usb:", sizeof (verified_port) - 1 );
  1365. strncat( verified_port, port, sizeof (verified_port) - strlen (verified_port) - 1 );
  1366. };
  1367. sprintf( tBuff, "Guessed port name. Using port '%s' from now on.", verified_port );
  1368. LOG(tBuff);
  1369. } else
  1370. strncpy ( verified_port, port, sizeof (verified_port) - 1);
  1371. /* Create the list of ports and load it. */
  1372. _get_portinfo_list();
  1373. /* Search our port in the list. */
  1374. /* NOTE: This call can modify "il" for regexp matches! */
  1375. p = gp_port_info_list_lookup_path( m_pPortInfoList, verified_port );
  1376. switch( p ){
  1377. case GP_ERROR_UNKNOWN_PORT:
  1378. 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 );
  1379. LOG( tBuff );
  1380. break;
  1381. default:
  1382. break;
  1383. };
  1384. /* Get info about our port. */
  1385. r = gp_port_info_list_get_info( m_pPortInfoList, p, &info );
  1386. if( r < 0 )return (r);
  1387. /* Set the port of our camera. */
  1388. r = gp_camera_set_port_info( m_pCamera, info );
  1389. if (r < 0) return (r);
  1390. gp_port_info_get_path( info, &path );
  1391. //gp_setting_set( "gphoto2", "port", path );
  1392. return (GP_OK);
  1393. };
  1394. int gPhotoCamera::action_camera_about(){
  1395. CameraText text;
  1396. CR( gp_camera_get_about( m_pCamera, &text, m_pContext ) );
  1397. LOG( "About the camera driver:" );
  1398. LOG( text.text );
  1399. return (GP_OK);
  1400. };
  1401. int gPhotoCamera::action_camera_summary(){
  1402. CameraText text;
  1403. CR( gp_camera_get_summary( m_pCamera, &text, m_pContext ) );
  1404. LOG( "Camera summary:" );
  1405. LOG( text.text );
  1406. return (GP_OK);
  1407. };
  1408. int gPhotoCamera::action_camera_manual(){
  1409. CameraText text;
  1410. CR( gp_camera_get_manual( m_pCamera, &text, m_pContext ) );
  1411. LOG( "Camera manual:" );
  1412. LOG( text.text );
  1413. return (GP_OK);
  1414. }
  1415. int gPhotoCamera::action_camera_set_speed( unsigned int speed ){
  1416. GPPortInfo info;
  1417. GPPortType type;
  1418. /* Make sure we've got a serial port. */
  1419. CR( gp_camera_get_port_info( m_pCamera, &info ) );
  1420. gp_port_info_get_type( info, &type );
  1421. if( type != GP_PORT_SERIAL ){
  1422. if( (m_nFlags & FLAGS_QUIET) == 0 ) LOG("You can only specify speeds for serial ports.");
  1423. return (GP_ERROR_BAD_PARAMETERS);
  1424. };
  1425. /* Set the speed. */
  1426. return gp_camera_set_port_speed( m_pCamera, speed );
  1427. };
  1428. int gPhotoCamera::action_camera_set_model( const char *model ){
  1429. CameraAbilities a;
  1430. int m;
  1431. CR( m = gp_abilities_list_lookup_model( gp_params_abilities_list(), model ) );
  1432. CR( gp_abilities_list_get_abilities( gp_params_abilities_list(), m, &a ) );
  1433. CR( gp_camera_set_abilities( m_pCamera, a ) );
  1434. //gp_setting_set ("gphoto2", "model", a.model);
  1435. return (GP_OK);
  1436. };
  1437. int gPhotoCamera::set_folder_action( const char *folder ){
  1438. sprintf( m_pCapFolder, folder );
  1439. return GP_OK;
  1440. };
  1441. int gPhotoCamera::set_filename_action( const char *filename ){
  1442. sprintf( m_pCapFile,filename );
  1443. return GP_OK;
  1444. };
  1445. #define CHECK_NULL(x) { if (x == NULL) { return(-1); /* FIXME: what code? */ } }
  1446. enum moviemode { MOVIE_ENDLESS, MOVIE_FRAMES, MOVIE_SECONDS };
  1447. int gPhotoCamera::override_usbids_action( int usb_vendor, int usb_product, int usb_vendor_modified, int usb_product_modified ){
  1448. CameraAbilitiesList *al = NULL;
  1449. int r, n, i;
  1450. CameraAbilities a;
  1451. CR( gp_abilities_list_new(&al) );
  1452. /* The override_usbides_action() function is a notable
  1453. * exception to the rule that one is not supposed to use
  1454. * p->_abilities_list directly, because it has to and does so
  1455. * in a safe way. */
  1456. n = gp_abilities_list_count( gp_params_abilities_list() );
  1457. for( i = 0; i < n; i++ ){
  1458. r = gp_abilities_list_get_abilities( gp_params_abilities_list(), i, &a );
  1459. if( r < 0 ) continue;
  1460. if( (a.usb_vendor == usb_vendor) && (a.usb_product == usb_product) ){
  1461. char tBuff[256];
  1462. 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 );
  1463. LOG(tBuff);
  1464. a.usb_vendor = usb_vendor_modified;
  1465. a.usb_product = usb_product_modified;
  1466. };
  1467. gp_abilities_list_append( al, a );
  1468. };
  1469. gp_abilities_list_free( m_pAbilitiesList );
  1470. m_pAbilitiesList = al;
  1471. return (GP_OK);
  1472. };
  1473. /* time zero for debug log time stamps */
  1474. static struct timeval glob_tv_zero = { 0, 0 };
  1475. int gPhotoCamera::_find_widget_by_name( const char *name, CameraWidget **child, CameraWidget **rootconfig ){
  1476. int ret;
  1477. *rootconfig = NULL;
  1478. ret = gp_camera_get_single_config( m_pCamera, name, child, m_pContext );
  1479. if( ret == GP_OK ){
  1480. *rootconfig = *child;
  1481. return GP_OK;
  1482. };
  1483. ret = gp_camera_get_config( m_pCamera, rootconfig, m_pContext );
  1484. if( ret != GP_OK ) return ret;
  1485. ret = gp_widget_get_child_by_name( *rootconfig, name, child );
  1486. if( ret != GP_OK ) ret = gp_widget_get_child_by_label( *rootconfig, name, child );
  1487. if (ret != GP_OK) {
  1488. char *part, *s, *newname;
  1489. newname = strdup( name );
  1490. if( !newname ) return GP_ERROR_NO_MEMORY;
  1491. *child = *rootconfig;
  1492. part = newname;
  1493. while( part[0] == '/' ) part++;
  1494. while( 1 ){
  1495. CameraWidget *tmp;
  1496. s = strchr( part, '/' );
  1497. if( s ) *s='\0';
  1498. ret = gp_widget_get_child_by_name( *child, part, &tmp );
  1499. if( ret != GP_OK ) ret = gp_widget_get_child_by_label (*child, part, &tmp);
  1500. if( ret != GP_OK ) break;
  1501. *child = tmp;
  1502. if( !s ){
  1503. /* end of path */
  1504. free (newname);
  1505. return GP_OK;
  1506. };
  1507. part = s+1;
  1508. while( part[0] == '/' ) part++;
  1509. };
  1510. gp_context_error( m_pContext, _("%s not found in configuration tree."), newname );
  1511. free (newname);
  1512. gp_widget_free( *rootconfig );
  1513. return GP_ERROR;
  1514. };
  1515. return GP_OK;
  1516. };
  1517. /* From the strftime(3) man page:
  1518. * BUGS
  1519. * Some buggy versions of gcc complain about the use of %c: warning:
  1520. * %c yields only last 2 digits of year in some locales.
  1521. * Of course programmers are encouraged to use %c, it gives the
  1522. * preferred date and time representation. One meets all kinds of
  1523. * strange obfuscations to circumvent this gcc problem. A relatively
  1524. * clean one is to add an intermediate function
  1525. */
  1526. size_t my_strftime(char *s, size_t max, const char *fmt, const struct tm *tm){
  1527. return strftime(s, max, fmt, tm);
  1528. }
  1529. int gPhotoCamera::get_config_action( const char *name ){
  1530. CameraWidget *rootconfig,*child;
  1531. int ret;
  1532. ret = _find_widget_by_name ( name, &child, &rootconfig );
  1533. if( ret != GP_OK) return ret;
  1534. //ret = print_widget (p, name, child);
  1535. gp_widget_free (rootconfig);
  1536. return ret;
  1537. };
  1538. int gPhotoCamera::set_config_action( const char *name, const char *value ){
  1539. CameraWidget *rootconfig,*child;
  1540. int ret, ro;
  1541. CameraWidgetType type;
  1542. ret = _find_widget_by_name ( name, &child, &rootconfig);
  1543. if( ret != GP_OK ) return ret;
  1544. ret = gp_widget_get_readonly( child, &ro );
  1545. if( ret != GP_OK ){
  1546. gp_widget_free (rootconfig);
  1547. return ret;
  1548. };
  1549. if( ro == 1 ){
  1550. gp_context_error( m_pContext, _("Property %s is read only."), name );
  1551. gp_widget_free( rootconfig );
  1552. return GP_ERROR;
  1553. };
  1554. ret = gp_widget_get_type( child, &type );
  1555. if( ret != GP_OK ){
  1556. gp_widget_free( rootconfig );
  1557. return ret;
  1558. };
  1559. switch (type) {
  1560. case GP_WIDGET_TEXT: { /* char * */
  1561. ret = gp_widget_set_value( child, value );
  1562. if (ret != GP_OK)
  1563. gp_context_error( m_pContext, _("Failed to set the value of text widget %s to %s."), name, value );
  1564. break;
  1565. }
  1566. case GP_WIDGET_RANGE: { /* float */
  1567. float f,t,b,s;
  1568. ret = gp_widget_get_range( child, &b, &t, &s );
  1569. if (ret != GP_OK)
  1570. break;
  1571. if (!sscanf (value, "%f", &f)) {
  1572. gp_context_error( m_pContext, _("The passed value %s is not a floating point value."), value );
  1573. ret = GP_ERROR_BAD_PARAMETERS;
  1574. break;
  1575. }
  1576. if ((f < b) || (f > t)) {
  1577. gp_context_error( m_pContext, _("The passed value %f is not within the expected range %f - %f."), f, b, t );
  1578. ret = GP_ERROR_BAD_PARAMETERS;
  1579. break;
  1580. }
  1581. ret = gp_widget_set_value( child, &f );
  1582. if (ret != GP_OK)
  1583. gp_context_error( m_pContext, _("Failed to set the value of range widget %s to %f."), name, f );
  1584. break;
  1585. }
  1586. case GP_WIDGET_TOGGLE: { /* int */
  1587. int t;
  1588. t = 2;
  1589. if( !strcasecmp( value, "off") || !strcasecmp (value, "no") ||
  1590. !strcasecmp( value, "false") || !strcmp (value, "0") ||
  1591. !strcasecmp( value, _("off")) || !strcasecmp (value, _("no")) ||
  1592. !strcasecmp( value, _("false"))
  1593. )
  1594. t = 0;
  1595. if( !strcasecmp( value, "on") || !strcasecmp (value, "yes") ||
  1596. !strcasecmp( value, "true") || !strcmp (value, "1") ||
  1597. !strcasecmp( value, _("on")) || !strcasecmp (value, _("yes")) ||
  1598. !strcasecmp( value, _("true"))
  1599. )
  1600. t = 1;
  1601. /*fprintf (stderr," value %s, t %d\n", value, t);*/
  1602. if (t == 2) {
  1603. gp_context_error( m_pContext, _("The passed value %s is not a valid toggle value."), value );
  1604. ret = GP_ERROR_BAD_PARAMETERS;
  1605. break;
  1606. }
  1607. ret = gp_widget_set_value( child, &t );
  1608. if (ret != GP_OK)
  1609. gp_context_error( m_pContext, _("Failed to set values %s of toggle widget %s."), value, name );
  1610. break;
  1611. }
  1612. case GP_WIDGET_DATE: { /* int */
  1613. time_t t = -1;
  1614. struct tm xtm;
  1615. memset(&xtm,0,sizeof(xtm));
  1616. /* We need to set UNIX time in seconds since Epoch */
  1617. /* We get ... local time */
  1618. if (!strcasecmp (value, "now") || !strcasecmp (value, _("now")))
  1619. t = time(NULL);
  1620. #ifdef HAVE_STRPTIME
  1621. else if( strptime( value, "%c", &xtm) || strptime (value, "%Ec", &xtm) ){
  1622. xtm.tm_isdst = -1;
  1623. t = mktime (&xtm);
  1624. };
  1625. #endif
  1626. if( t == -1 ){
  1627. unsigned long lt;
  1628. if( !sscanf(value, "%ld", &lt) ){
  1629. gp_context_error( m_pContext, _("The passed value %s is neither a valid time nor an integer."), value );
  1630. ret = GP_ERROR_BAD_PARAMETERS;
  1631. break;
  1632. };
  1633. t = lt;
  1634. };
  1635. ret = gp_widget_set_value( child, &t );
  1636. if( ret != GP_OK ) gp_context_error( m_pContext, _("Failed to set new time of date/time widget %s to %s."), name, value );
  1637. break;
  1638. }
  1639. case GP_WIDGET_MENU:
  1640. case GP_WIDGET_RADIO: { /* char * */
  1641. int cnt, i;
  1642. char *endptr;
  1643. char tBuff[256];
  1644. cnt = gp_widget_count_choices( child );
  1645. if( cnt < GP_OK ){
  1646. ret = cnt;
  1647. break;
  1648. };
  1649. ret = GP_ERROR_BAD_PARAMETERS;
  1650. for( i=0; i<cnt; i++ ){
  1651. const char *choice;
  1652. ret = gp_widget_get_choice( child, i, &choice );
  1653. LOG( choice );
  1654. if( ret != GP_OK ) continue;
  1655. if( !strcmp( choice, value ) ){
  1656. ret = gp_widget_set_value( child, value );
  1657. LOG("Test Vals");
  1658. LOG( child->value_string );
  1659. sprintf( tBuff, "%d", child->value_int );
  1660. LOG( tBuff );
  1661. sprintf( tBuff, "%0.3f", child->value_float );
  1662. LOG( tBuff );
  1663. break;
  1664. };
  1665. };
  1666. if( i != cnt ) break;
  1667. /* make sure we parse just 1 integer, and there is nothing more.
  1668. * sscanf just does not provide this, we need strtol.
  1669. */
  1670. i = strtol( value, &endptr, 10 );
  1671. if( (value != endptr) && (*endptr == '\0') ){
  1672. if( (i>= 0) && (i < cnt) ){
  1673. const char *choice;
  1674. ret = gp_widget_get_choice( child, i, &choice );
  1675. if( ret == GP_OK ) ret = gp_widget_set_value (child, choice);
  1676. LOG("Test Vals");
  1677. LOG( child->value_string );
  1678. sprintf( tBuff, "%d", child->value_int );
  1679. LOG( tBuff );
  1680. sprintf( tBuff, "%0.3f", child->value_float );
  1681. LOG( tBuff );
  1682. break;
  1683. };
  1684. };
  1685. /* Lets just try setting the value directly, in case we have flexible setters,
  1686. * like PTP shutterspeed. */
  1687. ret = gp_widget_set_value( child, value );
  1688. LOG("Test Vals");
  1689. LOG( child->value_string );
  1690. sprintf( tBuff, "%d", child->value_int );
  1691. LOG( tBuff );
  1692. sprintf( tBuff, "%0.3f", child->value_float );
  1693. LOG( tBuff );
  1694. if (ret == GP_OK) break;
  1695. gp_context_error( m_pContext, _("Choice %s not found within list of choices."), value );
  1696. break;
  1697. };
  1698. /* ignore: */
  1699. case GP_WIDGET_WINDOW:
  1700. case GP_WIDGET_SECTION:
  1701. case GP_WIDGET_BUTTON:
  1702. gp_context_error( m_pContext, _("The %s widget is not configurable."), name );
  1703. ret = GP_ERROR_BAD_PARAMETERS;
  1704. break;
  1705. };
  1706. if( ret == GP_OK ){
  1707. if (child == rootconfig) ret = gp_camera_set_single_config( m_pCamera, name, child, m_pContext );
  1708. else ret = gp_camera_set_config( m_pCamera, rootconfig, m_pContext );
  1709. if( ret != GP_OK ) gp_context_error( m_pContext, _("Failed to set new configuration value %s for configuration entry %s."), value, name );
  1710. };
  1711. gp_widget_free( rootconfig );
  1712. return (ret);
  1713. };
  1714. int gPhotoCamera::set_config_index_action( const char *name, const char *value ){
  1715. CameraWidget *rootconfig,*child;
  1716. int ret;
  1717. const char *label;
  1718. CameraWidgetType type;
  1719. ret = _find_widget_by_name( name, &child, &rootconfig );
  1720. if( ret != GP_OK ) return ret;
  1721. ret = gp_widget_get_type( child, &type );
  1722. if( ret != GP_OK ){
  1723. gp_widget_free (rootconfig);
  1724. return ret;
  1725. };
  1726. ret = gp_widget_get_label( child, &label );
  1727. if( ret != GP_OK ){
  1728. gp_widget_free (rootconfig);
  1729. return ret;
  1730. };
  1731. switch( type ){
  1732. case GP_WIDGET_MENU:
  1733. case GP_WIDGET_RADIO: { /* char * */
  1734. int cnt, i;
  1735. cnt = gp_widget_count_choices( child );
  1736. if (cnt < GP_OK) {
  1737. ret = cnt;
  1738. break;
  1739. };
  1740. ret = GP_ERROR_BAD_PARAMETERS;
  1741. if( sscanf (value, "%d", &i) ){
  1742. if( (i>= 0) && (i < cnt) ){
  1743. const char *choice;
  1744. ret = gp_widget_get_choice( child, i, &choice );
  1745. if (ret == GP_OK) ret = gp_widget_set_value( child, choice );
  1746. break;
  1747. };
  1748. };
  1749. gp_context_error( m_pContext, _("Choice %s not found within list of choices."), value );
  1750. break;
  1751. }
  1752. /* ignore: */
  1753. case GP_WIDGET_TOGGLE:
  1754. case GP_WIDGET_TEXT:
  1755. case GP_WIDGET_RANGE:
  1756. case GP_WIDGET_DATE:
  1757. case GP_WIDGET_WINDOW:
  1758. case GP_WIDGET_SECTION:
  1759. case GP_WIDGET_BUTTON:
  1760. gp_context_error( m_pContext, _("The %s widget has no indexed list of choices. Use --set-config-value instead."), name );
  1761. ret = GP_ERROR_BAD_PARAMETERS;
  1762. break;
  1763. };
  1764. if (ret == GP_OK) {
  1765. if( child == rootconfig ) ret = gp_camera_set_single_config( m_pCamera, name, child, m_pContext );
  1766. else ret = gp_camera_set_config( m_pCamera, rootconfig, m_pContext );
  1767. if (ret != GP_OK) gp_context_error ( m_pContext, _("Failed to set new configuration value %s for configuration entry %s."), value, name);
  1768. };
  1769. gp_widget_free (rootconfig);
  1770. return (ret);
  1771. };
  1772. int gPhotoCamera::set_config_value_action( const char *name, const char *value ){
  1773. CameraWidget *rootconfig,*child;
  1774. int ret;
  1775. CameraWidgetType type;
  1776. ret = _find_widget_by_name( name, &child, &rootconfig );
  1777. if( ret != GP_OK ) return ret;
  1778. ret = gp_widget_get_type( child, &type );
  1779. if( ret != GP_OK ){
  1780. gp_widget_free (rootconfig);
  1781. return ret;
  1782. };
  1783. switch (type) {
  1784. case GP_WIDGET_TEXT: { /* char * */
  1785. ret = gp_widget_set_value( child, value );
  1786. if( ret != GP_OK ) gp_context_error( m_pContext, _("Failed to set the value of text widget %s to %s."), name, value );
  1787. break;
  1788. };
  1789. case GP_WIDGET_RANGE: { /* float */
  1790. float f,t,b,s;
  1791. ret = gp_widget_get_range( child, &b, &t, &s );
  1792. if( ret != GP_OK ) break;
  1793. if( !sscanf( value, "%f", &f ) ){
  1794. gp_context_error( m_pContext, _("The passed value %s is not a floating point value."), value );
  1795. ret = GP_ERROR_BAD_PARAMETERS;
  1796. break;
  1797. };
  1798. if( (f < b) || (f > t) ){
  1799. gp_context_error( m_pContext, _("The passed value %f is not within the expected range %f - %f."), f, b, t );
  1800. ret = GP_ERROR_BAD_PARAMETERS;
  1801. break;
  1802. };
  1803. ret = gp_widget_set_value( child, &f );
  1804. if( ret != GP_OK ) gp_context_error( m_pContext, _("Failed to set the value of range widget %s to %f."), name, f );
  1805. break;
  1806. };
  1807. case GP_WIDGET_TOGGLE: { /* int */
  1808. int t;
  1809. t = 2;
  1810. if( !strcasecmp( value, "off") || !strcasecmp( value, "no") ||
  1811. !strcasecmp( value, "false") || !strcmp( value, "0") ||
  1812. !strcasecmp( value, _("off")) || !strcasecmp( value, _("no")) ||
  1813. !strcasecmp( value, _("false"))
  1814. )
  1815. t = 0;
  1816. if( !strcasecmp( value, "on") || !strcasecmp( value, "yes") ||
  1817. !strcasecmp( value, "true") || !strcmp( value, "1") ||
  1818. !strcasecmp( value, _("on")) || !strcasecmp( value, _("yes")) ||
  1819. !strcasecmp( value, _("true"))
  1820. )
  1821. t = 1;
  1822. /*fprintf (stderr," value %s, t %d\n", value, t);*/
  1823. if( t == 2 ){
  1824. gp_context_error( m_pContext, _("The passed value %s is not a valid toggle value."), value );
  1825. ret = GP_ERROR_BAD_PARAMETERS;
  1826. break;
  1827. };
  1828. ret = gp_widget_set_value( child, &t );
  1829. if( ret != GP_OK ) gp_context_error( m_pContext, _("Failed to set values %s of toggle widget %s."), value, name );
  1830. break;
  1831. }
  1832. case GP_WIDGET_DATE: { /* int */
  1833. int t = -1;
  1834. struct tm xtm;
  1835. if( !strcasecmp( value, "now" ) || !strcasecmp( value, _("now"))) t = time(NULL);
  1836. #ifdef HAVE_STRPTIME
  1837. else if( strptime( value, "%c", &xtm ) || strptime( value, "%Ec", &xtm )) t = mktime (&xtm);
  1838. #endif
  1839. if( t == -1 ){
  1840. if( !sscanf( value, "%d", &t) ){
  1841. gp_context_error( m_pContext, _("The passed value %s is neither a valid time nor an integer."), value );
  1842. ret = GP_ERROR_BAD_PARAMETERS;
  1843. break;
  1844. };
  1845. };
  1846. ret = gp_widget_set_value( child, &t );
  1847. if( ret != GP_OK ) gp_context_error( m_pContext, _("Failed to set new time of date/time widget %s to %s."), name, value );
  1848. break;
  1849. }
  1850. case GP_WIDGET_MENU:
  1851. case GP_WIDGET_RADIO: { /* char * */
  1852. int cnt, i;
  1853. cnt = gp_widget_count_choices( child );
  1854. if( cnt < GP_OK ){
  1855. ret = cnt;
  1856. break;
  1857. };
  1858. ret = GP_ERROR_BAD_PARAMETERS;
  1859. for( i=0; i<cnt; i++ ){
  1860. const char *choice;
  1861. ret = gp_widget_get_choice( child, i, &choice );
  1862. if( ret != GP_OK ) continue;
  1863. if( !strcmp( choice, value) ){
  1864. ret = gp_widget_set_value( child, value );
  1865. break;
  1866. };
  1867. };
  1868. if( i != cnt ) break;
  1869. /* Lets just try setting the value directly, in case we have flexible setters,
  1870. * like PTP shutterspeed. */
  1871. ret = gp_widget_set_value( child, value );
  1872. if( ret == GP_OK ) break;
  1873. gp_context_error( m_pContext, _("Choice %s not found within list of choices."), value );
  1874. break;
  1875. };
  1876. /* ignore: */
  1877. case GP_WIDGET_WINDOW:
  1878. case GP_WIDGET_SECTION:
  1879. case GP_WIDGET_BUTTON:
  1880. gp_context_error( m_pContext, _("The %s widget is not configurable."), name );
  1881. ret = GP_ERROR_BAD_PARAMETERS;
  1882. break;
  1883. };
  1884. if( ret == GP_OK ){
  1885. if( child == rootconfig ) ret = gp_camera_set_single_config( m_pCamera, name, child, m_pContext );
  1886. else ret = gp_camera_set_config( m_pCamera, rootconfig, m_pContext );
  1887. if (ret != GP_OK) gp_context_error ( m_pContext, _("Failed to set new configuration value %s for configuration entry %s."), value, name );
  1888. };
  1889. gp_widget_free( rootconfig );
  1890. return (ret);
  1891. };
  1892. CameraAbilitiesList *gPhotoCamera::gp_params_abilities_list(){
  1893. /* If p == NULL, the behaviour of this function is as undefined as
  1894. * the expression p->abilities_list would have been. */
  1895. if( m_pAbilitiesList == NULL ){
  1896. gp_abilities_list_new( &m_pAbilitiesList );
  1897. gp_abilities_list_load( m_pAbilitiesList, m_pContext);
  1898. };
  1899. return m_pAbilitiesList;
  1900. };
  1901. /*! \brief Create local filename for CameraFile according to pattern in gp_params
  1902. *
  1903. * \param folder Name of the folder on the camera the CameraFile is stored in
  1904. * \param file CameraFile to find a local name for.
  1905. * \param path The pointer to the generated complete path name of the local filename.
  1906. * \return GPError code
  1907. *
  1908. * \warning This function reads the static variable gp_params.
  1909. */
  1910. /*
  1911. int gPhotoCamera::get_path_for_file (const char *folder, const char *name, CameraFileType type, CameraFile *file, char **path){
  1912. unsigned int i, l;
  1913. char *s = NULL, b[1024];
  1914. time_t t = 0;
  1915. struct tm *tm;
  1916. int hour12;
  1917. static int filenr = 1;
  1918. if( !path )return (GP_ERROR_BAD_PARAMETERS);
  1919. *path = NULL;
  1920. if( file ){
  1921. CR( gp_file_get_mtime( file, &t ) );
  1922. if (!t) // use the current time as fallback if the camera did not return it.
  1923. t = time(NULL);
  1924. };
  1925. tm = localtime (&t);
  1926. hour12 = tm->tm_hour % 12;
  1927. if (hour12 == 0) {
  1928. hour12 = 12;
  1929. };
  1930. // If the user didn't specify a filename, use the original name (and prefix).
  1931. if( !gp_params.filename || !strcmp( gp_params.filename, "" ) ){
  1932. if (file) {
  1933. return gp_file_get_name_by_type (file, name, type, path);
  1934. } else if ((type == GP_FILE_TYPE_NORMAL) && strchr(name,'.')) {
  1935. // return the original name
  1936. *path = strdup(name);
  1937. return (GP_OK);
  1938. } else
  1939. // Download required to get the path from CameraFile *file
  1940. return (GP_ERROR_BAD_PARAMETERS);
  1941. };
  1942. // The user did specify a filename. Use it.
  1943. b[sizeof (b) - 1] = '\0';
  1944. for (i = 0; i < strlen (gp_params.filename); i++) {
  1945. if (gp_params.filename[i] == '%') {
  1946. char padding = '0'; // default padding character
  1947. int precision = 0; // default: no padding
  1948. i++;
  1949. // determine padding character
  1950. switch (gp_params.filename[i]) {
  1951. // case ' ':
  1952. // spaces are not supported everywhere, so we
  1953. // restrict ourselves to padding with zeros.
  1954. case '0':
  1955. padding = gp_params.filename[i];
  1956. precision = 1; // do padding
  1957. i++;
  1958. break;
  1959. }
  1960. // determine padding width
  1961. if (isdigit((int)gp_params.filename[i])) {
  1962. char *cp;
  1963. long int _prec;
  1964. _prec = strtol(&gp_params.filename[i],
  1965. &cp, 10);
  1966. if (_prec < 1)
  1967. precision = 1;
  1968. else if (_prec > 20)
  1969. precision = 20;
  1970. else
  1971. precision = _prec;
  1972. if (*cp != 'n') {
  1973. // make sure this is %n
  1974. gp_context_error (gp_params.context,
  1975. _("Zero padding numbers "
  1976. "in file names is only "
  1977. "possible with %%n."));
  1978. return GP_ERROR_BAD_PARAMETERS;
  1979. }
  1980. // go to first non-digit character
  1981. i += (cp - &gp_params.filename[i]);
  1982. } else if (precision && ( gp_params.filename[i] != 'n')) {
  1983. gp_context_error (gp_params.context,
  1984. _("You cannot use %%n "
  1985. "zero padding "
  1986. "without a "
  1987. "precision value!"
  1988. ));
  1989. return GP_ERROR_BAD_PARAMETERS;
  1990. }
  1991. switch (gp_params.filename[i]) {
  1992. case 'n':
  1993. // Previously this used an folder index number.
  1994. // Now this uses a linear increasing number.
  1995. if (precision > 1) {
  1996. char padfmt[16];
  1997. strcpy(padfmt, "%!.*i");
  1998. padfmt[1] = padding;
  1999. snprintf (b, sizeof (b), padfmt,
  2000. precision, filenr);
  2001. } else {
  2002. snprintf (b, sizeof (b), "%i",
  2003. filenr);
  2004. }
  2005. filenr++;
  2006. break;
  2007. case 'C':
  2008. // Get the suffix of the original name
  2009. s = strrchr (name, '.');
  2010. if (!s) {
  2011. free (*path);
  2012. *path = NULL;
  2013. gp_context_error (gp_params.context,
  2014. _("The filename provided "
  2015. "by the camera ('%s') "
  2016. "does not contain a "
  2017. "suffix!"), name);
  2018. return (GP_ERROR_BAD_PARAMETERS);
  2019. }
  2020. strncpy (b, s + 1, sizeof (b) - 1);
  2021. break;
  2022. case 'f':
  2023. // Get the file name without suffix
  2024. s = strrchr (name, '.');
  2025. if (!s)
  2026. strncpy (b, name, sizeof (b) - 1);
  2027. else {
  2028. l = MIN ((unsigned long)(sizeof(b) - 1),
  2029. (unsigned long)(s - name));
  2030. strncpy (b, name, l);
  2031. b[l] = '\0';
  2032. }
  2033. break;
  2034. case 'F':
  2035. // Get the folder name
  2036. {
  2037. const char *f = folder ? folder : "";
  2038. if (f[0] == '/') f++; // Skip first '/'
  2039. if (!f[0]) f = "."; // replace empty folder name by '.'
  2040. strncpy (b, f, sizeof (b) - 1 - strlen(b));
  2041. b[sizeof(b) - 1] = '\0';
  2042. }
  2043. break;
  2044. case 'a':
  2045. case 'A':
  2046. case 'b':
  2047. case 'B':
  2048. case 'd':
  2049. case 'H':
  2050. case 'k':
  2051. case 'I':
  2052. case 'l':
  2053. case 'j':
  2054. case 'm':
  2055. case 'M':
  2056. case 'S':
  2057. case 'y':
  2058. case 'Y':
  2059. {
  2060. char fmt[3] = { '%', '\0', '\0' };
  2061. if (!file) return (GP_ERROR_BAD_PARAMETERS); // mtime unknown
  2062. fmt[1] = gp_params.filename[i]; // the letter of this 'case'
  2063. strftime(b, sizeof (b), fmt, tm);
  2064. break;
  2065. }
  2066. case '%':
  2067. strcpy (b, "%");
  2068. break;
  2069. case ':':
  2070. strncpy_lower(b, name, sizeof(b));
  2071. b[sizeof(b)-1] = '\0';
  2072. break;
  2073. default:
  2074. free (*path);
  2075. *path = NULL;
  2076. gp_context_error (gp_params.context,
  2077. _("Invalid format '%s' (error at "
  2078. "position %i)."), gp_params.filename,
  2079. i + 1);
  2080. return (GP_ERROR_BAD_PARAMETERS);
  2081. }
  2082. } else {
  2083. b[0] = gp_params.filename[i];
  2084. b[1] = '\0';
  2085. }
  2086. s = *path ? realloc (*path, strlen (*path) + strlen (b) + 1) :
  2087. malloc (strlen (b) + 1);
  2088. if (!s) {
  2089. free (*path);
  2090. *path = NULL;
  2091. return (GP_ERROR_NO_MEMORY);
  2092. }
  2093. if (*path) {
  2094. *path = s;
  2095. strcat (*path, b);
  2096. } else {
  2097. *path = s;
  2098. strcpy (*path, b);
  2099. }
  2100. }
  2101. // If the file is a capture_preview,
  2102. // apply prefix over the calculated basename
  2103. if (type == GP_FILE_TYPE_PREVIEW) {
  2104. return gp_file_get_name_by_type (file, s, type, path);
  2105. }
  2106. return (GP_OK);
  2107. };
  2108. int gPhotoCamera::save_camera_file_to_file ( const char *folder, const char *name, CameraFileType type, CameraFile *file, const char *curname ){
  2109. char * path = NULL;
  2110. char s[1024];
  2111. char c[1024];
  2112. int res;
  2113. time_t mtime;
  2114. struct utimbuf u;
  2115. CR (get_path_for_file (folder, name, type, file, &path));
  2116. strncpy (s, path, sizeof (s) - 1);
  2117. s[sizeof (s) - 1] = '\0';
  2118. free (path);
  2119. path = NULL;
  2120. if( (gp_params.flags & FLAGS_SKIP_EXISTING) && gp_system_is_file(s) ){
  2121. #ifdef DEBUG
  2122. char tBuff[128];
  2123. sprintf( tBuff, "Skip existing file %s", s );
  2124. LOG( tBuff );
  2125. #endif
  2126. if( curname ) unlink( curname );
  2127. return( GP_OK );
  2128. };
  2129. if( (gp_params.flags & FLAGS_QUIET) == 0 ){
  2130. while((gp_params.flags & FLAGS_FORCE_OVERWRITE) == 0 && gp_system_is_file (s) ){
  2131. do{
  2132. putchar ('\007');
  2133. printf (_("File %s exists. Overwrite? [y|n] "), s);
  2134. fflush (stdout);
  2135. if (NULL == fgets (c, sizeof (c) - 1, stdin)) return GP_ERROR;
  2136. } while ((c[0]!='y')&&(c[0]!='Y')&& (c[0]!='n')&&(c[0]!='N'));
  2137. if ((c[0]=='y') || (c[0]=='Y')) break;
  2138. do {
  2139. printf (_("Specify new filename? [y|n] "));
  2140. fflush (stdout);
  2141. if (NULL == fgets (c, sizeof (c) - 1, stdin)) return GP_ERROR;
  2142. } while ((c[0]!='y')&&(c[0]!='Y')&& (c[0]!='n')&&(c[0]!='N'));
  2143. if (!((c[0]=='y') || (c[0]=='Y'))) {
  2144. if (curname) unlink (curname);
  2145. return (GP_OK);
  2146. };
  2147. printf (_("Enter new filename: "));
  2148. fflush (stdout);
  2149. if (NULL == fgets (s, sizeof (s) - 1, stdin)) return GP_ERROR;
  2150. s[strlen (s) - 1] = 0;
  2151. };
  2152. printf (_("Saving file as %s\n"), s);
  2153. fflush (stdout);
  2154. };
  2155. path = s;
  2156. while( (path = strchr(path, gp_system_dir_delim)) ){
  2157. *path = '\0';
  2158. if(!gp_system_is_dir (s)) gp_system_mkdir (s);
  2159. *path++ = gp_system_dir_delim;
  2160. };
  2161. if (curname) {
  2162. int x;
  2163. unlink(s);
  2164. if (-1 == rename (curname, s)) {
  2165. // happens if the user specified a absolute path with --filename
  2166. // EPERM happens on windows, see https://github.com/gphoto/libgphoto2/issues/97
  2167. if ((errno == EXDEV) || (errno == EPERM)) {
  2168. char buf[8192];
  2169. int in_fd, out_fd;
  2170. in_fd = open(curname, O_RDONLY);
  2171. if (in_fd < 0) LOG_ERROR("Can't open file for reading");
  2172. out_fd = open(s, O_CREAT | O_WRONLY, 0644);
  2173. if (out_fd < 0) LOG_ERROR("Can't open file for writing");
  2174. while (1) {
  2175. ssize_t result = read(in_fd, buf, sizeof(buf));
  2176. if (!result) break;
  2177. if (-1 == write(out_fd, buf, result)) {
  2178. LOG_ERROR("write");
  2179. break;
  2180. }
  2181. }
  2182. close(out_fd);
  2183. close(in_fd);
  2184. unlink(curname);
  2185. } else
  2186. perror("rename");
  2187. }
  2188. x = umask(0022); // get umask
  2189. umask(x); // set it back to the old value
  2190. chmod(s,0666 & ~x);
  2191. }
  2192. res = gp_file_get_mtime (file, &mtime);
  2193. if ((res == GP_OK) && (mtime)) {
  2194. u.actime = mtime;
  2195. u.modtime = mtime;
  2196. utime (s, &u);
  2197. }
  2198. gp_params_run_hook(&gp_params, "download", s);
  2199. return (GP_OK);
  2200. }
  2201. int gPhotoCamera::camera_file_exists( Camera *camera, GPContext *context, const char *folder, const char *filename, CameraFileType type ){
  2202. CameraFileInfo info;
  2203. CR (gp_camera_file_get_info (camera, folder, filename, &info,
  2204. context));
  2205. switch (type) {
  2206. case GP_FILE_TYPE_METADATA:
  2207. return TRUE;
  2208. case GP_FILE_TYPE_AUDIO:
  2209. return (info.audio.fields != 0);
  2210. case GP_FILE_TYPE_PREVIEW:
  2211. return (info.preview.fields != 0);
  2212. case GP_FILE_TYPE_RAW:
  2213. case GP_FILE_TYPE_NORMAL:
  2214. return (info.file.fields != 0);
  2215. default:
  2216. gp_context_error (context, "Unknown file type in camera_file_exists: %d", type);
  2217. return FALSE;
  2218. }
  2219. }
  2220. static int x_read(void*priv,unsigned char *data, uint64_t *size) {
  2221. struct privstr *ps = priv;
  2222. int fd = ps->fd;
  2223. uint64_t curread = 0, xsize, res;
  2224. gp_log (GP_LOG_DEBUG, "x_read", "(%p,%p,%u)", priv, data, (unsigned int)*size);
  2225. xsize = *size;
  2226. while (curread < xsize) {
  2227. res = read (fd, data+curread, xsize-curread);
  2228. if (res == -1) return GP_ERROR_IO_READ;
  2229. if (!res) break;
  2230. curread += res;
  2231. }
  2232. *size = curread;
  2233. return GP_OK;
  2234. }
  2235. static int x_write(void*priv,unsigned char *data, uint64_t *size) {
  2236. struct privstr *ps = priv;
  2237. int fd = ps->fd;
  2238. uint64_t curwritten = 0, xsize, res;
  2239. gp_log (GP_LOG_DEBUG, "x_write","(%p,%p,%u)", priv, data, (unsigned int)*size);
  2240. xsize = *size;
  2241. while (curwritten < xsize) {
  2242. res = write (fd, data+curwritten, xsize-curwritten);
  2243. if (res == -1) return GP_ERROR_IO_WRITE;
  2244. if (!res) break;
  2245. curwritten += res;
  2246. }
  2247. *size = curwritten;
  2248. return GP_OK;
  2249. }
  2250. static CameraFileHandler xhandler = { x_size, x_read, x_write };
  2251. int gPhotoCamera::save_file_to_file( Camera *camera, GPContext *context, Flags flags, const char *folder, const char *filename, CameraFileType type ){
  2252. int fd, res;
  2253. CameraFile * file;
  2254. char tmpname[20], * tmpfilename;
  2255. struct privstr * ps = NULL;
  2256. if( ( flags & FLAGS_SKIP_EXISTING ) && !(flags & FLAGS_STDOUT) ){
  2257. char *path = NULL;
  2258. // Check if the file is present before downloading it.
  2259. res = get_path_for_file (folder, filename, type, NULL, &path);
  2260. if( res == (GP_OK) && gp_system_is_file( path ) ){
  2261. // File name pattern do not require CameraFile and target file exists: Skip this file.
  2262. if( (gp_params.flags & FLAGS_QUIET) == 0 ){
  2263. printf (_("Skip existing file %s\n"), path);
  2264. fflush (stdout);
  2265. };
  2266. return (GP_OK);
  2267. };
  2268. };
  2269. if( flags & FLAGS_NEW ){
  2270. CameraFileInfo info;
  2271. CR( gp_camera_file_get_info(camera, folder, filename, &info, context) );
  2272. switch( type ){
  2273. case GP_FILE_TYPE_PREVIEW:
  2274. if (info.preview.fields & GP_FILE_INFO_STATUS && info.preview.status == GP_FILE_STATUS_DOWNLOADED) return (GP_OK);
  2275. break;
  2276. case GP_FILE_TYPE_NORMAL:
  2277. case GP_FILE_TYPE_RAW:
  2278. case GP_FILE_TYPE_EXIF:
  2279. if (info.file.fields & GP_FILE_INFO_STATUS && info.file.status == GP_FILE_STATUS_DOWNLOADED) return (GP_OK);
  2280. break;
  2281. case GP_FILE_TYPE_AUDIO:
  2282. if (info.audio.fields & GP_FILE_INFO_STATUS && info.audio.status == GP_FILE_STATUS_DOWNLOADED) return (GP_OK);
  2283. break;
  2284. default:
  2285. return (GP_ERROR_NOT_SUPPORTED);
  2286. };
  2287. };
  2288. strcpy (tmpname, "tmpfileXXXXXX");
  2289. fd = mkstemp(tmpname);
  2290. if (fd == -1) {
  2291. if (errno == EACCES) {
  2292. gp_context_error( context, _("Permission denied") );
  2293. return GP_ERROR;
  2294. };
  2295. CR( gp_file_new (&file) );
  2296. tmpfilename = NULL;
  2297. } else {
  2298. if (time(NULL) & 1) { // to test both methods.
  2299. gp_log (GP_LOG_DEBUG, "save_file_to_file","using fd method");
  2300. res = gp_file_new_from_fd (&file, fd);
  2301. if (res < GP_OK) {
  2302. close (fd);
  2303. unlink (tmpname);
  2304. return res;
  2305. }
  2306. } else {
  2307. gp_log (GP_LOG_DEBUG, "save_file_to_file","using handler method");
  2308. ps = malloc (sizeof(*ps));
  2309. if (!ps) return GP_ERROR_NO_MEMORY;
  2310. ps->fd = fd;
  2311. // just pass in the file pointer as private
  2312. res = gp_file_new_from_handler (&file, &xhandler, ps);
  2313. if (res < GP_OK) {
  2314. close (fd);
  2315. unlink (tmpname);
  2316. return res;
  2317. }
  2318. }
  2319. tmpfilename = tmpname;
  2320. }
  2321. res = gp_camera_file_get (camera, folder, filename, type,
  2322. file, context);
  2323. if( res < GP_OK ){
  2324. free (ps);
  2325. gp_file_unref( file );
  2326. if( tmpfilename ) unlink( tmpfilename );
  2327. return res;
  2328. };
  2329. if (flags & FLAGS_STDOUT) {
  2330. const char *data;
  2331. unsigned long int size;
  2332. CR (gp_file_get_data_and_size (file, &data, &size));
  2333. if (flags & FLAGS_STDOUT_SIZE) // this will be difficult in fd mode
  2334. printf ("%li\n", size);
  2335. if (1!=fwrite (data, size, 1, stdout))
  2336. fprintf(stderr,"fwrite failed writing to stdout.\n");
  2337. if (ps && ps->fd) close (ps->fd);
  2338. free (ps);
  2339. gp_file_unref (file);
  2340. unlink (tmpname);
  2341. return (GP_OK);
  2342. }
  2343. res = save_camera_file_to_file (folder, filename, type, file, tmpfilename);
  2344. if (ps && ps->fd) close (ps->fd);
  2345. free (ps);
  2346. gp_file_unref (file);
  2347. if ((res!=GP_OK) && tmpfilename)
  2348. unlink (tmpfilename);
  2349. return (res);
  2350. }
  2351. */
  2352. bool gPhotoCamera::isDead(){
  2353. return false;
  2354. };
  2355. #undef LOG
  2356. #undef LOG_ERROR