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 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
|
/* export_object_dialog.cpp
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <gerald@wireshark.org>
* Copyright 1998 Gerald Combs
*
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "export_object_dialog.h"
#include <ui_export_object_dialog.h>
#include <ui/alert_box.h>
#include <wsutil/utf8_entities.h>
#include <wsutil/filesystem.h>
#include <wsutil/str_util.h>
#include "qt_ui_utils.h"
#include "wireshark_application.h"
#include <QDialogButtonBox>
#include <QFileDialog>
#include <QMessageBox>
#include <QPushButton>
extern "C" {
// object_list_add_entry and object_list_get_entry are defined in ui/export_object.h
void object_list_add_entry(export_object_list_t *object_list, export_object_entry_t *entry) {
if (object_list && object_list->eod) object_list->eod->addObjectEntry(entry);
}
export_object_entry_t *object_list_get_entry(export_object_list_t *object_list, int row) {
if (object_list && object_list->eod) return object_list->eod->objectEntry(row);
return NULL;
}
// Called by taps
/* Runs at the beginning of tapping only */
static void
eo_reset(void *tapdata)
{
export_object_list_t *object_list = (export_object_list_t *) tapdata;
if (object_list && object_list->eod) object_list->eod->resetObjects();
}
} // extern "C"
enum {
COL_PACKET,
COL_HOSTNAME,
COL_CONTENT_TYPE,
COL_SIZE,
COL_FILENAME
};
enum {
export_object_row_type_ = 1000
};
class ExportObjectTreeWidgetItem : public QTreeWidgetItem
{
public:
ExportObjectTreeWidgetItem(QTreeWidget *parent, export_object_entry_t *entry) :
QTreeWidgetItem (parent, export_object_row_type_),
entry_(entry)
{
// Not perfect but better than nothing.
setTextAlignment(COL_SIZE, Qt::AlignRight);
}
~ExportObjectTreeWidgetItem() {
eo_free_entry(entry_);
}
export_object_entry_t *entry() { return entry_; }
virtual QVariant data(int column, int role) const {
if (!entry_ || role != Qt::DisplayRole) {
return QTreeWidgetItem::data(column, role);
}
switch (column) {
case COL_PACKET:
return QString::number(entry_->pkt_num);
case COL_HOSTNAME:
return entry_->hostname;
case COL_CONTENT_TYPE:
return entry_->content_type;
case COL_SIZE:
return file_size_to_qstring(entry_->payload_len);
case COL_FILENAME:
return entry_->filename;
default:
break;
}
return QTreeWidgetItem::data(column, role);
}
bool operator< (const QTreeWidgetItem &other) const
{
if (!entry_ || other.type() != export_object_row_type_) {
return QTreeWidgetItem::operator< (other);
}
const ExportObjectTreeWidgetItem *other_row = static_cast<const ExportObjectTreeWidgetItem *>(&other);
switch (treeWidget()->sortColumn()) {
case COL_PACKET:
return entry_->pkt_num < other_row->entry_->pkt_num;
case COL_SIZE:
return entry_->payload_len < other_row->entry_->payload_len;
default:
break;
}
return QTreeWidgetItem::operator< (other);
}
private:
export_object_entry_t *entry_;
};
ExportObjectDialog::ExportObjectDialog(QWidget &parent, CaptureFile &cf, ObjectType object_type) :
WiresharkDialog(parent, cf),
eo_ui_(new Ui::ExportObjectDialog),
save_bt_(NULL),
save_all_bt_(NULL),
tap_name_(NULL),
name_(NULL),
tap_packet_(NULL),
eo_protocoldata_resetfn_(NULL)
{
QPushButton *close_bt;
eo_ui_->setupUi(this);
setAttribute(Qt::WA_DeleteOnClose, true);
#if defined(Q_OS_MAC)
eo_ui_->progressLabel->setAttribute(Qt::WA_MacSmallSize, true);
eo_ui_->progressBar->setAttribute(Qt::WA_MacSmallSize, true);
#endif
export_object_list_.eod = this;
switch (object_type) {
case Dicom:
tap_name_ = "dicom_eo";
name_ = "DICOM";
tap_packet_ = eo_dicom_packet;
break;
case Http:
tap_name_ = "http_eo";
name_ = "HTTP";
tap_packet_ = eo_http_packet;
break;
case Smb:
tap_name_ = "smb_eo";
name_ = "SMB";
tap_packet_ = eo_smb_packet;
eo_protocoldata_resetfn_ = eo_smb_cleanup;
break;
case Tftp:
tap_name_ = "tftp_eo";
name_ = "TFTP";
tap_packet_ = eo_tftp_packet;
break;
}
save_bt_ = eo_ui_->buttonBox->button(QDialogButtonBox::Save);
save_all_bt_ = eo_ui_->buttonBox->button(QDialogButtonBox::SaveAll);
close_bt = eo_ui_->buttonBox->button(QDialogButtonBox::Close);
setWindowTitle(wsApp->windowTitleString(QStringList() << tr("Export") << tr("%1 object list").arg(name_)));
if (save_bt_) save_bt_->setEnabled(false);
if (save_all_bt_) save_all_bt_->setEnabled(false);
if (close_bt) close_bt->setDefault(true);
connect(&cap_file_, SIGNAL(captureFileClosing()), this, SLOT(captureFileClosing()));
show();
raise();
activateWindow();
}
ExportObjectDialog::~ExportObjectDialog()
{
delete eo_ui_;
export_object_list_.eod = NULL;
removeTapListeners();
}
void ExportObjectDialog::addObjectEntry(export_object_entry_t *entry)
{
if (!entry) return;
new ExportObjectTreeWidgetItem(eo_ui_->objectTree, entry);
if (save_all_bt_) save_all_bt_->setEnabled(true);
}
export_object_entry_t *ExportObjectDialog::objectEntry(int row)
{
QTreeWidgetItem *cur_ti = eo_ui_->objectTree->topLevelItem(row);
ExportObjectTreeWidgetItem *eo_ti = dynamic_cast<ExportObjectTreeWidgetItem *>(cur_ti);
if (eo_ti) {
return eo_ti->entry();
}
return NULL;
}
void ExportObjectDialog::resetObjects()
{
eo_ui_->objectTree->clear();
if (eo_protocoldata_resetfn_) eo_protocoldata_resetfn_();
if (save_bt_) save_bt_->setEnabled(false);
if (save_all_bt_) save_all_bt_->setEnabled(false);
}
void ExportObjectDialog::show()
{
/* Data will be gathered via a tap callback */
if (!registerTapListener(tap_name_, &export_object_list_, NULL, 0,
eo_reset,
tap_packet_,
NULL)) {
return;
}
QDialog::show();
cap_file_.retapPackets();
eo_ui_->progressFrame->hide();
for (int i = 0; i < eo_ui_->objectTree->columnCount(); i++)
eo_ui_->objectTree->resizeColumnToContents(i);
eo_ui_->objectTree->setSortingEnabled(true);
eo_ui_->objectTree->sortByColumn(COL_PACKET, Qt::AscendingOrder);
}
void ExportObjectDialog::accept()
{
// Don't close the dialog.
}
void ExportObjectDialog::captureFileClosing()
{
close();
}
void ExportObjectDialog::on_buttonBox_helpRequested()
{
wsApp->helpTopicAction(HELP_EXPORT_OBJECT_LIST);
}
void ExportObjectDialog::on_objectTree_currentItemChanged(QTreeWidgetItem *item, QTreeWidgetItem *)
{
if (!item) {
if (save_bt_) save_bt_->setEnabled(false);
return;
}
if (save_bt_) save_bt_->setEnabled(true);
ExportObjectTreeWidgetItem *eo_ti = dynamic_cast<ExportObjectTreeWidgetItem *>(item);
if (!eo_ti) {
return;
}
export_object_entry_t *entry = eo_ti->entry();
if (entry && !file_closed_) {
cf_goto_frame(cap_file_.capFile(), entry->pkt_num);
}
}
void ExportObjectDialog::on_buttonBox_clicked(QAbstractButton *button)
{
switch (eo_ui_->buttonBox->standardButton(button)) {
case QDialogButtonBox::Save:
saveCurrentEntry();
break;
case QDialogButtonBox::SaveAll:
saveAllEntries();
break;
default: // Help, Cancel
break;
}
}
void ExportObjectDialog::saveCurrentEntry()
{
QTreeWidgetItem *item = eo_ui_->objectTree->currentItem();
export_object_entry_t *entry;
QDir path(wsApp->lastOpenDir());
QString file_name;
ExportObjectTreeWidgetItem *eo_ti = dynamic_cast<ExportObjectTreeWidgetItem *>(item);
if (!eo_ti) {
return;
}
entry = eo_ti->entry();
if (!entry) {
return;
}
file_name = QFileDialog::getSaveFileName(this, wsApp->windowTitleString(tr("Save Object As" UTF8_HORIZONTAL_ELLIPSIS)),
path.filePath(entry->filename));
if (file_name.length() > 0) {
eo_save_entry(file_name.toUtf8().constData(), entry, TRUE);
}
}
#define MAXFILELEN 255
void ExportObjectDialog::saveAllEntries()
{
int i;
QTreeWidgetItem *item;
QDir save_in_dir(wsApp->lastOpenDir());
QString save_in_path;
bool all_saved = true;
//
// We want the user to be able to specify a directory in which
// to drop files for all the objects, not a file name.
//
// XXX - what we *really* want is something that asks the user
// for an existing directory *but* lets them create a new
// directory in the process. That's what we get on OS X,
// as the native dialog is used, and it supports that; does
// that also work on Windows and with Qt's own dialog?
//
save_in_path = QFileDialog::getExistingDirectory(this, wsApp->windowTitleString(tr("Save All Objects In" UTF8_HORIZONTAL_ELLIPSIS)),
save_in_dir.canonicalPath(),
QFileDialog::ShowDirsOnly);
if (save_in_path.length() < 1 || save_in_path.length() > MAXFILELEN) return;
for (i = 0; (item = eo_ui_->objectTree->topLevelItem(i)) != NULL; i++) {
int count = 0;
gchar *save_as_fullpath = NULL;
ExportObjectTreeWidgetItem *eo_ti = dynamic_cast<ExportObjectTreeWidgetItem *>(item);
if (!eo_ti) {
continue;
}
export_object_entry_t *entry = eo_ti->entry();
if (!entry) continue;
do {
GString *safe_filename;
g_free(save_as_fullpath);
if (entry->filename)
safe_filename = eo_massage_str(entry->filename,
MAXFILELEN - save_in_path.length(), count);
else {
char generic_name[256];
const char *ext;
ext = ct2ext(entry->content_type);
g_snprintf(generic_name, sizeof(generic_name),
"object%u%s%s", entry->pkt_num, ext ? "." : "",
ext ? ext : "");
safe_filename = eo_massage_str(generic_name,
MAXFILELEN - save_in_path.length(), count);
}
save_as_fullpath = g_build_filename(save_in_path.toUtf8().constData(),
safe_filename->str, NULL);
g_string_free(safe_filename, TRUE);
} while (g_file_test(save_as_fullpath, G_FILE_TEST_EXISTS) && ++count < 1000);
if (!eo_save_entry(save_as_fullpath, entry, FALSE))
all_saved = false;
g_free(save_as_fullpath);
save_as_fullpath = NULL;
}
if (!all_saved) {
QMessageBox::warning(
this,
tr("Object Export"),
tr("Some files could not be saved."),
QMessageBox::Ok
);
}
}
/*
* Editor modelines
*
* Local Variables:
* c-basic-offset: 4
* tab-width: 8
* indent-tabs-mode: nil
* End:
*
* ex: set shiftwidth=4 tabstop=8 expandtab:
* :indentSize=4:tabSize=8:noTabs=true:
*/
|