Skip to content

Commit 787acfa

Browse files
authored
Merge pull request #228 from saeugetier/226-improve-error-message-on-camera-failure
Improve error messages for camera initialization and image capture failures
2 parents 710667f + b25fba7 commit 787acfa

2 files changed

Lines changed: 48 additions & 5 deletions

File tree

qml/content/CameraSource.qml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ Item
201201
}
202202
PropertyChanges {
203203
target: cameraSource
204-
lastError: qsTr("No camera found with the name: ") + cameraName
204+
lastError: cameraName.length !== 0 ? qsTr("No camera found with the name: ") + cameraName : qsTr("No camera selected.")
205205
}
206206
},
207207
State {

src/gphotocamera.cpp

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,21 @@ void GPhotoCameraWorker::startCamera(const QString &cameraName) {
133133

134134
// Initialize camera connection
135135
int ret = gp_camera_init(mCamera.get(), mContext.get());
136+
136137
if (ret < GP_OK) {
137-
emit errorOccurred(tr("Failed to initialize camera: %1").arg(ret));
138+
// Translate gPhoto2 result code to a human-readable string and report it
139+
const char *gphotoErrStr = nullptr;
140+
141+
// gp_result_as_string is available in libgphoto2 and translates result
142+
// codes
143+
gphotoErrStr = gp_result_as_string(ret);
144+
145+
QString initErrorString = QString::fromLocal8Bit(
146+
gphotoErrStr ? gphotoErrStr : "Unknown gPhoto2 error");
147+
148+
emit errorOccurred(tr("Failed to initialize camera: (Code %1) - %2")
149+
.arg(ret)
150+
.arg(initErrorString));
138151
mCamera.reset();
139152
return;
140153
}
@@ -190,7 +203,17 @@ void GPhotoCameraWorker::captureImage() {
190203
int ret = gp_camera_capture(mCamera.get(), GP_CAPTURE_IMAGE,
191204
&camera_file_path, mContext.get());
192205
if (ret < GP_OK) {
193-
emit captureError(tr("Failed to capture image: %1").arg(ret));
206+
// Translate gPhoto2 result code to a human-readable string and report it
207+
const char *gphotoErrStr = nullptr;
208+
209+
// gp_result_as_string is available in libgphoto2 and translates result
210+
// codes
211+
gphotoErrStr = gp_result_as_string(ret);
212+
213+
QString captureErrorString = QString::fromLocal8Bit(
214+
gphotoErrStr ? gphotoErrStr : "Unknown gPhoto2 error");
215+
216+
emit captureError(tr("Failed to capture image: (Code %1) - $2").arg(ret).arg(captureErrorString));
194217
return;
195218
}
196219

@@ -199,7 +222,17 @@ void GPhotoCameraWorker::captureImage() {
199222
camera_file_path.name, GP_FILE_TYPE_NORMAL,
200223
captureFile.get(), mContext.get());
201224
if (ret < GP_OK) {
202-
emit captureError(tr("Failed to download image: %1").arg(ret));
225+
// Translate gPhoto2 result code to a human-readable string and report it
226+
const char *gphotoErrStr = nullptr;
227+
228+
// gp_result_as_string is available in libgphoto2 and translates result
229+
// codes
230+
gphotoErrStr = gp_result_as_string(ret);
231+
232+
QString captureErrorString = QString::fromLocal8Bit(
233+
gphotoErrStr ? gphotoErrStr : "Unknown gPhoto2 error");
234+
235+
emit captureError(tr("Failed to download image: (Code %1) - %2").arg(ret).arg(captureErrorString));
203236
return;
204237
}
205238

@@ -208,7 +241,17 @@ void GPhotoCameraWorker::captureImage() {
208241
unsigned long int size = 0;
209242
ret = gp_file_get_data_and_size(captureFile.get(), &data, &size);
210243
if (ret < GP_OK) {
211-
emit captureError(tr("Failed to get image data: %1").arg(ret));
244+
// Translate gPhoto2 result code to a human-readable string and report it
245+
const char *gphotoErrStr = nullptr;
246+
247+
// gp_result_as_string is available in libgphoto2 and translates result
248+
// codes
249+
gphotoErrStr = gp_result_as_string(ret);
250+
251+
QString captureErrorString = QString::fromLocal8Bit(
252+
gphotoErrStr ? gphotoErrStr : "Unknown gPhoto2 error");
253+
254+
emit captureError(tr("Failed to get image data: (Code %1) - %2").arg(ret).arg(captureErrorString));
212255
return;
213256
}
214257

0 commit comments

Comments
 (0)