1 from bisect
import insort
2 from gobject
import GObject
3 from straw
.error
import debug
9 Represents a base of single node in the model tree. This class is meant
10 for subclassing only, Node instances should not be used.
13 persistent_properties
= [ "id", "parent_id", "type", "norder" ]
14 persistent_table
= "nodes"
18 "norder": (int, "parent_id", "norder", 0, 999999, 0, gobject
.PARAM_READWRITE
),
19 "parent": (gobject
.TYPE_PYOBJECT
, "parent", "parent", gobject
.PARAM_READWRITE
),
20 "unread-count": (int, "unread-count", "unread-count", 0, 999999, 0, gobject
.PARAM_READWRITE
)
24 "parent-changed": (gobject
.SIGNAL_RUN_LAST
, gobject
.TYPE_NONE
, (gobject
.TYPE_PYOBJECT
,)),
25 "norder-changed": (gobject
.SIGNAL_RUN_LAST
, gobject
.TYPE_NONE
, (gobject
.TYPE_PYOBJECT
,)),
26 "unread-count-changed": (gobject
.SIGNAL_RUN_LAST
, gobject
.TYPE_NONE
, (gobject
.TYPE_PYOBJECT
, gobject
.TYPE_PYOBJECT
))
30 GObject
.__init
__(self
)
37 def __cmp__(self
, other
):
40 elif self
.norder
== other
.norder
:
42 elif self
.norder
< other
.norder
:
47 def do_get_property(self
, property):
48 if property.name
== "norder":
50 elif property.name
== "parent_id":
52 elif property.name
== "unread-count":
53 return self
.unread_count
55 def do_set_property(self
, property, value
):
56 if property.name
== "unread-count":
60 if self
.unread_count
!= value
:
61 delta
= value
- self
.unread_count
62 old_value
= self
.unread_count
63 self
.unread_count
= value
64 self
.emit("unread-count-changed", old_value
, delta
)
65 elif property.name
== "norder":
66 if self
.norder
!= value
:
67 old_value
= self
.norder
69 self
.emit("norder-changed", old_value
)
70 elif property.name
== "parent":
71 if self
.parent
.id != value
.id:
72 old_value
= self
.parent
74 self
.parent_id
= value
.id
75 self
.emit("parent-changed", old_value
)
77 def add_child(self
, node
, norder
= None, allow_append
= True):
79 if not norder
or norder
>= len(self
.children
):
80 norder
= len(self
.children
)
82 if node
.parent
== self
:
83 if norder
> len(self
.children
):
84 norder
= len(self
.children
)#raise AttributeError
87 norder
= len(self
.children
) - 1
90 norder
= len(self
.children
)
91 elif norder
> len(self
.children
):
92 norder
= len(self
.children
)
94 if norder
< len(self
.children
):
95 for child
in self
.children
[norder
:]:
96 child
.props
.norder
+= 1
98 node
.props
.norder
= norder
100 self
.append_child(node
)
102 def append_child(self
, node
):
103 debug("appending %d to %d" % (node
.id, self
.id))
104 insort(self
.children
, node
)
105 node
.connect("unread-count-changed", self
.on_unread_count_changed
)
106 self
.props
.unread_count
+= node
.unread_count
108 def remove_child(self
, node
):
109 debug("removing %d from %d" % (node
.id, self
.id))
110 for child
in self
.children
[(node
.norder
+ 1):]:
111 child
.props
.norder
-= 1
113 self
.children
.remove(node
)
114 self
.props
.unread_count
-= node
.unread_count
116 def move(self
, new_parent
, new_norder
= None):
117 debug("moving %d to %d (new_norder = %d)" % (self
.id, new_parent
.id, new_norder
))
119 self
.parent
.remove_child(self
)
120 new_parent
.add_child(self
, norder
= new_norder
, allow_append
= False)
121 self
.props
.parent
= new_parent
123 old_parent
= self
.parent
125 if new_parent
!= self
.parent
:
126 print "need to reparent %d -> %d" % (self
.parent
.id, new_parent
.id)
127 self
.props
.parent
= new_parent
128 print "reparented %d -> %d" % (old_parent
.id, new_parent
.id)
130 for child
in old_parent
.children
[(self
.norder
+ 1):]:
131 child
.props
.norder
-= 1
133 self
.parent
.children
.remove(self
)
135 if not new_norder
or new_norder
>= len(self
.parent
.children
):
136 new_norder
= len(self
.parent
.children
) - 1
138 for child
in self
.parent
.children
[new_norder
:]:
139 child
.props
.norder
+= 1
141 self
.props
.norder
= new_norder
142 insort(self
.parent
.children
, self
)
144 def get_by_path(self
, path
):
145 print "sss %d | %s | %s" % (self
.id, str(path
), str(len(self
.children
)))
148 elif path
[0] < len(self
.children
):
149 #print len(self.children)
150 #print self.children[path[0]].id
151 return self
.children
[path
[0]].get_by_path(path
[:-1])
155 def on_unread_count_changed(self
, obj
, old_value
, delta
):
158 class Category(Node
):
160 Represents a category - a node that can have child nodes.
163 persistent_properties
= [ "id", "name" ]
164 persistent_table
= "categories"
169 GObject
.__init
__(self
)
170 Node
.__gobject
_init
__(self
)
177 def do_get_property(self
, property):
178 if property.name
== "title":
181 return Node
.do_get_property(self
, property)
183 def do_set_property(self
, property, value
):
184 if property.name
== "title":
187 Node
.do_set_property(self
, property, value
)
189 def on_unread_count_changed(self
, obj
, old_value
, delta
):
190 self
.props
.unread_count
+= delta
194 Represents a feed - a node that can contain items. Feeds cannot have
198 persistent_properties
= [ "id", "title", "location", "link" ]
199 persistent_table
= "feeds"
204 "title": (str, "title", "title", "", gobject
.PARAM_READWRITE
),
205 "location": (str, "location", "location", "", gobject
.PARAM_READWRITE
),
206 "status": (int, "status", "status", 0, 9999, 0, gobject
.PARAM_READWRITE
),
210 GObject
.__init
__(self
)
211 Node
.__gobject
_init
__(self
)
218 def add_child(self
, node
):
221 def do_get_property(self
, property):
222 if property.name
== "title":
224 elif property.name
== "location":
226 elif property.name
== "status":
229 return Node
.do_get_property(self
, property)
231 def do_set_property(self
, property, value
):
232 if property.name
== "title":
234 elif property.name
== "location":
235 self
.location
= value
236 elif property.name
== "status":
239 Node
.do_set_property(self
, property, value
)
241 def on_unread_count_changed(self
, obj
, old_value
, delta
):
244 def add_item(self
, item
):
245 self
.items
.append(item
)
247 def on_is_read_changed(self
, obj
, is_read
):
249 self
.props
.unread_count
-= 1
251 self
.props
.unread_count
+= 1
254 persistent_properties
= [ "id", "title", "feed_id", "is_read", "link", "pub_date", "description" ]
255 persistent_table
= "items"
259 "title": (str, "title", "title", "", gobject
.PARAM_READWRITE
),
260 "location": (str, "location", "location", "", gobject
.PARAM_READWRITE
),
261 "is-read": (gobject
.TYPE_BOOLEAN
, "is-read", "is-read", False, gobject
.PARAM_READWRITE
)
265 "is-read-changed": (gobject
.SIGNAL_RUN_LAST
, gobject
.TYPE_NONE
, (gobject
.TYPE_PYOBJECT
,))
269 GObject
.__init
__(self
)
277 self
.description
= "description"
280 self
.guidislink
= False
282 self
.publication_name
= "publication_name"
283 self
.publication_volume
= "publication_volume"
284 self
.publication_number
= "publication_number"
285 self
.publication_section
= "publication_section"
286 self
.publication_starting_page
= "publication_starting_page"
288 self
.fm_license
= "fm_license"
289 self
.fm_changes
= "fm_changes"
291 self
.creator
= "creator"
293 self
.contributors
= []
295 self
.license_urls
= []
297 def do_get_property(self
, property):
298 if property.name
== "title":
300 elif property.name
== "is-read":
303 raise AttributeError, "unknown property %s" % property.name
305 def do_set_property(self
, property, value
):
306 if property.name
== "title":
308 elif property.name
== "is-read":
309 old_value
= bool(self
.is_read
)
311 if old_value
!= value
:
312 self
.emit("is-read-changed", int(value
))
314 raise AttributeError, "unknown property %s" % property.name