~cszikszoy/do-plugins/pastebin

« back to all changes in this revision

Viewing changes to LocateFiles/LocateFilesCommand.cs

  • Committer: djsiegel at gmail
  • Date: 2008-01-04 01:56:12 UTC
  • Revision ID: djsiegel@gmail.com-20080104015612-85vxa94a445uvkw5
Added locate files command.
Updated templates.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* LocateFilesCommand.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
namespace Do.Universe
 
26
{
 
27
        public class LocateFilesCommand : AbstractCommand
 
28
        {
 
29
                
 
30
                bool allowHidden = false;
 
31
                uint maxResults = 1000;
 
32
                
 
33
                public override string Name
 
34
                {
 
35
                        get { return "Locate Files"; }
 
36
                }
 
37
                
 
38
                public override string Description
 
39
                {
 
40
                        get { return "Search your filesystem using locate."; }
 
41
                }
 
42
                
 
43
                public override string Icon
 
44
                {
 
45
                        get { return "search"; }
 
46
                }
 
47
                
 
48
                public override Type[] SupportedItemTypes
 
49
                {
 
50
                        get {
 
51
                                return new Type[] {
 
52
                                        typeof (ITextItem)
 
53
                                };
 
54
                        }
 
55
                }
 
56
                
 
57
                public override IItem[] Perform (IItem[] items, IItem[] modifierItems)
 
58
                {
 
59
                        List<IItem> files;
 
60
                        System.Diagnostics.Process locate;
 
61
                        string query;
 
62
                                
 
63
                        files = new List<IItem> ();
 
64
                        query = (items[0] as ITextItem).Text;
 
65
                        
 
66
                        locate = new System.Diagnostics.Process ();
 
67
                        locate.StartInfo.FileName = "locate";
 
68
                        locate.StartInfo.Arguments = string.Format ("-i -n {0} {1}", maxResults, query);
 
69
                        locate.StartInfo.RedirectStandardOutput = true;
 
70
                        locate.StartInfo.UseShellExecute = false;
 
71
                        try {
 
72
                                locate.Start ();
 
73
                        } catch {
 
74
                                Console.Error.WriteLine ("LocateFilesCommand error: The program 'locate' could not be found.");
 
75
                                return null;
 
76
                        }
 
77
 
 
78
                        string path;
 
79
                        query = query.ToLower ();
 
80
                        while (null != (path = locate.StandardOutput.ReadLine ())) {
 
81
                                // Disallow hidden directories in the absolute path.
 
82
                                // This gets rid of messy .svn directories and their contents.
 
83
                                if (!allowHidden &&
 
84
                                                Path.GetDirectoryName (path).Contains ("/."))
 
85
                                        continue;
 
86
 
 
87
                                // Only allow files that contain the query as a substring
 
88
                                // of the file name. It may be faster to use grep, but I've
 
89
                                // tested this and it seems prety snappy.
 
90
                                if (Path.GetFileName (path).ToLower().Contains (query))
 
91
                                        files.Add (FileItem.Create (path));
 
92
                        }
 
93
                        files.Sort (new FileItemNameComparer (query));
 
94
                        return files.ToArray ();
 
95
                }
 
96
 
 
97
                // Order files by (A) position of query in the file name and
 
98
                // (B) by name length.
 
99
                private class FileItemNameComparer : IComparer<IItem>
 
100
                {
 
101
                        string query;
 
102
 
 
103
                        public FileItemNameComparer (string query)
 
104
                        {
 
105
                                this.query = query.ToLower ();
 
106
                        }
 
107
 
 
108
                        public int Compare (IItem a, IItem b)
 
109
                        {
 
110
                                string a_name_lower, b_name_lower;
 
111
                                int a_score, b_score;
 
112
 
 
113
                                a_name_lower = (a as FileItem).Path;
 
114
                                a_name_lower = Path.GetFileName (a_name_lower).ToLower (); 
 
115
                                b_name_lower = (b as FileItem).Path;
 
116
                                b_name_lower = Path.GetFileName (b_name_lower).ToLower (); 
 
117
 
 
118
                                a_score = a_name_lower.IndexOf (query);
 
119
                                b_score = b_name_lower.IndexOf (query);
 
120
 
 
121
                                if (a_score == b_score)
 
122
                                        return a_name_lower.Length - b_name_lower.Length;
 
123
                                else
 
124
                                        return a_score - b_score;
 
125
                        }
 
126
                }
 
127
        }
 
128
}