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

« back to all changes in this revision

Viewing changes to Do.Addins/src/Do.Universe/DefineWordAction.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
 
/* DefineWordAction.cs
2
 
 *
3
 
 * GNOME Do is the legal property of its developers. Please refer to the
4
 
 * COPYRIGHT file distributed with this source distribution.
5
 
 *
6
 
 * This program is free software: you can redistribute it and/or modify
7
 
 * it under the terms of the GNU General Public License as published by
8
 
 * the Free Software Foundation, either version 3 of the License, or
9
 
 * (at your option) any later version.
10
 
 *
11
 
 * This program is distributed in the hope that it will be useful,
12
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 
 * GNU General Public License for more details.
15
 
 *
16
 
 * You should have received a copy of the GNU General Public License
17
 
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
 
 */
19
 
 
20
 
using System;
21
 
using System.Text.RegularExpressions;
22
 
using Mono.Unix;
23
 
 
24
 
namespace Do.Universe
25
 
{
26
 
        /// <summary>
27
 
        /// Given an ITextItem, DefineWordAction will look up the Text
28
 
        /// contents of the ITextItem using the gnome-dictionary.
29
 
        /// </summary>
30
 
        public class DefineWordAction : AbstractAction
31
 
        {
32
 
                /// <summary>
33
 
                /// Should match those and only those strings that can be
34
 
                /// looked up in a dictionary.
35
 
                /// YES: "war", "peace", "hoi polloi"
36
 
                /// NO: "war9", "2 + 4", "___1337__"
37
 
                /// </summary>
38
 
                const string wordPattern = @"^([^\W0-9_]+([ -][^\W0-9_]+)?)$";
39
 
 
40
 
                Regex wordRegex;
41
 
                
42
 
                public DefineWordAction ()
43
 
                {
44
 
                        wordRegex = new Regex (wordPattern, RegexOptions.Compiled);
45
 
                }
46
 
                
47
 
                public override string Name {
48
 
                        get { return Catalog.GetString ("Define"); }
49
 
                }
50
 
                
51
 
                public override string Description
52
 
                {
53
 
                        get { return Catalog.GetString ("Define a given word."); }
54
 
                }
55
 
                
56
 
                public override string Icon
57
 
                {
58
 
                        get { return "accessories-dictionary"; }
59
 
                }
60
 
                
61
 
                public override Type[] SupportedItemTypes
62
 
                {
63
 
                        get {
64
 
                                return new Type[] {
65
 
                                        typeof (ITextItem),
66
 
                                };
67
 
                        }
68
 
                }
69
 
 
70
 
                /// <summary>
71
 
                /// Use wordRegex to determine whether item is definable.
72
 
                /// </summary>
73
 
                /// <param name="item">
74
 
                /// A <see cref="IItem"/> to define.
75
 
                /// </param>
76
 
                /// <returns>
77
 
                /// A <see cref="System.Boolean"/> indicating whether or not IITem
78
 
                /// can be defined.
79
 
                /// </returns>
80
 
                public override bool SupportsItem (IItem item)
81
 
                {
82
 
                        string word;
83
 
 
84
 
                        word = null;
85
 
                        if (item is ITextItem) {
86
 
                                word = (item as ITextItem).Text;
87
 
                        }
88
 
                        return !string.IsNullOrEmpty (word) && wordRegex.IsMatch (word);
89
 
                }
90
 
                
91
 
                public override IItem[] Perform (IItem[] items, IItem[] modifierItems)
92
 
                {
93
 
                        string word, cmd;
94
 
                        foreach (IItem item in items) {
95
 
                                if (item is ITextItem) {
96
 
                                        word = (item as ITextItem).Text;
97
 
                                } else {
98
 
                                        continue;
99
 
                                }
100
 
                                cmd = string.Format ("gnome-dictionary --look-up \"{0}\"", word);
101
 
                                System.Diagnostics.Process.Start (cmd);
102
 
                        }
103
 
                        return null;
104
 
                }
105
 
        }
106
 
}