Added 'VMime Viewer' example.
This commit is contained in:
parent
2ca56a209f
commit
f0e90748b3
@ -2,6 +2,11 @@
|
||||
VERSION 0.6.4cvs
|
||||
================
|
||||
|
||||
2005-03-17 Vincent Richard <vincent@vincent-richard.net>
|
||||
|
||||
* Added "Viewer" example in /examples/viewer: demonstrate the parsing
|
||||
capabilities of VMime and it can help when debugging...
|
||||
|
||||
2005-03-16 Vincent Richard <vincent@vincent-richard.net>
|
||||
|
||||
* Fixed compilation problems on Solaris 9.
|
||||
|
18
examples/viewer/Makefile
Normal file
18
examples/viewer/Makefile
Normal file
@ -0,0 +1,18 @@
|
||||
|
||||
COMPILER=g++
|
||||
CFLAGS=-g -I ../.. `pkg-config --cflags gtk+-2.0`
|
||||
LDFLAGS=-I ../.. `pkg-config --libs gtk+-2.0`
|
||||
|
||||
|
||||
viewer: viewer.o ../../libvmime-debug.a
|
||||
$(COMPILER) $(LDFLAGS) -o viewer viewer.o ../../libvmime-debug.a
|
||||
|
||||
viewer.o: viewer.cpp
|
||||
$(COMPILER) $(CFLAGS) -c -o viewer.o viewer.cpp
|
||||
|
||||
../../libvmime-debug.a:
|
||||
@echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
|
||||
@echo "! You must build VMime library before compiling the example !"
|
||||
@echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
|
||||
@false
|
||||
|
290
examples/viewer/viewer.cpp
Normal file
290
examples/viewer/viewer.cpp
Normal file
@ -0,0 +1,290 @@
|
||||
//
|
||||
// VMime library (http://vmime.sourceforge.net)
|
||||
// Copyright (C) 2002-2005 Vincent Richard <vincent@vincent-richard.net>
|
||||
//
|
||||
// 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., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
//
|
||||
|
||||
//
|
||||
// EXAMPLE DESCRIPTION:
|
||||
// ====================
|
||||
// A simple MIME viewer to show all the components of a message.
|
||||
// The user interface is written using GTK+ 2.6.
|
||||
//
|
||||
// For more information, please visit:
|
||||
// http://vmime.sourceforge.net/
|
||||
//
|
||||
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <vector>
|
||||
|
||||
#include <gtk/gtk.h>
|
||||
|
||||
#include "vmime/vmime.hpp"
|
||||
#include "vmime/platforms/posix/posixHandler.hpp"
|
||||
|
||||
|
||||
|
||||
GtkWidget* window = NULL;
|
||||
GtkWidget* treeView = NULL;
|
||||
GtkWidget* textArea = NULL;
|
||||
|
||||
GtkTreeStore* treeModel = NULL;
|
||||
|
||||
vmime::message* currentMessage = NULL;
|
||||
|
||||
|
||||
|
||||
void insertRowInModel(GtkTreeStore* model, const vmime::component* comp, GtkTreeIter* parent = NULL)
|
||||
{
|
||||
GtkTreeIter iter;
|
||||
|
||||
gtk_tree_store_append(model, &iter, parent);
|
||||
gtk_tree_store_set(model, &iter, 0, typeid(*comp).name(), 1, comp, -1);
|
||||
|
||||
const std::vector <const vmime::component*> children = comp->getChildComponents();
|
||||
|
||||
for (int i = 0 ; i < children.size() ; ++i)
|
||||
{
|
||||
insertRowInModel(model, children[i], &iter);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void updateTreeView()
|
||||
{
|
||||
GtkTreeStore* model = GTK_TREE_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(treeView)));
|
||||
|
||||
g_object_ref(model);
|
||||
gtk_tree_view_set_model(GTK_TREE_VIEW(treeView), NULL);
|
||||
|
||||
gtk_tree_store_clear(model);
|
||||
|
||||
insertRowInModel(model, currentMessage);
|
||||
|
||||
gtk_tree_view_set_model(GTK_TREE_VIEW(treeView), GTK_TREE_MODEL(model));
|
||||
g_object_unref(model);
|
||||
}
|
||||
|
||||
|
||||
static void treeViewSelChanged(GtkTreeView* treeView, gpointer userData)
|
||||
{
|
||||
GtkTreePath* path = NULL;
|
||||
GtkTreeViewColumn* col = NULL;
|
||||
|
||||
gtk_tree_view_get_cursor(treeView, &path, &col);
|
||||
|
||||
GtkTreeIter iter;
|
||||
gtk_tree_model_get_iter(GTK_TREE_MODEL(treeModel), &iter, path);
|
||||
|
||||
vmime::component* comp = NULL;
|
||||
gtk_tree_model_get(GTK_TREE_MODEL(treeModel), &iter, 1, &comp, -1);
|
||||
|
||||
GtkTextBuffer* textBuffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textArea));
|
||||
GtkTextIter start, end;
|
||||
|
||||
gtk_text_buffer_get_iter_at_offset(textBuffer, &start, comp->getParsedOffset());
|
||||
gtk_text_buffer_get_iter_at_offset(textBuffer, &end, comp->getParsedOffset() + comp->getParsedLength());
|
||||
|
||||
gtk_text_buffer_select_range(textBuffer, &start, &end);
|
||||
|
||||
gtk_text_view_scroll_to_iter(GTK_TEXT_VIEW(textArea), &start, 0.0, FALSE, 0.0, 0.0);
|
||||
|
||||
gtk_tree_path_free(path);
|
||||
}
|
||||
|
||||
|
||||
static void destroy(GtkWidget* widget, gpointer data)
|
||||
{
|
||||
gtk_main_quit();
|
||||
}
|
||||
|
||||
|
||||
void openFile(const std::string& filename)
|
||||
{
|
||||
std::ifstream file;
|
||||
file.open(filename.c_str(), std::ios::in | std::ios::binary);
|
||||
|
||||
if (!file)
|
||||
{
|
||||
std::cerr << "Can't open file '" << filename << "'." << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
vmime::string data;
|
||||
char buffer[16384];
|
||||
|
||||
do
|
||||
{
|
||||
file.read(buffer, sizeof(buffer));
|
||||
data += vmime::string(buffer, file.gcount());
|
||||
}
|
||||
while (file.gcount());
|
||||
|
||||
vmime::message* msg = new vmime::message();
|
||||
msg->parse(data);
|
||||
|
||||
delete (currentMessage);
|
||||
currentMessage = msg;
|
||||
|
||||
char* convData = g_convert_with_fallback(data.c_str(), data.length(),
|
||||
"UTF-8", "ISO-8859-1", "?", NULL, NULL, NULL);
|
||||
|
||||
if (convData == NULL)
|
||||
{
|
||||
gtk_text_buffer_set_text(gtk_text_view_get_buffer(GTK_TEXT_VIEW(textArea)),
|
||||
"GLib UTF-8 conversion error.", -1);
|
||||
}
|
||||
else
|
||||
{
|
||||
gtk_text_buffer_set_text(gtk_text_view_get_buffer(GTK_TEXT_VIEW(textArea)),
|
||||
convData, strlen(convData));
|
||||
|
||||
g_free(convData);
|
||||
}
|
||||
|
||||
updateTreeView();
|
||||
}
|
||||
|
||||
|
||||
static void onFileOpen()
|
||||
{
|
||||
GtkWidget* dlg = gtk_file_chooser_dialog_new
|
||||
("Open Message File", GTK_WINDOW(window), GTK_FILE_CHOOSER_ACTION_OPEN,
|
||||
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
|
||||
GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
|
||||
NULL);
|
||||
|
||||
if (gtk_dialog_run(GTK_DIALOG(dlg)) == GTK_RESPONSE_ACCEPT)
|
||||
{
|
||||
char* filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dlg));
|
||||
|
||||
openFile(filename);
|
||||
|
||||
g_free(filename);
|
||||
}
|
||||
|
||||
gtk_widget_destroy(dlg);
|
||||
}
|
||||
|
||||
|
||||
|
||||
// UI definitions
|
||||
static const GtkActionEntry uiActions[] =
|
||||
{
|
||||
{ "FileMenu", NULL, "_File" },
|
||||
{ "FileOpen", GTK_STOCK_OPEN, "_Open...", "<control>O", NULL, G_CALLBACK(onFileOpen) },
|
||||
{ "FileExit", GTK_STOCK_QUIT, "_Exit", "<control>Q", NULL, G_CALLBACK(gtk_main_quit) }
|
||||
};
|
||||
|
||||
static const char* uiDefinition =
|
||||
"<ui>" \
|
||||
" <menubar name=\"MainMenuBar\">" \
|
||||
" <menu action=\"FileMenu\">" \
|
||||
" <menuitem action=\"FileOpen\"/>" \
|
||||
" <menuitem action=\"FileExit\"/>" \
|
||||
" </menu>" \
|
||||
" </menubar>" \
|
||||
"</ui>";
|
||||
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
// VMime initialization
|
||||
vmime::platformDependant::setHandler<vmime::platforms::posix::posixHandler>();
|
||||
|
||||
// GTK+ initialization
|
||||
gtk_init(&argc, &argv);
|
||||
|
||||
// Create a new window
|
||||
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
|
||||
|
||||
gtk_window_set_default_size(GTK_WINDOW(window), 800, 550);
|
||||
gtk_window_set_title(GTK_WINDOW(window), "VMime Viewer Example");
|
||||
|
||||
g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(destroy), NULL);
|
||||
|
||||
GtkWidget* vbox = gtk_vbox_new(FALSE, 0);
|
||||
gtk_container_add(GTK_CONTAINER(window), vbox);
|
||||
|
||||
// Menubar
|
||||
GtkActionGroup* actionGroup = gtk_action_group_new ("Actions");
|
||||
gtk_action_group_add_actions(actionGroup, uiActions, G_N_ELEMENTS(uiActions), NULL);
|
||||
|
||||
GtkUIManager* uiManager = gtk_ui_manager_new();
|
||||
gtk_ui_manager_insert_action_group(uiManager, actionGroup, 1);
|
||||
gtk_ui_manager_add_ui_from_string(uiManager, uiDefinition, -1, NULL);
|
||||
|
||||
GtkWidget* menuBar = gtk_ui_manager_get_widget(uiManager, "/MainMenuBar");
|
||||
|
||||
gtk_box_pack_start(GTK_BOX(vbox), menuBar, FALSE, FALSE, 0);
|
||||
|
||||
// Horizontal Pane
|
||||
GtkWidget* hpane = gtk_hpaned_new();
|
||||
gtk_box_pack_start(GTK_BOX(vbox), hpane, TRUE, TRUE, 0);
|
||||
|
||||
// Tree View
|
||||
treeModel = gtk_tree_store_new(2, G_TYPE_STRING, G_TYPE_POINTER);
|
||||
|
||||
treeView = gtk_tree_view_new();
|
||||
|
||||
g_signal_connect(G_OBJECT(treeView), "cursor-changed", G_CALLBACK(treeViewSelChanged), NULL);
|
||||
|
||||
GtkWidget* scroll = gtk_scrolled_window_new(NULL, NULL);
|
||||
|
||||
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
|
||||
gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scroll), GTK_SHADOW_IN);
|
||||
gtk_container_add(GTK_CONTAINER(scroll), treeView);
|
||||
|
||||
gtk_paned_add1(GTK_PANED(hpane), scroll);
|
||||
|
||||
GtkTreeViewColumn* col = gtk_tree_view_column_new();
|
||||
gtk_tree_view_column_set_title(col, "Component Name");
|
||||
gtk_tree_view_append_column(GTK_TREE_VIEW(treeView), col);
|
||||
|
||||
GtkCellRenderer* renderer = gtk_cell_renderer_text_new();
|
||||
|
||||
gtk_tree_view_column_pack_start(col, renderer, TRUE);
|
||||
gtk_tree_view_column_add_attribute(col, renderer, "text", 0);
|
||||
|
||||
gtk_tree_view_set_model(GTK_TREE_VIEW(treeView), GTK_TREE_MODEL(treeModel));
|
||||
g_object_unref(treeModel);
|
||||
|
||||
gtk_widget_set_size_request(treeView, 200, 100);
|
||||
|
||||
// Text Area
|
||||
textArea = gtk_text_view_new();
|
||||
|
||||
gtk_text_view_set_editable(GTK_TEXT_VIEW(textArea), FALSE);
|
||||
|
||||
scroll = gtk_scrolled_window_new(NULL, NULL);
|
||||
|
||||
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
|
||||
gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scroll), GTK_SHADOW_IN);
|
||||
gtk_container_add(GTK_CONTAINER(scroll), textArea);
|
||||
|
||||
gtk_paned_add2(GTK_PANED(hpane), scroll);
|
||||
|
||||
// Show main window
|
||||
gtk_widget_show_all(window);
|
||||
|
||||
// GTK main loop
|
||||
gtk_main();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user