Compare commits

...

4 Commits

Author SHA1 Message Date
a6299aa7ac Added additional render mode + settings dialog 2017-02-19 14:42:50 +01:00
3e03b512e1 Fixed shutdown behavior 2017-02-19 13:42:02 +01:00
54a7372abf Fixed grave memory handling bug 2017-02-19 13:31:11 +01:00
faa2ff5612 Minor change 2017-02-19 12:54:41 +01:00
4 changed files with 200 additions and 31 deletions

View File

@ -492,4 +492,58 @@
</object>
</child>
</object>
<object class="GtkWindow" id="settingsDialog">
<property name="can_focus">False</property>
<property name="destroy_with_parent">True</property>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="margin_left">4</property>
<property name="margin_right">4</property>
<property name="margin_top">4</property>
<property name="margin_bottom">4</property>
<property name="orientation">vertical</property>
<property name="spacing">4</property>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="spacing">4</property>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Entry display mode:</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkComboBoxText" id="comboDisplayMode">
<property name="visible">True</property>
<property name="can_focus">False</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
</object>
</child>
<child type="titlebar">
<placeholder/>
</child>
</object>
</interface>

View File

@ -5,6 +5,11 @@
#include "mdiary.h"
#include "settings.h"
struct app_state {
struct mainWindow *mainWindow;
struct mdiary_settings *settings;
};
static void quit_cb(GSimpleAction *action, GVariant *parameter, gpointer user_data)
{
struct mainWindow *mainWindow = *((struct mainWindow **) user_data);
@ -15,8 +20,9 @@ static void quit_cb(GSimpleAction *action, GVariant *parameter, gpointer user_da
static void settings_cb(GSimpleAction *action, GVariant *parameter, gpointer user_data)
{
struct mainWindow *mainWindow = *((struct mainWindow **) user_data);
struct mainWindow *a = user_data;
g_print("nTODO\n");
mainWindow_settings_show(mainWindow);
}
const GActionEntry app_actions[] = {
@ -26,27 +32,38 @@ const GActionEntry app_actions[] = {
static void activate(GApplication *app, gpointer user_data)
{
struct mainWindow *mainWindow = *((struct mainWindow **) user_data);
struct mdiary_settings *settings;
struct app_state *app_state = user_data;
struct mainWindow **mainWindow = &(app_state->mainWindow);
struct mdiary_settings **settings = &(app_state->settings);
gchar *settings_path;
mainWindow = mainWindow_new();
gtk_application_add_window(GTK_APPLICATION(app), GTK_WINDOW(mainWindow->mainWindow));
*mainWindow = mainWindow_new();
gtk_application_add_window(GTK_APPLICATION(app), GTK_WINDOW((*mainWindow)->mainWindow));
settings_path = g_strdup_printf("%s/.mdiary.conf", g_get_home_dir());
settings = mdiary_settings_new(mainWindow);
mdiary_load_settings(settings_path, settings);
*settings = mdiary_settings_new(*mainWindow);
mdiary_load_settings(settings_path, *settings);
g_free(settings_path);
gtk_widget_show(mainWindow->mainWindow);
gtk_widget_show((*mainWindow)->mainWindow);
}
mdiary_save_settings(settings_path, settings);
mdiary_settings_free(settings);
static void shutdown(GApplication *app, gpointer user_data)
{
struct app_state *app_state = user_data;
struct mainWindow **mainWindow = &(app_state->mainWindow);
struct mdiary_settings **settings = &(app_state->settings);
gchar *settings_path;
settings_path = g_strdup_printf("%s/.mdiary.conf", g_get_home_dir());
mdiary_save_settings(settings_path, *settings);
mdiary_settings_free(*settings);
g_free(settings_path);
}
int main(int argc, char *argv[])
{
struct mainWindow *mainWindow;
struct app_state app_state;
GtkApplication *app;
GMenu *app_menu;
GMenu *p1;
@ -55,7 +72,8 @@ int main(int argc, char *argv[])
app = gtk_application_new("net.notsyncing.mdiary", G_APPLICATION_FLAGS_NONE);
g_application_register(G_APPLICATION(app), NULL, NULL);
g_signal_connect(app, "activate", G_CALLBACK(activate), &mainWindow);
g_signal_connect(app, "activate", G_CALLBACK(activate), &app_state);
g_signal_connect(app, "shutdown", G_CALLBACK(shutdown), &app_state);
app_menu = g_menu_new();
p1 = g_menu_new();
@ -65,7 +83,7 @@ int main(int argc, char *argv[])
g_menu_append_section(app_menu, NULL, G_MENU_MODEL(p1));
g_menu_append_section(app_menu, NULL, G_MENU_MODEL(p2));
gtk_application_set_app_menu(app, G_MENU_MODEL(app_menu));
g_action_map_add_action_entries(G_ACTION_MAP(app), app_actions, G_N_ELEMENTS(app_actions), &mainWindow);
g_action_map_add_action_entries(G_ACTION_MAP(app), app_actions, G_N_ELEMENTS(app_actions), &app_state);
gtk_application_set_app_menu(GTK_APPLICATION(app), G_MENU_MODEL(app_menu));
g_object_unref(p1);
g_object_unref(p2);

View File

@ -430,9 +430,9 @@ static void mainWindow_init_to_default(struct mainWindow *mainWindow)
* @param mainWindow the mainWindow
* @param state
*/
void mainWindow_webkit_set_enabled(struct mainWindow *mainWindow, gboolean state)
void mainWindow_renderer_set(struct mainWindow *mainWindow, enum renderer_mode state)
{
if (state) {
if (state == RENDERER_HTML) {
if (!mainWindow->webkitView) {
mainWindow->webkitView = webkit_web_view_new();
webkit_web_view_load_html_string(WEBKIT_WEB_VIEW(mainWindow->webkitView), "", "");
@ -450,6 +450,66 @@ void mainWindow_webkit_set_enabled(struct mainWindow *mainWindow, gboolean state
gtk_stack_set_visible_child(GTK_STACK(mainWindow->stackRenderMode),
mainWindow->entryTextScrollArea);
}
mainWindow->renderer = state;
}
static void mainWindow_settings_combo_changed(GtkComboBox *widget, gpointer user_data)
{
struct mainWindow *mainWindow = (struct mainWindow *)user_data;
if (!strcmp(gtk_combo_box_get_active_id(GTK_COMBO_BOX(mainWindow->settingsDialog.comboDisplayMode)), "text"))
mainWindow_renderer_set(mainWindow, RENDERER_TEXT);
else if (!strcmp(gtk_combo_box_get_active_id(GTK_COMBO_BOX(mainWindow->settingsDialog.comboDisplayMode)), "basic"))
mainWindow_renderer_set(mainWindow, RENDERER_BASIC);
else if (!strcmp(gtk_combo_box_get_active_id(GTK_COMBO_BOX(mainWindow->settingsDialog.comboDisplayMode)), "html"))
mainWindow_renderer_set(mainWindow, RENDERER_HTML);
mainWindow_entrySelected(NULL, user_data);
}
static gboolean mainWindow_settings_delete_event(GtkWidget *widget, GdkEvent *event, gpointer user_data)
{
gtk_widget_hide(widget);
return TRUE;
}
void mainWindow_settings_show(struct mainWindow *mainWindow)
{
switch (mainWindow->renderer) {
case RENDERER_TEXT:
gtk_combo_box_set_active_id(GTK_COMBO_BOX(mainWindow->settingsDialog.comboDisplayMode), "text");
break;
case RENDERER_BASIC:
gtk_combo_box_set_active_id(GTK_COMBO_BOX(mainWindow->settingsDialog.comboDisplayMode), "basic");
break;
case RENDERER_HTML:
gtk_combo_box_set_active_id(GTK_COMBO_BOX(mainWindow->settingsDialog.comboDisplayMode), "html");
break;
default:
break;
}
gtk_widget_show(mainWindow->settingsDialog.settingsDialog);
}
static void mainWindow_configure_settings_dialog(struct mainWindow *mainWindow)
{
gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(mainWindow->settingsDialog.comboDisplayMode),
"text", "Text only");
gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(mainWindow->settingsDialog.comboDisplayMode),
"basic", "Basic formatting");
gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(mainWindow->settingsDialog.comboDisplayMode),
"html", "Full HTML with images");
g_signal_connect(mainWindow->settingsDialog.comboDisplayMode,
"changed",
(GCallback) mainWindow_settings_combo_changed,
mainWindow);
g_signal_connect(mainWindow->settingsDialog.settingsDialog,
"delete-event",
(GCallback) mainWindow_settings_delete_event,
mainWindow);
}
/**
@ -495,6 +555,9 @@ struct mainWindow *mainWindow_new()
mainWindow->workspaceListSelection = GTK_TREE_SELECTION(gtk_builder_get_object(builder,
"treeWorkspaceSelection"));
mainWindow->settingsDialog.settingsDialog = GTK_WIDGET(gtk_builder_get_object(builder, "settingsDialog"));
mainWindow->settingsDialog.comboDisplayMode = GTK_WIDGET(gtk_builder_get_object(builder, "comboDisplayMode"));
mainWindow->filterSettings.time_start = 0;
mainWindow->filterSettings.time_end = -1;
mainWindow->filterSettings.hide_all = TRUE;
@ -506,6 +569,7 @@ struct mainWindow *mainWindow_new()
g_object_unref(G_OBJECT(builder));
mainWindow_configure_treeView(mainWindow);
mainWindow_configure_settings_dialog(mainWindow);
mainWindow->webkitView = NULL;
mainWindow->webkitScrollArea = NULL;
mainWindow_connect_signals(mainWindow);
@ -515,7 +579,7 @@ struct mainWindow *mainWindow_new()
mainWindow_configure_entry_completion(mainWindow);
mainWindow_set_meta_information(mainWindow, 0, 0, TRUE);
mainWindow_webkit_set_enabled(mainWindow, TRUE);
mainWindow_renderer_set(mainWindow, RENDERER_HTML);
return mainWindow;
}
@ -601,11 +665,12 @@ static void mainWindow_set_text(struct mainWindow *mainWindow, gchar *text, gcha
gchar *cmd_stderr = NULL;
gint exit_status = 0;
gchar *base_url = NULL;
GFileOutputStream *stream;
GFile *file;
GFileOutputStream *stream = NULL;
GFile *file = NULL;
if (mainWindow->webkitView &&
gtk_stack_get_visible_child(GTK_STACK(mainWindow->stackRenderMode)) == mainWindow->webkitScrollArea) {
if (mainWindow->renderer == RENDERER_TEXT) {
gtk_text_buffer_set_text(mainWindow->textBuffer, text, strlen(text));
} else {
if (text[0] != '\0') {
base_url = g_strdup_printf("file://%s", file_url);
file = g_file_new_for_path(tempfile_path);
@ -619,12 +684,28 @@ static void mainWindow_set_text(struct mainWindow *mainWindow, gchar *text, gcha
cmd = g_strdup_printf("markdown %s", tempfile_path);
if (g_spawn_command_line_sync(cmd, &cmd_stdout, &cmd_stderr, &exit_status, NULL) &&
exit_status == 0) {
webkit_web_view_load_html_string(WEBKIT_WEB_VIEW(mainWindow->webkitView),
cmd_stdout, base_url);
if (mainWindow->renderer == RENDERER_BASIC) {
/* TODO: This renderer is broken. Pango does not like HTML... */
GtkTextIter iter;
gtk_text_buffer_set_text(mainWindow->textBuffer, "", 0);
gtk_text_buffer_get_start_iter(mainWindow->textBuffer, &iter);
gtk_text_buffer_insert_markup(mainWindow->textBuffer,
&iter,
cmd_stdout,
strlen(cmd_stdout));
} else {
webkit_web_view_load_html_string(WEBKIT_WEB_VIEW(mainWindow->webkitView),
cmd_stdout, base_url);
}
} else {
g_warning("Could not render markdown for file. Showing plain text instead.\n");
webkit_web_view_load_html_string(WEBKIT_WEB_VIEW(mainWindow->webkitView),
text, "");
if (mainWindow->renderer == RENDERER_BASIC) {
gtk_text_buffer_set_text(mainWindow->textBuffer,
cmd_stdout, strlen(cmd_stdout));
} else {
webkit_web_view_load_html_string(WEBKIT_WEB_VIEW(mainWindow->webkitView),
text, base_url);
}
}
g_free(base_url);
} else {
@ -632,15 +713,17 @@ static void mainWindow_set_text(struct mainWindow *mainWindow, gchar *text, gcha
text, "");
}
g_output_stream_close(G_OUTPUT_STREAM(stream), NULL, NULL);
g_file_delete(file, NULL, NULL);
g_object_unref(stream);
g_object_unref(file);
if (stream) {
g_output_stream_close(G_OUTPUT_STREAM(stream), NULL, NULL);
g_object_unref(stream);
}
if (file) {
g_file_delete(file, NULL, NULL);
g_object_unref(file);
}
g_free(cmd);
g_free(cmd_stdout);
g_free(cmd_stderr);
} else {
gtk_text_buffer_set_text(mainWindow->textBuffer, text, strlen(text));
}
}

View File

@ -15,6 +15,12 @@ enum {
WSCOL_COUNT
};
enum renderer_mode {
RENDERER_TEXT = 0,
RENDERER_BASIC,
RENDERER_HTML
};
struct mainWindow {
GtkWidget *mainWindow;
GtkWidget *mainPane;
@ -47,6 +53,8 @@ struct mainWindow {
GtkWidget *selectedDateEntry;
GtkTreeModelFilter *entryListFiltered;
enum renderer_mode renderer;
gint64 time_earliest;
gint64 time_latest;
@ -67,6 +75,11 @@ struct mainWindow {
GtkTreeSelection *workspaceListSelection;
GtkListStore *workspaceListStore;
GtkTreeModelFilter *workspaceListFiltered;
struct settingsDialog {
GtkWidget *settingsDialog;
GtkWidget *comboDisplayMode;
} settingsDialog;
};
struct mainWindow *mainWindow_new();
@ -90,6 +103,7 @@ void mainWindow_dateIconPress(GtkWidget *widget, gint icon_pos, gint event, gpoi
void mainWindow_add_recent_workspace(struct mainWindow *mainWindow, gchar *path, gboolean append);
void mainWindow_switch_workspace(struct mainWindow *mainWindow, gchar *path, gboolean gpg_enabled);
void mainWindow_decrypt_gpg_clicked(GtkWidget *widget, gpointer user_data);
void mainWindow_webkit_set_enabled(struct mainWindow *mainWindow, gboolean state);
void mainWindow_renderer_set(struct mainWindow *mainWindow, enum renderer_mode state);
void mainWindow_settings_show(struct mainWindow *mainWindow);
#endif /* MAINWINDOW_H */