[pdfmod] Use the XDG cache dir, and cleanup tmp files better



commit 4aadcf2387dbbd71e643ea459da658d08157eb32
Author: Gabriel Burt <gabriel burt gmail com>
Date:   Mon Aug 10 10:52:57 2009 -0700

    Use the XDG cache dir, and cleanup tmp files better

 lib/Hyena.dll               |  Bin 235008 -> 235520 bytes
 lib/Hyena.dll.mdb           |  Bin 112051 -> 112477 bytes
 src/PdfMod/PdfMod/PdfMod.cs |   49 +++++++++++++++++++++++++++++++++---------
 3 files changed, 38 insertions(+), 11 deletions(-)
---
diff --git a/lib/Hyena.dll b/lib/Hyena.dll
index 601a44f..2f3db6a 100755
Binary files a/lib/Hyena.dll and b/lib/Hyena.dll differ
diff --git a/lib/Hyena.dll.mdb b/lib/Hyena.dll.mdb
index 767586d..a57fd45 100644
Binary files a/lib/Hyena.dll.mdb and b/lib/Hyena.dll.mdb differ
diff --git a/src/PdfMod/PdfMod/PdfMod.cs b/src/PdfMod/PdfMod/PdfMod.cs
index 15f85b7..21b79b0 100644
--- a/src/PdfMod/PdfMod/PdfMod.cs
+++ b/src/PdfMod/PdfMod/PdfMod.cs
@@ -1,4 +1,5 @@
 using System;
+using System.IO;
 
 using Mono.Unix;
 using Gtk;
@@ -15,7 +16,9 @@ namespace PdfMod
     public class PdfMod
     {
         private static int app_count = 0;
-        private static readonly string CacheDir = System.IO.Path.Combine (System.Environment.GetFolderPath (System.Environment.SpecialFolder.ApplicationData), "pdfmod");
+
+        private static readonly string old_cache_dir = Path.Combine (System.Environment.GetFolderPath (System.Environment.SpecialFolder.ApplicationData), "pdfmod");
+        private static readonly string CacheDir = Path.Combine (XdgBaseDirectorySpec.GetUserDirectory ("XDG_CACHE_HOME", ".cache"), "pdfmod");
 
         public static void Main (string[] args)
         {
@@ -32,12 +35,7 @@ namespace PdfMod
             Hyena.Log.DebugFormat ("Starting PdfMod");
 
             InitCatalog ("/usr/local/share/locale/", Defines.PREFIX + "/share/locale/");
-
-            try {
-                System.IO.Directory.CreateDirectory (CacheDir);
-            } catch (Exception e) {
-                Log.Exception (String.Format ("Unable to create cache directory: {0}", CacheDir), e);
-            }
+            InitCache ();
 
             var app = new PdfMod ();
             RunIdle (app.LoadFiles);
@@ -368,8 +366,8 @@ namespace PdfMod
         private static void InitCatalog (params string [] dirs)
         {
             foreach (var dir in dirs) {
-                var test_file = System.IO.Path.Combine (dir, "fr/LC_MESSAGES/pdfmod.mo");
-                if (System.IO.File.Exists (test_file)) {
+                var test_file = Path.Combine (dir, "fr/LC_MESSAGES/pdfmod.mo");
+                if (File.Exists (test_file)) {
                     Log.DebugFormat ("Initializing i18n catalog from {0}", dir);
                     Catalog.Init ("pdfmod", dir);
                     break;
@@ -377,13 +375,42 @@ namespace PdfMod
             }
         }
 
+        private static void InitCache ()
+        {
+            // Remove the old "cache" dir that really ended up being ~/.config/
+            if (Directory.Exists (old_cache_dir)) {
+                try {
+                    Directory.Delete (old_cache_dir);
+                } catch {}
+            }
+
+            // Make sure the new one exists
+            try {
+                Directory.CreateDirectory (CacheDir);
+                Log.DebugFormat ("Cache directory set to {0}", CacheDir);
+
+                // Remove any tmp files that haven't been touched in three days
+                var too_old = DateTime.Now;
+                too_old.AddDays (-3);
+                foreach (string file in Directory.GetFiles (CacheDir)) {
+                    if (file.Contains ("tmpfile-")) {
+                        if (File.GetLastAccessTime (file) < too_old) {
+                            File.Delete (file);
+                        }
+                    }
+                }
+            } catch (Exception e) {
+                Log.Exception (String.Format ("Unable to create cache directory: {0}", CacheDir), e);
+            }
+        }
+
         public static string GetTmpFilename ()
         {
             string filename = null;
             int i = 0;
             while (filename == null) {
-                filename = System.IO.Path.Combine (CacheDir, "tmpfile-" + i++);
-                if (System.IO.File.Exists (filename)) {
+                filename = Path.Combine (CacheDir, "tmpfile-" + i++);
+                if (File.Exists (filename)) {
                     filename = null;
                 }
             }



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]