// Callbacks
self->callbacks.log = NULL;
+ self->callbacks.confirm = NULL;
self->callbacks.status = NULL;
self->callbacks.progress = NULL;
}
Py_XDECREF(result);
}
+static int Pakfire_confirm_callback(struct pakfire* pakfire, void* data,
+ const char* message, const char* question) {
+ PyObject* callback = (PyObject*)data;
+ int r = 0;
+
+ // Do nothing if callback isn't set
+ if (!callback)
+ return 0;
+
+ PyObject* args = Py_BuildValue("(ss)", message, question);
+ if (!args)
+ return -1;
+
+ PyObject* result = PyObject_CallObject(callback, args);
+
+ // Extract return code
+ if (PyLong_Check(result))
+ r = PyLong_AsLong(result);
+
+ Py_DECREF(args);
+ Py_DECREF(result);
+
+ return r;
+}
+
static void Pakfire_status_callback(void* data, const char* message) {
struct callbacks* callbacks = (struct callbacks*)data;
"build",
"enable_ccache",
"enable_snapshot",
+ "confirm_callback",
NULL,
};
const char* path = NULL;
int enable_ccache = 1;
int enable_snapshot = 1;
- if (!PyArg_ParseTupleAndKeywords(args, kwds, "|zzOppzppp", kwlist,
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "|zzOppzpppO", kwlist,
&path, &arch, &self->callbacks.log, &interactive, &offline, &conf, &build,
- &enable_ccache, &enable_snapshot))
+ &enable_ccache, &enable_snapshot, &self->callbacks.confirm))
return -1;
// Check if log callback is callable
return -1;
}
+ // Check if confirm callback is callable
+ if (self->callbacks.confirm && !PyCallable_Check(self->callbacks.confirm)) {
+ PyErr_SetString(PyExc_TypeError, "Confirm callback is not callable");
+ return -1;
+ }
+
int flags = 0;
// Enable interactive mode
return -1;
}
+ // Configure confirm callback
+ if (self->callbacks.confirm) {
+ pakfire_set_confirm_callback(self->pakfire,
+ Pakfire_confirm_callback, self->callbacks.confirm);
+
+ Py_INCREF(self->callbacks.confirm);
+ }
+
return 0;
}
static void Pakfire_dealloc(PakfireObject* self) {
if (self->pakfire) {
// Reset log callback
- pakfire_set_log_callback(self->pakfire, NULL, NULL);
+ if (self->callbacks.log) {
+ pakfire_set_log_callback(self->pakfire, NULL, NULL);
+ Py_DECREF(self->callbacks.log);
+ }
+
+ // Reset confirm callback
+ if (self->callbacks.confirm) {
+ pakfire_set_confirm_callback(self->pakfire, NULL, NULL);
+ Py_DECREF(self->callbacks.confirm);
+ }
pakfire_unref(self->pakfire);
}
// Free callbacks
- Py_XDECREF(self->callbacks.log);
Py_XDECREF(self->callbacks.status);
Py_XDECREF(self->callbacks.progress);
// Callbacks
struct callbacks {
PyObject* log;
+ PyObject* confirm;
PyObject* status;
PyObject* progress;
} callbacks;
int line, const char* fn, const char* format, va_list args);
void pakfire_set_log_callback(struct pakfire* pakfire,
pakfire_log_callback callback, void* data);
+typedef int (*pakfire_confirm_callback)(struct pakfire* pakfire, void* data,
+ const char* message, const char* question);
+void pakfire_set_confirm_callback(struct pakfire* pakfire,
+ pakfire_confirm_callback callback, void* data);
int pakfire_create(struct pakfire** pakfire, const char* path, const char* arch,
const char* conf, int flags, pakfire_log_callback log_callback, void* log_data);
struct pakfire_config* pakfire_get_config(struct pakfire* pakfire);
int pakfire_is_mountpoint(struct pakfire* pakfire, const char* path);
+int pakfire_confirm(struct pakfire* pakfire, const char* message, const char* question);
void pakfire_call_status_callback(struct pakfire* pakfire, const char* message, ...);
void pakfire_call_progress_callback(struct pakfire* pakfire, int progress);
#include <pakfire/problem.h>
#include <pakfire/request.h>
-int pakfire_ui_confirm(struct pakfire* pakfire, const char* message, const char* question);
+int pakfire_ui_confirm(struct pakfire* pakfire, void* data,
+ const char* message, const char* question);
int pakfire_ui_pick_solution(struct pakfire* pakfire, struct pakfire_request* request,
struct pakfire_problem** problems);
pakfire_ref;
pakfire_refresh;
pakfire_search;
+ pakfire_set_confirm_callback;
pakfire_set_log_callback;
pakfire_sync;
pakfire_unref;
pakfire_log_callback log;
void* log_data;
+ // Confirm
+ pakfire_confirm_callback confirm;
+ void* confirm_data;
+
// Status
void (*status)(void* data, const char* message);
void* status_data;
if (env)
pakfire_log_set_priority(p, log_priority(env));
+ // Setup confirm callback
+ pakfire_set_confirm_callback(p, pakfire_ui_confirm, NULL);
+
// Initialise configuration
r = pakfire_config_create(&p->config);
if (r)
errno = saved_errno;
}
+// UI
+
+PAKFIRE_EXPORT void pakfire_set_confirm_callback(struct pakfire* pakfire,
+ pakfire_confirm_callback callback, void* data) {
+ pakfire->callbacks.confirm = callback;
+ pakfire->callbacks.confirm_data = data;
+}
+
+int pakfire_confirm(struct pakfire* pakfire, const char* message, const char* question) {
+ // No callback configured
+ if (!pakfire->callbacks.confirm)
+ return 0;
+
+ // Skip this, if running in non-interactive mode
+ if (!pakfire_has_flag(pakfire, PAKFIRE_FLAGS_INTERACTIVE))
+ return 0;
+
+ // Run callback
+ return pakfire->callbacks.confirm(
+ pakfire, pakfire->callbacks.confirm_data, message, question);
+}
+
static const char* pakfire_user_lookup(void* data, la_int64_t uid) {
struct pakfire* pakfire = (struct pakfire*)data;
// Check if we should continue
if (pakfire_has_flag(transaction->pakfire, PAKFIRE_FLAGS_INTERACTIVE)) {
- r = pakfire_ui_confirm(transaction->pakfire, dump, _("Is this okay? [y/N]"));
+ r = pakfire_confirm(transaction->pakfire, dump, _("Is this okay? [y/N]"));
if (r) {
ERROR(transaction->pakfire, "Transaction aborted upon user request\n");
goto ERROR;
#include <pakfire/request.h>
#include <pakfire/ui.h>
-int pakfire_ui_confirm(struct pakfire* pakfire, const char* message, const char* question) {
+int pakfire_ui_confirm(struct pakfire* pakfire, void* data, const char* message,
+ const char* question) {
// The message is only printed once
printf("%s\n", message);
- // Skip this, if running in non-interactive mode
- if (!pakfire_has_flag(pakfire, PAKFIRE_FLAGS_INTERACTIVE))
- return 0;
-
char* line = NULL;
size_t length = 0;
int r = 1;