def read def read_child el parent el elements each if attributes inclu

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
def read
def read_child el, parent
el.elements.each {|e|
if e.attributes["included"]
reminder = {:days => e.attributes["days"].split(",").map{|day| day.strip}, :date => e.attributes["date"], :time => e.attributes["time"]}
reminder[:included] = true if e.attributes["included"] == "true"
reminder[:included] = false if e.attributes["included"] == "false"
item = add_task parent, e.attributes["name"], reminder
read_child(e, item)
else
item = add_task parent, e.attributes["name"]
read_child(e, item)
end
item.setDisabled true if e.attributes["visible"] == "false"
}
end
xml_data = IO.read("tasks.xml")
doc = REXML::Document.new(xml_data)
doc.elements.each("tasks/task") {|el|
if el.attributes["included"]
reminder = {:days => el.attributes["days"].split(",").map{|day| day.strip}, :date => el.attributes["date"], :time => el.attributes["time"]}
reminder[:included] = true if el.attributes["included"] == "true"
reminder[:included] = false if el.attributes["included"] == "false"
item = add_task @treewidget, el.attributes["name"], reminder
read_child el, item
else
item = add_task @treewidget, el.attributes["name"]
read_child el, item
end
item.setDisabled true if el.attributes["visible"] == "false"
}
end
def save
out_file = File.open("tasks.xml", 'w')
builder = Builder::XmlMarkup.new(:target=>out_file, :indent=>2)
builder.instruct!
def write b, item
item_text = item.text(0)
if @reminder[item.object_id].included
if item.childCount > 0
b.task(:visible => !item.isDisabled, :name => item_text, :included => @reminder[item.object_id].included,:days => @reminder[item.object_id].days.inject{|res, el| res += ", " + el},:date => @reminder[item.object_id].date, :time => @reminder[item.object_id].time) {
(0...item.childCount).each { |child_index| write(b, item.child(child_index)) }
}
else
b.task :visible => !item.isDisabled, :name => item_text, :included => @reminder[item.object_id].included,:days => @reminder[item.object_id].days.inject{|res, el| res += ", " + el},:date => @reminder[item.object_id].date, :time => @reminder[item.object_id].time
end
else
if item.childCount > 0
b.task(:visible => !item.isDisabled, :name => item_text) { (0...item.childCount).each { |child_index| write(b, item.child(child_index)) } }
else
b.task :visible => !item.isDisabled, :name => item_text
end
end
end
builder.tasks {
(0...@treewidget.topLevelItemCount).each {|row|
top_item = @treewidget.topLevelItem(row)
top_item_text = top_item.text 0
if @reminder[top_item.object_id].included
if top_item.childCount > 0
builder.task(:visible => !top_item.isDisabled, :name => top_item_text, :included => @reminder[top_item.object_id].included,:days => @reminder[top_item.object_id].days.inject{|res, el| res += ", " + el},:date => @reminder[top_item.object_id].date, :time => @reminder[top_item.object_id].time) {
(0...top_item.childCount).each { |child_index| write(builder, top_item.child(child_index)) }
}
else
builder.task :visible => !top_item.isDisabled, :name => top_item_text, :included => @reminder[top_item.object_id].included,:days => @reminder[top_item.object_id].days.inject{|res, el| res += ", " + el},:date => @reminder[top_item.object_id].date, :time => @reminder[top_item.object_id].time
end
else
if top_item.childCount > 0
builder.task(:visible => !top_item.isDisabled, :name => top_item_text) {
(0...top_item.childCount).each { |child_index| write(builder, top_item.child(child_index)) }
}
else
builder.task :visible => !top_item.isDisabled, :name => top_item_text
end
end
}
}
out_file.close
end