-
Notifications
You must be signed in to change notification settings - Fork 2
/
MainWindowImpl.cpp
211 lines (171 loc) · 6.11 KB
/
MainWindowImpl.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
/* Copyright © 2007, 2009 Michael Pyne <[email protected]>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "MainWindowImpl.h"
#include "LibrariesInfo.h"
#include <QTimer>
#include <QStandardItemModel>
#include <QFileInfo>
#include <QFileDialog>
#include <QMessageBox>
#include <QDragEnterEvent>
#include <QMimeData>
const char *versionString = "1.0";
class MainWindowImpl::Private
{
friend class MainWindowImpl;
QStringList basePaths;
QStringList envPaths;
QTimer highlightLibrariesTimer;
LibrariesInfo libs;
LibrariesStatistics stats;
};
MainWindowImpl::MainWindowImpl(QWidget *parent) :
QMainWindow(parent), m_ui(new Ui::MainWindow), m_model(0), d(new Private)
{
m_ui->setupUi(this);
setAcceptDrops(true);
m_model = new QStandardItemModel(this);
m_ui->libView->setModel(m_model);
m_ui->libView->setEditTriggers(QAbstractItemView::NoEditTriggers);
// Used to automatically highlight libraries if the user stops typing but hasn't
// hit enter or return.
d->highlightLibrariesTimer.setInterval(1000);
d->highlightLibrariesTimer.setSingleShot(true);
connect(&d->highlightLibrariesTimer, SIGNAL(timeout()), SLOT(highlightMatchingLibraries()));
}
void MainWindowImpl::openFile(const QString &fileName)
{
if (fileName.isEmpty())
return;
m_model->clear();
QStringList list;
list << tr("Shared Object") << tr("Resolved Path");
m_model->setHorizontalHeaderLabels(list);
QFileInfo fi(fileName);
QList<QStandardItem *> items;
items << new QStandardItem(fi.fileName());
items << new QStandardItem(fileName);
m_model->invisibleRootItem()->appendRow(items);
d->libs.loadFile(fileName);
d->stats = d->libs.getStatistics();
addFile(fileName, m_model->item(0));
d->libs.clear();
m_ui->libView->expand(m_model->indexFromItem(items[0]));
m_ui->libView->resizeColumnToContents(0);
m_ui->statusbar->showMessage(statusBarStats());
setWindowFilePath(fileName);
}
void MainWindowImpl::on_actionOpen_triggered()
{
QString fileName = QFileDialog::getOpenFileName(this, tr("Open executable or library"));
openFile(fileName);
}
void MainWindowImpl::on_actionQuit_triggered()
{
emit quit();
}
void MainWindowImpl::addFile(const QString &name, QStandardItem *root)
{
QList<QStandardItem *> items;
QStringList libs = d->libs.getLibraryChildren(name);
foreach(QString soname, libs) {
QString lib = d->libs.getLibraryPath(soname);
items << new QStandardItem(soname);
if (lib.isEmpty()) {
items << new QStandardItem(tr("not found"));
QFont f = items[0]->font();
f.setItalic(true);
items[0]->setFont(f);
items[1]->setFont(f);
}
else {
items << new QStandardItem(lib);
}
root->appendRow(items);
if(!lib.isEmpty())
{
addFile(soname, root->child(root->rowCount() - 1, 0));
}
items.clear();
}
}
void MainWindowImpl::resetItems(QStandardItem *root)
{
for(int i = 0; i < root->rowCount(); ++i)
resetItems(root->child(i, 0));
root->setForeground(Qt::black);
}
QString MainWindowImpl::statusBarStats() const
{
double count = d->stats.depsCount;
double bytes = d->stats.depsSizeInBytes;
if (bytes > 1024 * 1024)
{
// We need at maximum 2 digits in the fractional part.
bytes = 0.01 * (qint64(bytes) * 100 / (1024 * 1024));
return tr("%1 dependencies with total size %2 MB.").arg(count).arg(bytes);
}
if (bytes > 1024)
{
// We need at maximum 2 digits in the fractional part.
bytes = 0.01 * (qint64(bytes) * 100 / 1024);
return tr("%1 dependencies with total size %2 KB.").arg(count).arg(bytes);
}
return tr("%1 dependencies with total size %2 bytes.").arg(count).arg(bytes);
}
void MainWindowImpl::on_actionAbout_triggered()
{
QMessageBox::about(this, tr("About elflibviewer"),
tr("elflibviewer is a program to display the required library dependencies of"
" a program or shared library (in ELF format). It requires the readelf"
" tool.\n\nCopyright © 2007, 2009 Michael Pyne. This program may be distributed"
" under the terms of the GNU GPL v2 (or any later version).\n\n"
"Icons are from the KDE 4 Oxygen icon set, distributed under the "
"LGPL, version 2."
"\n\nVersion: %1").arg(versionString));
}
void MainWindowImpl::highlightMatchingLibraries()
{
QString findText = m_ui->libSearchName->text();
resetItems(m_model->invisibleRootItem());
if(findText.isEmpty())
return;
QList<QStandardItem *> results = m_model->findItems(findText, Qt::MatchContains | Qt::MatchRecursive);
foreach(QStandardItem *i, results) {
i->setForeground(Qt::red);
QStandardItem *item = i->parent();
while(item) {
item->setForeground(Qt::red);
item = item->parent();
}
}
}
void MainWindowImpl::restartTimer()
{
d->highlightLibrariesTimer.start();
}
void MainWindowImpl::dragEnterEvent(QDragEnterEvent *event)
{
if (event->mimeData()->hasFormat(QLatin1String("text/plain")))
event->acceptProposedAction();
}
void MainWindowImpl::dropEvent(QDropEvent *event)
{
QList<QUrl> urls = event->mimeData()->urls();
if (urls.isEmpty())
return;
openFile(urls.first().toLocalFile());
}
// vim: set ts=8 sw=4 et encoding=utf8: