~cszikszoy/do-plugins/fix-confluence

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// FileItemSource.cs
//
// GNOME Do is the legal property of its developers. Please refer to the
// COPYRIGHT file distributed with this source distribution.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//

using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;

using Mono.Unix;

using Do.Universe;
using Do.Platform;
using Do.Platform.Linux;

namespace Do.FilesAndFolders {

	/// <summary>
	/// Indexes files recursively starting in a specific directory.
	/// </summary>
	public class FileItemSource : ItemSource, IConfigurable {

		IEnumerable<Item> items;
		bool maximum_files_warned;

		string MaximumFilesIndexedWarning {
			get {
				return string.Format (
					"{0} has indexed over {1} files, which is the maximum number of files it will index. "+
				    "This usually means that you are indexing more files that are useful to you. " +
				    "Do you really require instant access to all of these files? " +
				    "Consider indexing fewer files; a good rule of thumb is to only index the files you open at least once a month, " +
				    "and browse for files that you open less often."

				    , Name, Plugin.Preferences.MaximumFilesIndexed
				);
			}
		}

		public FileItemSource ()
		{
			items = Enumerable.Empty<Item> ();
		}

		public override IEnumerable<Item> Items {
			get { return items; }
		}
		
		public Gtk.Bin GetConfiguration ()
		{
			return new Configuration ();
		}

		public override IEnumerable<Type> SupportedItemTypes {
			get {
				yield return typeof (IFileItem);
				yield return typeof (ITextItem);
				yield return typeof (IApplicationItem);
				yield return typeof (IUriItem);
			}
		}
		
		public override string Name {
			get { return Catalog.GetString ("Files and Folders"); }
		}
		
		public override string Description {
			get {
				return Catalog.GetString ("Catalogs important files and folders for quick access.");
			}
		}
		
		public override string Icon {
			get { return "folder"; }
		}
		
		public override IEnumerable<Item> ChildrenOfItem (Item item)
		{
			IFileItem file = null;

			if (item is ITextItem)
				file = Plugin.NewFileItem ((item as ITextItem).Text);
			else if (item is IFileItem)
				file = item as IFileItem;
			else if (item is IUriItem && (item as IUriItem).Uri.StartsWith ("file://"))
				file = Plugin.NewFileItem ((item as IUriItem).Uri.Replace ("file://", ""));
			else if (item is IApplicationItem)
				return Enumerable.Empty<Item> ();
			
			return RecursiveGetItems (file.Path, 1, Plugin.Preferences.IncludeHiddenFilesWhenBrowsing);
		}
		
		public override void UpdateItems ()
		{
			try {
				IEnumerable<IndexedFolder> ignored = Enumerable.Empty<IndexedFolder> ();
				
				ignored = Plugin.FolderIndex
					.Where (folder => folder.Status == FolderStatus.Ignored);
				
				if (Plugin.Preferences.LimitMaxFilesIndexed)
					items = Plugin.FolderIndex
						.SelectMany (folder => RecursiveGetItems (folder.Path, folder.Level, Plugin.Preferences.IncludeHiddenFiles, ignored))
						.Take (Plugin.Preferences.MaximumFilesIndexed)
						.ToArray ();
				else
					items = Plugin.FolderIndex
						.SelectMany (folder => RecursiveGetItems (folder.Path, folder.Level, Plugin.Preferences.IncludeHiddenFiles, ignored))
						.ToArray ();
								
				if (!maximum_files_warned && items.Count () == Plugin.Preferences.MaximumFilesIndexed) {
					Log.Warn (MaximumFilesIndexedWarning);
					Services.Notifications.Notify ("Do is indexing too many files.", MaximumFilesIndexedWarning);
					maximum_files_warned = true;
				}
			}
			catch (Exception e) {
				Console.WriteLine(e.ToString());
			}
		}

		/// <summary>
		/// Recursively scan files in path to the given level, creating IFileItems
		/// and IApplicationItems from the files found.
		/// </summary>
		/// <remarks>
		/// This should remain lazy.
		/// </remarks>
		/// <param name="path">
		/// A <see cref="System.String"/>
		/// </param>
		/// <param name="levels">
		/// A <see cref="System.UInt32"/>
		/// </param>
		/// <param name="includeHidden">
		/// A <see cref="System.Boolean"/>
		/// </param>
		/// <returns>
		/// A <see cref="IEnumerable"/>
		/// </returns>
		
		static IEnumerable<Item> RecursiveGetItems (string path, uint levels, bool includeHidden)
		{
			return RecursiveGetItems(path, levels, includeHidden, Enumerable.Empty<IndexedFolder> ());
		}
		
		static IEnumerable<Item> RecursiveGetItems (string path, uint levels, bool includeHidden, IEnumerable<IndexedFolder> ignored)
		{
			IEnumerable<string> files;
			IEnumerable<Item> fileItems, applicationItems;
			
			files = RecursiveListFiles (path, levels, includeHidden, ignored);

			fileItems = files
				.Select (filepath => Plugin.NewFileItem (filepath))
				.OfType<Item> ();

			applicationItems = files
				.Where (filepath => filepath.EndsWith (".desktop"))
				.Select (filepath => Plugin.NewApplicationItem (filepath))
				.OfType<Item> ();

			return applicationItems.Concat (fileItems);
		}
		
		static IEnumerable<string> RecursiveListFiles (string path, uint levels, bool includeHidden, IEnumerable<IndexedFolder> ignored)
		{
			IEnumerable<string> results = null;

			if (path == null) throw new ArgumentNullException ("path");
			
			if (levels == 0 || !Directory.Exists (path))
				return Enumerable.Empty<string> ();
			
			try {
				IEnumerable<string> files, directories, recursiveFiles;
				
				files = Directory.GetFiles (path)
					.Where (filepath => ShouldIndexPath (filepath, includeHidden, ignored));
				directories = Directory.GetDirectories (path)
					.Where (filepath => ShouldIndexPath (filepath, includeHidden, ignored));
				recursiveFiles = directories
					.SelectMany (dir => RecursiveListFiles (dir, levels - 1, includeHidden, ignored));
				results = files.Concat (directories).Concat (recursiveFiles);
				
			} catch (Exception e) {
				Log.Error ("Encountered an error while attempting to index {0}: {1}", path, e.Message);
				Log.Debug (e.StackTrace);
				results = Enumerable.Empty<string> ();
			}
			return results;
		}

		static bool ShouldIndexPath (string path, bool includeHidden, IEnumerable<IndexedFolder> ignored)
		{
			string filename = Path.GetFileName (path);
			bool isForbidden = filename == "." || filename == ".." || filename.EndsWith ("~");
			bool isHidden = filename.StartsWith (".");
			if (ignored.Where (folder => path == folder.Path).Any ()) 
				isForbidden = true;
			return !isForbidden && (includeHidden || !isHidden);
		}
		
	}
}