~do-plugins/do-plugins/future

« back to all changes in this revision

Viewing changes to File/src/Do.FilesAndFolders/FileItemSource.cs

  • Committer: David Siegel
  • Date: 2008-12-23 22:55:20 UTC
  • Revision ID: david@david-desktop-20081223225520-fqdedh21xtm635wl
Update File plugin (had to replace files, merge was very difficult).

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.Linq;
 
24
using System.Collections.Generic;
 
25
 
 
26
using Mono.Unix;
 
27
 
 
28
using Do.Universe;
 
29
using Do.Platform;
 
30
 
 
31
namespace Do.FilesAndFolders {
 
32
 
 
33
        /// <summary>
 
34
        /// Indexes files recursively starting in a specific directory.
 
35
        /// </summary>
 
36
        public class FileItemSource : ItemSource, IConfigurable {
 
37
 
 
38
                public IEnumerable<IItem> Items { get; protected set; }
 
39
 
 
40
                public FileItemSource ()
 
41
                {
 
42
                        Items = Enumerable.Empty<IItem> ();
 
43
                }
 
44
                
 
45
                public Gtk.Bin GetConfiguration ()
 
46
                {
 
47
                        return new Configuration ();
 
48
                }
 
49
 
 
50
                public override IEnumerable<Type> SupportedItemTypes {
 
51
                        get {
 
52
                                yield return typeof (IFileItem);
 
53
                                yield return typeof (ITextItem);
 
54
                                yield return typeof (IApplicationItem);
 
55
                        }
 
56
                }
 
57
                
 
58
                public override string Name {
 
59
                        get { return Catalog.GetString ("File Indexer"); }
 
60
                }
 
61
                
 
62
                public override string Description {
 
63
                        get {
 
64
                                return Catalog.GetString ("Frequently used files and folders.");
 
65
                        }
 
66
                }
 
67
                
 
68
                public override string Icon {
 
69
                        get { return "folder"; }
 
70
                }
 
71
                
 
72
                public override IEnumerable<IItem> ChildrenOfItem (IItem item)
 
73
                {
 
74
                        IFileItem file = null;
 
75
 
 
76
                        if (item is ITextItem)
 
77
                                file = Services.UniverseFactory.NewFileItem ((item as ITextItem).Text);
 
78
                        else if (item is IFileItem)
 
79
                                file = item as IFileItem;
 
80
                        else if (item is IApplicationItem)
 
81
                                return Enumerable.Empty<IItem> ();
 
82
                        
 
83
                        return RecursiveGetFileItems (file.Path, 0).Cast<IItem> ();
 
84
                }
 
85
                
 
86
                public void UpdateItems ()
 
87
                {
 
88
                        Items = Plugin.FolderIndex
 
89
                                .SelectMany (folder => RecursiveGetFileItems (folder.Path, folder.Level))
 
90
                                .Take (Plugin.Preferences.MaximumFilesIndexed)
 
91
                                .ToArray ();
 
92
                }
 
93
 
 
94
                static IEnumerable<IFileItem> RecursiveGetFileItems (string path, int levels)
 
95
                {
 
96
                        return RecursiveListFiles (path, levels)
 
97
                                .Select (filepath => Services.UniverseFactory.NewFileItem (filepath));
 
98
                }
 
99
                
 
100
                static IEnumerable<string> RecursiveListFiles (string path, int levels)
 
101
                {
 
102
                        IEnumerable<string> results = null;
 
103
 
 
104
                        if (path == null) throw new ArgumentNullException ("path");
 
105
                        
 
106
                        if (levels < 0 || !Directory.Exists (path))
 
107
                                return Enumerable.Empty<string> ();
 
108
                        
 
109
                        try {
 
110
                                IEnumerable<string> files, directories, recursiveFiles;
 
111
                                
 
112
                                files = Directory.GetFiles (path).Where (ShouldIndexFile);
 
113
                                directories = Directory.GetDirectories (path).Where (ShouldIndexFile);
 
114
                                recursiveFiles = directories.SelectMany (dir => RecursiveListFiles (dir, levels - 1));
 
115
                                results = files.Concat (directories).Concat (recursiveFiles);
 
116
                        } catch (Exception e) {
 
117
                                Log.Error ("Encountered an error while attempting to index {0}: {1}", path, e.Message);
 
118
                                Log.Debug (e.StackTrace);
 
119
                                results = Enumerable.Empty<string> ();
 
120
                        }
 
121
                        return results;
 
122
                }
 
123
 
 
124
                static bool ShouldIndexFile (string path)
 
125
                {
 
126
                        string filename = Path.GetFileName (path);
 
127
                        return filename != "." && filename != ".." &&
 
128
                                (Plugin.Preferences.IncludeHiddenFiles || !filename.StartsWith ("."));
 
129
                }
 
130
                
 
131
        }
 
132
}