2016-03-12 11:18:00 +00:00
|
|
|
# coding=utf-8
|
2016-03-12 17:37:54 +00:00
|
|
|
from sqlite3 import connect
|
2016-04-03 20:51:46 +00:00
|
|
|
import settings
|
2016-03-12 17:37:54 +00:00
|
|
|
from os import chdir
|
2016-05-15 20:02:05 +00:00
|
|
|
import os.path
|
|
|
|
from toxencryptsave import LibToxEncryptSave
|
2016-03-12 11:18:00 +00:00
|
|
|
|
|
|
|
|
2016-03-29 14:11:30 +00:00
|
|
|
PAGE_SIZE = 42
|
|
|
|
|
2016-03-12 15:25:16 +00:00
|
|
|
MESSAGE_OWNER = {
|
|
|
|
'ME': 0,
|
2016-06-04 19:17:32 +00:00
|
|
|
'FRIEND': 1,
|
|
|
|
'NOT_SENT': 2
|
2016-03-12 15:25:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-12 11:18:00 +00:00
|
|
|
class History(object):
|
2016-04-03 20:51:46 +00:00
|
|
|
|
2016-03-12 11:18:00 +00:00
|
|
|
def __init__(self, name):
|
|
|
|
self._name = name
|
2016-04-03 20:51:46 +00:00
|
|
|
chdir(settings.ProfileHelper.get_path())
|
2016-05-15 20:02:05 +00:00
|
|
|
path = settings.ProfileHelper.get_path() + self._name + '.hstr'
|
|
|
|
if os.path.exists(path):
|
|
|
|
decr = LibToxEncryptSave.get_instance()
|
2016-06-05 11:59:36 +00:00
|
|
|
try:
|
|
|
|
with open(path, 'rb') as fin:
|
|
|
|
data = fin.read()
|
|
|
|
if decr.is_data_encrypted(data):
|
|
|
|
data = decr.pass_decrypt(data)
|
|
|
|
with open(path, 'wb') as fout:
|
|
|
|
fout.write(data)
|
|
|
|
except:
|
|
|
|
os.remove(path)
|
2016-03-12 17:37:54 +00:00
|
|
|
db = connect(name + '.hstr')
|
2016-03-12 11:18:00 +00:00
|
|
|
cursor = db.cursor()
|
|
|
|
cursor.execute('CREATE TABLE IF NOT EXISTS friends('
|
|
|
|
' tox_id TEXT PRIMARY KEY'
|
|
|
|
')')
|
|
|
|
db.close()
|
|
|
|
|
2016-05-15 20:02:05 +00:00
|
|
|
def save(self):
|
|
|
|
encr = LibToxEncryptSave.get_instance()
|
|
|
|
if encr.has_password():
|
|
|
|
path = settings.ProfileHelper.get_path() + self._name + '.hstr'
|
|
|
|
with open(path, 'rb') as fin:
|
|
|
|
data = fin.read()
|
|
|
|
data = encr.pass_encrypt(data)
|
|
|
|
with open(path, 'wb') as fout:
|
|
|
|
fout.write(data)
|
|
|
|
|
2016-03-15 20:35:15 +00:00
|
|
|
def export(self, directory):
|
2016-04-03 20:51:46 +00:00
|
|
|
path = settings.ProfileHelper.get_path() + self._name + '.hstr'
|
2016-03-15 20:35:15 +00:00
|
|
|
new_path = directory + self._name + '.hstr'
|
|
|
|
with open(path, 'rb') as fin:
|
|
|
|
data = fin.read()
|
|
|
|
with open(new_path, 'wb') as fout:
|
|
|
|
fout.write(data)
|
|
|
|
|
2016-03-12 11:18:00 +00:00
|
|
|
def add_friend_to_db(self, tox_id):
|
2016-04-03 20:51:46 +00:00
|
|
|
chdir(settings.ProfileHelper.get_path())
|
2016-03-12 17:37:54 +00:00
|
|
|
db = connect(self._name + '.hstr')
|
2016-03-12 11:18:00 +00:00
|
|
|
try:
|
|
|
|
cursor = db.cursor()
|
|
|
|
cursor.execute('INSERT INTO friends VALUES (?);', (tox_id, ))
|
|
|
|
cursor.execute('CREATE TABLE id' + tox_id + '('
|
|
|
|
' id INTEGER PRIMARY KEY,'
|
|
|
|
' message TEXT,'
|
|
|
|
' owner INTEGER,'
|
2016-03-25 13:24:38 +00:00
|
|
|
' unix_time REAL,'
|
2016-03-12 11:18:00 +00:00
|
|
|
' message_type INTEGER'
|
|
|
|
')')
|
|
|
|
db.commit()
|
2016-03-12 16:22:33 +00:00
|
|
|
except:
|
2016-03-12 11:18:00 +00:00
|
|
|
db.rollback()
|
2016-03-12 16:22:33 +00:00
|
|
|
raise
|
2016-03-12 11:18:00 +00:00
|
|
|
finally:
|
|
|
|
db.close()
|
|
|
|
|
|
|
|
def delete_friend_from_db(self, tox_id):
|
2016-04-03 20:51:46 +00:00
|
|
|
chdir(settings.ProfileHelper.get_path())
|
2016-03-12 17:37:54 +00:00
|
|
|
db = connect(self._name + '.hstr')
|
2016-03-12 11:18:00 +00:00
|
|
|
try:
|
|
|
|
cursor = db.cursor()
|
|
|
|
cursor.execute('DELETE FROM friends WHERE tox_id=?;', (tox_id, ))
|
|
|
|
cursor.execute('DROP TABLE id' + tox_id + ';')
|
|
|
|
db.commit()
|
2016-03-12 16:22:33 +00:00
|
|
|
except:
|
2016-03-12 11:18:00 +00:00
|
|
|
db.rollback()
|
2016-03-12 16:22:33 +00:00
|
|
|
raise
|
2016-03-12 11:18:00 +00:00
|
|
|
finally:
|
|
|
|
db.close()
|
|
|
|
|
2016-03-12 16:22:33 +00:00
|
|
|
def friend_exists_in_db(self, tox_id):
|
2016-04-03 20:51:46 +00:00
|
|
|
chdir(settings.ProfileHelper.get_path())
|
2016-03-12 17:37:54 +00:00
|
|
|
db = connect(self._name + '.hstr')
|
2016-03-12 16:22:33 +00:00
|
|
|
cursor = db.cursor()
|
|
|
|
cursor.execute('SELECT 0 FROM friends WHERE tox_id=?', (tox_id, ))
|
|
|
|
result = cursor.fetchone()
|
|
|
|
db.close()
|
|
|
|
return result is not None
|
|
|
|
|
2016-03-12 11:18:00 +00:00
|
|
|
def save_messages_to_db(self, tox_id, messages_iter):
|
2016-04-03 20:51:46 +00:00
|
|
|
chdir(settings.ProfileHelper.get_path())
|
2016-03-12 17:37:54 +00:00
|
|
|
db = connect(self._name + '.hstr')
|
2016-03-12 11:18:00 +00:00
|
|
|
try:
|
|
|
|
cursor = db.cursor()
|
2016-03-12 17:37:54 +00:00
|
|
|
cursor.executemany('INSERT INTO id' + tox_id + '(message, owner, unix_time, message_type) '
|
|
|
|
'VALUES (?, ?, ?, ?);', messages_iter)
|
|
|
|
db.commit()
|
|
|
|
except:
|
|
|
|
db.rollback()
|
|
|
|
raise
|
|
|
|
finally:
|
|
|
|
db.close()
|
|
|
|
|
2016-06-13 21:32:45 +00:00
|
|
|
def update_messages(self, tox_id, unsent_time):
|
|
|
|
chdir(settings.ProfileHelper.get_path())
|
|
|
|
db = connect(self._name + '.hstr')
|
|
|
|
try:
|
|
|
|
cursor = db.cursor()
|
|
|
|
cursor.execute('UPDATE id' + tox_id + ' SET owner = 0 '
|
|
|
|
'WHERE unix_time < ' + str(unsent_time) + ' AND owner = 2;')
|
|
|
|
db.commit()
|
|
|
|
except:
|
|
|
|
db.rollback()
|
|
|
|
raise
|
|
|
|
finally:
|
|
|
|
db.close()
|
2016-06-07 09:27:58 +00:00
|
|
|
pass
|
|
|
|
|
2016-03-12 17:37:54 +00:00
|
|
|
def delete_messages(self, tox_id):
|
2016-04-03 20:51:46 +00:00
|
|
|
chdir(settings.ProfileHelper.get_path())
|
2016-03-12 17:37:54 +00:00
|
|
|
db = connect(self._name + '.hstr')
|
|
|
|
try:
|
|
|
|
cursor = db.cursor()
|
|
|
|
cursor.execute('DELETE FROM id' + tox_id + ';')
|
2016-03-12 11:18:00 +00:00
|
|
|
db.commit()
|
2016-03-12 16:22:33 +00:00
|
|
|
except:
|
2016-03-12 11:18:00 +00:00
|
|
|
db.rollback()
|
2016-03-12 16:22:33 +00:00
|
|
|
raise
|
2016-03-12 11:18:00 +00:00
|
|
|
finally:
|
|
|
|
db.close()
|
|
|
|
|
|
|
|
def messages_getter(self, tox_id):
|
2016-03-12 15:25:16 +00:00
|
|
|
return History.MessageGetter(self._name, tox_id)
|
2016-03-12 11:18:00 +00:00
|
|
|
|
2016-03-12 15:25:16 +00:00
|
|
|
class MessageGetter(object):
|
|
|
|
def __init__(self, name, tox_id):
|
2016-04-03 20:51:46 +00:00
|
|
|
chdir(settings.ProfileHelper.get_path())
|
2016-03-12 17:37:54 +00:00
|
|
|
self._db = connect(name + '.hstr')
|
2016-03-12 15:25:16 +00:00
|
|
|
self._cursor = self._db.cursor()
|
2016-03-12 17:37:54 +00:00
|
|
|
self._cursor.execute('SELECT message, owner, unix_time, message_type FROM id' + tox_id +
|
2016-03-12 15:25:16 +00:00
|
|
|
' ORDER BY unix_time DESC;')
|
2016-03-12 11:18:00 +00:00
|
|
|
|
2016-03-12 15:25:16 +00:00
|
|
|
def get_one(self):
|
|
|
|
return self._cursor.fetchone()
|
2016-03-12 11:18:00 +00:00
|
|
|
|
2016-03-12 15:25:16 +00:00
|
|
|
def get_all(self):
|
|
|
|
return self._cursor.fetchall()
|
2016-03-12 11:18:00 +00:00
|
|
|
|
2016-03-12 15:25:16 +00:00
|
|
|
def get(self, count):
|
|
|
|
return self._cursor.fetchmany(count)
|
2016-03-12 11:18:00 +00:00
|
|
|
|
2016-03-12 15:25:16 +00:00
|
|
|
def __del__(self):
|
|
|
|
self._db.close()
|