Skip to content

Commit

Permalink
w2: show parts in IFRAMEs (fixes astroidmail#499)
Browse files Browse the repository at this point in the history
  • Loading branch information
gauteh committed Jun 21, 2018
1 parent 93e40e3 commit 72f3906
Show file tree
Hide file tree
Showing 14 changed files with 162 additions and 35 deletions.
6 changes: 4 additions & 2 deletions CMakeLists.txt
Expand Up @@ -385,6 +385,8 @@ if(DISABLE_LIBSASS)
message (STATUS "Generating thread-view.css")
add_custom_command ( TARGET astroid
COMMAND ${SCSS_COMPILER} ${CMAKE_SOURCE_DIR}/ui/thread-view.scss ${CMAKE_BINARY_DIR}/thread-view.css )
add_custom_command ( TARGET astroid
COMMAND ${SCSS_COMPILER} ${CMAKE_SOURCE_DIR}/ui/part.scss ${CMAKE_BINARY_DIR}/part.css )
endif()

##
Expand Down Expand Up @@ -516,10 +518,10 @@ install (FILES ui/thread-view.html ui/edit-message.glade ui/no-mail.png
)

if(DISABLE_LIBSASS)
install (FILES ${CMAKE_BINARY_DIR}/thread-view.css
install (FILES ${CMAKE_BINARY_DIR}/thread-view.css ${CMAKE_BINARY_DIR}/part.css
DESTINATION share/astroid/ui )
else()
install (FILES ui/thread-view.scss
install (FILES ui/thread-view.scss ui/part.scss
DESTINATION share/astroid/ui)
endif()

Expand Down
3 changes: 2 additions & 1 deletion src/modes/thread_view/page_client.cc
Expand Up @@ -257,6 +257,7 @@ namespace Astroid {
LOG (debug) << "pc: sending page..";
AstroidMessages::Page s;
s.set_css (thread_view->theme.thread_view_css.c_str ());
s.set_part_css (thread_view->theme.part_css.c_str ());
s.set_html (thread_view->theme.thread_view_html.c_str ());
AeProtocol::send_message_sync (AeProtocol::MessageTypes::Page, s, ostream, m_ostream, istream, m_istream);
}
Expand Down Expand Up @@ -698,7 +699,7 @@ namespace Astroid {
image_content_type = "image/png";
}

return DomUtils::assemble_data_uri (image_content_type, content, content_size);
return DomUtils::assemble_data_uri (image_content_type.c_str (), content, content_size);
} // }}}

void PageClient::scroll_down_big () {
Expand Down
26 changes: 23 additions & 3 deletions src/modes/thread_view/theme.cc
Expand Up @@ -28,11 +28,14 @@ namespace Astroid {
const char * Theme::thread_view_html_f = "ui/thread-view.html";
# ifndef DISABLE_LIBSASS
const char * Theme::thread_view_scss_f = "ui/thread-view.scss";
const char * Theme::part_scss_f = "ui/part.scss";
# else
const char * Theme::thread_view_css_f = "ui/thread-view.css";
const char * Theme::part_css_f = "ui/part.css";
# endif
ustring Theme::thread_view_html;
ustring Theme::thread_view_css;
ustring Theme::part_css;

Theme::Theme () {
load (false);
Expand Down Expand Up @@ -60,12 +63,19 @@ namespace Astroid {
LOG (error) << "tv: scss file version does not match!";

}

path part_scss = Resource (true, part_scss_f).get_path ();
if (!check_theme_version (part_scss)) {

LOG (error) << "tv: part scss file version does not match!";

}
# else
path tv_css = Resource (true, thread_view_css_f).get_path ();
path part_css = Resource (true, part_css_f).get_path ();

if (!check_theme_version (tv_css)) {
if (!check_theme_version (part_css)) {

LOG (error) << "tv: css file version does not match!";
LOG (error) << "tv: part css file version does not match!";

}
# endif
Expand All @@ -83,6 +93,7 @@ namespace Astroid {
/* load style sheet */
# ifndef DISABLE_LIBSASS
thread_view_css = process_scss (tv_scss.c_str ());
part_css = process_scss (part_scss.c_str ());
# else
{
std::ifstream tv_css_f (tv_css.c_str());
Expand All @@ -92,6 +103,15 @@ namespace Astroid {
thread_view_css.append (tv_iit, eos);
tv_css_f.close ();
}

{
std::ifstream part_css_f (part_css.c_str());
std::istreambuf_iterator<char> eos; // default is eos
std::istreambuf_iterator<char> tv_iit (tv_css_f);

part_css.append (tv_iit, eos);
part_css_f.close ();
}
# endif

theme_loaded = true;
Expand Down
3 changes: 3 additions & 0 deletions src/modes/thread_view/theme.hh
Expand Up @@ -18,11 +18,14 @@ namespace Astroid {
static const char * thread_view_html_f;
# ifndef DISABLE_LIBSASS
static const char * thread_view_scss_f;
static const char * part_scss_f;
# else
static const char * thread_view_css_f;
static const char * part_css_f;
# endif
static ustring thread_view_html;
static ustring thread_view_css;
static ustring part_css;
const char * STYLE_NAME = "STYLE";
const int THEME_VERSION = 4;

Expand Down
1 change: 1 addition & 0 deletions src/modes/thread_view/thread_view.cc
Expand Up @@ -282,6 +282,7 @@ namespace Astroid {
home_uri,
"data:image/png;base64",
"data:image/jpeg;base64",
"data:text/html",
};

if (page_client->enable_gravatar) {
Expand Down
1 change: 0 additions & 1 deletion src/modes/thread_view/webextension/ae_protocol.cc
Expand Up @@ -66,7 +66,6 @@ namespace Astroid {
Glib::RefPtr<Gio::InputStream> istream,
std::mutex & m_istream)
{

LOG (debug) << "ae: sending: " << mt << endl;
LOG (debug) << "ae: send (sync) waiting for lock.." << endl;
std::lock_guard<std::mutex> rlk (m_istream);
Expand Down
8 changes: 6 additions & 2 deletions src/modes/thread_view/webextension/dom_utils.cc
Expand Up @@ -16,9 +16,13 @@ using std::endl;
using std::vector;

namespace Astroid {
std::string DomUtils::assemble_data_uri (ustring mime_type, gchar * &data, gsize len) {
std::string DomUtils::assemble_data_uri (const char * mime_type, const gchar * data, gsize len) {
return DomUtils::assemble_data_uri (std::string (mime_type), std::string (data, len));
}

std::string DomUtils::assemble_data_uri (const std::string mime_type, const std::string data) {

std::string base64 = "data:" + mime_type + ";base64," + Glib::Base64::encode (std::string(data, len));
std::string base64 = "data:" + mime_type + ";base64," + Glib::Base64::encode (data);

return base64;
}
Expand Down
3 changes: 2 additions & 1 deletion src/modes/thread_view/webextension/dom_utils.hh
Expand Up @@ -17,7 +17,8 @@ using std::vector;
namespace Astroid {
class DomUtils {
public:
static std::string assemble_data_uri (ustring, gchar *&, gsize);
static std::string assemble_data_uri (const char *, const gchar *, gsize);
static std::string assemble_data_uri (const std::string mime_type, const std::string data);

#ifdef ASTROID_WEBEXTENSION

Expand Down
1 change: 1 addition & 0 deletions src/modes/thread_view/webextension/messages.proto
Expand Up @@ -73,6 +73,7 @@ message Debug {
message Page {
string html = 1;
string css = 2;
string part_css = 3;
}

message Info {
Expand Down
86 changes: 73 additions & 13 deletions src/modes/thread_view/webextension/tvextension.cc
Expand Up @@ -46,6 +46,7 @@ webkit_web_extension_initialize_with_user_data (
* per web page. That means that there can only be one page in each web view,
* and each web view much use its own process. */

cout << "ae: init (CB)." << endl;
ext = new AstroidExtension (extension, pipes);

g_signal_connect (extension, "page-created",
Expand Down Expand Up @@ -345,7 +346,7 @@ void AstroidExtension::handle_page (AstroidMessages::Page &s) {/*{{{*/
webkit_dom_document_set_body (d, WEBKIT_DOM_HTML_ELEMENT(he), (err = NULL, &err));

/* load css style */
cout << "ae: loading stylesheet.." << flush;;
cout << "ae: loading stylesheet.." << flush;
WebKitDOMElement *e = webkit_dom_document_create_element (d, "STYLE", (err = NULL, &err));

WebKitDOMText *t = webkit_dom_document_create_text_node
Expand All @@ -357,6 +358,9 @@ void AstroidExtension::handle_page (AstroidMessages::Page &s) {/*{{{*/
webkit_dom_node_append_child (WEBKIT_DOM_NODE(head), WEBKIT_DOM_NODE(e), (err = NULL, &err));
cout << "done" << endl;

/* store part / iframe css for later */
part_css = s.part_css ();

g_object_unref (he);
g_object_unref (head);
g_object_unref (t);
Expand Down Expand Up @@ -740,7 +744,7 @@ void AstroidExtension::create_message_part_html (

if (c.use()) {
if (c.viewable() && c.preferred()) {
create_body_part (c, span_body);
create_body_part (message, c, span_body);
} else if (c.viewable()) {
create_sibling_part (c, span_body);
}
Expand All @@ -755,7 +759,7 @@ void AstroidExtension::create_message_part_html (


void AstroidExtension::create_body_part (
/* const AstroidMessages::Message &message, */
const AstroidMessages::Message &message,
const AstroidMessages::Message::Chunk &c,
WebKitDOMHTMLElement * span_body)
{
Expand All @@ -772,6 +776,10 @@ void AstroidExtension::create_body_part (
webkit_dom_element_remove_attribute (WEBKIT_DOM_ELEMENT (body_container),
"id");

ustring cid = "chunk_" + message.mid () + "_" + c.sid ();
webkit_dom_element_set_id (WEBKIT_DOM_ELEMENT (body_container),
cid.c_str());

ustring body = c.content();

/*
Expand All @@ -788,10 +796,8 @@ void AstroidExtension::create_body_part (
}
*/

webkit_dom_element_set_inner_html (
WEBKIT_DOM_ELEMENT(body_container),
body.c_str(),
(err = NULL, &err));
webkit_dom_node_append_child (WEBKIT_DOM_NODE (span_body),
WEBKIT_DOM_NODE (body_container), (err = NULL, &err));

/* check encryption */
//
Expand Down Expand Up @@ -1041,9 +1047,63 @@ void AstroidExtension::create_body_part (
}
# endif

webkit_dom_node_append_child (WEBKIT_DOM_NODE (span_body),
WEBKIT_DOM_NODE (body_container), (err = NULL, &err));
g_object_unref (d);

Glib::signal_idle().connect_once (
sigc::bind (
sigc::mem_fun(*this, &AstroidExtension::set_iframe_src), message.mid(), cid, body));

cout << "ae: create_body_part done." << endl;
}

void AstroidExtension::set_iframe_src (ustring mid, ustring cid, ustring body) {
cout << "ae: set iframe src: " << mid << ", " << cid << endl;
/* this function is designed to be run async (on GUI thread in extension)
* to avoid blocking the main astroid thread. ThreadView needs to approve
* the request caused by the iframe and cannot be blocked by e.g.
* add_message(...) */

WebKitDOMDocument * d = webkit_web_page_get_dom_document (page);
GError *err;

WebKitDOMHTMLElement * body_container = WEBKIT_DOM_HTML_ELEMENT(webkit_dom_document_get_element_by_id (d, cid.c_str ()));

/* we are creating the iframe here so that no requests will be made
* before this function. */

WebKitDOMHTMLElement * iframe = DomUtils::select (WEBKIT_DOM_NODE(body_container), ".body_iframe");

/* webkit_dom_html_iframe_element_set_src (WEBKIT_DOM_HTML_IFRAME_ELEMENT (iframe), */
/* DomUtils::assemble_data_uri ("text/html", body).c_str()); */

/* set style */
WebKitDOMDocument * iframe_d = webkit_dom_html_iframe_element_get_content_document (WEBKIT_DOM_HTML_IFRAME_ELEMENT(iframe));

WebKitDOMElement *e = webkit_dom_document_create_element (iframe_d, "STYLE", (err = NULL, &err));

WebKitDOMText *t = webkit_dom_document_create_text_node
(iframe_d, part_css.c_str());

webkit_dom_node_append_child (WEBKIT_DOM_NODE(e), WEBKIT_DOM_NODE(t), (err = NULL, &err));

WebKitDOMHTMLHeadElement * head = webkit_dom_document_get_head (iframe_d);

webkit_dom_node_append_child (WEBKIT_DOM_NODE(head), WEBKIT_DOM_NODE(e), (err = NULL, &err));

WebKitDOMHTMLElement * b = webkit_dom_document_get_body (iframe_d);
webkit_dom_element_set_inner_html (WEBKIT_DOM_ELEMENT(b), body.c_str (), (err = NULL, &err));

double height = webkit_dom_element_get_scroll_height (WEBKIT_DOM_ELEMENT(b));
ustring h = ustring::compose ("%1px", height);

webkit_dom_html_iframe_element_set_height (WEBKIT_DOM_HTML_IFRAME_ELEMENT (iframe), h.c_str ());

g_object_unref (b);
g_object_unref (head);
g_object_unref (t);
g_object_unref (e);
g_object_unref (iframe_d);
g_object_unref (iframe);
g_object_unref (body_container);
g_object_unref (d);
}
Expand Down Expand Up @@ -1286,7 +1346,7 @@ void AstroidExtension::set_attachment_icon (

err = NULL;
webkit_dom_element_set_attribute (WEBKIT_DOM_ELEMENT (img), "src",
DomUtils::assemble_data_uri (image_content_type, content, content_size).c_str(), &err);
DomUtils::assemble_data_uri (image_content_type.c_str (), content, content_size).c_str(), &err);

g_object_unref (attachment_icon_img);

Expand All @@ -1297,7 +1357,7 @@ void AstroidExtension::set_attachment_icon (

err = NULL;
webkit_dom_element_set_attribute (WEBKIT_DOM_ELEMENT (img), "src",
DomUtils::assemble_data_uri (image_content_type, content, content_size).c_str(), &err);
DomUtils::assemble_data_uri (image_content_type.c_str (), content, content_size).c_str(), &err);

WebKitDOMDOMTokenList * class_list =
webkit_dom_element_get_class_list (WEBKIT_DOM_ELEMENT(div_message));
Expand All @@ -1321,7 +1381,7 @@ void AstroidExtension::load_marked_icon (WebKitDOMHTMLElement * div_message) {

WebKitDOMHTMLImageElement *img = WEBKIT_DOM_HTML_IMAGE_ELEMENT (marked_icon_img);

webkit_dom_html_image_element_set_src (img, DomUtils::assemble_data_uri (image_content_type, content, content_size).c_str());
webkit_dom_html_image_element_set_src (img, DomUtils::assemble_data_uri (image_content_type.c_str (), content, content_size).c_str());

g_object_unref (marked_icon_img);

Expand All @@ -1330,7 +1390,7 @@ void AstroidExtension::load_marked_icon (WebKitDOMHTMLElement * div_message) {
".marked.icon.sec");
img = WEBKIT_DOM_HTML_IMAGE_ELEMENT (marked_icon_img);

webkit_dom_html_image_element_set_src (img, DomUtils::assemble_data_uri (image_content_type, content, content_size).c_str());
webkit_dom_html_image_element_set_src (img, DomUtils::assemble_data_uri (image_content_type.c_str (), content, content_size).c_str());

g_object_unref (marked_icon_img);
}
Expand Down
5 changes: 4 additions & 1 deletion src/modes/thread_view/webextension/tvextension.hh
Expand Up @@ -57,6 +57,7 @@ class AstroidExtension {
WebKitDOMNode * container;

void handle_page (AstroidMessages::Page &s);
ustring part_css;

/* state */
bool edit_mode = false;
Expand Down Expand Up @@ -84,10 +85,12 @@ class AstroidExtension {
WebKitDOMHTMLElement * span_body);

void create_body_part (
/* const AstroidMessages::Message &message, */
const AstroidMessages::Message &message,
const AstroidMessages::Message::Chunk &c,
WebKitDOMHTMLElement * span_body);

void set_iframe_src (ustring, ustring, ustring);

void create_sibling_part (
/* const AstroidMessages::Message &message, */
const AstroidMessages::Message::Chunk &c,
Expand Down
29 changes: 29 additions & 0 deletions ui/part.scss
@@ -0,0 +1,29 @@
/* Fonts */
@if not(global-variable-exists(font-base-size)) {
$font-base-size: 16px !global;
}

@if not(global-variable-exists(font-mono)) {
$font-mono: monospace !global;
}

@if not(global-variable-exists(font-sans)) {
$font-sans: sans-serif !global;
}

@if not(global-variable-exists(font-family-default)) {
$font-family-default: $font-sans !global;
}

body {
background-color: white !important;
overflow-x: auto;
overflow-y: hidden;
word-break: break-all;

font-size: $font-base-size;
font-family: $font-family-default;
margin: 0 0 0 0;
text-align: left;
}

0 comments on commit 72f3906

Please sign in to comment.