예제 #1
0
 /// <summary>
 ///
 /// </summary>
 private void FetchKeys()
 {
     MattimonSQLite.SQLiteClientDatabase db = GetLocalDatabase();
     fetchedUserId    = db.GetUserId();
     fetchedDeviceId  = db.GetDeviceId();
     fetchedCompanyId = db.GetCompanyId();
 }
        /// <summary>
        ///
        /// </summary>
        /// <param name="db"></param>
        /// <returns></returns>
        private static bool DatabaseFileExists(out MattimonSQLite.SQLiteClientDatabase db)
        {
            try
            {
                db = new MattimonSQLite.SQLiteClientDatabase(
                    MattimonAgentLibrary.Static.Constants.CommonAppData,
                    MattimonAgentLibrary.Static.MattimonRegistrySubkeyNames.Publisher,
                    MattimonAgentLibrary.Static.MattimonRegistrySubkeyNames.DisplayName,
                    MattimonAgentLibrary.Static.Constants.LocalDatabaseName);
            }
            catch (Exception ex)
            {
                db = null;
                GUI.BitscoreForms.BitscoreMessageBox.Show(ex.Message + "\n\n" + ex.StackTrace, "Local Database Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(false);
            }

            if (!db.FileExists(out string foundDatabaseFilePath))
            {
                db = null;
                new FormError(MattimonAgentLibrary.Static.MattimonRegistrySubkeyNames.DisplayName,
                              "The program can't find the database file on your machine. Please re-install the application.");
                return(false);
            }
            return(true);
        }
        /// <summary>
        /// Use this only if the application is installed but
        /// the device entry is not longer found on the server.
        /// </summary>
        public static bool RepostDevice()
        {
            MattimonSQLite.SQLiteClientDatabase db = GetLocalDatabase();
            bool ok = PostDevice(db, out string errmsg);

            if (errmsg != "")
            {
                throw (new Exception(errmsg));
            }

            // Refresh the services
            List <GUI.Controls.UCMattimonServicesGrid.ServiceMetaData> services = GetMattimonServices();

            foreach (GUI.Controls.UCMattimonServicesGrid.ServiceMetaData s in services)
            {
                MyServiceController.StopService(s.Name);
                while (MyServiceController.GetServiceStatus(s.Name) != MyServiceController.ServiceState.Stopped)
                {
                    ;
                }
                MyServiceController.StartService(s.Name);
            }
            return(ok);
        }
예제 #4
0
 /// <summary>
 ///
 /// </summary>
 private void FetchIntervals()
 {
     MattimonSQLite.SQLiteClientDatabase db = GetLocalDatabase();
     selectedIntervalRadioButton = db.GetReportingInterval();
     fetchedIntervals            = db.GetReportingInterval();
 }
        private static Boolean PostDevice(MattimonSQLite.SQLiteClientDatabase db, out string errorMessage)
        {
            MattimonAgentLibrary.WMI.WMIProvider provider = new MattimonAgentLibrary.WMI.WMIProvider();
            Device device = new Device(); DeviceOptions deviceOptions = new DeviceOptions();
            /// If Operating System string contains 'server', we'll suppose that the device is a server
            /// Otherwise, we'll suppose that the device is a workstation.
            Boolean isServer = provider.GetOperatingSystemString().ToLower().Contains("server");
            /// Device type id should match psm_device_types primary key
            /// '2' means Server and '3' means Workstation, according to the Mattimon database on server.
            int definedDeviceTypeId = isServer ? 2 : 3;

            //device.Port = 80;  // Port should not be defined in this context---the user should select an active port from his own machine using MattimonAgentApplication.
            device.BIOSSerialNumber            = provider.GetBIOSSerialNumber();
            device.Company_Id                  = db.GetCompanyId();
            device.ComputerName                = provider.GetComputerName();
            device.IpAddress                   = provider.GetIPAddress();
            device.MacAddress                  = provider.GetMacAddress();
            device.Model                       = provider.GetModel();
            device.OperatingSystem             = provider.GetOperatingSystemString();
            device.OperatingSystemSerialNumber = provider.GetOSSerialNumber();
            device.User_Id                     = db.GetUserId();
            device.Device_Type_Id              = definedDeviceTypeId;
            device.AgentReportInterval         = db.GetReportingInterval();
            device.MonitorSql                  = 0;

            DeviceRequests requests = new DeviceRequests();

            device = requests.CreateDeviceEntry(device);

            Boolean postDeviceSuccess =
                device.Exception == null &&
                device.HttpRequestException == null &&
                device.MySqlExceptionMessage == null &&
                device.Device_Id > 0;

            if (postDeviceSuccess)
            {
                errorMessage = "";

                db.UpdateDeviceID(device.Device_Id);
            }
            else
            {
                errorMessage = "We could not receive your device due to the following errors.\n\n";

                if (device.HttpRequestException != null)
                {
                    errorMessage += device.HttpRequestException.Message + "\n\n" +
                                    device.HttpRequestException.StackTrace;
                }
                else if (device.Exception != null)
                {
                    errorMessage += device.Exception.Message + "\n\n" +
                                    device.Exception.StackTrace;
                }
                else if (device.MySqlExceptionMessage != null)
                {
                    errorMessage += device.MySqlExceptionMessage;
                }
                else
                {
                    errorMessage = "An unhandled error has occurred.";
                }
            }
            return(postDeviceSuccess);
        }
예제 #6
0
        /// <summary>
        ///
        /// </summary>
        private void Uninstall()
        {
            bool deleteEventLogs =
                MessageBox.Show(this,
                                "You're now about to uninstall " + Static.MattimonRegistrySubkeyNames.DisplayName + ".\n" +
                                "Do you wish to also remove the application's event logs?", Text,
                                MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes;


            // First, fetch the device id from the local database and delete the device from the server
            MattimonSQLite.SQLiteClientDatabase db =
                new MattimonSQLite.SQLiteClientDatabase(
                    Static.Constants.CommonAppData,
                    Static.MattimonRegistrySubkeyNames.Publisher,
                    Static.MattimonRegistrySubkeyNames.DisplayName,
                    Static.Constants.LocalDatabaseName
                    );

            long deviceID = db.GetDeviceId();

            db.Dispose();
            db = null;

            // all files related to SQLite should now be deleted.
            try
            {
                Tools.GUITools.SetControlPropertyThreadSafe(label1, "Text", "Deleting local database...");
                // Try and delete the database directory and all its content
                IOTools.DeleteDatabaseDirectory();
                // Indicate success
                Tools.GUITools.SetControlPropertyThreadSafe(label1, "Text", "Done");

                Tools.GUITools.SetControlPropertyThreadSafe(label1, "Text", "Deleting SQLite source files...");
                string sqliteX64dir = Directory.GetCurrentDirectory() + "\\" + "x64";
                string sqliteX86dir = Directory.GetCurrentDirectory() + "\\" + "x86";
            }
            catch
            {
            }



            RequestResult rr = null;

            try
            {
                Tools.GUITools.SetControlPropertyThreadSafe(label1, "Text", "Deleting device entry from server...");
                rr = DeleteDeviceEntry(deviceID);
                Tools.GUITools.SetControlPropertyThreadSafe(label1, "Text", "Done");
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, "An error occurred while attempted to delete your device entry from our server.\n" +
                                "Click \"OK\" to continue uninstalling.\n\n" + ex.ToString(), "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (rr != null && !rr.Success)
            {
                // Indicate failure
                Tools.GUITools.SetControlPropertyThreadSafe(label1, "Text", "Failed");

                // Create the error message
                string msg = "We could not delete your device from our server.\n" +
                             "You may manually delete your device from the Mattimon website.\n" +
                             "Click \"OK\" to continue unistalling." +
                             "\n\n\nError details:\n";

                // Get the error details
                if (rr.HttpRequestException != null)
                {
                    msg += rr.HttpRequestException.Message + "\n\n" + rr.HttpRequestException.ToString();
                }

                else if (rr.MySqlExceptionMessage != null)
                {
                    msg += rr.MySqlExceptionMessage;
                }

                else if (rr.Exception != null)
                {
                    msg += rr.Exception.Message + "\n\n" + rr.Exception.ToString();
                }
                else
                {
                    msg += "Unknown error";
                }

                // Show the error and continue uninstalling
                MessageBox.Show(this, msg,
                                "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                // Indicate success
                Tools.GUITools.SetControlPropertyThreadSafe(label1, "Text", "Done");
            }


            try
            {
                Tools.GUITools.SetControlPropertyThreadSafe(label1, "Text", "Deleting local database...");
                // Try and delete the database directory and all its content
                IOTools.DeleteDatabaseDirectory();
                // Indicate success
                Tools.GUITools.SetControlPropertyThreadSafe(label1, "Text", "Done");
            }
            catch (Exception)
            {
                Tools.GUITools.SetControlPropertyThreadSafe(label1, "Text", "Unable to delete local database directory.");
            }



            /// Get the directory of the MattimonAgentService and MattimonUpdateService
            /// WARNING: Tools.RegistryTools.GetInstallLocationByDisplayName must be called before
            /// removing the application entry from the registry and before actually
            /// deleting the directory, so we can also get Assembly Information from the service executable (.exe) file.
            String svcPath = Path.Combine(Tools.RegistryTools.GetInstallLocationByDisplayName(
                                              GetDisplayName()),
                                          "MattimonAgentService.exe");

            String svcUpdPath = Path.Combine(Tools.RegistryTools.GetInstallLocationByDisplayName(
                                                 GetDisplayName()),
                                             "MattimonUpdateService.exe");

            String svcMattSqlPath = Path.Combine(Tools.RegistryTools.GetInstallLocationByDisplayName(
                                                     GetDisplayName()),
                                                 "MattimonSQLServerService.exe");

            String svcEvtLogsPath = Path.Combine(Tools.RegistryTools.GetInstallLocationByDisplayName(
                                                     GetDisplayName()),
                                                 "MattimonEventLogService.exe");



            /// Get the the assembly name of the MattimonAgentService and MattimonUpdateService as Service Name
            string svcName = new Tools.ProjectAssemblyAtrributes(
                Path.Combine(svcPath)).AssemblyTitle;

            string svcUpdName = new Tools.ProjectAssemblyAtrributes(
                Path.Combine(svcUpdPath)).AssemblyTitle;

            /// Get the assembly product of the MattimonAgentService and MattimonUpdateService as Display Name
            string svcDisplay = new Tools.ProjectAssemblyAtrributes(
                Path.Combine(svcPath)).AssemblyProduct;

            string svcUpdDisplay = new Tools.ProjectAssemblyAtrributes(
                Path.Combine(svcUpdPath)).AssemblyProduct;

            string svcMattSQLName = new Tools.ProjectAssemblyAtrributes(
                Path.Combine(svcMattSqlPath)).AssemblyTitle;

            string svcMattSQLDisplay = new Tools.ProjectAssemblyAtrributes(
                Path.Combine(svcMattSqlPath)).AssemblyProduct;

            string svcEvtLogName = new Tools.ProjectAssemblyAtrributes(
                Path.Combine(svcEvtLogsPath)).AssemblyTitle;

            string svcEvtLogDisplay = new Tools.ProjectAssemblyAtrributes(
                Path.Combine(svcEvtLogsPath)).AssemblyProduct;

            // User this variable to know if the services where uninstalled later.
            bool servicesUninstalled = true;

            /// Try Stop/Uninstall Agent service
            try
            {
                // Stop the service if it is not stopped
                if (Tools.MyServiceController.GetServiceStatus(svcName) != Tools.MyServiceController.ServiceState.Stopped)
                {
                    Tools.MyServiceController.StopService(svcName);
                }

                // Uninstall the service
                Tools.MyServiceController.Uninstall(svcName);
            }
            catch (Exception ex)
            {
                String msg = "";
                if (Tools.MyServiceController.ServiceIsInstalled(new Tools.ProjectAssemblyAtrributes(
                                                                     Path.Combine(svcPath)).AssemblyTitle))
                {
                    // Mark to false
                    servicesUninstalled = false;

                    msg = "Failed to uninstall " + new Tools.ProjectAssemblyAtrributes(
                        Path.Combine(svcPath)).AssemblyProduct;
                }
                MessageBox.Show((msg != "" ? msg + ".\n\nError details:\n" : "") + ex.Message + "\n\n" + ex.ToString(),
                                Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            /// Try Stop/Uninstall Update service
            try
            {
                // Stop the service if it is not stopped
                if (Tools.MyServiceController.GetServiceStatus(svcUpdName) != Tools.MyServiceController.ServiceState.Stopped)
                {
                    Tools.MyServiceController.StopService(svcUpdName);
                }

                // Uninstall the service
                Tools.MyServiceController.Uninstall(svcUpdName);
            }
            catch (Exception ex)
            {
                String msg = "";
                if (Tools.MyServiceController.ServiceIsInstalled(new Tools.ProjectAssemblyAtrributes(
                                                                     Path.Combine(svcUpdPath)).AssemblyTitle))
                {
                    // Mark to false
                    servicesUninstalled = false;
                    msg = "Failed to uninstall " + svcUpdName;
                }
                MessageBox.Show((msg != "" ? msg + ".\n\nError details:\n" : "") + ex.Message + "\n\n" + ex.ToString(),
                                Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            /// Try Stop/Uninstall Mattimon SQL Service
            try
            {
                // Stop the service if it is not stopped
                if (Tools.MyServiceController.ServiceIsInstalled(svcMattSQLName))
                {
                    if (Tools.MyServiceController.GetServiceStatus(svcMattSQLName) != Tools.MyServiceController.ServiceState.Stopped)
                    {
                        Tools.MyServiceController.StopService(svcMattSQLName);
                    }
                }

                // Uninstall the service
                Tools.MyServiceController.Uninstall(svcMattSQLName);
            }
            catch (Exception ex)
            {
                String msg = "";
                if (Tools.MyServiceController.ServiceIsInstalled(new Tools.ProjectAssemblyAtrributes(
                                                                     Path.Combine(svcMattSqlPath)).AssemblyTitle))
                {
                    // Mark to false
                    servicesUninstalled = false;
                    msg = "Failed to uninstall " + svcMattSQLName;
                }
                MessageBox.Show((msg != "" ? msg + ".\n\nError details:\n" : "") + ex.Message + "\n\n" + ex.ToString(),
                                Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            /// Try Stop/Uninstall Mattimon Event Log Service
            try
            {
                // Stop the service if it is not stopped
                if (Tools.MyServiceController.ServiceIsInstalled(svcEvtLogName))
                {
                    if (Tools.MyServiceController.GetServiceStatus(svcEvtLogName) != Tools.MyServiceController.ServiceState.Stopped)
                    {
                        Tools.MyServiceController.StopService(svcEvtLogName);
                    }
                }

                // Uninstall the service
                Tools.MyServiceController.Uninstall(svcEvtLogName);
            }
            catch (Exception ex)
            {
                String msg = "";
                if (Tools.MyServiceController.ServiceIsInstalled(new Tools.ProjectAssemblyAtrributes(
                                                                     Path.Combine(svcEvtLogsPath)).AssemblyTitle))
                {
                    // Mark to false
                    servicesUninstalled = false;
                    msg = "Failed to uninstall " + svcEvtLogName;
                }
                MessageBox.Show((msg != "" ? msg + ".\n\nError details:\n" : "") + ex.Message + "\n\n" + ex.ToString(),
                                Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            try
            {
                // Show progress bar
                Tools.GUITools.SetControlPropertyThreadSafe(progressBar1, "Visible", true);

                // Remove the directory content
                RemoveDirectories(Directory.GetParent(
                                      this.GetApplicationRegistryInstallLocation()).FullName);

                // Progress bar not longer needed, hide it
                Tools.GUITools.SetControlPropertyThreadSafe(progressBar1, "Visible", false);

                // Show
                Tools.GUITools.SetControlPropertyThreadSafe(label1, "Text", "Finalizing uninstall...");


                // Indicate success
                Tools.GUITools.SetControlPropertyThreadSafe(label1, "Text", "Done");
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.ToString(), Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }


            try
            {
                // Finally program from the control panel
                RemoveControlPanelProgram();
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message + "\n\n" + ex.StackTrace,
                                Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            /// Delete the event logs only if the user wishes to and that
            /// both services have been uninstalled.
            if (deleteEventLogs && servicesUninstalled)
            {
                string mainLogName = Static.MattimonEventLogConstants.MainLogName;
                string srcName1    = Static.MattimonEventLogConstants.UpdaterSourceName;
                string srcName2    = Static.MattimonEventLogConstants.AgentSourceName;
                string srcName3    = Static.MattimonEventLogConstants.SQLServerSourceName;


                if (EventLog.SourceExists(srcName1))
                {
                    Tools.GUITools.SetControlPropertyThreadSafe(label1, "Text", "Removing " + srcName1);
                    EventLog.DeleteEventSource(srcName1);
                    Tools.GUITools.SetControlPropertyThreadSafe(label1, "Text", "Done");
                }

                if (EventLog.SourceExists(srcName2))
                {
                    Tools.GUITools.SetControlPropertyThreadSafe(label1, "Text", "Removing " + srcName2);
                    EventLog.DeleteEventSource(srcName2);
                    Tools.GUITools.SetControlPropertyThreadSafe(label1, "Text", "Done");
                }

                if (EventLog.SourceExists(srcName3))
                {
                    Tools.GUITools.SetControlPropertyThreadSafe(label1, "Text", "Removing " + srcName3);
                    EventLog.DeleteEventSource(srcName3);
                    Tools.GUITools.SetControlPropertyThreadSafe(label1, "Text", "Done");
                }

                if (EventLog.Exists(mainLogName))
                {
                    Tools.GUITools.SetControlPropertyThreadSafe(label1, "Text", "Removing " + mainLogName);
                    EventLog.Delete(mainLogName);
                    Tools.GUITools.SetControlPropertyThreadSafe(label1, "Text", "Done");
                }
            }



            //if (rebootRequired)
            //{
            //    Tools.GUITools.SetControlPropertyThreadSafe(label1, Text,
            //        Static.MattimonRegistrySubkeyNames.DisplayName + " has been uninstalled.");
            //    Tools.GUITools.SetControlPropertyThreadSafe(progressBar1, "Visible", false);

            //    DialogResult reboot = MessageBox.Show(this,
            //          Static.MattimonRegistrySubkeyNames.DisplayName +
            //          " has finished uninstalling.\n" +
            //         "Do you wish to reboot your system now?",
            //          Static.MattimonRegistrySubkeyNames.DisplayName,
            //         MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button2);

            //    if (reboot != DialogResult.Yes)
            //    {
            //        Application.Exit();
            //        return;
            //    }

            //    System.Diagnostics.Process.Start("shutdown.exe", "-r -t 0");
            //    Application.Exit();
            //    return;
            //}

            Application.Exit();
        }