def write_db_node(self, db, tree_iter, sequence, node_father_id, write_dict, exporting="", sel_range=None): """Write a node in DB""" node_id = self.dad.treestore[tree_iter][3] print "write node content, node_id", node_id, ", write_dict", write_dict name = self.dad.treestore[tree_iter][1].decode(cons.STR_UTF8) syntax = self.dad.treestore[tree_iter][4] tags = self.dad.treestore[tree_iter][6].decode(cons.STR_UTF8) # is_ro (bitfield) is_ro = self.dad.treestore[tree_iter][9] << 1 if self.dad.treestore[tree_iter][7]: is_ro |= 0x01 # is_richtxt (bitfield) is_richtxt = 0x01 if syntax == cons.RICH_TEXT_ID else 0x00 if support.get_pango_is_bold(self.dad.treestore[tree_iter][10]): is_richtxt |= 0x02 foreground = self.dad.treestore[tree_iter][11] if foreground: is_richtxt |= 0x04 is_richtxt |= exports.rgb_int24bit_from_str(foreground[1:]) << 3 ts_creation = self.dad.treestore[tree_iter][12] ts_lastsave = self.dad.treestore[tree_iter][13] if not self.db_tables_check_done: self.db_tables_execute_check(db) self.db_tables_check_done = True if write_dict['buff']: if not self.dad.treestore[tree_iter][2]: # we are using db storage and the buffer was not created yet if exporting: self.read_db_node_content(tree_iter, self.dad.db) else: self.read_db_node_content(tree_iter, self.dad.db_old) curr_buffer = self.dad.treestore[tree_iter][2] if not sel_range: start_iter = curr_buffer.get_start_iter() end_iter = curr_buffer.get_end_iter() else: start_iter = curr_buffer.get_iter_at_offset(sel_range[0]) end_iter = curr_buffer.get_iter_at_offset(sel_range[1]) if (is_richtxt & 0x01): # prepare xml dom node if "dom" in dir(self): del self.dom self.dom = xml.dom.minidom.Document() dom_iter = self.dom.createElement("node") self.dom.appendChild(dom_iter) # init attributes self.curr_attributes = {} for tag_property in cons.TAG_PROPERTIES: self.curr_attributes[tag_property] = "" # go! curr_iter = start_iter.copy() self.dad.xml_handler.rich_text_attributes_update(curr_iter, self.curr_attributes) tag_found = curr_iter.forward_to_tag_toggle(None) while tag_found: self.dad.xml_handler.rich_txt_serialize(dom_iter, start_iter, curr_iter, self.curr_attributes, dom=self.dom) if curr_iter.compare(end_iter) >= 0: break else: self.dad.xml_handler.rich_text_attributes_update(curr_iter, self.curr_attributes) offset_old = curr_iter.get_offset() start_iter.set_offset(offset_old) tag_found = curr_iter.forward_to_tag_toggle(None) if curr_iter.get_offset() == offset_old: break else: self.dad.xml_handler.rich_txt_serialize(dom_iter, start_iter, curr_iter, self.curr_attributes, dom=self.dom) # time for the objects if write_dict['upd']: db.execute('DELETE FROM codebox WHERE node_id=?', (node_id,)) db.execute('DELETE FROM grid WHERE node_id=?', (node_id,)) db.execute('DELETE FROM image WHERE node_id=?', (node_id,)) pixbuf_table_codebox_vector = self.dad.state_machine.get_embedded_pixbufs_tables_codeboxes(curr_buffer, sel_range=sel_range) # pixbuf_table_codebox_vector is [ [ "pixbuf"/"table"/"codebox", [offset, pixbuf, alignment] ],... ] codeboxes_tuples = [] tables_tuples = [] images_tuples = [] for element in pixbuf_table_codebox_vector: if sel_range: element[1][0] -= sel_range[0] if element[0] == "pixbuf": images_tuples.append(self.get_image_db_tuple(element[1], node_id)) elif element[0] == "table": tables_tuples.append(self.get_table_db_tuple(element[1], node_id)) elif element[0] == "codebox": codeboxes_tuples.append(self.get_codebox_db_tuple(element[1], node_id)) if codeboxes_tuples: has_codebox = 1 db.executemany('INSERT INTO codebox VALUES(?,?,?,?,?,?,?,?,?,?)', codeboxes_tuples) else: has_codebox = 0 if tables_tuples: has_table = 1 db.executemany('INSERT INTO grid VALUES(?,?,?,?,?,?)', tables_tuples) else: has_table = 0 if images_tuples: has_image = 1 db.executemany('INSERT INTO image VALUES(?,?,?,?,?,?,?,?)', images_tuples) else: has_image = 0 # retrieve xml text txt = (self.dom.toxml()).decode(cons.STR_UTF8) else: # not richtext has_codebox = 0 has_table = 0 has_image = 0 txt = (start_iter.get_text(end_iter)).decode(cons.STR_UTF8) if write_dict['prop'] and write_dict['buff']: if write_dict['upd']: db.execute('DELETE FROM node WHERE node_id=?', (node_id,)) node_tuple = (node_id, name, txt, syntax, tags, is_ro, is_richtxt, has_codebox, has_table, has_image, 0, ts_creation, ts_lastsave) # TODO remove the column 'level' db.execute('INSERT INTO node VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)', node_tuple) elif write_dict['buff']: db.execute('UPDATE node SET txt=?, syntax=?, is_richtxt=?, has_codebox=?, has_table=?, has_image=?, ts_lastsave=? WHERE node_id=?', (txt, syntax, is_richtxt, has_codebox, has_table, has_image, ts_lastsave, node_id)) elif write_dict['prop']: db.execute('UPDATE node SET name=?, syntax=?, tags=?, is_ro=?, is_richtxt=? WHERE node_id=?', (name, syntax, tags, is_ro, is_richtxt, node_id)) if write_dict['hier']: if write_dict['upd']: db.execute('DELETE FROM children WHERE node_id=?', (node_id,)) db.execute('INSERT INTO children VALUES(?,?,?)', (node_id, node_father_id, sequence)) if not write_dict['child']: return # let's take care about the children child_tree_iter = self.dad.treestore.iter_children(tree_iter) child_sequence = 0 while child_tree_iter != None: child_sequence += 1 self.write_db_node(db, child_tree_iter, child_sequence, node_id, write_dict, exporting) child_tree_iter = self.dad.treestore.iter_next(child_tree_iter)
def write_db_node(self, db, tree_iter, level, sequence, node_father_id, write_dict, exporting="", sel_range=None): """Write a node in DB""" node_id = self.dad.treestore[tree_iter][3] print "write node content, node_id", node_id, ", write_dict", write_dict name = self.dad.treestore[tree_iter][1].decode(cons.STR_UTF8) syntax = self.dad.treestore[tree_iter][4] tags = self.dad.treestore[tree_iter][6].decode(cons.STR_UTF8) # is_ro (bitfield) is_ro = self.dad.treestore[tree_iter][9] << 1 if self.dad.treestore[tree_iter][7]: is_ro |= 0x01 # is_richtxt (bitfield) is_richtxt = 0x01 if syntax == cons.RICH_TEXT_ID else 0x00 if support.get_pango_is_bold(self.dad.treestore[tree_iter][10]): is_richtxt |= 0x02 foreground = self.dad.treestore[tree_iter][11] if foreground: is_richtxt |= 0x04 is_richtxt |= exports.rgb_int24bit_from_str(foreground[1:]) << 3 if write_dict['buff']: if not self.dad.treestore[tree_iter][2]: # we are using db storage and the buffer was not created yet if exporting: self.read_db_node_content(tree_iter, self.dad.db) else: self.read_db_node_content(tree_iter, self.dad.db_old) curr_buffer = self.dad.treestore[tree_iter][2] if not sel_range: start_iter = curr_buffer.get_start_iter() end_iter = curr_buffer.get_end_iter() else: start_iter = curr_buffer.get_iter_at_offset(sel_range[0]) end_iter = curr_buffer.get_iter_at_offset(sel_range[1]) if (is_richtxt & 0x01): # prepare xml dom node if "dom" in dir(self): del self.dom self.dom = xml.dom.minidom.Document() dom_iter = self.dom.createElement("node") self.dom.appendChild(dom_iter) # init attributes self.curr_attributes = {} for tag_property in cons.TAG_PROPERTIES: self.curr_attributes[tag_property] = "" # go! curr_iter = start_iter.copy() self.dad.xml_handler.rich_text_attributes_update(curr_iter, self.curr_attributes) tag_found = curr_iter.forward_to_tag_toggle(None) while tag_found: self.dad.xml_handler.rich_txt_serialize(dom_iter, start_iter, curr_iter, self.curr_attributes, dom=self.dom) if curr_iter.compare(end_iter) >= 0: break else: self.dad.xml_handler.rich_text_attributes_update(curr_iter, self.curr_attributes) offset_old = curr_iter.get_offset() start_iter.set_offset(offset_old) tag_found = curr_iter.forward_to_tag_toggle(None) if curr_iter.get_offset() == offset_old: break else: self.dad.xml_handler.rich_txt_serialize(dom_iter, start_iter, curr_iter, self.curr_attributes, dom=self.dom) # time for the objects if write_dict['upd']: db.execute('DELETE FROM codebox WHERE node_id=?', (node_id,)) db.execute('DELETE FROM grid WHERE node_id=?', (node_id,)) db.execute('DELETE FROM image WHERE node_id=?', (node_id,)) pixbuf_table_codebox_vector = self.dad.state_machine.get_embedded_pixbufs_tables_codeboxes(curr_buffer, sel_range=sel_range) # pixbuf_table_codebox_vector is [ [ "pixbuf"/"table"/"codebox", [offset, pixbuf, alignment] ],... ] codeboxes_tuples = [] tables_tuples = [] images_tuples = [] for element in pixbuf_table_codebox_vector: if sel_range: element[1][0] -= sel_range[0] if element[0] == "pixbuf": images_tuples.append(self.get_image_db_tuple(element[1], node_id)) elif element[0] == "table": tables_tuples.append(self.get_table_db_tuple(element[1], node_id)) elif element[0] == "codebox": codeboxes_tuples.append(self.get_codebox_db_tuple(element[1], node_id)) if codeboxes_tuples: has_codebox = 1 db.executemany('INSERT INTO codebox VALUES(?,?,?,?,?,?,?,?,?,?)', codeboxes_tuples) else: has_codebox = 0 if tables_tuples: has_table = 1 db.executemany('INSERT INTO grid VALUES(?,?,?,?,?,?)', tables_tuples) else: has_table = 0 if images_tuples: has_image = 1 curr_num_cols = len(db.execute('PRAGMA table_info(image)').fetchall()) if curr_num_cols == 5: db.execute('ALTER TABLE image ADD COLUMN filename TEXT') db.execute('ALTER TABLE image ADD COLUMN link TEXT') db.execute('ALTER TABLE image ADD COLUMN time INTEGER') elif curr_num_cols == 7: db.execute('ALTER TABLE image ADD COLUMN time INTEGER') db.executemany('INSERT INTO image VALUES(?,?,?,?,?,?,?,?)', images_tuples) else: has_image = 0 # retrieve xml text txt = (self.dom.toxml()).decode(cons.STR_UTF8) else: # not richtext has_codebox = 0 has_table = 0 has_image = 0 txt = (start_iter.get_text(end_iter)).decode(cons.STR_UTF8) if write_dict['prop'] and write_dict['buff']: if write_dict['upd']: db.execute('DELETE FROM node WHERE node_id=?', (node_id,)) node_tuple = (node_id, name, txt, syntax, tags, is_ro, is_richtxt, has_codebox, has_table, has_image, level) db.execute('INSERT INTO node VALUES(?,?,?,?,?,?,?,?,?,?,?)', node_tuple) elif write_dict['buff']: db.execute('UPDATE node SET txt=?, syntax=?, is_richtxt=?, has_codebox=?, has_table=?, has_image=?, level=? WHERE node_id=?', (txt, syntax, is_richtxt, has_codebox, has_table, has_image, level, node_id)) elif write_dict['prop']: db.execute('UPDATE node SET name=?, syntax=?, tags=?, is_ro=?, is_richtxt=?, level=? WHERE node_id=?', (name, syntax, tags, is_ro, is_richtxt, level, node_id)) if write_dict['hier']: if write_dict['upd']: db.execute('DELETE FROM children WHERE node_id=?', (node_id,)) db.execute('INSERT INTO children VALUES(?,?,?)', (node_id, node_father_id, sequence)) # need to write "level" if not write_dict['buff'] and not write_dict['prop']: db.execute('UPDATE node SET level=? WHERE node_id=?', (level, node_id)) if not write_dict['child']: return # let's take care about the children child_tree_iter = self.dad.treestore.iter_children(tree_iter) child_sequence = 0 while child_tree_iter != None: child_sequence += 1 self.write_db_node(db, child_tree_iter, level+1, child_sequence, node_id, write_dict, exporting) child_tree_iter = self.dad.treestore.iter_next(child_tree_iter)