CheckTables() public static method

public static CheckTables ( bool defaultDBLocation ) : bool
defaultDBLocation bool
return bool
Esempio n. 1
0
    protected void sqliteThings()
    {
        configChronojump = new Config();
        configChronojump.Read();

        bool badExit = checkIfChronojumpExitAbnormally();

        if (badExit)
        {
            if (chronojumpIsExecutingNTimes())
            {
                messageToShowOnBoot += Catalog.GetString("Chronojump is already running") + "\n\n" +
                                       Catalog.GetString("Chronojump will exit now.");

                chronojumpHasToExit = true;
                quitNowCjTwoTimes   = true;
                LogB.Error("Chronojump is already running.");

                return;
            }
            else
            {
                chronojumpCrashedBefore();
            }
        }

        //print version of chronojump
        progVersion = BuildInfo.chronojumpVersion;

        LogB.Information("Chronojump version: {0}", progVersion);

        //to store user videos and photos
        Util.CreateMultimediaDirsIfNeeded();

        //to store (encoder, force sensor, run encoder) data and graphs
        UtilEncoder.CreateEncoderDirIfNeeded();
        Util.CreateForceSensorDirIfNeeded();
        Util.CreateRaceAnalyzerDirIfNeeded();

//TODO: when a session is deleted, encoder data has to be deleted, also multimedia videos, I suppose. Show message to user warning about it
//TODO: encoder weight auto written depending on person loaded, and changes if it changes person or weight


        //move database to new location if chronojump version is before 0.7
        moveDatabaseToNewLocationIfNeeded();

        LogB.Information("move? ended");

        splashMessageChange(1);          //checking database

        /*
         * splashMessage = "pre-connect";
         * needUpdateSplashMessage = true;
         * Console.ReadLine();
         */

        Sqlite.CreateDir();
        bool defaultDBLocation = Sqlite.Connect();

        LogB.SQL("sqlite connected");

        /*
         * splashMessage = "post-connect" + defaultDBLocation.ToString();
         * needUpdateSplashMessage = true;
         * Console.ReadLine();
         */

        //Chech if the DB file exists
        if (!Sqlite.CheckTables(defaultDBLocation))
        {
            LogB.SQL(Catalog.GetString("no tables, creating ..."));

            creatingDB = true;
            splashMessageChange(2);              //creating database



            /*
             * splashMessage = "pre-create";
             * needUpdateSplashMessage = true;
             * Console.ReadLine();
             */



            Sqlite.CreateDir();
            Sqlite.CreateFile();
            //Sqlite.CreateFile(defaultDBLocation);



            /*
             * splashMessage = "post-create";
             * needUpdateSplashMessage = true;
             * Console.ReadLine();
             */



            createRunningFileName(runningFileName);
            Sqlite.CreateTables(false);             //not server
            creatingDB = false;
        }
        else
        {
            LogB.SQL("doing backup");
            //backup the database
            Util.BackupDirCreateIfNeeded();

            splashMessageChange(3);              //making db backup

            Util.BackupDatabase();
            LogB.SQL("made a database backup");              //not compressed yet, it seems System.IO.Compression.DeflateStream and
            //System.IO.Compression.GZipStream are not in mono


            if (!Sqlite.IsSqlite3())
            {
                bool ok = Sqlite.ConvertFromSqlite2To3();
                if (!ok)
                {
                    LogB.Error("problem with sqlite");
                    //check (spanish)
                    //http://mail.gnome.org/archives/chronojump-devel-list/2008-March/msg00011.html
                    string errorMessage = Catalog.GetString("Failed database conversion, ensure you have libsqlite3-0 installed. \nIf problems persist ask in chronojump-list");
                    errorMessage += "\n\n" + string.Format(Catalog.GetString("If you have no data on your database (you just installed Chronojump), you can fix this problem deleting this file: {0}"),
                                                           Util.GetDatabaseDir() + Path.DirectorySeparatorChar + "chronojump.db") +
                                    "\n" + Catalog.GetString("And starting Chronojump again.");
                    LogB.Error(errorMessage);
                    messageToShowOnBoot += errorMessage;
                    chronojumpHasToExit  = true;
                    return;
                }
                Sqlite.Connect();
            }

            splashMessageChange(4);              //updating DB
            updatingDB = true;

            if (Sqlite.ChangeDjToDJna())
            {
                messageToShowOnBoot += Catalog.GetString("All DJ jumps have been renamed as 'DJna' (Drop Jumps with No Arms).") + "\n\n" +
                                       Catalog.GetString("If your Drop Jumps were executed using the arms, please rename them manually as 'DJa'.") + "\n";
            }

            bool softwareIsNew = Sqlite.ConvertToLastChronojumpDBVersion();
            updatingDB = false;


            if (!softwareIsNew)
            {
                //Console.Clear();
                string errorMessage = string.Format(Catalog.GetString("Sorry, this Chronojump version ({0}) is too old for your database."), progVersion) + "\n" +
                                      Catalog.GetString("Please update Chronojump") + ":\n";
                errorMessage += "http://chronojump.org";
                //errorMessage += "\n\n" + Catalog.GetString("Press any key");
                LogB.Error(errorMessage);
                messageToShowOnBoot += errorMessage;
                chronojumpHasToExit  = true;
            }

            LogB.Information(Catalog.GetString("tables already created"));


            //check for bad Rjs (activate if program crashes and you use it in the same db before v.0.41)
            //SqliteJump.FindBadRjs();

            createRunningFileName(runningFileName);
        }


        //splashMessageChange(5);  //check for new version
        splashMessageChange(5);          //connecting to server

        messageToShowOnBoot += recuperateBrokenEvents();

        //connect to server to Ping
        versionAvailable = "";
        versionAvailable = Constants.ServerOffline;


        //doing ping using json methods

        /*
         * temporarily disabled on start
         * string machineID = SqlitePreferences.Select("machineID", false);
         * Json js = new Json();
         * bool success = js.Ping(UtilAll.GetOS(), progVersion, machineID);
         * if(success)
         *      LogB.Information(js.ResultMessage);
         * else
         *      LogB.Error(js.ResultMessage);
         */

        allSQLCallsDoneOnSqliteThingsThread = false;

        //wait until pinging ends (or it's cancelled)
        //while(! pingEnd) {
        //}

        Sqlite.Open();

//TODO: fix this to the new code

        string versionAvailableKnown = SqlitePreferences.Select("versionAvailable", true);

        if (versionAvailable != Constants.ServerOffline && new Version(versionAvailable) > new Version(progVersion))
        {
            //check if available version is higher than known available version
            Version versionAvailableAsV = new Version(versionAvailable);

            Version versionAvailableKnownAsV;
            bool    updateKnownVersion = false;
            if (versionAvailableKnown == "")
            {
                updateKnownVersion = true;
            }
            else
            {
                versionAvailableKnownAsV = new Version(versionAvailableKnown);
                if (versionAvailableAsV > versionAvailableKnownAsV)
                {
                    updateKnownVersion = true;
                }
            }

            if (updateKnownVersion)
            {
                //is the first time we know about this new version
                //just write on db and show message to user
                SqlitePreferences.Update(Constants.PrefVersionAvailable, versionAvailable, true);
                versionAvailableKnown = versionAvailable;
                messageToShowOnBoot  += string.Format(Catalog.GetString(
                                                          "\nNew Chronojump version available on website.\nYour Chronojump version is: {1}"),
                                                      versionAvailable, progVersion) + "\n\n" +
                                        Catalog.GetString("Please, update to new version.") + "\n";
            }
        }


        //if chronojump chrashed before
        if (badExit)
        {
            if (versionAvailableKnown.Length > 0 && new Version(versionAvailableKnown) > new Version(progVersion))
            {
                messageToShowOnBoot += "\n" + Catalog.GetString("Chronojump crashed before.") + "\n" +
                                       Catalog.GetString("Please, update to new version: ") + versionAvailableKnown + "\n";
            }
            else
            {
                messageToShowOnBoot += messageCrashedBefore;
                //SqlitePreferences.Update("videoOn", "False", true);
            }
        }


        splashMessageChange(6);          //preparing main window


        //start as "simulated"
        SqlitePreferences.Update("simulated", "True", true);         //dbcon opened

        Sqlite.Close();

        // Chronojump sqlite is in an initialized state, let's keep the Sqlite state here
        // to be re-used
        Sqlite.saveClassState();

        allSQLCallsDoneOnSqliteThingsThread = true;
        LogB.SQL("all SQL calls done on sqliteThings thread");

        UtilAll.IsWindows();            //only as additional info here

        //Application.Init();

        needEndSplashWin = true;
    }