comparison demos/browser/xbel.d @ 77:0654fc9bac95

more porting
author mandel
date Sun, 24 May 2009 13:46:32 +0000
parents 37caa90ce503
children 85c59c4e5f19
comparison
equal deleted inserted replaced
76:454e4b4beb59 77:0654fc9bac95
78 } 78 }
79 79
80 bool operator==(BookmarkNode other) 80 bool operator==(BookmarkNode other)
81 { 81 {
82 if (url != other.url || title != other.title || desc != other.desc || expanded != other.expanded 82 if (url != other.url || title != other.title || desc != other.desc || expanded != other.expanded
83 || m_type != other.m_type || m_children.count() != other.m_children.count()) 83 || m_type != other.m_type || m_children.length != other.m_children.length)
84 return false; 84 return false;
85 85
86 for (int i = 0; i < m_children.count(); ++i) 86 for (int i = 0; i < m_children.length; ++i)
87 if (!((*(m_children[i])) == (*(other.m_children[i])))) 87 if (!((*(m_children[i])) == (*(other.m_children[i]))))
88 return false; 88 return false;
89 return true; 89 return true;
90 } 90 }
91 91
102 BookmarkNode[] children() 102 BookmarkNode[] children()
103 { 103 {
104 return m_children; 104 return m_children;
105 } 105 }
106 106
107 107 BookmarkNode parent()
108 BookmarkNode parent() const
109 { 108 {
110 return m_parent; 109 return m_parent;
111 } 110 }
112 111
113 void add(BookmarkNode child, int offset = -1) 112 void add(BookmarkNode child, int offset = -1)
115 assert(child.m_type != Root); 114 assert(child.m_type != Root);
116 if (child.m_parent) 115 if (child.m_parent)
117 child.m_parent.remove(child); 116 child.m_parent.remove(child);
118 child.m_parent = this; 117 child.m_parent = this;
119 if (-1 == offset) 118 if (-1 == offset)
120 offset = m_children.size(); 119 offset = m_children.length;
121 m_children.insert(offset, child); 120 m_children.insert(offset, child);
122 } 121 }
123 122
124 void remove(BookmarkNode child) 123 void remove(BookmarkNode child)
125 { 124 {
126 child.m_parent = 0; 125 child.m_parent = 0;
127 m_children.removeAll(child); 126 m_children.removeAll(child);
128 } 127 }
129 128
130 QString url; 129 string url;
131 QString title; 130 string title;
132 QString desc; 131 string desc;
133 bool expanded; 132 bool expanded;
134 133
135 private: 134 private:
136 135
137 BookmarkNode m_parent; 136 BookmarkNode m_parent;
145 144
146 this() 145 this()
147 { 146 {
148 } 147 }
149 148
150 BookmarkNode read(QString fileName) 149 BookmarkNode read(string fileName)
151 { 150 {
152 auto file = new QFile(fileName); 151 auto file = new QFile(fileName);
153 if (!file.exists()) { 152 if (!file.exists()) {
154 return new BookmarkNode(BookmarkNode.Root); 153 return new BookmarkNode(BookmarkNode.Root);
155 } 154 }
162 BookmarkNode root = new BookmarkNode(BookmarkNode.Root); 161 BookmarkNode root = new BookmarkNode(BookmarkNode.Root);
163 setDevice(device); 162 setDevice(device);
164 while (!atEnd()) { 163 while (!atEnd()) {
165 readNext(); 164 readNext();
166 if (isStartElement()) { 165 if (isStartElement()) {
167 QString version_ = attributes().value(QLatin1String("version")).toString(); 166 string version_ = attributes().value(QLatin1String("version")).toString();
168 if (name() == QLatin1String("xbel") && (version_.isEmpty() || version_ == QLatin1String("1.0"))) { 167 if (name() == QLatin1String("xbel") && (version_.isEmpty() || version_ == QLatin1String("1.0"))) {
169 readXBEL(root); 168 readXBEL(root);
170 } else { 169 } else {
171 raiseError(QObject.tr("The file is not an XBEL version 1.0 file.")); 170 raiseError(QObject.tr("The file is not an XBEL version 1.0 file."));
172 } 171 }
297 this() 296 this()
298 { 297 {
299 setAutoFormatting(true); 298 setAutoFormatting(true);
300 } 299 }
301 300
302 bool write(QString fileName, BookmarkNode root) 301 bool write(string fileName, BookmarkNode root)
303 { 302 {
304 QFile file(fileName); 303 auto file = new QFile(fileName);
305 if (!root || !file.open(QFile.WriteOnly)) 304 if (!root || !file.open(QFile.WriteOnly))
306 return false; 305 return false;
307 return write(&file, root); 306 return write(&file, root);
308 } 307 }
309 308
314 writeStartDocument(); 313 writeStartDocument();
315 writeDTD(QLatin1String("<!DOCTYPE xbel>")); 314 writeDTD(QLatin1String("<!DOCTYPE xbel>"));
316 writeStartElement(QLatin1String("xbel")); 315 writeStartElement(QLatin1String("xbel"));
317 writeAttribute(QLatin1String("version"), QLatin1String("1.0")); 316 writeAttribute(QLatin1String("version"), QLatin1String("1.0"));
318 if (root.type() == BookmarkNode.Root) { 317 if (root.type() == BookmarkNode.Root) {
319 for (int i = 0; i < root.children().count(); ++i) 318 for (int i = 0; i < root.children().length; ++i)
320 writeItem(root.children().at(i)); 319 writeItem(root.children()[i]);
321 } else { 320 } else {
322 writeItem(root); 321 writeItem(root);
323 } 322 }
324 323
325 writeEndDocument(); 324 writeEndDocument();
334 case BookmarkNode.Folder: 333 case BookmarkNode.Folder:
335 writeStartElement(QLatin1String("folder")); 334 writeStartElement(QLatin1String("folder"));
336 writeAttribute(QLatin1String("folded"), parent.expanded ? QLatin1String("no") : QLatin1String("yes")); 335 writeAttribute(QLatin1String("folded"), parent.expanded ? QLatin1String("no") : QLatin1String("yes"));
337 writeTextElement(QLatin1String("title"), parent.title); 336 writeTextElement(QLatin1String("title"), parent.title);
338 for (int i = 0; i < parent.children().count(); ++i) 337 for (int i = 0; i < parent.children().count(); ++i)
339 writeItem(parent.children().at(i)); 338 writeItem(parent.children()[i]);
340 writeEndElement(); 339 writeEndElement();
341 break; 340 break;
342 case BookmarkNode.Bookmark: 341 case BookmarkNode.Bookmark:
343 writeStartElement(QLatin1String("bookmark")); 342 writeStartElement(QLatin1String("bookmark"));
344 if (!parent.url.isEmpty()) 343 if (!parent.url.isEmpty())