(no commit message)
authorJean-Philippe Orsini <jeanfi@gmail.com>
Wed, 1 May 2013 10:00:44 +0000 (10:00 +0000)
committerJean-Philippe Orsini <jeanfi@gmail.com>
Wed, 1 May 2013 10:00:44 +0000 (10:00 +0000)
src/Makefile.am
src/Makefile.in
src/main.c
src/ttrss.c
src/ttrss_cache.c [new file with mode: 0644]
src/ttrss_cache.h [new file with mode: 0644]

index 45e583c..2f9f9b3 100644 (file)
@@ -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 \
index e2373f5..78ac250 100644 (file)
@@ -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@
index b7a2e65..a89c5ad 100644 (file)
@@ -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);
index ad78c0b..2e50e08 100644 (file)
 #include <sys/stat.h>
 #include <unistd.h>
 
-
 #include <glib.h>
 #include <json/json.h>
 
 #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 (file)
index 0000000..1526e25
--- /dev/null
@@ -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 <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#include <glib.h>
+
+#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 (file)
index 0000000..caf4ca5
--- /dev/null
@@ -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