]> git.plutz.net Git - tabnote/blobdiff - tabnote
intoduced new data format (enabelingfor syncing
[tabnote] / tabnote
diff --git a/tabnote b/tabnote
index 21de6bb58fb34306e4fc6c7541845d74efe48adc..79295deab0a21e60c75a149ed5274ef3b2f6bfbd 100755 (executable)
--- a/tabnote
+++ b/tabnote
@@ -34,6 +34,9 @@ class Persistence():
     """
     self.dbcon = dbapi2.connect(os.getenv('HOME') + os.sep + '.tabnote.sqlite')
     self.dbcur = self.dbcon.cursor()
+    self.initDB()
+
+  def initDB(self):
     initnote = ('Welcome to TabNote!\n\n' +
                 'Click the "+"-Tab to open a new note.\n' +
                 'Double Click on the selected tab to relabel or delete it.\n' +
@@ -44,13 +47,17 @@ class Persistence():
                'Ctrl Return - bring up relabel/delete dialog\n' +
                'Ctrl t      - create new tab\n' +
                'Ctrl q      - save and quit')
+    initnote = self.escape(initnote)
+
     try:
-      self.dbcur.execute('CREATE TABLE settings (key TEXT, value TEXT, UNIQUE (key))')
-      self.dbcur.execute('INSERT INTO settings (key, value) VALUES ("version", "1.0");')
+      self.dbcur.execute('CREATE TABLE settings (key TEXT, value TEXT, UNIQUE (key));')
+      self.dbcur.execute('INSERT INTO settings (key, value) VALUES ("version", "1.6");')
       self.dbcur.execute('INSERT INTO settings (key, value) VALUES ("geometry", "420x300");')
-      self.dbcur.execute('CREATE TABLE notes (id INTEGER PRIMARY KEY, label TEXT, content TEXT);')
-      self.dbcur.execute('INSERT INTO notes (label, content) VALUES ("Introduction", "' +
-                        self.escape(initnote) + '");')
+      self.dbcur.execute('CREATE TABLE notes (uuid TEXT, seq INTEGER PRIMARY KEY, label TEXT, ' +
+                  'content TEXT, revision DATETIME, lastsync DATETIME, UNIQUE (uuid));')
+      self.dbcur.execute('INSERT INTO notes (uuid, label, content, revision, lastsync) VALUES ' +
+                  '(lower(hex(randomblob(16))), "Intro", "' + initnote +
+                  '", datetime(\'now\'), datetime(\'1970-01-01\'));')
     except: pass
 
   def getSetting(self, key):
@@ -81,33 +88,36 @@ class Persistence():
     Return list of all recorded notes
     Each item is an array of the format [id, label, textcontent]
     """
-    self.dbcur.execute('SELECT id, label, content FROM notes ORDER BY id;')
+    self.dbcur.execute('SELECT uuid, label, content FROM notes ORDER BY seq;')
     return self.dbcur.fetchall()
 
-  def setRecord(self, rid, content = None, label = None):
-    """
-    Alter an existing record
-    """
-    if content:
-      self.dbcur.execute('UPDATE notes SET content = "' + self.escape(content) +
-                        '" WHERE id = ' + str(rid) + ';')
-    if label:
-      self.dbcur.execute('UPDATE notes SET label = "' + self.escape(label) +
-                        '" WHERE id = ' + str(rid) + ';')
-
-  def addRecord(self, label = '', content = ''):
-    """
-    Create a new record, return the new record id
-    """
-    self.dbcur.execute('INSERT INTO notes (label, content) VALUES ("' + self.escape(label) +
-                      '", "' + self.escape(content) + '");')
+  def setRecord(self, uuid = None, label = None, content = None):
+    """
+    Create or alter a record
+    """
+    if not uuid:
+      if label: label = self.escape(label)
+      else: label = ''
+      if content: content = self.escape(content)
+      else: content = ''
+      self.dbcur.execute('INSERT INTO notes (uuid, label, content, revision, lastsync) VALUES ' +
+                        '(lower(hex(randomblob(16))), "' + label + '", "' + content +
+                        '", datetime(\'now\'), datetime(\'1970-01-01\'));')
+    else:    
+      if content:
+        self.dbcur.execute('UPDATE notes SET content = "' + self.escape(content) +
+                          '", revision = datetime(\'now\') WHERE uuid = "' + str(uuid) +
+                          '" AND NOT content = "' + self.escape(content) + '";')
+      if label:
+        self.dbcur.execute('UPDATE notes SET label = "' + self.escape(label) +
+                          '", revision = datetime(\'now\') WHERE uuid = "' + str(uuid) + '";')
     return self.dbcur.lastrowid
 
-  def delRecord(self, rid):
+  def delRecord(self, uuid):
     """
     Delete record by ID
     """
-    self.dbcur.execute('DELETE FROM notes WHERE id = ' + str(rid) + ';')
+    self.dbcur.execute('DELETE FROM notes WHERE uuid = "' + str(uuid) + '";')
 
   def close(self):
     """
@@ -255,7 +265,7 @@ class Main(Tk):
     """
     for tab in self.contents:
       if tab[1] == self.tablist.page(name):
-       self.storage.setRecord(tab[0], content=tab[2].getvalue()[:-1])
+       self.storage.setRecord(tab[0], content = tab[2].getvalue()[:-1])
 
   def addTab(self, rid, label = 'New', content = ""):
     """
@@ -275,7 +285,7 @@ class Main(Tk):
     If the + tab is selected, this function creates a new record and tab
     """
     if name == '+':
-      self.addTab(self.storage.addRecord('New', ''), 'New', '');
+      self.addTab(self.storage.setRecord(None, 'New', ''), 'New', '');
       self.tablist.previouspage();
     else:
       for tab in self.contents: