Renamed Bugzilla -> Trac, bugzilla -> trac
[tomboy-trac.git] / src / TracNoteAddin.cs
blobeada0a9d839b59e8a54eb64efae04ffa06ea6ef2
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 regexString =
64 @"show_bug\.cgi\?(\S+\&){0,1}id=(?<" + bugIdGroup + @">\d{5,})";
66 Match match = Regex.Match (uriString, regexString);
67 if (match.Success) {
68 int bugId = int.Parse (match.Groups [bugIdGroup].Value);
69 if (InsertBug (args.X, args.Y, uriString, bugId)) {
70 Gtk.Drag.Finish (args.Context, true, false, args.Time);
71 g_signal_stop_emission_by_name(Window.Editor.Handle,
72 "drag_data_received");
78 bool InsertBug (int x, int y, string uri, int id)
80 try {
81 // Debounce. I'm not sure why this is necessary :(
82 if (id == last_bug) {
83 last_bug = -1;
84 return true;
86 last_bug = id;
88 TracLink link_tag = (TracLink)
89 Note.TagTable.CreateDynamicTag (TracLinkTagName);
90 link_tag.BugUrl = uri;
92 // Place the cursor in the position where the uri was
93 // dropped, adjusting x,y by the TextView's VisibleRect.
94 Gdk.Rectangle rect = Window.Editor.VisibleRect;
95 x = x + rect.X;
96 y = y + rect.Y;
97 Gtk.TextIter cursor = Window.Editor.GetIterAtLocation (x, y);
98 Buffer.PlaceCursor (cursor);
100 Buffer.Undoer.AddUndoAction (new InsertBugAction (cursor, id.ToString (), Buffer, link_tag));
102 Gtk.TextTag[] tags = {link_tag};
103 Buffer.InsertWithTags (ref cursor, id.ToString (), tags);
104 return true;
105 } catch {
106 return false;