/// <summary> /// Gets the file path that a user's document is locally stored at. /// </summary> /// <param name="doc">Userdocument from the server</param> /// <returns>The file path to the document from the local root directory</returns> private string FetchRelativeFilePath(ServiceReference.ServiceUserdocument doc) { StringBuilder sb = new StringBuilder(); ServiceReference.ServiceFolder folder = null; using (ServiceReference.Service1Client proxy = new ServiceReference.Service1Client()) { folder = proxy.GetFolder(doc.folderId); } while (folder != null || folder.parentFolderId != null) { if (folder.parentFolderId == null) { break; } else { sb.Insert(0, "\\" + folder.name); } using (ServiceReference.Service1Client proxy = new ServiceReference.Service1Client()) { folder = proxy.GetFolder((int)folder.parentFolderId); } } String userEmail = session.Email; sb.Insert(0, ""); return(sb.ToString()); }
/// <summary> /// Synchronizes a specific document to push changes to the server. If another user made changes meanwhile, a conflict arises and a difference view is displayed. /// </summary> /// <param name="document">The document content from the UI</param> public void SyncDocument(FlowDocument document) { if (System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable()) { //Metadata //0: docid -> docid 11 //1: userid -> userid 11 //2: timestamp -> timestamp 12-12-2012 12:18:19 object[] metadata = LocalPersistenceHandler.RetrieveMetadataFromFile(session.CurrentDocumentPath); int documentID = (int)metadata[0]; DateTime baseDocumentCreationTime = (DateTime)metadata[2]; //int folderID = (int)metadata[3]; StringBuilder sb = new StringBuilder(); string metadataString = Metadata.GenerateMetadataString(documentID, session.UserID, DateTime.UtcNow);//, folderID); sb.Append(metadataString); sb.AppendLine(); //generate xaml for the document String xaml = System.Windows.Markup.XamlWriter.Save(document); sb.Append(xaml); //get the text from the document String content = new TextRange(document.ContentStart, document.ContentEnd).Text; String[][] responseArrays = null; //connect to the websevice using (ServiceReference.Service1Client proxy = new ServiceReference.Service1Client()) { //push the current document responseArrays = proxy.SyncDocument(session.UserID, documentID, session.CurrentDocumentPath, sb.ToString(), session.CurrentDocumentTitle, content); } if (responseArrays == null) //if there is no conflict { if (documentID == 0) { //connect to the websevice using (ServiceReference.Service1Client proxy = new ServiceReference.Service1Client()) { documentID = proxy.GetDocumentId(session.UserID, session.CurrentDocumentTitle); } } //save document with new metadata - basedocument localPersistence.SaveDocumentToFile(document, Metadata.ReplaceDocumentIDInMetadata(metadataString, documentID)); session.CurrentDocumentID = documentID; } else //if there is a conflict { gui.SetupMergeView(responseArrays); } } else { //No web connection } }
/// <summary> /// Method to remove a users participation in a document - the document still lives on the server, but the currently online user can no longer access it /// </summary> /// <param name="item">TreeViewItem represetation of the item which is to be removed</param> /// <param name="items">An item collection which contains the soon to be removed document</param> public void DeleteDocument(System.Windows.Controls.TreeViewItem item, System.Windows.Controls.ItemCollection items) { //get document id for the file int documentId = Metadata.FetchDocumentIDFromFileContent(localPersistence.GetFileContent(item.Tag.ToString())); using (ServiceReference.Service1Client proxy = new ServiceReference.Service1Client()) { //delete the users document reference proxy.DeleteDocumentReference(session.UserID, documentId); } //delete the file locally localPersistence.DeleteFile(item.Tag.ToString()); UpdateExplorerView(); }
/// <summary> /// Add file to server /// </summary> /// <param name="filePath">Path to the file which should be added to the server</param> private void AddDocumentToServer(String filePath) { //load file content String content = localPersistence.GetFileContent(filePath); //fetch filename String fileName = filePath.Substring(filePath.LastIndexOf("\\") + 1, (filePath.IndexOf(".txt") - filePath.LastIndexOf("\\") - 1)); //Connect to webservice using (ServiceReference.Service1Client proxy = new ServiceReference.Service1Client()) { //add document online proxy.AddDocumentWithUserDocument(fileName, session.UserID, filePath, content); } }
/// <summary> /// Pushes the merged document revision to the server to resolve the conflict. /// </summary> /// <param name="document">The document content from the UI</param> public void SaveMergedDocument(FlowDocument document) { Object[] oldMetadata = LocalPersistenceHandler.RetrieveMetadataFromFile(session.CurrentDocumentPath); int documentid = (int)oldMetadata[0]; int userid = session.UserID; DateTime timestamp = DateTime.UtcNow; //int folderid = (int)oldMetadata[3]; String metadata = Metadata.GenerateMetadataString(documentid, userid, timestamp);//, folderid); String xamlContent = System.Windows.Markup.XamlWriter.Save(document); String content = metadata + xamlContent; using (ServiceReference.Service1Client proxy = new ServiceReference.Service1Client()) { proxy.AddDocumentRevision(session.UserID, documentid, content); } localPersistence.SaveDocumentToFile(content, session.CurrentDocumentPath); }
/// <summary> /// Method to register a new user in the database /// Using an email and 2 identical passwords, in order protect against typoes /// </summary> /// <param name="email">The users email</param> /// <param name="passUnencrypted1">unencrypted first password</param> /// <param name="passUnencrypted2">unencrypted second password</param> /// <returns>Wheter the creation of the user was successful - return -1 if it was unsuccesful</returns> public int RegisterUser(string email, string passUnencrypted1, string passUnencrypted2) { //boolean which will be returned int successful = -1; //Check if something have been entered as email if (email != null && email.Length > 0) { //Check that something has been entered as been entered as passwords and check that the two passwords are identical if (passUnencrypted1 != null && passUnencrypted1.Length > 0 && passUnencrypted2 != null && passUnencrypted1 == passUnencrypted2) { //encrypt password string pass = Security.EncryptString(passUnencrypted1); //connect to webservice using (ServiceReference.Service1Client proxy = new ServiceReference.Service1Client()) { //register user successful = proxy.AddUser(email, pass); } if (successful == -1) //if the user already exsist { System.Windows.MessageBox.Show("User aldready exsists", "Creation error"); } else //the user has been created { System.Windows.MessageBox.Show("User with email: " + email + " have been successfully created", "Successful"); } } else //the passwords does not match { System.Windows.MessageBox.Show("User could not be created. Entered passwords does not match", "Creation error"); } } else //no email was entered { System.Windows.MessageBox.Show("Enter email address", "Creation error"); } return(successful); //return the result }
/// <summary> /// /// </summary> /// <param name="documentId"></param> /// <returns> /// [x]a new revision /// [x][0]timestamp /// [x][1]editor name /// [x][2]filecontent with metadata /// </returns> public string[][] GetAllDocumentRevisionsWithContent(int documentId) { string[][] returnArray = null; if (documentId > 0) { ServiceReference.ServiceDocumentrevision[] revisions = null; using (ServiceReference.Service1Client proxy = new ServiceReference.Service1Client()) { revisions = proxy.GetAllDocumentRevisionsByDocumentId(documentId); } //remove duplicates //here returnArray = new string[revisions.Length][]; using (ServiceReference.Service1Client proxy = new ServiceReference.Service1Client()) { for (int i = 0; i < revisions.Length; i++) { ServiceReference.ServiceDocumentrevision doc = revisions[i]; ServiceReference.ServiceDocument originalDocument = proxy.GetDocumentById(doc.documentId); String creationTime = doc.creationTime.ToString().Replace(":", "."); String filename = originalDocument.name + "_revision_" + creationTime; String content = proxy.GetDocumentContent(originalDocument.path, filename); string[] item = new string[3]; item[0] = doc.creationTime.ToString(); item[1] = proxy.GetUserById(doc.editorId).email; item[2] = Metadata.RemoveMetadataFromFileContent(content); returnArray[i] = item; } } } return(returnArray); }
public bool LoginUser(string email, string unencrytedPass) { bool successfulLogin = false; if (email != null && email.Length > 0 && unencrytedPass != null && unencrytedPass.Length > 0) { //encrypt passowrd String pass = Security.EncryptString(unencrytedPass); ServiceReference.ServiceUser user = null; //connect to webservice using (ServiceReference.Service1Client proxy = new ServiceReference.Service1Client()) { //get the user id - -1 if no user exists user = proxy.GetUserByEmailAndPass(email, pass); } if (user != null) //login successful { //User logged in session.UserID = user.id; session.Email = email; session.RootFolderPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\sliceofpie\\" + email; Directory.CreateDirectory(session.RootFolderPath); System.Windows.MessageBox.Show("Logged in successfully", "Login"); successfulLogin = true; UpdateExplorerView(); } else { MessageBox.Show("Wrong email or password", "Unable to login"); } } else { MessageBox.Show("Enter email and password", "Login error"); } return(successfulLogin); }
/// <summary> /// Method to share the currently opened document with another user /// </summary> /// <param name="email">The email of the sue rwhich you want to share the doument</param> public void ShareDocument(string email) { if (email != null && email.Length > 0 && session.CurrentDocumentPath.Length > 0 && session.CurrentDocumentID != -1) { ServiceReference.ServiceUser shareUser = null; using (ServiceReference.Service1Client proxy = new ServiceReference.Service1Client()) { shareUser = proxy.GetUserByEmail(email); } if (shareUser != null) { using (ServiceReference.Service1Client proxy = new ServiceReference.Service1Client()) { proxy.AddUserDocumentInRoot(shareUser.id, session.CurrentDocumentID); } MessageBox.Show("Document shared with " + shareUser.email, "Share success"); } else { MessageBox.Show("User does not exist", "Email error"); } } }
/// <summary> /// Synchronizes all documents. This includes downloading of files from the server and overwritting of eventual exisiting local saves that can cause conflicts. /// Locally absent folders from documents downloaded from the server are created locally. /// </summary> public void SyncAllDocuments() { if (System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable()) { if (session.UserID != -1) //Check if user is logged in { ServiceReference.ServiceUserdocument[] documents; //Connect to webservice using (ServiceReference.Service1Client proxy = new ServiceReference.Service1Client()) { //Retrieve all documents the users documents documents = proxy.GetAllUserDocumentsByUserId(session.UserID); } if (documents != null) //check if any documents is found { //For each document found foreach (ServiceReference.ServiceUserdocument currentDoc in documents) { String relativeDirPath = FetchRelativeFilePath(currentDoc); String dirPath = session.RootFolderPath + "\\" + relativeDirPath; ServiceReference.ServiceDocument documentReference = null; using (ServiceReference.Service1Client proxy = new ServiceReference.Service1Client()) { //Get the original document from the server documentReference = proxy.GetDocumentById(currentDoc.documentId); } String filePath = dirPath + "\\" + documentReference.name + ".txt"; String content; //Connect to webservice using (ServiceReference.Service1Client proxy = new ServiceReference.Service1Client()) { //Get the content of the file on the server content = proxy.GetLatestDocumentContent(documentReference.id); //add a document revision in order to be able to detect merge conflicts later proxy.AddDocumentRevision(session.UserID, documentReference.id, content); } //Create the directories needed? Directory.CreateDirectory(Path.GetDirectoryName(filePath)); //Create a document locally with the content localPersistence.SaveDocumentToFile(content, filePath); } } else //No documents found by this userId { //loop through all the users folders and add all files to the server List <String> files = new List <String>(); foreach (String file in Directory.GetFiles(session.RootFolderPath)) { AddDocumentToServer(file); } foreach (String dir in Directory.GetDirectories(session.RootFolderPath)) { foreach (String file in Directory.GetFiles(dir)) { AddDocumentToServer(file); } } } } else { //not logged in } UpdateExplorerView(); } else { MessageBox.Show("No internet connection", "Internet connection error"); } }