summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJoel Klinghed <the_jk@opera.com>2017-08-01 20:03:29 +0200
committerJoel Klinghed <the_jk@opera.com>2017-08-01 20:03:29 +0200
commit29e49d4c9eb854325644b7efaebb1680ffbb49b2 (patch)
tree706a8b14f05f49be367db1f5a572a5fd10675338 /src
parent913d9eba5a277c3693e44752a161bf060bae259d (diff)
Add Export (binary) as a complement to Copy
Diffstat (limited to 'src')
-rw-r--r--src/monitor-gui.cc77
1 files changed, 68 insertions, 9 deletions
diff --git a/src/monitor-gui.cc b/src/monitor-gui.cc
index ea2132c..fa40e42 100644
--- a/src/monitor-gui.cc
+++ b/src/monitor-gui.cc
@@ -62,6 +62,8 @@ std::string const ACTION_OPEN = "open";
std::string const ACTION_SAVE = "save";
std::string const ACTION_SAVE_AS = "save_as";
std::string const ACTION_JUMP = "jump";
+std::string const ACTION_EXPORT_RAW = "export_raw";
+std::string const ACTION_EXPORT_TEXT = "export_text";
bool valid_hostname(std::string const& host) {
return !host.empty();
@@ -692,6 +694,17 @@ public:
crt_filter_.back().name = "All files";
crt_filter_.back().masks.emplace_back("*.*");
+ txt_filter_.emplace_back();
+ txt_filter_.back().name = "Text files (*.txt)";
+ txt_filter_.back().masks.emplace_back("*.txt");
+ txt_filter_.emplace_back();
+ txt_filter_.back().name = "All files";
+ txt_filter_.back().masks.emplace_back("*.*");
+
+ raw_filter_.emplace_back();
+ raw_filter_.back().name = "All files";
+ raw_filter_.back().masks.emplace_back("*.*");
+
auto file = menu_->add_menu("File");
file->add_item(ACTION_NEW, "New",
GuiMenu::Shortcut(GuiMenu::CTRL, 'N'));
@@ -732,6 +745,11 @@ public:
edit->add_item(ACTION_COPY_RAW, "Copy binary",
GuiMenu::Shortcut(GuiMenu::CTRL | GuiMenu::SHIFT, 'C'));
edit->add_separator();
+ edit->add_item(ACTION_EXPORT_TEXT, "Export...",
+ GuiMenu::Shortcut(GuiMenu::CTRL, 'E'));
+ edit->add_item(ACTION_EXPORT_RAW, "Export binary...",
+ GuiMenu::Shortcut(GuiMenu::CTRL | GuiMenu::SHIFT, 'E'));
+ edit->add_separator();
edit->add_item(ACTION_JUMP, "Jump to related",
GuiMenu::Shortcut(GuiMenu::CTRL, ' '));
edit->add_separator();
@@ -755,6 +773,8 @@ public:
menu_->enable_item(ACTION_COPY_RAW, false);
menu_->enable_item(ACTION_COPY_TEXT, false);
+ menu_->enable_item(ACTION_EXPORT_RAW, false);
+ menu_->enable_item(ACTION_EXPORT_TEXT, false);
menu_->enable_item(ACTION_JUMP, false);
menu_->add_listener(this);
@@ -867,15 +887,7 @@ public:
assert(false);
return;
}
- auto& pkg = packages_->package(selection_);
- std::unique_ptr<AttributedText> text(AttributedText::create());
- HexDump::write(text.get(), HexDump::ADDRESS | HexDump::CHARS, pkg.data);
- std::string str;
- str.append("From: ").append(pkg.from).push_back('\n');
- str.append("To : ").append(pkg.to).push_back('\n');
- str.append("At : ").append(pkg.timestamp).push_back('\n');
- str.append(text->text());
- main_->add_to_clipboard(str);
+ main_->add_to_clipboard(text_for_selection());
} else if (id == ACTION_COPY_RAW) {
if (!has_selection_) {
assert(false);
@@ -883,6 +895,19 @@ public:
}
auto& pkg = packages_->package(selection_);
main_->add_to_clipboard(pkg.data, "application/octet-stream");
+ } else if (id == ACTION_EXPORT_TEXT) {
+ if (!has_selection_) {
+ assert(false);
+ return;
+ }
+ export_text(text_for_selection(), txt_filter_);
+ } else if (id == ACTION_EXPORT_RAW) {
+ if (!has_selection_) {
+ assert(false);
+ return;
+ }
+ auto& pkg = packages_->package(selection_);
+ export_text(pkg.data, raw_filter_);
} else if (id == ACTION_JUMP) {
if (!has_selection_) {
assert(false);
@@ -965,6 +990,8 @@ public:
menu_->enable_item(ACTION_COPY_RAW, true);
menu_->enable_item(ACTION_COPY_TEXT, true);
+ menu_->enable_item(ACTION_EXPORT_RAW, true);
+ menu_->enable_item(ACTION_EXPORT_TEXT, true);
menu_->enable_item(ACTION_JUMP, has_related_);
}
@@ -976,6 +1003,8 @@ public:
menu_->enable_item(ACTION_COPY_RAW, false);
menu_->enable_item(ACTION_COPY_TEXT, false);
+ menu_->enable_item(ACTION_EXPORT_RAW, false);
+ menu_->enable_item(ACTION_EXPORT_TEXT, false);
menu_->enable_item(ACTION_JUMP, false);
}
@@ -1171,6 +1200,34 @@ private:
message->show(main_.get());
}
+ std::string text_for_selection() {
+ auto& pkg = packages_->package(selection_);
+ std::unique_ptr<AttributedText> text(AttributedText::create());
+ HexDump::write(text.get(), HexDump::ADDRESS | HexDump::CHARS, pkg.data);
+ std::string str;
+ str.append("From: ").append(pkg.from).push_back('\n');
+ str.append("To : ").append(pkg.to).push_back('\n');
+ str.append("At : ").append(pkg.timestamp).push_back('\n');
+ str.append(text->text());
+ return str;
+ }
+
+ void export_text(std::string const& data,
+ std::vector<GuiFile::Filter> const& filter) {
+ auto file = main_->file_dialog("Export", "", GuiFile::FILE_SAVE, filter);
+ if (file.empty()) return;
+ std::ofstream f(file);
+ if (!f.good()) {
+ show_error("Unable to open " + file + " for writing.");
+ return;
+ }
+ f.write(data.data(), data.size());
+ if (!f.good()) {
+ show_error("Error writing to " + file);
+ return;
+ }
+ }
+
#if HAVE_PCAP
static bool is_pcap(std::string const& file) {
return file.size() >= 5 && file.compare(file.size() - 5, 5, ".pcap") == 0;
@@ -1532,6 +1589,8 @@ private:
std::vector<GuiFile::Filter> file_filter_;
std::string file_;
bool modified_;
+ std::vector<GuiFile::Filter> txt_filter_;
+ std::vector<GuiFile::Filter> raw_filter_;
};
} // namespace