public Document(User owner, string file, Permission.Permissions perm) { this.owner = owner; this.permission = permission; this.lastChanged = DateTime.Now; // TODO file }
// Primary public Document(User owner, string content, string path, Permission.Permissions perm) { this.permission = perm; this.owner = owner; this.content = content; this.lastChanged = DateTime.Now; }
/// <summary> /// Creates a new directory. /// </summary> /// <param name="path">Path to the new directory.</param> public void CreateNewFolder(User user, string path) { string root = "root/" + user.username + "/"; Console.WriteLine("FOLDRE SADFSDFSADF" + path); if(!Directory.Exists(root + path)) { Directory.CreateDirectory(root + path); Console.WriteLine("FOLDER.CREATED Directory"); } //string[] splitPath = path.Split('/'); //string fileName = splitPath[splitPath.Length - 1]; //string curDir = root; //// Check if the directory exists. //// Create if it doesn't. //for (int i = 2; i < splitPath.Length - 2; i++) //{ // if(!Directory.Exists(curDir)) // { // Directory.CreateDirectory(curDir); // Console.WriteLine("CREATED FOLDER IN FOLDER CLASS"); // } // curDir += splitPath + "/"; //} }
public override Project AddProject(string title, string userMail, int id = 0, bool db = false) { if (title == null || userMail == null) throw new ArgumentNullException(); Project p = new Project() { Title = title }; using (var dbContext = new sliceofpieEntities2()) { //Insert project dbContext.Projects.AddObject(p); dbContext.SaveChanges(); } User u = new User() { Email = userMail }; ProjectUser pu = new ProjectUser() { UserEmail = userMail, ProjectId = p.Id }; using (var dbContext = new sliceofpieEntities2()) { //Insert projectUser if (dbContext.Users.Count(dbUser => dbUser.Email.Equals(u.Email)) < 1) throw new ArgumentException("No user with email " + u.Email + " exists!"); dbContext.ProjectUsers.AddObject(pu); dbContext.SaveChanges(); } return new Project() { Title = p.Title, Id = p.Id }; }
// WEB public Document(User owner, int id, string content, string path) { this.owner = owner; this.documentId = id; this.content = content; this.lastChanged = DateTime.Now; this.path = path; }
public Document(User owner) { this.owner = owner; // file //this.documentId this.content = ""; this.lastChanged = DateTime.Now; }
private bool modified; // Does this document have unsaved changes? #endregion Fields #region Constructors public EditWindow(Project proj, Document doc, User user) { currentProj = proj; currentDoc = doc; currentUser = user; InitializeComponent(); }
public DocumentStruct(string title, User user, string ID, string path, bool modified, bool deleted) { id = ID; fileType = DocType.Document; this.title = title; this.path = path; this.modified = modified; this.deleted = deleted; }
// Same as constructor above, but this one allows to set id as well. // This is primarily used by tests. public Document(string text, string title, User owner, string id) { this.text = text; this.title = title; this.owner = owner; this.path = ""; this.images = new List<Picture>(); log = new Document.DocumentLog(owner); this.id = id; }
/// <summary> /// Returns all documents from the database that belong to /// the specific user. /// </summary> /// <param name="user">Owner of the documents</param> /// <returns>List of all documents that belong to the user.</returns> public List<Document> GetAllUsersDocuments(User user) { //List<int> documentIDList = dbCon.SelectDocumentsFromUser(user); List<int> documentIDList = dbCon.GetUserdocumentsByUser(user); List<Document> usersDocuments = new List<Document>(); foreach (int i in documentIDList) usersDocuments.Add(OpenSharedDocument(i, user)); return usersDocuments; }
// Default constructor for creating a document object. public Document(string text, string title, string path, User owner) { this.text = text; this.title = title; this.owner = owner; this.path = path; this.images = new List<Picture>(); log = new Document.DocumentLog(owner); CreateId(); }
/** * Asks the server for a list of all the projects for this user */ public static List<Project> GetAllProjectsAvailable(User user) { List<Project> result; using (SliceOfPieServiceClient serviceClient = new SliceOfPieServiceClient()) { result = serviceClient.GetAllProjectsOnServer(user); } return result; }
/** * Shares the changes the user have made in a project * to the server, and the server shares changes from other * users back. This synchronization is done through a series * of method calls to the server. */ public static void SyncWithServer(Project project, User user) { // Preparing transfer data // finding all modified documents to send List<DocumentStruct> docs = new List<DocumentStruct>(); Folder.GetAllStructs(project, docs); // fill up the list List<DocumentStruct> docsToSend = new List<DocumentStruct>(); foreach (DocumentStruct d in docs) { if (d.Modified) docsToSend.Add(d); } // Call the server using (SliceOfPieServiceClient serviceClient = new SliceOfPieServiceClient()) { // Tell the server who we are and what project we want to sync Project newProj = serviceClient.StartSync(user, project.Id); // If null is returned, means that the project was deleted // or you've lost access to this project, so we delete it if (newProj == null) Controller.DeleteProject(project.Id); else { // Send all modified documents one at a time foreach (DocumentStruct d in docsToSend) { Document document = Controller.OpenDocument(project.Id, d.Id); serviceClient.SendDocument(document); if (document.Deleted) Controller.DeleteDocument(project.Id, document.Id); } // Keep getting new documents from server until there are none remaining while (true) { // Grab a new document from the server Document newDoc = serviceClient.GetUpdatedDocument(); // if null is returned, means that there are no new docs left if (newDoc == null) break; // Delete the old copy and write the new one instead Storage.DeleteDocument(project.Id, newDoc.Id); Storage.WriteToFile(newProj, newDoc, true); } // We're done syncing, close the session with the server serviceClient.StopSync(); } } }
public static void SaveDocument(Project proj, Document doc, User user, bool fromServer = false) { // If the document exists in the storage, merge old version with newer verion. // Otherwise just save it to the storage. Document docInStorage = Storage.ReadFromFile(proj.Id, doc.Id); if (docInStorage == null) Storage.WriteToFile(proj, doc, fromServer); else { docInStorage.MergeWith(doc, user); Storage.WriteToFile(proj, docInStorage, fromServer); } }
public void DeleteFile(User user, string path) { Console.WriteLine("DELETE DOCUMENT() " + path); dbCon.DeleteDocumentByPath(path); if (File.Exists(path)) { try { File.Delete(path); } catch (IOException e) { Console.WriteLine(e.StackTrace); } } }
protected void Page_Load(object sender, EventArgs e) { user = facade.GetUser(Request.QueryString["username"].ToString()); string FullFilePath = "root/" + user.username + "/" + Request.QueryString["filename"]; List<string> docHistory = facade.GetDocumentHistory(user, FullFilePath); string[] history; foreach (string s in docHistory) { history = s.Split(','); historyRow.Text += "<tr> <td>" + history[0] + "</td><td>" + history[1] + "</td><td>" + history[2] + "</td><td>" + history[3] + "</td></tr>"; } }
public User GetUserByUsername(string username) { // Get data from database. string[] userValues = dbCon.SelectUser(username); // Store the users values. int newId = Convert.ToInt32(userValues[0]); string newName = userValues[1]; string newUsername = userValues[2]; string newPassword = userValues[3]; // Create user object. User user = new User(newId, newName, newUsername, newPassword); // Return user object. return user; }
public List<Project> GetAllProjectsOnServer(User user) { Console.WriteLine("{0} wants to see his projects", user); List<Project> projects = Storage.GetAllProjects(); List<Project> UserProjects = new List<Project>(); foreach(Project p in projects) { if (p != null) { if (user.ToString().ToLower().CompareTo(p.Owner.ToString().ToLower()) == 0 || p.SharedWith.Contains(user)) { UserProjects.Add(p); } } } return UserProjects; }
/// <summary> /// /// </summary> /// <param name="username"></param> /// <param name="password"></param> /// <param name="users"> /// Array of usernames /// </param> /// <param name="documents"> /// [0] = owner /// [1] = content /// [2] = filePath /// </param> /// <param name="permission"></param> public void ShareDocuments(string username, string password, string[] users, string userDoc, string permission) { User authUser = engine.dbCon.AuthenticateUser(username, password); if (authUser != null) { User user = engine.userhandler.GetUserByUsername(username); Document tmpDocument = engine.userhandler.docHandler.GetDocumentByPath(user, userDoc); User[] newUsers = new User[users.Length]; for (int i = 0; i < users.Length; i++) { newUsers[i] = engine.userhandler.GetUserByUsername(users[i]); } Console.WriteLine("ClientSystemFacade2 - ShareDocuments"); engine.userhandler.docHandler.ShareDocument(user, tmpDocument, Permission.Permissions.Edit, tmpDocument.path, newUsers); } }
/// <summary> /// Checks the database for already existing document /// </summary> /// <param name="user">document's owner</param> /// <param name="doc">document instance</param> /// <returns>Wether the document exists or not. /// True = it exists /// False= it does not exist</returns> public bool CheckForDocument(User user, Document doc) { string query = "SELECT * FROM document WHERE owner='" + user.username + "' AND id='" + doc.documentId + "'"; List<int> counterList = new List<int>(); MySqlDataReader reader = ExecuteReader(query); while (reader.Read()) { counterList.Add((int)reader["id"]); } reader.Close(); CloseConnection(); if (counterList.Count > 0) return true; else return false; }
public ActionResult LogOn(User user) { if (ModelState.IsValid) { //if (Membership.ValidateUser(user.Email, user.Password)) { if (um.ValidateLogin(user.Email, user.Password)) { FormsAuthentication.SetAuthCookie(user.Email, false); //if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/") // && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\")) { // return Redirect(returnUrl); //} else { return RedirectToAction("Overview", "Project"); //} } else { ModelState.AddModelError("", "The user name or password provided is incorrect."); } } // If we got this far, something failed, redisplay form return View(user); }
protected void Page_Load(object sender, EventArgs e) { user = facade.GetUser(Request.QueryString["username"].ToString()); string FullFilePath = "root/" + user.username + "/" + Request.QueryString["filename"]; //Response.Write("USER: "******" FILENAME: " + Request.QueryString["filename"]); //Request.QueryString["param1"].ToString(); //string FullFilePath = facade.GetDocumentByPath(user, Request.QueryString["filename"]).path; FileInfo file = new FileInfo(FullFilePath); if (file.Exists) { Response.ContentType = "text/html"; //"application/vnd.ms-word"; Response.AddHeader("Content-Disposition", "inline; filename=\"" + file.Name + "\""); Response.AddHeader("Content-Length", file.Length.ToString()); Response.TransmitFile(file.FullName); } }
public static List<Project> GetAllProjectsForUser(User user) { // Gets all projects currently on the filesystem. List<Project> allProjects = Storage.GetAllProjects(); List<Project> projectsToReturn = new List<Project>(); // Adds the projecs the user either owns or that are shared with him. foreach (Project p in allProjects) { if (String.Compare(p.Owner.ToString().ToLower(), user.ToString().ToLower()) == 0) projectsToReturn.Add(p); else { foreach (User u in p.SharedWith) { if (String.Compare(u.ToString().ToLower(), user.ToString().ToLower()) == 0) projectsToReturn.Add(p); } } } return projectsToReturn; }
/// <summary> /// Checks if there is an instance of a user in the database matching the input. /// </summary> /// <param name="username">users username</param> /// <param name="password">users password</param> /// <returns>a User instance, if null there is no user maching in the database, else returns the instance for confirmation</returns> public User AuthenticateUser(string username, string password) { User user = null; string query = "SELECT * FROM user WHERE username = '******' AND password = '******'"; MySqlDataReader reader = ExecuteReader(query); if (reader != null) { while (reader.Read()) { int id = (int)reader[0]; string name = (string)reader[1]; string userName = (string)reader[2]; string passWord = (string)reader[3]; user = new User(id, name, userName, passWord); } } //Console.WriteLine("READER " + reader[1] + " " + reader[2] + " " + reader[3]); reader.Close(); CloseConnection(); return user; }
/// <summary> /// Deletes a folder. /// Recursively deletes a folder, all subfolders and /// all files that the folder might contain. /// </summary> /// <param name="user">Name of the user (used for root directory)</param> /// <param name="path">The folder to delete.</param> public void DeleteFolder(User user, string path) { string[] splittedPath = path.Split('/'); string dir = ""; // Check if a full path is given or just the folder name. if (splittedPath[0].Contains("root") && splittedPath[1].Contains(user.username)) { dir = path; } else { dir = dir = "root/" + user.username + "/" + path; } // Check if the folder exists. if (Directory.Exists(dir)) { // Check if directory has sub directories while (DirHasSubfolder(dir).Count() > 0) { // Recursively delete all subdirectories foreach (string s in DirHasSubfolder(dir)) { DeleteFolder(user, s); } } // Check if a directory has any files while (DirHasFiles(dir).Count() > 0) { foreach (string s in DirHasFiles(dir)) { File.Delete(s); } } Directory.Delete(dir); } }
// Constructor that allows specifying when the Entry is from. public Entry(User u, string desc, List<string> log, DateTime time) { user = u; description = desc; changeLog = log; this.time = time; }
// Constructor that creates an Entry with the parameters specified, as well // as defining the DateTime to be the second the entry object was created. public Entry(User u, string desc, List<string> log) { time = DateTime.Now; user = u; description = desc; changeLog = log; }
public DocumentLog(User user) { entries = new List<Entry>(); List<string> emptyLog = new List<string>(); entries.Add(new Entry(user,"created the document",emptyLog)); }
// This functions takes a newer version of this document, and merges it with this one // acording to "Simple Merge Policy" given in slice-of-pie.pdf. // It also updated all other fields in the document according to the new version // and generates a log based on the changes. // MergeWith returns a bool as well, it returns false if the ID of the updated // document is not the same as this documents ID, otherwise it returns true public bool MergeWith(Document doc, User user) { if (this.id != doc.id) return false; List<string> changes = new List<string>(); // Create original and latest arrays. ( step 1 ) string[] original = this.GetTextAsArray(); string[] latest = doc.GetTextAsArray(); // Create merged array ( made as a list instead ). ( step 2 ) List<string> merged = new List<string>(); // Definer o and n index, which point to lines in the original and latest arrays. ( step 3 ) int o = 0; int n = 0; bool done = false; Console.Out.WriteLine("Started merge."); // While loop that continues untill the end of both versions of the document have been reached. while (!done) { Console.Out.WriteLine("New round in while loop"); // All remaining lines in latest are new. ( step 4 ) if ((o == original.Length) && (n != latest.Length)) { Console.Out.WriteLine("Step 4"); for (int N=n; N < latest.Length; N++) { changes.Add("+L"+N+": "+latest[N]); merged.Add(latest[N]); n++; } } // All remaining lines in original have been removed. ( step 5 ) else if ((o != original.Length) && (n == latest.Length)) { Console.Out.WriteLine("Step 5"); for (int O=o; O < original.Length; O++) { changes.Add("-L"+O+": "+original[O]); } o = original.Length; } // No changes have occured in this line ( step 6 ) else if (String.Compare(original[o], latest[n]) == 0) { Console.Out.WriteLine("Step 6"); merged.Add(original[o]); n++; o++; } else if (String.Compare(original[o], latest[n]) != 0) { List<string> temp = new List<string>(); bool found = false; int t = 0; for (int N = n; N < latest.Length; N++) { temp.Add(latest[N]); if (String.Compare(original[o], latest[N]) == 0) { found = true; t = N; break; } } // Line has been removed ( Step 7.b ) if (found == false) { Console.Out.WriteLine("Step 7.b"); changes.Add("-L"+o+": "+original[o]); o++; } // All lines in between have been added ( step 7.c ) else { Console.Out.WriteLine("Step 7.c"); int counter = 0; foreach (string s in temp) { if (!(counter == temp.Count-1)) changes.Add("+L"+n+": "+latest[n]); merged.Add(s); n++; counter++; } o++; n = t + 1; } } if (((o == original.Length) && (n == latest.Length))) { done = true; } } Console.Out.WriteLine("Merge complete"); StringBuilder newTextBuilder = new StringBuilder(); for (int i = 0; i < merged.Count; i++) { if (!(i == merged.Count-1)) newTextBuilder.AppendFormat(merged[i] + "\n"); else newTextBuilder.AppendFormat(merged[i]); } text = newTextBuilder.ToString(); // Check and see if the document has been marked for deletion this.deleted = doc.deleted; // A log that will document what changes have been made to the document. List<string> changeLog = new List<string>(); bool titleChanged = false; bool pathChanged = false; bool textChanged = (!(changes.Count==0)); bool picturesAdded = false; bool picturesRemoved = false; List<Picture> imagesAdded = new List<Picture>(); List<Picture> imagesRemoved = new List<Picture>(); // Has title been changed? if (String.Compare(doc.Title, this.Title) != 0) { changeLog.Add("Title has been changed from '" + this.Title + "' to '" + doc.Title + "'"); this.title = doc.Title; titleChanged = true; } // Has path been changed? if (String.Compare(doc.Path, this.Path) != 0) { changeLog.Add("Path has been changed from '" + this.Path + "' to '" + doc.Path + "'"); this.path = doc.Path; pathChanged = true; } // Are there any new pictures? foreach (Picture pic in doc.Images) { if (!(this.Images.Contains(pic))) { picturesAdded = true; imagesAdded.Add(pic); } } // Are any of the old pictures removed? foreach (Picture pic in this.Images) { if (!(doc.Images.Contains(pic))) { picturesRemoved = true; imagesRemoved.Add(pic); } } foreach (Picture pic in imagesRemoved) { images.Remove(pic); pic.Image.Dispose(); } foreach (Picture pic in imagesAdded) { images.Add(pic); } // If there were pictures added, add it to the changelog. if (picturesAdded) { StringBuilder imageLineBuilder = new StringBuilder(); for (int i = 0; i < imagesAdded.Count; i++) { if (i == imagesAdded.Count - 1) imageLineBuilder.AppendFormat(imagesAdded[i].Id); else imageLineBuilder.AppendFormat(imagesAdded[i].Id + ","); } changeLog.Add("Following pictures were added: " + imageLineBuilder.ToString()); } // If there were pictures removed, add it to the changelog. if (picturesRemoved) { StringBuilder imageLineBuilder = new StringBuilder(); for (int i = 0; i < imagesRemoved.Count; i++) { if (i == imagesRemoved.Count - 1) imageLineBuilder.AppendFormat(imagesRemoved[i].Id); else imageLineBuilder.AppendFormat(imagesRemoved[i].Id + ", "); } changeLog.Add("Following pictures were removed: " + imageLineBuilder.ToString()); } // Finally, add changes to the text to the changelog. if (textChanged) changeLog.Add("Changes to the documents text:"); foreach (string change in changes) { changeLog.Add(change); } string titleString = ""; string pathString = ""; string textString = ""; string pictureString = ""; string masterString = "Changed the document's: "; if (titleChanged) titleString = "Title. "; if (textChanged) textString = "Text. "; if (pathChanged) pathString = "Path. "; if ((picturesAdded || picturesRemoved)&&textChanged) pictureString = " And changed attached pictures"; else if ((picturesAdded || picturesRemoved) && textChanged == false) { masterString = ""; pictureString = "Changed the attached pictures"; } this.Log.AddEntry(new DocumentLog.Entry(user, masterString + titleString + textString + pathString + pictureString, changeLog)); return true; }
// Creates and returns a document in it's complete version from a file on the file system. public static Document CreateDocumentFromFile(string id, string text, string title, bool modified, bool deleted, User owner, List<Picture> pictures, string path, Document.DocumentLog log) { Document doc = new Document(); doc.id = id; doc.text = text; doc.title = title; doc.owner = owner; doc.path = path; doc.log = log; doc.images = pictures; doc.modified = modified; doc.deleted = deleted; return doc; }