Пример #1
0
        public static IWebHost CreateHtmlHost(ILimnorDesignerLoader loader, Form caller)
        {
            IWebHost      host    = null;
            Encoding      encode  = Encoding.Unicode;
            string        docType = null;
            VirtualWebDir webSite = WebHost.GetWebSite(loader.Project, caller);

            if (webSite != null)
            {
                installHtmlEdiotr(caller);
                if (!loader.Project.WebFolderGranted)
                {
                    string err = VPLUtil.GrantFolderFullPermission(webSite.PhysicalDirectory);
                    if (string.IsNullOrEmpty(err))
                    {
                        loader.Project.SetWebFolderGranted();
                    }
                    else
                    {
                        MessageBox.Show(caller, string.Format(CultureInfo.InvariantCulture, "Error granting permissions to [{0}]. {1}", webSite.PhysicalDirectory, err), "Web Editor", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                WebPage wpage      = loader.RootObject as WebPage;
                bool    openWebTab = true;
                string  hf         = WebHost.GetHtmlFile(loader.ComponentFilePath);                 //saved HTML file
                string  editFile   = Path.Combine(webSite.PhysicalDirectory, Path.GetFileName(hf)); //in WebFiles
                //create editFile contents
                if (File.Exists(hf))
                {
                    try
                    {
                        //set editing file to the doc type for editing (IE requirement)
                        //for Chrome, try not to do it
                        string        line;
                        StringBuilder sbBeforeHtml = new StringBuilder();
                        bool          gotHtml      = false;
                        StreamReader  sr           = new StreamReader(hf, true);
                        encode = sr.CurrentEncoding;
                        while (!sr.EndOfStream)
                        {
                            line = sr.ReadLine();
                            if (gotHtml)
                            {
                            }
                            else
                            {
                                if (!string.IsNullOrEmpty(line))
                                {
                                    int pos = line.IndexOf("<html", StringComparison.OrdinalIgnoreCase);
                                    if (pos >= 0)
                                    {
                                        if (pos > 0)
                                        {
                                            sbBeforeHtml.Append(line.Substring(0, pos));
                                        }
                                        docType = sbBeforeHtml.ToString();
                                        gotHtml = true;
                                        break;                                         //for Chrome
                                    }
                                    else
                                    {
                                        sbBeforeHtml.Append(line);
                                        sbBeforeHtml.Append("\r\n");
                                    }
                                }
                            }
                        }
                        sr.Close();
                    }
                    catch (Exception err)
                    {
                        MessageBox.Show(caller, string.Format(CultureInfo.InvariantCulture, "Error copying [{0}] to [{1}]. {2}", hf, editFile, err.Message), "Load HTML", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        openWebTab = false;
                    }
                    //for Chrome
                    if (openWebTab)
                    {
                        try
                        {
                            File.Copy(hf, editFile, true);
                        }
                        catch (Exception err)
                        {
                            MessageBox.Show(caller, string.Format(CultureInfo.InvariantCulture, "Error copying from [{0}] to [{1}]. {2}.", hf, editFile, err.Message), "Load HTML", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            openWebTab = false;
                        }
                    }
                    if (openWebTab)
                    {
                        int p0 = hf.LastIndexOf('.');
                        if (p0 > 0)
                        {
                            string css = string.Format(CultureInfo.InvariantCulture, "{0}.css", hf.Substring(0, p0));
                            if (File.Exists(css))
                            {
                                string tgt = Path.Combine(webSite.PhysicalDirectory, Path.GetFileName(css));
                                try
                                {
                                    File.Copy(css, tgt, true);
                                }
                                catch (Exception err)
                                {
                                    MessageBox.Show(caller, string.Format(CultureInfo.InvariantCulture, "Error copying from [{0}] to [{1}]. {2}.", css, tgt, err.Message), "Load HTML", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                    openWebTab = false;
                                }
                            }
                        }
                    }
                }
                else
                {
                    try
                    {
                        StreamWriter sw = new StreamWriter(editFile);
                        sw.Write(Resources.emptyPageEdit);
                        sw.Close();
                    }
                    catch (Exception err)
                    {
                        MessageBox.Show(caller, string.Format(CultureInfo.InvariantCulture, "Error creating empty html file for editing: [{0}]. {1}", editFile, err.Message), "Load HTML", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        openWebTab = false;
                    }
                }
                if (openWebTab)
                {
                    try
                    {
                        CopyJsLib(webSite.PhysicalDirectory);
                        CopyJsColor(webSite.PhysicalDirectory);
                    }
                    catch (Exception err)
                    {
                        MathNode.Log(err, "Error preparing html file for editing: [{0}]", editFile);
                        openWebTab = false;
                    }
                    bool   bErr;
                    string rootPath = IisUtility.FindLocalRootWebPath(caller, out bErr);
                    if (string.IsNullOrEmpty(rootPath))
                    {
                        MathNode.Log("Cannot get physical path for web root.");
                    }
                    else
                    {
                        string libjs = Path.Combine(rootPath, "libjs");
                        if (!Directory.Exists(libjs))
                        {
                            try
                            {
                                Directory.CreateDirectory(libjs);
                            }
                            catch (Exception err2)
                            {
                                MathNode.Log(err2, "Error creating web folder: [{0}]", libjs);
                            }
                        }
                        string jsfile = Path.Combine(libjs, "htmlEditorClient.js");
                        if (!File.Exists(jsfile))
                        {
                            try
                            {
                                StreamWriter sw = new StreamWriter(jsfile, false, Encoding.ASCII);
                                sw.Write(Resources.htmleditorClient);
                                sw.Close();
                            }
                            catch (Exception err3)
                            {
                                MathNode.Log(err3, "Error creating file: [{0}]", jsfile);
                            }
                        }
                    }
                }
                if (openWebTab)
                {
#if USECEF
                    if (IntPtr.Size == 4)
                    {
                        WebHostX86 hx86 = new WebHostX86();
                        host = hx86;
                        hx86.LoadWebHost(loader.Project.ProjectFolder, webSite.PhysicalDirectory, webSite.WebName, editFile, encode, docType, wpage);
                    }
                    else
                    {
                    }
                    //host.SetHtmlFile(editFile, webSite.WebName);
#else
                    host = new WebHost(loader.Project.ProjectFolder, webSite.PhysicalDirectory, webSite.WebName, editFile, encode, docType, wpage);
                    host.SetEncode(encode);
                    if (!string.IsNullOrEmpty(docType))
                    {
                        host.SetDocType(docType);
                    }
#endif
                    wpage.RefreshWebDisplay();
                }
            }
            return(host);
        }
Пример #2
0
 private static void installHtmlEdiotr(Form caller)
 {
     try
     {
         string srcDir = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "DistributeIIS");
         if (Directory.Exists(srcDir))
         {
             string newFile = Path.Combine(srcDir, "new.txt");
             if (File.Exists(newFile))
             {
                 bool   bErr;
                 string localhostDir = IisUtility.FindLocalRootWebPath(caller, out bErr);
                 if (string.IsNullOrEmpty(localhostDir))
                 {
                     MessageBox.Show(caller, "Cannot get physical path for web root.", "Checking web root", MessageBoxButtons.OK, MessageBoxIcon.Error);
                 }
                 else if (!Directory.Exists(localhostDir))
                 {
                     MessageBox.Show(caller, string.Format(CultureInfo.InvariantCulture, "Folder {0} does not exist", srcDir), "Initialize Visual Programming", MessageBoxButtons.OK, MessageBoxIcon.Error);
                 }
                 else
                 {
                     if (MessageBox.Show(caller, string.Format(CultureInfo.InvariantCulture, "Visual Web Page Editor requires that full control permission is granted to Everyone for folder {0}. \r\n\r\nDo you want to allow it?", localhostDir), "Visual Web Page Editor", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
                     {
                         string        e;
                         StringBuilder sbErr = new StringBuilder();
                         e = VPLUtil.GrantFileFullPermission(localhostDir);
                         if (!string.IsNullOrEmpty(e))
                         {
                             sbErr.Append(string.Format(CultureInfo.InvariantCulture, "Error granting permissions for [{0}]. {1}\r\n", localhostDir, e));
                         }
                         else
                         {
                             string   tgt;
                             string[] ss = Directory.GetFiles(srcDir);
                             for (int i = 0; i < ss.Length; i++)
                             {
                                 string sn = Path.GetFileName(ss[i]);
                                 if (string.Compare(sn, "new.txt", StringComparison.OrdinalIgnoreCase) != 0)
                                 {
                                     tgt = Path.Combine(localhostDir, sn);
                                     File.Copy(ss[i], tgt, true);
                                     e = VPLUtil.GrantFileFullPermission(tgt);
                                     if (!string.IsNullOrEmpty(e))
                                     {
                                         sbErr.Append(string.Format(CultureInfo.InvariantCulture, "Error granting permissions for [{0}]. {1}\r\n", tgt, e));
                                     }
                                 }
                             }
                             ss = Directory.GetDirectories(srcDir);
                             for (int i = 0; i < ss.Length; i++)
                             {
                                 tgt = Path.Combine(localhostDir, Path.GetFileName(ss[i]));
                                 copyFolder(ss[i], tgt, sbErr);
                             }
                         }
                         if (sbErr.Length > 0)
                         {
                             MessageBox.Show(caller, string.Format(CultureInfo.InvariantCulture, "Error setting up Visual Web Editor.\r\n {0}", sbErr.ToString()), "Initialize Visual Programming", MessageBoxButtons.OK, MessageBoxIcon.Error);
                         }
                         else
                         {
                             File.Delete(newFile);
                             DesignUtil.EnableHtmlEditor();
                         }
                     }
                 }
             }
             else
             {
                 DesignUtil.EnableHtmlEditor();
             }
         }
         else
         {
             MessageBox.Show(caller, string.Format(CultureInfo.InvariantCulture, "Folder {0} does not exist", srcDir), "Initialize Visual Programming", MessageBoxButtons.OK, MessageBoxIcon.Error);
         }
     }
     catch (Exception err)
     {
         MessageBox.Show(caller, err.Message, "Initialize Visual Programming", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
 }