summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrootavish <[email protected]>2014-08-14 07:41:35 +0530
committerrootavish <[email protected]>2014-08-14 07:41:35 +0530
commit700637ca1dd675ce18f2e9e56387dab86020a623 (patch)
treec442065088b57266d7d9519b07f3226474c8293d
parent89bf352b3d2725b11328e7641cef5c3cc2a8ea39 (diff)
downloadatril-700637ca1dd675ce18f2e9e56387dab86020a623.tar.bz2
atril-700637ca1dd675ce18f2e9e56387dab86020a623.tar.xz
Inverted colors(night) viewing mode for ePub
Added the capability to view and epub document with inverted colors, yet to test for documents where we write the stylesheet, although the code is there.
-rw-r--r--backend/epub/epub-document.c135
-rw-r--r--libdocument/ev-document.c14
-rw-r--r--libdocument/ev-document.h5
-rw-r--r--libview/ev-document-model.c16
-rw-r--r--libview/ev-web-view.c47
-rw-r--r--shell/ev-sidebar-thumbnails.c2
6 files changed, 179 insertions, 40 deletions
diff --git a/backend/epub/epub-document.c b/backend/epub/epub-document.c
index d0dce118..9caa8432 100644
--- a/backend/epub/epub-document.c
+++ b/backend/epub/epub-document.c
@@ -1267,6 +1267,139 @@ epub_document_get_page(EvDocument *document,
return page ;
}
+static void
+change_to_night_sheet(contentListNode *nodedata,gpointer user_data)
+{
+ gchar *filename = g_filename_from_uri(nodedata->value,NULL,NULL);
+ open_xml_document(filename);
+ set_xml_root_node(NULL);
+ xmlNodePtr head =xml_get_pointer_to_node((xmlChar*)"head",NULL,NULL);
+
+ xmlretval = NULL;
+ xml_parse_children_of_node(head,(xmlChar*)"link",(xmlChar*)"rel",(xmlChar*)"stylesheet");
+
+ xmlNodePtr day = xmlretval;
+ xmlSetProp(day,(xmlChar*)"rel",(xmlChar*)"alternate stylesheet");
+
+ xmlretval = NULL;
+ xml_parse_children_of_node(head,(xmlChar*)"link",(xmlChar*)"class",(xmlChar*)"night");
+ xmlSetProp(xmlretval,(xmlChar*)"rel",(xmlChar*)"stylesheet");
+ xmlSaveFormatFile (filename, xmldocument, 0);
+ xml_free_doc();
+ g_free(filename);
+}
+
+static void
+change_to_day_sheet(contentListNode *nodedata,gpointer user_data)
+{
+ gchar *filename = g_filename_from_uri(nodedata->value,NULL,NULL);
+ open_xml_document(filename);
+ set_xml_root_node(NULL);
+ xmlNodePtr head =xml_get_pointer_to_node((xmlChar*)"head",NULL,NULL);
+
+ xmlretval = NULL;
+ xml_parse_children_of_node(head,(xmlChar*)"link",(xmlChar*)"rel",(xmlChar*)"stylesheet");
+
+ xmlNodePtr day = xmlretval;
+ xmlSetProp(day,(xmlChar*)"rel",(xmlChar*)"alternate stylesheet");
+
+ xmlretval = NULL;
+ xml_parse_children_of_node(head,(xmlChar*)"link",(xmlChar*)"class",(xmlChar*)"day");
+ xmlSetProp(xmlretval,(xmlChar*)"rel",(xmlChar*)"stylesheet");
+ xmlSaveFormatFile (filename, xmldocument, 0);
+ xml_free_doc();
+ g_free(filename);
+}
+
+static gchar*
+epub_document_get_alternate_stylesheet(gchar *docuri)
+{
+ gchar *filename = g_filename_from_uri(docuri,NULL,NULL);
+ open_xml_document(filename);
+
+ set_xml_root_node(NULL);
+
+ xmlNodePtr head= xml_get_pointer_to_node((xmlChar*)"head",NULL,NULL);
+
+ xmlretval = NULL;
+
+ xml_parse_children_of_node(head,(xmlChar*)"link",(xmlChar*)"class",(xmlChar*)"night");
+
+ if (xmlretval != NULL) {
+ return (gchar*)xml_get_data_from_node(xmlretval,XML_ATTRIBUTE,(xmlChar*)"href");
+ }
+ g_free(filename);
+ xml_free_doc();
+ return NULL;
+}
+
+static void
+add_night_sheet(contentListNode *listdata,gchar *sheet)
+{
+ gchar *sheeturi = g_filename_to_uri(sheet,NULL,NULL);
+ open_xml_document(listdata->value);
+
+ set_xml_root_node(NULL);
+ xmlNodePtr head = xml_get_pointer_to_node((xmlChar*)"head",NULL,NULL);
+
+ xmlNodePtr link = xmlNewTextChild(head,NULL,(xmlChar*)"link",NULL);
+ xmlAttrPtr href = xmlNewProp(link,(xmlChar*)"href",(xmlChar*)sheeturi);
+ xmlAttrPtr rel = xmlNewProp(link,(xmlChar*)"rel",(xmlChar*)"alternate stylesheet");
+ xmlAttrPtr class = xmlNewProp(link,(xmlChar*)"class",(xmlChar*)"night");
+
+ xmlSaveFormatFile (listdata->value, xmldocument, 0);
+ xml_free_doc();
+ g_free(sheeturi);
+}
+
+static void
+epub_document_check_add_night_sheet(EvDocument *document)
+{
+ EpubDocument *epub_document = EPUB_DOCUMENT(document);
+
+ g_return_if_fail(EPUB_IS_DOCUMENT(epub_document));
+
+ /*
+ * We'll only check the first page for a supplied night mode stylesheet.
+ * Odds are, if this one has it, all others have it too.
+ */
+ contentListNode *node = epub_document->contentList->data;
+ gchar* stylesheetfilename = epub_document_get_alternate_stylesheet((gchar*)node->value) ;
+
+ if (stylesheetfilename == NULL) {
+ gchar *style = "body {color:rgb(255,255,255);background-color:rgb(0,0,0);text-align:justify;line-spacing:1.8; margin-top:0px;margin-bottom:4px;margin-right:50px;\
+ margin-left:50px;text-indent:3em;";
+
+ gchar *csspath = g_strdup_printf("%s/atrilnightstyle.css",epub_document->documentdir);
+
+
+ GFile *styles = g_file_new_for_path (csspath);
+ GOutputStream *outstream = (GOutputStream*)g_file_create(styles,G_FILE_CREATE_PRIVATE,NULL,NULL);
+ if ( g_output_stream_write((GOutputStream*)outstream,style,strlen(style),NULL,NULL) == -1 )
+ {
+ return ;
+ }
+ g_output_stream_close((GOutputStream*)outstream,NULL,NULL);
+ g_object_unref(styles) ;
+ g_object_unref(outstream) ;
+ //add this stylesheet to each document, for later.
+ g_list_foreach(epub_document->contentList,(GFunc)add_night_sheet,csspath);
+ g_free(csspath);
+ }
+ g_free(stylesheetfilename);
+}
+
+static void
+epub_document_toggle_night_mode(EvDocument *document,gboolean night)
+{
+ EpubDocument *epub_document = EPUB_DOCUMENT(document);
+
+ g_return_if_fail(EPUB_IS_DOCUMENT(epub_document));
+ if (night)
+ g_list_foreach(epub_document->contentList,(GFunc)change_to_night_sheet,NULL);
+ else
+ g_list_foreach(epub_document->contentList,(GFunc)change_to_day_sheet,NULL);
+}
static gboolean
epub_document_load (EvDocument* document,
@@ -1388,4 +1521,6 @@ epub_document_class_init (EpubDocumentClass *klass)
ev_document_class->get_n_pages = epub_document_get_n_pages;
ev_document_class->get_info = epub_document_get_info;
ev_document_class->get_page = epub_document_get_page;
+ ev_document_class->toggle_night_mode = epub_document_toggle_night_mode;
+ ev_document_class->check_add_night_sheet = epub_document_check_add_night_sheet;
} \ No newline at end of file
diff --git a/libdocument/ev-document.c b/libdocument/ev-document.c
index a242bdf6..667f89f1 100644
--- a/libdocument/ev-document.c
+++ b/libdocument/ev-document.c
@@ -910,3 +910,17 @@ ev_rect_cmp (EvRectangle *a,
(ABS (a->x2 - b->x2) < EPSILON) &&
(ABS (a->y2 - b->y2) < EPSILON));
}
+
+void
+ev_document_toggle_night_mode(EvDocument *document,gboolean night)
+{
+ EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS(document);
+ return klass->toggle_night_mode(document,night) ;
+}
+
+void
+ev_document_check_add_night_sheet(EvDocument *document)
+{
+ EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS(document);
+ return klass->check_add_night_sheet(document) ;
+} \ No newline at end of file
diff --git a/libdocument/ev-document.h b/libdocument/ev-document.h
index 24e31912..46a0971c 100644
--- a/libdocument/ev-document.h
+++ b/libdocument/ev-document.h
@@ -118,6 +118,9 @@ struct _EvDocumentClass
gboolean (* get_backend_info)(EvDocument *document,
EvDocumentBackendInfo *info);
gboolean (* support_synctex) (EvDocument *document);
+
+ void (* toggle_night_mode) (EvDocument *document,gboolean night);
+ void (*check_add_night_sheet)(EvDocument *document);
};
GType ev_document_get_type (void) G_GNUC_CONST;
@@ -184,6 +187,8 @@ EvMapping *ev_document_synctex_forward_search
gint ev_rect_cmp (EvRectangle *a,
EvRectangle *b);
+void ev_document_toggle_night_mode (EvDocument *document,gboolean night);
+void ev_document_check_add_night_sheet (EvDocument *document);
#define EV_TYPE_RECTANGLE (ev_rectangle_get_type ())
struct _EvRectangle
diff --git a/libview/ev-document-model.c b/libview/ev-document-model.c
index dbf0477f..403df466 100644
--- a/libview/ev-document-model.c
+++ b/libview/ev-document-model.c
@@ -109,12 +109,7 @@ ev_document_model_set_property (GObject *object,
ev_document_model_set_rotation (model, g_value_get_int (value));
break;
case PROP_INVERTED_COLORS:
- if ( model->document->iswebdocument == TRUE ) {
- atril_web_document_set_inverted_colors(model,g_value_get_boolean(value));
- }
- else {
- ev_document_model_set_inverted_colors (model, g_value_get_boolean (value));
- }
+ ev_document_model_set_inverted_colors (model, g_value_get_boolean (value));
break;
case PROP_SCALE:
ev_document_model_set_scale (model, g_value_get_double (value));
@@ -495,15 +490,6 @@ ev_document_model_set_inverted_colors (EvDocumentModel *model,
g_object_notify (G_OBJECT (model), "inverted-colors");
}
-void
-atril_web_document_set_inverted_colors (EvDocumentModel *model,
- gboolean inverted_colors)
-{
- //TODO
- model->inverted_colors = FALSE;
- g_object_notify (G_OBJECT (model), "inverted-colors");
-}
-
gboolean
ev_document_model_get_inverted_colors (EvDocumentModel *model)
{
diff --git a/libview/ev-web-view.c b/libview/ev-web-view.c
index b1fe3b08..5d1f5034 100644
--- a/libview/ev-web-view.c
+++ b/libview/ev-web-view.c
@@ -58,7 +58,7 @@ struct _EvWebView
EvDocument *document;
EvDocumentModel *model;
gint current_page;
- gboolean inverted_colors ;
+ gboolean inverted_stylesheet ;
gboolean fullscreen;
SearchParams *search;
#if GTK_CHECK_VERSION (3, 0, 0)
@@ -157,7 +157,7 @@ ev_web_view_init (EvWebView *webview)
webview->search->search_jump = TRUE ;
webview->fullscreen = FALSE;
-
+ webview->inverted_stylesheet = FALSE;
webview->hlink = NULL;
}
@@ -262,29 +262,28 @@ ev_web_view_inverted_colors_changed_cb (EvDocumentModel *model,
GParamSpec *pspec,
EvWebView *webview)
{
- guint inverted_colors = ev_document_model_get_inverted_colors (model);
- inverted_colors = !inverted_colors;
- /*TODO*/
+ EvDocument *document = ev_document_model_get_document(model);
+
+ if (ev_document_model_get_inverted_colors(model) == TRUE) {
+ if (document == NULL) {
+ ev_document_model_set_inverted_colors(model,FALSE);
+ return;
+ }
+ if (webview->inverted_stylesheet == FALSE) {
+ ev_document_check_add_night_sheet(document);
+ webview->inverted_stylesheet = TRUE;
+ }
+ ev_document_toggle_night_mode(document,TRUE);
+ webkit_web_view_reload(WEBKIT_WEB_VIEW(webview));
+ }
+ else {
+ if (document != NULL) {
+ ev_document_toggle_night_mode(document,FALSE);
+ webkit_web_view_reload(WEBKIT_WEB_VIEW(webview));
+ }
+ }
}
-static void
-ev_web_view_fullscreen_changed_cb (EvDocumentModel *model,
- GParamSpec *pspec,
- EvWebView *webview)
-{
- gboolean fullscreen = ev_document_model_get_fullscreen (model);
-
- webview->fullscreen = fullscreen;
-#if GTK_CHECK_VERSION (3, 0, 0)
- WebKitWindowProperties *window_properties =
- webkit_web_view_get_window_properties (WEBKIT_WEB_VIEW(webview));
-
- webkit_window_properties_get_fullscreen(window_properties);
- /*TODO*/
-#else
- webkit_web_view_set_view_mode(WEBKIT_WEB_VIEW(webview), WEBKIT_WEB_VIEW_VIEW_MODE_FULLSCREEN);
-#endif
-}
void
ev_web_view_set_model (EvWebView *webview,
EvDocumentModel *model)
@@ -309,7 +308,7 @@ ev_web_view_set_model (EvWebView *webview,
/* Initialize webview from model */
webview->fullscreen = ev_document_model_get_fullscreen (webview->model);
webview->document = ev_document_model_get_document(webview->model);
- webview->inverted_colors = ev_document_model_get_inverted_colors(webview->model);
+
ev_web_view_document_changed_cb (webview->model, NULL, webview);
g_signal_connect (webview->model, "notify::document",
diff --git a/shell/ev-sidebar-thumbnails.c b/shell/ev-sidebar-thumbnails.c
index a6102355..5fb951ac 100644
--- a/shell/ev-sidebar-thumbnails.c
+++ b/shell/ev-sidebar-thumbnails.c
@@ -870,7 +870,7 @@ thumbnail_job_completed_callback (EvJobThumbnail *job,
GtkTreeIter *iter;
iter = (GtkTreeIter *) g_object_get_data (G_OBJECT (job), "tree_iter");
- if (priv->inverted_colors)
+ if (priv->inverted_colors && priv->document->iswebdocument == FALSE)
ev_document_misc_invert_pixbuf (job->thumbnail);
gtk_list_store_set (priv->list_store,
iter,