(no commit message)
[prss.git] / src / ttrss_cache.c
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;
+}