3
* GNOME Do is the legal property of its developers. Please refer to the
4
* COPYRIGHT file distributed with this
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.
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.
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/>.
24
using System.Collections.Generic;
31
namespace Do.FilesAndFolders {
34
/// Indexes files recursively starting in a specific directory.
36
public class FileItemSource : ItemSource, IConfigurable {
38
public IEnumerable<IItem> Items { get; protected set; }
40
public FileItemSource ()
42
Items = Enumerable.Empty<IItem> ();
45
public Gtk.Bin GetConfiguration ()
47
return new Configuration ();
50
public override IEnumerable<Type> SupportedItemTypes {
52
yield return typeof (IFileItem);
53
yield return typeof (ITextItem);
54
yield return typeof (IApplicationItem);
58
public override string Name {
59
get { return Catalog.GetString ("File Indexer"); }
62
public override string Description {
64
return Catalog.GetString ("Frequently used files and folders.");
68
public override string Icon {
69
get { return "folder"; }
72
public override IEnumerable<IItem> ChildrenOfItem (IItem item)
74
IFileItem file = null;
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> ();
83
return RecursiveGetFileItems (file.Path, 0).Cast<IItem> ();
86
public void UpdateItems ()
88
Items = Plugin.FolderIndex
89
.SelectMany (folder => RecursiveGetFileItems (folder.Path, folder.Level))
90
.Take (Plugin.Preferences.MaximumFilesIndexed)
94
static IEnumerable<IFileItem> RecursiveGetFileItems (string path, int levels)
96
return RecursiveListFiles (path, levels)
97
.Select (filepath => Services.UniverseFactory.NewFileItem (filepath));
100
static IEnumerable<string> RecursiveListFiles (string path, int levels)
102
IEnumerable<string> results = null;
104
if (path == null) throw new ArgumentNullException ("path");
106
if (levels < 0 || !Directory.Exists (path))
107
return Enumerable.Empty<string> ();
110
IEnumerable<string> files, directories, recursiveFiles;
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> ();
124
static bool ShouldIndexFile (string path)
126
string filename = Path.GetFileName (path);
127
return filename != "." && filename != ".." &&
128
(Plugin.Preferences.IncludeHiddenFiles || !filename.StartsWith ("."));