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";
63 string baseUrlGroup
= "baseurl";
64 // Allow for trac link to contain '#' after it in case of 'preview' item
66 @"^(?<" + baseUrlGroup
+ @">.*/ticket/(?<" + bugIdGroup
+ @">\d+))(?:#.*)?$";
68 Match match
= Regex
.Match (uriString
, regexString
);
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
)
84 // Debounce. I'm not sure why this is necessary :(
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
;
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
);