~ubuntu-branches/ubuntu/oneiric/monodevelop/oneiric

« back to all changes in this revision

Viewing changes to src/addins/MonoDevelop.SourceEditor2/MonoDevelop.SourceEditor/AutoSave.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2011-06-27 17:03:13 UTC
  • mto: (1.8.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 54.
  • Revision ID: james.westby@ubuntu.com-20110627170313-6cvz3s19x6e9hqe9
ImportĀ upstreamĀ versionĀ 2.5.92+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// 
 
1
//
2
2
// AutoSave.cs
3
 
//  
 
3
//
4
4
// Author:
5
5
//       Mike KrĆ¼ger <mkrueger@novell.com>
6
 
// 
 
6
//
7
7
// Copyright (c) 2009 Novell, Inc (http://www.novell.com)
8
 
// 
 
8
//
9
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
10
10
// of this software and associated documentation files (the "Software"), to deal
11
11
// in the Software without restriction, including without limitation the rights
12
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
13
// copies of the Software, and to permit persons to whom the Software is
14
14
// furnished to do so, subject to the following conditions:
15
 
// 
 
15
//
16
16
// The above copyright notice and this permission notice shall be included in
17
17
// all copies or substantial portions of the Software.
18
 
// 
 
18
//
19
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25
25
// THE SOFTWARE.
26
26
 
27
27
using System;
 
28
using System.Collections.Generic;
28
29
using System.IO;
29
30
using System.Threading;
30
31
using MonoDevelop.Core;
31
32
 
32
33
namespace MonoDevelop.SourceEditor
33
34
{
34
 
        public class AutoSave : IDisposable
 
35
        static class AutoSave
35
36
        {
36
 
                static string autoSavePath = Path.Combine (PropertyService.Get<string> ("MonoDevelop.CodeCompletion.DataDirectory", String.Empty), "AutoSave");
37
 
                
 
37
                //FIXME: is this path a good one? wouldn't it be better to put autosaves beside the files anyway?
 
38
                static string autoSavePath = PropertyService.Locations.Cache.Combine ("AutoSave");
 
39
 
38
40
                static AutoSave ()
39
41
                {
40
42
                        if (!Directory.Exists (autoSavePath))
41
43
                                Directory.CreateDirectory (autoSavePath);
 
44
                        StartAutoSaveThread ();
42
45
                }
43
 
                
 
46
 
44
47
                static string GetAutoSaveFileName (string fileName)
45
48
                {
46
49
                        if (fileName == null)
49
52
                        newFileName = Path.Combine (autoSavePath, newFileName.Replace(',','_').Replace(" ","").Replace (":","").Replace (Path.DirectorySeparatorChar, '_').Replace (Path.AltDirectorySeparatorChar, '_'));
50
53
                        return newFileName;
51
54
                }
52
 
                
 
55
 
53
56
                public static bool AutoSaveExists (string fileName)
54
57
                {
55
58
                        return File.Exists (GetAutoSaveFileName (fileName));
56
59
                }
57
 
                
 
60
 
58
61
                static void CreateAutoSave (string fileName, string content)
59
62
                {
60
63
                        try {
64
67
                        } catch (Exception) {
65
68
                        }
66
69
                }
67
 
                
68
 
#region AutoSave 
69
 
                class FileContent 
 
70
 
 
71
#region AutoSave
 
72
                class FileContent
70
73
                {
71
74
                        public string FileName;
72
75
                        public Mono.TextEditor.Document Content;
77
80
                                this.Content = content;
78
81
                        }
79
82
                }
80
 
                
81
 
                public bool Running {
 
83
 
 
84
                public static bool Running {
82
85
                        get {
83
86
                                return autoSaveThreadRunning;
84
87
                        }
85
88
                }
86
 
                
87
 
                public bool IsDirty {
88
 
                        get;
89
 
                        set;
90
 
                }
91
 
                
92
 
                public string FileName {
93
 
                        get;
94
 
                        set;
95
 
                }
96
 
                
97
 
                public AutoSave ()
98
 
                {
99
 
                        IsDirty = false;
100
 
                }
101
 
                AutoResetEvent resetEvent = new AutoResetEvent (false);
102
 
                bool autoSaveThreadRunning = false;
103
 
                Thread autoSaveThread;
104
 
                bool fileChanged   = false;
105
 
                FileContent content = null;
106
 
                object contentLock = new object ();
107
 
                
108
 
                public void StartAutoSaveThread ()
 
89
 
 
90
                static AutoResetEvent resetEvent = new AutoResetEvent (false);
 
91
                static bool autoSaveThreadRunning = false;
 
92
                static Thread autoSaveThread;
 
93
                static Queue<FileContent> queue = new Queue<FileContent> ();
 
94
                static object contentLock = new object ();
 
95
 
 
96
                static void StartAutoSaveThread ()
109
97
                {
110
98
                        autoSaveThreadRunning = true;
111
99
                        if (autoSaveThread == null) {
115
103
                                autoSaveThread.Start ();
116
104
                        }
117
105
                }
118
 
                
119
 
                void AutoSaveThread ()
 
106
 
 
107
                static void AutoSaveThread ()
120
108
                {
121
109
                        while (autoSaveThreadRunning) {
122
110
                                resetEvent.WaitOne ();
123
 
                                lock (contentLock) {
124
 
                                        if (fileChanged) {
125
 
                                                CreateAutoSave (content.FileName, content.Content.Text);
126
 
                                                fileChanged = false;
 
111
                                while (queue.Count > 0) {
 
112
                                        var content = queue.Dequeue ();
 
113
                                        lock (contentLock) {
 
114
                                                string text;
 
115
                                                try {
 
116
                                                        text = content.Content.Text;
 
117
                                                } catch (Exception e) {
 
118
                                                        LoggingService.LogError ("Exception in auto save thread.", e);
 
119
                                                        continue;
 
120
                                                }
 
121
                                                CreateAutoSave (content.FileName, text);
127
122
                                        }
128
123
                                }
129
124
                        }
130
125
                }
131
 
                
132
 
                public string LoadAutoSave ()
 
126
 
 
127
                public static string LoadAutoSave (string fileName)
133
128
                {
134
 
                        string autoSaveFileName = GetAutoSaveFileName (FileName);
 
129
                        string autoSaveFileName = GetAutoSaveFileName (fileName);
135
130
                        return File.ReadAllText (autoSaveFileName);
136
131
                }
137
 
                
138
 
                public void RemoveAutoSaveFile ()
 
132
 
 
133
                public static void RemoveAutoSaveFile (string fileName)
139
134
                {
140
 
                        IsDirty = false;
141
 
                        if (AutoSaveExists (FileName)) {
142
 
                                string autoSaveFileName = GetAutoSaveFileName (FileName);
 
135
                        if (AutoSaveExists (fileName)) {
 
136
                                string autoSaveFileName = GetAutoSaveFileName (fileName);
143
137
                                try {
144
138
                                        lock (contentLock) {
145
139
                                                File.Delete (autoSaveFileName);
149
143
                                }
150
144
                        }
151
145
                }
152
 
                
153
 
                public void InformAutoSaveThread (Mono.TextEditor.Document content)
 
146
 
 
147
                public static void InformAutoSaveThread (Mono.TextEditor.Document content)
154
148
                {
155
 
                        if (FileName == null || content == null)
 
149
                        if (content == null)
156
150
                                return;
157
 
                        if (!autoSaveThreadRunning)
158
 
                                StartAutoSaveThread ();
159
 
                        
160
 
                        IsDirty = true;
161
 
                        lock (contentLock) {
162
 
                                fileChanged = true;
163
 
                                this.content = new FileContent (FileName, content);
164
 
                                
 
151
                        if (content.IsDirty) {
 
152
                                queue.Enqueue (new FileContent (content.FileName, content));
165
153
                                resetEvent.Set ();
 
154
                        } else {
 
155
                                RemoveAutoSaveFile (content.FileName);
166
156
                        }
167
157
                }
168
 
                
169
 
                public void Dispose ()
 
158
 
 
159
/*              public static void Dispose ()
170
160
                {
171
161
                        autoSaveThreadRunning = false;
172
162
                        if (autoSaveThread != null) {
174
164
                                autoSaveThread.Join ();
175
165
                                autoSaveThread = null;
176
166
                        }
177
 
                }
 
167
                }*/
178
168
#endregion
179
 
        
180
169
        }
181
170
}