import sys, os, datetime, shutil, time
from PyQt4 import QtGui
from ui.main import MainWindow
from lib.misc import Application
from lib.services.service import UserService
from lib.models.model import User
from server.server import Server

def checkUser():
    userService = UserService()
    users = userService.findAll(1)

    if len(users) == 0:
        user = User()
        user.username = "New Profile"
        user = userService.save(user)
        Application.user = user
        userService.loginUser(user.userId)
    else:
        Application.user = users[0]
        userService.loginUser(users[0].userId)
        
def backupDb(stage):
    if not os.path.exists(Application.connectionString):
        return
    
    path = os.path.join(Application.pathDatabase, "backup")
    
    if not os.path.exists(path):
        os.mkdir(path)
        
    counter = 1
    backupFile = "{0}-{1}-{2}.sqlite".format(datetime.datetime.now().strftime("%Y-%m-%d"), stage, counter)
    
    while os.path.exists(os.path.join(path, backupFile)):
        counter += 1
        backupFile = "{0}-{1}-{2}.sqlite".format(datetime.datetime.now().strftime("%Y-%m-%d"), stage, counter)
        
    shutil.copy(Application.connectionString, os.path.join(path, backupFile))
    
    files = [file for file in os.listdir(path) if os.path.isfile(os.path.join(path,file))]
    
    if len(files)>15:
        d = []
        for file in files:
            d.append([file, os.path.getmtime(os.path.join(path,file))])
            
        d = sorted(d, key=lambda tup:tup[1])
        
        difference = len(files)-15
        
        if difference>0:
            for i in range(0, difference):
                os.remove(os.path.join(path, d[i][0]))
          
def cleanOldFiles():
    path = Application.pathOutput
    files = [file for file in os.listdir(path) if os.path.isfile(os.path.join(path,file))]
    
    for file in files:
        (name, ext) = os.path.splitext(file)
        
        if not ext.lower()==".html" and not ext.lower()==".xml":
            continue
        
        now = time.time() 
        created = os.path.getmtime(os.path.join(path,file))
        
        if now-created>60*60*24*7:
            os.remove(os.path.join(path, file))
        
if __name__=="__main__":
    cleanOldFiles()
    checkUser()
    backupDb("start")
    
    Application.server = Server(embed=False)
    app = QtGui.QApplication(sys.argv)
    myapp = MainWindow()
    myapp.show()
    Application.server.start()
    ret = app.exec_()
    Application.server.stop()
    backupDb("stop")
    sys.exit(ret)

@@ -9,34 +9,15 @@
 ys, 
-os, datetime, shutil, time
+logging
 %0Afro
@@ -110,36 +110,29 @@
 rom 
-lib.services
+server
 .serv
-ic
 e
+r
  import 
 User
@@ -127,27 +127,22 @@
  import 
-User
 Serv
-ic
 e
+r
 %0Afrom li
@@ -147,1570 +147,181 @@
 lib.
-models.model import User%0Afrom server.server import Server%0A%0Adef checkUser():%0A    userService = UserService()%0A    users = userService.findAll(1)%0A%0A    if len(users) == 0:%0A        user = User()%0A        user.username = %22New Profile%22%0A        user = userService.save(user)%0A        Application.user = user%0A        userService.loginUser(user.userId)%0A    else:%0A        Application.user = users%5B0%5D%0A        userService.loginUser(users%5B0%5D.userId)%0A        %0Adef backupDb(stage):%0A    if not os.path.exists(Application.connectionString):%0A        return%0A    %0A    path = os.path.join(Application.pathDatabase, %22backup%22)%0A    %0A    if not os.path.exists(path):%0A        os.mkdir(path)%0A        %0A    counter = 1%0A    backupFile = %22%7B0%7D-%7B1%7D-%7B2%7D.sqlite%22.format(datetime.datetime.now().strftime(%22%25Y-%25m-%25d%22), stage, counter)%0A    %0A    while os.path.exists(os.path.join(path, backupFile)):%0A        counter += 1%0A        backupFile = %22%7B0%7D-%7B1%7D-%7B2%7D.sqlite%22.format(datetime.datetime.now().strftime(%22%25Y-%25m-%25d%22), stage, counter)%0A        %0A    shutil.copy(Application.connectionString, os.path.join(path, backupFile))%0A    %0A    files = %5Bfile for file in os.listdir(path) if os.path.isfile(os.path.join(path,file))%5D%0A    %0A    if len(files)%3E15:%0A        d = %5B%5D%0A        for file in files:%0A            d.append(%5Bfile, os.path.getmtime(os.path.join(path,file))%5D)%0A            %0A        d = sorted(d, key=lambda tup:tup%5B1%5D)%0A        %0A        difference = len(files)-15%0A        %0A        if difference%3E0:%0A            for i in range(0, difference):%0A                os.remove(os.path.join(path, d%5Bi%5D%5B0%5D))%0A          %0Adef 
+tasks import Startup%0A%0Alogging.basicConfig(level=logging.DEBUG, format=%22%25(asctime)s - %25(levelname)s - %25(message)s%22)%0A%0Aif __name__==%22__main__%22:%0A    start = Startup()%0A    start.
 clea
@@ -335,583 +335,94 @@
 es()
-:
 %0A    
-path = Application.pathOutput%0A    files = %5Bfile for file in os.listdir(path) if os.path.isfile(os.path.join(path,file))%5D%0A    %0A    for file in files:%0A        (name, ext) = os.path.splitext(file)%0A        %0A        if not ext.lower()==%22.html%22 and not ext.lower()==%22.xml%22:%0A            continue%0A        %0A        now = time.time() %0A        created = os.path.getmtime(os.path.join(path,file))%0A        %0A        if now-created%3E60*60*24*7:%0A            os.remove(os.path.join(path, file))%0A        %0Aif __name__==%22__main__%22:%0A    cleanOldFiles()%0A    checkUser()%0A    backupDb(%22start%22)%0A
+start.checkUser()%0A    start.backupDb(%22start%22)%0A    start.checkDbForUpgrade()%0A     
     
@@ -464,12 +464,11 @@
 bed=
-Fals
+Tru
 e)%0A 
@@ -543,24 +543,32 @@
 yapp.show()%0A
+       %0A
     Applicat
@@ -642,16 +642,22 @@
 p()%0A    
+start.
 backupDb

Correct main