Example #1
0
        /// <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);
        }
		/// <summary>
		/// Loop through the system tags looking for notebooks
		/// </summary>
		private static void LoadNotebooks ()
		{
			Logger.Debug ("Loading notebooks");
			Gtk.TreeIter iter = Gtk.TreeIter.Zero;
			foreach (Tag tag in TagManager.AllTags) {
				// Skip over tags that aren't notebooks
				if (tag.IsSystem == false
						|| tag.Name.StartsWith (Tag.SYSTEM_TAG_PREFIX + Notebook.NotebookTagPrefix) == false) {
					continue;
				}
				Notebook notebook = new Notebook (tag);
				iter = notebooks.Append ();
				notebooks.SetValue (iter, 0, notebook);
				notebookMap [notebook.NormalizedName] = iter;
			}
		}
		public static void FireNoteRemovedFromNoteBook (Note note, Notebook notebook)
		{
			if (NoteRemovedFromNotebook != null)
				NoteRemovedFromNotebook (note, notebook);
		}
		public static void FireNoteAddedToNoteBook (Note note, Notebook notebook)
		{
			if (NoteAddedToNotebook != null)
				NoteAddedToNotebook (note, notebook);
		}
		/// <summary>
		/// Place the specified note into the specified notebook.  If the
		/// note already belongs to a notebook, it will be removed from that
		/// notebook first.
		/// </summary>
		/// <param name="note">
		/// A <see cref="Note"/>
		/// </param>
		/// <param name="notebook">
		/// A <see cref="Notebook"/>.  If Notebook is null, the note will
		/// be removed from its current notebook.
		/// </param>
		/// <returns>True if the note was successfully moved.</returns>
		public static bool MoveNoteToNotebook (Note note, Notebook notebook)
		{
			if (note == null)
				return false;
			
			// NOTE: In the future we may want to allow notes
			// to exist in multiple notebooks.  For now, to
			// alleviate the confusion, only allow a note to
			// exist in one notebook at a time.
			
			Notebook currentNotebook = GetNotebookFromNote (note);
			if (currentNotebook == notebook)
				return true; // It's already there.
			
			if (currentNotebook != null) {
				note.RemoveTag (currentNotebook.Tag);
				if (NoteRemovedFromNotebook != null)
					NoteRemovedFromNotebook (note, currentNotebook);
			}
			
			// Only attempt to add the notebook tag when this
			// menu item is not the "No notebook" menu item.
			if (notebook != null && (notebook is SpecialNotebook) == false) {
				note.AddTag (notebook.Tag);
				if (NoteAddedToNotebook != null)
					NoteAddedToNotebook (note, notebook);
			}
			
			return true;
		}
		/// <summary>
		/// Prompt the user and delete the notebok (if they say so).
		/// </summary>
		/// <param name="parent">
		/// A <see cref="Gtk.Window"/>
		/// </param>
		/// <param name="notebook">
		/// A <see cref="Notebook"/>
		/// </param>
		public static void PromptDeleteNotebook (Gtk.Window parent, Notebook notebook)
		{
			// Confirmation Dialog
			HIGMessageDialog dialog =
				new HIGMessageDialog (parent,
									  Gtk.DialogFlags.Modal,
									  Gtk.MessageType.Question,
									  Gtk.ButtonsType.YesNo,
									  Catalog.GetString ("Really delete this notebook?"),
									  Catalog.GetString (
									  	"The notes that belong to this notebook will not be " +
									  	"deleted, but they will no longer be associated with " +
									  	"this notebook.  This action cannot be undone."));
			dialog.DefaultResponse = Gtk.ResponseType.No;
			int response = dialog.Run ();
			dialog.Destroy ();
			if (response != (int) Gtk.ResponseType.Yes)
				return;
			
			// Grab the template note before removing all the notebook tags
			Note templateNote = notebook.GetTemplateNote ();
			
			DeleteNotebook (notebook);

			// Delete the template note
			if (templateNote != null) {
				NoteManager noteManager = Tomboy.DefaultNoteManager;
				noteManager.Delete (templateNote);
			}
		}
		/// <summary>
		/// Returns the Gtk.TreeIter that points to the specified Notebook.
		/// </summary>
		/// <param name="notebook">
		/// A <see cref="Notebook"/>
		/// </param>
		/// <param name="iter">
		/// A <see cref="Gtk.TreeIter"/>.  Will be set to a valid iter if
		/// the specified notebook is found.
		/// </param>
		/// <returns>
		/// A <see cref="System.Boolean"/>.  True if the specified notebook
		/// was found, false otherwise.
		/// </returns>
		public static bool GetNotebookIter (Notebook notebook, out Gtk.TreeIter iter)
		{
			Gtk.TreeIter current_iter;
			if (sortedNotebooks.GetIterFirst (out current_iter)) {
				do {
					Notebook current_notebook = (Notebook)sortedNotebooks.GetValue (current_iter, 0);
					if (notebook == current_notebook) {
						iter = current_iter;
						return true;
					}
				} while (sortedNotebooks.IterNext (ref current_iter));
			}
			
			iter = Gtk.TreeIter.Zero;
			return false;
		}
		/// <summary>
		/// Delete the specified notebook from the system
		/// </summary>
		/// <param name="notebook">
		/// A <see cref="Notebook"/>
		/// </param>
		public static void DeleteNotebook (Notebook notebook)
		{
			if (notebook == null)
				throw new ArgumentNullException ("NotebookManager.DeleteNotebook () called with a null argument.");
			
			if (notebookMap.ContainsKey (notebook.NormalizedName) == false)
				return;
			
			lock (locker) {
				if (notebookMap.ContainsKey (notebook.NormalizedName) == false)
					return;
				
				Gtk.TreeIter iter = notebookMap [notebook.NormalizedName];
				if (notebooks.Remove (ref iter) == true) {
					Logger.Debug ("NotebookManager: Removed notebook: {0}", notebook.NormalizedName);
				} else {
					Logger.Warn ("NotebookManager: Call to remove notebook failed: {0}", notebook.NormalizedName);
				}
				
				notebookMap.Remove (notebook.NormalizedName);
				
				// Remove the notebook tag from every note that's in the notebook
				foreach (Note note in notebook.Tag.Notes) {
					note.RemoveTag (notebook.Tag);
					if (NoteRemovedFromNotebook != null)
						NoteRemovedFromNotebook (note, notebook);
				}
			}
		}
		public static Notebook GetOrCreateNotebook (string notebookName)
		{
			if (notebookName == null)
				throw new ArgumentNullException ("NotebookManager.GetNotebook () called with a null name.");
			
			Notebook notebook = GetNotebook (notebookName);
			if (notebook != null)
				return notebook;
			
			Gtk.TreeIter iter = Gtk.TreeIter.Zero;
			lock (locker) {
				notebook = GetNotebook (notebookName);
				if (notebook != null)
					return notebook;
				
				try {
					AddingNotebook = true;
					notebook = new Notebook (notebookName);
				} finally {
					AddingNotebook = false;
				}
				iter = notebooks.Append ();
				notebooks.SetValue (iter, 0, notebook);
				notebookMap [notebook.NormalizedName] = iter;
				
				// Create the template note so the system tag
				// that represents the notebook actually gets
				// saved to a note (and persisted after Tomboy
				// is shut down).
				Note templateNote = notebook.GetTemplateNote ();
				
				// Make sure the template note has the notebook tag.
				// Since it's possible for the template note to already
				// exist, we need to make sure it gets tagged.
				templateNote.AddTag (notebook.Tag);
				if (NoteAddedToNotebook != null)
					NoteAddedToNotebook (templateNote, notebook);
			}

			return notebook;
		}
		void UpdateNotebookButtonLabel (Notebook notebook)
		{
			string labelText =
				notebook == null ?
					Catalog.GetString ("Notebook") :
					notebook.Name;
			
			// Ellipsize names longer than 12 chars in length
			// TODO: Should we hardcode the ellipsized notebook name to 12 chars?
			Gtk.Label l = toolButton.LabelWidget as Gtk.Label;
			if (l != null) {
				l.Text = labelText;
				l.MaxWidthChars = 12;
				l.Ellipsize = Pango.EllipsizeMode.End;
				l.ShowAll ();
			}
		}
		void OnNoteRemovedFromNotebook (Note note, Notebook notebook)
		{
			if (note == Note)
				UpdateNotebookButtonLabel (null);
		}
		void OnNoteAddedToNotebook (Note note, Notebook notebook)
		{
			if (note == Note)
				UpdateNotebookButtonLabel (notebook);
		}
Example #13
0
        void UpdateNotebookButtonLabel()
        {
            Notebook currentNotebook = NotebookManager.GetNotebookFromNote(Note);

            UpdateNotebookButtonLabel(currentNotebook);
        }