~ubuntu-branches/ubuntu/lucid/tomboy/lucid-proposed

« back to all changes in this revision

Viewing changes to Tomboy/Note.cs

  • Committer: Ken VanDine
  • Date: 2010-10-26 14:37:51 UTC
  • mfrom: (47.1.1 lucid-proposed)
  • Revision ID: ken.vandine@canonical.com-20101026143751-m1bsy5ukg3h8f0rb
* New upstream version:
  - Fix bullet list indentation when printing (lp: #516494)
  - Add timestamp to logger messages, useful for watching autosync output, etc
  - Other fixes: LP:#556628

Show diffs side-by-side

added added

removed removed

Lines of Context:
896
896
                        xmlDoc.LoadXml (foreignNoteXml);
897
897
                        xmlDoc = null;
898
898
 
899
 
                        StringReader reader = new StringReader (foreignNoteXml);
900
 
                        XmlTextReader xml = new XmlTextReader (reader);
901
 
                        xml.Namespaces = false;
902
 
 
903
899
                        // Remove tags now, since a note with no tags has
904
900
                        // no "tags" element in the XML
905
901
                        List<Tag> newTags = new List<Tag> ();
906
 
                        DateTime date;
907
902
 
908
 
                        while (xml.Read ()) {
909
 
                                switch (xml.NodeType) {
910
 
                                case XmlNodeType.Element:
911
 
                                        switch (xml.Name) {
912
 
                                        case "title":
913
 
                                                Title = xml.ReadString ();
914
 
                                                break;
915
 
                                        case "text":
916
 
                                                XmlContent = xml.ReadInnerXml ();
917
 
                                                break;
918
 
                                        case "last-change-date":
919
 
                                                if (DateTime.TryParse (xml.ReadString (), out date))
920
 
                                                        data.Data.ChangeDate = date;
921
 
                                                else
922
 
                                                        data.Data.ChangeDate = DateTime.Now;
923
 
                                                break;
924
 
                                        case "last-metadata-change-date":
925
 
                                                if (DateTime.TryParse (xml.ReadString (), out date))
926
 
                                                        data.Data.MetadataChangeDate = date;
927
 
                                                else
928
 
                                                        data.Data.MetadataChangeDate = DateTime.Now;
929
 
                                                break;
930
 
                                        case "create-date":
931
 
                                                if (DateTime.TryParse (xml.ReadString (), out date))
932
 
                                                        data.Data.CreateDate = date;
933
 
                                                else
934
 
                                                        data.Data.CreateDate = DateTime.Now;
935
 
                                                break;
936
 
                                        case "tags":
937
 
                                                XmlDocument doc = new XmlDocument ();
938
 
                                                List<string> tag_strings = ParseTags (doc.ReadNode (xml.ReadSubtree ()));
939
 
                                                foreach (string tag_str in tag_strings) {
940
 
                                                        Tag tag = TagManager.GetOrCreateTag (tag_str);
941
 
                                                        newTags.Add (tag);
 
903
                        using (var xml = new XmlTextReader (new StringReader (foreignNoteXml)) {Namespaces = false}) {
 
904
                                DateTime date;
 
905
        
 
906
                                while (xml.Read ()) {
 
907
                                        switch (xml.NodeType) {
 
908
                                        case XmlNodeType.Element:
 
909
                                                switch (xml.Name) {
 
910
                                                case "title":
 
911
                                                        Title = xml.ReadString ();
 
912
                                                        break;
 
913
                                                case "text":
 
914
                                                        XmlContent = xml.ReadInnerXml ();
 
915
                                                        break;
 
916
                                                case "last-change-date":
 
917
                                                        if (DateTime.TryParse (xml.ReadString (), out date))
 
918
                                                                data.Data.ChangeDate = date;
 
919
                                                        else
 
920
                                                                data.Data.ChangeDate = DateTime.Now;
 
921
                                                        break;
 
922
                                                case "last-metadata-change-date":
 
923
                                                        if (DateTime.TryParse (xml.ReadString (), out date))
 
924
                                                                data.Data.MetadataChangeDate = date;
 
925
                                                        else
 
926
                                                                data.Data.MetadataChangeDate = DateTime.Now;
 
927
                                                        break;
 
928
                                                case "create-date":
 
929
                                                        if (DateTime.TryParse (xml.ReadString (), out date))
 
930
                                                                data.Data.CreateDate = date;
 
931
                                                        else
 
932
                                                                data.Data.CreateDate = DateTime.Now;
 
933
                                                        break;
 
934
                                                case "tags":
 
935
                                                        XmlDocument doc = new XmlDocument ();
 
936
                                                        List<string> tag_strings = ParseTags (doc.ReadNode (xml.ReadSubtree ()));
 
937
                                                        foreach (string tag_str in tag_strings) {
 
938
                                                                Tag tag = TagManager.GetOrCreateTag (tag_str);
 
939
                                                                newTags.Add (tag);
 
940
                                                        }
 
941
                                                        break;
 
942
                                                case "open-on-startup":
 
943
                                                        bool isStartup;
 
944
                                                        if (bool.TryParse (xml.ReadString (), out isStartup))
 
945
                                                                IsOpenOnStartup = isStartup;
 
946
                                                        break;
942
947
                                                }
943
948
                                                break;
944
 
                                        case "open-on-startup":
945
 
                                                bool isStartup;
946
 
                                                if (bool.TryParse (xml.ReadString (), out isStartup))
947
 
                                                        IsOpenOnStartup = isStartup;
948
 
                                                break;
949
949
                                        }
950
 
                                        break;
951
950
                                }
952
951
                        }
953
952
 
954
 
                        xml.Close ();
955
 
 
956
953
                        foreach (Tag oldTag in Tags)
957
954
                                if (!newTags.Contains (oldTag))
958
955
                                    RemoveTag (oldTag);
1267
1264
 
1268
1265
                public virtual NoteData ReadFile (string read_file, string uri)
1269
1266
                {
1270
 
                        StreamReader reader = new StreamReader (read_file,
1271
 
                                                                System.Text.Encoding.UTF8);
1272
 
                        XmlTextReader xml = new XmlTextReader (reader);
1273
 
                        xml.Namespaces = false;
1274
 
 
 
1267
                        NoteData data;
1275
1268
                        string version;
1276
 
                        NoteData data = Read (xml, uri, out version);
 
1269
                        using (var xml = new XmlTextReader (new StreamReader (read_file, System.Text.Encoding.UTF8)) {Namespaces = false})
 
1270
                                data = Read (xml, uri, out version);
1277
1271
 
1278
1272
                        if (version != NoteArchiver.CURRENT_VERSION) {
1279
1273
                                // Note has old format, so rewrite it.  No need
1282
1276
                                NoteArchiver.Write (read_file, data);
1283
1277
                        }
1284
1278
 
1285
 
                        reader.Close ();
1286
 
                        xml.Close ();
1287
 
 
1288
1279
                        return data;
1289
1280
                }
1290
1281
 
1379
1370
                public static string WriteString(NoteData note)
1380
1371
                {
1381
1372
                        StringWriter str = new StringWriter ();
1382
 
                        XmlWriter xml = XmlWriter.Create (str, XmlEncoder.DocumentSettings);
1383
 
                        Instance.Write (xml, note);
1384
 
                        xml.Close ();
 
1373
                        using (var xml = XmlWriter.Create (str, XmlEncoder.DocumentSettings))
 
1374
                                Instance.Write (xml, note);
1385
1375
                        str.Flush();
1386
1376
                        return str.ToString ();
1387
1377
                }
1395
1385
                {
1396
1386
                        string tmp_file = write_file + ".tmp";
1397
1387
 
1398
 
                        XmlWriter xml = XmlWriter.Create (tmp_file, XmlEncoder.DocumentSettings);
1399
 
                        Write (xml, note);
1400
 
                        xml.Close ();
 
1388
                        using (var xml = XmlWriter.Create (tmp_file, XmlEncoder.DocumentSettings))
 
1389
                                Write (xml, note);
1401
1390
 
1402
1391
                        if (File.Exists (write_file)) {
1403
1392
                                string backup_path = write_file + "~";
1425
1414
 
1426
1415
                public void WriteFile (TextWriter writer, NoteData note)
1427
1416
                {
1428
 
                        XmlWriter xml = XmlWriter.Create (writer, XmlEncoder.DocumentSettings);
1429
 
                        Write (xml, note);
1430
 
                        xml.Close ();
 
1417
                        using (var xml = XmlWriter.Create (writer, XmlEncoder.DocumentSettings))
 
1418
                                Write (xml, note);
1431
1419
                }
1432
1420
 
1433
1421
                void Write (XmlWriter xml, NoteData note)