2
* install-expense.c: Palm expense installer
4
* Copyright (C) Boisy G. Pitre, year unknown - 2001 probably.
6
* This program is free software; you can redistribute it and/or modify it
7
* under the terms of the GNU General Public License as published by the
8
* Free Software Foundation; either version 2 of the License, or (at your
9
* option) any later version.
11
* This program is distributed in the hope that it will be useful, but
12
* WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
14
* Public License for more details.
16
* You should have received a copy of the GNU General Public License along
17
* with this program; if not, write to the Free Software Foundation, Inc.,
18
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24
#include "pi-source.h"
26
#include "pi-expense.h"
27
#include "pi-userland.h"
29
int main(int argc, const char *argv[])
40
*category_name = NULL,
46
unsigned char buf[0xffff];
48
pi_buffer_t *appblock;
50
struct PilotUser User;
51
struct ExpenseAppInfo eai;
52
struct Expense theExpense;
56
struct poptOption options[] = {
57
USERLAND_RESERVED_OPTIONS
58
{"ptype", 't', POPT_ARG_STRING, &paymentType, 0,"Payment type (Cash, Check, etc.)"},
59
{"etype", 'e', POPT_ARG_STRING, &expenseType, 0, "Expense type (Airfare, Hotel, etc.)"},
60
{"amount", 'a', POPT_ARG_STRING, &theExpense.amount, 0, "Payment amount"},
61
{"vendor", 'V', POPT_ARG_STRING, &theExpense.vendor, 0, "Expense vendor name (Joe's Restaurant)"},
62
{"city", 'i', POPT_ARG_STRING, &theExpense.city, 0, "Location/city for this expense entry"},
63
{"guests", 'g', POPT_ARG_STRING, &theExpense.attendees, 0, "Number of guests for this expense entry","NUMBER"},
64
{"note", 'n', POPT_ARG_STRING, &theExpense.note, 0, "Notes for this expense entry"},
65
{"category", 'c', POPT_ARG_STRING, &category_name, 0, "Install entry into this category", "CATEGORY" },
66
{"replace", 0, POPT_ARG_VAL, &replace_category, 1, "Replace all entries in category by this one"},
70
/* Zero 'em out to be sure. */
71
theExpense.amount=theExpense.vendor=theExpense.city=
72
theExpense.attendees=theExpense.note = NULL ;
74
po = poptGetContext("install-expenses", argc, argv, options, 0);
75
poptSetOtherOptionHelp(po,"\n\n"
76
" Install Expense application entries to your Palm device\n\n"
77
" Example arguments:\n"
78
" %s -p /dev/pilot -c Unfiled -t Cash -e Meals -a 10.00 -V McDonalds \n"
79
" -g 21 -l \"San Francisco\" -N \"This is a note\"\n\n");
82
poptPrintUsage(po,stderr,0);
86
while ((po_err = poptGetNextOpt(po)) >= 0) {
87
fprintf(stderr," ERROR: Unhandled option %d.\n",po_err);
91
theExpense.type = etBus;
93
for (i = 0; expenseType && ExpenseTypeNames[i] != NULL; i++)
95
if (strcasecmp(expenseType, ExpenseTypeNames[i]) == 0)
103
fprintf(stderr," WARNING: Expense type '%s' not recognized, using 'Bus Fare'.\n",expenseType);
106
theExpense.payment = epCash;
108
for (i = 0; paymentType && ExpensePaymentNames[i] != NULL; i++)
110
if (strcasecmp(paymentType, ExpensePaymentNames[i]) == 0)
112
theExpense.payment = i;
118
fprintf(stderr," WARNING: Payment type '%s' not recognized, using 'Cash'.\n", paymentType);
121
if (replace_category && (!category_name)) {
123
" ERROR: category required when specifying replace\n");
128
if (!(theExpense.amount || theExpense.vendor || theExpense.city ||
129
theExpense.attendees ||theExpense.note)) {
130
fprintf(stderr," ERROR: Must specify at least one of amount, vendor, city, attendees or note.\n");
138
if (dlp_OpenConduit(sd) < 0)
141
dlp_ReadUserInfo(sd, &User);
144
/* Open the Expense's database, store access handle in db */
145
if (dlp_OpenDB(sd, 0, 0x80 | 0x40, Expense_DB, &db) < 0) {
146
fprintf(stderr," ERROR: Unable to open ExpenseDB on Palm.");
147
dlp_AddSyncLogEntry(sd, "Unable to open ExpenseDB.\n");
151
appblock = pi_buffer_new(0xffff);
152
l = dlp_ReadAppBlock(sd, db, 0, 0xffff, appblock);
153
unpack_ExpenseAppInfo(&eai, appblock->data, l);
154
pi_buffer_free(appblock);
156
category = 0; /* unfiled */
158
category = plu_findcategory(&eai.category,category_name,
159
PLU_CAT_CASE_INSENSITIVE | PLU_CAT_WARN_UNKNOWN);
164
if (replace_category) {
165
dlp_DeleteCategory(sd, db, category);
170
theExpense.currency = 0;
172
if (!theExpense.amount) {
173
theExpense.amount = "";
175
if (!theExpense.vendor) {
176
theExpense.vendor = "";
178
if (!theExpense.city) {
179
theExpense.city = "";
181
if (!theExpense.attendees) {
182
theExpense.attendees = "";
184
if (!theExpense.note) {
185
theExpense.note = "";
194
*(b++) = theExpense.type;
195
*(b++) = theExpense.payment;
196
*(b++) = theExpense.currency;
199
strcpy(b, theExpense.amount);
200
b += strlen(theExpense.amount) + 1;
202
strcpy(b, theExpense.vendor);
203
b += strlen(theExpense.vendor) + 1;
205
strcpy(b, theExpense.city);
206
b += strlen(theExpense.city) + 1;
208
strcpy(b, theExpense.attendees);
209
b += strlen(theExpense.attendees) + 1;
211
strcpy(b, theExpense.note);
212
b += strlen(theExpense.note) + 1;
215
dlp_WriteRecord(sd, (unsigned char)db, 0, 0, category,
216
(unsigned char *)buf, size, 0);
218
/* Close the database */
221
/* Tell the user who it is, with a different PC id. */
222
User.lastSyncPC = 0x00010000;
223
User.successfulSyncDate = time(NULL);
224
User.lastSyncDate = User.successfulSyncDate;
225
dlp_WriteUserInfo(sd, &User);
227
dlp_AddSyncLogEntry(sd, "Wrote expense entry to Palm.\n");
228
dlp_EndOfSync(sd, 0);