changeset 99885:7161bac8c5a8

newsticker: cache file redesign.
author Ulf Jasper <ulf.jasper@web.de>
date Mon, 24 Nov 2008 19:40:35 +0000
parents e73ab780756d
children d5afbdd2b77c
files lisp/ChangeLog lisp/net/newst-backend.el lisp/net/newst-reader.el lisp/net/newst-treeview.el
diffstat 4 files changed, 151 insertions(+), 53 deletions(-) [+]
line wrap: on
line diff
--- a/lisp/ChangeLog	Mon Nov 24 19:24:45 2008 +0000
+++ b/lisp/ChangeLog	Mon Nov 24 19:40:35 2008 +0000
@@ -1,3 +1,38 @@
+2008-11-24  Ulf Jasper  <ulf@web.de>
+
+	* net/newst-treeview.el (newsticker--treeview-current-feed): Doc
+	string.
+	(newsticker--treeview-item-show): Renamed arg FEED to
+	FEED-NAME-SYMBOL.
+	(newsticker--treeview-tree-update-tag): Removed dead code.
+	(newsticker-treeview-update): Don't update cache.
+	(newsticker-treeview-quit): Don't save cache.
+	(newsticker-treeview-save): Don't save cache. Use `newsticker-dir'.
+	(newsticker-treeview-save): Use `newsticker-dir'.
+	(newsticker--treeview-load): Use `newsticker-dir'.
+	(newsticker--treeview-mark-item): Call `newsticker--cache-save-feed'.
+	Update tags.
+	(newsticker-treeview-tree-do-click): Slightly simplified.
+
+	* net/newst-backend.el (newsticker-dir): New.
+	(newsticker-start): Use `newsticker--cache-read'.
+	(newsticker-stop): Use `newsticker--cache-save'.
+	(newsticker--sentinel-work): Use `newsticker--cache-save-feed'.
+	(newsticker--images-dir): New.
+	(newsticker--image-get, newsticker--image-sentinel): Use
+	`newsticker--images-dir'.
+	(newsticker--cache-save-version1): Backuped version of
+	`newsticker--cache-save'.
+	(newsticker--cache-update): Create newsticker-dir if necessary.
+	(newsticker--cache-dir): New.
+	(newsticker--cache-save): Save feeds separately in `newsticker-dir'.
+	(newsticker--cache-save-feed): New.
+	(newsticker--cache-read-version1): New. Backuped version for reading
+	cache data..
+	(newsticker--cache-read, newsticker--cache-do-read): New.
+
+	* net/newst-reader.el (newsticker--image-read): Use `newsticker--images-dir'.
+
 2008-11-24  Chong Yidong  <cyd@stupidchicken.com>
 
 	* files.el (confirm-nonexistent-file-or-buffer): Allow
--- a/lisp/net/newst-backend.el	Mon Nov 24 19:24:45 2008 +0000
+++ b/lisp/net/newst-backend.el	Mon Nov 24 19:40:35 2008 +0000
@@ -7,7 +7,7 @@
 ;; Filename:    newst-backend.el
 ;; URL:         http://www.nongnu.org/newsticker
 ;; Keywords:    News, RSS, Atom
-;; Time-stamp:  "31. Oktober 2008, 21:07:17 (ulf)"
+;; Time-stamp:  "24. November 2008, 19:39:24 (ulf)"
 
 ;; ======================================================================
 
@@ -424,9 +424,9 @@
   :type 'string
   :group 'newsticker-miscellaneous)
 
-(defcustom newsticker-imagecache-dirname
-  "~/.newsticker-images"
-  "Name of the directory where newsticker stores cached images."
+(defcustom newsticker-dir
+  "~/.newsticker"
+  "Name of the directory where newsticker saves cached data."
   :type 'string
   :group 'newsticker-miscellaneous)
 
@@ -629,16 +629,7 @@
   (let ((running (newsticker-running-p)))
     ;; read old cache if it exists and newsticker is not running
     (unless running
-      (let ((coding-system-for-read 'utf-8))
-        (when (file-exists-p newsticker-cache-filename)
-          (with-temp-buffer
-            (insert-file-contents newsticker-cache-filename)
-            (goto-char (point-min))
-            (condition-case nil
-              (setq newsticker--cache (read (current-buffer)))
-            (error
-             (message "Error while reading newsticker cache file!")
-             (setq newsticker--cache nil)))))))
+      (newsticker--cache-read))
     ;; start retrieval timers -- one timer for each feed
     (dolist (feed (append newsticker-url-list-defaults newsticker-url-list))
       (newsticker--start-feed feed))
@@ -660,7 +651,7 @@
 Cancel the timers for display and retrieval.  Run `newsticker-stop-hook'
 if newsticker has been running."
   (interactive)
-  (newsticker--cache-update t)
+  (newsticker--cache-save)
   (when (fboundp 'newsticker-stop-ticker) ; silence compiler warnings
     (newsticker-stop-ticker))
   (when (newsticker-running-p)
@@ -743,7 +734,7 @@
 See `newsticker-get-news'."
   (let ((coding-system-for-read 'no-conversion))
     (condition-case error-data
-        (url-retrieve url 'newsticker--get-news-by-url-callback 
+        (url-retrieve url 'newsticker--get-news-by-url-callback
                       (list feed-name))
           (error (message "Error retrieving news from %s: %s" feed-name
                           error-data))))
@@ -1035,7 +1026,8 @@
         (setq newsticker--latest-update-time (current-time))
         (when something-was-added
           ;; FIXME: should we care about removed items as well?
-          (newsticker--cache-update)
+          (newsticker--cache-save-feed
+           (newsticker--cache-get-feed name-symbol))
           (when (fboundp 'newsticker--buffer-set-uptodate) ;silence
                                                            ;compiler
                                                            ;warnings
@@ -1733,11 +1725,14 @@
 ;; ======================================================================
 ;;; Images
 ;; ======================================================================
+(defun newsticker--images-dir ()
+  "Return directory where feed images are saved."
+  (concat newsticker-dir "/images"))
+
 (defun newsticker--image-get (feed-name url)
   "Get image of the news site FEED-NAME from URL.
 If the image has been downloaded in the last 24h do nothing."
-  (let ((image-name (concat newsticker-imagecache-dirname "/"
-                            feed-name)))
+  (let ((image-name (concat (newsticker--images-dir) feed-name)))
     (if (and (file-exists-p image-name)
              (time-less-p (current-time)
                           (time-add (nth 5 (file-attributes image-name))
@@ -1788,12 +1783,11 @@
       (let (image-name)
         (save-excursion
           (set-buffer (process-buffer process))
-          (setq image-name (concat newsticker-imagecache-dirname "/"
-                                   feed-name))
+          (setq image-name (concat (newsticker--images-dir) feed-name))
           (set-buffer-file-coding-system 'no-conversion)
           ;; make sure the cache dir exists
-          (unless (file-directory-p newsticker-imagecache-dirname)
-            (make-directory newsticker-imagecache-dirname))
+          (unless (file-directory-p (newsticker--images-dir))
+            (make-directory (newsticker--images-dir)))
           ;; write and close buffer
           (let ((require-final-newline nil)
                 (backup-inhibited t)
@@ -1802,8 +1796,6 @@
           (set-buffer-modified-p nil)
           (kill-buffer (current-buffer)))))))
 
-
-
 (defun newsticker--insert-image (img string)
   "Insert IMG with STRING at point."
   (insert-image img string))
@@ -1936,12 +1928,12 @@
                                                     newsticker-desc-comp-max))
                                             (string= (substring
                                                       (newsticker--desc anitem)
-                                                      0 
+                                                      0
                                                       newsticker-desc-comp-max)
                                                      desc)
                                           (string= (newsticker--desc anitem)
                                                    desc)))))))
-                       ;;(newsticker--debug-msg "Found %s guid=%s" 
+                       ;;(newsticker--debug-msg "Found %s guid=%s"
                        ;; (newsticker--title anitem)
                        ;;                     (newsticker--guid anitem))
                        (throw 'found anitem)))
@@ -1970,7 +1962,7 @@
     (if item
         ;; does exist already -- change age, update time and position
         (progn
-          ;;(newsticker--debug-msg "Updating item %s %s %s %s %s -> %s %s 
+          ;;(newsticker--debug-msg "Updating item %s %s %s %s %s -> %s %s
           ;;                   (guid %s -> %s)"
           ;;                   feed-name-symbol title link time age
           ;;                 updated-time updated-age
@@ -2094,9 +2086,7 @@
                  (throw 'result t)))))
     (< (or (newsticker--pos item1) 0) (or (newsticker--pos item2) 0))))
 
-
-
-(defun newsticker--cache-save ()
+(defun newsticker--cache-save-version1 ()
   "Update and save newsticker cache file."
   (interactive)
   (newsticker--cache-update t))
@@ -2105,6 +2095,8 @@
   "Update newsticker cache file.
 If optional argument SAVE is not nil the cache file is saved to disk."
   (save-excursion
+    (unless (file-directory-p newsticker-dir)
+      (make-directory newsticker-dir t))
     (let ((coding-system-for-write 'utf-8)
           (buf (find-file-noselect newsticker-cache-filename)))
       (when buf
@@ -2121,6 +2113,69 @@
 FEED is a symbol!"
   (assoc feed newsticker--cache))
 
+(defun newsticker--cache-dir ()
+  "Return directory for saving cache data."
+  (concat newsticker-dir "/feeds"))
+
+(defun newsticker--cache-save ()
+    "Save cache data for all feeds."
+    (unless (file-directory-p newsticker-dir)
+      (make-directory newsticker-dir t))
+    (mapc 'newsticker--cache-save-feed newsticker--cache)
+    nil)
+
+(defun newsticker--cache-save-feed (feed)
+  "Save cache data for FEED."
+  (let ((dir (concat (newsticker--cache-dir) "/" (symbol-name (car feed)))))
+    (unless (file-directory-p dir)
+      (make-directory dir t))
+    (let ((coding-system-for-write 'utf-8))
+      (with-temp-file (concat dir "/data")
+        (insert ";; -*- coding: utf-8 -*-\n")
+        (insert (prin1-to-string (cdr feed)))))))
+
+(defun newsticker--cache-read-version1 ()
+  "Read version1 cache data."
+  (let ((coding-system-for-read 'utf-8))
+    (when (file-exists-p newsticker-cache-filename)
+      (with-temp-buffer
+        (insert-file-contents newsticker-cache-filename)
+        (goto-char (point-min))
+        (condition-case nil
+            (setq newsticker--cache (read (current-buffer)))
+          (error
+           (message "Error while reading newsticker cache file!")
+           (setq newsticker--cache nil)))))))
+
+(defun newsticker--cache-read ()
+  "Read cache data."
+  (setq newsticker--cache nil)
+  (if (file-exists-p newsticker-cache-filename)
+      (progn
+        (when (y-or-n-p "Old newsticker cache file exists.  Read it? ")
+          (newsticker--cache-read-version1))
+        (message "Please remove/rename the old cache file (%s) now."
+                 newsticker-cache-filename))
+    (mapc (lambda (f)
+            (newsticker--cache-read-feed (car f)))
+          (append newsticker-url-list-defaults newsticker-url-list))))
+
+(defun newsticker--cache-read-feed (feed-name)
+  "Read cache data for feed named FEED-NAME."
+  (let ((file-name (concat (newsticker--cache-dir) "/" feed-name "/data"))
+        (coding-system-for-read 'utf-8))
+    (when (file-exists-p file-name)
+      (with-temp-buffer
+        (insert-file-contents file-name)
+        (goto-char (point-min))
+        (condition-case nil
+            (add-to-list 'newsticker--cache (cons (intern feed-name)
+                                                  (read (current-buffer))))
+          (error
+           (message "Error while reading newsticker cache file %s!"
+                    file-name))
+          (setq newsticker--cache nil))))))
+
 ;; ======================================================================
 ;;; Statistics
 ;; ======================================================================
@@ -2316,3 +2371,7 @@
 
 ;; arch-tag: 0e37b658-56e9-49ab-90f9-f2df57e1a659
 ;;; newsticker-backend.el ends here
+
+(provide 'newst-backend)
+
+;;; newst-backend.el ends here
--- a/lisp/net/newst-reader.el	Mon Nov 24 19:24:45 2008 +0000
+++ b/lisp/net/newst-reader.el	Mon Nov 24 19:40:35 2008 +0000
@@ -6,7 +6,7 @@
 ;; Author:      Ulf Jasper <ulf.jasper@web.de>
 ;; Filename:    newst-reader.el
 ;; URL:         http://www.nongnu.org/newsticker
-;; Time-stamp:  "25. August 2008, 19:35:45 (ulf)"
+;; Time-stamp:  "18. November 2008, 20:06:13 (ulf)"
 
 ;; ======================================================================
 
@@ -262,7 +262,7 @@
 If DISABLED is non-nil the image will be converted to a disabled look
 \(unless `newsticker-enable-logo-manipulations' is not t\).
 Return the image."
-  (let ((image-name (concat newsticker-imagecache-dirname "/"
+  (let ((image-name (concat (newsticker--images-dir)
                             (symbol-name feed-name-symbol)))
         (img nil))
     (when (file-exists-p image-name)
--- a/lisp/net/newst-treeview.el	Mon Nov 24 19:24:45 2008 +0000
+++ b/lisp/net/newst-treeview.el	Mon Nov 24 19:40:35 2008 +0000
@@ -7,7 +7,7 @@
 ;; URL:         http://www.nongnu.org/newsticker
 ;; Created:     2007
 ;; Keywords:    News, RSS, Atom
-;; Time-stamp:  "31. Oktober 2008, 20:44:46 (ulf)"
+;; Time-stamp:  "24. November 2008, 19:49:20 (ulf)"
 
 ;; ======================================================================
 
@@ -149,7 +149,8 @@
 ;; ======================================================================
 (defvar newsticker--treeview-windows nil)
 (defvar newsticker--treeview-buffers nil)
-(defvar newsticker--treeview-current-feed nil)
+(defvar newsticker--treeview-current-feed nil
+  "Feed name of currently shown item.")
 (defvar newsticker--treeview-current-vfeed nil)
 (defvar newsticker--treeview-list-show-feed nil)
 (defvar newsticker--saved-window-config nil)
@@ -691,8 +692,9 @@
       (newsticker-treeview-mode)
       (goto-char (point-min)))))
 
-(defun newsticker--treeview-item-show (item feed)
-  "Show news ITEM coming from FEED in treeview item buffer."
+(defun newsticker--treeview-item-show (item feed-name-symbol)
+  "Show news ITEM coming from FEED-NAME-SYMBOL in treeview item buffer."
+  (setq newsticker--treeview-current-feed (symbol-name feed-name-symbol))
   (save-excursion
     (set-buffer (newsticker--treeview-item-buffer))
     (when (fboundp 'w3m-process-stop)
@@ -706,7 +708,7 @@
       (kill-all-local-variables)
       (remove-overlays)
 
-      (when (and item feed)
+      (when (and item feed-name-symbol)
         (let ((wwidth (1- (window-width (newsticker--treeview-item-window)))))
           (if newsticker-use-full-width
               (set (make-local-variable 'fill-column) wwidth))
@@ -726,10 +728,10 @@
         (goto-char (point-min))
         ;; insert logo at top
         (let* ((newsticker-enable-logo-manipulations nil)
-               (img (newsticker--image-read feed nil)))
+               (img (newsticker--image-read feed-name-symbol nil)))
           (if (and (display-images-p) img)
               (newsticker--insert-image img (car item))
-            (insert (newsticker--real-feed-name feed))))
+            (insert (newsticker--real-feed-name feed-name-symbol))))
         (add-text-properties (point-min) (point)
                              (list 'face 'newsticker-feed-face
                                    'mouse-face 'highlight
@@ -989,8 +991,6 @@
 well.  Argument IGNORE is ignored.  Note that this function, if
 called recursively, makes w invalid.  You should keep w's nt-id in
 that case."
-  ;;(message "newsticker--treeview-tree-update-tag %s, %s" (widget-get w :tag)
-    ;;       (widget-type w))
   (let* ((parent (widget-get w :parent))
          (feed (or (widget-get w :nt-feed) (widget-get parent :nt-feed)))
          (vfeed (or (widget-get w :nt-vfeed) (widget-get parent :nt-vfeed)))
@@ -1207,7 +1207,6 @@
   "Update all treeview buffers and windows.
 Note: does not update the layout."
   (interactive)
-  (newsticker--cache-update)
   (newsticker--group-manage-orphan-feeds)
   (newsticker--treeview-list-update t)
   (newsticker--treeview-item-update)
@@ -1223,7 +1222,6 @@
 (defun newsticker-treeview-quit ()
   "Quit newsticker treeview."
   (interactive)
-  (newsticker-treeview-save)
   (setq newsticker--sentinel-callback nil)
   (bury-buffer "*Newsticker Tree*")
   (bury-buffer "*Newsticker List*")
@@ -1237,10 +1235,9 @@
 (defun newsticker-treeview-save ()
   "Save newsticker data including treeview settings."
   (interactive)
-  (newsticker--cache-save)
   (save-excursion
     (let ((coding-system-for-write 'utf-8)
-          (buf (find-file-noselect newsticker-groups-filename)))
+          (buf (find-file-noselect (concat newsticker-dir "/groups"))))
       (when buf
         (set-buffer buf)
         (setq buffer-undo-list t)
@@ -1252,8 +1249,14 @@
 (defun newsticker--treeview-load ()
   "Load treeview settings."
   (let* ((coding-system-for-read 'utf-8)
-         (buf (and (file-exists-p newsticker-groups-filename)
-                   (find-file-noselect newsticker-groups-filename))))
+         (filename
+          (or (and (file-exists-p newsticker-groups-filename)
+                   (y-or-n-p
+                    (format "Old newsticker groups (%s) file exists.  Read it? "
+                            newsticker-groups-filename))
+                   newsticker-groups-filename)
+              (concat newsticker-dir "/groups")))
+         (buf (find-file-noselect filename)))
     (when buf
       (set-buffer buf)
       (goto-char (point-min))
@@ -1377,7 +1380,9 @@
     (setcar (nthcdr 4 item) new-age)
     ;; clean up ticker FIXME
     )
-  (newsticker--cache-update))
+  (newsticker--cache-save-feed
+   (newsticker--cache-get-feed (intern newsticker--treeview-current-feed)))
+  (newsticker--treeview-tree-do-update-tags newsticker--treeview-vfeed-tree))
 
 (defun newsticker-treeview-mark-list-items-old ()
   "Mark all listed items as old."
@@ -1908,10 +1913,9 @@
   "Actually handle click event.
 POS gives the position where EVENT occurred."
   (interactive)
-  (unless pos (setq pos (point)))
-  (let ((pos (or pos (point)))
-        (nt-id (get-text-property pos :nt-id))
-        (item (get-text-property pos :nt-item)))
+  (let* ((pos (or pos (point)))
+         (nt-id (get-text-property pos :nt-id))
+         (item (get-text-property pos :nt-item)))
     (cond (item
            ;; click in list buffer
            (newsticker-treeview-show-item))