-
Notifications
You must be signed in to change notification settings - Fork 3
/
mainwindow.cpp
executable file
·298 lines (252 loc) · 10.3 KB
/
mainwindow.cpp
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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTextStream>
#include <QFileDialog>
#include <QShortcut>
#include <QMessageBox>
#include <QClipboard>
#include <QFontDatabase>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QFile>
#include <QDir>
#include <utility>
#include "finfdialog.h"
#include "version.h"
#include "config.h"
#include "demanglerules.h"
#include <sstream>
MainWindow::MainWindow(QString fileName, QWidget *parent)
: QMainWindow(parent),
ui(new Ui::MainWindow),
_fileName(std::move(fileName)),
qldd(nullptr),
shortcutClose(nullptr),
fileMenu(nullptr),
helpMenu(nullptr),
openAct(nullptr),
aboutAct(nullptr),
aboutQtAct(nullptr),
exitAct(nullptr) {
ui->setupUi(this);
int id = QFontDatabase::addApplicationFont(":/monofont.ttf");
QString family = QFontDatabase::applicationFontFamilies(id).at(0);
fixedFont = QFont(family);
ui->listWidgetExportTable->setFont(fixedFont);
ui->treeWidget->setFont(fixedFont);
shortcutClose = new QShortcut(QKeySequence(Qt::Key_Escape), this);
connect(shortcutClose, SIGNAL(activated()), this, SLOT(myClose()));
shortcutFind = new QShortcut(QKeySequence(Qt::CTRL | Qt::Key_F), this);
connect(shortcutFind, SIGNAL(activated()), this, SLOT(find()));
ui->filterButton->setIcon(ui->filterButton->style()->standardIcon(QStyle::SP_FileDialogListView));
ui->resetButton->setIcon(ui->resetButton->style()->standardIcon(QStyle::SP_DialogCancelButton));
createActions();
createMenus();
initDemangleRules();
if (!_fileName.isEmpty()) {
reset();
} else {
QTreeWidgetItem *header = ui->treeWidget->headerItem();
header->setText(0, "Dependency");
}
ui->tabWidget->setCurrentIndex(0);
}
MainWindow::~MainWindow() {
delete ui;
delete fileMenu;
delete helpMenu;
}
void MainWindow::fillExportTable(const QString &filter) {
if (qldd) {
qldd->fillExportTable(*ui->listWidgetExportTable, filter);
}
}
const RulesMap &MainWindow::demangleRules() const { return _demangleRules; }
void MainWindow::reset() {
if (!_fileName.isEmpty()) {
qldd.reset(new QLdd(_fileName, qApp->applicationDirPath(), _demangleRules));
QTreeWidgetItem *header = ui->treeWidget->headerItem();
header->setText(0, "Dependency");
qldd->fillDependency(*ui->treeWidget);
fillExportTable("");
ui->lineEditFileName->setText(qldd->getBinaryName());
ui->lineEditFileSize->setText(qldd->getStringFileSize() + "( " + QString::number(qldd->getFileSize()) + " bytes )");
ui->lineEditTimeAccess->setText(qldd->getAccessTime());
ui->lineEditTimeStatus->setText(qldd->getCreatedTime());
ui->lineEditTimeModify->setText(qldd->getModifyTime());
ui->lineEditOwner->setText(qldd->getOwnerName());
ui->lineEditGroup->setText(qldd->getGroupName());
ui->textEditInformation->setText(qldd->getInfo());
QMOD owner = qldd->getOwnerMod();
QMOD group = qldd->getGroupMod();
QMOD other = qldd->getOtherMod();
ui->checkBoxOwnerRead->setChecked(owner.read);
ui->checkBoxOwnerWrite->setChecked(owner.write);
ui->checkBoxOwnerExec->setChecked(owner.execute);
ui->checkBoxGroupRead->setChecked(group.read);
ui->checkBoxGroupWrite->setChecked(group.write);
ui->checkBoxGroupExec->setChecked(group.execute);
ui->checkBoxOtherRead->setChecked(other.read);
ui->checkBoxOtherWrite->setChecked(other.write);
ui->checkBoxOtherExec->setChecked(other.execute);
}
}
QFont MainWindow::getFixedFont() const { return fixedFont; }
void MainWindow::saveDemangleRules(const RulesMap &rules) {
QDir d(QDir::home());
if (!d.exists(DEMANGLE_RULES_DEFAULT_PATH)) {
if (!d.mkdir(DEMANGLE_RULES_DEFAULT_PATH)) {
QMessageBox::critical(this, "Demangle rules", "Can't createdirectory " + d.homePath() + DEMANGLE_RULES_DEFAULT_PATH);
}
}
QString path = d.homePath() + "/" + DEMANGLE_RULES_DEFAULT_PATH + "/rules.json";
QFile relesFile(path);
if (relesFile.open(QFile::OpenModeFlag::ReadWrite | QFile::OpenModeFlag::Truncate | QFile::OpenModeFlag::Text)) {
QJsonDocument doc;
QJsonObject obj;
QJsonArray arrRules;
for (auto item = rules.begin(); item != rules.end(); ++item) {
QJsonObject r;
r[item->first] = item->second;
arrRules.append(r);
}
obj["rules"] = arrRules;
doc.setObject(obj);
relesFile.write(doc.toJson());
relesFile.close();
}
}
void MainWindow::open() {
_fileName = QFileDialog::getOpenFileName(this);
#ifdef __APPLE__
if (_fileName.contains(".app") && QDir(_fileName).exists()) {
QString infoPlist = _fileName;
infoPlist.append("/Contents/Info.plist");
std::stringstream ss;
ss << "plutil -extract CFBundleExecutable raw "
<< " \"" << infoPlist.toStdString() << "\"";
QString execBaseName;
execAndDoOnEveryLine(ss.str(), [this, &execBaseName](const QString &line) {
if (!line.isEmpty()) {
execBaseName = line;
}
});
if (!execBaseName.isEmpty()) {
_fileName.append("/Contents/MacOS/").append(execBaseName);
}
}
#endif
reset();
}
void MainWindow::about() {
QString application = tr("Application");
QString fullVersion = tr("Full Version");
QString qtVersion = tr("Qt Version");
QString buildDate = tr("Build Date");
QMessageBox::about(this,
"About",
tr("DependencyViewer shows all dependecies of a "
"given executable or dynamic library. It is a GUI wrapper for the ldd, file and nm commands.") +
QString("<qt><table><tr><td><b>") + application + ("</b></td><td>") + tr("DependencyViewerr") + QString("</td></tr>") +
QString("<tr><td><b>") + fullVersion + ("</b></td><td>") + PROJECT_VERSION + QString("</td></tr>") + QString("<tr><td><b>") +
qtVersion + ("</b></td><td>") + QT_VERSION_STR + QString("</td></tr>") + QString("<tr><td><b>") + buildDate +
("</b></td><td>") + COMMITTER_DATE + QString("</td></tr>") + QString("</td></tr></table></qt>"));
}
void MainWindow::find() {
auto fd = new FinfDialog(this);
fd->show();
}
void MainWindow::myClose() {
if (ui->tabWidget->currentIndex() == 1) {
fillExportTable("");
} else {
close();
}
}
void MainWindow::createActions() {
openAct = new QAction(QIcon("gtk-open"), tr("&Open..."), this);
openAct->setShortcuts(QKeySequence::Open);
openAct->setStatusTip(tr("Open an existing file"));
connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
aboutAct = new QAction(tr("&About"), this);
aboutAct->setStatusTip(tr("Show the application's About box"));
connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
exitAct = new QAction(tr("E&xit"), this);
exitAct->setShortcuts(QKeySequence::Quit);
exitAct->setStatusTip(tr("Exit the application"));
connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
aboutQtAct = new QAction(tr("About &Qt"), this);
aboutQtAct->setStatusTip(tr("Show the Qt library's About box"));
connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
}
void MainWindow::createMenus() {
fileMenu = menuBar()->addMenu(tr("&File"));
fileMenu->addAction(openAct);
fileMenu->addSeparator();
fileMenu->addAction(exitAct);
helpMenu = menuBar()->addMenu(tr("&Help"));
helpMenu->addAction(aboutAct);
helpMenu->addAction(aboutQtAct);
ui->listWidgetExportTable->setContextMenuPolicy(Qt::CustomContextMenu);
connect(ui->listWidgetExportTable, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(showContextMenu(QPoint)));
}
void MainWindow::initDemangleRules() {
QDir d(QDir::home());
QString path = d.homePath() + "/" + DEMANGLE_RULES_DEFAULT_PATH + "/rules.json";
QFile relesFile(path);
if (!relesFile.exists()) {
d.mkpath(DEMANGLE_RULES_DEFAULT_PATH);
QFile::copy(":/rules.json", path);
relesFile.setPermissions(QFile::ReadUser | QFile::WriteUser);
}
if (relesFile.open(QFile::OpenModeFlag::ReadOnly | QFile::OpenModeFlag::Text)) {
_demangleRules.clear();
QString val = relesFile.readAll();
QJsonDocument doc = QJsonDocument::fromJson(val.toUtf8());
QJsonObject obj = doc.object();
QJsonArray arrRules = obj["rules"].toArray();
for (const auto &item : qAsConst(arrRules)) {
auto itemObj = item.toObject();
auto itemKeys = itemObj.keys();
if (itemKeys.size() == 1) {
auto key = itemKeys.first();
_demangleRules.push_back({key, itemObj[key].toString()});
}
}
}
}
void MainWindow::showContextMenu(const QPoint &pos) {
// Handle global position
QPoint globalPos = ui->listWidgetExportTable->mapToGlobal(pos);
// Create menu and insert some actions
QMenu myMenu;
myMenu.addAction("Copy", this, SLOT(copyExportItem()));
// Show context menu at handling position
myMenu.exec(globalPos);
}
void MainWindow::copyExportItem() {
QClipboard *clipboard = qApp->clipboard();
// If multiple selection is on, we need to erase all selected items
for (int i = 0; i < ui->listWidgetExportTable->selectedItems().size(); ++i) {
// Get curent item on selected row
QListWidgetItem *item = ui->listWidgetExportTable->item(ui->listWidgetExportTable->currentRow());
// And copy text from it
clipboard->setText(item->text());
}
}
void MainWindow::on_checkBoxOwnerRead_clicked(bool checked) { ui->checkBoxOwnerRead->setChecked(!checked); }
void MainWindow::on_checkBoxOwnerWrite_clicked(bool checked) { ui->checkBoxOwnerWrite->setChecked(!checked); }
void MainWindow::on_checkBoxOwnerExec_clicked(bool checked) { ui->checkBoxOwnerExec->setChecked(!checked); }
void MainWindow::on_checkBoxGroupRead_clicked(bool checked) { ui->checkBoxGroupRead->setChecked(!checked); }
void MainWindow::on_checkBoxGroupWrite_clicked(bool checked) { ui->checkBoxGroupWrite->setChecked(!checked); }
void MainWindow::on_checkBoxGroupExec_clicked(bool checked) { ui->checkBoxGroupExec->setChecked(!checked); }
void MainWindow::on_checkBoxOtherRead_clicked(bool checked) { ui->checkBoxOtherRead->setChecked(!checked); }
void MainWindow::on_checkBoxOtherWrite_clicked(bool checked) { ui->checkBoxOtherWrite->setChecked(!checked); }
void MainWindow::on_checkBoxOtherExec_clicked(bool checked) { ui->checkBoxOtherExec->setChecked(!checked); }
void MainWindow::on_filterButton_clicked() { find(); }
void MainWindow::on_resetButton_clicked() { myClose(); }
void MainWindow::on_rulesButton_clicked() {
auto dr = new demanglerules(this);
dr->show();
}