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