Logo Search packages:      
Sourcecode: zim version File versions  Download package

def zim::index::Index::_update_pagelist (   self,
  path,
  checkcontent 
) [private]

Checks and updates the pagelist for a path if needed and queues any
child pages for updating based on "checkcontents" and whether
the child has children itself. Called indirectly by update().

Definition at line 546 of file index.py.

00546                                                     :
            '''Checks and updates the pagelist for a path if needed and queues any
            child pages for updating based on "checkcontents" and whether
            the child has children itself. Called indirectly by update().
            '''
            #~ print '!! UPDATE LIST', path, path._indexpath
            assert isinstance(path, IndexPath)
            if not path.hasdata:
                  path = path.lookup_data(path)
            hadchildren = path.haschildren
            hadcontent = path.hascontent

            if path.isroot:
                  rawpath = Path(':')
                  hascontent = False
                  indexpath = (ROOT_ID,)
            else:
                  rawpath = self.notebook.get_page(path)
                  hascontent = rawpath.hascontent
                  indexpath = path._indexpath

            # Check if listing is uptodate

            def check_and_queue(child, rawchild):
                  # Helper function to queue individual children
                  if child.haschildren:
                        self._update_pagelist_queue.append(child)
                  elif checkcontent:
                        pagekey = self.notebook.get_page_indexkey(rawchild or child)
                        if not (pagekey and child.contentkey == pagekey):
                              self._index_page_queue.append(child)

            listkey = self.notebook.get_pagelist_indexkey(rawpath)
            uptodate = listkey and path.childrenkey == listkey

            cursor = self.db.cursor()
            cursor.execute('select * from pages where parent==?', (path.id,))

            if uptodate:
                  #~ print '!! ... is up to date'
                  for row in cursor:
                        p = IndexPath(path.name+':'+row['basename'], indexpath+(row['id'],), row)
                        check_and_queue(p, None)
            else:
                  #~ print '!! ... updating'
                  children = {}
                  for row in cursor:
                        children[row['basename']] = row
                  seen = set()
                  changes = []
                  with self.db_commit:
                        for page in self.notebook.get_pagelist(rawpath):
                              seen.add(page.basename)
                              if page.basename in children:
                                    row = children[page.basename]
                                    if page.hascontent == row['hascontent']:
                                          child = IndexPath(path.name+':'+row['basename'], indexpath+(row['id'],), row)
                                          check_and_queue(child, page)
                                    else:
                                          # Child aquired content - let's index it
                                          cursor = self.db.cursor()
                                          cursor.execute(
                                                'update pages set hascontent=?, contentkey=NULL where id==?',
                                                (page.hascontent, row['id'],) )
                                          child = IndexPath(path.name+':'+row['basename'], indexpath+(row['id'],),
                                                {     'hascontent': page.hascontent,
                                                      'haschildren': page.haschildren,
                                                      'childrenkey': row['childrenkey'],
                                                      'contentkey': None,
                                                } )
                                          changes.append((child, 2))
                                          if page.haschildren:
                                                self._update_pagelist_queue.append(child)
                                          if page.hascontent:
                                                self._index_page_queue.append(child)
                              else:
                                    # We set haschildren to False untill we have actualy seen those
                                    # children. Failing to do so will cause trouble with the
                                    # gtk.TreeModel interface to the database, which can not handle
                                    # nodes that say they have children but fail to deliver when
                                    # asked.
                                    cursor = self.db.cursor()
                                    cursor.execute(
                                          'insert into pages(basename, parent, hascontent, haschildren) values (?, ?, ?, ?)',
                                          (page.basename, path.id, page.hascontent, False))
                                    child = IndexPath(page.name, indexpath + (cursor.lastrowid,),
                                          {     'hascontent': page.hascontent,
                                                'haschildren': False,
                                                'childrenkey': None,
                                                'contentkey': None,
                                          } )
                                    changes.append((child, 1))
                                    if page.haschildren:
                                          self._update_pagelist_queue.append(child)
                                    if page.hascontent:
                                          self._index_page_queue.append(child)

                        # Figure out which pages to delete - but keep placeholders
                        keep = set()
                        delete = []
                        for basename in set(children.keys()).difference(seen):
                              row = children[basename]
                              child = IndexPath(
                                    path.name+':'+basename, indexpath+(row['id'],), row)
                              if child.haschildren or self.n_list_links(child, direction=LINK_DIR_BACKWARD) > 0:
                                    keep.add(child)
                                    self.db.execute(
                                          'update pages set hascontent=0, contentkey=NULL where id==?', (child.id,))
                                          # If you're not in the pagelist, you don't have content
                                    changes.append((child, 2))
                                    if child.haschildren:
                                          self._update_pagelist_queue.append(child)
                              else:
                                    delete.append(child)

                        # Update index key to reflect we did our updates
                        haschildren = len(seen) + len(keep) > 0
                        self.db.execute(
                              'update pages set childrenkey=?, haschildren=?, hascontent=? where id==?',
                              (listkey, haschildren, hascontent, path.id) )

                        # ... commit

                  path = self.lookup_data(path) # refresh
                  if not path.isroot and (hadchildren != path.haschildren):
                        self.emit('page-haschildren-toggled', path)

                  if not path.isroot and (hadcontent != path.hascontent):
                        self.emit('page-updated', path)

                  # All these signals should come in proper order...
                  changes.sort(key=lambda c: c[0].basename.lower())
                  for child, action in changes:
                        if action == 1:
                              self.emit('page-inserted', child)
                        else: # action == 2:
                              self.emit('page-updated', child)

                  # Clean up pages that disappeared
                  for child in delete:
                        self.emit('delete', child)

                  # ... we are followed by an cleanup_all() when indexing is done

      def delete(self, path):


Generated by  Doxygen 1.6.0   Back to index