~ubuntu-branches/debian/squeeze/gnome-do-plugins/squeeze

« back to all changes in this revision

Viewing changes to OpenSearch/src/OpenSearchAction.cs

  • Committer: Bazaar Package Importer
  • Author(s): Colin Turner
  • Date: 2008-05-11 15:08:58 UTC
  • Revision ID: james.westby@ubuntu.com-20080511150858-upa7kbyw601luqyb
Tags: upstream-0.4.0
ImportĀ upstreamĀ versionĀ 0.4.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//  OpenSearchItemAction.cs
 
2
//
 
3
//  GNOME Do is the legal property of its developers, whose names are too numerous
 
4
//  to list here.  Please refer to the 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
using System;
 
21
using System.Collections.Generic;
 
22
using System.Web;
 
23
 
 
24
using Mono.Unix;
 
25
 
 
26
using Do.Addins;
 
27
using Do.Universe;
 
28
 
 
29
namespace Do.Plugins.OpenSearch
 
30
{
 
31
        public class OpenSearchAction : AbstractAction
 
32
        {               
 
33
                static OpenSearchAction()
 
34
                {
 
35
                        OpenSearch.GetOpenSearchItems ();
 
36
                }
 
37
                
 
38
                public override string Name
 
39
                {
 
40
                        get { return Catalog.GetString ("Search Web"); }
 
41
                }
 
42
                
 
43
                public override string Description
 
44
                {
 
45
                        get { return Catalog.GetString ("Searches the web using OpenSearch plugins."); }
 
46
                }
 
47
                
 
48
                public override string Icon
 
49
                {
 
50
                        get { return "web-browser"; }
 
51
                }
 
52
                
 
53
                public override Type[] SupportedItemTypes
 
54
                {
 
55
                        get {
 
56
                                return new Type[] {
 
57
                                        typeof (ITextItem),
 
58
                                };
 
59
                        }
 
60
                }
 
61
 
 
62
                public override bool SupportsItem (IItem item)
 
63
                {
 
64
                        if (item is ITextItem) 
 
65
                                return true;
 
66
                        return false;
 
67
                }
 
68
                
 
69
                public override Type[] SupportedModifierItemTypes
 
70
                {
 
71
                        get {
 
72
                                return new Type[] {
 
73
                                        typeof (OpenSearchItem),
 
74
                                };
 
75
                        }
 
76
                }
 
77
                                
 
78
                public override IItem[] DynamicModifierItemsForItem (IItem item)
 
79
                {
 
80
                        try
 
81
                        {
 
82
                                List<IItem> items = new List<IItem>();
 
83
                                foreach(IItem openSearchItem in OpenSearch.GetOpenSearchItems ()) {
 
84
                                        items.Add (openSearchItem);
 
85
                                }
 
86
                                return items.ToArray ();
 
87
                        }
 
88
                        catch
 
89
                        {
 
90
                                return null;
 
91
                        }
 
92
                }
 
93
        
 
94
                public override IItem[] Perform (IItem[] items, IItem[] modifierItems)
 
95
                {
 
96
                        List<string> searchTerms;
 
97
                                
 
98
                        if (modifierItems.Length > 0)
 
99
                        {
 
100
                                searchTerms = new List<string> ();
 
101
                                foreach (IItem item in items) {
 
102
                                        searchTerms.Add ((item as ITextItem).Text);
 
103
                                }
 
104
                                
 
105
                                foreach (string searchTerm in searchTerms) {
 
106
                                        string url = BuildSearchUrl ((modifierItems[0] as IOpenSearchItem), searchTerm);
 
107
                                        Util.Environment.Open (url);    
 
108
                                }
 
109
                        }
 
110
                        return null; 
 
111
                }
 
112
                
 
113
                private static string BuildSearchUrl (IOpenSearchItem openSearchItem, string searchTerm)
 
114
                {
 
115
                        return openSearchItem.UrlTemplate.Replace ("{searchTerms}",  HttpUtility.UrlEncode(searchTerm));
 
116
                }
 
117
        }
 
118
}