~ubuntu-branches/ubuntu/lucid/tomboy/lucid-proposed

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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Xml;

using Tomboy;

public class InsertBugAction : SplitterAction
{
	BugzillaLink Tag;
	int Offset;
	string Id;

	public InsertBugAction (Gtk.TextIter start,
	                        string id,
	                        Gtk.TextBuffer buffer,
				BugzillaLink tag)
	{
		Tag = tag;
		Id = id;

		Offset = start.Offset;
	}

	public override void Undo (Gtk.TextBuffer buffer)
	{
		// Tag images change the offset by one, but only when deleting.
		Gtk.TextIter start_iter = buffer.GetIterAtOffset (Offset);
		Gtk.TextIter end_iter = buffer.GetIterAtOffset (Offset + chop.Length + 1);
		buffer.Delete (ref start_iter, ref end_iter);
		buffer.MoveMark (buffer.InsertMark, buffer.GetIterAtOffset (Offset));
		buffer.MoveMark (buffer.SelectionBound, buffer.GetIterAtOffset (Offset));

		Tag.ImageLocation = null;

		ApplySplitTags (buffer);
	}

	public override void Redo (Gtk.TextBuffer buffer)
	{
		RemoveSplitTags (buffer);

		Gtk.TextIter cursor = buffer.GetIterAtOffset (Offset);

		Gtk.TextTag[] tags = {Tag};
		buffer.InsertWithTags (ref cursor, Id, tags);

		buffer.MoveMark (buffer.SelectionBound, buffer.GetIterAtOffset (Offset));
		buffer.MoveMark (buffer.InsertMark,
		                 buffer.GetIterAtOffset (Offset + chop.Length));

	}

	public override void Merge (EditAction action)
	{
		SplitterAction splitter = action as SplitterAction;
		this.splitTags = splitter.SplitTags;
		this.chop = splitter.Chop;
	}

	/*
	 * The internal listeners will create an InsertAction when the text
	 * is inserted.  Since it's ugly to have the bug insertion appear
	 * to the user as two items in the undo stack, have this item eat
	 * the other one.
	 */
	public override bool CanMerge (EditAction action)
	{
		InsertAction insert = action as InsertAction;
		if (insert == null) {
			return false;
		}

		if (String.Compare(Id, insert.Chop.Text) == 0) {
			return true;
		}

		return false;
	}

	public override void Destroy ()
	{
	}
}

public class BugzillaLink : DynamicNoteTag
{
	Gdk.Pixbuf Icon;

	public BugzillaLink ()
		: base ()
	{
	}

	public override void Initialize (string element_name)
	{
		base.Initialize (element_name);

		Underline = Pango.Underline.Single;
		Foreground = "blue";
		CanActivate = true;
		CanGrow = true;
		CanSpellCheck = false;
		CanSplit = false;
	}

	public string BugUrl
	{
		get { return (string) Attributes ["uri"]; }
		set { Attributes ["uri"] = value; }
	}

	protected override bool OnActivate (NoteEditor editor, Gtk.TextIter start, Gtk.TextIter end)
	{
		if (BugUrl != string.Empty) {
			Logger.Log ("Opening url '{0}'...", BugUrl);
			Gnome.Url.Show (BugUrl);
		}
		return true;
	}

	public override void Read (XmlTextReader xml, bool start)
	{
		base.Read (xml, start);
	}

	public override Gdk.Pixbuf Image
	{
		get
		{
			if (Icon != null)
				return Icon;

			System.Uri uri = new System.Uri(BugUrl);
			if (uri == null)
				return null;

			string host = uri.Host;
			string imageDir = "~/.tomboy/BugzillaIcons/";

			string imagePath = imageDir.Replace ("~", Environment.GetEnvironmentVariable ("HOME")) + host + ".png";

			try {
				Icon = new Gdk.Pixbuf (imagePath);
			} catch (GLib.GException) {
				Icon = new Gdk.Pixbuf(null, "stock_bug.png");
			}

			return Icon;
		}
	}
}

public class BugzillaPlugin : NotePlugin
{
	static int last_bug;

	static BugzillaPlugin ()
	{
		last_bug = -1;
	}

	protected override void Initialize ()
	{
		if (!Note.TagTable.IsDynamicTagRegistered ("link:bugzilla")) {
			Note.TagTable.RegisterDynamicTag ("link:bugzilla", typeof (BugzillaLink));
		}
	}

	protected override void Shutdown ()
	{
	}

	protected override void OnNoteOpened ()
	{
		Window.Editor.DragDataReceived += OnDragDataReceived;
	}

	[DllImport("libgobject-2.0.so.0")]
	static extern void g_signal_stop_emission_by_name (IntPtr raw, string name);

	[GLib.ConnectBefore]
	void OnDragDataReceived (object sender, Gtk.DragDataReceivedArgs args)
	{
		foreach (Gdk.Atom atom in args.Context.Targets) {
			if (atom.Name == "text/uri-list" ||
			    atom.Name == "_NETSCAPE_URL") {
				DropUriList (args);
				return;
			}
		}
	}

	void DropUriList (Gtk.DragDataReceivedArgs args)
	{
		string uriString = Encoding.UTF8.GetString (args.SelectionData.Data);

		if (uriString.IndexOf ("show_bug.cgi?id=") != -1) {
			if (InsertBug (uriString)) {
				Gtk.Drag.Finish (args.Context, true, false, args.Time);
				g_signal_stop_emission_by_name(Window.Editor.Handle,
							       "drag_data_received");
			}
		}
	}

	bool InsertBug(string uri)
	{
		try {
			string bug = uri.Substring (uri.IndexOf ("show_bug.cgi?id=") + 16);
			int id = int.Parse (bug);
			// Debounce.  I'm not sure why this is necessary :(
			if (id == last_bug) {
				last_bug = -1;
				return true;
			}
			last_bug = id;

			BugzillaLink link_tag = (BugzillaLink)
				Note.TagTable.CreateDynamicTag ("link:bugzilla");
			link_tag.BugUrl = uri;

			Gtk.TextIter cursor = Buffer.GetIterAtMark (Buffer.InsertMark);

			Buffer.Undoer.AddUndoAction (new InsertBugAction (cursor, bug, Buffer, link_tag));

			Gtk.TextTag[] tags = {link_tag};
			Buffer.InsertWithTags (ref cursor, bug, tags);
			return true;
		} catch {
			return false;
		}
	}
}