X-Git-Url: https://git.wpitchoune.net/gitweb/?p=prss.git;a=blobdiff_plain;f=src%2Fmain.c;h=5b237208b5cffbf0446539e996992c0b518b8163;hp=fd0a11f66f4ffc55de51ddabe7d9833566f49e9d;hb=ba998dd7b5a61fc2a1fa25538dc370eff0368c2c;hpb=db5c55a431ba0a76d6e269f80176711e73eb9e6a diff --git a/src/main.c b/src/main.c index fd0a11f..5b23720 100644 --- a/src/main.c +++ b/src/main.c @@ -22,15 +22,28 @@ #include #include -#include "ttrss.h" #include #include #include -#include "phttp.h" +#include "http.h" +#include "ttrss.h" #include "webbrowser.h" static const char *program_name; +static GSettings *settings; + +static int model_state; + +enum { + COL_FEED_TITLE, + COL_FEED_ID +}; + +enum { + COL_HEADLINE_TITLE, + COL_HEADLINE_ID +}; static struct option long_options[] = { {"version", no_argument, 0, 'v'}, @@ -68,84 +81,192 @@ static void print_help() printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL); } -GtkTreeView *w_treeview; -GtkTreeView *w_headlineview; +GtkTreeView *w_feedtree; +GtkTreeView *w_headlinetree; void update() { - GSettings *settings; struct feed **feeds; - GtkTreeModel *model; + GtkTreeModel *model, *headlinemodel; GtkTreeIter iter; + char *title; - printf("update\n"); - phttp_init(); - - settings = g_settings_new("prss"); - - ttrss_login(g_settings_get_string(settings, "url"), - g_settings_get_string(settings, "user"), - g_settings_get_string(settings, "password")); + model = gtk_tree_view_get_model(GTK_TREE_VIEW(w_feedtree)); - model = gtk_tree_view_get_model(GTK_TREE_VIEW(w_treeview)); + printf("update(): clear feed tree\n"); + model_state = 1; gtk_list_store_clear(GTK_LIST_STORE(model)); + headlinemodel = gtk_tree_view_get_model(w_headlinetree); + gtk_list_store_clear(GTK_LIST_STORE(headlinemodel)); + printf("update(): clear feed tree done\n"); + feeds = ttrss_get_feeds(); + while (feeds && *feeds) { + if ((*feeds)->unread) + title = g_strdup_printf("%s (%d)", + (*feeds)->title, + (*feeds)->unread); + else + title = strdup((*feeds)->title); + - for (feeds = ttrss_get_feeds(); *feeds; feeds++) { gtk_list_store_append(GTK_LIST_STORE(model), &iter); gtk_list_store_set(GTK_LIST_STORE(model), &iter, - 0, (*feeds)->title, - 1, (*feeds), + 0, title, + 1, (*feeds)->id, -1); - - printf("add\n"); + free(title); + feeds++; } + model_state = 0; + printf("refresh done\n"); } -int feed_cursor_changed_cbk(GtkTreeView *treeview, gpointer data) +void refresh_clicked_cbk(GtkWidget *btn, gpointer data) +{ + printf("refresh_clicked_cbk\n"); + update(); +} + +void settings_activate_cbk(GtkWidget *menu_item, gpointer data) +{ + GtkDialog *diag; + GtkBuilder *builder; + GtkEntry *login, *url, *pwd; + + printf("settings_activate_cbk\n"); + + builder = gtk_builder_new(); + + gtk_builder_add_from_file + (builder, + PACKAGE_DATA_DIR G_DIR_SEPARATOR_S "prss.glade", + NULL); + + diag = GTK_DIALOG(gtk_builder_get_object(builder, "settings_dialog")); + + url = GTK_ENTRY(gtk_builder_get_object(builder, "url_entry")); + gtk_entry_set_text(url, g_settings_get_string(settings, "url")); + + login = GTK_ENTRY(gtk_builder_get_object(builder, "login_entry")); + gtk_entry_set_text(login, g_settings_get_string(settings, "user")); + + pwd = GTK_ENTRY(gtk_builder_get_object(builder, "password_entry")); + gtk_entry_set_text(pwd, g_settings_get_string(settings, "password")); + + if (gtk_dialog_run(diag)) { + g_settings_set_string(settings, + "password", + gtk_entry_get_text(pwd)); + g_settings_set_string(settings, + "url", + gtk_entry_get_text(url)); + g_settings_set_string(settings, + "user", + gtk_entry_get_text(login)); + + update(); + } + + gtk_widget_destroy(GTK_WIDGET(diag)); + + g_object_unref(G_OBJECT(builder)); +} + +static struct feed *get_selected_feed() { GtkTreePath *path; GtkTreeViewColumn *cols; GtkTreeIter iter; - GtkTreeModel *model, *headline_model; + GtkTreeModel *model; + int id; + + gtk_tree_view_get_cursor(w_feedtree, &path, &cols); + + if (path) { + model = gtk_tree_view_get_model(w_feedtree); + gtk_tree_model_get_iter(model, &iter, path); + gtk_tree_model_get(model, &iter, 1, &id, -1); + + gtk_tree_path_free(path); + + return ttrss_get_feed(id); + } + + return NULL; +} + +static struct headline *get_selected_headline(GtkTreeIter *iter) +{ + GtkTreePath *path; + GtkTreeViewColumn *cols; + GtkTreeModel *model; + int id; + + gtk_tree_view_get_cursor(w_headlinetree, &path, &cols); + + if (path) { + model = gtk_tree_view_get_model(w_headlinetree); + gtk_tree_model_get_iter(model, iter, path); + gtk_tree_model_get(model, iter, 1, &id, -1); + + gtk_tree_path_free(path); + + return ttrss_get_headline(id); + } + + return NULL; +} + +int feed_cursor_changed_cbk(GtkTreeView *treeview, gpointer data) +{ + GtkTreeIter iter; + GtkTreeModel *headline_model; GtkListStore *headline_store; struct feed *feed; struct headline **headlines; char *title; + if (model_state) + return TRUE; + printf("feed_cursor_changed_cbk\n"); - gtk_tree_view_get_cursor(treeview, &path, &cols); + feed = get_selected_feed(); - if (path) { - model = gtk_tree_view_get_model(treeview); - gtk_tree_model_get_iter(model, &iter, path); - gtk_tree_model_get(model, &iter, 1, &feed, -1); - - headline_model = gtk_tree_view_get_model(w_headlineview); + if (feed) { + headline_model = gtk_tree_view_get_model(w_headlinetree); headline_store = GTK_LIST_STORE(headline_model); + model_state = 1; gtk_list_store_clear(headline_store); - for (headlines = feed->headlines; *headlines; headlines++) { - gtk_list_store_append(headline_store, &iter); - if ((*headlines)->unread) - title = g_strdup_printf("%s", - (*headlines)->title); - else - title = strdup((*headlines)->title); + if (feed) { + headlines = ttrss_feed_get_headlines(feed); + while (headlines && *headlines) { + gtk_list_store_append(headline_store, &iter); - gtk_list_store_set(headline_store, - &iter, - 0, title, - 1, (*headlines), - -1); + if ((*headlines)->unread) + title = g_strdup_printf + ("%s", + (*headlines)->title); + else + title = strdup((*headlines)->title); - free(title); + gtk_list_store_set(headline_store, + &iter, + 0, title, + 1, (*headlines)->id, + -1); + + free(title); + + headlines++; + } } - gtk_tree_path_free(path); + model_state = 0; } return 1; @@ -153,27 +274,88 @@ int feed_cursor_changed_cbk(GtkTreeView *treeview, gpointer data) int headline_cursor_changed_cbk(GtkTreeView *treeview, gpointer data) { - printf("headline_cursor_changed_cbk\n"); - - GtkTreePath *path; - GtkTreeViewColumn *cols; GtkTreeIter iter; GtkTreeModel *model; struct headline *headline; + char *str; - gtk_tree_view_get_cursor(treeview, &path, &cols); + printf("headline_cursor_changed_cbk\n"); - if (path) { - model = gtk_tree_view_get_model(GTK_TREE_VIEW(treeview)); - gtk_tree_model_get_iter(model, &iter, path); - gtk_tree_model_get(model, &iter, 1, &headline, -1); + if (model_state) + return TRUE; - web_load(((struct headline *)headline)->content); + headline = get_selected_headline(&iter); - gtk_tree_path_free(path); + if (headline) { + str = ttrss_get_headline_content(headline); + + if (str) + web_load(str); + else + web_load(""); + + free(str); + + if (headline->unread) { + ttrss_set_article_unread(headline->id, 0); + headline->unread = 0; + } + + model = gtk_tree_view_get_model(w_headlinetree); + gtk_list_store_set(GTK_LIST_STORE(model), + &iter, + 0, headline->title, + -1); + } else { + printf("headline not found\n"); } - return 1; + return TRUE; +} + +void headline_row_activated_cbk(GtkWidget *widget, gpointer data) +{ + GdkScreen *screen; + GError *error; + GtkTreeSelection *selection; + GtkTreeModel *model; + GtkTreeIter iter; + struct headline *h; + const char *url; + int id; + + printf("headline_row_activated_cbk\n"); + + selection = gtk_tree_view_get_selection(w_headlinetree); + model = gtk_tree_view_get_model(w_headlinetree); + + if (gtk_tree_selection_get_selected(selection, &model, &iter)) { + gtk_tree_model_get(model, &iter, 1, &id, -1); + + h = ttrss_get_headline(id); + if (h) { + url = h->url; + + if (gtk_widget_has_screen(widget)) + screen = gtk_widget_get_screen(widget); + else + screen = gdk_screen_get_default(); + + error = NULL; + gtk_show_uri(screen, url, + gtk_get_current_event_time(), &error); + } + } +} + +gboolean +window_delete_event_cbk(GtkWidget *widget, GdkEvent *event, gpointer data) +{ + printf("window_delete_event_cbk\n"); + + gtk_main_quit(); + + return TRUE; } int main(int argc, char **argv) @@ -213,22 +395,29 @@ int main(int argc, char **argv) exit(EXIT_FAILURE); } + http_init(); + gtk_init(NULL, NULL); + settings = g_settings_new("prss"); + + ttrss_set_config(g_settings_get_string(settings, "url"), + g_settings_get_string(settings, "user"), + g_settings_get_string(settings, "password")); + ws_open_session(); + builder = gtk_builder_new(); gtk_builder_add_from_file (builder, PACKAGE_DATA_DIR G_DIR_SEPARATOR_S "prss.glade", NULL); window = GTK_WIDGET(gtk_builder_get_object(builder, "window")); - w_treeview = GTK_TREE_VIEW(gtk_builder_get_object(builder, + w_feedtree = GTK_TREE_VIEW(gtk_builder_get_object(builder, "feed_tree")); - w_headlineview = GTK_TREE_VIEW(gtk_builder_get_object(builder, + w_headlinetree = GTK_TREE_VIEW(gtk_builder_get_object(builder, "headline_tree")); gtk_builder_connect_signals(builder, NULL); - update(); - panh = GTK_WIDGET(gtk_builder_get_object(builder, "webkit_window")); gtk_container_add(GTK_CONTAINER(panh), web_get_widget());