File: contactemail.cpp

package info (click to toggle)
postbooks 4.9.5-1~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 103,120 kB
  • sloc: cpp: 288,269; sh: 607; xml: 214; awk: 104; makefile: 49
file content (213 lines) | stat: -rw-r--r-- 6,142 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
/*
 * 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 <QMessageBox>
#include <QSqlError>

#include "contactemail.h"

/*
 *  This screen will receive the contact id from the calling screen and
 *  populate alternate email addresses from the contact.  Users can add
 *  or delete alternate email addresses.
 */

contactEmail::contactEmail(QWidget* parent, const char* name, bool modal, Qt::WindowFlags fl)
  : QDialog(parent, fl)
{
  setupUi(this);
  setObjectName(name ? name : "contactEmail");
  setModal(modal);

  _new = _buttonBox->addButton(tr("New"), QDialogButtonBox::ActionRole);
  _new->setObjectName("_new");
  _new->setShortcut(QKeySequence::New);
  _delete = _buttonBox->addButton(tr("Delete"), QDialogButtonBox::ActionRole);
  _delete->setObjectName("_delete");
  _delete->setEnabled(false);

  _list->addColumn("Hidden", -1, Qt::AlignLeading, true, "cntcteml_email");
  _list->setHeaderHidden(true);
  _list->setAlternatingRowColors(false);

  connect(_buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
  connect(_buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
  connect(_delete, SIGNAL(clicked()), this, SLOT(sDelete()));
  connect(_list, SIGNAL(valid(bool)), this, SLOT(sHandleButtons(bool)));
  connect(_new, SIGNAL(clicked()), this, SLOT(sAdd()));
  connect(_list, SIGNAL(currentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*)), this,
          SLOT(sCloseEdit(QTreeWidgetItem*, QTreeWidgetItem*)));
  connect(_list, SIGNAL(itemDoubleClicked(QTreeWidgetItem*, int)), this,
          SLOT(sOpenEdit(QTreeWidgetItem*)));

  _buttonBox->setFocus();
}

/*
 *  Destroys the object and frees any allocated resources
 */

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

/*
 *  Sets the strings of the subwidgets using the current
 *  language.
 */
void contactEmail::languageChange()
{
  retranslateUi(this);
}

void contactEmail::accept()
{
  XTreeWidgetItem* item;

  // Update the database
  XSqlQuery ins;
  ins.prepare("INSERT INTO cntcteml ( "
              "  cntcteml_cntct_id, cntcteml_email) "
              "VALUES ("
              "  :cntct_id, :email) "
              "RETURNING cntcteml_id;");

  XSqlQuery upd;
  upd.prepare("UPDATE cntcteml SET "
              "  cntcteml_email=:email "
              "WHERE (cntcteml_id=:cntcteml_id);");

  XSqlQuery del;
  del.prepare("DELETE FROM cntcteml "
              "WHERE (cntcteml_id=:cntcteml_id);");

  for (int i = 0; i < _list->topLevelItemCount(); i++)
  {
    item = _list->topLevelItem(i);
    // Inserted
    if (!item->id())
    {
      if (item->data(0,Qt::DisplayRole).toString().isEmpty())
        continue;
      ins.bindValue(":cntct_id", _cntctid);
      ins.bindValue(":email", item->data(0, Qt::DisplayRole));
      ins.exec();
      if(ins.first())
        item->setId(ins.value("cntcteml_id").toInt());
      else if (ins.lastError().type() != QSqlError::NoError)
        QMessageBox::critical(this, tr("A System Error Occurred at %1::%2.")
                              .arg(__FILE__)
                              .arg(__LINE__),
                              ins.lastError().databaseText());
    }
    // Deleted
    else if (item->data(0, Xt::DeletedRole).toBool())
    {
      del.bindValue(":cntcteml_id", item->id());
      del.exec();
      if (del.lastError().type() != QSqlError::NoError)
        QMessageBox::critical(this, tr("A System Error Occurred at %1::%2.")
                              .arg(__FILE__)
                              .arg(__LINE__),
                              del.lastError().databaseText());
    }
    // Updated
    else
    {
      upd.bindValue(":email", item->data(0, Qt::DisplayRole));
      upd.bindValue(":cntcteml_id", item->id());
      upd.exec();
      if (upd.lastError().type() != QSqlError::NoError)
        QMessageBox::critical(this, tr("A System Error Occurred at %1::%2.")
                              .arg(__FILE__)
                              .arg(__LINE__),
                              upd.lastError().databaseText());
    }
  }

  if (_list->id() > 0)
    done(_list->id());
  else
    reject();
}

void contactEmail::set(const ParameterList &pParams)
{
  QVariant param;
  bool     valid;

  param = pParams.value("cntct_id", &valid);
  if (valid)
  {
    _cntctid = param.toInt();
    sFillList();
  }
}

void contactEmail::sFillList()
{
  XSqlQuery qry;
  qry.prepare("SELECT cntcteml_id, cntcteml_email "
              "FROM cntcteml "
              "WHERE (cntcteml_cntct_id=:cntct_id);");
  qry.bindValue(":cntct_id", _cntctid);
  qry.exec();
  _list->populate(qry);
}

void contactEmail::sAdd()
{
  XTreeWidgetItem* item = new XTreeWidgetItem(_list, 0, 0, "");
  sOpenEdit(item);
}

void contactEmail::sDelete()
{
  XTreeWidgetItem* item = _list->currentItem();
  item->setData(0, Xt::DeletedRole, true);
  QFont font = item->font(0);
  font.setStrikeOut(true);
  item->setFont(0, font);
  item->setTextColor(Qt::gray);
  _delete->setEnabled(false);
}

void contactEmail::sOpenEdit(QTreeWidgetItem *item)
{
  XTreeWidgetItem* xitem = (XTreeWidgetItem*)item;
  if (xitem)
  {
    _list->openPersistentEditor(xitem,0);
    _list->editItem(xitem,0);
    _delete->setEnabled(false);
  }
}

void contactEmail::sCloseEdit(QTreeWidgetItem* /*current*/, QTreeWidgetItem* previous)
{
  XTreeWidgetItem* xitem = (XTreeWidgetItem*)previous;
  if (xitem)
  {
    _list->closePersistentEditor(xitem,0);
    QString email = xitem->data(0, Qt::DisplayRole).toString().toLower();
    xitem->setData(0, Qt::EditRole, email);
  }
}

void contactEmail::sHandleButtons(bool valid)
{
  if (_list->currentItem())
  {
    bool deleted = _list->currentItem()->data(0, Xt::DeletedRole).toBool();
    _delete->setEnabled(valid && !deleted);
  }
}