3 using System
.Runtime
.InteropServices
;
5 using System
.Text
.RegularExpressions
;
14 public class TracNoteAddin
: NoteAddin
18 public const string TracLinkTagName
= "link:trac";
20 static TracNoteAddin ()
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
);
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") {
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";
64 @"show_bug\.cgi\?(\S+\&){0,1}id=(?<" + bugIdGroup
+ @">\d{5,})";
66 Match match
= Regex
.Match (uriString
, regexString
);
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
)
81 // Debounce. I'm not sure why this is necessary :(
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
;
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
);