~ted/ubuntu/lucid/tomboy/with-patch

« back to all changes in this revision

Viewing changes to Tomboy/Addins/NoteOfTheDay/NoteOfTheDay.cs

  • Committer: Bazaar Package Importer
  • Author(s): Sebastian Dröge
  • Date: 2007-07-16 10:26:35 UTC
  • mfrom: (1.1.21 upstream)
  • mto: This revision was merged to the branch mainline in revision 4.
  • Revision ID: james.westby@ubuntu.com-20070716102635-0wzk26jo50csob7b
Tags: 0.7.2-0ubuntu1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
using System;
 
2
using System.Collections;
 
3
using Tomboy;
 
4
using Mono.Unix;
 
5
 
 
6
namespace Tomboy.NoteOfTheDay
 
7
{
 
8
        public class NoteOfTheDay 
 
9
        {
 
10
                public static string TemplateTitle = Catalog.GetString ("Today: Template");
 
11
                
 
12
                static string title_prefix = Catalog.GetString ("Today: ");
 
13
 
 
14
                public static string GetTitle (DateTime day)
 
15
                {
 
16
                        // Format: "Today: Friday, July 01 2005"
 
17
                        return title_prefix + day.ToString (Catalog.GetString ("dddd, MMMM d yyyy"));
 
18
                }
 
19
 
 
20
                public static string GetContent (DateTime day, NoteManager manager)
 
21
                {
 
22
                        string title = GetTitle (day);
 
23
 
 
24
                        // Attempt to load content from template
 
25
                        Note templateNote = manager.Find (TemplateTitle);
 
26
                        if (templateNote != null)
 
27
                                return templateNote.XmlContent.Replace (TemplateTitle, title);
 
28
                        else
 
29
                                return GetTemplateContent (title);
 
30
                }
 
31
                
 
32
                public static string GetTemplateContent (string title)
 
33
                {
 
34
                        const string base_xml =
 
35
                                "<note-content>" +
 
36
                                "<note-title>{0}</note-title>\n\n\n\n" +
 
37
                                "<size:huge>{1}</size:huge>\n\n\n" +
 
38
                                "<size:huge>{2}</size:huge>\n\n\n" +
 
39
                                "</note-content>";
 
40
 
 
41
                        return string.Format (base_xml, 
 
42
                                      title, 
 
43
                                      Catalog.GetString ("Tasks"),
 
44
                                      Catalog.GetString ("Appointments"));
 
45
                }
 
46
 
 
47
                public static Note Create (NoteManager manager, DateTime day)
 
48
                {
 
49
                        string title = GetTitle (day);
 
50
                        string xml = GetContent (day, manager);
 
51
 
 
52
                        Note notd = null;
 
53
                        try {
 
54
                                notd = manager.Create (title, xml);
 
55
                        } catch (Exception e) {
 
56
                                // Prevent blowup if note creation fails
 
57
                                Logger.Error (
 
58
                                        "NoteOfTheDay could not create \"{0}\": {1}",
 
59
                                        title,
 
60
                                        e.Message);
 
61
                                notd = null;
 
62
                        }
 
63
 
 
64
                        if (notd != null) {
 
65
                                // Automatically tag all new Note of the Day notes
 
66
                                Tag notd_tag = TagManager.GetOrCreateTag (
 
67
                                                Catalog.GetString ("Note of the Day"));
 
68
                                notd.AddTag (notd_tag);
 
69
                                
 
70
                                // notd.AddTag queues a save so the following is no longer necessary
 
71
                                //notd.Save ();
 
72
                        }
 
73
 
 
74
                        return notd;
 
75
                }
 
76
 
 
77
                static string GetContentWithoutTitle (string content)
 
78
                {
 
79
                        return content.Substring (content.IndexOf ("\n"));
 
80
                }
 
81
 
 
82
                public static bool HasChanged (Note note)
 
83
                {
 
84
                        string original_xml = GetContent(note.CreateDate, note.Manager);
 
85
                        if (GetContentWithoutTitle (note.TextContent) == 
 
86
                            GetContentWithoutTitle (XmlDecoder.Decode (original_xml))) {
 
87
                                return false;
 
88
                        }
 
89
                        return true;
 
90
                }
 
91
 
 
92
                public static void CleanupOld (NoteManager manager)
 
93
                {
 
94
                        ArrayList kill_list = new ArrayList();
 
95
                        DateTime date_today = DateTime.Today; // time set to 00:00:00
 
96
 
 
97
                        foreach (Note note in manager.Notes) {
 
98
                                if (note.Title.StartsWith (title_prefix) &&
 
99
                                    note.Title != TemplateTitle &&
 
100
                                    note.CreateDate.Date != date_today &&
 
101
                                    !HasChanged (note)) {
 
102
                                        kill_list.Add (note);
 
103
                                }
 
104
                        }
 
105
                        
 
106
                        foreach (Note note in kill_list) {
 
107
                                Logger.Log ("NoteOfTheDay: Deleting old unmodified '{0}'",
 
108
                                                   note.Title);
 
109
                                manager.Delete (note);
 
110
                        }
 
111
                }
 
112
                
 
113
                ///
 
114
                /// Returns the NotD note for the specified date
 
115
                ///
 
116
                public static Note GetNoteByDate (NoteManager manager, DateTime date)
 
117
                {
 
118
                        DateTime normalized_date = date.Date; // same date with time set to 00:00:00
 
119
                        Note found_note = null;
 
120
 
 
121
                        // Go through all the NotD notes and look for the one that was
 
122
                        // created on the date specified.
 
123
                        foreach (Note note in manager.Notes) {
 
124
                                if (note.Title.StartsWith (title_prefix) &&
 
125
                                                note.Title != TemplateTitle) {
 
126
                                        DateTime note_date = note.CreateDate.Date;
 
127
                                        if (note_date == normalized_date) {
 
128
                                                found_note = note;
 
129
                                                break;
 
130
                                        }
 
131
                                }
 
132
                        }
 
133
                        
 
134
                        return found_note;
 
135
                }
 
136
        }
 
137
}