File: BookmarkWidget.cpp

package info (click to toggle)
edb-debugger 1.3.0-2.2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,124 kB
  • sloc: cpp: 46,241; xml: 4,998; ansic: 3,088; sh: 52; asm: 33; makefile: 5
file content (245 lines) | stat: -rw-r--r-- 7,104 bytes parent folder | download | duplicates (4)
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
/*
Copyright (C) 2006 - 2015 Evan Teran
                          evan.teran@gmail.com

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "BookmarkWidget.h"
#include "BookmarksModel.h"
#include "Expression.h"
#include "edb.h"
#include <QInputDialog>
#include <QMenu>
#include <QMessageBox>
#include <QTableWidgetItem>

namespace BookmarksPlugin {

/**
 * @brief BookmarkWidget::BookmarkWidget
 * @param parent
 * @param f
 */
BookmarkWidget::BookmarkWidget(QWidget *parent, Qt::WindowFlags f)
	: QWidget(parent, f) {

	ui.setupUi(this);

	model_ = new BookmarksModel(this);
	ui.tableView->setModel(model_);

	connect(edb::v1::debugger_ui, SIGNAL(detachEvent()), model_, SLOT(clearBookmarks()));
	connect(ui.buttonAdd, &QPushButton::clicked, this, &BookmarkWidget::buttonAddClicked);
	connect(ui.buttonDel, &QPushButton::clicked, this, &BookmarkWidget::buttonDelClicked);
	connect(ui.buttonClear, &QPushButton::clicked, this, &BookmarkWidget::buttonClearClicked);
}

/**
 * @brief BookmarkWidget::on_tableView_doubleClicked
 * @param index
 */
void BookmarkWidget::on_tableView_doubleClicked(const QModelIndex &index) {

	if (auto item = static_cast<BookmarksModel::Bookmark *>(index.internalPointer())) {
		switch (index.column()) {
		case 0: //address
			switch (item->type) {
			case BookmarksModel::Bookmark::Code:
				edb::v1::jump_to_address(item->address);
				break;
			case BookmarksModel::Bookmark::Data:
				edb::v1::dump_data(item->address);
				break;
			case BookmarksModel::Bookmark::Stack:
				edb::v1::dump_stack(item->address);
				break;
			}
			break;
		case 1: // type
		{
			QString old_type = BookmarksModel::bookmarkTypeToString(item->type);
			QStringList items;
			items << tr("Code") << tr("Data") << tr("Stack");

			bool ok;
			const QString new_type = QInputDialog::getItem(ui.tableView, tr("Comment"), tr("Set Type:"), items, items.indexOf(old_type), false, &ok);
			if (ok) {
				model_->setType(index, new_type);
			}
		} break;
		case 2: //comment
		{
			QString old_comment = item->comment;
			bool ok;
			const QString new_comment = QInputDialog::getText(ui.tableView, tr("Comment"), tr("Set Comment:"), QLineEdit::Normal, old_comment, &ok);
			if (ok) {
				model_->setComment(index, new_comment);
			}
		} break;
		}
	}
}

/**
 * @brief BookmarkWidget::buttonAddClicked
 */
void BookmarkWidget::buttonAddClicked() {

	if (boost::optional<edb::address_t> address = edb::v2::get_expression_from_user(tr("Bookmark Address"), tr("Address:"))) {
		addAddress(*address);
	}
}

/**
 * @brief BookmarkWidget::buttonDelClicked
 */
void BookmarkWidget::buttonDelClicked() {

	const QItemSelectionModel *const selModel = ui.tableView->selectionModel();
	const QModelIndexList selections          = selModel->selectedRows();

	if (selections.size() == 1) {
		QModelIndex index = selections[0];
		model_->deleteBookmark(index);
	}
}

/**
 * @brief BookmarkWidget::buttonClearClicked
 */
void BookmarkWidget::buttonClearClicked() {
	model_->clearBookmarks();
}

/**
 * @brief BookmarkWidget::addAddress
 * @param address
 * @param type
 * @param comment
 */
void BookmarkWidget::addAddress(edb::address_t address, const QString &type, const QString &comment) {

	const QVector<BookmarksModel::Bookmark> &bookmarks = model_->bookmarks();

	auto it = std::find_if(bookmarks.begin(), bookmarks.end(), [address](const BookmarksModel::Bookmark &bookmark) {
		return bookmark.address == address;
	});

	if (it == bookmarks.end()) {
		BookmarksModel::Bookmark bookmark = {
			address,
			BookmarksModel::bookmarkStringToType(type),
			comment,
		};

		model_->addBookmark(bookmark);
	}
}

/**
 * @brief BookmarkWidget::shortcut
 * @param index
 */
void BookmarkWidget::shortcut(int index) {

	const QVector<BookmarksModel::Bookmark> &bookmarks = model_->bookmarks();
	if (index < bookmarks.size()) {
		const BookmarksModel::Bookmark *item = &bookmarks[index];

		switch (item->type) {
		case BookmarksModel::Bookmark::Code:
			edb::v1::jump_to_address(item->address);
			break;
		case BookmarksModel::Bookmark::Data:
			edb::v1::dump_data(item->address);
			break;
		case BookmarksModel::Bookmark::Stack:
			edb::v1::dump_stack(item->address);
			break;
		}
	}
}

/**
 * @brief BookmarkWidget::on_tableView_customContextMenuRequested
 * @param pos
 */
void BookmarkWidget::on_tableView_customContextMenuRequested(const QPoint &pos) {

	QMenu menu;
	QAction *const actionAdd   = menu.addAction(tr("&Add Address"));
	QAction *const actionDel   = menu.addAction(tr("&Delete Address"));
	QAction *const actionClear = menu.addAction(tr("&Clear"));
	menu.addSeparator();
	QAction *const actionComment = menu.addAction(tr("&Set Comment"));
	QAction *const actionType    = menu.addAction(tr("Set &Type"));
	QAction *const chosen        = menu.exec(ui.tableView->mapToGlobal(pos));

	if (chosen == actionAdd) {
		buttonAddClicked();
	} else if (chosen == actionDel) {
		buttonDelClicked();
	} else if (chosen == actionClear) {
		buttonClearClicked();
	} else if (chosen == actionComment) {

		const QItemSelectionModel *const selModel = ui.tableView->selectionModel();
		const QModelIndexList selections          = selModel->selectedRows();

		if (selections.size() == 1) {
			QModelIndex index = selections[0];

			if (auto item = static_cast<BookmarksModel::Bookmark *>(index.internalPointer())) {
				QString old_comment = item->comment;
				bool ok;
				const QString new_comment = QInputDialog::getText(ui.tableView, tr("Comment"), tr("Set Comment:"), QLineEdit::Normal, old_comment, &ok);
				if (ok) {
					model_->setComment(index, new_comment);
				}
			}
		}
	} else if (chosen == actionType) {
		const QItemSelectionModel *const selModel = ui.tableView->selectionModel();
		const QModelIndexList selections          = selModel->selectedRows();

		if (selections.size() == 1) {
			QModelIndex index = selections[0];

			if (auto item = static_cast<BookmarksModel::Bookmark *>(index.internalPointer())) {

				QString old_type = BookmarksModel::bookmarkTypeToString(item->type);
				QStringList items;
				items << tr("Code") << tr("Data") << tr("Stack");

				bool ok;
				const QString new_type = QInputDialog::getItem(ui.tableView, tr("Comment"), tr("Set Type:"), items, items.indexOf(old_type), false, &ok);
				if (ok) {
					model_->setType(index, new_type);
				}
			}
		}
	}
}

/**
 * @brief BookmarkWidget::entries
 * @return
 */
QList<BookmarksModel::Bookmark> BookmarkWidget::entries() const {
	const QVector<BookmarksModel::Bookmark> &bookmarks = model_->bookmarks();
	return bookmarks.toList();
}

}