Пример #1
0
	//protected override void OnGotFocus(EventArgs e) { _c.Focus(); }

	/// <summary>
	/// Loads existing or new workspace.
	/// If fails, shows a task dialog with several choices - retry, load another, create new, cancel. If then fails, ends process.
	/// Sets Model and Text properties of the main form. Updates recent files.
	/// By default runs startup script.
	/// </summary>
	/// <param name="wsDir">
	/// Workspace's directory. The directory should contain file "files.xml" and subdirectory "files".
	/// If null, loads the last used workspace (its path is in settings).
	/// If the setting does not exist, uses AFolders.ThisAppDocuments + @"Main".
	/// If the file does not exist, copies from AFolders.ThisApp + @"Default\Workspace".
	/// </param>
	public FilesModel ZLoadWorkspace(string wsDir = null)
	{
		wsDir ??= Program.Settings.workspace;
		if(wsDir.NE()) wsDir = AFolders.ThisAppDocuments + "Main";
		var xmlFile = wsDir + @"\files.xml";
		var oldModel = _model;
		FilesModel m = null;
		_isNewWorkspace = false;
		g1:
		try {
			//SHOULDDO: if editor runs as admin, the workspace directory should be write-protected from non-admin processes.

			if(_isNewWorkspace = !AFile.ExistsAsFile(xmlFile)) {
				AFile.Copy(AFolders.ThisAppBS + @"Default\Workspace", wsDir);
			}

			_model?.UnloadingWorkspace(); //saves all, closes documents, sets current file = null

			m = new FilesModel(_c, xmlFile);
			_c.Model = m;
		}
		catch(Exception ex) {
			m?.Dispose();
			m = null;
			//AOutput.Write($"Failed to load '{wsDir}'. {ex.Message}");
			switch(ADialog.ShowError("Failed to load workspace", wsDir,
				"1 Retry|2 Load another|3 Create new|0 Cancel",
				owner: this, expandedText: ex.ToString())) {
			case 1: goto g1;
			case 2: m = ZLoadAnotherWorkspace(); break;
			case 3: m = ZLoadNewWorkspace(); break;
			}
			if(m != null) return m;
			if(_model != null) return _model;
			Environment.Exit(1);
		}

		oldModel?.Dispose();
		Program.Model = _model = m;

		//CONSIDER: unexpand path
		if(wsDir != Program.Settings.workspace) {
			if(Program.Settings.workspace != null) {
				var ar = Program.Settings.recentWS ?? Array.Empty<string>();
				int i = Array.IndexOf(ar, wsDir); if(i >= 0) ar = ar.RemoveAt(i);
				Program.Settings.recentWS = ar.InsertAt(0, Program.Settings.workspace);
			}
			Program.Settings.workspace = wsDir;
		}

		Program.MainForm.ZSetTitle();
		if(Program.Loaded >= EProgramState.LoadedWorkspace) {
			ZOpenDocuments();
			ZModel.RunStartupScripts();
		}

		return _model;
	}
Пример #2
0
        /// <summary>
        /// Copies the file at the source to the destination. 
        /// </summary>
        /// <param name="source">The source.</param>
        /// <param name="destination">The destination.</param>
        /// <param name="overwrite">if set to <c>true</c> [overwrite]. By default, is set to false (do not overwrite the destination file).</param>
        /// <returns>Whether the copy was successful or not.</returns>
        public static bool Copy(AFile source, AFile destination, bool overwrite = false)
        {
            Exceptions.NotNullException<AFile>(source, nameof(source));
            Exceptions.NotNullException<AFile>(destination, nameof(destination));

            if ((!overwrite && Fenrir.FileSystem.FileExists(destination.FullPath)) || !Fenrir.FileSystem.FileExists(source.FullPath))
                return false;

            return source.Copy(destination.FullPath, FileCollisionOption.ReplaceExisting) != null;
        }