From: Jean-Philippe Orsini Date: Wed, 1 May 2013 10:00:44 +0000 (+0000) Subject: (no commit message) X-Git-Url: https://git.wpitchoune.net/gitweb/?p=prss.git;a=commitdiff_plain;h=a3f79452df23ac3db9e7a827e16097322de377c0 --- diff --git a/src/Makefile.am b/src/Makefile.am index 45e583c..2f9f9b3 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -22,6 +22,8 @@ prss_SOURCES = main.c \ log.h \ ttrss.c \ ttrss.h \ + ttrss_cache.c \ + ttrss_cache.h \ ttrss_model.c \ ttrss_model.h \ ttrss_ws.c \ diff --git a/src/Makefile.in b/src/Makefile.in index e2373f5..78ac250 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -66,7 +66,7 @@ am__installdirs = "$(DESTDIR)$(bindir)" "$(DESTDIR)$(man1dir)" PROGRAMS = $(bin_PROGRAMS) am_prss_OBJECTS = main.$(OBJEXT) http.$(OBJEXT) io.$(OBJEXT) \ list.$(OBJEXT) log.$(OBJEXT) ttrss.$(OBJEXT) \ - ttrss_model.$(OBJEXT) ttrss_ws.$(OBJEXT) \ + ttrss_cache.$(OBJEXT) ttrss_model.$(OBJEXT) ttrss_ws.$(OBJEXT) \ ttrss_wsasync.$(OBJEXT) url.$(OBJEXT) webbrowser.$(OBJEXT) prss_OBJECTS = $(am_prss_OBJECTS) prss_LDADD = $(LDADD) @@ -305,6 +305,8 @@ prss_SOURCES = main.c \ log.h \ ttrss.c \ ttrss.h \ + ttrss_cache.c \ + ttrss_cache.h \ ttrss_model.c \ ttrss_model.h \ ttrss_ws.c \ @@ -409,6 +411,7 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/log.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/main.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ttrss.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ttrss_cache.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ttrss_model.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ttrss_ws.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ttrss_wsasync.Po@am__quote@ diff --git a/src/main.c b/src/main.c index b7a2e65..a89c5ad 100644 --- a/src/main.c +++ b/src/main.c @@ -110,7 +110,7 @@ static char *feed_get_formated_title(struct feed *f) void update() { - struct feed **feeds; + struct feed **feeds, **cur; GtkListStore *model, *headline_model; GtkTreeIter iter; char *title; @@ -125,18 +125,19 @@ void update() gtk_list_store_clear(headline_model); log_debug("update(): clear feed tree done."); feeds = ttrss_get_feeds(); - while (feeds && *feeds) { - title = feed_get_formated_title(*feeds); + cur = feeds; + while (cur && *cur) { + title = feed_get_formated_title(*cur); gtk_list_store_append(model, &iter); gtk_list_store_set(model, &iter, COL_FEED_TITLE, title, - COL_FEED_ID, (*feeds)->id, + COL_FEED_ID, (*cur)->id, -1); g_free(title); - feeds++; + cur++; } ttrs_download_headline_content(feeds); diff --git a/src/ttrss.c b/src/ttrss.c index ad78c0b..2e50e08 100644 --- a/src/ttrss.c +++ b/src/ttrss.c @@ -24,95 +24,31 @@ #include #include - #include #include #include "http.h" #include "io.h" #include "log.h" +#include "ttrss_cache.h" #include "ttrss_wsasync.h" #include "url.h" static struct feed **data; -static char *cache_dir; - -static const char *get_cache_dir() -{ - char *home; - - if (!cache_dir) { - home = getenv("HOME"); - - if (!home) - return NULL; - - cache_dir = path_append(home, ".prss/cache"); - mkdirs(cache_dir, 0777); - } - - return cache_dir; -} - -static void file_set_content(const char *path, const char *content) -{ - FILE *fp; - - log_debug("file_set_content(): path=%s", path); - - fp = fopen(path, "w"); - if (fp) { - fwrite(content, 1, strlen(content), fp); - fclose(fp); - } -} - -static gchar *content_get_path(const struct headline *h) -{ - const char *cache_dir; - - cache_dir = get_cache_dir(); - if (cache_dir) - return g_strdup_printf("%s/%d", cache_dir, h->id); - - return NULL; -} - -static int is_content_cached(const struct headline *h) -{ - struct stat s; - char *path; - int result; - - path = content_get_path(h); - - if (stat(path, &s) == -1) - result = 0; - else - result = 1; - - g_free(path); - - return result; -} - char *ttrss_get_headline_content(struct headline *h) { - char *path, *content; + char *content; - path = content_get_path(h); - if (path) - content = file_get_content(path); - else - content = NULL; + content = cache_get(h); - if (!content) { + if (content) { + log_debug("ttrss_get_headline_content: cache hit"); + } else { + log_debug("ttrss_get_headline_content: cache miss"); content = ws_get_article_content(h->id); - file_set_content(path, content); + cache_put(h, content); } - g_free(path); - return content; } @@ -162,11 +98,16 @@ void ttrs_download_headline_content(struct feed **feeds) struct feed **fcur; struct headline **hcur; + log_debug("ttrs_download_headline_content"); + for (fcur = feeds; *fcur; fcur++) { hcur = ttrss_feed_get_headlines(*fcur); + log_debug("ttrs_download_headline_content(): %s", + (*fcur)->title); + while (hcur && *hcur) { - if (!is_content_cached(*hcur)) + if (!cache_exists(*hcur)) free(ttrss_get_headline_content(*hcur)); hcur++; } diff --git a/src/ttrss_cache.c b/src/ttrss_cache.c new file mode 100644 index 0000000..1526e25 --- /dev/null +++ b/src/ttrss_cache.c @@ -0,0 +1,119 @@ +/* + * Copyright (C) 2010-2013 jeanfi@gmail.com + * + * 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., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301 USA + */ + +#include +#include +#include +#include +#include +#include + +#include + +#include "io.h" +#include "log.h" +#include "ttrss_cache.h" + +static char *cache_dir; + +static const char *get_cache_dir() +{ + char *home; + + if (!cache_dir) { + home = getenv("HOME"); + + if (!home) + return NULL; + + cache_dir = path_append(home, ".prss/cache"); + mkdirs(cache_dir, 0777); + } + + return cache_dir; +} + +static gchar *content_get_path(const struct headline *h) +{ + const char *cache_dir; + + cache_dir = get_cache_dir(); + if (cache_dir) + return g_strdup_printf("%s/%d", cache_dir, h->id); + + return NULL; +} + +static void file_set_content(const char *path, const char *content) +{ + FILE *fp; + + log_debug("file_set_content(): path=%s", path); + + fp = fopen(path, "w"); + if (fp) { + fwrite(content, 1, strlen(content), fp); + fclose(fp); + } +} + +void cache_put(const struct headline *h, const char *content) +{ + char *path; + + path = content_get_path(h); + + if (path) + file_set_content(path, content); + + g_free(path); +} + +int cache_exists(const struct headline *h) +{ + struct stat s; + char *path; + int result; + + path = content_get_path(h); + + if (stat(path, &s) == -1) + result = 0; + else + result = 1; + + g_free(path); + + return result; +} + +char *cache_get(const struct headline *h) +{ + char *content, *path; + + path = content_get_path(h); + if (path) + content = file_get_content(path); + else + content = NULL; + + g_free(path); + + return content; +} diff --git a/src/ttrss_cache.h b/src/ttrss_cache.h new file mode 100644 index 0000000..caf4ca5 --- /dev/null +++ b/src/ttrss_cache.h @@ -0,0 +1,29 @@ +/* + * Copyright (C) 2010-2013 jeanfi@gmail.com + * + * 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., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301 USA + */ + +#ifndef _TTRSS_CACHE_H_ +#define _TTRSS_CACHE_H_ + +#include "ttrss_model.h" + +int cache_exists(const struct headline *h); +char *cache_get(const struct headline *h); +void cache_put(const struct headline *h, const char *content); + +#endif