Use trac icon for trac bugs to diff from bugzilla
[tomboy-trac.git] / src / TracNoteAddin.cs
blobe72b853b778c356512f3f7f7eca5b82f204a8b6b
2 using System;
3 using System.Runtime.InteropServices;
4 using System.Text;
5 using System.Text.RegularExpressions;
6 using System.Xml;
8 //using Mono.Unix;
10 using Tomboy;
12 namespace Tomboy.Trac
14 public class TracNoteAddin : NoteAddin
16 static int last_bug;
18 public const string TracLinkTagName = "link:trac";
20 static TracNoteAddin ()
22 last_bug = -1;
25 public override void Initialize ()
27 if (!Note.TagTable.IsDynamicTagRegistered (TracLinkTagName)) {
28 Note.TagTable.RegisterDynamicTag (TracLinkTagName, typeof (TracLink));
32 public override void Shutdown ()
36 public override void OnNoteOpened ()
38 Window.Editor.DragDataReceived += OnDragDataReceived;
41 [DllImport("libgobject-2.0.so.0")]
42 static extern void g_signal_stop_emission_by_name (IntPtr raw, string name);
44 [GLib.ConnectBefore]
45 void OnDragDataReceived (object sender, Gtk.DragDataReceivedArgs args)
47 Logger.Debug ("Trac.OnDragDataReceived");
48 foreach (Gdk.Atom atom in args.Context.Targets) {
49 if (atom.Name == "text/uri-list" ||
50 atom.Name == "_NETSCAPE_URL") {
51 DropUriList (args);
52 return;
57 void DropUriList (Gtk.DragDataReceivedArgs args)
59 if (args.SelectionData.Length > 0) {
60 string uriString = Encoding.UTF8.GetString (args.SelectionData.Data);
62 string bugIdGroup = "bugid";
63 string baseUrlGroup = "baseurl";
64 // Allow for trac link to contain '#' after it in case of 'preview' item
65 string regexString =
66 @"^(?<" + baseUrlGroup + @">.*/ticket/(?<" + bugIdGroup + @">\d+))(?:#.*)?$";
68 Match match = Regex.Match (uriString, regexString);
69 if (match.Success) {
70 int bugId = int.Parse (match.Groups [bugIdGroup].Value);
71 string baseurl = match.Groups [baseUrlGroup].Value;
72 if (InsertBug (args.X, args.Y, baseurl, bugId)) {
73 Gtk.Drag.Finish (args.Context, true, false, args.Time);
74 g_signal_stop_emission_by_name(Window.Editor.Handle,
75 "drag_data_received");
81 bool InsertBug (int x, int y, string uri, int id)
83 try {
84 // Debounce. I'm not sure why this is necessary :(
85 if (id == last_bug) {
86 last_bug = -1;
87 return true;
89 last_bug = id;
91 TracLink link_tag = (TracLink)
92 Note.TagTable.CreateDynamicTag (TracLinkTagName);
93 link_tag.BugUrl = uri;
95 // Place the cursor in the position where the uri was
96 // dropped, adjusting x,y by the TextView's VisibleRect.
97 Gdk.Rectangle rect = Window.Editor.VisibleRect;
98 x = x + rect.X;
99 y = y + rect.Y;
100 Gtk.TextIter cursor = Window.Editor.GetIterAtLocation (x, y);
101 Buffer.PlaceCursor (cursor);
103 Buffer.Undoer.AddUndoAction (new InsertBugAction (cursor, id.ToString (), Buffer, link_tag));
105 Gtk.TextTag[] tags = {link_tag};
106 Buffer.InsertWithTags (ref cursor, id.ToString (), tags);
107 return true;
108 } catch {
109 return false;