1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
|
#include <Python.h>
#include "pytype_contacts.h"
#include "pytype_basics.h"
/* Here we're using ContactObject as if it were a parent class for all
TodoObject, et. al. */
void SetBasicRecordObjectAttributeDefaults(PyObject *self, int (*packer)( )) {
((PyPiContact*)self)->saved_br.size = 0;
((PyPiContact*)self)->saved_br.buf = NULL;
((PyPiContact*)self)->deleted = 0;
((PyPiContact*)self)->modified = 0;
((PyPiContact*)self)->busy = 0;
((PyPiContact*)self)->secret = 0;
((PyPiContact*)self)->archived = 0;
((PyPiContact*)self)->category = 0;
((PyPiContact*)self)->unique_id = 0;
((PyPiContact*)self)->unsaved_changes = 0;
((PyPiContact*)self)->packer = packer;
}
void SetSavedBrAndRTandUniqueIDandAttribs(PCRecType rt, unsigned int unique_id,
unsigned char attrib, int size,
void *buf, PyObject *self) {
((PyPiContact*)self)->saved_br.size = size;
((PyPiContact*)self)->saved_br.attrib = attrib;
((PyPiContact*)self)->saved_br.rt = ((PyPiContact*)self)->rt = rt;
((PyPiContact*)self)->saved_br.unique_id = ((PyPiContact*)self)->unique_id = unique_id;
((PyPiContact*)self)->saved_br.buf = malloc(size);
memcpy(((PyPiContact*)self)->saved_br.buf,buf,size);
((PyPiContact*)self)->deleted = (attrib & 0x80) ? 1 : 0;
((PyPiContact*)self)->modified = (attrib & 0x40) ? 1 : 0;
((PyPiContact*)self)->busy = (attrib & 0x20) ? 1 : 0;
((PyPiContact*)self)->secret = (attrib & 0x10) ? 1 : 0;
/* http://cvs.coldsync.org/cgi-bin/viewcvs.cgi/coldsync/libpdb/
pdb.c?rev=1.44&content-type=text/vnd.viewcvs-markup
The archived bit is only set, if deleted is set. Otherwise,
the bit is used as part of the category */
if (((PyPiContact*)self)->deleted) {
((PyPiContact*)self)->archived = (attrib & 0x08) ? 1 : 0;
((PyPiContact*)self)->category = 0;
} else {
((PyPiContact*)self)->archived = 0;
((PyPiContact*)self)->category = (attrib & 0x0F);
}
}
char *FriendlyNameForRecordType(PCRecType rt) {
switch(rt) {
case PALM_REC:
return "normal";
case MODIFIED_PALM_REC:
return "modified";
case DELETED_PALM_REC:
return "deleted";
case NEW_PC_REC:
return "new_pc";
case DELETED_PC_REC:
return "deleted_pc";
case REPLACEMENT_PALM_REC:
return "replacement_rec";
case DELETED_DELETED_PALM_REC:
return "deleted_palm";
default:
return "unknown";
}
}
PyObject *Attribute_Repr(PyObject* self) {
static PyObject *format = NULL;
PyObject *args, *result;
if (format == NULL) {
format = PyString_FromString("d:%d m:%d b:%d s:%d a:%d cat:%d type:%s uid=%d");
if (format == NULL)
return NULL;
}
args = Py_BuildValue("iiiiiisi",
((PyPiContact*)self)->deleted,
((PyPiContact*)self)->modified,
((PyPiContact*)self)->busy,
((PyPiContact*)self)->secret,
((PyPiContact*)self)->archived,
((PyPiContact*)self)->category,
FriendlyNameForRecordType(((PyPiContact*)self)->rt),
((PyPiContact*)self)->unique_id);
if (args == NULL)
return NULL;
result = PyString_Format(format, args);
Py_DECREF(args);
return result;
}
PyObject * PyPi_Getbool(PyObject *self, void *closure) {
void *addr = (void *)self + (size_t) closure;
if (*(size_t *)(addr)) {
Py_INCREF(Py_True);
return Py_True;
} else {
Py_INCREF(Py_False);
return Py_False;
}
}
PyObject * PyPi_GetCategory(PyObject *self, void *closure) {
void *addr = (void *)self + (size_t) closure;
return PyInt_FromLong(*(long *)addr);
}
int PyPi_Setbool(PyObject *self, PyObject *value, void *closure) {
void *addr = (void *)self + (size_t) closure;
if (value == NULL) {
PyErr_SetString(PyExc_TypeError, "Cannot delete the attribute");
return -1;
}
if (!(PyBool_Check(value) || PyInt_Check(value))) {
PyErr_SetString(PyExc_TypeError, "The attribute value must be a boolean");
return -1;
}
if (PyInt_Check(value) && PyInt_AsLong(value)) {
*(size_t*)addr = 1;
} else if (PyBool_Check(value) && (value == Py_True)) {
*(size_t*)addr = 1;
} else {
*(size_t*)addr = 0;
}
return 0;
}
int PyPi_SetCategory(PyObject *self, PyObject *value, void *closure) {
void *addr = (void *)self + (size_t) closure;
int category;
if (value == NULL) {
PyErr_SetString(PyExc_TypeError, "Cannot delete the attribute");
return -1;
}
if (!PyInt_Check(value)) {
PyErr_SetString(PyExc_TypeError, "The category must be an integer");
return -1;
}
category = PyInt_AsLong(value);
if ((category >= 0) && (category <= 15)) {
*(size_t*)addr = category;
} else {
PyErr_SetString(PyExc_ValueError, "The category value must be 0 <= x <= 15");
return -1;
}
return 0;
}
VObject *safeAddPropValue(VObject *o, const char *id, char *val) {
VObject *vo = NULL;
unsigned int charsetflag = 0;
unsigned int qpflag = 0;
unsigned int n = 0;
if (val) {
GString *val_copy = g_string_new(val);
for (n=0; ; n++) {
if (val[n] == 0)
break;
if ((unsigned char)(val[n]) > 127) {
qpflag = 1;
charsetflag = 1;
};
if ((unsigned char)(val[n]) == '\n')
qpflag = 1;
};
if (qpflag) {
for (n=0; n < val_copy->len ; n++) {
if ((unsigned char)(val_copy->str[n]) == '\n') {
g_string_insert_c(val_copy, n, '\r');
n++;
}
};
};
vo = addPropValue(o, id, val_copy->str);
g_string_free(val_copy,1);
if (charsetflag) {
if (isAPropertyOf(vo,"CHARSET") == NULL)
addPropValue(vo,"CHARSET","cp1252");
}
if (qpflag) {
if (isAPropertyOf(vo,VCEncodingProp) == NULL)
addPropValue(vo,VCEncodingProp,VCQuotedPrintableProp);
}
}
return vo;
}
// adds the properties to the original VObject
VObject *safeAddPropValuePart(VObject *o, const char *id, char *val) {
VObject *vo = NULL;
unsigned int charsetflag = 0;
unsigned int qpflag = 0;
unsigned int n = 0;
if (val) {
GString *val_copy = g_string_new(val);
for (n=0; ; n++) {
if (val[n] == 0)
break;
if ((unsigned char)(val[n]) > 127) {
qpflag = 1;
charsetflag = 1;
};
if ((unsigned char)(val[n]) == '\n')
qpflag = 1;
};
if (qpflag) {
for (n=0; n < val_copy->len ; n++) {
if ((unsigned char)(val_copy->str[n]) == '\n') {
g_string_insert_c(val_copy, n, '\r');
n++;
}
};
};
vo = addPropValue(o, id, val_copy->str);
g_string_free(val_copy,1);
if (charsetflag) {
if (isAPropertyOf(o,"CHARSET") == NULL)
addPropValue(o,"CHARSET","cp1252");
}
if (qpflag) {
if (isAPropertyOf(o,VCEncodingProp) == NULL)
addPropValue(o,VCEncodingProp,VCQuotedPrintableProp);
}
}
return vo;
}
void PyPi_extend_keys_from_filters(PyPiBase* base, PyObject *list) {
if (base->filters != NULL) {
//printf("Have filters to use!\n");
if (PySequence_Check(base->filters)) {
//printf("And it's a sequence\n");
Py_ssize_t i, n;
for (i=0; i<PySequence_Size(base->filters); i++) {
//printf("Using filter %d\n", i);
PyObject *filter = PySequence_GetItem(base->filters, i); // new ref
PyObject *keys = PyObject_CallMethod(filter, "fields", NULL);
for (n=0; n<PySequence_Size(keys); n++) {
PyObject *item = PySequence_GetItem(keys, n); // new ref
PyList_Append(list, item);
Py_DECREF(item);
}
Py_DECREF(keys);
Py_DECREF(filter);
}
}
}
}
PyObject *PyPi_GetItem_from_filters(PyPiBase* self, PyObject* key) {
PyErr_Clear();
if (self->filters != NULL) {
PyObject *filters = self->filters;
// disable the filters while we're calling each one, so they don't
// have an infinite recursion. Later, we may explore turning off just
// the one we're calling, to allow them to stack...
self->filters = NULL;
//printf("Have filters to use!\n");
if (PySequence_Check(filters)) {
//printf("And it's a sequence\n");
Py_ssize_t i, n;
for (i=0; i<PySequence_Size(filters); i++) {
int handled=0;
//printf("Using filter %d\n", i);
PyObject *filter = PySequence_GetItem(filters, i); // new ref
PyObject *handles_field = PyObject_CallMethod(filter, "handles_field", "O", key);
if (Py_True == handles_field) {
PyObject *result;
//printf("Using filter %d to handle field\n", i);
result = PyObject_CallMethod(filter, "get_field",
"(OO)", self, key);
if (result == NULL) {
//printf("Filter %d returned NULL :-(\n", i);
Py_DECREF(handles_field);
Py_DECREF(filter);
self->filters = filters;
return NULL; // we expect caller to use PyErr_Occurred()
}
//printf("Filter %d returned a value\n", i);
Py_DECREF(handles_field);
Py_DECREF(filter);
self->filters = filters;
return result;
}
}
}
self->filters = filters;
}
return NULL;
}
int PyPi_SetItem_from_filters(PyPiBase* self, PyObject* key, PyObject* value) {
PyErr_Clear();
// once this works well, we could put it into the 'basic' files
if (self->filters != NULL) {
PyObject *filters = self->filters;
// disable the filters while we're calling each one, so they don't
// have an infinite recursion. Later, we may explore turning off just
// the one we're calling, to allow them to stack...
self->filters = NULL;
//printf("Have filters to use!\n");
if (PySequence_Check(filters)) {
//printf("And it's a sequence\n");
Py_ssize_t i, n;
for (i=0; i<PySequence_Size(filters); i++) {
int handled=0;
//printf("Using filter %d\n", i);
PyObject *filter = PySequence_GetItem(filters, i); // new ref
PyObject *handles_field = PyObject_CallMethod(filter, "handles_field", "O", key);
if (Py_True == handles_field) {
//printf("Using filter %d to handle field\n", i);
PyObject *result = PyObject_CallMethod(filter, "set_field",
"(OOO)", self, key, value);
if (result == NULL) {
Py_DECREF(handles_field);
Py_DECREF(filter);
self->filters = filters;
return 2;
}
Py_DECREF(result);
handled = 1;
}
Py_DECREF(handles_field);
Py_DECREF(filter);
if(handled) {
self->filters = filters;
return 1;
}
}
}
}
return 0;
}
|