Exemple #1
0
        ///<summary>If this is middle tier, pass in null.</summary>
        public static void LoadAllPlugins(Form host)
        {
            //No need to check RemotingRole; no call to db.
            List <PluginContainer> listPlugins = new List <PluginContainer>();

            //Loop through all programs that are enabled with a plug-in dll name set.
            foreach (Program program in Programs.GetWhere(x => x.Enabled && !string.IsNullOrEmpty(x.PluginDllName)))
            {
                string dllPath = ODFileUtils.CombinePaths(Application.StartupPath, program.PluginDllName);
                if (RemotingClient.RemotingRole == RemotingRole.ServerWeb)
                {
                    dllPath = ODFileUtils.CombinePaths(System.Web.HttpContext.Current.Server.MapPath(null), program.PluginDllName);
                }
                //Check for the versioning trigger.
                //For example, the plug-in might be entered as MyPlugin[VersionMajMin].dll. The bracketed section will be removed when loading the dll.
                //So it will look for MyPlugin.dll as the dll to load. However, before it loads, it will look for a similar dll with a version number.
                //For example, if using version 14.3.23, it would look for MyPlugin14.3.dll.
                //If that file is found, it would replace MyPlugin.dll with the contents of MyPlugin14.3.dll, and then it would load MyPlugin.dll as normal.
                if (dllPath.Contains("[VersionMajMin]"))
                {
                    Version vers = new Version(Application.ProductVersion);
                    string  dllPathWithVersion = dllPath.Replace("[VersionMajMin]", vers.Major.ToString() + "." + vers.Minor.ToString());
                    dllPath = dllPath.Replace("[VersionMajMin]", "");                 //now stripped clean
                    if (File.Exists(dllPathWithVersion))
                    {
                        File.Copy(dllPathWithVersion, dllPath, true);
                    }
                    else
                    {
                        //try the Plugins folder
                        if (PrefC.AtoZfolderUsed != DataStorageType.InDatabase)                       //must have an AtoZ folder to check
                        {
                            string dllPathVersionCentral = FileAtoZ.CombinePaths(ImageStore.GetPreferredAtoZpath(), "Plugins",
                                                                                 program.PluginDllName.Replace("[VersionMajMin]", vers.Major.ToString() + "." + vers.Minor.ToString()));
                            if (FileAtoZ.Exists(dllPathVersionCentral))
                            {
                                FileAtoZ.Copy(dllPathVersionCentral, dllPath, FileAtoZSourceDestination.AtoZToLocal, doOverwrite: true);
                            }
                        }
                    }
                }
                //We now know the exact name of the dll for the plug-in.  Check to see if it is present.
                if (!File.Exists(dllPath))
                {
                    continue;                    //Nothing to do.
                }
                //The dll was found, try and load it in.
                PluginBase plugin  = null;
                Assembly   ass     = null;
                string     assName = "";
                try {
                    ass     = Assembly.LoadFile(dllPath);
                    assName = Path.GetFileNameWithoutExtension(dllPath);
                    string typeName = assName + ".Plugin";
                    Type   type     = ass.GetType(typeName);
                    plugin      = (PluginBase)Activator.CreateInstance(type);
                    plugin.Host = host;
                }
                catch (Exception ex) {
                    //Never try and show message boxes when on the middle tier, there is no UI.  We should instead log to a file or the event viewer.
                    if (RemotingClient.RemotingRole != RemotingRole.ServerWeb)
                    {
                        //Notify the user that their plug-in is not loaded.
                        MessageBox.Show("Error loading Plugin:" + program.PluginDllName + "\r\n" + ex.Message);
                    }
                    continue;                    //Don't add it to plugin list.
                }
                //The plug-in was successfully loaded and will start getting hook notifications.  Add it to the list of loaded plug-ins.
                PluginContainer container = new PluginContainer();
                container.Plugin     = plugin;
                container.ProgramNum = program.ProgramNum;
                container.Assemb     = ass;
                container.Name       = assName;
                listPlugins.Add(container);
            }
            ListPlugins = listPlugins;
        }
Exemple #2
0
        ///<summary>Throws exceptions.  Creates a new file inside of the email attachment path (inside OpenDentImages) and returns an EmailAttach object
        ///referencing the new file.  If isOutbound is true, then the file will be saved to the "Out" subfolder, otherwise the file will be saved to the
        ///"In" subfolder.  The displayFileName will always contain valid file name characters, because it is either a hard coded value or is based on an
        ///existing valid file name.  If a file already exists matching the actualFileName, then an exception will occur.  Set actualFileName to empty
        ///string to generate a unique actual file name.  If the actual file name is generated, then actual file name will end with the displayFileName,
        ///so that the actual files are easier to locate and have the same file extension as the displayedFileName.</summary>
        public static EmailAttach CreateAttach(string displayedFileName, string actualFileName, byte[] arrayData, bool isOutbound)
        {
            //No need to check RemotingRole; no call to db.
            EmailAttach emailAttach = new EmailAttach();

            emailAttach.DisplayedFileName = displayedFileName;
            if (String.IsNullOrEmpty(emailAttach.DisplayedFileName))
            {
                //This could only happen for malformed incoming emails, but should not happen.  Name uniqueness is virtually guaranteed below.
                //The actual file name will not have an extension, so the user will be asked to pick the program to open the attachment with when
                //the attachment is double-clicked.
                emailAttach.DisplayedFileName = "attach";
            }
            string attachDir = GetAttachPath();
            string subDir    = "In";

            if (isOutbound)
            {
                subDir = "Out";
            }
            if (!CloudStorage.IsCloudStorage && !Directory.Exists(ODFileUtils.CombinePaths(attachDir, subDir)))
            {
                Directory.CreateDirectory(ODFileUtils.CombinePaths(attachDir, subDir));
            }
            if (String.IsNullOrEmpty(actualFileName))
            {
                while (String.IsNullOrEmpty(emailAttach.ActualFileName) ||
                       FileAtoZ.Exists(FileAtoZ.CombinePaths(attachDir, emailAttach.ActualFileName)))
                {
                    //Display name is tacked onto actual file name last as to ensure file extensions are the same.
                    emailAttach.ActualFileName = FileAtoZ.CombinePaths(subDir,
                                                                       DateTime.Now.ToString("yyyyMMdd") + "_" + DateTime.Now.TimeOfDay.Ticks.ToString()
                                                                       + "_" + MiscUtils.CreateRandomAlphaNumericString(4) + "_" + emailAttach.DisplayedFileName);
                }
            }
            else
            {
                //The caller wants a specific actualFileName.  Use the given name as is.
                emailAttach.ActualFileName = FileAtoZ.CombinePaths(subDir, actualFileName);
            }
            string attachFilePath = FileAtoZ.CombinePaths(attachDir, emailAttach.ActualFileName);

            if (FileAtoZ.Exists(attachFilePath))
            {
                throw new ApplicationException("Email attachment could not be saved because a file with the same name already exists.");
            }
            try {
                FileAtoZ.WriteAllBytes(attachFilePath, arrayData);
            }
            catch (Exception ex) {
                try {
                    if (FileAtoZ.Exists(attachFilePath))
                    {
                        FileAtoZ.Delete(attachFilePath);
                    }
                }
                catch {
                    //We tried our best to delete the file, and there is nothing else to try.
                }
                throw ex;                //Show the initial error message, even if the Delete() failed.
            }
            return(emailAttach);
        }