using System; using System.IO; using Tomboy; public class TagNotePlugin : NotePlugin { private Gtk.Label tagLabel; static TagNotePlugin () { } protected override void Initialize () { Gtk.MenuItem item = new Gtk.MenuItem ("Tag note"); item.Activated += TagButtonClicked; item.Show (); AddPluginMenuItem (item); Note.Manager.NoteDeleted += OnNoteDeleted; } protected override void Shutdown () { } protected override void OnNoteOpened () { InsertIntoToolbar (this.Note.Window); } protected void OnNoteDeleted (object sender, Note deleted) { if (deleted.Equals(Note)) RemoveTags(Note.Uri); } Gtk.Label TagLabel { get { return this.tagLabel; } } void TagButtonClicked (object sender, EventArgs args) { ShowDialog (); } private void ShowDialog () { LeaftagGtk.TagDialog tagDialog = new LeaftagGtk.TagDialog (); tagDialog.Modal = false; tagDialog.Uris = new string[] { this.Note.Uri }; tagDialog.Run (); tagDialog.Destroy (); UpdateTagLabelText (TagLabel); } private void InsertIntoToolbar (NoteWindow window) { Gtk.Toolbar toolbar = window.Toolbar; this.tagLabel = CreateTagLabel (); UpdateTagLabelText (TagLabel); toolbar.AppendSpace (); toolbar.AppendElement (Gtk.ToolbarChildType.Widget, TagLabel, null, null, null, null, null); Gtk.Button editTagsButton = new Gtk.Button ("Edit"); editTagsButton.Clicked += EditClicked; editTagsButton.Show (); toolbar.AppendWidget (editTagsButton, "Edit tags", null); } private void UpdateTagLabelText (Gtk.Label label) { label.Text = ReadTags (this.Note.Uri); } private Gtk.Label CreateTagLabel () { Gtk.Label tagLabel = new Gtk.Label (); tagLabel.Ellipsize = Pango.EllipsizeMode.End; tagLabel.Xpad = 6; tagLabel.Show (); tagLabel.WidthChars = 8; return tagLabel; } private void EditClicked (object sender, EventArgs args) { ShowDialog (); } /* can't be used with the old-style toolbar api used by tomboy */ /* private void NewStyleInsert (Gtk.Toolbar toolbar) { toolbar.Insert (new Gtk.SeparatorToolItem (), -1); toolbar.Insert (CreateTagLabelToolItem (), -1); toolbar.Insert (CreateTagEditToolButton (), -1); } private Gtk.ToolItem CreateTagLabelToolItem () { Gtk.ToolItem item = new Gtk.ToolItem (); item.Child = CreateTagLabel (); return item; } private Gtk.ToolItem CreateTagEditToolButton () { Gtk.ToolButton button = new Gtk.ToolButton (null, "Edit"); button.Clicked += EditClicked; return button; } */ private string ReadTags (string uri) { Leaftag.LtSource source = Leaftag.LtSource.Lookup (uri); if (source == null) return ""; Leaftag.LtTag[] tags = source.Tags; if (tags != null) { string[] tagNames = new string[tags.Length]; for (int i = 0; i < tags.Length; i++) { tagNames [i] = tags [i].Name; } return string.Join(", ", tagNames); } else { return ""; } } private void RemoveTags (string uri) { Leaftag.LtSource source = Leaftag.LtSource.Lookup (uri); if (source == null) return; source.Delete (); } }