Merge branch '0.5.x' into 0.6.0.x
[novacoin.git] / src / qt / addressbookpage.cpp
1 #include "addressbookpage.h"
2 #include "ui_addressbookpage.h"
3
4 #include "addresstablemodel.h"
5 #include "bitcoingui.h"
6 #include "editaddressdialog.h"
7 #include "csvmodelwriter.h"
8 #include "guiutil.h"
9
10 #include <QSortFilterProxyModel>
11 #include <QClipboard>
12 #include <QMessageBox>
13 #include <QMenu>
14
15 #ifdef USE_QRCODE
16 #include "qrcodedialog.h"
17 #endif
18
19 AddressBookPage::AddressBookPage(Mode mode, Tabs tab, QWidget *parent) :
20     QDialog(parent),
21     ui(new Ui::AddressBookPage),
22     model(0),
23     mode(mode),
24     tab(tab)
25 {
26     ui->setupUi(this);
27
28 #ifdef Q_WS_MAC // Icons on push buttons are very uncommon on Mac
29     ui->newAddressButton->setIcon(QIcon());
30     ui->copyToClipboard->setIcon(QIcon());
31     ui->deleteButton->setIcon(QIcon());
32 #endif
33
34 #ifndef USE_QRCODE
35     ui->showQRCode->setVisible(false);
36 #endif
37
38     switch(mode)
39     {
40     case ForSending:
41         connect(ui->tableView, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(accept()));
42         ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
43         ui->tableView->setFocus();
44         break;
45     case ForEditing:
46         ui->buttonBox->hide();
47         break;
48     }
49     switch(tab)
50     {
51     case SendingTab:
52         ui->labelExplanation->hide();
53         break;
54     case ReceivingTab:
55         break;
56     }
57     ui->tableView->setTabKeyNavigation(false);
58     ui->tableView->setContextMenuPolicy(Qt::CustomContextMenu);
59
60     // Context menu actions
61     QAction *copyAddressAction = new QAction(tr("Copy address"), this);
62     QAction *copyLabelAction = new QAction(tr("Copy label"), this);
63     QAction *editAction = new QAction(tr("Edit"), this);
64     deleteAction = new QAction(tr("Delete"), this);
65
66     contextMenu = new QMenu();
67     contextMenu->addAction(copyAddressAction);
68     contextMenu->addAction(copyLabelAction);
69     contextMenu->addAction(editAction);
70     contextMenu->addAction(deleteAction);
71
72     connect(copyAddressAction, SIGNAL(triggered()), this, SLOT(on_copyToClipboard_clicked()));
73     connect(copyLabelAction, SIGNAL(triggered()), this, SLOT(onCopyLabelAction()));
74     connect(editAction, SIGNAL(triggered()), this, SLOT(onEditAction()));
75     connect(deleteAction, SIGNAL(triggered()), this, SLOT(on_deleteButton_clicked()));
76
77     connect(ui->tableView, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(contextualMenu(QPoint)));
78
79     // Pass through accept action from button box
80     connect(ui->buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
81 }
82
83 AddressBookPage::~AddressBookPage()
84 {
85     delete ui;
86 }
87
88 void AddressBookPage::setModel(AddressTableModel *model)
89 {
90     this->model = model;
91     if(!model)
92         return;
93     // Refresh list from core
94     model->updateList();
95
96     proxyModel = new QSortFilterProxyModel(this);
97     proxyModel->setSourceModel(model);
98     proxyModel->setDynamicSortFilter(true);
99     switch(tab)
100     {
101     case ReceivingTab:
102         // Receive filter
103         proxyModel->setFilterRole(AddressTableModel::TypeRole);
104         proxyModel->setFilterFixedString(AddressTableModel::Receive);
105         break;
106     case SendingTab:
107         // Send filter
108         proxyModel->setFilterRole(AddressTableModel::TypeRole);
109         proxyModel->setFilterFixedString(AddressTableModel::Send);
110         break;
111     }
112     ui->tableView->setModel(proxyModel);
113     ui->tableView->sortByColumn(0, Qt::AscendingOrder);
114
115     // Set column widths
116     ui->tableView->horizontalHeader()->resizeSection(
117             AddressTableModel::Address, 320);
118     ui->tableView->horizontalHeader()->setResizeMode(
119             AddressTableModel::Label, QHeaderView::Stretch);
120
121     connect(ui->tableView->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
122             this, SLOT(selectionChanged()));
123
124     selectionChanged();
125 }
126
127 void AddressBookPage::on_copyToClipboard_clicked()
128 {
129     GUIUtil::copyEntryData(ui->tableView, AddressTableModel::Address);
130 }
131 void AddressBookPage::onCopyLabelAction()
132 {
133     GUIUtil::copyEntryData(ui->tableView, AddressTableModel::Label);
134 }
135
136 void AddressBookPage::onEditAction()
137 {
138     if(!ui->tableView->selectionModel())
139         return;
140     QModelIndexList indexes = ui->tableView->selectionModel()->selectedRows();
141     if(indexes.isEmpty())
142         return;
143
144     EditAddressDialog dlg(
145             tab == SendingTab ?
146             EditAddressDialog::EditSendingAddress :
147             EditAddressDialog::EditReceivingAddress);
148     dlg.setModel(model);
149     QModelIndex origIndex = proxyModel->mapToSource(indexes.at(0));
150     dlg.loadRow(origIndex.row());
151     dlg.exec();
152 }
153
154 void AddressBookPage::on_signMessage_clicked()
155 {
156     QTableView *table = ui->tableView;
157     QModelIndexList indexes = table->selectionModel()->selectedRows(AddressTableModel::Address);
158     QString addr;
159
160     foreach (QModelIndex index, indexes)
161     {
162         QVariant address = index.data();
163         addr = address.toString();
164     }
165
166     QObject *qoGUI = parent()->parent();
167     BitcoinGUI *gui = qobject_cast<BitcoinGUI *>(qoGUI);
168     if (gui)
169         gui->gotoMessagePage(addr);
170 }
171
172 void AddressBookPage::on_newAddressButton_clicked()
173 {
174     if(!model)
175         return;
176     EditAddressDialog dlg(
177             tab == SendingTab ?
178             EditAddressDialog::NewSendingAddress :
179             EditAddressDialog::NewReceivingAddress);
180     dlg.setModel(model);
181     if(dlg.exec())
182     {
183         // Select row for newly created address
184         QString address = dlg.getAddress();
185         QModelIndexList lst = proxyModel->match(proxyModel->index(0,
186                           AddressTableModel::Address, QModelIndex()),
187                           Qt::EditRole, address, 1, Qt::MatchExactly);
188         if(!lst.isEmpty())
189         {
190             ui->tableView->setFocus();
191             ui->tableView->selectRow(lst.at(0).row());
192         }
193     }
194 }
195
196 void AddressBookPage::on_deleteButton_clicked()
197 {
198     QTableView *table = ui->tableView;
199     if(!table->selectionModel())
200         return;
201     QModelIndexList indexes = table->selectionModel()->selectedRows();
202     if(!indexes.isEmpty())
203     {
204         table->model()->removeRow(indexes.at(0).row());
205     }
206 }
207
208 void AddressBookPage::selectionChanged()
209 {
210     // Set button states based on selected tab and selection
211     QTableView *table = ui->tableView;
212     if(!table->selectionModel())
213         return;
214
215     if(table->selectionModel()->hasSelection())
216     {
217         switch(tab)
218         {
219         case SendingTab:
220             // In sending tab, allow deletion of selection
221             ui->deleteButton->setEnabled(true);
222             deleteAction->setEnabled(true);
223             ui->signMessage->setEnabled(false);
224             break;
225         case ReceivingTab:
226             // Deleting receiving addresses, however, is not allowed
227             ui->deleteButton->setEnabled(false);
228             deleteAction->setEnabled(false);
229             ui->signMessage->setEnabled(true);
230             break;
231         }
232         ui->copyToClipboard->setEnabled(true);
233         ui->showQRCode->setEnabled(true);
234     }
235     else
236     {
237         ui->deleteButton->setEnabled(false);
238         ui->showQRCode->setEnabled(false);
239         ui->copyToClipboard->setEnabled(false);
240         ui->signMessage->setEnabled(false);
241     }
242 }
243
244 void AddressBookPage::done(int retval)
245 {
246     QTableView *table = ui->tableView;
247     if(!table->selectionModel() || !table->model())
248         return;
249     // When this is a tab/widget and not a model dialog, ignore "done"
250     if(mode == ForEditing)
251         return;
252
253     // Figure out which address was selected, and return it
254     QModelIndexList indexes = table->selectionModel()->selectedRows(AddressTableModel::Address);
255
256     foreach (QModelIndex index, indexes)
257     {
258         QVariant address = table->model()->data(index);
259         returnValue = address.toString();
260     }
261
262     if(returnValue.isEmpty())
263     {
264         // If no address entry selected, return rejected
265         retval = Rejected;
266     }
267
268     QDialog::done(retval);
269 }
270
271 void AddressBookPage::exportClicked()
272 {
273     // CSV is currently the only supported format
274     QString filename = GUIUtil::getSaveFileName(
275             this,
276             tr("Export Address Book Data"), QString(),
277             tr("Comma separated file (*.csv)"));
278
279     if (filename.isNull()) return;
280
281     CSVModelWriter writer(filename);
282
283     // name, column, role
284     writer.setModel(proxyModel);
285     writer.addColumn("Label", AddressTableModel::Label, Qt::EditRole);
286     writer.addColumn("Address", AddressTableModel::Address, Qt::EditRole);
287
288     if(!writer.write())
289     {
290         QMessageBox::critical(this, tr("Error exporting"), tr("Could not write to file %1.").arg(filename),
291                               QMessageBox::Abort, QMessageBox::Abort);
292     }
293 }
294
295 void AddressBookPage::on_showQRCode_clicked()
296 {
297 #ifdef USE_QRCODE
298     QTableView *table = ui->tableView;
299     QModelIndexList indexes = table->selectionModel()->selectedRows(AddressTableModel::Address);
300
301
302     QRCodeDialog *d;
303     foreach (QModelIndex index, indexes)
304     {
305         QString address = index.data().toString(),
306             label = index.sibling(index.row(), 0).data(Qt::EditRole).toString(),
307             title = QString("%1%2<< %3 >>").arg(label).arg(label.isEmpty() ? "" : " ").arg(address);
308
309         QRCodeDialog *d = new QRCodeDialog(title, address, label, tab == ReceivingTab, this);
310         d->show();
311     }
312 #endif
313 }
314
315 void AddressBookPage::contextualMenu(const QPoint &point)
316 {
317     QModelIndex index = ui->tableView->indexAt(point);
318     if(index.isValid())
319     {
320         contextMenu->exec(QCursor::pos());
321     }
322 }