Example #1
0
        public static void Main(string[] args)
        {
            InstallData var_data = new InstallData();

            bool isFirstInstance;

            using (Mutex mtx = new Mutex(true, "PROGRAM" + InstallData.app, out isFirstInstance))
            {
                if (!isFirstInstance)
                {
                    Application.Exit();
                    return;
                }

                if (!Directory.Exists("Pendencias"))
                {
                    Directory.CreateDirectory("Pendencias");
                }

                Translator var_translator = new Translator();

                if (!var_data.IsAppInstalled)
                {
                    var_translator.SetLanguage(Language.English.ToString());

                    MessageBox.Show(var_translator.GetMsg("ServerSystem.FailRun"), "SYSTEM");
                    return;
                }

                var_translator.SetLanguage(var_data.language);

                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);

                try
                {
                    Exchange        exchange = new Exchange();
                    ApplicationUtil var_util = new ApplicationUtil();

                    try
                    {
                        exchange.GetNewDataClient(var_data.serverMachine,
                                                  ref var_data.serverStandby,
                                                  ref var_data.serverStandByPort,
                                                  Convert.ToInt32(var_data.clientServerPort),
                                                  Convert.ToInt32(var_data.maxPacket),
                                                  var_data.language);

                        var_data.ins_registration();
                    }
                    catch (System.Exception sez)
                    {
                        if (var_data.serverStandby != "" &&
                            var_data.serverStandByPort != "")
                        {
                            try
                            {
                                exchange.GetNewDataClient(var_data.serverStandby,
                                                          Convert.ToInt32(var_data.serverStandByPort),
                                                          Convert.ToInt32(var_data.maxPacket),
                                                          var_data.language);
                            }
                            catch (System.Exception se)
                            {
                                if (se.Message == "Exit")
                                {
                                    MessageBox.Show(var_translator.GetMsg("ServerSystem.Unavailable"), "SYSTEM");
                                    var_util.offline = true;
                                }
                            }
                        }
                        else if (sez.Message == "Exit")
                        {
                            MessageBox.Show(var_translator.GetMsg("ServerSystem.Unavailable"), "SYSTEM");
                            var_util.offline = true;
                        }
                    }

                    if (var_util.offline)
                    {
                        // ###################################
                        // ## Start main program! (offline)
                        // ###################################

                        event_MainForm ev_MainForm = new event_MainForm(var_util, exchange);

                        ev_MainForm.doEvent(event_MainForm.event_Start, null);
                    }
                    else
                    {
                        string st_versionOutdated = "";

                        exchange.fetch_softwareVersion(var_data.st_version, ref st_versionOutdated);

                        if (st_versionOutdated == Context.TRUE)
                        {
                            #region  - fetch latest version -

                            string target_path = "app.txt";

                            if (File.Exists(target_path))
                            {
                                File.Delete(target_path);
                            }

                            StreamWriter sw = new StreamWriter(target_path);

                            sw.WriteLine(InstallData.app);
                            sw.Close();

                            event_dlgUpdate ev_call = new event_dlgUpdate(var_util, exchange);

                            ev_call.i_Form.ShowDialog();

                            #endregion
                        }
                        else
                        {
                            // ############################
                            // ## Start main program!
                            // ############################

                            event_MainForm ev_MainForm = new event_MainForm(var_util, exchange);

                            ev_MainForm.doEvent(event_MainForm.event_Start, null);
                        }
                    }
                }
                catch (System.Exception se)
                {
                    if (se.Message != "Exit")
                    {
                        MessageBox.Show(se.Message, "SYSTEM OUT");
                        Application.ExitThread();
                    }
                }
                finally {}
            }
        }
        public override bool doEvent(int event_number, object arg)
        {
            switch (event_number)
            {
                #region - event_Load -

            case event_Load:
            {
                //InitEventCode event_Load

                doEvent(event_Translate, null);
                doEvent(event_FormIsOpening, null);

                //EndEventCode
                return(true);
            }

                #endregion

                #region - event_Translate -

            case event_Translate:
            {
                //InitEventCode event_Translate
                //EndEventCode
                return(true);
            }

                #endregion

                #region - event_FormIsOpening -

            case event_FormIsOpening:
            {
                //InitEventCode event_FormIsOpening

                string st_csv_files = "";

                i_Form.LblCmd.Text = "Buscando ultima versão do programa de atualização...";
                i_Form.Show();

                Application.DoEvents();
                Thread.Sleep(500);

                                        #if LINUX_DIST
                //var_exchange.infra_fetchUpdaterVersion ( Context.FALSE, ref st_csv_files );
                                        #else
                var_exchange.infra_fetchUpdaterVersion(ref st_csv_files);
                                        #endif

                // pares de "nome_arquivo,indice"
                int tot = var_util.indexCSV(st_csv_files);

                int num = 1;

                for (int t = 0, dot = 0; t < tot; ++t)
                {
                    string fileName  = var_util.getCSV(t);                                      // busca nome
                    string next_part = var_util.getCSV(++t);                                    // busca indice

                    // mostrador no diálogo
                    string basicTag = "Buscando arquivo [" + num.ToString() + " de " + (tot / 2).ToString() + "] ";

                    ++num;

                    StringBuilder archive = new StringBuilder();

                    while (next_part != "")
                    {
                        string current_tag = basicTag,
                               tmp_content = "";

                        // pequena animação indicando download...
                        if (++dot == 15)
                        {
                            dot = 1;
                        }

                        for (int g = 0; g < dot; ++g)
                        {
                            current_tag += ".";
                        }

                        Application.DoEvents();
                        i_Form.LblCmd.Text = current_tag;
                        Application.DoEvents();

                        var_exchange.infra_fetchFile(next_part, ref next_part, ref tmp_content);

                        archive.Append(tmp_content);
                    }

                    if (File.Exists(fileName))
                    {
                        File.Delete(fileName);
                    }

                    FileStream   fs = new FileStream(fileName, FileMode.CreateNew);
                    BinaryWriter bw = new BinaryWriter(fs);

                    bw.Write(HexEncoding.GetBytes(archive.ToString()));

                    bw.Close();
                    fs.Close();
                }

                var_exchange.m_Client.ExitSession();

                Process p = new Process();

                                    #if LINUX_DIST
                p.StartInfo.FileName = "LinuxUpdater.exe";
                                    #else
                p.StartInfo.FileName = "Updater.exe";
                                    #endif

                p.StartInfo.Arguments      = "";
                p.StartInfo.CreateNoWindow = false;
                p.Start();

                i_Form.Close();

                Application.ExitThread();

                //EndEventCode
                return(true);
            }

                #endregion

                #region - robot_ShowDialog -

            case robot_ShowDialog:
            {
                //InitEventCode robot_ShowDialog
                //EndEventCode
                return(true);
            }

                #endregion

                #region - robot_CloseDialog -

            case robot_CloseDialog:
            {
                //InitEventCode robot_CloseDialog
                //EndEventCode
                return(true);
            }

                #endregion

            default: break;
            }

            return(false);
        }
        public override bool doEvent(int event_number, object arg)
        {
            switch (event_number)
            {
                #region - event_Start -

            case event_Start:
            {
                //InitEventCode event_Start

                                        #if ROBOT
                                        #else
                doEvent(event_Translate, null);
                doEvent(event_FormIsOpening, null);
                                        #endif

                //EndEventCode
                return(true);
            }

                #endregion

                #region - event_Translate -

            case event_Translate:
            {
                //InitEventCode event_Translate
                //EndEventCode
                return(true);
            }

                #endregion

                #region - event_FormIsOpening -

            case event_FormIsOpening:
            {
                //InitEventCode event_FormIsOpening

                string st_csv_files   = "";
                string st_new_version = "";

                StreamReader sr = new StreamReader("app.txt");

                string app = sr.ReadLine();

                sr.Close();

                i_Form.LblCmd.Text = "Buscando lista de arquivos...";
                i_Form.Show();

                Application.DoEvents();
                Thread.Sleep(500);

                                        #if LINUX_DIST
                var_exchange.infra_fetchIncomingVersion(app, Context.FALSE, ref st_csv_files, ref st_new_version);
                                        #else
                var_exchange.infra_fetchIncomingVersion(app, ref st_csv_files, ref st_new_version);
                                        #endif

                // pares de "nome_arquivo,indice"
                int tot = var_util.indexCSV(st_csv_files);

                int num = 1;

                for (int t = 0, dot = 0; t < tot; ++t)
                {
                    string fileName  = var_util.getCSV(t);                                      // busca nome
                    string next_part = var_util.getCSV(++t);                                    // busca indice

                    // mostrador no diálogo
                    string basicTag = "Buscando arquivo [" + num.ToString() + " de " + (tot / 2).ToString() + "] ";

                    ++num;

                    StringBuilder archive = new StringBuilder();

                    while (next_part != "")
                    {
                        string current_tag = basicTag,
                               tmp_content = "";

                        // pequena animação indicando download...
                        if (++dot == 15)
                        {
                            dot = 1;
                        }

                        for (int g = 0; g < dot; ++g)
                        {
                            current_tag += ".";
                        }

                        Application.DoEvents();
                        i_Form.LblCmd.Text = current_tag;
                        Application.DoEvents();

                        var_exchange.infra_fetchFile(next_part, ref next_part, ref tmp_content);

                        archive.Append(tmp_content);
                    }

                    if (File.Exists(fileName))
                    {
                        File.Delete(fileName);
                    }

                    FileStream   fs = new FileStream(fileName, FileMode.CreateNew);
                    BinaryWriter bw = new BinaryWriter(fs);

                    bw.Write(HexEncoding.GetBytes(archive.ToString()));

                    bw.Close();
                    fs.Close();
                }

                InstallData ins = new InstallData();

                i_Form.LblCmd.Text = "Alterando versão do software...";
                Application.DoEvents();
                Thread.Sleep(250);

                ins.st_version = st_new_version;

                ins.ins_registration();

                string ver_path = System.Environment.GetEnvironmentVariable("COMMONPROGRAMFILES") +
                                  "\\" +
                                  InfraSoftwareClient.nameClient + " " + app + "\\version.txt";

                if (File.Exists(ver_path))
                {
                    File.Delete(ver_path);
                }

                StreamWriter sw = new StreamWriter(ver_path);
                sw.WriteLine(st_new_version);
                sw.Close();

                File.Delete("app.txt");

                var_exchange.m_Client.ExitSession();

                Process p = new Process();

                                        #if LINUX_DIST
                p.StartInfo.FileName = "Linux" + app + ".exe";
                                    #else
                p.StartInfo.FileName = app + ".exe";
                                    #endif

                p.StartInfo.Arguments      = "";
                p.StartInfo.CreateNoWindow = false;
                p.Start();

                i_Form.Close();

                Application.ExitThread();

                //EndEventCode
                return(true);
            }

                #endregion

                #region - event_FormIsClosing -

            case event_FormIsClosing:
            {
                //InitEventCode event_FormIsClosing
                //EndEventCode
                return(true);
            }

                #endregion

                #region - robot_ShowDialog -

            case robot_ShowDialog:
            {
                //InitEventCode robot_ShowDialog

                if (i_Form.IsDisposed)
                {
                    i_Form = new MainForm(this);
                }

                i_Form.Show();

                //EndEventCode
                return(true);
            }

                #endregion

                #region - robot_CloseDialog -

            case robot_CloseDialog:
            {
                //InitEventCode robot_CloseDialog

                i_Form.Close();

                //EndEventCode
                return(true);
            }

                #endregion

            default: break;
            }

            return(false);
        }