Beispiel #1
0
PyObject *RingtoneToPython(GSM_Ringtone * inring)
{
	GSM_Ringtone ring;
	PyObject *name;
	PyObject *v;
	PyObject *f;
	PyObject *result;
	int i;

	if (inring->Format != RING_NOTETONE) {
		if (GSM_RingtoneConvert(&ring, inring, RING_NOTETONE) !=
		    ERR_NONE) {
			pyg_warning
			    ("Ringtone can not be converted to RING_NOTETONE, ignoring!\n");
			Py_RETURN_NONE;
		}
	} else {
		ring = *inring;
	}

	v = PyList_New(0);
	if (v == NULL)
		return NULL;

	for (i = 0; i < ring.NoteTone.NrCommands; i++) {
		f = RingCommadToPython(&(ring.NoteTone.Commands[i]));
		if (f == NULL) {
			Py_DECREF(v);
			return NULL;
		}
		if (PyList_Append(v, f) != 0) {
			Py_DECREF(f);
			Py_DECREF(v);
			return NULL;
		}
		Py_DECREF(f);
	}

	name = UnicodeStringToPython(ring.Name);
	if (name == NULL) {
		Py_DECREF(v);
		return NULL;
	}

	result = Py_BuildValue("{s:i,s:O,s:O}",
			       "AllNotesScale",
			       (int)ring.NoteTone.AllNotesScale, "Name", name,
			       "Notes", v);
	Py_DECREF(v);
	Py_DECREF(name);

	return result;
}
Beispiel #2
0
int RingtoneFromPython(PyObject * dict, GSM_Ringtone * ring)
{
	Py_ssize_t i;
	Py_ssize_t len;
	PyObject *list;
	PyObject *item;

	if (!PyDict_Check(dict)) {
		PyErr_Format(PyExc_ValueError, "Ringtone is not a dictionary");
		return 0;
	}

	memset(ring, 0, sizeof(GSM_Ringtone));

	/* FIXME: change constant to some define */
	if (!CopyStringFromDict(dict, "Name", 19, ring->Name)) {
		return 0;
	}

	list = PyDict_GetItemString(dict, "Notes");

	if (!PyList_Check(list)) {
		PyErr_Format(PyExc_ValueError, "Notes are not a list");
		return 0;
	}

	len = PyList_Size(list);

	if (len > GSM_MAX_RINGTONE_NOTES) {
		pyg_warning("Truncating Notes entries to %d entries! (from %"
			    PY_FORMAT_SIZE_T "d))\n", GSM_MAX_RINGTONE_NOTES,
			    len);
		len = GSM_MAX_RINGTONE_NOTES;
	}
	ring->NoteTone.NrCommands = len;

	for (i = 0; i < len; i++) {
		item = PyList_GetItem(list, i);
		if (item == NULL)
			return 0;
		if (!PyDict_Check(item)) {
			PyErr_Format(PyExc_ValueError,
				     "Element %" PY_FORMAT_SIZE_T
				     "d in Notes is not a dictionary", i);
			return 0;
		}
		if (!RingCommadFromPython(item, &(ring->NoteTone.Commands[i])))
			return 0;
	}

	return 1;
}
Beispiel #3
0
int CopyStringFromDict(PyObject * dict, const char *key, size_t len,
		       unsigned char *dest)
{
	unsigned char *s;

	s = GetStringFromDict(dict, key);
	if (s == NULL)
		return 0;
	if (UnicodeLength(s) > len) {
		pyg_warning("Truncating text %s to %ld chars!\n", key,
			    (long)len);
		s[2 * len] = 0;
		s[(2 * len) + 1] = 0;
	}
	CopyUnicodeString(dest, s);
	free(s);
	return 1;
}
Beispiel #4
0
int MultiBitmapFromPython(PyObject * list, GSM_MultiBitmap * bmp)
{
	PyObject *item;
	Py_ssize_t len;
	Py_ssize_t i;

	if (!PyList_Check(list)) {
		PyErr_Format(PyExc_ValueError, "Multi bitmap is not a list");
		return 0;
	}

	len = PyList_Size(list);

	if (len > GSM_MAX_MULTI_BITMAP) {
		pyg_warning
		    ("Truncating Multi Bitmap entries to %d entries! (from %"
		     PY_FORMAT_SIZE_T "d))\n", GSM_MAX_MULTI_BITMAP, len);
		len = GSM_MAX_MULTI_BITMAP;
	}
	bmp->Number = len;

	for (i = 0; i < len; i++) {
		item = PyList_GetItem(list, i);
		if (item == NULL)
			return 0;
		if (!PyDict_Check(item)) {
			PyErr_Format(PyExc_ValueError,
				     "Element %" PY_FORMAT_SIZE_T
				     "d in Bitmaps is not dictionary", i);
			return 0;
		}
		if (!BitmapFromPython(item, &(bmp->Bitmap[i])))
			return 0;
	}

	return 1;
}
Beispiel #5
0
int MemoryEntryFromPython(PyObject * dict, GSM_MemoryEntry * entry,
			  int needs_location)
{
	PyObject *o;
	PyObject *item;
	Py_ssize_t len, data_len;
	Py_ssize_t i;
	char *type, *location;
	char valuetype;
	const char *bmptype;

	if (!PyDict_Check(dict)) {
		PyErr_Format(PyExc_ValueError,
			     "Memory entry is not a dictionary");
		return 0;
	}

	memset(entry, 0, sizeof(GSM_MemoryEntry));

	entry->Location = GetIntFromDict(dict, "Location");
	if (entry->Location == INT_INVALID) {
		if (needs_location) {
			return 0;
		}
		PyErr_Clear();
	}

	entry->MemoryType = GetMemoryTypeFromDict(dict, "MemoryType");
	if (entry->MemoryType == ENUM_INVALID) {
		if (needs_location) {
			return 0;
		}
		PyErr_Clear();
	}

	o = PyDict_GetItemString(dict, "Entries");
	if (o == NULL) {
		PyErr_Format(PyExc_ValueError,
			     "Can not get string value for key Entries");
		return 0;
	}

	if (!PyList_Check(o)) {
		PyErr_Format(PyExc_ValueError,
			     "Key Entries doesn't contain list");
		return 0;
	}

	len = PyList_Size(o);
	if (len > GSM_PHONEBOOK_ENTRIES) {
		pyg_warning("Using just %i entries from list!",
			    GSM_PHONEBOOK_ENTRIES);
		len = GSM_PHONEBOOK_ENTRIES;
	}
	entry->EntriesNum = len;

	for (i = 0; i < len; i++) {
		item = PyList_GetItem(o, i);
		if (item == NULL)
			return 0;
		if (!PyDict_Check(item)) {
			PyErr_Format(PyExc_ValueError,
				     "Element %" PY_FORMAT_SIZE_T
				     "d in Entries is not dictionary", i);
			return 0;
		}
		type = GetCharFromDict(item, "Type");
		if (type == NULL)
			return 0;

		location = GetCharFromDict(item, "Location");
		if (location == NULL) {
            entry->Entries[i].Location = PBK_Location_Unknown;
        } else {
            if (strcmp(location, "Home") == 0) {
                entry->Entries[i].Location = PBK_Location_Home;
            } else if (strcmp(location, "Work") == 0) {
                entry->Entries[i].Location = PBK_Location_Work;
            } else {
                entry->Entries[i].Location = PBK_Location_Unknown;
            }
        }

		/* Zero everything, just to be sure */
		entry->Entries[i].Text[0] = 0;
		entry->Entries[i].Text[1] = 0;
		entry->Entries[i].SMSList[0] = 0;
		entry->Entries[i].Number = 0;

		/* Get VoiceTag flag */
		entry->Entries[i].VoiceTag = GetIntFromDict(item, "VoiceTag");
		if (entry->Entries[i].VoiceTag == INT_INVALID) {
			entry->Entries[i].VoiceTag = 0;
			PyErr_Clear();
		}

		/* Get AddError flag */
		entry->Entries[i].AddError = GetIntFromDict(item, "AddError");
		if (entry->Entries[i].AddError == INT_INVALID) {
			entry->Entries[i].AddError = ERR_NONE;
			PyErr_Clear();
		}

		if (strcmp("Number_General", type) == 0) {
			valuetype = 't';
			entry->Entries[i].EntryType = PBK_Number_General;
		} else if (strcmp("Number_Mobile_Work", type) == 0) {
			valuetype = 't';
			entry->Entries[i].EntryType = PBK_Number_Mobile;
            entry->Entries[i].Location = PBK_Location_Work;
		} else if (strcmp("Number_Mobile_Home", type) == 0) {
			valuetype = 't';
			entry->Entries[i].EntryType = PBK_Number_Mobile;
            entry->Entries[i].Location = PBK_Location_Home;
		} else if (strcmp("Number_Mobile", type) == 0) {
			valuetype = 't';
			entry->Entries[i].EntryType = PBK_Number_Mobile;
		} else if (strcmp("Number_Work", type) == 0) {
			valuetype = 't';
			entry->Entries[i].EntryType = PBK_Number_General;
            entry->Entries[i].Location = PBK_Location_Work;
		} else if (strcmp("Number_Fax", type) == 0) {
			valuetype = 't';
			entry->Entries[i].EntryType = PBK_Number_Fax;
		} else if (strcmp("Number_Home", type) == 0) {
			valuetype = 't';
			entry->Entries[i].EntryType = PBK_Number_General;
            entry->Entries[i].Location = PBK_Location_Home;
		} else if (strcmp("Number_Pager", type) == 0) {
			valuetype = 't';
			entry->Entries[i].EntryType = PBK_Number_Pager;
		} else if (strcmp("Number_Other", type) == 0) {
			valuetype = 't';
			entry->Entries[i].EntryType = PBK_Number_Other;
		} else if (strcmp("Number_Messaging", type) == 0) {
			valuetype = 't';
			entry->Entries[i].EntryType = PBK_Number_Messaging;
		} else if (strcmp("Number_Video", type) == 0) {
			valuetype = 't';
			entry->Entries[i].EntryType = PBK_Number_Video;
		} else if (strcmp("Text_Note", type) == 0) {
			valuetype = 't';
			entry->Entries[i].EntryType = PBK_Text_Note;
		} else if (strcmp("Text_Postal", type) == 0) {
			valuetype = 't';
			entry->Entries[i].EntryType = PBK_Text_Postal;
		} else if (strcmp("Text_WorkPostal", type) == 0) {
			valuetype = 't';
			entry->Entries[i].EntryType = PBK_Text_Postal;
            entry->Entries[i].Location = PBK_Location_Work;
		} else if (strcmp("Text_Email", type) == 0) {
			valuetype = 't';
			entry->Entries[i].EntryType = PBK_Text_Email;
		} else if (strcmp("Text_Email2", type) == 0) {
			valuetype = 't';
			entry->Entries[i].EntryType = PBK_Text_Email2;
		} else if (strcmp("Text_URL", type) == 0) {
			valuetype = 't';
			entry->Entries[i].EntryType = PBK_Text_URL;
		} else if (strcmp("Date", type) == 0) {
			valuetype = 'd';
			entry->Entries[i].EntryType = PBK_Date;
		} else if (strcmp("LastModified", type) == 0) {
			valuetype = 'd';
			entry->Entries[i].EntryType = PBK_LastModified;
		} else if (strcmp("Caller_Group", type) == 0) {
			valuetype = 'n';
			entry->Entries[i].EntryType = PBK_Caller_Group;
		} else if (strcmp("Text_Name", type) == 0) {
			valuetype = 't';
			entry->Entries[i].EntryType = PBK_Text_Name;
		} else if (strcmp("Text_LastName", type) == 0) {
			valuetype = 't';
			entry->Entries[i].EntryType = PBK_Text_LastName;
		} else if (strcmp("Text_FirstName", type) == 0) {
			valuetype = 't';
			entry->Entries[i].EntryType = PBK_Text_FirstName;
		} else if (strcmp("Text_SecondName", type) == 0) {
			valuetype = 't';
			entry->Entries[i].EntryType = PBK_Text_SecondName;
		} else if (strcmp("Text_NickName", type) == 0) {
			valuetype = 't';
			entry->Entries[i].EntryType = PBK_Text_NickName;
		} else if (strcmp("Text_FormalName", type) == 0) {
			valuetype = 't';
			entry->Entries[i].EntryType = PBK_Text_FormalName;
		} else if (strcmp("Text_NameSuffix", type) == 0) {
			valuetype = 't';
			entry->Entries[i].EntryType = PBK_Text_NameSuffix;
		} else if (strcmp("Text_NamePrefix", type) == 0) {
			valuetype = 't';
			entry->Entries[i].EntryType = PBK_Text_NamePrefix;
		} else if (strcmp("Text_Company", type) == 0) {
			valuetype = 't';
			entry->Entries[i].EntryType = PBK_Text_Company;
		} else if (strcmp("Text_JobTitle", type) == 0) {
			valuetype = 't';
			entry->Entries[i].EntryType = PBK_Text_JobTitle;
		} else if (strcmp("Category", type) == 0) {
			valuetype = 'o';
			entry->Entries[i].EntryType = PBK_Category;
		} else if (strcmp("Private", type) == 0) {
			valuetype = 'n';
			entry->Entries[i].EntryType = PBK_Private;
		} else if (strcmp("Text_StreetAddress", type) == 0) {
			valuetype = 't';
			entry->Entries[i].EntryType = PBK_Text_StreetAddress;
		} else if (strcmp("Text_City", type) == 0) {
			valuetype = 't';
			entry->Entries[i].EntryType = PBK_Text_City;
		} else if (strcmp("Text_State", type) == 0) {
			valuetype = 't';
			entry->Entries[i].EntryType = PBK_Text_State;
		} else if (strcmp("Text_Zip", type) == 0) {
			valuetype = 't';
			entry->Entries[i].EntryType = PBK_Text_Zip;
		} else if (strcmp("Text_Country", type) == 0) {
			valuetype = 't';
			entry->Entries[i].EntryType = PBK_Text_Country;
		} else if (strcmp("Text_WorkStreetAddress", type) == 0) {
			valuetype = 't';
			entry->Entries[i].EntryType = PBK_Text_StreetAddress;
            entry->Entries[i].Location = PBK_Location_Work;
		} else if (strcmp("Text_WorkCity", type) == 0) {
			valuetype = 't';
			entry->Entries[i].EntryType = PBK_Text_City;
            entry->Entries[i].Location = PBK_Location_Work;
		} else if (strcmp("Text_WorkState", type) == 0) {
			valuetype = 't';
			entry->Entries[i].EntryType = PBK_Text_State;
            entry->Entries[i].Location = PBK_Location_Work;
		} else if (strcmp("Text_WorkZip", type) == 0) {
			valuetype = 't';
			entry->Entries[i].EntryType = PBK_Text_Zip;
            entry->Entries[i].Location = PBK_Location_Work;
		} else if (strcmp("Text_WorkCountry", type) == 0) {
			valuetype = 't';
			entry->Entries[i].EntryType = PBK_Text_Country;
            entry->Entries[i].Location = PBK_Location_Work;
		} else if (strcmp("Text_Custom1", type) == 0) {
			valuetype = 't';
			entry->Entries[i].EntryType = PBK_Text_Custom1;
		} else if (strcmp("Text_Custom2", type) == 0) {
			valuetype = 't';
			entry->Entries[i].EntryType = PBK_Text_Custom2;
		} else if (strcmp("Text_Custom3", type) == 0) {
			valuetype = 't';
			entry->Entries[i].EntryType = PBK_Text_Custom3;
		} else if (strcmp("Text_Custom4", type) == 0) {
			valuetype = 't';
			entry->Entries[i].EntryType = PBK_Text_Custom4;
		} else if (strcmp("Text_LUID", type) == 0) {
			valuetype = 't';
			entry->Entries[i].EntryType = PBK_Text_LUID;
		} else if (strcmp("Text_VOIP", type) == 0) {
			valuetype = 't';
			entry->Entries[i].EntryType = PBK_Text_VOIP;
		} else if (strcmp("Text_SWIS", type) == 0) {
			valuetype = 't';
			entry->Entries[i].EntryType = PBK_Text_SWIS;
		} else if (strcmp("Text_WVID", type) == 0) {
			valuetype = 't';
			entry->Entries[i].EntryType = PBK_Text_WVID;
		} else if (strcmp("Text_SIP", type) == 0) {
			valuetype = 't';
			entry->Entries[i].EntryType = PBK_Text_SIP;
		} else if (strcmp("Text_DTMF", type) == 0) {
			valuetype = 't';
			entry->Entries[i].EntryType = PBK_Text_DTMF;
		} else if (strcmp("Text_UserID", type) == 0) {
			valuetype = 't';
			entry->Entries[i].EntryType = PBK_Text_UserID;
		} else if (strcmp("Text_PictureName", type) == 0) {
			valuetype = 't';
			entry->Entries[i].EntryType = PBK_Text_PictureName;
		} else if (strcmp("RingtoneID", type) == 0) {
			valuetype = 'n';
			entry->Entries[i].EntryType = PBK_RingtoneID;
		} else if (strcmp("PictureID", type) == 0) {
			valuetype = 'n';
			entry->Entries[i].EntryType = PBK_PictureID;
		} else if (strcmp("CallLength", type) == 0) {
			valuetype = 'c';
			entry->Entries[i].EntryType = PBK_CallLength;
		} else if (strcmp("PushToTalkID", type) == 0) {
			valuetype = 't';
			entry->Entries[i].EntryType = PBK_PushToTalkID;
		} else if (strcmp("Photo", type) == 0) {
			valuetype = 'p';
			entry->Entries[i].EntryType = PBK_Photo;
		} else {
			PyErr_Format(PyExc_ValueError,
				     "Element %" PY_FORMAT_SIZE_T
				     "d in Entries has bad type: %s", i, type);
			return 0;
		}

		switch (valuetype) {
			case 'o':
				entry->Entries[i].Number =
				    GetIntFromDict(item, "Value");
				if (entry->Entries[i].Number == INT_INVALID) {
					PyErr_Clear();
					if (!CopyStringFromDict
					    (item, "Value",
					     GSM_PHONEBOOK_TEXT_LENGTH,
					     entry->Entries[i].Text))
						return 0;
				}
				break;
			case 'n':
				entry->Entries[i].Number =
				    GetIntFromDict(item, "Value");
				if (entry->Entries[i].Number == INT_INVALID)
					return 0;
				break;
			case 'c':
				entry->Entries[i].CallLength =
				    GetIntFromDict(item, "Value");
				if (entry->Entries[i].CallLength == INT_INVALID)
					return 0;
				break;
			case 't':
				if (!CopyStringFromDict
				    (item, "Value", GSM_PHONEBOOK_TEXT_LENGTH,
				     entry->Entries[i].Text))
					return 0;
				break;
			case 'd':
				entry->Entries[i].Date =
				    GetDateTimeFromDict(item, "Value");
				if (entry->Entries[i].Date.Year == -1)
					return 0;
				break;
			case 'p':
				entry->Entries[i].Picture.Buffer =
				    (unsigned char *)
				    GetCStringLengthFromDict(item, "Value",
							     &data_len);
				entry->Entries[i].Picture.Length = data_len;
				if (entry->Entries[i].Picture.Buffer == NULL) {
					return 0;
				}
				bmptype =
				    GetCStringFromDict(item, "PictureType");
				if (strcmp(bmptype, "BMP") == 0) {
					entry->Entries[i].Picture.Type =
					    PICTURE_BMP;
				} else if (strcmp(bmptype, "GIF") == 0) {
					entry->Entries[i].Picture.Type =
					    PICTURE_GIF;
				} else if (strcmp(bmptype, "JPG") == 0) {
					entry->Entries[i].Picture.Type =
					    PICTURE_JPG;
				} else if (strcmp(bmptype, "ICN") == 0) {
					entry->Entries[i].Picture.Type =
					    PICTURE_ICN;
				} else if (strcmp(bmptype, "PNG") == 0) {
					entry->Entries[i].Picture.Type =
					    PICTURE_PNG;
				} else {
					entry->Entries[i].Picture.Type = 0;
				}
				break;
		}
	}			/* end for */

	return 1;
}
Beispiel #6
0
int TodoFromPython(PyObject * dict, GSM_ToDoEntry * entry, int needs_location)
{
	PyObject *o;
	PyObject *item;
	Py_ssize_t len;
	Py_ssize_t i;
	char *p;
	char *t;
	char *type;
	char valuetype;

	if (!PyDict_Check(dict)) {
		PyErr_Format(PyExc_ValueError,
			     "Todo entry is not a dictionary");
		return 0;
	}

	memset(entry, 0, sizeof(GSM_ToDoEntry));

	if (needs_location) {
		entry->Location = GetIntFromDict(dict, "Location");
		if (entry->Location == INT_INVALID)
			return 0;
	}

	t = GetCharFromDict(dict, "Type");
	if (t == NULL)
		return 0;
	entry->Type = StringToCalendarType(t);
	free(t);
	if (entry->Type == 0)
		return 0;

	p = GetCharFromDict(dict, "Priority");
	if (p == NULL)
		return 0;
	entry->Priority = StringToTodoPriority(p);
	free(p);
	if (entry->Priority == GSM_Priority_INVALID)
		return 0;

	o = PyDict_GetItemString(dict, "Entries");
	if (o == NULL) {
		PyErr_Format(PyExc_ValueError,
			     "Can not get string value for key Values");
		return 0;
	}

	if (!PyList_Check(o)) {
		PyErr_Format(PyExc_ValueError,
			     "Key Values doesn't contain list");
		return 0;
	}

	len = PyList_Size(o);
	if (len > GSM_TODO_ENTRIES) {
		pyg_warning("Using just %i entries from list!",
			    GSM_TODO_ENTRIES);
		len = GSM_TODO_ENTRIES;
	}
	entry->EntriesNum = len;

	for (i = 0; i < len; i++) {
		item = PyList_GetItem(o, i);
		if (item == NULL)
			return 0;
		if (!PyDict_Check(item)) {
			PyErr_Format(PyExc_ValueError,
				     "Element %" PY_FORMAT_SIZE_T
				     "d in Values is not dictionary", i);
			return 0;
		}
		type = GetCharFromDict(item, "Type");
		if (type == NULL)
			return 0;

		if (strcmp("END_DATETIME", type) == 0) {
			valuetype = 'd';
			entry->Entries[i].EntryType = TODO_END_DATETIME;
		} else if (strcmp("START_DATETIME", type) == 0) {
			valuetype = 'd';
			entry->Entries[i].EntryType = TODO_START_DATETIME;
		} else if (strcmp("COMPLETED_DATETIME", type) == 0) {
			valuetype = 'd';
			entry->Entries[i].EntryType = TODO_COMPLETED_DATETIME;
		} else if (strcmp("COMPLETED", type) == 0) {
			valuetype = 'n';
			entry->Entries[i].EntryType = TODO_COMPLETED;
		} else if (strcmp("ALARM_DATETIME", type) == 0) {
			valuetype = 'd';
			entry->Entries[i].EntryType = TODO_ALARM_DATETIME;
		} else if (strcmp("SILENT_ALARM_DATETIME", type) == 0) {
			valuetype = 'd';
			entry->Entries[i].EntryType =
			    TODO_SILENT_ALARM_DATETIME;
		} else if (strcmp("LAST_MODIFIED", type) == 0) {
			valuetype = 'd';
			entry->Entries[i].EntryType = TODO_LAST_MODIFIED;
		} else if (strcmp("LUID", type) == 0) {
			valuetype = 't';
			entry->Entries[i].EntryType = TODO_LUID;
		} else if (strcmp("LOCATION", type) == 0) {
			valuetype = 't';
			entry->Entries[i].EntryType = TODO_LOCATION;
		} else if (strcmp("DESCRIPTION", type) == 0) {
			valuetype = 't';
			entry->Entries[i].EntryType = TODO_DESCRIPTION;
		} else if (strcmp("TEXT", type) == 0) {
			valuetype = 't';
			entry->Entries[i].EntryType = TODO_TEXT;
		} else if (strcmp("PRIVATE", type) == 0) {
			valuetype = 'n';
			entry->Entries[i].EntryType = TODO_PRIVATE;
		} else if (strcmp("CATEGORY", type) == 0) {
			valuetype = 'n';
			entry->Entries[i].EntryType = TODO_CATEGORY;
		} else if (strcmp("CONTACTID", type) == 0) {
			valuetype = 'n';
			entry->Entries[i].EntryType = TODO_CONTACTID;
		} else if (strcmp("PHONE", type) == 0) {
			valuetype = 't';
			entry->Entries[i].EntryType = TODO_PHONE;
		} else {
			PyErr_Format(PyExc_ValueError,
				     "Element %" PY_FORMAT_SIZE_T
				     "d in Values has bad type: %s", i, type);
			free(type);
			return 0;
		}
		free(type);

		switch (valuetype) {
			case 'n':
				entry->Entries[i].Number =
				    GetIntFromDict(item, "Value");
				if (entry->Entries[i].Number == INT_INVALID)
					return 0;
				break;
			case 't':
				if (!CopyStringFromDict
				    (item, "Value", GSM_PHONEBOOK_TEXT_LENGTH,
				     entry->Entries[i].Text))
					return 0;
				break;
			case 'd':
				entry->Entries[i].Date =
				    GetDateTimeFromDict(item, "Value");
				if (entry->Entries[i].Date.Year == -1)
					return 0;
				break;
		}
	}			/* end for */

	return 1;
}
Beispiel #7
0
int CalendarFromPython(PyObject * dict, GSM_CalendarEntry * entry,
		       int needs_location)
{
	PyObject *o;
	PyObject *item;
	Py_ssize_t len;
	Py_ssize_t i;
	char *t;
	char *type;
	char valuetype;

	if (!PyDict_Check(dict)) {
		PyErr_Format(PyExc_ValueError,
			     "Calendar entry is not a dictionary");
		return 0;
	}

	memset(entry, 0, sizeof(GSM_CalendarEntry));

	if (needs_location) {
		entry->Location = GetIntFromDict(dict, "Location");
		if (entry->Location == INT_INVALID)
			return 0;
	}

	t = GetCharFromDict(dict, "Type");
	if (t == NULL)
		return 0;
	entry->Type = StringToCalendarType(t);
	free(t);
	if (entry->Type == 0)
		return 0;

	o = PyDict_GetItemString(dict, "Entries");
	if (o == NULL) {
		PyErr_Format(PyExc_ValueError,
			     "Can not get string value for key Values");
		return 0;
	}

	if (!PyList_Check(o)) {
		PyErr_Format(PyExc_ValueError,
			     "Key Values doesn't contain list");
		return 0;
	}

	len = PyList_Size(o);
	if (len > GSM_CALENDAR_ENTRIES) {
		pyg_warning("Using just %i entries from list!",
			    GSM_CALENDAR_ENTRIES);
		len = GSM_CALENDAR_ENTRIES;
	}
	entry->EntriesNum = len;

	for (i = 0; i < len; i++) {
		item = PyList_GetItem(o, i);
		if (item == NULL)
			return 0;
		if (!PyDict_Check(item)) {
			PyErr_Format(PyExc_ValueError,
				     "Element %" PY_FORMAT_SIZE_T
				     "d in Values is not dictionary", i);
			return 0;
		}
		type = GetCharFromDict(item, "Type");
		if (type == NULL)
			return 0;

		if (strcmp("START_DATETIME", type) == 0) {
			valuetype = 'd';
			entry->Entries[i].EntryType = CAL_START_DATETIME;
		} else if (strcmp("END_DATETIME", type) == 0) {
			valuetype = 'd';
			entry->Entries[i].EntryType = CAL_END_DATETIME;
		} else if (strcmp("TONE_ALARM_DATETIME", type) == 0) {
			valuetype = 'd';
			entry->Entries[i].EntryType = CAL_TONE_ALARM_DATETIME;
		} else if (strcmp("SILENT_ALARM_DATETIME", type) == 0) {
			valuetype = 'd';
			entry->Entries[i].EntryType = CAL_SILENT_ALARM_DATETIME;
		} else if (strcmp("LAST_MODIFIED", type) == 0) {
			valuetype = 'd';
			entry->Entries[i].EntryType = CAL_LAST_MODIFIED;
		} else if (strcmp("REPEAT_STARTDATE", type) == 0) {
			valuetype = 'D';
			entry->Entries[i].EntryType = CAL_REPEAT_STARTDATE;
		} else if (strcmp("REPEAT_STOPDATE", type) == 0) {
			valuetype = 'D';
			entry->Entries[i].EntryType = CAL_REPEAT_STOPDATE;

		} else if (strcmp("TEXT", type) == 0) {
			valuetype = 't';
			entry->Entries[i].EntryType = CAL_TEXT;
		} else if (strcmp("DESCRIPTION", type) == 0) {
			valuetype = 't';
			entry->Entries[i].EntryType = CAL_DESCRIPTION;
		} else if (strcmp("LUID", type) == 0) {
			valuetype = 't';
			entry->Entries[i].EntryType = CAL_LUID;
		} else if (strcmp("LOCATION", type) == 0) {
			valuetype = 't';
			entry->Entries[i].EntryType = CAL_LOCATION;
		} else if (strcmp("PHONE", type) == 0) {
			valuetype = 't';
			entry->Entries[i].EntryType = CAL_PHONE;

		} else if (strcmp("PRIVATE", type) == 0) {
			valuetype = 'n';
			entry->Entries[i].EntryType = CAL_PRIVATE;
		} else if (strcmp("CONTACTID", type) == 0) {
			valuetype = 'n';
			entry->Entries[i].EntryType = CAL_CONTACTID;
		} else if (strcmp("REPEAT_DAYOFWEEK", type) == 0) {
			valuetype = 'n';
			entry->Entries[i].EntryType = CAL_REPEAT_DAYOFWEEK;
		} else if (strcmp("REPEAT_DAY", type) == 0) {
			valuetype = 'n';
			entry->Entries[i].EntryType = CAL_REPEAT_DAY;
		} else if (strcmp("REPEAT_WEEKOFMONTH", type) == 0) {
			valuetype = 'n';
			entry->Entries[i].EntryType = CAL_REPEAT_WEEKOFMONTH;
		} else if (strcmp("REPEAT_MONTH", type) == 0) {
			valuetype = 'n';
			entry->Entries[i].EntryType = CAL_REPEAT_MONTH;
		} else if (strcmp("REPEAT_FREQUENCY", type) == 0) {
			valuetype = 'n';
			entry->Entries[i].EntryType = CAL_REPEAT_FREQUENCY;
		} else if (strcmp("REPEAT_COUNT", type) == 0) {
			valuetype = 'n';
			entry->Entries[i].EntryType = CAL_REPEAT_COUNT;
		} else if (strcmp("REPEAT_DAYOFYEAR", type) == 0) {
			valuetype = 'n';
			entry->Entries[i].EntryType = CAL_REPEAT_DAYOFYEAR;

		} else {
			PyErr_Format(PyExc_ValueError,
				     "Element %" PY_FORMAT_SIZE_T
				     "d in Values has bad type: %s", i, type);
			free(type);
			return 0;
		}
		free(type);

		switch (valuetype) {
			case 'n':
				entry->Entries[i].Number =
				    GetIntFromDict(item, "Value");
				if (entry->Entries[i].Number == INT_INVALID)
					return 0;
				break;
			case 't':
				if (!CopyStringFromDict
				    (item, "Value", GSM_PHONEBOOK_TEXT_LENGTH,
				     entry->Entries[i].Text))
					return 0;
				break;
			case 'D':
				entry->Entries[i].Date =
				    GetDateFromDict(item, "Value");
				if (entry->Entries[i].Date.Year == -1)
					return 0;
				break;
			case 'd':
				entry->Entries[i].Date =
				    GetDateTimeFromDict(item, "Value");
				if (entry->Entries[i].Date.Year == -1)
					return 0;
				break;
		}

		/* Clear AddError flag */
		entry->Entries[i].AddError = ERR_NONE;
	}			/* end for */

	return 1;
}