File: configureIE.cpp

package info (click to toggle)
postbooks 4.10.0-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 112,660 kB
  • ctags: 22,890
  • sloc: cpp: 310,358; sh: 607; xml: 214; python: 140; awk: 104; makefile: 50
file content (360 lines) | stat: -rw-r--r-- 12,943 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
 * This file is part of the xTuple ERP: PostBooks Edition, a free and
 * open source Enterprise Resource Planning software suite,
 * Copyright (c) 1999-2014 by OpenMFG LLC, d/b/a xTuple.
 * It is licensed to you under the Common Public Attribution License
 * version 1.0, the full text of which (including xTuple-specific Exhibits)
 * is available at www.xtuple.com/CPAL.  By using this software, you agree
 * to be bound by its terms.
 */

#include "configureIE.h"

#include <QMenu>
#include <QMessageBox>
#include <QSqlError>
#include <QVariant>

#include "storedProcErrorLookup.h"
#include "atlasMap.h"
#include "xsltMap.h"
#include "errorReporter.h"

bool configureIE::userHasPriv()
{
  return _privileges->check("ConfigureImportExport");
}

configureIE::configureIE(QWidget* parent, const char* name, bool /*modal*/, Qt::WindowFlags fl)
    : XAbstractConfigure(parent, fl)
{
  setupUi(this);

  if (name)
    setObjectName(name);

  connect(_atlasMap, SIGNAL(itemDoubleClicked(QTreeWidgetItem*,int)), this, SLOT(sEditAtlasMap()));
  connect(_atlasMap,   SIGNAL(populateMenu(QMenu*,QTreeWidgetItem*)), this, SLOT(sPopulateMenu(QMenu*,QTreeWidgetItem*)));
  connect(_deleteAtlasMap, SIGNAL(clicked()), this, SLOT(sDeleteAtlasMap()));
  connect(_deleteMap,      SIGNAL(clicked()), this, SLOT(sDeleteMap()));
  connect(_editAtlasMap,   SIGNAL(clicked()), this, SLOT(sEditAtlasMap()));
  connect(_editMap,        SIGNAL(clicked()), this, SLOT(sEditMap()));
  connect(_map,      SIGNAL(itemDoubleClicked(QTreeWidgetItem*,int)), this, SLOT(sEditMap()));
  connect(_map,        SIGNAL(populateMenu(QMenu*,QTreeWidgetItem*)), this, SLOT(sPopulateMenu(QMenu*,QTreeWidgetItem*)));
  connect(_newAtlasMap, SIGNAL(clicked()), this, SLOT(sNewAtlasMap()));
  connect(_newMap,      SIGNAL(clicked()), this, SLOT(sNewMap()));

  _map->addColumn(tr("Name"),             -1, Qt::AlignLeft, true, "xsltmap_name");
  _map->addColumn(tr("Document Type"),    -1, Qt::AlignLeft, true, "xsltmap_doctype");
  _map->addColumn(tr("System Identifier"),-1, Qt::AlignLeft, true, "xsltmap_system");
  _map->addColumn(tr("Import XSLT File"), -1, Qt::AlignLeft, true, "xsltmap_import");
  _map->addColumn(tr("Export XSLT File"), -1, Qt::AlignLeft, true, "xsltmap_export");

  _atlasMap->addColumn(tr("Name"),       -1, Qt::AlignLeft, true, "atlasmap_name");
  _atlasMap->addColumn(tr("Type"),       -1, Qt::AlignLeft, true, "atlasmap_filtertype");
  _atlasMap->addColumn(tr("Matches"),    -1, Qt::AlignLeft, true, "atlasmap_filter");
  _atlasMap->addColumn(tr("Atlas File"), -1, Qt::AlignLeft, true, "atlasmap_atlas");
  _atlasMap->addColumn(tr("CSV Map"),    -1, Qt::AlignLeft, true, "atlasmap_map");

  // TODO: fix these when support for an internal XSLT processor is enabled
  _internal->setEnabled(false);
  _internal->setVisible(false);
  _external->setVisible(false);

#ifdef Q_OS_WIN
  _os->setCurrentIndex(1);
#endif
#ifdef Q_OS_MAC
  _os->setCurrentIndex(2);
#endif

  sPopulate();
}

configureIE::~configureIE()
{
  // no need to delete child widgets, Qt does it all for us
}

void configureIE::languageChange()
{
  retranslateUi(this);
}

bool configureIE::sSave()
{
  emit saving();

  if (_importErrorXMLHandling->isChecked() &&
      _importErrorSelector->option() != FileMoveSelector::Suffix &&
      _importErrorSelector->option() != FileMoveSelector::ChangeDir)
  {
    QMessageBox::critical(this, tr("Inconsistent Selection"),
                          tr("<p>Please choose to add a suffix after errors, "
                             "move the import file after errors, or "
                             "uncheck '%1'.")
                          .arg(_importErrorXMLHandling->text()));
    return false;
  }

  _metrics->set("XMLSuccessTreatment",  _importSuccessSelector->code());

  if (_internal->isChecked())
    _metrics->set("XSLTLibrary",          _internal->isChecked());
  else if (_external->isChecked())
  {
    if (_linuxCmd->text().isEmpty() && _macCmd->text().isEmpty() &&
        _windowsCmd->text().isEmpty() &&
        QMessageBox::question(this, tr("Incomplete Data"),
                              tr("<p>XSLT is not configured. Some XML imports may fail. "
                                 "Would you like to fix this now?</p>"),
                              QMessageBox::Yes | QMessageBox::Default,
                              QMessageBox::No) == QMessageBox::Yes)
    {
      _tabs->setCurrentIndex(_tabs->indexOf(_mappingTab));
      _linuxCmd->setFocus();
      return false;
    }
    _metrics->set("XSLTLibrary",        ! _external->isChecked());
  }
  else
  {
    QMessageBox::critical(this, tr("Incomplete Data"),
                          tr("<p>Please choose whether to use the internal "
                             "XSLT processor or an external XSLT processor."));
    _tabs->setCurrentIndex(_tabs->indexOf(_importTab));
    _internal->setFocus();
    return false;
  }

  _metrics->set("XSLTDefaultDirLinux",    _xsltLinuxDir->text());
  _metrics->set("XSLTDefaultDirMac",      _xsltMacDir->text());
  _metrics->set("XSLTDefaultDirWindows",  _xsltWindowsDir->text());

  _metrics->set("XSLTProcessorLinux",     _linuxCmd->text());
  _metrics->set("XSLTProcessorMac",       _macCmd->text());
  _metrics->set("XSLTProcessorWindows",   _windowsCmd->text());

  _metrics->set("XMLDefaultDirLinux",          _importLinuxDir->text());
  _metrics->set("XMLDefaultDirMac",            _importMacDir->text());
  _metrics->set("XMLDefaultDirWindows",        _importWindowsDir->text());

  _metrics->set("CSVAtlasDefaultDirLinux",      _atlasLinuxDir->text());
  _metrics->set("CSVAtlasDefaultDirMac",        _atlasMacDir->text());
  _metrics->set("CSVAtlasDefaultDirWindows",    _atlasWindowsDir->text());

  _metrics->set("XMLSuccessSuffix",       _importSuccessSelector->suffix());
  _metrics->set("XMLSuccessDir",          _importSuccessSelector->destdir());

  _metrics->set("ImportFailureTreatment", _importErrorSelector->code());
  _metrics->set("ImportFailureSuffix",    _importErrorSelector->suffix());
  _metrics->set("ImportFailureDir",       _importErrorSelector->destdir());

  _metrics->set("ImportXMLCreateErrorFile", _importErrorXMLHandling->isChecked());

  _metrics->set("XMLExportDefaultDirLinux",    _exportLinuxDir->text());
  _metrics->set("XMLExportDefaultDirMac",      _exportMacDir->text());
  _metrics->set("XMLExportDefaultDirWindows",  _exportWindowsDir->text());

  return true;
}

void configureIE::sPopulate()
{
  _xsltLinuxDir->setText(_metrics->value("XSLTDefaultDirLinux"));
  _xsltMacDir->setText(_metrics->value("XSLTDefaultDirMac"));
  _xsltWindowsDir->setText(_metrics->value("XSLTDefaultDirWindows"));

  //  TODO: start using Qt's XSLT processor
  _external->setChecked(true);

  _linuxCmd->setText(_metrics->value("XSLTProcessorLinux"));
  _macCmd->setText(_metrics->value("XSLTProcessorMac"));
  _windowsCmd->setText(_metrics->value("XSLTProcessorWindows"));

  _importLinuxDir->setText(_metrics->value("XMLDefaultDirLinux"));
  _importMacDir->setText(_metrics->value("XMLDefaultDirMac"));
  _importWindowsDir->setText(_metrics->value("XMLDefaultDirWindows"));

  _atlasLinuxDir->setText(_metrics->value("CSVAtlasDefaultDirLinux"));
  _atlasMacDir->setText(_metrics->value("CSVAtlasDefaultDirMac"));
  _atlasWindowsDir->setText(_metrics->value("CSVAtlasDefaultDirWindows"));

  _importSuccessSelector->setCode(_metrics->value("XMLSuccessTreatment"));
  _importSuccessSelector->setDestdir(_metrics->value("XMLSuccessDir"));
  _importSuccessSelector->setSuffix(_metrics->value("XMLSuccessSuffix"));

  _importErrorSelector->setCode(_metrics->value("ImportFailureTreatment"));
  _importErrorSelector->setDestdir(_metrics->value("ImportFailureDir"));
  _importErrorSelector->setSuffix(_metrics->value("ImportFailureSuffix"));

  _importErrorXMLHandling->setChecked(_metrics->boolean("ImportXMLCreateErrorFile"));

  _exportLinuxDir->setText(_metrics->value("XMLExportDefaultDirLinux"));
  _exportMacDir->setText(_metrics->value("XMLExportDefaultDirMac"));
  _exportWindowsDir->setText(_metrics->value("XMLExportDefaultDirWindows"));

  sFillList();
}

void configureIE::sFillList()
{
  XSqlQuery xsltq("SELECT * FROM xsltmap ORDER BY xsltmap_name;");
  xsltq.exec();
  _map->populate(xsltq);
  if (ErrorReporter::error(QtCriticalMsg, this, tr("Error Retrieving Import/Export Setting Information"),
                                xsltq, __FILE__, __LINE__))
  {
    return;
  }

  XSqlQuery atlasq("SELECT * FROM atlasmap ORDER BY atlasmap_name;");
  atlasq.exec();
  _atlasMap->populate(atlasq);
  if (ErrorReporter::error(QtCriticalMsg, this, tr("Error Retrieving Import/Export Setting Information"),
                                atlasq, __FILE__, __LINE__))
  {
    return;
  }
}

void configureIE::sDeleteAtlasMap()
{
  if (QMessageBox::question(this, tr("Delete Map?"),
                    tr("Are you sure you want to delete this CSV Atlas Map?"),
                    QMessageBox::Yes | QMessageBox::No,
                    QMessageBox::No) == QMessageBox::Yes)
  {
    XSqlQuery delq;
    delq.prepare("DELETE FROM atlasmap WHERE (atlasmap_id=:mapid);");
    delq.bindValue(":mapid", _atlasMap->id());
    delq.exec();
    if (ErrorReporter::error(QtCriticalMsg, this, tr("Error Deleting Map"),
                                  delq, __FILE__, __LINE__))
    {
      return;
    }

    sFillList();
  }
}

void configureIE::sDeleteMap()
{
  if (QMessageBox::question(this, tr("Delete Map?"),
                    tr("Are you sure you want to delete this XSLT Map?"),
                    QMessageBox::Yes | QMessageBox::No,
                    QMessageBox::No) == QMessageBox::Yes)
  {
    XSqlQuery delq;
    delq.prepare("DELETE FROM xsltmap WHERE (xsltmap_id=:mapid);");
    delq.bindValue(":mapid", _map->id());
    delq.exec();
    if (ErrorReporter::error(QtCriticalMsg, this, tr("Error Deleting Map"),
                                  delq, __FILE__, __LINE__))
    {
      return;
    }

    sFillList();
  }
}

void configureIE::sEditAtlasMap()
{
  ParameterList params;
  params.append("mode",        "edit");
  params.append("atlasmap_id", _atlasMap->id());
#if defined Q_OS_MAC
  params.append("defaultDir",  _atlasMacDir->text());
#elif defined Q_OS_WIN
  params.append("defaultDir",  _atlasWindowsDir->text());
#elif defined Q_OS_LINUX
  params.append("defaultDir",  _atlasLinuxDir->text());
#endif

  atlasMap newdlg(this);
  if (newdlg.set(params) == NoError && newdlg.exec() == XDialog::Accepted)
    sFillList();
}

void configureIE::sEditMap()
{
  ParameterList params;
  params.append("mode",       "edit");
  params.append("xsltmap_id", _map->id());
#if defined Q_OS_MAC
  params.append("defaultDir", _atlasMacDir->text());
#elif defined Q_OS_WIN
  params.append("defaultDir", _atlasWindowsDir->text());
#elif defined Q_OS_X11
  params.append("defaultDir", _atlasLinuxDir->text());
#endif

  xsltMap newdlg(this);
  if (newdlg.set(params) == NoError && newdlg.exec() == XDialog::Accepted)
    sFillList();
}

void configureIE::sNewAtlasMap()
{
  ParameterList params;
  params.append("mode",       "new");
#if defined Q_OS_MAC
  params.append("defaultDir", _atlasMacDir->text());
#elif defined Q_OS_WIN
  params.append("defaultDir", _atlasWindowsDir->text());
#elif defined Q_OS_LINUX
  params.append("defaultDir", _atlasLinuxDir->text());
#endif

  atlasMap newdlg(this);
  if (newdlg.set(params) == NoError && newdlg.exec() == XDialog::Accepted)
    sFillList();
}

void configureIE::sNewMap()
{
  ParameterList params;
  params.append("mode",       "new");
#if defined Q_OS_MAC
  params.append("defaultDir", _atlasMacDir->text());
#elif defined Q_OS_WIN
  params.append("defaultDir", _atlasWindowsDir->text());
#elif defined Q_OS_LINUX
  params.append("defaultDir", _atlasLinuxDir->text());
#endif

  xsltMap newdlg(this);
  if (newdlg.set(params) == NoError && newdlg.exec() == XDialog::Accepted)
    sFillList();
}

void configureIE::sPopulateMenu(QMenu* pMenu, QTreeWidgetItem* /* pItem */)
{

  QAction *newAct  = pMenu->addAction(tr("New..."));
  QAction *editAct = pMenu->addAction(tr("Edit..."));
  QAction *delAct  = pMenu->addAction(tr("Delete"));

  if (sender() == _map)
  {
    newAct->setEnabled(xsltMap::userHasPriv());
    editAct->setEnabled(xsltMap::userHasPriv());
    delAct->setEnabled(xsltMap::userHasPriv());

    connect(newAct,  SIGNAL(triggered()), this, SLOT(sNewMap()));
    connect(editAct, SIGNAL(triggered()), this, SLOT(sEditMap()));
    connect(delAct,  SIGNAL(triggered()), this, SLOT(sDeleteMap()));
  }
  else if (sender() == _atlasMap)
  {
    newAct->setEnabled(atlasMap::userHasPriv());
    editAct->setEnabled(atlasMap::userHasPriv());
    delAct->setEnabled(atlasMap::userHasPriv());

    connect(newAct,  SIGNAL(triggered()), this, SLOT(sNewAtlasMap()));
    connect(editAct, SIGNAL(triggered()), this, SLOT(sEditAtlasMap()));
    connect(delAct,  SIGNAL(triggered()), this, SLOT(sDeleteAtlasMap()));
  }
}