~ubuntu-branches/ubuntu/precise/gnome-do/precise-proposed

« back to all changes in this revision

Viewing changes to Do.Addins/src/Do.Universe/FileItemSource.cs

  • Committer: Bazaar Package Importer
  • Author(s): Christopher James Halse Rogers
  • Date: 2008-09-14 10:09:40 UTC
  • mto: (0.1.8 sid)
  • mto: This revision was merged to the branch mainline in revision 7.
  • Revision ID: james.westby@ubuntu.com-20080914100940-kyghudg7py14bu2z
Tags: upstream-0.6.0.0
ImportĀ upstreamĀ versionĀ 0.6.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* FileItemSource.cs
2
 
 *
3
 
 * GNOME Do is the legal property of its developers. Please refer to the
4
 
 * COPYRIGHT file distributed with this
5
 
 * source distribution.
6
 
 *
7
 
 * This program is free software: you can redistribute it and/or modify
8
 
 * it under the terms of the GNU General Public License as published by
9
 
 * the Free Software Foundation, either version 3 of the License, or
10
 
 * (at your option) any later version.
11
 
 *
12
 
 * This program is distributed in the hope that it will be useful,
13
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 
 * GNU General Public License for more details.
16
 
 *
17
 
 * You should have received a copy of the GNU General Public License
18
 
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 
 */
20
 
 
21
 
using System;
22
 
using System.IO;
23
 
using System.Collections.Generic;
24
 
 
25
 
using Do.Addins;
26
 
 
27
 
namespace Do.Universe {
28
 
 
29
 
        /// <summary>
30
 
        /// Indexes files recursively starting in a specific directory.
31
 
        /// </summary>
32
 
        public class FileItemSource : IItemSource {
33
 
 
34
 
                List<IItem> items;
35
 
                bool include_hidden;
36
 
                IEnumerable<DirectoryLevelPair> dirs;
37
 
 
38
 
                struct DirectoryLevelPair {
39
 
                        public string Directory;
40
 
                        public int Levels;
41
 
                        
42
 
                        public DirectoryLevelPair (string dir, int levels)
43
 
                        {
44
 
                                Directory = dir.Replace ("~", Paths.UserHome);
45
 
                                Levels = levels;
46
 
                        }
47
 
                }
48
 
        
49
 
                static string ConfigFile {
50
 
                        get {
51
 
                                return Paths.Combine (Paths.ApplicationData,
52
 
                                        "FileItemSource.config");
53
 
                        }
54
 
                }
55
 
 
56
 
                static DirectoryLevelPair [] DefaultDirectories {
57
 
                        get     {
58
 
                                return new DirectoryLevelPair [] {
59
 
                                        new DirectoryLevelPair ("/home",                1),
60
 
                                        new DirectoryLevelPair (Paths.UserHome, 1),
61
 
                                        new DirectoryLevelPair (Desktop,                1),
62
 
                                        new DirectoryLevelPair (Documents,              3),
63
 
                                };
64
 
                        }
65
 
                }
66
 
 
67
 
                static IEnumerable<IItem> GtkBookmarkItems {
68
 
                        get {
69
 
                                string line;
70
 
                                StreamReader reader;
71
 
                                List<IItem> bookmarks;
72
 
 
73
 
                                reader = null;
74
 
                                bookmarks = new List<IItem> ();
75
 
                                try {
76
 
                                        reader = File.OpenText (
77
 
                                                Path.Combine (Paths.UserHome, ".gtk-bookmarks"));
78
 
                                } catch {
79
 
                                        return bookmarks;
80
 
                                }
81
 
 
82
 
                                while (null != (line = reader.ReadLine ())) {
83
 
                                        if (!line.StartsWith ("file://")) continue;
84
 
                                        line = line.Substring ("file://".Length);
85
 
                                        if (File.Exists (line) || Directory.Exists (line))
86
 
                                                bookmarks.Add (new FileItem (line));
87
 
                                }
88
 
                                return bookmarks;
89
 
                        }
90
 
                }
91
 
 
92
 
                public FileItemSource ()
93
 
                {
94
 
                        dirs = Deserialize ();
95
 
                        items = new List<IItem> ();
96
 
                        include_hidden = false;
97
 
                        UpdateItems ();
98
 
                }
99
 
                                
100
 
                
101
 
                static IEnumerable<DirectoryLevelPair> Deserialize ()
102
 
                {
103
 
                        List<DirectoryLevelPair> dirs;
104
 
 
105
 
                        if (!File.Exists (ConfigFile)) {
106
 
                                Serialize (DefaultDirectories);
107
 
                                return DefaultDirectories;
108
 
                        }
109
 
                        
110
 
                        dirs = new List<DirectoryLevelPair> ();
111
 
                        if (File.Exists (ConfigFile)) {
112
 
                                try {
113
 
                                        foreach (string line in File.ReadAllLines (ConfigFile)) {
114
 
                                                string [] parts;
115
 
                                                if (line.Trim ().StartsWith ("#")) continue;
116
 
                                                parts = line.Trim ().Split (':');
117
 
                                                if (parts.Length != 2) continue;
118
 
                                                dirs.Add (new DirectoryLevelPair (parts [0].Trim (),
119
 
                                                          int.Parse (parts [1].Trim ())));
120
 
                                        }
121
 
                                } catch (Exception e) {
122
 
                                        Console.Error.WriteLine (
123
 
                                                "Error reading FileItemSource config file {0}: {1}",
124
 
                                                ConfigFile, e.Message);
125
 
                                }
126
 
                        } 
127
 
                        return dirs;
128
 
                }
129
 
                
130
 
                static void Serialize (IEnumerable<DirectoryLevelPair> dirs)
131
 
                {
132
 
                        string configDir = Path.GetDirectoryName (ConfigFile);
133
 
                        try {
134
 
                                if (!Directory.Exists (configDir))
135
 
                                        Directory.CreateDirectory (configDir);
136
 
                                foreach (DirectoryLevelPair pair in dirs) {
137
 
                                        File.AppendAllText (ConfigFile,
138
 
                                                string.Format ("{0}: {1}\n", pair.Directory,
139
 
                                                        pair.Levels)); 
140
 
                                }
141
 
                        } catch (Exception e) {
142
 
                                Console.Error.WriteLine (
143
 
                                        "Error saving FileItemSource config file {0}: {1}",
144
 
                                        ConfigFile, e.Message);
145
 
                        }
146
 
                }
147
 
 
148
 
                public Type [] SupportedItemTypes {
149
 
                        get {
150
 
                                return new Type [] {
151
 
                                        typeof (IFileItem),
152
 
                                        typeof (ITextItem),
153
 
                                };
154
 
                        }
155
 
                }
156
 
                
157
 
                public string Name {
158
 
                        get { return "File Indexer"; }
159
 
                }
160
 
                
161
 
                public string Description {
162
 
                        get {
163
 
                                return string.Format ("Frequently used files and folders.");
164
 
                        }
165
 
                }
166
 
                
167
 
                public string Icon {
168
 
                        get { return "folder"; }
169
 
                }
170
 
                
171
 
                public ICollection<IItem> Items {
172
 
                        get { return items; }
173
 
                }
174
 
                
175
 
                public ICollection<IItem> ChildrenOfItem (IItem item)
176
 
                {
177
 
                        IFileItem fi;
178
 
                        List<IItem> children;
179
 
                        
180
 
                        if (item is ITextItem) {
181
 
                                string path = (item as ITextItem).Text;
182
 
                                path = path.Replace ("~", Paths.UserHome);
183
 
                                if (!Directory.Exists (path)) return null;
184
 
                                fi = new FileItem (path);
185
 
                        } else {
186
 
                                fi = item as IFileItem;
187
 
                        }
188
 
                        children = new List<IItem> ();
189
 
                        if (FileItem.IsDirectory (fi)) {
190
 
                                foreach (string path in
191
 
                                        Directory.GetFileSystemEntries (fi.Path)) {
192
 
                                        children.Add (new FileItem (path));
193
 
                                }
194
 
                        }
195
 
                        return children;
196
 
                }
197
 
                
198
 
                public void UpdateItems ()
199
 
                {
200
 
                        items.Clear ();
201
 
                        items.AddRange (GtkBookmarkItems);
202
 
                        foreach (DirectoryLevelPair dir in dirs) {
203
 
                                ReadItems (dir.Directory, dir.Levels);
204
 
                        }
205
 
                }
206
 
                
207
 
                /// <summary>
208
 
                /// Create items for files found in a given directory. Recurses a
209
 
                /// given number of levels deep into nested directories.
210
 
                /// </summary>
211
 
                /// <param name="dir">
212
 
                /// A <see cref="System.String"/> containing the absolute path
213
 
                /// to the directory to read FileItems from.
214
 
                /// </param>
215
 
                /// <param name="levels">
216
 
                /// A <see cref="System.Int32"/> specifying the number of levels
217
 
                /// of nested directories to explore.
218
 
                /// </param>
219
 
                protected virtual void ReadItems (string dir, int levels)
220
 
                {
221
 
                        FileItem item;
222
 
                        string [] files;
223
 
                        string [] directories;
224
 
                        
225
 
                        if (levels == 0) return;
226
 
 
227
 
                        try {
228
 
                                files = Directory.GetFiles (dir);
229
 
                                directories = Directory.GetDirectories (dir);
230
 
                        } catch {
231
 
                                return;
232
 
                        }
233
 
                        foreach (string file in files) {
234
 
                                // Ignore system/hidden files.
235
 
                                if (!include_hidden && FileItem.IsHidden (file)) continue;
236
 
 
237
 
                                item = new FileItem (file);
238
 
                                items.Add (item);
239
 
                        }
240
 
                        foreach (string directory in directories) {
241
 
                                if (!include_hidden && FileItem.IsHidden (directory)) continue;
242
 
 
243
 
                                item = new FileItem (directory);
244
 
                                items.Add (item);
245
 
                                ReadItems (directory, levels - 1);
246
 
                        }
247
 
                }
248
 
 
249
 
                static string Music {
250
 
                        get {
251
 
                                return Paths.ReadXdgUserDir ("XDG_MUSIC_DIR", "Music");
252
 
                        }
253
 
                }
254
 
 
255
 
                static string Pictures {
256
 
                        get {
257
 
                                return Paths.ReadXdgUserDir ("XDG_PICTURES_DIR", "Pictures");
258
 
                        }
259
 
                }
260
 
 
261
 
                static string Videos {
262
 
                        get {
263
 
                                return Paths.ReadXdgUserDir ("XDG_VIDEOS_DIR", "Videos");
264
 
                        }
265
 
                }
266
 
 
267
 
                static string Desktop {
268
 
                        get {
269
 
                                return Paths.ReadXdgUserDir ("XDG_DESKTOP_DIR", "Desktop");
270
 
                        }
271
 
                }
272
 
 
273
 
                static string Downloads {
274
 
                        get {
275
 
                                return Paths.ReadXdgUserDir ("XDG_DOWNLOAD_DIR", "Downloads");
276
 
                        }
277
 
                }
278
 
 
279
 
                static string Documents {
280
 
                        get {
281
 
                                return Paths.ReadXdgUserDir ("XDG_DOCUMENTS_DIR", "Documents");
282
 
                        }
283
 
                }
284
 
        }
285
 
}