source: trunk/src/org/expeditee/auth/account/Contacts.java@ 1387

Last change on this file since 1387 was 1387, checked in by bln4, 5 years ago

When sharing contact details. If we can detect that the recipient is present on the same filesystem as the sender (due to the presence of a resources-recipient directory) then instead of providing directions, just copy the credentials-sender directory.

File size: 3.5 KB
Line 
1package org.expeditee.auth.account;
2
3import java.awt.Desktop;
4import java.io.File;
5import java.io.IOException;
6import java.net.URI;
7import java.net.URISyntaxException;
8import java.net.URL;
9import java.nio.file.Files;
10import java.nio.file.Path;
11import java.nio.file.Paths;
12
13import org.expeditee.gui.FrameIO;
14import org.expeditee.settings.UserSettings;
15
16public class Contacts {
17
18 public static String distributeContactDetails(String username) {
19 StringBuilder sb = new StringBuilder();
20 String nl = System.getProperty("line.separator");
21 String me = UserSettings.UserName.get();
22 Path profilePath = Paths.get(FrameIO.PROFILE_PATH).resolve(me);
23 Path resourcesUsernamePath = Paths.get(FrameIO.PARENT_FOLDER).resolve("resources-" + username);
24 if (resourcesUsernamePath.toFile().exists()) {
25 sb.append("Detected the presence of the user " + username + " present in your Expeditee file system. Contact details automatically distributed.");
26 Path credentialsDirectoryToSharePath = profilePath.resolve(me + "-credentials");
27 Path partnerContactsDirectory = resourcesUsernamePath.resolve("contacts");
28 try {
29 copyFileTree(credentialsDirectoryToSharePath, partnerContactsDirectory.resolve(me + "-credentials"));
30 return sb.toString();
31 } catch (IOException e) {
32 sb = new StringBuilder();
33 }
34 }
35
36 try {
37 Desktop.getDesktop().open(profilePath.toFile());
38 sb.append("1. We have opened your profile directory for you. Tab to it and right click on the " + me + "-credentials directory." + nl);
39 } catch (IOException e) {
40 sb.append("1. Open your profile directory (" + profilePath + ") and right click on the " + me + "-credentials directory." + nl);
41 }
42
43 sb.append("2. Click on 'Share with Google Drive'." + nl);
44 sb.append("3. Enter " + username + "'s in the text box." + nl);
45 sb.append("4. Click on the button to the right of the text box in order to set the share to 'View Only'." + nl);
46 sb.append("5. Optionally add a note for " + username + " to read. Click the send button.");
47
48 return sb.toString();
49 }
50
51 public static String addContactDetails(String username) {
52 StringBuilder sb = new StringBuilder();
53 String nl = System.getProperty("line.separator");
54 String me = UserSettings.UserName.get();
55 String driveSharedWithMeString = "https://drive.google.com/drive/shared-with-me";
56 try {
57 URI driveSharedWithMeURL = new URL(driveSharedWithMeString).toURI();
58 Desktop.getDesktop().browse(driveSharedWithMeURL);
59 sb.append("1. We have opened Google Drive to the 'Shared With Me' page in your browser. Right click on " + username + "-credentials and click on 'Add To My Drive'." + nl);
60 } catch (IOException | URISyntaxException e) {
61 sb.append("1. Open your web browser to " + driveSharedWithMeString + ". Right click on " + username + "-credentials and click on 'Add To My Drive'." + nl);
62 }
63
64 sb.append("2. Click organise and navigate the dialog to resources-" + me + "/contacts and click the 'Move Here' button to relocate your copy of " + username + "'s credentials folder.");
65
66 return sb.toString();
67 }
68
69 private static void copyFileTree(Path source, Path target) throws IOException {
70 if (source.toFile().isDirectory()) {
71 if (!target.toFile().exists()) {
72 Files.copy(source, target);
73 }
74 File[] files = source.toFile().listFiles();
75 for (File file: files) {
76 Path asPath = Paths.get(file.getAbsolutePath());
77 copyFileTree(asPath, target.resolve(asPath.getFileName()));
78 }
79 } else {
80 Files.copy(source, target);
81 }
82 }
83}
Note: See TracBrowser for help on using the repository browser.