~ubuntu-branches/ubuntu/trusty/pdfmod/trusty

« back to all changes in this revision

Viewing changes to src/PdfMod/Core/Client.cs

  • Committer: Bazaar Package Importer
  • Author(s): Chow Loong Jin
  • Date: 2010-06-18 03:44:46 UTC
  • Revision ID: james.westby@ubuntu.com-20100618034446-bogifrsscpayp361
Tags: upstream-0.8.3
ImportĀ upstreamĀ versionĀ 0.8.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (C) 2009 Novell, Inc.
 
2
//
 
3
// This program is free software; you can redistribute it and/or
 
4
// modify it under the terms of the GNU General Public License
 
5
// as published by the Free Software Foundation; either version 2
 
6
// of the License, or (at your option) any later version.
 
7
//
 
8
// This program is distributed in the hope that it will be useful,
 
9
// but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
// GNU General Public License for more details.
 
12
//
 
13
// You should have received a copy of the GNU General Public License
 
14
// along with this program; if not, write to the Free Software
 
15
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
16
 
 
17
using System;
 
18
using System.Collections.Generic;
 
19
using System.IO;
 
20
 
 
21
using Mono.Unix;
 
22
 
 
23
using Hyena;
 
24
 
 
25
using PdfMod.Pdf;
 
26
 
 
27
namespace PdfMod.Core
 
28
{
 
29
    public abstract class Client
 
30
    {
 
31
        static readonly string old_cache_dir = Path.Combine (System.Environment.GetFolderPath (System.Environment.SpecialFolder.ApplicationData), "pdfmod");
 
32
        static readonly string CacheDir = Path.Combine (XdgBaseDirectorySpec.GetUserDirectory ("XDG_CACHE_HOME", ".cache"), "pdfmod");
 
33
 
 
34
        public Document Document { get; protected set; }
 
35
        public static Configuration Configuration { get; private set; }
 
36
 
 
37
        public event EventHandler DocumentLoaded;
 
38
 
 
39
        static Client ()
 
40
        {
 
41
            Configuration = new Configuration ();
 
42
            InitCache ();
 
43
        }
 
44
 
 
45
        public Client ()
 
46
        {
 
47
        }
 
48
 
 
49
        protected void OnDocumentLoaded ()
 
50
        {
 
51
            var handler = DocumentLoaded;
 
52
            if (handler != null) {
 
53
                handler (this, EventArgs.Empty);
 
54
            }
 
55
        }
 
56
 
 
57
        protected void LoadFiles ()
 
58
        {
 
59
            // This variable should probably be marked volatile
 
60
            Hyena.Log.Debugging = true;
 
61
            LoadFiles (ApplicationContext.CommandLine.Files);
 
62
        }
 
63
 
 
64
        public abstract void LoadFiles (IList<string> files);
 
65
 
 
66
        public void LoadPath (string path)
 
67
        {
 
68
            LoadPath (path, null);
 
69
        }
 
70
 
 
71
        public abstract void LoadPath (string path, string suggestedFilename);
 
72
 
 
73
        static void InitCache ()
 
74
        {
 
75
            // Remove the old "cache" dir that really ended up being ~/.config/
 
76
            if (Directory.Exists (old_cache_dir)) {
 
77
                try {
 
78
                    foreach (string file in Directory.GetFiles (CacheDir)) {
 
79
                        if (file.Contains ("tmpfile-")) {
 
80
                            File.Delete (file);
 
81
                        }
 
82
                    }
 
83
                } catch {}
 
84
            }
 
85
 
 
86
            // Make sure the new one exists
 
87
            try {
 
88
                Directory.CreateDirectory (CacheDir);
 
89
                Log.DebugFormat ("Cache directory set to {0}", CacheDir);
 
90
 
 
91
                // Remove any tmp files that haven't been touched in three days
 
92
                var too_old = DateTime.Now;
 
93
                too_old.AddDays (-3);
 
94
                foreach (string file in Directory.GetFiles (CacheDir)) {
 
95
                    if (file.Contains ("tmpfile-")) {
 
96
                        if (File.GetLastAccessTime (file) < too_old) {
 
97
                            File.Delete (file);
 
98
                        }
 
99
                    }
 
100
                }
 
101
            } catch (Exception e) {
 
102
                Log.Exception (String.Format ("Unable to create cache directory: {0}", CacheDir), e);
 
103
            }
 
104
        }
 
105
 
 
106
        public static string GetTmpFilename ()
 
107
        {
 
108
            string filename = null;
 
109
            int i = 0;
 
110
            while (filename == null) {
 
111
                filename = Path.Combine (CacheDir, "tmpfile-" + i++);
 
112
                if (File.Exists (filename)) {
 
113
                    filename = null;
 
114
                }
 
115
            }
 
116
            return filename;
 
117
        }
 
118
    }
 
119
}