Kaydet (Commit) eb80fc78 authored tarafından Bahadır Kandemir's avatar Bahadır Kandemir

Get process locale and translate strings properly

üst 62820176
......@@ -62,7 +62,8 @@ SET (SOURCES src/main.c
SET (LIBS python2.5
dbus-1
polkit-dbus
db)
db
proc)
# Include standard headers
INCLUDE_DIRECTORIES (include/
......
......@@ -7,11 +7,12 @@
** option) any later version. Please read the COPYING file.
*/
#include <dbus/dbus.h>
#include <Python.h>
#include <dbus/dbus.h>
void dbus_send(DBusMessage *reply);
void dbus_signal(const char *path, const char *interface, const char *name, PyObject *obj);
void dbus_method_call(DBusMessage* msg, DBusConnection* conn);
void dbus_listen();
void dbus_app_methods(const char *interface, const char *path, const char *method);
char *dbus_caller_locale(DBusMessage *msg);
......@@ -21,6 +21,7 @@ struct Proc {
// parent info
struct ProcChild parent;
const char *desc;
const char *locale;
// children info
int nr_children;
int max_children;
......
......@@ -17,3 +17,5 @@ char *load_file(const char *fname, int *sizeptr);
int save_file(const char *fname, const char *buffer, size_t size);
unsigned long time_diff(struct timeval *start, struct timeval *end);
char* get_proc_lang(pid_t pid);
......@@ -44,7 +44,12 @@ c_i18n(PyObject *self, PyObject *args)
if (!PyArg_ParseTuple(args, "O!", &PyDict_Type, &dict))
return NULL;
if (my_proc.locale) {
ret = PyDict_GetItemString(dict, my_proc.locale);
}
else {
ret = PyDict_GetItemString(dict, "en");
}
Py_INCREF(ret);
return ret;
}
......
......@@ -156,6 +156,46 @@ dbus_signal(const char *path, const char *interface, const char *name, PyObject
dbus_send(msg);
}
//! Get locale of caller
char *
dbus_caller_locale(DBusMessage *msg)
{
const char *sender = dbus_message_get_sender(msg);
DBusError err;
DBusConnection *conn;
PolKitContext *polkit_ctx;
PolKitCaller *polkit_clr;
PolKitError *perr;
dbus_error_init(&err);
conn = dbus_bus_get_private(DBUS_BUS_SYSTEM, &err);
if (dbus_error_is_set(&err)) {
log_error("Unable to open connection to query CK: %s\n", err.message);
dbus_error_free(&err);
return NULL;
}
polkit_ctx = polkit_context_new();
if (!polkit_context_init(polkit_ctx, &perr)) {
log_error("Unable to initialize PK context: %s\n", polkit_error_get_error_message(perr));
polkit_error_free(perr);
return NULL;
}
polkit_clr = polkit_caller_new_from_dbus_name(conn, sender, &err);
if (dbus_error_is_set(&err)) {
log_error("Unable to get caller info: %s\n", err.message);
dbus_error_free(&err);
return NULL;
}
pid_t pid;
polkit_caller_get_pid(polkit_clr, &pid);
return get_proc_lang(pid);
}
//! Creates a message from Python object and sends
static void
dbus_reply_object(PyObject *obj)
......
......@@ -7,6 +7,7 @@
** option) any later version. Please read the COPYING file.
*/
#include <Python.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
......@@ -21,6 +22,7 @@
#include <time.h>
#include "cfg.h"
#include "dbus.h"
#include "log.h"
#include "process.h"
......@@ -318,6 +320,7 @@ proc_fork(void (*child_func)(void), const char *desc, DBusConnection *bus_conn,
my_proc.parent.to = fdr[1];
my_proc.parent.pid = getppid();
my_proc.desc = desc;
my_proc.locale = dbus_caller_locale(bus_msg);
my_proc.bus_conn = bus_conn;
my_proc.bus_msg = bus_msg;
handle_signals();
......
......@@ -13,9 +13,39 @@
#include <stdio.h>
#include <time.h>
#include <sys/stat.h>
#include <proc/readproc.h>
#include "utility.h"
//! Returns locale of a process
char *
get_proc_lang(pid_t pid)
{
pid_t pids[2];
pids[0] = pid;
pids[1] = 0;
proc_t proc;
PROCTAB *ptp;
int i = 0;
char *lang = NULL;
ptp = openproc(PROC_FILLENV | PROC_PID, pids);
while(readproc(ptp, &proc)) {
while (proc.environ[++i]) {
if (strncmp(proc.environ[i - 1], "LANG", 4) == 0) {
lang = strsub(proc.environ[i - 1], 5, 7);
goto out;
}
}
}
out:
closeproc(ptp);
return lang;
}
//! Returns part of a string
char *
strsub(const char *str, int start, int end)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment