5 A Linux/Unix GUI for Nokia mobile phones.
6 Copyright (C) 1999 Pavel Janík ml., Hugh Blemings
7 & Ján Derfiòák <ja@mail.upjs.sk>.
9 Released under the terms of the GNU GPL, see file COPYING for more details.
11 Last modification: Sun Dec 17 2000
12 Modified by Jan Derfinak
21 #include "gsm-common.h"
23 static PGconn *connIn = NULL;
24 static PGconn *connOut = NULL;
36 gint DB_ConnectInbox (const gchar * const conninfo)
38 connIn = PQconnectdb (conninfo);
40 if (PQstatus (connIn) == CONNECTION_BAD)
42 g_print ("Connection to database '%s' failed.\n", conninfo);
43 g_print ("%s", PQerrorMessage(connIn));
51 gint DB_ConnectOutbox (const gchar * const conninfo)
53 connOut = PQconnectdb (conninfo);
55 if (PQstatus (connOut) == CONNECTION_BAD)
57 g_print ("Connection to database '%s' failed.\n", conninfo);
58 g_print ("%s", PQerrorMessage(connOut));
66 gint DB_InsertSMS (const GSM_SMSMessage * const data)
71 buf = g_string_sized_new (128);
72 g_string_sprintf (buf, "INSERT INTO inbox VALUES ('%s',
73 '%02d-%02d-%02d %02d:%02d:%02d+01', 'now', '%s', 'f')",
74 data->RemoteNumber.number, data->Time.Year + 2000, data->Time.Month,
75 data->Time.Day, data->Time.Hour, data->Time.Minute,
76 data->Time.Second, data->MessageText);
77 res = PQexec(connIn, buf->str);
78 g_string_free(buf, TRUE);
79 if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
94 PGresult *res1, *res2;
97 buf = g_string_sized_new (128);
99 g_string_sprintf (buf, "BEGIN");
101 res1 = PQexec(connOut, buf->str);
104 g_string_sprintf (buf, "SELECT id, number, text FROM outbox \
105 WHERE processed='f' FOR UPDATE");
107 res1 = PQexec(connOut, buf->str);
108 if (!res1 || PQresultStatus (res1) != PGRES_TUPLES_OK)
110 g_print ("%s\n", PQcmdStatus (res1));
112 g_print ("%d: SELECT FROM command failed\n", __LINE__);
113 res1 = PQexec (connOut, "ROLLBACK TRANSACTION");
115 g_string_free (buf, TRUE);
119 for (i = 0; i < PQntuples (res1); i++)
123 sms.MessageCenter.No = 1;
124 sms.Type = SMS_Submit;
125 sms.DCS.Type = SMS_GeneralDataCoding;
126 sms.DCS.u.General.Compressed = false;
127 sms.DCS.u.General.Alphabet = SMS_DefaultAlphabet;
128 sms.DCS.u.General.Class = 0;
129 sms.Validity.VPF = SMS_RelativeFormat;
130 sms.Validity.u.Relative = 4320; /* 4320 minutes == 72 hours */
134 strncpy (sms.RemoteNumber.number, PQgetvalue (res1, i, 1), GSM_MAX_DESTINATION_LENGTH + 1);
135 sms.RemoteNumber.number[GSM_MAX_DESTINATION_LENGTH] = '\0';
136 if (sms.RemoteNumber.number[0] == '+') sms.RemoteNumber.type = SMS_International;
137 else sms.RemoteNumber.type = SMS_Unknown;
139 strncpy (sms.MessageText, PQgetvalue (res1, i, 2), GSM_MAX_SMS_LENGTH + 1);
140 sms.MessageText[GSM_MAX_SMS_LENGTH] = '\0';
143 g_print ("%s, %s\n", sms.Destination, sms.MessageText);
146 if (WriteSMS (&sms) != 0)
148 g_string_sprintf (buf, "UPDATE outbox SET processed='t' WHERE id='%s'",
149 PQgetvalue (res1, i, 0));
150 res2 = PQexec(connOut, buf->str);
151 if (!res2 || PQresultStatus (res2) != PGRES_COMMAND_OK)
153 g_print ("%s\n", PQcmdStatus (res2));
154 g_print ("%d: UPDATE command failed\n", __LINE__);
162 g_string_sprintf (buf, "COMMIT");
163 res1 = PQexec(connOut, buf->str);
165 g_string_free(buf, TRUE);