Example #1
0
        /// <summary>
        /// Create a new empty web resource
        /// </summary>
        /// <param name="extension">Type of web resource file to create</param>
        public void CreateEmptyWebResource(string extension)
        {
            var callerNode = tv.SelectedNode;

            var nwrDialog = new NewWebResourceDialog(extension)
            {
                StartPosition = FormStartPosition.CenterParent
            };

            if (nwrDialog.ShowDialog(ParentForm) == DialogResult.OK)
            {
                var    tempNode = callerNode;
                string name     = callerNode.Text;
                while (tempNode.Parent != null)
                {
                    name     = string.Format("{0}/{1}", tempNode.Parent.Text, name);
                    tempNode = tempNode.Parent;
                }

                var webResource = new Entity("webresource");
                webResource["content"]         = "";
                webResource["webresourcetype"] = new OptionSetValue(WebResource.GetTypeFromExtension(extension));
                webResource["name"]            = string.Format("{0}/{1}", name, string.Format("{0}.{1}", nwrDialog.WebResourceName, extension));
                var wr = new WebResource(webResource, null);

                var parts = nwrDialog.WebResourceName.Split('/');

                for (int i = 0; i < parts.Length; i++)
                {
                    if (i != parts.Length - 1)
                    {
                        var folderNode = new TreeNode(parts[i])
                        {
                            ImageIndex = 1, SelectedImageIndex = 1
                        };

                        callerNode.Nodes.Add(folderNode);
                        callerNode.Expand();
                        callerNode = folderNode;
                    }
                    else
                    {
                        var node = new TreeNode(string.Format("{0}.{1}", parts[i], extension))
                        {
                            ImageIndex =
                                WebResource.GetImageIndexFromExtension
                                    (extension)
                        };
                        node.SelectedImageIndex = node.ImageIndex;
                        node.Tag = wr;

                        callerNode.Nodes.Add(node);
                        callerNode.Expand();
                    }
                }
            }
        }
Example #2
0
        private void tv_DragDrop(object sender, DragEventArgs e)
        {
            var errorList = new List <string>();

            // Retrieve the current selected node
            var treeview    = (TreeView)sender;
            var location    = tv.PointToScreen(Point.Empty);
            var currentNode = treeview.GetNodeAt(e.X - location.X, e.Y - location.Y);

            var files = (string[])e.Data.GetData(DataFormats.FileDrop);

            foreach (var file in files)
            {
                var    fi             = new FileInfo(file);
                string nodeObjectName = GetName(currentNode);

                // Test valid characters
                if (WebResource.IsNameInvalid(fi.Name))
                {
                    errorList.Add(file);
                }
                else
                {
                    // Create CRM web resource
                    var webResource = new Entity("webresource")
                    {
                        ["content"]         = Convert.ToBase64String(File.ReadAllBytes(file)),
                        ["webresourcetype"] = new OptionSetValue(WebResource.GetTypeFromExtension(fi.Extension.Remove(0, 1))),
                        ["name"]            = string.Format("{0}/{1}", nodeObjectName, fi.Name),
                        ["displayname"]     = string.Format("{0}/{1}", nodeObjectName, fi.Name)
                    };

                    var newWebResource = new WebResource(webResource, fi.FullName);

                    // Create file if the current node has a filepath in its tag
                    // this means, wen resources come from disk
                    if (currentNode.Tag != null && currentNode.Tag is string &&
                        Directory.Exists(currentNode.Tag.ToString()))
                    {
                        var resultingFileName = Path.Combine(currentNode.Tag.ToString(), fi.Name);

                        if (resultingFileName.ToLower() != fi.FullName.ToLower())
                        {
                            if (DialogResult.Yes == MessageBox.Show(
                                    "Would you like to also copy this file to folder '" + currentNode.Tag + "'?",
                                    "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
                            {
                                File.WriteAllBytes(resultingFileName, File.ReadAllBytes(file));
                            }
                        }
                    }

                    var node = new TreeNode(fi.Name)
                    {
                        ImageIndex = WebResource.GetImageIndexFromExtension(fi.Extension.Remove(0, 1))
                    };
                    node.SelectedImageIndex = node.ImageIndex;
                    node.Tag = newWebResource;

                    newWebResource.Node = node;

                    currentNode.Nodes.Add(node);
                    currentNode.Expand();
                }
            }

            if (errorList.Any())
            {
                MessageBox.Show(ParentForm,
                                Resources.WebresourceTreeView_InvalidFileNameWarningMessage + string.Join("\r\n", errorList),
                                Resources.MessageBox_WarningTitle,
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Warning);
            }
        }
Example #3
0
        /// <summary>
        /// Add existing files from disk as new web resources
        /// </summary>
        public void AddExistingWebResource()
        {
            TreeNode selectedNode = tv.SelectedNode;
            TreeNode tempNode     = selectedNode;

            string name = tempNode.Text;

            while (tempNode.Parent != null)
            {
                name     = string.Format("{0}/{1}", tempNode.Parent.Text, name);
                tempNode = tempNode.Parent;
            }

            var ofd = new OpenFileDialog {
                Multiselect = true, Title = "Select file(s) for web resource(s)"
            };

            if (ofd.ShowDialog(ParentForm) == DialogResult.OK)
            {
                var errorList = new List <string>();

                foreach (string fileName in ofd.FileNames)
                {
                    var fi = new FileInfo(fileName);

                    //Test valid characters
                    if (WebResource.IsNameInvalid(fi.Name))
                    {
                        errorList.Add(fileName);
                    }
                    else
                    {
                        var webResource = new Entity("webresource");
                        webResource["content"]         = Convert.ToBase64String(File.ReadAllBytes(fileName));
                        webResource["webresourcetype"] =
                            new OptionSetValue(WebResource.GetTypeFromExtension(fi.Extension.Remove(0, 1)));
                        webResource["name"]        = string.Format("{0}/{1}", name, fi.Name);
                        webResource["displayname"] = string.Format("{0}/{1}", name, fi.Name);
                        var wr = new WebResource(webResource, fileName);

                        var node = new TreeNode(fi.Name)
                        {
                            ImageIndex =
                                WebResource.GetImageIndexFromExtension(fi.Extension.Remove(0,
                                                                                           1))
                        };
                        node.SelectedImageIndex = node.ImageIndex;
                        node.Tag = wr;

                        selectedNode.Nodes.Add(node);

                        selectedNode.Expand();
                    }

                    if (errorList.Count > 0)
                    {
                        MessageBox.Show("Some file have not been added since their name does not match naming policy\r\n"
                                        + string.Join("\r\n", errorList));
                    }
                }
            }

            tv.TreeViewNodeSorter = new NodeSorter();
            tv.Sort();
        }