public FileInformation GetFilenameToOpen(FileLocation location)
        {
            if (m_currentEditor == null || location == FileLocation.Unknown)
            {
                return(null);
            }

            FileInformation fileInfo = new FileInformation();

            if (location == FileLocation.Local)
            {
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.CheckFileExists = true;
                ofd.Multiselect     = false;
                ofd.Filter          = m_fileFilter;

                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    fileInfo.Location = location;

                    fileInfo.Filename.Assign(ofd.FileName, PathFormat.Windows);
                }
                else
                {
                    return(null);
                }
            }
            else if (location == FileLocation.Remote)
            {
                RemoteFileDialog rfd = RemoteFileDialog.Instance;

                rfd.Prepare(true, m_fileFilter);                //, "");
                DialogResult dr = rfd.ShowDialog();

                if (dr == DialogResult.OK)
                {
                    fileInfo.Filename.Assign(rfd.SelectedFile);
                }
                else
                {
                    return(null);
                }
            }

            fileInfo.Location = location;
            return(fileInfo);
        }
        public bool SaveFile(ChameleonEditor editor, FileLocation suggestedLocation,
                             bool saveAs, bool askLocalRemote)
        {
            bool         doSaveAs;
            FileLocation location = FileLocation.Unknown;

            if (suggestedLocation == FileLocation.Unknown)
            {
                doSaveAs = true;
            }
            else
            {
                doSaveAs = saveAs || !m_currentEditor.HasBeenSaved();
            }

            string filename = "";

            if (doSaveAs && suggestedLocation == FileLocation.Unknown)
            {
                string message = "Where should this file be saved?";

                List <string> buttons = new List <string>();
                buttons.Add("Locally (on the computer you're using)");
                buttons.Add("Remotely (on the server)");
                buttons.Add("Cancel");

                int button = cTaskDialog.ShowCommandBox("Save File Location", message, "",
                                                        string.Join("|", buttons), false);
                location = (FileLocation)button;
            }
            else
            {
                location = suggestedLocation;
            }

            if (location == FileLocation.Unknown)
            {
                return(false);
            }

            if (App.UserSettings.PermittedFeatures.HasFlag(ChameleonFeatures.AutoReformat))
            {
                m_currentEditor.ReformatBuffer();
            }

            string fileContents = m_currentEditor.Text;

LocationSelected:
            if (location == FileLocation.Local)
            {
                if (doSaveAs)
                {
                    SaveFileDialog sfd = new SaveFileDialog();
                    sfd.Title  = "Save File As";
                    sfd.Filter = m_fileFilter;

                    if (sfd.ShowDialog() != DialogResult.OK)
                    {
                        return(false);
                    }

                    // SFD automatically appends the appropriate extension if necessary
                    filename = sfd.FileName;
                }
                else
                {
                    filename = m_currentEditor.Filename;
                }

                if (filename == "")
                {
                    return(false);
                }

                File.WriteAllText(filename, fileContents);
            }
            else if (location == FileLocation.Remote)
            {
                if (!ChameleonNetworking.Instance.IsConnected)
                {
                    string message = "This is a remote file, but Chameleon is not currently connected to a remote server.  You can: ";

                    List <string> buttons = new List <string>();
                    buttons.Add("Save the file locally (on the computer you're using)");
                    buttons.Add("Cancel saving");

                    int button = cTaskDialog.ShowCommandBox("Save File Location", message, "",
                                                            string.Join("|", buttons), false);
                    location = (FileLocation)button;

                    if (location == FileLocation.Local)
                    {
                        doSaveAs = true;
                        goto LocationSelected;
                    }

                    return(false);
                }

                if (doSaveAs)
                {
                    RemoteFileDialog rfd = RemoteFileDialog.Instance;
                    rfd.Prepare(false, m_fileFilter);                    //, "");

                    DialogResult dr = rfd.ShowDialog();

                    if (dr == DialogResult.OK)
                    {
                        filename = rfd.SelectedFile.GetFullPath();
                    }
                    else
                    {
                        return(false);
                    }

                    if (filename == "")
                    {
                        return(false);
                    }
                }
                else
                {
                    filename = m_currentEditor.Filename;
                }

                FilePath fp = new FilePath(filename, PathFormat.Unix);

                ChameleonNetworking.Instance.SendFileContents(fp, fileContents);
            }
            // shouldn't be Unknown by this point

            editor.SetFileSaved(filename, location);

            if (EditorStatusChanged != null)
            {
                EditorStatusChanged(this, null);
            }

            if (App.UserSettings.PermittedFeatures.HasFlag(ChameleonFeatures.CodeRules))
            {
                RunCodeRules(editor);
            }


            return(true);
        }