~cszikszoy/do-plugins/pastebin

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
/* ManLookUpAction.cs is part of ManLookUp, a Gnome-Do plugin
 *
 * Copyright 2008 J. Carlos Navea
 * loconet@gmail.com
 *
 * Man Look Up 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/>.
 *
 * Contributors:
 * J. Carlos Navea <loconet@gmail.com>
 *
 * Based on the excellent work of sample plugins included with Gnome-Do 
 */
using System;
using Do.Universe;
using System.Diagnostics;
using System.Text.RegularExpressions;

using GConf;
using Mono.Unix;


namespace GnomeDoManLookUp {

	/// <summary>
	/// 	ManLookUpAction - the main action for our plugin. 
	///		Allows us to hook up to Gnome-Do as a command
	///		that can be applied to raw text or our own man page items. 
	/// </summary>
	public class ManLookUpAction : AbstractAction {
		
		/// <value>
		/// 	The name of the action
		/// </value>
		public override string Name {
			get { return Catalog.GetString ("Read help documentation (man)"); }
		}
		
		/// <value>
		/// 	Action's description
		/// </value>
		public override string Description {
			get { return Catalog.GetString ("Look up and read a manual page (man)"); }
		}
		
		/// <value>
		/// 	The pretty icon
		/// </value>	
		public override string Icon { 
			get { return "applications-office"; }
		}
		
		/// <value>
		/// 	List of supported items (ITextItem , ManLookUpItem)
		/// </value>
		public override Type[] SupportedItemTypes {
			get {
				return new Type[] {
					typeof(ITextItem),typeof(ManLookUpItem),typeof(ApplicationItem)
				};
			}
           
		}
		
		/// <summary>
		/// 	Do we support the item?
		/// </summary>
		/// <param name="item">
		/// A <see cref="IItem"/>
		/// </param>
		/// <returns>
		/// A <see cref="System.Boolean"/>
		/// </returns>
		public override bool SupportsItem (IItem item) 
		{
			
			bool rc  = false;
			string execStr;

			if (!(item is ApplicationItem)) {			
				rc = true;				
			} else {
				
				ApplicationItem appItem = item as ApplicationItem;

				//grab the parameter we'll be using for whatis command from the Exec string
				//note, we use whatis becuase redirecting its output is a lot more efficient
				//than that of a regular 'man page' which can contain thousands of lines. 
				//man and whatis should use the same db anyways..
				execStr = this.getExecutableName (appItem);
				
				//is there a matching man page?
				if (execStr.Length > 0) {
					Process term 				= new Process ();
					term.StartInfo.FileName 		= "whatis";
					term.StartInfo.Arguments 		= "'"+execStr+"'";
					term.StartInfo.UseShellExecute 		= false;
					term.StartInfo.RedirectStandardOutput 	= true;
					term.StartInfo.RedirectStandardError 	= true;
					term.Start ();
					
					//if we don't put these, WaitForExit () blocks
					//on big buffers or ExitCode is not avalable yet..
					term.StandardOutput.ReadToEnd ();
					term.WaitForExit ();
					
					//if we didn't find a man page, we don't support it. 
					rc = term.ExitCode == 0;
				}
				
				
			}
			
			return rc;
			
		}
		
		/// <summary>
		///    Parse the <see cref="ApplicationItem"/> Exec string to attempt to obtain
		///    name of the binary being executed so we may use it as a parameter for man
		/// </summary>
		/// <param name="appItem">
		/// A <see cref="ApplicationItem"/>
		/// </param>
		/// <returns>
		/// A <see cref="System.String"/>
		/// </returns>
		private static string getExecutableName (ApplicationItem appItem) 
		{			
			string execStr;
			Match m;
			Regex r;
			int i;		
			
			// Now we parse the execute string to attempt to find a binary 
			// name that we can look up a man page for. Ideally this should
			// be given to us rather than guess the format but this
			// will have to do for now.
			// 1. if being invoked with gksu, ignore gksu itself and grab its parameter
			// 2. remove any arguments sent to the call
			// 3. reduce absolute paths to just the filename. 
			//
			r = new Regex ("^(gksu\\s+)?([^ ]+)\\s?.*$");
			m = r.Match (appItem.Exec);			
			execStr = !m.Success ? appItem.Exec : execStr = m.Groups [2].ToString ();
			
			//grab base
			i = execStr.LastIndexOf ('/');
			if (i != -1)
				execStr = execStr.Substring (i+1);
			
			return execStr;
		}
		
		/// <summary>
		/// 	Called by Gnome-Do in order to perform our action.
		/// </summary>
		/// <param name="items">
		/// List of <see cref="IItem"/> objects, either raw text or custom look up items
		/// </param>
		/// <param name="modItems">
		/// List of <see cref="IItem"/> objects, action modifiers
		/// </param>
		/// <returns>
		/// List of <see cref="IItem"/>
		/// </returns>
		public override IItem[] Perform (IItem[] items, IItem[] modItems) 
		{
			
            		string keyword = null;

			//ok, was it plain text, an application item, or one of our own?
			if (items [0] is ApplicationItem) {
				keyword = this.getExecutableName (items [0] as ApplicationItem);
			} else if (items [0] is ManLookUpItem) {
				ManLookUpItem keyworditem = items [0] as ManLookUpItem;
				keyword = keyworditem.Text;							
			} else {
				ITextItem textitem = items [0] as ITextItem;
				keyword = textitem.Text;				 
			}
			 
			
			if (keyword != null && keyword.Length > 0) {
				Process term = new Process ();
				term.StartInfo.FileName = "yelp";
				term.StartInfo.Arguments = " 'man:"+keyword+"' ";				
				term.Start ();
			}
			
			return null;
		}
	}
}