Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (23 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

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

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

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

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

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

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

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

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

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

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

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

    
58

    
59

    
60
AB_IMEXPORTER *AB_Banking__CreateImExporterPlugin(AB_BANKING *ab, const char *modname){
61
  if (modname && *modname) {
62
#ifdef AQBANKING_WITH_PLUGIN_IMEXPORTER_CSV
63
    if (strcasecmp(modname, "csv")==0)
64
      return AB_ImExporterCSV_new(ab);
65
#endif
66

    
67
#ifdef AQBANKING_WITH_PLUGIN_IMEXPORTER_ERI2
68
    if (strcasecmp(modname, "eri2")==0)
69
      return AB_ImExporterERI2_new(ab);
70
#endif
71

    
72
#ifdef AQBANKING_WITH_PLUGIN_IMEXPORTER_OFX
73
    if (strcasecmp(modname, "ofx")==0)
74
      return AB_ImExporterOFX_new(ab);
75
#endif
76

    
77
#ifdef AQBANKING_WITH_PLUGIN_IMEXPORTER_OPENHBCI1
78
    if (strcasecmp(modname, "ofx")==0)
79
      return AB_ImExporterOpenHBCI1_new(ab);
80
#endif
81

    
82
#ifdef AQBANKING_WITH_PLUGIN_IMEXPORTER_SWIFT
83
    if (strcasecmp(modname, "swift")==0)
84
      return AB_ImExporterSWIFT_new(ab);
85
#endif
86

    
87
#ifdef AQBANKING_WITH_PLUGIN_IMEXPORTER_XMLDB
88
    if (strcasecmp(modname, "xmldb")==0)
89
      return AB_ImExporterXMLDB_new(ab);
90
#endif
91

    
92
#ifdef AQBANKING_WITH_PLUGIN_IMEXPORTER_YELLOWNET
93
    if (strcasecmp(modname, "yellownet")==0)
94
      return AB_ImExporterYellowNet_new(ab);
95
#endif
96

    
97
#ifdef AQBANKING_WITH_PLUGIN_IMEXPORTER_SEPA
98
    if (strcasecmp(modname, "sepa")==0)
99
      return AB_ImExporterSEPA_new(ab);
100
#endif
101

    
102
#ifdef AQBANKING_WITH_PLUGIN_IMEXPORTER_CTXFILE
103
    if (strcasecmp(modname, "ctxfile")==0)
104
      return AB_ImExporterCtxFile_new(ab);
105
#endif
106

    
107
#ifdef AQBANKING_WITH_PLUGIN_IMEXPORTER_Q43
108
    if (strcasecmp(modname, "q43")==0)
109
      return AB_ImExporterQ43_new(ab);
110
#endif
111

    
112
#ifdef AQBANKING_WITH_PLUGIN_IMEXPORTER_CAMT
113
    if (strcasecmp(modname, "camt")==0)
114
      return AB_ImExporterCAMT_new(ab);
115
#endif
116

    
117
    DBG_ERROR(AQBANKING_LOGDOMAIN, "Plugin [%s] not compiled-in", modname);
118
  }
119

    
120
  return NULL;
121
}
122

    
123

    
124

    
125
AB_IMEXPORTER *AB_Banking__LoadImExporterPlugin(AB_BANKING *ab, const char *modname){
126
  GWEN_PLUGIN *pl;
127

    
128
  pl=GWEN_PluginManager_GetPlugin(ab_pluginManagerImExporter, modname);
129
  if (pl) {
130
    AB_IMEXPORTER *ie;
131

    
132
    ie=AB_Plugin_ImExporter_Factory(pl, ab);
133
    if (!ie) {
134
      DBG_ERROR(AQBANKING_LOGDOMAIN, "Error in plugin [%s]: No im/exporter created", modname);
135
      return NULL;
136
    }
137
    return ie;
138
  }
139
  else {
140
    DBG_INFO(AQBANKING_LOGDOMAIN, "Plugin [%s] not found", modname);
141
    return NULL;
142
  }
143
}
144

    
145

    
146

    
147
AB_IMEXPORTER *AB_Banking_FindImExporter(AB_BANKING *ab, const char *name) {
148
  AB_IMEXPORTER *ie;
149

    
150
  assert(ab);
151
  assert(name);
152
  ie=AB_ImExporter_List_First(ab_imexporters);
153
  while(ie) {
154
    if (strcasecmp(AB_ImExporter_GetName(ie), name)==0)
155
      break;
156
    ie=AB_ImExporter_List_Next(ie);
157
  } /* while */
158

    
159
  return ie;
160
}
161

    
162

    
163

    
164
AB_IMEXPORTER *AB_Banking_GetImExporter(AB_BANKING *ab, const char *name){
165
  AB_IMEXPORTER *ie;
166

    
167
  assert(ab);
168
  assert(name);
169

    
170
  ie=AB_Banking_FindImExporter(ab, name);
171
  if (ie)
172
    return ie;
173
  ie=AB_Banking__CreateImExporterPlugin(ab, name);
174
  if (ie)
175
    return ie;
176
  ie=AB_Banking__LoadImExporterPlugin(ab, name);
177
  if (ie)
178
    AB_ImExporter_List_Add(ie, ab_imexporters);
179

    
180
  return ie;
181
}
182

    
183

    
184

    
185
void AB_Banking_FillGapsInTransaction(AB_BANKING *ab, AB_ACCOUNT *a, AB_TRANSACTION *t) {
186
  assert(t);
187

    
188
  if (a) {
189
    const char *s;
190

    
191
    /* local account */
192
    s=AB_Account_GetCountry(a);
193
    if (!s || !*s)
194
      s="de";
195
    AB_Transaction_SetLocalCountry(t, s);
196
    AB_Transaction_SetRemoteCountry(t, s);
197
  
198
    s=AB_Account_GetBankCode(a);
199
    if (s && *s)
200
      AB_Transaction_SetLocalBankCode(t, s);
201
  
202
    s=AB_Account_GetAccountNumber(a);
203
    if (s && *s)
204
      AB_Transaction_SetLocalAccountNumber(t, s);
205
  
206
    s=AB_Account_GetOwnerName(a);
207
    if (s && *s)
208
      AB_Transaction_SetLocalName(t, s);
209
  
210
    s=AB_Account_GetBic(a);
211
    if (s && *s)
212
      AB_Transaction_SetLocalBic(t, s);
213
  
214
    s=AB_Account_GetIban(a);
215
    if (s && *s)
216
      AB_Transaction_SetLocalIban(t, s);
217
  }
218
}
219

    
220

    
221

    
222
int AB_Banking_ExportToBuffer(AB_BANKING *ab,
223
                              AB_IMEXPORTER_CONTEXT *ctx,
224
                              const char *exporterName,
225
                              const char *profileName,
226
                              GWEN_BUFFER *buf) {
227
  AB_IMEXPORTER *ie;
228
  GWEN_DB_NODE *dbProfile;
229
  int rv;
230

    
231
  ie=AB_Banking_GetImExporter(ab, exporterName);
232
  if (ie==NULL) {
233
    DBG_INFO(AQBANKING_LOGDOMAIN, "here");
234
    return GWEN_ERROR_NO_DATA;
235
  }
236

    
237
  if (profileName && *profileName)
238
    dbProfile=AB_Banking_GetImExporterProfile(ab, exporterName, profileName);
239
  else
240
    dbProfile=GWEN_DB_Group_new("profile");
241
  if (dbProfile==NULL) {
242
    DBG_ERROR(AQBANKING_LOGDOMAIN, "Profile [%s] not found",
243
              profileName?profileName:"(null)");
244
    return GWEN_ERROR_NO_DATA;
245
  }
246

    
247
  rv=AB_ImExporter_ExportToBuffer(ie, ctx, buf, dbProfile);
248
  GWEN_DB_Group_free(dbProfile);
249

    
250
  if (rv<0) {
251
    DBG_INFO(AQBANKING_LOGDOMAIN, "here (%d)", rv);
252
    return rv;
253
  }
254

    
255
  return 0;
256
}
257

    
258

    
259

    
260
int AB_Banking_ExportToFile(AB_BANKING *ab,
261
                            AB_IMEXPORTER_CONTEXT *ctx,
262
                            const char *exporterName,
263
                            const char *profileName,
264
                            const char *fname) {
265
  AB_IMEXPORTER *ie;
266
  GWEN_DB_NODE *dbProfile;
267
  int rv;
268

    
269
  ie=AB_Banking_GetImExporter(ab, exporterName);
270
  if (ie==NULL) {
271
    DBG_INFO(AQBANKING_LOGDOMAIN, "here");
272
    return GWEN_ERROR_NO_DATA;
273
  }
274

    
275
  if (profileName && *profileName)
276
    dbProfile=AB_Banking_GetImExporterProfile(ab, exporterName, profileName);
277
  else
278
    dbProfile=GWEN_DB_Group_new("profile");
279
  if (dbProfile==NULL) {
280
    DBG_ERROR(AQBANKING_LOGDOMAIN, "Profile [%s] not found",
281
              profileName?profileName:"(null)");
282
    return GWEN_ERROR_NO_DATA;
283
  }
284

    
285
  rv=AB_ImExporter_ExportToFile(ie, ctx, fname, dbProfile);
286
  GWEN_DB_Group_free(dbProfile);
287

    
288
  if (rv<0) {
289
    DBG_INFO(AQBANKING_LOGDOMAIN, "here (%d)", rv);
290
    return rv;
291
  }
292

    
293
  return 0;
294
}
295

    
296

    
297

    
298
int AB_Banking_ImportBuffer(AB_BANKING *ab,
299
                            AB_IMEXPORTER_CONTEXT *ctx,
300
                            const char *exporterName,
301
                            const char *profileName,
302
                            GWEN_BUFFER *buf) {
303
  AB_IMEXPORTER *ie;
304
  GWEN_DB_NODE *dbProfile;
305
  int rv;
306

    
307
  ie=AB_Banking_GetImExporter(ab, exporterName);
308
  if (ie==NULL) {
309
    DBG_INFO(AQBANKING_LOGDOMAIN, "here");
310
    return GWEN_ERROR_NO_DATA;
311
  }
312

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

    
323
  rv=AB_ImExporter_ImportBuffer(ie, ctx, buf, dbProfile);
324
  GWEN_DB_Group_free(dbProfile);
325

    
326
  if (rv<0) {
327
    DBG_INFO(AQBANKING_LOGDOMAIN, "here (%d)", rv);
328
    return rv;
329
  }
330

    
331
  return 0;
332
}
333

    
334

    
335

    
336
GWEN_PLUGIN_DESCRIPTION_LIST2 *AB_Banking_GetImExporterDescrs(AB_BANKING *ab){
337
  assert(ab);
338
  if (ab_pluginManagerImExporter) {
339
    GWEN_PLUGIN_DESCRIPTION_LIST2 *l;
340

    
341
    l=GWEN_PluginManager_GetPluginDescrs(ab_pluginManagerImExporter);
342
    return l;
343
  }
344
  else {
345
    DBG_ERROR(AQBANKING_LOGDOMAIN, "No plugin manager for imexporters");
346
    return NULL;
347
  }
348
}
349

    
350

    
351

    
352
int AB_Banking__ReadImExporterProfiles(AB_BANKING *ab,
353
                                       const char *path,
354
                                       GWEN_DB_NODE *db,
355
                                       int isGlobal) {
356
  GWEN_DIRECTORY *d;
357
  GWEN_BUFFER *nbuf;
358
  char nbuffer[64];
359
  unsigned int pathLen;
360

    
361
  if (!path)
362
    path="";
363

    
364
  /* create path */
365
  nbuf=GWEN_Buffer_new(0, 256, 0, 1);
366
  GWEN_Buffer_AppendString(nbuf, path);
367
  pathLen=GWEN_Buffer_GetUsedBytes(nbuf);
368

    
369
  d=GWEN_Directory_new();
370
  if (GWEN_Directory_Open(d, GWEN_Buffer_GetStart(nbuf))) {
371
    DBG_INFO(AQBANKING_LOGDOMAIN,
372
             "Path \"%s\" is not available",
373
             GWEN_Buffer_GetStart(nbuf));
374
    GWEN_Buffer_free(nbuf);
375
    GWEN_Directory_free(d);
376
    return GWEN_ERROR_NOT_FOUND;
377
  }
378

    
379
  while(!GWEN_Directory_Read(d,
380
                             nbuffer,
381
                             sizeof(nbuffer))) {
382
    if (strcmp(nbuffer, ".") &&
383
        strcmp(nbuffer, "..")) {
384
      int nlen;
385

    
386
      nlen=strlen(nbuffer);
387
      if (nlen>4) {
388
        if (strcasecmp(nbuffer+nlen-5, ".conf")==0) {
389
          struct stat st;
390

    
391
          GWEN_Buffer_Crop(nbuf, 0, pathLen);
392
          GWEN_Buffer_SetPos(nbuf, pathLen);
393
          GWEN_Buffer_AppendString(nbuf, DIRSEP);
394
          GWEN_Buffer_AppendString(nbuf, nbuffer);
395

    
396
          if (stat(GWEN_Buffer_GetStart(nbuf), &st)) {
397
            DBG_ERROR(AQBANKING_LOGDOMAIN, "stat(%s): %s",
398
                      GWEN_Buffer_GetStart(nbuf),
399
                      strerror(errno));
400
          }
401
          else {
402
            if (!S_ISDIR(st.st_mode)) {
403
              GWEN_DB_NODE *dbT;
404

    
405
              dbT=GWEN_DB_Group_new("profile");
406
              if (GWEN_DB_ReadFile(dbT,
407
                                   GWEN_Buffer_GetStart(nbuf),
408
                                   GWEN_DB_FLAGS_DEFAULT |
409
                                   GWEN_PATH_FLAGS_CREATE_GROUP)) {
410
                DBG_ERROR(AQBANKING_LOGDOMAIN,
411
                          "Could not read file \"%s\"",
412
                          GWEN_Buffer_GetStart(nbuf));
413
              }
414
              else {
415
                const char *s;
416

    
417
                s=GWEN_DB_GetCharValue(dbT, "name", 0, 0);
418
                if (!s) {
419
                  DBG_ERROR(AQBANKING_LOGDOMAIN,
420
                            "Bad file \"%s\" (no name)",
421
                            GWEN_Buffer_GetStart(nbuf));
422
                }
423
                else {
424
                  GWEN_DB_NODE *dbTarget;
425
                  DBG_INFO(AQBANKING_LOGDOMAIN,
426
                           "File \"%s\" contains profile \"%s\"",
427
                           GWEN_Buffer_GetStart(nbuf), s);
428

    
429
                  dbTarget=GWEN_DB_GetGroup(db,
430
                                            GWEN_DB_FLAGS_OVERWRITE_GROUPS,
431
                                            s);
432
                  assert(dbTarget);
433
                  GWEN_DB_AddGroupChildren(dbTarget, dbT);
434
                  GWEN_DB_SetIntValue(dbTarget, GWEN_DB_FLAGS_OVERWRITE_VARS, "isGlobal", isGlobal);
435
                  GWEN_DB_SetCharValue(dbTarget, GWEN_DB_FLAGS_OVERWRITE_VARS, "fileName", nbuffer);
436
                } /* if name */
437
              } /* if file successfully read */
438
              GWEN_DB_Group_free(dbT);
439
            } /* if !dir */
440
          } /* if stat was ok */
441
        } /* if conf */
442
      } /* if name has more than 4 chars */
443
    } /* if not "." and not ".." */
444
  } /* while */
445
  GWEN_Directory_Close(d);
446
  GWEN_Directory_free(d);
447
  GWEN_Buffer_free(nbuf);
448

    
449
  return 0;
450
}
451

    
452

    
453

    
454
GWEN_DB_NODE *AB_Banking_GetImExporterProfiles(AB_BANKING *ab,
455
                                               const char *name){
456
  GWEN_BUFFER *buf;
457
  GWEN_DB_NODE *db;
458
  int rv;
459
  GWEN_STRINGLIST *sl;
460
  GWEN_STRINGLISTENTRY *sentry;
461

    
462
  buf=GWEN_Buffer_new(0, 256, 0, 1);
463
  db=GWEN_DB_Group_new("profiles");
464

    
465
  sl=AB_Banking_GetGlobalDataDirs();
466
  assert(sl);
467

    
468
  sentry=GWEN_StringList_FirstEntry(sl);
469
  assert(sentry);
470

    
471
  while(sentry) {
472
    const char *pkgdatadir;
473

    
474
    pkgdatadir = GWEN_StringListEntry_Data(sentry);
475
    assert(pkgdatadir);
476

    
477
    /* read global profiles */
478
    GWEN_Buffer_AppendString(buf, pkgdatadir);
479
    GWEN_Buffer_AppendString(buf,
480
                             DIRSEP
481
                             "aqbanking"
482
                             DIRSEP
483
                             AB_IMEXPORTER_FOLDER
484
                             DIRSEP);
485
    if (GWEN_Text_EscapeToBufferTolerant(name, buf)) {
486
      DBG_ERROR(AQBANKING_LOGDOMAIN,
487
                "Bad name for importer/exporter");
488
      GWEN_StringList_free(sl);
489
      GWEN_DB_Group_free(db);
490
      GWEN_Buffer_free(buf);
491
      return 0;
492
    }
493
    GWEN_Buffer_AppendString(buf, DIRSEP "profiles");
494
    rv=AB_Banking__ReadImExporterProfiles(ab,
495
                                          GWEN_Buffer_GetStart(buf),
496
                                          db,
497
                                          1);
498
    if (rv && rv!=GWEN_ERROR_NOT_FOUND) {
499
      DBG_ERROR(AQBANKING_LOGDOMAIN,
500
                "Error reading global profiles");
501
      GWEN_StringList_free(sl);
502
      GWEN_DB_Group_free(db);
503
      GWEN_Buffer_free(buf);
504
      return 0;
505
    }
506
    GWEN_Buffer_Reset(buf);
507
    sentry=GWEN_StringListEntry_Next(sentry);
508
  }
509
  GWEN_StringList_free(sl);
510

    
511
  /* read local user profiles */
512
  if (AB_Banking_GetUserDataDir(ab, buf)) {
513
    DBG_ERROR(AQBANKING_LOGDOMAIN,
514
              "Could not get user data dir");
515
    GWEN_DB_Group_free(db);
516
    GWEN_Buffer_free(buf);
517
    return 0;
518
  }
519
  GWEN_Buffer_AppendString(buf, DIRSEP AB_IMEXPORTER_FOLDER DIRSEP);
520
  if (GWEN_Text_EscapeToBufferTolerant(name, buf)) {
521
    DBG_ERROR(AQBANKING_LOGDOMAIN,
522
              "Bad name for importer/exporter");
523
    GWEN_DB_Group_free(db);
524
    GWEN_Buffer_free(buf);
525
    return 0;
526
  }
527
  GWEN_Buffer_AppendString(buf, DIRSEP "profiles");
528

    
529
  rv=AB_Banking__ReadImExporterProfiles(ab,
530
                                        GWEN_Buffer_GetStart(buf),
531
                                        db,
532
                                        0);
533
  if (rv && rv!=GWEN_ERROR_NOT_FOUND) {
534
    DBG_ERROR(AQBANKING_LOGDOMAIN,
535
              "Error reading users profiles");
536
    GWEN_DB_Group_free(db);
537
    GWEN_Buffer_free(buf);
538
    return 0;
539
  }
540
  GWEN_Buffer_free(buf);
541

    
542
  return db;
543
}
544

    
545

    
546

    
547
int AB_Banking_SaveLocalImExporterProfile(AB_BANKING *ab,
548
                                          const char *imexporterName,
549
                                          GWEN_DB_NODE *dbProfile,
550
                                          const char *fname){
551
  GWEN_BUFFER *buf;
552
  int rv;
553

    
554
  buf=GWEN_Buffer_new(0, 256, 0, 1);
555

    
556
  /* get folder for local user profiles */
557
  rv=AB_Banking_GetUserDataDir(ab, buf);
558
  if (rv<0) {
559
    DBG_ERROR(AQBANKING_LOGDOMAIN,
560
              "Could not get user data dir");
561
    GWEN_Buffer_free(buf);
562
    return rv;
563
  }
564
  GWEN_Buffer_AppendString(buf, DIRSEP AB_IMEXPORTER_FOLDER DIRSEP);
565
  rv=GWEN_Text_EscapeToBufferTolerant(imexporterName, buf);
566
  if (rv<0) {
567
    DBG_ERROR(AQBANKING_LOGDOMAIN,
568
              "Bad name for importer/exporter (%d)", rv);
569
    GWEN_Buffer_free(buf);
570
    return rv;
571
  }
572
  GWEN_Buffer_AppendString(buf, DIRSEP "profiles");
573

    
574
  /* make sure the path exists */
575
  rv=GWEN_Directory_GetPath(GWEN_Buffer_GetStart(buf), GWEN_PATH_FLAGS_CHECKROOT);
576
  if (rv<0) {
577
    DBG_INFO(AQBANKING_LOGDOMAIN, "here (%d)", rv);
578
    GWEN_Buffer_free(buf);
579
    return rv;
580
  }
581

    
582
  GWEN_Buffer_AppendString(buf, DIRSEP);
583
  if (fname && *fname)
584
    GWEN_Buffer_AppendString(buf, fname);
585
  else {
586
    const char *s;
587

    
588
    s=GWEN_DB_GetCharValue(dbProfile, "name", 0, NULL);
589
    if (s && *s) {
590
      FILE *f;
591

    
592
      rv=GWEN_Text_EscapeToBufferTolerant(s, buf);
593
      if (rv<0) {
594
        DBG_ERROR(AQBANKING_LOGDOMAIN,
595
                  "Bad profile name (%d)", rv);
596
        GWEN_Buffer_free(buf);
597
        return rv;
598
      }
599
      GWEN_Buffer_AppendString(buf, ".conf");
600

    
601
      f=fopen(GWEN_Buffer_GetStart(buf), "r");
602
      if (f) {
603
        DBG_ERROR(AQBANKING_LOGDOMAIN, "There already is a profile of that name");
604
        GWEN_Buffer_free(buf);
605
        fclose(f);
606
        return GWEN_ERROR_INVALID;
607
      }
608
    }
609
    else {
610
      DBG_ERROR(AQBANKING_LOGDOMAIN,
611
                "Missing profile name");
612
      GWEN_Buffer_free(buf);
613
      return GWEN_ERROR_INVALID;
614
    }
615
  }
616

    
617
  rv=GWEN_DB_WriteFile(dbProfile,
618
                       GWEN_Buffer_GetStart(buf),
619
                       GWEN_DB_FLAGS_DEFAULT);
620
  if (rv<0) {
621
    DBG_ERROR(AQBANKING_LOGDOMAIN,
622
              "Error writing users profile (%d)", rv);
623
    GWEN_Buffer_free(buf);
624
    return 0;
625
  }
626
  GWEN_Buffer_free(buf);
627

    
628
  return 0;
629
}
630

    
631

    
632

    
633

    
634

    
635
int AB_Banking_ExportWithProfile(AB_BANKING *ab,
636
                                 const char *exporterName,
637
                                 AB_IMEXPORTER_CONTEXT *ctx,
638
                                 const char *profileName,
639
                                 const char *profileFile,
640
                                 GWEN_SYNCIO *sio) {
641
  AB_IMEXPORTER *exporter;
642
  GWEN_DB_NODE *dbProfiles;
643
  GWEN_DB_NODE *dbProfile;
644
  int rv;
645

    
646
  exporter=AB_Banking_GetImExporter(ab, exporterName);
647
  if (!exporter) {
648
    DBG_ERROR(AQBANKING_LOGDOMAIN,
649
              "Export module \"%s\" not found",
650
              exporterName);
651
    return GWEN_ERROR_NOT_FOUND;
652
  }
653

    
654
  /* get profiles */
655
  if (profileFile) {
656
    dbProfiles=GWEN_DB_Group_new("profiles");
657
    if (GWEN_DB_ReadFile(dbProfiles, profileFile,
658
                         GWEN_DB_FLAGS_DEFAULT |
659
                         GWEN_PATH_FLAGS_CREATE_GROUP)) {
660
      DBG_ERROR(0, "Error reading profiles from \"%s\"",
661
                profileFile);
662
      return GWEN_ERROR_GENERIC;
663
    }
664
  }
665
  else {
666
    dbProfiles=AB_Banking_GetImExporterProfiles(ab, exporterName);
667
  }
668

    
669
  /* select profile */
670
  dbProfile=GWEN_DB_GetFirstGroup(dbProfiles);
671
  while(dbProfile) {
672
    const char *name;
673

    
674
    name=GWEN_DB_GetCharValue(dbProfile, "name", 0, 0);
675
    assert(name);
676
    if (strcasecmp(name, profileName)==0)
677
      break;
678
    dbProfile=GWEN_DB_GetNextGroup(dbProfile);
679
  }
680
  if (!dbProfile) {
681
    DBG_ERROR(AQBANKING_LOGDOMAIN,
682
              "Profile \"%s\" for exporter \"%s\" not found",
683
              profileName, exporterName);
684
    GWEN_DB_Group_free(dbProfiles);
685
    return GWEN_ERROR_NOT_FOUND;
686
  }
687

    
688
  rv=AB_ImExporter_Export(exporter,
689
                          ctx,
690
                          sio,
691
                          dbProfile);
692
  if (rv<0) {
693
    DBG_INFO(AQBANKING_LOGDOMAIN, "here (%d)", rv);
694
    GWEN_DB_Group_free(dbProfiles);
695
    return rv;
696
  }
697

    
698
  GWEN_DB_Group_free(dbProfiles);
699
  return 0;
700
}
701

    
702

    
703

    
704
int AB_Banking_ImportWithProfile(AB_BANKING *ab,
705
                                 const char *importerName,
706
                                 AB_IMEXPORTER_CONTEXT *ctx,
707
                                 const char *profileName,
708
                                 const char *profileFile,
709
                                 GWEN_SYNCIO *sio) {
710
  AB_IMEXPORTER *importer;
711
  GWEN_DB_NODE *dbProfiles;
712
  GWEN_DB_NODE *dbProfile;
713
  int rv;
714

    
715
  importer=AB_Banking_GetImExporter(ab, importerName);
716
  if (!importer) {
717
    DBG_ERROR(AQBANKING_LOGDOMAIN,
718
              "Import module \"%s\" not found",
719
              importerName);
720
    return GWEN_ERROR_NOT_FOUND;
721
  }
722

    
723
  /* get profiles */
724
  if (profileFile) {
725
    dbProfiles=GWEN_DB_Group_new("profiles");
726
    if (GWEN_DB_ReadFile(dbProfiles, profileFile,
727
                         GWEN_DB_FLAGS_DEFAULT |
728
                         GWEN_PATH_FLAGS_CREATE_GROUP)) {
729
      DBG_ERROR(0, "Error reading profiles from \"%s\"",
730
                profileFile);
731
      return GWEN_ERROR_GENERIC;
732
    }
733
  }
734
  else {
735
    dbProfiles=AB_Banking_GetImExporterProfiles(ab, importerName);
736
  }
737

    
738
  /* select profile */
739
  dbProfile=GWEN_DB_GetFirstGroup(dbProfiles);
740
  while(dbProfile) {
741
    const char *name;
742

    
743
    name=GWEN_DB_GetCharValue(dbProfile, "name", 0, 0);
744
    if (!name) {
745
        DBG_ERROR(AQBANKING_LOGDOMAIN,
746
                  "No element \"name\" found in group \"%s\" of file \"%s\" (watch out: --profileFile most probably does not work)",
747
                  GWEN_DB_GroupName(dbProfile),
748
                  profileFile);
749
        GWEN_DB_Group_free(dbProfiles);
750
        return GWEN_ERROR_NOT_FOUND;
751
    }
752
    assert(name);
753
    if (strcasecmp(name, profileName)==0)
754
      break;
755
    dbProfile=GWEN_DB_GetNextGroup(dbProfile);
756
  }
757
  if (!dbProfile) {
758
    DBG_ERROR(AQBANKING_LOGDOMAIN,
759
              "Profile \"%s\" for importer \"%s\" not found",
760
              profileName, importerName);
761
    GWEN_DB_Group_free(dbProfiles);
762
    return GWEN_ERROR_NOT_FOUND;
763
  }
764

    
765
  rv=AB_ImExporter_Import(importer,
766
                          ctx,
767
                          sio,
768
                          dbProfile);
769
  if (rv<0) {
770
    DBG_INFO(AQBANKING_LOGDOMAIN, "here (%d)", rv);
771
    GWEN_DB_Group_free(dbProfiles);
772
    return rv;
773
  }
774

    
775
  GWEN_DB_Group_free(dbProfiles);
776
  return 0;
777
}
778

    
779

    
780

    
781
int AB_Banking_ImportFileWithProfile(AB_BANKING *ab,
782
                                     const char *importerName,
783
                                     AB_IMEXPORTER_CONTEXT *ctx,
784
                                     const char *profileName,
785
                                     const char *profileFile,
786
                                     const char *inputFileName) {
787
  GWEN_SYNCIO *sio;
788
  int rv;
789

    
790
  if (inputFileName) {
791
    sio=GWEN_SyncIo_File_new(inputFileName, GWEN_SyncIo_File_CreationMode_OpenExisting);
792
    GWEN_SyncIo_AddFlags(sio, GWEN_SYNCIO_FILE_FLAGS_READ);
793
    rv=GWEN_SyncIo_Connect(sio);
794
    if (rv<0) {
795
      DBG_INFO(AQBANKING_LOGDOMAIN, "here (%d)", rv);
796
      GWEN_SyncIo_free(sio);
797
      return rv;
798
    }
799
  }
800
  else {
801
    sio=GWEN_SyncIo_File_fromStdin();
802
    GWEN_SyncIo_AddFlags(sio, GWEN_SYNCIO_FLAGS_DONTCLOSE);
803
  }
804

    
805
  rv=AB_Banking_ImportWithProfile(ab,
806
                                  importerName,
807
                                  ctx,
808
                                  profileName,
809
                                  profileFile,
810
                                  sio);
811
  if (rv<0) {
812
    DBG_INFO(AQBANKING_LOGDOMAIN, "here (%d)", rv);
813
    GWEN_SyncIo_Disconnect(sio);
814
    GWEN_SyncIo_free(sio);
815
    return rv;
816
  }
817

    
818
  GWEN_SyncIo_Disconnect(sio);
819
  GWEN_SyncIo_free(sio);
820

    
821
  return 0;
822
}
823

    
824

    
825

    
826
int AB_Banking_ExportToFileWithProfile(AB_BANKING *ab,
827
                                       const char *exporterName,
828
                                       AB_IMEXPORTER_CONTEXT *ctx,
829
                                       const char *profileName,
830
                                       const char *profileFile,
831
                                       const char *outputFileName) {
832
  GWEN_SYNCIO *sio;
833
  int rv;
834

    
835
  if (outputFileName) {
836
    sio=GWEN_SyncIo_File_new(outputFileName, GWEN_SyncIo_File_CreationMode_CreateAlways);
837
    GWEN_SyncIo_AddFlags(sio,
838
                         GWEN_SYNCIO_FILE_FLAGS_READ |
839
                         GWEN_SYNCIO_FILE_FLAGS_WRITE |
840
                         GWEN_SYNCIO_FILE_FLAGS_UREAD |
841
                         GWEN_SYNCIO_FILE_FLAGS_UWRITE |
842
                         GWEN_SYNCIO_FILE_FLAGS_GREAD |
843
                         GWEN_SYNCIO_FILE_FLAGS_GWRITE);
844
    rv=GWEN_SyncIo_Connect(sio);
845
    if (rv<0) {
846
      DBG_INFO(AQBANKING_LOGDOMAIN, "here (%d)", rv);
847
      GWEN_SyncIo_free(sio);
848
      return rv;
849
    }
850
  }
851
  else {
852
    sio=GWEN_SyncIo_File_fromStdout();
853
    GWEN_SyncIo_AddFlags(sio, GWEN_SYNCIO_FLAGS_DONTCLOSE);
854
  }
855

    
856
  rv=AB_Banking_ExportWithProfile(ab,
857
                                  exporterName,
858
                                  ctx,
859
                                  profileName,
860
                                  profileFile,
861
                                  sio);
862
  if (rv<0) {
863
    DBG_INFO(AQBANKING_LOGDOMAIN, "here (%d)", rv);
864
    GWEN_SyncIo_Disconnect(sio);
865
    GWEN_SyncIo_free(sio);
866
    return rv;
867
  }
868

    
869
  GWEN_SyncIo_Disconnect(sio);
870
  GWEN_SyncIo_free(sio);
871

    
872
  return 0;
873
}
874

    
875

    
876

    
877
GWEN_DB_NODE *AB_Banking_GetImExporterProfile(AB_BANKING *ab,
878
                                              const char *imExporterName,
879
                                              const char *profileName) {
880
  GWEN_DB_NODE *dbProfiles;
881

    
882
  dbProfiles=AB_Banking_GetImExporterProfiles(ab, imExporterName);
883
  if (dbProfiles) {
884
    GWEN_DB_NODE *dbProfile;
885

    
886
    dbProfile=GWEN_DB_GetFirstGroup(dbProfiles);
887
    while(dbProfile) {
888
      const char *name;
889

    
890
      name=GWEN_DB_GetCharValue(dbProfile, "name", 0, 0);
891
      assert(name);
892
      if (strcasecmp(name, profileName)==0)
893
        break;
894
      dbProfile=GWEN_DB_GetNextGroup(dbProfile);
895
    }
896
    if (!dbProfile) {
897
      DBG_ERROR(AQBANKING_LOGDOMAIN,
898
                "Profile \"%s\" for exporter \"%s\" not found",
899
                profileName, imExporterName);
900
      GWEN_DB_Group_free(dbProfiles);
901
      return NULL;
902
    }
903

    
904
    GWEN_DB_UnlinkGroup(dbProfile);
905
    GWEN_DB_Group_free(dbProfiles);
906
    return dbProfile;
907
  }
908
  else {
909
    DBG_ERROR(AQBANKING_LOGDOMAIN,
910
              "No profiles found for exporter \"%s\"",
911
              imExporterName);
912
    return NULL;
913
  }
914

    
915
  return NULL;
916
}
917

    
918

    
919