File: documents.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 (627 lines) | stat: -rw-r--r-- 23,679 bytes parent folder | download | duplicates (2)
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
/*
 * 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 <QDebug>
#include <QDesktopServices>
#include <QDialog>
#include <QDir>
#include <QFileInfo>
#include <QMenu>
#include <QMessageBox>
#include <QSettings>
#include <QUrl>

#include <openreports.h>
#include <parameter.h>
#include <xsqlquery.h>
#include <metasql.h>
#include "mqlutil.h"

#include "documents.h"
#include "errorReporter.h"
#include "imageview.h"
#include "imageAssignment.h"
#include "docAttach.h"

QMap<QString, struct DocumentMap*> Documents::_strMap;
QMap<int,     struct DocumentMap*> Documents::_intMap;

/** Add another document type to the map by both key and int.

    Actually there are two parallel maps, one by DocumentSources/integer
    and the other by string key. This simplifies lookups and provides
    backward compatibility with existing UI file definitions.

    @param id    A DocumentSources enum value or a unique extension-supplied int
    @param key   A human-readable abbreviation for the document type
    @param trans A human-readable, translatable term for the document type
    @param param The name of the parameter a window checks for the record id
    @param ui    The application or extension window to create a new record
    @param priv  The privileges required to create a new record

    @return true on success, false if this entry would create a duplicate
            on either id or key.
 */
bool Documents::addToMap(int id,        QString key, QString trans,
                         QString param, QString ui,  QString priv)
{
  if (_strMap.contains(key) || _intMap.contains(id)) {
    qDebug() << "Documents::addToMap(" << id << ", " << key << ") duplicate!";
    return false;
  }

  DocumentMap *entry = new DocumentMap(id, key, trans, param, ui, priv);
  _strMap.insert(key, entry);
  _intMap.insert(id,  entry);
  return true;
}

// Inconsistencies between here and the rest of the app: S? Q?
QMap<QString, struct DocumentMap *> &Documents::documentMap() {
  if (! _x_privileges)
    return _strMap;

  if (_strMap.isEmpty()) {
    XSqlQuery q("SELECT * FROM source;");
    addToMap(Uninitialized,     "",      tr("[Pick a Document Type]")                           );
    while (q.next()) {
      addToMap(q.value("source_docass_num").toInt(),
               q.value("source_docass").toString(),
               tr(q.value("source_descrip").toString().toLatin1().data()),
               q.value("source_key_param").toString(),
               q.value("source_uiform_name").toString(),
               q.value("source_create_priv").toString());
    }
    ErrorReporter::error(QtCriticalMsg, 0, tr("Error Getting Document Types"),
                         q, __FILE__, __LINE__);
  }

  return _strMap;
}

GuiClientInterface* Documents::_guiClientInterface = 0;

Documents::Documents(QWidget *pParent) :
  QWidget(pParent)
{
  setupUi(this);

  _sourceid = -1;
  _readOnly = false;
  if (_strMap.isEmpty()) {
    (void)documentMap();
  }

  _doc->addColumn(tr("Type"),  _itemColumn,  Qt::AlignLeft, true, "target_type" );
  _doc->addColumn(tr("Number"), _itemColumn, Qt::AlignLeft, true, "target_number" );
  _doc->addColumn(tr("Name"), -1,  Qt::AlignLeft, true, "name" );
  _doc->addColumn(tr("Description"),  -1, Qt::AlignLeft, true, "description");
  _doc->addColumn(tr("Relationship"),  _itemColumn, Qt::AlignLeft,true, "purpose");
  _doc->addColumn(tr("Can View"), _ynColumn, Qt::AlignCenter, false, "canview");
  _doc->addColumn(tr("Can Edit"), _ynColumn, Qt::AlignCenter, false, "canedit");
  
  connect(_attachDoc, SIGNAL(clicked()), this, SLOT(sAttachDoc()));
  connect(_editDoc, SIGNAL(clicked()), this, SLOT(sEditDoc()));
  connect(_viewDoc, SIGNAL(clicked()), this, SLOT(sViewDoc()));
  connect(_detachDoc, SIGNAL(clicked()), this, SLOT(sDetachDoc()));
  connect(_doc, SIGNAL(valid(bool)), this, SLOT(handleSelection()));
  connect(_doc, SIGNAL(itemSelected(int)), this, SLOT(handleItemSelected()));
  handleSelection();

  if (_x_privileges)
  {
    QMenu * newDocMenu = new QMenu;

    QAction* imgAct = new QAction(tr("Image"), this);
    imgAct->setEnabled(_x_privileges->check("MaintainImages"));
    connect(imgAct, SIGNAL(triggered()), this, SLOT(sNewImage()));
    newDocMenu->addAction(imgAct);

    foreach (DocumentMap *value, _strMap) {
      if (! value->newPriv.isEmpty()) {
        QAction* act = new QAction(value->translation, this);
        QStringList data;
        data << value->doctypeStr << value->uiname;
        act->setEnabled(_x_privileges->check(value->newPriv));
        act->setData(data);
        connect(act, SIGNAL(triggered()), this, SLOT(sNewDoc()));
        newDocMenu->addAction(act);
      }
    }

    _newDoc->setMenu(newDocMenu);
  }
}

int Documents::type() const
{
  DocumentMap *elem = _strMap.value(_sourcetype);
  return elem ? elem->doctypeId : Uninitialized;
}

void Documents::setType(int sourceType)
{
  DocumentMap *elem = _intMap.value(sourceType);
  setType(elem ? elem->doctypeStr : "");
}

void Documents::setType(QString sourceType)
{
  _sourcetype = sourceType;
}

void Documents::setId(int pSourceid)
{
  _sourceid = pSourceid;
  refresh();
}

void Documents::setReadOnly(bool pReadOnly)
{
  _readOnly = pReadOnly;

  _newDoc->setEnabled(!pReadOnly);
  _attachDoc->setEnabled(!pReadOnly);
  _editDoc->setEnabled(!pReadOnly);
  _detachDoc->setEnabled(!pReadOnly);

  handleSelection();
}

void Documents::sNewDoc(QString ptype, QString pui)
{
  QString  type = ptype;
  QString  ui   = pui;
  QAction *act = qobject_cast<QAction*>(sender());
  if (act) {
    QStringList elem = act->data().toStringList();
    type = elem.at(0);
    ui   = elem.at(1);
  }

  ParameterList params;
  params.append("mode", "new");
  int target_id = -1;
  QWidget *window = _guiClientInterface->openWindow(ui, params, parentWidget(), Qt::WindowModal, Qt::Dialog);
  QDialog *newdlg = qobject_cast<QDialog*>(window);
  if (newdlg)
  {
    target_id = newdlg->exec();
    if (target_id != QDialog::Rejected) {
      sInsertDocass(type, target_id);
    }
  }
  else if (window)
  {
    window->show();
    // TODO: how do we get the ID and when?
    // sInsertDocass(type, target_id);
  }
  else
  {
    QMessageBox::critical(this, tr("Error Creating Document"),
                          tr("Cannot find the '%1'' window to create a %2").arg(ui, type));
  }
  refresh();
}

void Documents::sNewImage()
{
  ParameterList params;
  params.append("sourceType", _sourcetype);
  params.append("source_id", _sourceid);

  imageAssignment newdlg(this, "", true);
  newdlg.set(params);

  if (newdlg.exec() != QDialog::Rejected)
    refresh();
}

void Documents::sInsertDocass(QString target_type, int target_id)
{
  XSqlQuery ins;
  ins.prepare("INSERT INTO docass ( docass_source_id, docass_source_type, docass_target_id, docass_target_type )"
            "  VALUES ( :sourceid, :sourcetype::text, :targetid, :targettype); ");
  ins.bindValue(":sourceid", _sourceid);
  ins.bindValue(":sourcetype", _sourcetype);
  ins.bindValue(":targetid", target_id);
  ins.bindValue(":targettype", target_type);
  ins.exec();
  ErrorReporter::error(QtCriticalMsg, this, tr("Attachment Error"),
                       ins, __FILE__, __LINE__);
}

void Documents::sEditDoc()
{
  sOpenDoc("edit");
}

void Documents::sOpenDoc(QString mode)
{
  QString ui;
  QString docType = _doc->currentItem()->rawValue("target_type").toString();
  int targetid = _doc->currentItem()->id("target_number");
  ParameterList params;
  if (docType == "Q" && mode == "view")
    params.append("mode", "viewQuote");
  else if (docType == "Q" && mode == "edit")
    params.append("mode", "editQuote");
  else
    params.append("mode", mode);

  // TODO: image -- change to use docass instead of imageass
  if (docType == "IMG")
  {
    XSqlQuery img;
    img.prepare("SELECT imageass_image_id "
                "FROM imageass "
                "WHERE (imageass_id=:imageass_id); ");
    img.bindValue(":imageass_id", _doc->id());
    img.exec();
    img.first();
    if (ErrorReporter::error(QtCriticalMsg, this, tr("Error Getting Image Info"),
                             img, __FILE__, __LINE__))
      return;

    params.append("image_id", img.value("imageass_image_id").toInt());
    imageview newdlg(this, "", true);
    newdlg.set(params);

    if (newdlg.exec() != QDialog::Rejected)
      refresh();
    return;
  }
  // TODO: url -- change to use docass instead of url
  // TODO: separate URL from FILE handling and replace use of url view
  else if (docType == "URL" || docType == "FILE")
  {
    if (mode == "edit")
    {
      ParameterList params;
      params.append("url_id", targetid);

      docAttach newdlg(this, "", true);
      newdlg.set(params);
      newdlg.exec();

      refresh();
      return;
    }

    XSqlQuery qfile;
    qfile.prepare("SELECT url_id, url_source_id, url_source, url_title, url_url, url_stream"
                  " FROM url"
                  " WHERE (url_id=:url_id);");

    qfile.bindValue(":url_id", _doc->id());
    qfile.exec();

    // If file is in the database, copy to a temp. directory in the file system and open it.
    if (qfile.first() && (docType == "FILE"))
    {
      QFileInfo fi( qfile.value("url_url").toString() );
      QDir tdir;
      // TODO: QDesktopServices::openUrl(urldb) on windows does not open files
      // containing spaces. why not?
#ifdef Q_OS_WIN
      QString fileName = fi.fileName().remove(" ");
#else
      QString fileName = fi.fileName();
#endif
      QString filePath = tdir.tempPath() + "/xtTempDoc/" +
	                 qfile.value("url_id").toString() + "/";
      QFile tfile(filePath + fileName);

      // Remove any previous watches
      if (_guiClientInterface)
        _guiClientInterface->removeDocumentWatch(tfile.fileName());

      if (! tdir.exists(filePath))
        tdir.mkpath(filePath);

      if (!tfile.open(QIODevice::WriteOnly))
      {
        QMessageBox::warning( this, tr("File Open Error"),
                             tr("Could Not Create File %1.").arg(tfile.fileName()) );
        return;
      }
      tfile.write(qfile.value("url_stream").toByteArray());
      QUrl urldb;
      urldb.setUrl(tfile.fileName());
#ifndef Q_OS_WIN
      urldb.setScheme("file");
#endif
      tfile.close();
      if (! QDesktopServices::openUrl(urldb))
      {
        QMessageBox::warning(this, tr("File Open Error"),
			     tr("Could not open %1.").arg(urldb.toString()));
	return;
      }

      // Add a watch to the file that will save any changes made to the file back to the database.
      if (_guiClientInterface && !_readOnly) // TODO: only if NOT read-only
        _guiClientInterface->addDocumentWatch(tfile.fileName(),qfile.value("url_id").toInt());
      return;
    }
    else if (ErrorReporter::error(QtCriticalMsg, this,
                                  tr("Error Getting Assignment"),
                                  qfile, __FILE__, __LINE__))
      return;
    else
    {
      QUrl url(_doc->currentItem()->rawValue("description").toString());
      if (url.scheme().isEmpty())
        url.setScheme("file");
      QDesktopServices::openUrl(url);
      return;
    }
  }
  else
  {
    struct DocumentMap *elem = _strMap.value(docType);
    if (! elem) {
      QMessageBox::critical(this, tr("Error"),
                            tr("Unknown document type %1").arg(docType));
      return;
    }

    params.append(elem->idParam, targetid);
    ui = elem->uiname;
  }

  QWidget* w = 0;
  if (parentWidget()->window())
  {
    if (parentWidget()->window()->isModal())
      w = _guiClientInterface->openWindow(ui, params, parentWidget()->window(),
                                          Qt::WindowModal, Qt::Dialog);
    else
      w = _guiClientInterface->openWindow(ui, params, parentWidget()->window(),
                                          Qt::NonModal, Qt::Window);
  }

  if (w && w->inherits("QDialog"))
  {
    QDialog* newdlg = qobject_cast<QDialog*>(w);
    newdlg->exec();
  }

  refresh();
}

void Documents::sViewDoc()
{
  sOpenDoc("view");
}

void Documents::sAttachDoc()
{
  ParameterList params;
  params.append("sourceType", _sourcetype);
  params.append("source_id", _sourceid);

  docAttach newdlg(this, "", true);
  newdlg.set(params);
  newdlg.exec();

  refresh();
}

void Documents::sDetachDoc()
{
  XSqlQuery detach;
  if (_doc->id() < 0)
    return;

  if(QMessageBox::question( this, tr("Confirm Detach"),
       tr("<p>You have requested to detach the selected document."
          " In some cases this may permanently remove the document from the system.</p>"
          "<p>Are you sure you want to continue?</p>"), QMessageBox::Yes|QMessageBox::No, QMessageBox::No) == QMessageBox::No)
    return; // user doesn't want to continue so get out of here

  if ( _doc->currentItem()->rawValue("target_type") == "IMG" )
  {
    detach.prepare( "DELETE FROM imageass "
                    "WHERE (imageass_id = :docid );" );
  }
  else if ( _doc->currentItem()->rawValue("target_type") == "URL" ||
            _doc->currentItem()->rawValue("target_type") == "FILE")
  {
    detach.prepare( "DELETE FROM url "
                    "WHERE (url_id = :docid );" );
  }
  else
  {
    detach.prepare( "DELETE FROM docass "
                    "WHERE (docass_id = :docid );" );
  }
  detach.bindValue(":docid", _doc->id());
  detach.exec();
  ErrorReporter::error(QtCriticalMsg, this, tr("Error Detaching"),
                       detach, __FILE__, __LINE__);
  refresh();
}

void Documents::refresh()
{
  if(-1 == _sourceid)
  {
    _doc->clear();
    return;
  }

  XSqlQuery query;
  
  //Populate doc list
  QString sql("SELECT id, target_number, target_type, "
              " target_id AS target_number_xtidrole, source_type, source_id, purpose, "
              " name, description, "
              " hasPrivOnObject('view', target_type, target_id) AS canview,"
              " hasPrivOnObject('edit', target_type, target_id) AS canedit,"
              " CASE WHEN (purpose='I') THEN :inventory"
              " WHEN (purpose='P') THEN :product"
              " WHEN (purpose='E') THEN :engineering"
              " WHEN (purpose='M') THEN :misc"
              " WHEN (purpose='A') THEN :child"
              " WHEN (purpose='C') THEN :parent"
              " WHEN (purpose='S') THEN :sibling"
              " WHEN (purpose='D') THEN :dupe"
              " ELSE :other"
              " END AS purpose_qtdisplayrole, "
              " CASE "
              " WHEN (target_type='ADDR') THEN :address "
              " WHEN (target_type='BBH') THEN :bbomhead "
              " WHEN (target_type='BBI') THEN :bbomitem "
              " WHEN (target_type='BMH') THEN :bomhead "
              " WHEN (target_type='BMI') THEN :bomitem "
              " WHEN (target_type='BOH') THEN :boohead "
              " WHEN (target_type='BOI') THEN :booitem "
              " WHEN (target_type='CRMA') THEN :crma "
              " WHEN (target_type='T') THEN :contact "
              " WHEN (target_type='CNTR') THEN :contract "
              " WHEN (target_type='CM') THEN :creditmemo "
              " WHEN (target_type='CMI') THEN :creditmemoitem "
              " WHEN (target_type='C') THEN :cust "
              " WHEN (target_type='EMP') THEN :emp "
              " WHEN (target_type='INCDT') THEN :incident "
              " WHEN (target_type='INV') THEN :invoice "
              " WHEN (target_type='INVI') THEN :invoiceitem "
              " WHEN (target_type='I') THEN :item "
              " WHEN (target_type='IS') THEN :itemsite "
              " WHEN (target_type='IR') THEN :itemsrc "
              " WHEN (target_type='L') THEN :location "
              " WHEN (target_type='LS') THEN :lotserial "
              " WHEN (target_type='OPP') THEN :opp "
              " WHEN (target_type='J') THEN :project "
              " WHEN (target_type='P') THEN :po "
              " WHEN (target_type='PI') THEN :poitem "
              " WHEN (target_type='RA') THEN :ra "
              " WHEN (target_type='RI') THEN :raitem "
              " WHEN (target_type='Q') THEN :quote "
              " WHEN (target_type='QI') THEN :quoteitem "
              " WHEN (target_type='S') THEN :so "
              " WHEN (target_type='SI') THEN :soitem "
              " WHEN (target_type='SHP') THEN :shipto "
              " WHEN (target_type='TE') THEN :timeexpense "
              " WHEN (target_type='TODO') THEN :todo "
              " WHEN (target_type='TO') THEN :to "
              " WHEN (target_type='TI') THEN :toitem "
              " WHEN (target_type='V') THEN :vendor "
              " WHEN (target_type='VCH') THEN :voucher "
              " WHEN (target_type='WH') THEN :whse "
              " WHEN (target_type='W') THEN :wo "
              " WHEN (target_type='TASK') THEN :projecttask "
              " WHEN (target_type='URL') THEN :url "
              " WHEN (target_type='FILE') THEN :file "
              " WHEN (target_type='IMG') THEN :image "
              " ELSE NULL "
              " END AS target_type_qtdisplayrole "
              " FROM _docinfo(:sourceid, :source)"
              "ORDER by target_type_qtdisplayrole, target_number; ");
  query.prepare(sql);
  query.bindValue(":inventory", tr("Inventory Description"));
  query.bindValue(":product", tr("Product Description"));
  query.bindValue(":engineering", tr("Engineering Reference"));
  query.bindValue(":misc", tr("Miscellaneous"));

  query.bindValue(":parent", tr("Parent"));
  query.bindValue(":child", tr("Child"));
  query.bindValue(":sibling", tr("Related to"));
  query.bindValue(":dupe", tr("Duplicate of"));
  query.bindValue(":other", tr("Other"));

  query.bindValue(":address",        _strMap.contains("ADDR") ? _strMap.value("ADDR")->translation : "ADDR");
  query.bindValue(":bbomhead",       _strMap.contains("BBH") ? _strMap.value("BBH")->translation : "BBH");
  query.bindValue(":bbomitem",       _strMap.contains("BBI") ? _strMap.value("BBI")->translation : "BBI");
  query.bindValue(":bomhead",        _strMap.contains("BMH") ? _strMap.value("BMH")->translation : "BMH");
  query.bindValue(":bomitem",        _strMap.contains("BMI") ? _strMap.value("BMI")->translation : "BMI");
  query.bindValue(":boohead",        _strMap.contains("BOH") ? _strMap.value("BOH")->translation : "BOH");
  query.bindValue(":booitem",        _strMap.contains("BOI") ? _strMap.value("BOI")->translation : "BOI");
  query.bindValue(":crma",           _strMap.contains("CRMA") ? _strMap.value("CRMA")->translation : "CRMA");
  query.bindValue(":contact",        _strMap.contains("T") ? _strMap.value("T")->translation : "T");
  query.bindValue(":contract",       _strMap.contains("CNTR") ? _strMap.value("CNTR")->translation : "CNTR");
  query.bindValue(":creditmemo",     _strMap.contains("CM") ? _strMap.value("CM")->translation : "CM");
  query.bindValue(":creditmemoitem", _strMap.contains("CMI") ? _strMap.value("CMI")->translation : "CMI");
  query.bindValue(":cust",           _strMap.contains("C") ? _strMap.value("C")->translation : "C");
  query.bindValue(":emp",            _strMap.contains("EMP") ? _strMap.value("EMP")->translation : "EMP");
  query.bindValue(":incident",       _strMap.contains("INCDT") ? _strMap.value("INCDT")->translation : "INCDT");
  query.bindValue(":invoice",        _strMap.contains("INV") ? _strMap.value("INV")->translation : "INV");
  query.bindValue(":invoiceitem",    _strMap.contains("INVI") ? _strMap.value("INVI")->translation : "INVI");
  query.bindValue(":item",           _strMap.contains("I") ? _strMap.value("I")->translation : "I");
  query.bindValue(":itemsite",       _strMap.contains("IS") ? _strMap.value("IS")->translation : "IS");
  query.bindValue(":itemsrc",        _strMap.contains("IR") ? _strMap.value("IR")->translation : "IR");
  query.bindValue(":location",       _strMap.contains("L") ? _strMap.value("L")->translation : "L");
  query.bindValue(":lotserial",      _strMap.contains("LS") ? _strMap.value("LS")->translation : "LS");
  query.bindValue(":opp",            _strMap.contains("OPP") ? _strMap.value("OPP")->translation : "OPP");
  query.bindValue(":project",        _strMap.contains("J") ? _strMap.value("J")->translation : "J");
  query.bindValue(":po",             _strMap.contains("P") ? _strMap.value("P")->translation : "P");
  query.bindValue(":poitem",         _strMap.contains("PI") ? _strMap.value("PI")->translation : "PI");
  query.bindValue(":ra",             _strMap.contains("RA") ? _strMap.value("RA")->translation : "RA");
  query.bindValue(":raitem",         _strMap.contains("RI") ? _strMap.value("RI")->translation : "RI");
  query.bindValue(":quote",          _strMap.contains("Q") ? _strMap.value("Q")->translation : "Q");
  query.bindValue(":quoteitem",      _strMap.contains("QI") ? _strMap.value("QI")->translation : "QI");
  query.bindValue(":so",             _strMap.contains("S") ? _strMap.value("S")->translation : "S");
  query.bindValue(":soitem",         _strMap.contains("SI") ? _strMap.value("SI")->translation : "SI");
  query.bindValue(":shipto",         _strMap.contains("SHP") ? _strMap.value("SHP")->translation : "SHP");
  query.bindValue(":timeexpense",    _strMap.contains("TE") ? _strMap.value("TE")->translation : "TE");
  query.bindValue(":todo",           _strMap.contains("TODO") ? _strMap.value("TODO")->translation : "TODO");
  query.bindValue(":to",             _strMap.contains("TO") ? _strMap.value("TO")->translation : "TO");
  query.bindValue(":toitem",         _strMap.contains("TI") ? _strMap.value("TI")->translation : "TI");
  query.bindValue(":vendor",         _strMap.contains("V") ? _strMap.value("V")->translation : "V");
  query.bindValue(":voucher",        _strMap.contains("VCH") ? _strMap.value("VCH")->translation : "VCH");
  query.bindValue(":whse",           _strMap.contains("WH") ? _strMap.value("WH")->translation : "WH");
  query.bindValue(":wo",             _strMap.contains("W") ? _strMap.value("W")->translation : "W");
  query.bindValue(":projecttask",    _strMap.contains("TASK") ? _strMap.value("TASK")->translation : "TASK");

  query.bindValue(":image", tr("Image"));
  query.bindValue(":url", tr("URL"));
  query.bindValue(":file", tr("File"));

  query.bindValue(":source",   _sourcetype);
  query.bindValue(":sourceid", _sourceid);
  query.exec();
  _doc->populate(query,true);
  ErrorReporter::error(QtCriticalMsg, this, tr("Error Getting Documents"),
                       query, __FILE__, __LINE__);
}

void Documents::handleSelection(bool /*pReadOnly*/)
{
  if (_doc->selectedItems().count() &&
      (_doc->currentItem()->rawValue("target_type").toString() == "URL" ||
       _doc->currentItem()->rawValue("target_type").toString() == "FILE" ))
  {
    _viewDoc->setText(tr("Open"));
  }
  else
  {
    _viewDoc->setText(tr("View"));
  }

  bool valid = (_doc->selectedItems().count() > 0);
  _editDoc->setEnabled(valid && !_readOnly && _doc->currentItem()->rawValue("canedit").toBool());
  _viewDoc->setEnabled(valid && _doc->currentItem()->rawValue("canview").toBool());
}

void Documents::handleItemSelected()
{
  if(_readOnly ||
     (_doc->currentItem()->rawValue("target_type").toString() == "URL" ||
      _doc->currentItem()->rawValue("target_type").toString() == "FILE" ))
  {
    _viewDoc->animateClick();
  }
  else
  {
    _editDoc->animateClick();
  }
}