Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

aqbanking / src / libs / aqbanking / banking_imex.c @ eb50ded7

History | View | Annotate | Download (32.5 KB)

1
/***************************************************************************
2
 begin       : Mon Mar 01 2004
3
 copyright   : (C) 2018 by Martin Preuss
4
 email       : martin@libchipcard.de
5

6
 ***************************************************************************
7
 * This file is part of the project "AqBanking".                           *
8
 * Please see toplevel file COPYING of that project for license details.   *
9
 ***************************************************************************/
10

    
11
/* This file is included by banking.c */
12

    
13
#include "aqbanking/backendsupport/swiftdescr.h"
14

    
15

    
16
#ifdef AQBANKING_WITH_PLUGIN_IMEXPORTER_CSV
17
# include "src/libs/plugins/imexporters/csv/csv.h"
18
#endif
19

    
20
#ifdef AQBANKING_WITH_PLUGIN_IMEXPORTER_ERI2
21
# include "src/libs/plugins/imexporters/eri2/eri2.h"
22
#endif
23

    
24
#ifdef AQBANKING_WITH_PLUGIN_IMEXPORTER_OFX
25
# include "src/libs/plugins/imexporters/ofx/ofx.h"
26
#endif
27

    
28
#ifdef AQBANKING_WITH_PLUGIN_IMEXPORTER_OPENHBCI1
29
# include "src/libs/plugins/imexporters/openhbci1/openhbci1.h"
30
#endif
31

    
32
#ifdef AQBANKING_WITH_PLUGIN_IMEXPORTER_SWIFT
33
# include "src/libs/plugins/imexporters/swift/swift.h"
34
#endif
35

    
36
#ifdef AQBANKING_WITH_PLUGIN_IMEXPORTER_XMLDB
37
# include "src/libs/plugins/imexporters/xmldb/xmldb.h"
38
#endif
39

    
40
#ifdef AQBANKING_WITH_PLUGIN_IMEXPORTER_YELLOWNET
41
# include "src/libs/plugins/imexporters/yellownet/yellownet.h"
42
#endif
43

    
44
#ifdef AQBANKING_WITH_PLUGIN_IMEXPORTER_SEPA
45
# include "src/libs/plugins/imexporters/sepa/sepa.h"
46
#endif
47

    
48
#ifdef AQBANKING_WITH_PLUGIN_IMEXPORTER_CTXFILE
49
# include "src/libs/plugins/imexporters/ctxfile/ctxfile.h"
50
#endif
51

    
52
#ifdef AQBANKING_WITH_PLUGIN_IMEXPORTER_Q43
53
# include "src/libs/plugins/imexporters/q43/q43.h"
54
#endif
55

    
56
#ifdef AQBANKING_WITH_PLUGIN_IMEXPORTER_CAMT
57
# include "src/libs/plugins/imexporters/camt/camt.h"
58
#endif
59

    
60

    
61
#ifdef AQBANKING_WITH_PLUGIN_IMEXPORTER_XML
62
# include "src/libs/plugins/imexporters/xml/xml.h"
63
#endif
64

    
65

    
66

    
67

    
68
AB_IMEXPORTER *AB_Banking__CreateImExporterPlugin(AB_BANKING *ab, const char *modname)
69
{
70
  if (modname && *modname) {
71
#ifdef AQBANKING_WITH_PLUGIN_IMEXPORTER_CSV
72
    if (strcasecmp(modname, "csv")==0)
73
      return AB_ImExporterCSV_new(ab);
74
#endif
75

    
76
#ifdef AQBANKING_WITH_PLUGIN_IMEXPORTER_ERI2
77
    if (strcasecmp(modname, "eri2")==0)
78
      return AB_ImExporterERI2_new(ab);
79
#endif
80

    
81
#ifdef AQBANKING_WITH_PLUGIN_IMEXPORTER_OFX
82
    if (strcasecmp(modname, "ofx")==0)
83
      return AB_ImExporterOFX_new(ab);
84
#endif
85

    
86
#ifdef AQBANKING_WITH_PLUGIN_IMEXPORTER_OPENHBCI1
87
    if (strcasecmp(modname, "ofx")==0)
88
      return AB_ImExporterOpenHBCI1_new(ab);
89
#endif
90

    
91
#ifdef AQBANKING_WITH_PLUGIN_IMEXPORTER_SWIFT
92
    if (strcasecmp(modname, "swift")==0)
93
      return AB_ImExporterSWIFT_new(ab);
94
#endif
95

    
96
#ifdef AQBANKING_WITH_PLUGIN_IMEXPORTER_XMLDB
97
    if (strcasecmp(modname, "xmldb")==0)
98
      return AB_ImExporterXMLDB_new(ab);
99
#endif
100

    
101
#ifdef AQBANKING_WITH_PLUGIN_IMEXPORTER_YELLOWNET
102
    if (strcasecmp(modname, "yellownet")==0)
103
      return AB_ImExporterYellowNet_new(ab);
104
#endif
105

    
106
#ifdef AQBANKING_WITH_PLUGIN_IMEXPORTER_SEPA
107
    if (strcasecmp(modname, "sepa")==0)
108
      return AB_ImExporterSEPA_new(ab);
109
#endif
110

    
111
#ifdef AQBANKING_WITH_PLUGIN_IMEXPORTER_CTXFILE
112
    if (strcasecmp(modname, "ctxfile")==0)
113
      return AB_ImExporterCtxFile_new(ab);
114
#endif
115

    
116
#ifdef AQBANKING_WITH_PLUGIN_IMEXPORTER_Q43
117
    if (strcasecmp(modname, "q43")==0)
118
      return AB_ImExporterQ43_new(ab);
119
#endif
120

    
121
#ifdef AQBANKING_WITH_PLUGIN_IMEXPORTER_CAMT
122
    if (strcasecmp(modname, "camt")==0)
123
      return AB_ImExporterCAMT_new(ab);
124
#endif
125

    
126
#ifdef AQBANKING_WITH_PLUGIN_IMEXPORTER_XML
127
    if (strcasecmp(modname, "xml")==0)
128
      return AB_ImExporterXML_new(ab);
129
#endif
130

    
131
    DBG_ERROR(AQBANKING_LOGDOMAIN, "Plugin [%s] not compiled-in", modname);
132
  }
133

    
134
  return NULL;
135
}
136

    
137

    
138

    
139
AB_IMEXPORTER *AB_Banking_FindImExporter(AB_BANKING *ab, const char *name)
140
{
141
  AB_IMEXPORTER *ie;
142

    
143
  assert(ab);
144
  assert(name);
145
  ie=AB_ImExporter_List_First(ab_imexporters);
146
  while (ie) {
147
    if (strcasecmp(AB_ImExporter_GetName(ie), name)==0)
148
      break;
149
    ie=AB_ImExporter_List_Next(ie);
150
  } /* while */
151

    
152
  return ie;
153
}
154

    
155

    
156

    
157
AB_IMEXPORTER *AB_Banking_GetImExporter(AB_BANKING *ab, const char *name)
158
{
159
  AB_IMEXPORTER *ie;
160

    
161
  assert(ab);
162
  assert(name);
163

    
164
  ie=AB_Banking_FindImExporter(ab, name);
165
  if (ie)
166
    return ie;
167
  ie=AB_Banking__CreateImExporterPlugin(ab, name);
168
  if (ie)
169
    AB_ImExporter_List_Add(ie, ab_imexporters);
170

    
171
  return ie;
172
}
173

    
174

    
175

    
176
void AB_Banking_FillGapsInTransaction(AB_BANKING *ab, AB_ACCOUNT *a, AB_TRANSACTION *t)
177
{
178
  assert(t);
179

    
180
  if (a) {
181
    const char *s;
182

    
183
    /* local account */
184
    s=AB_Account_GetCountry(a);
185
    if (!s || !*s)
186
      s="de";
187
    AB_Transaction_SetLocalCountry(t, s);
188
    AB_Transaction_SetRemoteCountry(t, s);
189

    
190
    s=AB_Account_GetBankCode(a);
191
    if (s && *s)
192
      AB_Transaction_SetLocalBankCode(t, s);
193

    
194
    s=AB_Account_GetAccountNumber(a);
195
    if (s && *s)
196
      AB_Transaction_SetLocalAccountNumber(t, s);
197

    
198
    s=AB_Account_GetOwnerName(a);
199
    if (s && *s)
200
      AB_Transaction_SetLocalName(t, s);
201

    
202
    s=AB_Account_GetBic(a);
203
    if (s && *s)
204
      AB_Transaction_SetLocalBic(t, s);
205

    
206
    s=AB_Account_GetIban(a);
207
    if (s && *s)
208
      AB_Transaction_SetLocalIban(t, s);
209
  }
210
}
211

    
212

    
213

    
214
int AB_Banking_GetEditImExporterProfileDialog(AB_BANKING *ab,
215
                                              const char *imExporterName,
216
                                              GWEN_DB_NODE *dbProfile,
217
                                              const char *testFileName,
218
                                              GWEN_DIALOG **pDlg)
219
{
220
  AB_IMEXPORTER *ie;
221
  int rv;
222

    
223
  ie=AB_Banking_GetImExporter(ab, imExporterName);
224
  if (ie==NULL) {
225
    DBG_INFO(AQBANKING_LOGDOMAIN, "here");
226
    return GWEN_ERROR_NO_DATA;
227
  }
228

    
229
  if (AB_ImExporter_GetFlags(ie) & AB_IMEXPORTER_FLAGS_GETPROFILEEDITOR_SUPPORTED) {
230
    GWEN_DIALOG *dlg=NULL;
231

    
232
    rv=AB_ImExporter_GetEditProfileDialog(ie, dbProfile, testFileName, &dlg);
233
    if (rv<0) {
234
      DBG_INFO(AQBANKING_LOGDOMAIN, "here (%d)", rv);
235
      return rv;
236
    }
237

    
238
    *pDlg=dlg;
239
    return 0;
240
  }
241
  else {
242
    DBG_ERROR(AQBANKING_LOGDOMAIN, "EditProfileDialog not supported by imExporter \"%s\"", imExporterName);
243
    return GWEN_ERROR_NOT_SUPPORTED;
244
  }
245
}
246

    
247

    
248

    
249
int AB_Banking_Import(AB_BANKING *ab,
250
                      const char *importerName,
251
                      AB_IMEXPORTER_CONTEXT *ctx,
252
                      GWEN_SYNCIO *sio,
253
                      GWEN_DB_NODE *dbProfile)
254
{
255
  AB_IMEXPORTER *ie;
256
  int rv;
257

    
258
  ie=AB_Banking_GetImExporter(ab, importerName);
259
  if (ie==NULL) {
260
    DBG_INFO(AQBANKING_LOGDOMAIN, "here");
261
    return GWEN_ERROR_NO_DATA;
262
  }
263

    
264
  rv=AB_ImExporter_Import(ie, ctx, sio, dbProfile);
265
  if (rv<0) {
266
    DBG_INFO(AQBANKING_LOGDOMAIN, "here (%d)", rv);
267
    return rv;
268
  }
269

    
270
  return 0;
271
}
272

    
273

    
274

    
275
int AB_Banking_Export(AB_BANKING *ab,
276
                      const char *exporterName,
277
                      AB_IMEXPORTER_CONTEXT *ctx,
278
                      GWEN_SYNCIO *sio,
279
                      GWEN_DB_NODE *dbProfile)
280
{
281
  AB_IMEXPORTER *ie;
282
  int rv;
283

    
284
  ie=AB_Banking_GetImExporter(ab, exporterName);
285
  if (ie==NULL) {
286
    DBG_INFO(AQBANKING_LOGDOMAIN, "here");
287
    return GWEN_ERROR_NO_DATA;
288
  }
289

    
290
  rv=AB_ImExporter_Export(ie, ctx, sio, dbProfile);
291
  if (rv<0) {
292
    DBG_INFO(AQBANKING_LOGDOMAIN, "here (%d)", rv);
293
    return rv;
294
  }
295

    
296
  return 0;
297
}
298

    
299

    
300

    
301
int AB_Banking_ImportLoadProfile(AB_BANKING *ab,
302
                                 const char *importerName,
303
                                 AB_IMEXPORTER_CONTEXT *ctx,
304
                                 GWEN_SYNCIO *sio,
305
                                 const char *profileName,
306
                                 const char *profileFile)
307
{
308
  GWEN_DB_NODE *dbProfile;
309
  int rv;
310

    
311
  if (profileName && *profileName)
312
    dbProfile=AB_Banking_GetImExporterProfile(ab, importerName, profileName);
313
  else
314
    dbProfile=GWEN_DB_Group_new("profile");
315
  if (dbProfile==NULL) {
316
    DBG_ERROR(AQBANKING_LOGDOMAIN, "Profile [%s] not found",
317
              profileName?profileName:"(null)");
318
    return GWEN_ERROR_NO_DATA;
319
  }
320

    
321
  rv=AB_Banking_Import(ab, importerName, ctx, sio, dbProfile);
322
  if (rv<0) {
323
    DBG_INFO(AQBANKING_LOGDOMAIN, "here (%d)", rv);
324
    GWEN_DB_Group_free(dbProfile);
325
    return rv;
326
  }
327
  GWEN_DB_Group_free(dbProfile);
328

    
329
  return 0;
330
}
331

    
332

    
333

    
334
int AB_Banking_ExportLoadProfile(AB_BANKING *ab,
335
                                 const char *exporterName,
336
                                 AB_IMEXPORTER_CONTEXT *ctx,
337
                                 GWEN_SYNCIO *sio,
338
                                 const char *profileName,
339
                                 const char *profileFile)
340
{
341
  GWEN_DB_NODE *dbProfile;
342
  int rv;
343

    
344
  if (profileName && *profileName)
345
    dbProfile=AB_Banking_GetImExporterProfile(ab, exporterName, profileName);
346
  else
347
    dbProfile=GWEN_DB_Group_new("profile");
348
  if (dbProfile==NULL) {
349
    DBG_ERROR(AQBANKING_LOGDOMAIN, "Profile [%s] not found",
350
              profileName?profileName:"(null)");
351
    return GWEN_ERROR_NO_DATA;
352
  }
353

    
354
  rv=AB_Banking_Export(ab, exporterName, ctx, sio, dbProfile);
355
  if (rv<0) {
356
    DBG_INFO(AQBANKING_LOGDOMAIN, "here (%d)", rv);
357
    GWEN_DB_Group_free(dbProfile);
358
    return rv;
359
  }
360
  GWEN_DB_Group_free(dbProfile);
361

    
362
  return 0;
363
}
364

    
365

    
366

    
367

    
368
int AB_Banking_ImportFromFile(AB_BANKING *ab,
369
                              const char *importerName,
370
                              AB_IMEXPORTER_CONTEXT *ctx,
371
                              const char *inputFileName,
372
                              GWEN_DB_NODE *dbProfile)
373
{
374
  GWEN_SYNCIO *sio;
375
  int rv;
376

    
377
  if (inputFileName) {
378
    sio=GWEN_SyncIo_File_new(inputFileName, GWEN_SyncIo_File_CreationMode_OpenExisting);
379
    GWEN_SyncIo_AddFlags(sio, GWEN_SYNCIO_FILE_FLAGS_READ);
380
    rv=GWEN_SyncIo_Connect(sio);
381
    if (rv<0) {
382
      DBG_INFO(AQBANKING_LOGDOMAIN, "here (%d)", rv);
383
      GWEN_SyncIo_free(sio);
384
      return rv;
385
    }
386
  }
387
  else {
388
    sio=GWEN_SyncIo_File_fromStdin();
389
    GWEN_SyncIo_AddFlags(sio, GWEN_SYNCIO_FLAGS_DONTCLOSE);
390
  }
391

    
392
  rv=AB_Banking_Import(ab, importerName, ctx, sio, dbProfile);
393
  if (rv<0) {
394
    DBG_INFO(AQBANKING_LOGDOMAIN, "here (%d)", rv);
395
    GWEN_SyncIo_Disconnect(sio);
396
    GWEN_SyncIo_free(sio);
397
    return rv;
398
  }
399

    
400
  GWEN_SyncIo_Disconnect(sio);
401
  GWEN_SyncIo_free(sio);
402

    
403
  return 0;
404
}
405

    
406

    
407

    
408
int AB_Banking_ExportToFile(AB_BANKING *ab,
409
                            const char *exporterName,
410
                            AB_IMEXPORTER_CONTEXT *ctx,
411
                            const char *outputFileName,
412
                            GWEN_DB_NODE *dbProfile)
413
{
414
  GWEN_SYNCIO *sio;
415
  int rv;
416

    
417
  if (outputFileName) {
418
    sio=GWEN_SyncIo_File_new(outputFileName, GWEN_SyncIo_File_CreationMode_CreateAlways);
419
    GWEN_SyncIo_AddFlags(sio,
420
                         GWEN_SYNCIO_FILE_FLAGS_READ |
421
                         GWEN_SYNCIO_FILE_FLAGS_WRITE |
422
                         GWEN_SYNCIO_FILE_FLAGS_UREAD |
423
                         GWEN_SYNCIO_FILE_FLAGS_UWRITE |
424
                         GWEN_SYNCIO_FILE_FLAGS_GREAD |
425
                         GWEN_SYNCIO_FILE_FLAGS_GWRITE);
426
    rv=GWEN_SyncIo_Connect(sio);
427
    if (rv<0) {
428
      DBG_WARN(AQBANKING_LOGDOMAIN, "Failed to Connect() syncio (%d)", rv);
429
      GWEN_SyncIo_free(sio);
430
      return rv;
431
    }
432
  }
433
  else {
434
    sio=GWEN_SyncIo_File_fromStdout();
435
    GWEN_SyncIo_AddFlags(sio, GWEN_SYNCIO_FLAGS_DONTCLOSE);
436
  }
437

    
438
  rv=AB_Banking_Export(ab, exporterName, ctx, sio, dbProfile);
439
  if (rv<0) {
440
    DBG_WARN(AQBANKING_LOGDOMAIN, "Failed to export (%d)", rv);
441
    GWEN_SyncIo_Disconnect(sio);
442
    GWEN_SyncIo_free(sio);
443
    return rv;
444
  }
445

    
446
  rv=GWEN_SyncIo_Disconnect(sio);
447
  if (rv<0) {
448
    DBG_WARN(AQBANKING_LOGDOMAIN, "Failed to Disconnect() syncio (%d)", rv);
449
    GWEN_SyncIo_free(sio);
450
    return rv;
451
  }
452
  GWEN_SyncIo_free(sio);
453

    
454
  return 0;
455
}
456

    
457

    
458

    
459
int AB_Banking_ImportFromFileLoadProfile(AB_BANKING *ab,
460
                                         const char *importerName,
461
                                         AB_IMEXPORTER_CONTEXT *ctx,
462
                                         const char *profileName,
463
                                         const char *profileFile,
464
                                         const char *inputFileName)
465
{
466
  GWEN_DB_NODE *dbProfile;
467
  int rv;
468

    
469
  if (profileName && *profileName)
470
    dbProfile=AB_Banking_GetImExporterProfile(ab, importerName, profileName);
471
  else
472
    dbProfile=GWEN_DB_Group_new("profile");
473
  if (dbProfile==NULL) {
474
    DBG_ERROR(AQBANKING_LOGDOMAIN, "Profile [%s] not found",
475
              profileName?profileName:"(null)");
476
    return GWEN_ERROR_NO_DATA;
477
  }
478

    
479
  rv=AB_Banking_ImportFromFile(ab, importerName, ctx, inputFileName, dbProfile);
480
  if (rv<0) {
481
    DBG_INFO(AQBANKING_LOGDOMAIN, "here (%d)", rv);
482
    GWEN_DB_Group_free(dbProfile);
483
    return rv;
484
  }
485
  GWEN_DB_Group_free(dbProfile);
486

    
487
  return 0;
488
}
489

    
490

    
491

    
492
int AB_Banking_ExportToFileLoadProfile(AB_BANKING *ab,
493
                                       const char *exporterName,
494
                                       AB_IMEXPORTER_CONTEXT *ctx,
495
                                       const char *outputFileName,
496
                                       const char *profileName,
497
                                       const char *profileFile)
498
{
499
  GWEN_DB_NODE *dbProfile;
500
  int rv;
501

    
502
  if (profileName && *profileName)
503
    dbProfile=AB_Banking_GetImExporterProfile(ab, exporterName, profileName);
504
  else
505
    dbProfile=GWEN_DB_Group_new("profile");
506
  if (dbProfile==NULL) {
507
    DBG_ERROR(AQBANKING_LOGDOMAIN, "Profile [%s] not found",
508
              profileName?profileName:"(null)");
509
    return GWEN_ERROR_NO_DATA;
510
  }
511

    
512
  rv=AB_Banking_ExportToFile(ab, exporterName, ctx, outputFileName, dbProfile);
513
  if (rv<0) {
514
    DBG_INFO(AQBANKING_LOGDOMAIN, "here (%d)", rv);
515
    GWEN_DB_Group_free(dbProfile);
516
    return rv;
517
  }
518
  GWEN_DB_Group_free(dbProfile);
519

    
520
  return 0;
521
}
522

    
523

    
524

    
525
int AB_Banking_ImportFromBuffer(AB_BANKING *ab,
526
                                const char *importerName,
527
                                AB_IMEXPORTER_CONTEXT *ctx,
528
                                const uint8_t *dataPtr,
529
                                uint32_t dataLen,
530
                                GWEN_DB_NODE *dbProfile)
531
{
532
  GWEN_BUFFER *buf;
533
  GWEN_SYNCIO *sio;
534
  int rv;
535

    
536
  buf=GWEN_Buffer_new((char *) dataPtr, dataLen, dataLen, 0);
537
  GWEN_Buffer_SetMode(buf, GWEN_BUFFER_MODE_READONLY);
538

    
539
  sio=GWEN_SyncIo_Memory_new(buf, 0);
540

    
541
  rv=AB_Banking_Import(ab, importerName, ctx, sio, dbProfile);
542
  if (rv<0) {
543
    DBG_INFO(AQBANKING_LOGDOMAIN, "here (%d)", rv);
544
    GWEN_SyncIo_free(sio);
545
    GWEN_Buffer_free(buf);
546
    return rv;
547
  }
548

    
549
  GWEN_SyncIo_free(sio);
550
  GWEN_Buffer_free(buf);
551

    
552
  return 0;
553
}
554

    
555

    
556

    
557
int AB_Banking_ExportToBuffer(AB_BANKING *ab,
558
                              const char *exporterName,
559
                              AB_IMEXPORTER_CONTEXT *ctx,
560
                              GWEN_BUFFER *outputBuffer,
561
                              GWEN_DB_NODE *dbProfile)
562
{
563
  int rv;
564
  GWEN_SYNCIO *sio;
565

    
566
  sio=GWEN_SyncIo_Memory_new(outputBuffer, 0);
567
  rv=AB_Banking_Export(ab, exporterName, ctx, sio, dbProfile);
568
  if (rv<0) {
569
    DBG_INFO(AQBANKING_LOGDOMAIN, "here (%d)", rv);
570
    GWEN_SyncIo_free(sio);
571
    return rv;
572
  }
573
  GWEN_SyncIo_free(sio);
574

    
575
  return 0;
576
}
577

    
578

    
579

    
580
int AB_Banking_ImportFromBufferLoadProfile(AB_BANKING *ab,
581
                                           const char *importerName,
582
                                           AB_IMEXPORTER_CONTEXT *ctx,
583
                                           const char *profileName,
584
                                           const char *profileFile,
585
                                           const uint8_t *dataPtr,
586
                                           uint32_t dataLen)
587
{
588
  GWEN_DB_NODE *dbProfile;
589
  int rv;
590

    
591
  if (profileName && *profileName)
592
    dbProfile=AB_Banking_GetImExporterProfile(ab, importerName, profileName);
593
  else
594
    dbProfile=GWEN_DB_Group_new("profile");
595
  if (dbProfile==NULL) {
596
    DBG_ERROR(AQBANKING_LOGDOMAIN, "Profile [%s] not found",
597
              profileName?profileName:"(null)");
598
    return GWEN_ERROR_NO_DATA;
599
  }
600

    
601
  rv=AB_Banking_ImportFromBuffer(ab, importerName, ctx, dataPtr, dataLen, dbProfile);
602
  if (rv<0) {
603
    DBG_INFO(AQBANKING_LOGDOMAIN, "here (%d)", rv);
604
    GWEN_DB_Group_free(dbProfile);
605
    return rv;
606
  }
607
  GWEN_DB_Group_free(dbProfile);
608

    
609
  return 0;
610
}
611

    
612

    
613

    
614
int AB_Banking_ExportToBufferLoadProfile(AB_BANKING *ab,
615
                                         const char *exporterName,
616
                                         AB_IMEXPORTER_CONTEXT *ctx,
617
                                         GWEN_BUFFER *outputBuffer,
618
                                         const char *profileName,
619
                                         const char *profileFile)
620
{
621
  GWEN_DB_NODE *dbProfile;
622
  int rv;
623

    
624
  if (profileName && *profileName)
625
    dbProfile=AB_Banking_GetImExporterProfile(ab, exporterName, profileName);
626
  else
627
    dbProfile=GWEN_DB_Group_new("profile");
628
  if (dbProfile==NULL) {
629
    DBG_ERROR(AQBANKING_LOGDOMAIN, "Profile [%s] not found",
630
              profileName?profileName:"(null)");
631
    return GWEN_ERROR_NO_DATA;
632
  }
633

    
634
  rv=AB_Banking_ExportToBuffer(ab, exporterName, ctx, outputBuffer, dbProfile);
635
  if (rv<0) {
636
    DBG_INFO(AQBANKING_LOGDOMAIN, "here (%d)", rv);
637
    GWEN_DB_Group_free(dbProfile);
638
    return rv;
639
  }
640
  GWEN_DB_Group_free(dbProfile);
641

    
642
  return 0;
643
}
644

    
645

    
646

    
647
GWEN_PLUGIN_DESCRIPTION_LIST2 *AB_Banking_GetImExporterDescrs(AB_BANKING *ab)
648
{
649
  assert(ab);
650
  if (ab_pluginManagerImExporter) {
651
    GWEN_PLUGIN_DESCRIPTION_LIST2 *l;
652

    
653
    l=GWEN_PluginManager_GetPluginDescrs(ab_pluginManagerImExporter);
654
    return l;
655
  }
656
  else {
657
    DBG_ERROR(AQBANKING_LOGDOMAIN, "No plugin manager for imexporters");
658
    return NULL;
659
  }
660
}
661

    
662

    
663

    
664
int AB_Banking__ReadImExporterProfiles(AB_BANKING *ab,
665
                                       const char *path,
666
                                       GWEN_DB_NODE *db,
667
                                       int isGlobal)
668
{
669
  GWEN_DIRECTORY *d;
670
  GWEN_BUFFER *nbuf;
671
  char nbuffer[64];
672
  unsigned int pathLen;
673

    
674
  if (!path)
675
    path="";
676

    
677
  /* create path */
678
  nbuf=GWEN_Buffer_new(0, 256, 0, 1);
679
  GWEN_Buffer_AppendString(nbuf, path);
680
  pathLen=GWEN_Buffer_GetUsedBytes(nbuf);
681

    
682
  d=GWEN_Directory_new();
683
  if (GWEN_Directory_Open(d, GWEN_Buffer_GetStart(nbuf))) {
684
    DBG_INFO(AQBANKING_LOGDOMAIN,
685
             "Path \"%s\" is not available",
686
             GWEN_Buffer_GetStart(nbuf));
687
    GWEN_Buffer_free(nbuf);
688
    GWEN_Directory_free(d);
689
    return GWEN_ERROR_NOT_FOUND;
690
  }
691

    
692
  while (!GWEN_Directory_Read(d,
693
                              nbuffer,
694
                              sizeof(nbuffer))) {
695
    if (strcmp(nbuffer, ".") &&
696
        strcmp(nbuffer, "..")) {
697
      int nlen;
698

    
699
      nlen=strlen(nbuffer);
700
      if (nlen>4) {
701
        if (strcasecmp(nbuffer+nlen-5, ".conf")==0) {
702
          struct stat st;
703

    
704
          GWEN_Buffer_Crop(nbuf, 0, pathLen);
705
          GWEN_Buffer_SetPos(nbuf, pathLen);
706
          GWEN_Buffer_AppendString(nbuf, DIRSEP);
707
          GWEN_Buffer_AppendString(nbuf, nbuffer);
708

    
709
          if (stat(GWEN_Buffer_GetStart(nbuf), &st)) {
710
            DBG_ERROR(AQBANKING_LOGDOMAIN, "stat(%s): %s",
711
                      GWEN_Buffer_GetStart(nbuf),
712
                      strerror(errno));
713
          }
714
          else {
715
            if (!S_ISDIR(st.st_mode)) {
716
              GWEN_DB_NODE *dbT;
717

    
718
              dbT=GWEN_DB_Group_new("profile");
719
              if (GWEN_DB_ReadFile(dbT,
720
                                   GWEN_Buffer_GetStart(nbuf),
721
                                   GWEN_DB_FLAGS_DEFAULT |
722
                                   GWEN_PATH_FLAGS_CREATE_GROUP)) {
723
                DBG_ERROR(AQBANKING_LOGDOMAIN,
724
                          "Could not read file \"%s\"",
725
                          GWEN_Buffer_GetStart(nbuf));
726
              }
727
              else {
728
                const char *s;
729

    
730
                s=GWEN_DB_GetCharValue(dbT, "name", 0, 0);
731
                if (!s) {
732
                  DBG_ERROR(AQBANKING_LOGDOMAIN,
733
                            "Bad file \"%s\" (no name)",
734
                            GWEN_Buffer_GetStart(nbuf));
735
                }
736
                else {
737
                  GWEN_DB_NODE *dbTarget;
738
                  DBG_INFO(AQBANKING_LOGDOMAIN,
739
                           "File \"%s\" contains profile \"%s\"",
740
                           GWEN_Buffer_GetStart(nbuf), s);
741

    
742
                  dbTarget=GWEN_DB_GetGroup(db,
743
                                            GWEN_DB_FLAGS_OVERWRITE_GROUPS,
744
                                            s);
745
                  assert(dbTarget);
746
                  GWEN_DB_AddGroupChildren(dbTarget, dbT);
747
                  GWEN_DB_SetIntValue(dbTarget, GWEN_DB_FLAGS_OVERWRITE_VARS, "isGlobal", isGlobal);
748
                  GWEN_DB_SetCharValue(dbTarget, GWEN_DB_FLAGS_OVERWRITE_VARS, "fileName", nbuffer);
749
                } /* if name */
750
              } /* if file successfully read */
751
              GWEN_DB_Group_free(dbT);
752
            } /* if !dir */
753
          } /* if stat was ok */
754
        } /* if conf */
755
      } /* if name has more than 4 chars */
756
    } /* if not "." and not ".." */
757
  } /* while */
758
  GWEN_Directory_Close(d);
759
  GWEN_Directory_free(d);
760
  GWEN_Buffer_free(nbuf);
761

    
762
  return 0;
763
}
764

    
765

    
766

    
767
GWEN_DB_NODE *AB_Banking_GetImExporterProfiles(AB_BANKING *ab,
768
                                               const char *name)
769
{
770
  GWEN_BUFFER *buf;
771
  GWEN_DB_NODE *db;
772
  int rv;
773
  GWEN_STRINGLIST *sl;
774
  GWEN_STRINGLISTENTRY *sentry;
775

    
776
  buf=GWEN_Buffer_new(0, 256, 0, 1);
777
  db=GWEN_DB_Group_new("profiles");
778

    
779
  sl=AB_Banking_GetGlobalDataDirs();
780
  assert(sl);
781

    
782
  sentry=GWEN_StringList_FirstEntry(sl);
783
  assert(sentry);
784

    
785
  while (sentry) {
786
    const char *pkgdatadir;
787

    
788
    pkgdatadir = GWEN_StringListEntry_Data(sentry);
789
    assert(pkgdatadir);
790

    
791
    /* read global profiles */
792
    GWEN_Buffer_AppendString(buf, pkgdatadir);
793
    GWEN_Buffer_AppendString(buf,
794
                             DIRSEP
795
                             "aqbanking"
796
                             DIRSEP
797
                             AB_IMEXPORTER_FOLDER
798
                             DIRSEP);
799
    if (GWEN_Text_EscapeToBufferTolerant(name, buf)) {
800
      DBG_ERROR(AQBANKING_LOGDOMAIN,
801
                "Bad name for importer/exporter");
802
      GWEN_StringList_free(sl);
803
      GWEN_DB_Group_free(db);
804
      GWEN_Buffer_free(buf);
805
      return 0;
806
    }
807
    GWEN_Buffer_AppendString(buf, DIRSEP "profiles");
808
    rv=AB_Banking__ReadImExporterProfiles(ab,
809
                                          GWEN_Buffer_GetStart(buf),
810
                                          db,
811
                                          1);
812
    if (rv && rv!=GWEN_ERROR_NOT_FOUND) {
813
      DBG_ERROR(AQBANKING_LOGDOMAIN,
814
                "Error reading global profiles");
815
      GWEN_StringList_free(sl);
816
      GWEN_DB_Group_free(db);
817
      GWEN_Buffer_free(buf);
818
      return 0;
819
    }
820
    GWEN_Buffer_Reset(buf);
821
    sentry=GWEN_StringListEntry_Next(sentry);
822
  }
823
  GWEN_StringList_free(sl);
824

    
825
  /* read local user profiles */
826
  if (AB_Banking_GetUserDataDir(ab, buf)) {
827
    DBG_ERROR(AQBANKING_LOGDOMAIN,
828
              "Could not get user data dir");
829
    GWEN_DB_Group_free(db);
830
    GWEN_Buffer_free(buf);
831
    return 0;
832
  }
833
  GWEN_Buffer_AppendString(buf, DIRSEP AB_IMEXPORTER_FOLDER DIRSEP);
834
  if (GWEN_Text_EscapeToBufferTolerant(name, buf)) {
835
    DBG_ERROR(AQBANKING_LOGDOMAIN,
836
              "Bad name for importer/exporter");
837
    GWEN_DB_Group_free(db);
838
    GWEN_Buffer_free(buf);
839
    return 0;
840
  }
841
  GWEN_Buffer_AppendString(buf, DIRSEP "profiles");
842

    
843
  rv=AB_Banking__ReadImExporterProfiles(ab,
844
                                        GWEN_Buffer_GetStart(buf),
845
                                        db,
846
                                        0);
847
  if (rv && rv!=GWEN_ERROR_NOT_FOUND) {
848
    DBG_ERROR(AQBANKING_LOGDOMAIN,
849
              "Error reading users profiles");
850
    GWEN_DB_Group_free(db);
851
    GWEN_Buffer_free(buf);
852
    return 0;
853
  }
854
  GWEN_Buffer_free(buf);
855

    
856
  return db;
857
}
858

    
859

    
860

    
861
int AB_Banking_SaveLocalImExporterProfile(AB_BANKING *ab,
862
                                          const char *imexporterName,
863
                                          GWEN_DB_NODE *dbProfile,
864
                                          const char *fname)
865
{
866
  GWEN_BUFFER *buf;
867
  int rv;
868

    
869
  buf=GWEN_Buffer_new(0, 256, 0, 1);
870

    
871
  /* get folder for local user profiles */
872
  rv=AB_Banking_GetUserDataDir(ab, buf);
873
  if (rv<0) {
874
    DBG_ERROR(AQBANKING_LOGDOMAIN,
875
              "Could not get user data dir");
876
    GWEN_Buffer_free(buf);
877
    return rv;
878
  }
879
  GWEN_Buffer_AppendString(buf, DIRSEP AB_IMEXPORTER_FOLDER DIRSEP);
880
  rv=GWEN_Text_EscapeToBufferTolerant(imexporterName, buf);
881
  if (rv<0) {
882
    DBG_ERROR(AQBANKING_LOGDOMAIN,
883
              "Bad name for importer/exporter (%d)", rv);
884
    GWEN_Buffer_free(buf);
885
    return rv;
886
  }
887
  GWEN_Buffer_AppendString(buf, DIRSEP "profiles");
888

    
889
  /* make sure the path exists */
890
  rv=GWEN_Directory_GetPath(GWEN_Buffer_GetStart(buf), GWEN_PATH_FLAGS_CHECKROOT);
891
  if (rv<0) {
892
    DBG_INFO(AQBANKING_LOGDOMAIN, "here (%d)", rv);
893
    GWEN_Buffer_free(buf);
894
    return rv;
895
  }
896

    
897
  GWEN_Buffer_AppendString(buf, DIRSEP);
898
  if (fname && *fname)
899
    GWEN_Buffer_AppendString(buf, fname);
900
  else {
901
    const char *s;
902

    
903
    s=GWEN_DB_GetCharValue(dbProfile, "name", 0, NULL);
904
    if (s && *s) {
905
      FILE *f;
906

    
907
      rv=GWEN_Text_EscapeToBufferTolerant(s, buf);
908
      if (rv<0) {
909
        DBG_ERROR(AQBANKING_LOGDOMAIN,
910
                  "Bad profile name (%d)", rv);
911
        GWEN_Buffer_free(buf);
912
        return rv;
913
      }
914
      GWEN_Buffer_AppendString(buf, ".conf");
915

    
916
      f=fopen(GWEN_Buffer_GetStart(buf), "r");
917
      if (f) {
918
        DBG_ERROR(AQBANKING_LOGDOMAIN, "There already is a profile of that name");
919
        GWEN_Buffer_free(buf);
920
        fclose(f);
921
        return GWEN_ERROR_INVALID;
922
      }
923
    }
924
    else {
925
      DBG_ERROR(AQBANKING_LOGDOMAIN,
926
                "Missing profile name");
927
      GWEN_Buffer_free(buf);
928
      return GWEN_ERROR_INVALID;
929
    }
930
  }
931

    
932
  rv=GWEN_DB_WriteFile(dbProfile,
933
                       GWEN_Buffer_GetStart(buf),
934
                       GWEN_DB_FLAGS_DEFAULT);
935
  if (rv<0) {
936
    DBG_ERROR(AQBANKING_LOGDOMAIN,
937
              "Error writing users profile (%d)", rv);
938
    GWEN_Buffer_free(buf);
939
    return 0;
940
  }
941
  GWEN_Buffer_free(buf);
942

    
943
  return 0;
944
}
945

    
946

    
947

    
948
int AB_Banking_FindDataFileForImExporter(AB_BANKING *ab, const char *imExpName, const char *fileName,
949
                                         GWEN_BUFFER *fullPathBuffer)
950
{
951
  GWEN_BUFFER *buf;
952
  int rv;
953
  GWEN_STRINGLIST *sl;
954

    
955
  buf=GWEN_Buffer_new(0, 256, 0, 1);
956
  GWEN_Buffer_AppendString(buf,
957
                           DIRSEP
958
                           "aqbanking"
959
                           DIRSEP
960
                           AB_IMEXPORTER_FOLDER
961
                           DIRSEP);
962
  GWEN_Buffer_AppendString(buf, imExpName);
963
  GWEN_Buffer_AppendString(buf, DIRSEP "data" DIRSEP);
964
  GWEN_Buffer_AppendString(buf, fileName);
965

    
966

    
967
  sl=AB_Banking_GetGlobalDataDirs();
968
  assert(sl);
969

    
970
  rv=GWEN_Directory_FindFileInPaths(sl, GWEN_Buffer_GetStart(buf), fullPathBuffer);
971
  if (rv==0) {
972
    GWEN_Buffer_free(buf);
973
    return rv;
974
  }
975
  GWEN_Buffer_Reset(buf);
976

    
977
  /* try local storage */
978
  if (AB_Banking_GetUserDataDir(ab, buf)) {
979
    DBG_ERROR(AQBANKING_LOGDOMAIN,
980
              "Could not get user data dir");
981
    GWEN_Buffer_free(buf);
982
    return GWEN_ERROR_NOT_FOUND;
983
  }
984
  GWEN_Buffer_AppendString(buf, DIRSEP AB_IMEXPORTER_FOLDER DIRSEP);
985
  GWEN_Buffer_AppendString(buf, fileName);
986

    
987
  rv=GWEN_Directory_GetPath(GWEN_Buffer_GetStart(buf), GWEN_PATH_FLAGS_PATHMUSTEXIST);
988
  if (rv==0) {
989
    GWEN_Buffer_AppendString(fullPathBuffer, GWEN_Buffer_GetStart(buf));
990
    GWEN_Buffer_free(buf);
991
    return 0;
992
  }
993

    
994
  GWEN_Buffer_free(buf);
995

    
996
  return GWEN_ERROR_NOT_FOUND;
997
}
998

    
999

    
1000

    
1001
GWEN_STRINGLIST *AB_Banking_ListDataFilesForImExporter(AB_BANKING *ab, const char *imExpName, const char *fileMask)
1002
{
1003
  int rv;
1004
  GWEN_BUFFER *pathBuffer;
1005
  GWEN_STRINGLIST *slGlobalDataDirs;
1006
  GWEN_STRINGLIST *slMatchingFiles;
1007
  GWEN_STRINGLISTENTRY *seGlobalDataDir;
1008

    
1009
  slGlobalDataDirs=AB_Banking_GetGlobalDataDirs();
1010
  assert(slGlobalDataDirs);
1011

    
1012
  slMatchingFiles=GWEN_StringList_new();
1013

    
1014
  pathBuffer=GWEN_Buffer_new(0, 256, 0, 1);
1015
  seGlobalDataDir=GWEN_StringList_FirstEntry(slGlobalDataDirs);
1016
  while (seGlobalDataDir) {
1017
    GWEN_Buffer_AppendString(pathBuffer, GWEN_StringListEntry_Data(seGlobalDataDir));
1018
    GWEN_Buffer_AppendString(pathBuffer,
1019
                             DIRSEP
1020
                             "aqbanking"
1021
                             DIRSEP
1022
                             AB_IMEXPORTER_FOLDER
1023
                             DIRSEP);
1024
    GWEN_Buffer_AppendString(pathBuffer, imExpName);
1025
    GWEN_Buffer_AppendString(pathBuffer, DIRSEP "data");
1026

    
1027
    rv=GWEN_Directory_GetMatchingFilesRecursively(GWEN_Buffer_GetStart(pathBuffer), slMatchingFiles, fileMask);
1028
    if (rv<0) {
1029
      DBG_INFO(AQBANKING_LOGDOMAIN,
1030
               "Error listing matching files in folder \"%s\", ignoring",
1031
               GWEN_Buffer_GetStart(pathBuffer));
1032
    }
1033
    GWEN_Buffer_Reset(pathBuffer);
1034

    
1035
    seGlobalDataDir=GWEN_StringListEntry_Next(seGlobalDataDir);
1036
  } /* while(seGlobalDataDir) */
1037
  GWEN_Buffer_free(pathBuffer);
1038

    
1039

    
1040
  if (GWEN_StringList_Count(slMatchingFiles)<1) {
1041
    DBG_INFO(AQBANKING_LOGDOMAIN, "No matching data files");
1042
    GWEN_StringList_free(slMatchingFiles);
1043
    return NULL;
1044
  }
1045

    
1046
  return slMatchingFiles;
1047
}
1048

    
1049

    
1050

    
1051
GWEN_DB_NODE *AB_Banking_GetImExporterProfile(AB_BANKING *ab,
1052
                                              const char *imExporterName,
1053
                                              const char *profileName)
1054
{
1055
  GWEN_DB_NODE *dbProfiles;
1056

    
1057
  dbProfiles=AB_Banking_GetImExporterProfiles(ab, imExporterName);
1058
  if (dbProfiles) {
1059
    GWEN_DB_NODE *dbProfile;
1060

    
1061
    dbProfile=GWEN_DB_GetFirstGroup(dbProfiles);
1062
    while (dbProfile) {
1063
      const char *name;
1064

    
1065
      name=GWEN_DB_GetCharValue(dbProfile, "name", 0, 0);
1066
      assert(name);
1067
      if (strcasecmp(name, profileName)==0)
1068
        break;
1069
      dbProfile=GWEN_DB_GetNextGroup(dbProfile);
1070
    }
1071
    if (!dbProfile) {
1072
      DBG_ERROR(AQBANKING_LOGDOMAIN,
1073
                "Profile \"%s\" for exporter \"%s\" not found",
1074
                profileName, imExporterName);
1075
      GWEN_DB_Group_free(dbProfiles);
1076
      return NULL;
1077
    }
1078

    
1079
    GWEN_DB_UnlinkGroup(dbProfile);
1080
    GWEN_DB_Group_free(dbProfiles);
1081
    return dbProfile;
1082
  }
1083
  else {
1084
    DBG_ERROR(AQBANKING_LOGDOMAIN,
1085
              "No profiles found for exporter \"%s\"",
1086
              imExporterName);
1087
    return NULL;
1088
  }
1089

    
1090
  return NULL;
1091
}
1092

    
1093

    
1094

    
1095
GWEN_DB_NODE *AB_Banking_FindMatchingSwiftImExporterProfile(AB_BANKING *ab,
1096
                                                            const char *imExporterName,
1097
                                                            const char *family,
1098
                                                            int version1,
1099
                                                            int version2,
1100
                                                            int version3)
1101
{
1102
  GWEN_DB_NODE *dbProfiles;
1103

    
1104
  dbProfiles=AB_Banking_GetImExporterProfiles(ab, imExporterName);
1105
  if (dbProfiles) {
1106
    GWEN_DB_NODE *dbProfile;
1107

    
1108
    dbProfile=GWEN_DB_GetFirstGroup(dbProfiles);
1109
    while (dbProfile) {
1110
      const char *name;
1111
      AB_SWIFT_DESCR *swiftDescr;
1112

    
1113
      name=GWEN_DB_GetCharValue(dbProfile, "name", 0, 0);
1114
      assert(name);
1115

    
1116
      swiftDescr=AB_SwiftDescr_FromString(name);
1117
      if (swiftDescr) {
1118
        if (AB_SwiftDescr_Matches(swiftDescr, family, version1, version2, version3)) {
1119
          AB_SwiftDescr_free(swiftDescr);
1120
          break;
1121
        }
1122
      }
1123
      AB_SwiftDescr_free(swiftDescr);
1124

    
1125
      dbProfile=GWEN_DB_GetNextGroup(dbProfile);
1126
    }
1127
    if (!dbProfile) {
1128
      DBG_ERROR(AQBANKING_LOGDOMAIN,
1129
                "Profile \"%s.%03d.%03d.%02d\" for exporter \"%s\" not found",
1130
                family, version1, version2, version3,
1131
                imExporterName);
1132
      GWEN_DB_Group_free(dbProfiles);
1133
      return NULL;
1134
    }
1135

    
1136
    GWEN_DB_UnlinkGroup(dbProfile);
1137
    GWEN_DB_Group_free(dbProfiles);
1138
    return dbProfile;
1139
  }
1140
  else {
1141
    DBG_ERROR(AQBANKING_LOGDOMAIN,
1142
              "No profiles found for exporter \"%s\"",
1143
              imExporterName);
1144
    return NULL;
1145
  }
1146

    
1147
  return NULL;
1148
}
1149

    
1150

    
1151

    
1152
AB_SWIFT_DESCR_LIST *AB_Banking_GetSwiftDescriptorsForImExporter(AB_BANKING *ab, const char *imExporterName)
1153
{
1154
  GWEN_DB_NODE *dbProfiles;
1155

    
1156
  dbProfiles=AB_Banking_GetImExporterProfiles(ab, imExporterName);
1157
  if (dbProfiles) {
1158
    GWEN_DB_NODE *dbProfile;
1159
    AB_SWIFT_DESCR_LIST *descrList;
1160

    
1161
    descrList=AB_SwiftDescr_List_new();
1162
    dbProfile=GWEN_DB_GetFirstGroup(dbProfiles);
1163
    while (dbProfile) {
1164
      const char *name;
1165
      AB_SWIFT_DESCR *descr;
1166

    
1167
      name=GWEN_DB_GetCharValue(dbProfile, "name", 0, 0);
1168
      assert(name);
1169

    
1170
      descr=AB_SwiftDescr_FromString(name);
1171
      if (descr) {
1172
        DBG_INFO(AQBANKING_LOGDOMAIN, "Adding matching profile [%s]", name);
1173
        AB_SwiftDescr_SetAlias1(descr, name);
1174
        AB_SwiftDescr_List_Add(descr, descrList);
1175
      }
1176

    
1177
      dbProfile=GWEN_DB_GetNextGroup(dbProfile);
1178
    }
1179

    
1180
    if (AB_SwiftDescr_List_GetCount(descrList)==0) {
1181
      AB_SwiftDescr_List_free(descrList);
1182
      return NULL;
1183
    }
1184

    
1185
    return descrList;
1186
  }
1187
  else {
1188
    DBG_ERROR(AQBANKING_LOGDOMAIN,
1189
              "No profiles found for exporter \"%s\"",
1190
              imExporterName);
1191
    return NULL;
1192
  }
1193

    
1194
  return NULL;
1195
}
1196

    
1197

    
1198

    
1199