;; Copyright (C) 2001 Michel Schinz

;; Emacs Lisp Archive Entry
;; Filename: desktop-kde-recent.el
;; Version: 1.0
;; Keywords: desktop, kde
;; Author: Michel Schinz <schinz@alphanet.ch>
;; Maintainer: Michel Schinz <schinz@alphanet.ch>
;; Created: 28 Feb 2001
;; Description: put entries in KDE's Recent Document menu for Emacs desktops
;; URL: http://lampwww.epfl.ch/~schinz/elisp/
;; Compatibility: Emacs20, Emacs21

;; 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., 59 Temple Place, Suite 330, Boston,
;; MA 02111-1307 USA

;; $Id: desktop-kde-recent.el,v 1.1.1.1 2001/03/06 07:59:36 schinz Exp $

;; Note: this file is useful to you only if you use both KDE and Emacs
;; desktop files.
;;
;; To use it, place it in some place where Emacs can find it, compile
;; it (if you want) and add the following lines to your .emacs:
;;
;;   (require 'desktop-kde-recent)
;;   (add-hook 'desktop-save-hook #'desktop-kde-recent)
;;
;; Then each time the desktop is saved (usually when you exit Emacs),
;; an entry for the desktop is added to KDE's Recent Document menu.
;;
;; Notice that you can also create a directory to store only Emacs
;; desktop entries, reference it in `desktop-kde-recent-dir' and have
;; a Quick Browser in your KDE panel showing the contents of that
;; directory. That way you have a nice menu to start Emacs on your
;; various projects.

(defcustom desktop-kde-encode-function
  (if (fboundp 'md5) #'md5 #'base64-encode-string)
  "*Function to generate a file name from the given directory name."
  :type 'function
  :group 'desktop)

(defcustom desktop-kde-recent-skeleton "[Desktop Entry]
Exec=sh -c \"cd %s; emacs\"
Icon=emacs
Name=Emacs in %s
Type=Application"

  "*Skeleton for the file to put in KDE's directory of recent documents.

All the %s in this string will be replaced by the name of the directory in
which the desktop is saved."
  :type 'string
  :group 'desktop)

(defcustom desktop-kde-recent-dir
  (expand-file-name "~/.kde/share/apps/RecentDocuments")
  "*Directory containing KDE's recent documents.

This does not have to point to KDE's Recent Documents directory: you can
perfectly well set that to some directory in which you only store Emacs desktop
files and then put a Quick Browser for that directory in your KDE panel."
  :type 'directory
  :group 'desktop)

(defun desktop-kde-recent ()
  (when desktop-dirname
    (let* ((tmp-buf (generate-new-buffer "desktop-kde"))
	   (dir-name (expand-file-name desktop-dirname))
	   (file-name (concat (expand-file-name
			       (funcall desktop-kde-encode-function dir-name)
			       desktop-kde-recent-dir)
			      ".desktop")))
      (with-current-buffer tmp-buf
	(insert desktop-kde-recent-skeleton)
	(while (search-backward "%s" nil t)
	  (replace-match dir-name))
	(let ((make-backup-files nil))
	  (write-file file-name)))
      (kill-buffer tmp-buf))))

(provide 'desktop-kde-recent)
