~ubuntu-branches/ubuntu/karmic/pilot-link/karmic

« back to all changes in this revision

Viewing changes to src/install-expenses.c

  • Committer: Bazaar Package Importer
  • Author(s): Ludovic Rousseau
  • Date: 2007-02-14 23:30:59 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20070214233059-znxrekb2mgjr0mgd
Tags: 0.12.2-1
* New upstream release
 - Closes: #410152 "pilot-link: udev rules need updating"
* debian/control: add Build-Depends: libbluetooth2-dev
 - add bluetooth support

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * install-expense.c: Palm expense installer
3
 
 *
4
 
 * Copyright (C) Boisy G. Pitre, year unknown - 2001 probably.
5
 
 *
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.
10
 
 *
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.
15
 
 *
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
19
 
 *
20
 
 */
21
 
 
22
 
#include <stdio.h>
23
 
 
24
 
#include "pi-source.h"
25
 
#include "pi-dlp.h"
26
 
#include "pi-expense.h"
27
 
#include "pi-userland.h"
28
 
 
29
 
int main(int argc, const char *argv[])
30
 
{
31
 
        int     db,
32
 
                sd              = -1,
33
 
                i,
34
 
                l,
35
 
                category,
36
 
                po_err          = -1,
37
 
                replace_category = 0;
38
 
 
39
 
        char
40
 
                *category_name  = NULL,
41
 
                *expenseType    = NULL,
42
 
                *paymentType    = NULL;
43
 
        size_t size;
44
 
        int found;
45
 
 
46
 
        unsigned char buf[0xffff];
47
 
        unsigned char *b;
48
 
        pi_buffer_t *appblock;
49
 
 
50
 
        struct  PilotUser User;
51
 
        struct  ExpenseAppInfo eai;
52
 
        struct  Expense theExpense;
53
 
 
54
 
        poptContext po;
55
 
 
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"},
67
 
                POPT_TABLEEND
68
 
        };
69
 
 
70
 
        /* Zero 'em out to be sure. */
71
 
        theExpense.amount=theExpense.vendor=theExpense.city=
72
 
        theExpense.attendees=theExpense.note = NULL ;
73
 
 
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");
80
 
 
81
 
        if (argc < 2) {
82
 
                poptPrintUsage(po,stderr,0);
83
 
                return 1;
84
 
        }
85
 
 
86
 
         while ((po_err = poptGetNextOpt(po)) >= 0) {
87
 
                fprintf(stderr,"   ERROR: Unhandled option %d.\n",po_err);
88
 
                return 1;
89
 
        }
90
 
 
91
 
        theExpense.type = etBus;
92
 
        found = 0;
93
 
        for (i = 0; expenseType && ExpenseTypeNames[i] != NULL; i++)
94
 
        {
95
 
                if (strcasecmp(expenseType, ExpenseTypeNames[i]) == 0)
96
 
                {
97
 
                        theExpense.type = i;
98
 
                        found = 1;
99
 
                        break;
100
 
                }
101
 
        }
102
 
        if (!found) {
103
 
                fprintf(stderr,"   WARNING: Expense type '%s' not recognized, using 'Bus Fare'.\n",expenseType);
104
 
        }
105
 
 
106
 
        theExpense.payment = epCash;
107
 
        found = 0;
108
 
        for (i = 0; paymentType && ExpensePaymentNames[i] != NULL; i++)
109
 
        {
110
 
                if (strcasecmp(paymentType, ExpensePaymentNames[i]) == 0)
111
 
                {
112
 
                        theExpense.payment = i;
113
 
                        found = 1;
114
 
                        break;
115
 
                }
116
 
        }
117
 
        if (!found) {
118
 
                fprintf(stderr,"   WARNING: Payment type '%s' not recognized, using 'Cash'.\n", paymentType);
119
 
        }
120
 
 
121
 
        if (replace_category && (!category_name)) {
122
 
                fprintf(stderr,
123
 
                        "   ERROR: category required when specifying replace\n");
124
 
                return 1;
125
 
        }
126
 
 
127
 
 
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");
131
 
                return 1;
132
 
        }
133
 
 
134
 
        sd = plu_connect();
135
 
        if (sd < 0)
136
 
                goto error;
137
 
 
138
 
        if (dlp_OpenConduit(sd) < 0)
139
 
                goto error_close;
140
 
 
141
 
        dlp_ReadUserInfo(sd, &User);
142
 
        dlp_OpenConduit(sd);
143
 
 
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");
148
 
                goto error_close;
149
 
        }
150
 
 
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);
155
 
 
156
 
        category = 0;   /* unfiled */
157
 
        if (category_name) {
158
 
                category = plu_findcategory(&eai.category,category_name,
159
 
                        PLU_CAT_CASE_INSENSITIVE | PLU_CAT_WARN_UNKNOWN);
160
 
                if (category < 0) {
161
 
                        goto error_close;
162
 
                }
163
 
 
164
 
                if (replace_category) {
165
 
                        dlp_DeleteCategory(sd, db, category);
166
 
                }
167
 
 
168
 
        }
169
 
 
170
 
                theExpense.currency     = 0;
171
 
 
172
 
                if (!theExpense.amount) {
173
 
                        theExpense.amount = "";
174
 
                }
175
 
                if (!theExpense.vendor) {
176
 
                        theExpense.vendor = "";
177
 
                }
178
 
                if (!theExpense.city) {
179
 
                        theExpense.city = "";
180
 
                }
181
 
                if (!theExpense.attendees) {
182
 
                        theExpense.attendees    = "";
183
 
                }
184
 
                if (!theExpense.note) {
185
 
                        theExpense.note = "";
186
 
                }
187
 
 
188
 
                b = buf;
189
 
 
190
 
                /* Date */
191
 
                *(b++)  = 0xc3;
192
 
                *(b++)  = 0x45;
193
 
 
194
 
                *(b++)  = theExpense.type;
195
 
                *(b++)  = theExpense.payment;
196
 
                *(b++)  = theExpense.currency;
197
 
                *(b++)  = 0x00;
198
 
 
199
 
                strcpy(b, theExpense.amount);
200
 
                b += strlen(theExpense.amount) + 1;
201
 
 
202
 
                strcpy(b, theExpense.vendor);
203
 
                b += strlen(theExpense.vendor) + 1;
204
 
 
205
 
                strcpy(b, theExpense.city);
206
 
                b += strlen(theExpense.city) + 1;
207
 
 
208
 
                strcpy(b, theExpense.attendees);
209
 
                b += strlen(theExpense.attendees) + 1;
210
 
 
211
 
                strcpy(b, theExpense.note);
212
 
                b += strlen(theExpense.note) + 1;
213
 
 
214
 
                size = b - buf;
215
 
                dlp_WriteRecord(sd, (unsigned char)db, 0, 0, category,
216
 
                                (unsigned char *)buf, size, 0);
217
 
 
218
 
        /* Close the database */
219
 
        dlp_CloseDB(sd, db);
220
 
 
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);
226
 
 
227
 
        dlp_AddSyncLogEntry(sd, "Wrote expense entry to Palm.\n");
228
 
        dlp_EndOfSync(sd, 0);
229
 
        pi_close(sd);
230
 
        poptFreeContext(po);
231
 
        return 0;
232
 
 
233
 
error_close:
234
 
        pi_close(sd);
235
 
 
236
 
error:
237
 
        return -1;
238
 
}