/// <summary> /// Get the existing template note or create a new one /// if it doesn't already exist. /// </summary> /// <returns> /// A <see cref="Note"/> /// </returns> public Note GetOrCreateTemplateNote() { Note template_note = Find(NoteTemplateTitle); if (template_note == null) { template_note = Create(NoteTemplateTitle, GetNoteTemplateContent(NoteTemplateTitle)); // Select the initial text NoteBuffer buffer = template_note.Buffer; Gtk.TextIter iter = buffer.GetIterAtLineOffset(2, 0); buffer.MoveMark(buffer.SelectionBound, iter); buffer.MoveMark(buffer.InsertMark, buffer.EndIter); // Flag this as a template note Tag tag = TagManager.GetOrCreateSystemTag(TagManager.TemplateNoteSystemTag); template_note.AddTag(tag); template_note.QueueSave(ChangeType.ContentChanged); } return(template_note); }
/// <summary> /// Search the notes! A match number of /// <see cref="int.MaxValue"/> indicates that the note /// title contains the search term. /// </summary> /// <param name="query"> /// A <see cref="System.String"/> /// </param> /// <param name="case_sensitive"> /// A <see cref="System.Boolean"/> /// </param> /// <param name="selected_notebook"> /// A <see cref="Notebooks.Notebook"/>. If this is not /// null, only the notes of the specified notebook will /// be searched. /// </param> /// <returns> /// A <see cref="IDictionary`2"/> with the relevant Notes /// and a match number. If the search term is in the title, /// number will be <see cref="int.MaxValue"/>. /// </returns> public IDictionary <Note, int> SearchNotes( string query, bool case_sensitive, Notebooks.Notebook selected_notebook) { string [] words = Search.SplitWatchingQuotes(query); // Used for matching in the raw note XML string [] encoded_words = SplitWatchingQuotes(XmlEncoder.Encode(query)); Dictionary <Note, int> temp_matches = new Dictionary <Note, int>(); // Skip over notes that are template notes Tag template_tag = TagManager.GetOrCreateSystemTag(TagManager.TemplateNoteSystemTag); foreach (Note note in manager.Notes) { // Skip template notes if (note.ContainsTag(template_tag)) { continue; } // Skip notes that are not in the // selected notebook if (selected_notebook != null && selected_notebook.ContainsNote(note) == false) { continue; } // First check the note's title for a match, // if there is no match check the note's raw // XML for at least one match, to avoid // deserializing Buffers unnecessarily. if (0 < FindMatchCountInNote(note.Title, words, case_sensitive)) { temp_matches.Add(note, int.MaxValue); } else if (CheckNoteHasMatch(note, encoded_words, case_sensitive)) { int match_count = FindMatchCountInNote(note.TextContent, words, case_sensitive); if (match_count > 0) { // TODO: Improve note.GetHashCode() temp_matches.Add(note, match_count); } } } return(temp_matches); }
// Creates a new note with the given title and guid with body based on // the template note. private Note CreateNoteFromTemplate(string title, Note template_note, string guid) { Tag template_save_title = TagManager.GetOrCreateSystemTag(TagManager.TemplateNoteSaveTitleSystemTag); if (template_note.ContainsTag(template_save_title)) { title = GetUniqueName(template_note.Title, notes.Count); } // Use the body from the template note string xml_content = template_note.XmlContent.Replace(XmlEncoder.Encode(template_note.Title), XmlEncoder.Encode(title)); xml_content = SanitizeXmlContent(xml_content); Note new_note = CreateNewNote(title, xml_content, guid); // Copy template note's properties Tag template_save_size = TagManager.GetOrCreateSystemTag(TagManager.TemplateNoteSaveSizeSystemTag); if (template_note.Data.HasExtent() && template_note.ContainsTag(template_save_size)) { new_note.Data.Height = template_note.Data.Height; new_note.Data.Width = template_note.Data.Width; } Tag template_save_selection = TagManager.GetOrCreateSystemTag(TagManager.TemplateNoteSaveSelectionSystemTag); if (template_note.Data.CursorPosition > 0 && template_note.ContainsTag(template_save_selection)) { Gtk.TextBuffer buffer = new_note.Buffer; Gtk.TextIter iter; // Because the titles will be different between template and // new note, we can't just drop the cursor at template's // CursorPosition. Whitespace after the title makes this more // complicated so let's just start counting from the line after the title. int title_offset_difference = buffer.GetIterAtLine(1).Offset - template_note.Buffer.GetIterAtLine(1).Offset; iter = buffer.GetIterAtOffset(template_note.Data.CursorPosition + title_offset_difference); buffer.PlaceCursor(iter); iter = buffer.GetIterAtOffset(template_note.Data.SelectionBoundPosition + title_offset_difference); buffer.MoveMark(buffer.SelectionBound.Name, iter); } return(new_note); }
/// <summary> /// Get the existing template note or create a new one /// if it doesn't already exist. /// </summary> /// <returns> /// A <see cref="Note"/> /// </returns> public Note GetOrCreateTemplateNote() { // The default template note will have the system template tag and // will belong to zero notebooks. We find this by searching all // notes with the TemplateNoteSystemTag and check that it's // notebook == null Note template_note = null; Tag template_tag = TagManager.GetOrCreateSystemTag(TagManager.TemplateNoteSystemTag); foreach (Note note in template_tag.Notes) { if (Notebooks.NotebookManager.GetNotebookFromNote(note) == null) { template_note = note; break; } } if (template_note == null) { template_note = Create(NoteTemplateTitle, GetNoteTemplateContent(NoteTemplateTitle)); // Select the initial text NoteBuffer buffer = template_note.Buffer; Gtk.TextIter iter = buffer.GetIterAtLineOffset(2, 0); buffer.MoveMark(buffer.SelectionBound, iter); buffer.MoveMark(buffer.InsertMark, buffer.EndIter); // Flag this as a template note template_note.AddTag(template_tag); template_note.QueueSave(ChangeType.ContentChanged); } return(template_note); }
public void AddRecentlyChangedNotes() { int min_size = (int)Preferences.Get(Preferences.MENU_NOTE_COUNT); int max_size = (int)Preferences.Get(Preferences.MENU_MAX_NOTE_COUNT); if (max_size < min_size) { max_size = min_size; } int list_size = 0; bool menuOpensUpward = tray.MenuOpensUpward(); NoteMenuItem item; // Remove the old dynamic items RemoveRecentlyChangedNotes(); // Assume menu opens downward, move common items to top of menu Gtk.MenuItem newNoteItem = Tomboy.ActionManager.GetWidget( "/TrayIconMenu/TrayNewNotePlaceholder/TrayNewNote") as Gtk.MenuItem; Gtk.MenuItem searchNotesItem = Tomboy.ActionManager.GetWidget( "/TrayIconMenu/ShowSearchAllNotes") as Gtk.MenuItem; tray_menu.ReorderChild(newNoteItem, 0); int insertion_point = 1; // If menu opens downward // Find all child widgets under the TrayNewNotePlaceholder // element. Make sure those added by add-ins are // properly accounted for and reordered. List <Gtk.Widget> newNotePlaceholderWidgets = new List <Gtk.Widget> (); IList <Gtk.Widget> allChildWidgets = Tomboy.ActionManager.GetPlaceholderChildren("/TrayIconMenu/TrayNewNotePlaceholder"); foreach (Gtk.Widget child in allChildWidgets) { if (child is Gtk.MenuItem && child != newNoteItem) { newNotePlaceholderWidgets.Add(child); tray_menu.ReorderChild(child, insertion_point); insertion_point++; } } tray_menu.ReorderChild(searchNotesItem, insertion_point); insertion_point++; DateTime days_ago = DateTime.Today.AddDays(-3); // Prevent template notes from appearing in the menu Tag template_tag = TagManager.GetOrCreateSystemTag(TagManager.TemplateNoteSystemTag); // List the most recently changed notes, any currently // opened notes, and any pinned notes... foreach (Note note in manager.Notes) { if (note.IsSpecial) { continue; } // Skip template notes if (note.ContainsTag(template_tag)) { continue; } bool show = false; // Test for note.IsPinned first so that all of the pinned notes // are guaranteed to be included regardless of the size of the // list. if (note.IsPinned) { show = true; } else if ((note.IsOpened && note.Window.IsMapped) || note.ChangeDate > days_ago || list_size < min_size) { if (list_size <= max_size) { show = true; } } if (show) { item = new NoteMenuItem(note, true); // Add this widget to the menu (+insertion_point to add after new+search+...) tray_menu.Insert(item, list_size + insertion_point); // Keep track of this item so we can remove it later recent_notes.Add(item); list_size++; } } Note start = manager.FindByUri(NoteManager.StartNoteUri); if (start != null) { item = new NoteMenuItem(start, false); if (menuOpensUpward) { tray_menu.Insert(item, list_size + insertion_point); } else { tray_menu.Insert(item, insertion_point); } recent_notes.Add(item); list_size++; bool enable_keybindings = (bool) Preferences.Get(Preferences.ENABLE_KEYBINDINGS); if (enable_keybindings) { GConfKeybindingToAccel.AddAccelerator( item, Preferences.KEYBINDING_OPEN_START_HERE); } } // FIXME: Rearrange this stuff to have less wasteful reordering if (menuOpensUpward) { // Relocate common items to bottom of menu insertion_point -= 1; tray_menu.ReorderChild(searchNotesItem, list_size + insertion_point); foreach (Gtk.Widget widget in newNotePlaceholderWidgets) { tray_menu.ReorderChild(widget, list_size + insertion_point); } tray_menu.ReorderChild(newNoteItem, list_size + insertion_point); insertion_point = list_size; } Gtk.SeparatorMenuItem separator = new Gtk.SeparatorMenuItem(); tray_menu.Insert(separator, insertion_point); recent_notes.Add(separator); }
IEnumerable <Gtk.MenuItem> CurrentMenuItems() { Gtk.ImageMenuItem item; item = new Gtk.ImageMenuItem(Catalog.GetString("Create New Note")); item.Image = new Gtk.Image(Gtk.Stock.New, Gtk.IconSize.Menu); item.Activated += (o, a) => Tomboy.ActionManager["NewNoteAction"].Activate(); item.Activated += (o, a) => SetMenuItems(); yield return(item); item = new Gtk.ImageMenuItem(Catalog.GetString("_Search All Notes")); item.Image = new Gtk.Image(Gtk.Stock.Find, Gtk.IconSize.Menu); item.Activated += (o, a) => Tomboy.ActionManager["ShowSearchAllNotesAction"].Activate(); yield return(item); yield return(new Gtk.SeparatorMenuItem()); if (manager != null && manager.Notes != null) { Tag template_tag = TagManager.GetOrCreateSystemTag(TagManager.TemplateNoteSystemTag); var menuItems = manager.Notes .Where(n => !n.IsSpecial && !n.ContainsTag(template_tag)) .OrderByDescending(n => n.ChangeDate) .Take(10) .Select(n => CreateNoteMenuItem(n)) .ToArray(); // avoid lazy eval for menu item construction foreach (Gtk.MenuItem mi in menuItems) { yield return(mi); } } yield return(new Gtk.SeparatorMenuItem()); Gtk.MenuItem mitem = new Gtk.MenuItem(Catalog.GetString("S_ynchronize Notes")); // setting this changes the menu text to "Convert" // item.Image = new Gtk.Image (Gtk.Stock.Convert, Gtk.IconSize.Menu); mitem.Activated += (o, a) => Tomboy.ActionManager["NoteSynchronizationAction"].Activate(); yield return(mitem); item = new Gtk.ImageMenuItem(Catalog.GetString("_Preferences")); item.Image = new Gtk.Image(Gtk.Stock.Preferences, Gtk.IconSize.Menu); item.Activated += (o, a) => Tomboy.ActionManager["ShowPreferencesAction"].Activate(); yield return(item); item = new Gtk.ImageMenuItem(Catalog.GetString("_Help")); item.Image = new Gtk.Image(Gtk.Stock.Help, Gtk.IconSize.Menu); item.Activated += (o, a) => Tomboy.ActionManager["ShowHelpAction"].Activate(); yield return(item); item = new Gtk.ImageMenuItem(Catalog.GetString("_About Tomboy")); item.Image = new Gtk.Image(Gtk.Stock.About, Gtk.IconSize.Menu); item.Activated += (o, a) => Tomboy.ActionManager["ShowAboutAction"].Activate(); yield return(item); yield return(new Gtk.SeparatorMenuItem()); item = new Gtk.ImageMenuItem(Catalog.GetString("_Quit")); item.Image = new Gtk.Image(Gtk.Stock.Quit, Gtk.IconSize.Menu); item.Activated += (o, a) => Tomboy.ActionManager["QuitTomboyAction"].Activate(); yield return(item); }
private static void AddRecentNotes(ICustomDestinationList custom_destinationd_list, NoteManager note_manager, uint slots) { IObjectCollection object_collection = (IObjectCollection)Activator.CreateInstance(Type.GetTypeFromCLSID(CLSID.EnumerableObjectCollection)); // Prevent template notes from appearing in the menu Tag template_tag = TagManager.GetOrCreateSystemTag(TagManager.TemplateNoteSystemTag); uint index = 0; foreach (Note note in note_manager.Notes) { if (note.IsSpecial) { continue; } // Skip template notes if (note.ContainsTag(template_tag)) { continue; } string note_title = note.Title; if (note.IsNew) { note_title = String.Format(Catalog.GetString("{0} (new)"), note_title); } IShellLink note_link = CreateShellLink(note_title, tomboy_path, "--open-note " + note.Uri, System.IO.Path.Combine(icons_path, NoteIcon), -1); if (note_link != null) { object_collection.AddObject(note_link); } if (++index == slots - 1) { break; } } // Add Start Here note Note start_note = note_manager.FindByUri(NoteManager.StartNoteUri); if (start_note != null) { IShellLink start_note_link = CreateShellLink(start_note.Title, tomboy_path, "--open-note " + NoteManager.StartNoteUri, System.IO.Path.Combine(icons_path, NoteIcon), -1); if (start_note_link != null) { object_collection.AddObject(start_note_link); } } custom_destinationd_list.AppendCategory(Catalog.GetString("Recent Notes"), (IObjectArray)object_collection); Marshal.ReleaseComObject(object_collection); object_collection = null; }