ask the user to confirm if he wants to open the file

This commit is contained in:
Maxim.Mossienko
2016-04-20 13:12:43 +02:00
parent bdb1cd4197
commit 5bb322a454
5 changed files with 43 additions and 14 deletions
@@ -23,6 +23,7 @@ import com.intellij.openapi.fileEditor.OpenFileDescriptor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectManager;
import com.intellij.openapi.project.ProjectUtil;
import com.intellij.openapi.util.Ref;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.util.text.StringUtilRt;
@@ -44,6 +45,7 @@ import org.jetbrains.builtInWebServer.WebServerPathToFileManager;
import org.jetbrains.concurrency.AsyncPromise;
import org.jetbrains.concurrency.Promise;
import javax.swing.*;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.ConcurrentLinkedQueue;
@@ -169,16 +171,33 @@ class OpenFileHttpService extends RestService {
@NotNull
Promise<Void> openFile(@NotNull OpenFileRequest request) {
String path = FileUtil.expandUserHome(request.file);
final File file = new File(FileUtil.toSystemDependentName(path));
String systemIndependentName = FileUtil.toSystemIndependentName(FileUtil.expandUserHome(request.file));
final File file = new File(systemIndependentName);
if (file.isAbsolute()) {
if (com.intellij.ide.impl.ProjectUtil.isRemotePath(systemIndependentName)) {
Ref<Boolean> confirmLoadingRemoteFile = new Ref<>();
try {
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
boolean value = com.intellij.ide.impl.ProjectUtil
.confirmLoadingFromRemotePath(systemIndependentName, "warning.load.file.from.share", "title.load.file.from.share");
confirmLoadingRemoteFile.set(value);
}
});
} catch (Throwable ignored) {}
if (confirmLoadingRemoteFile.get() != Boolean.TRUE) {
return Promise.reject(NOT_FOUND);
}
}
return openAbsolutePath(file, request);
}
// we don't want to call refresh for each attempt on findFileByRelativePath call, so, we do what ourSaveAndSyncHandlerImpl does on frame activation
RefreshQueue queue = RefreshQueue.getInstance();
queue.cancelSession(refreshSessionId);
OpenFileTask task = new OpenFileTask(FileUtil.toCanonicalPath(FileUtil.toSystemIndependentName(path), '/'), request);
OpenFileTask task = new OpenFileTask(FileUtil.toCanonicalPath(systemIndependentName, '/'), request);
requests.offer(task);
RefreshSession session = queue.createSession(true, true, new Runnable() {
@Override
@@ -18,6 +18,7 @@ package org.jetbrains.ide;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.ModalityState;
import com.intellij.platform.ProjectSetReader;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.FullHttpRequest;
@@ -63,13 +64,13 @@ public class ProjectSetRequestHandler extends RestService {
@Override
public String execute(@NotNull QueryStringDecoder urlDecoder, @NotNull FullHttpRequest request, @NotNull ChannelHandlerContext context) throws IOException {
final JsonObject descriptor = new JsonParser().parse(createJsonReader(request)).getAsJsonObject();
ApplicationManager.getApplication().invokeLater(new Runnable() {
ApplicationManager.getApplication().invokeAndWait(new Runnable() {
@Override
public void run() {
new ProjectSetReader().readDescriptor(descriptor, null);
activateLastFocusedFrame();
}
});
}, ModalityState.any());
sendOk(request, context);
return null;
}
@@ -35,7 +35,7 @@ public class IdeBundle {
}
private static Reference<ResourceBundle> ourBundle;
@NonNls private static final String BUNDLE = "messages.IdeBundle";
@NonNls public static final String BUNDLE = "messages.IdeBundle";
private IdeBundle() {
}
@@ -44,6 +44,7 @@ import com.intellij.util.SystemProperties;
import org.jdom.JDOMException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.PropertyKey;
import javax.swing.*;
import java.awt.*;
@@ -178,12 +179,7 @@ public class ProjectUtil {
}
if (isRemotePath(path) && !RecentProjectsManager.getInstance().hasPath(path)) {
final Window window = getActiveFrameOrWelcomeScreen();
final String msg = IdeBundle.message("warning.load.project.from.share", path);
final String title = IdeBundle.message("title.load.project.from.share");
final Icon icon = Messages.getWarningIcon();
final int answer = window == null ? Messages.showYesNoDialog(msg, title, icon) : Messages.showYesNoDialog(window, msg, title, icon);
if (answer != Messages.YES) {
if (!confirmLoadingFromRemotePath(path, "warning.load.project.from.share", "title.load.project.from.share")) {
return null;
}
}
@@ -210,6 +206,17 @@ public class ProjectUtil {
return project;
}
public static boolean confirmLoadingFromRemotePath(@NotNull String path,
@NotNull @PropertyKey(resourceBundle = IdeBundle.BUNDLE) String msgKey,
@NotNull @PropertyKey(resourceBundle = IdeBundle.BUNDLE) String titleKey) {
final Window window = getActiveFrameOrWelcomeScreen();
final String msg = IdeBundle.message(msgKey, path);
final String title = IdeBundle.message(titleKey);
final Icon icon = Messages.getWarningIcon();
final int answer = window == null ? Messages.showYesNoDialog(msg, title, icon) : Messages.showYesNoDialog(window, msg, title, icon);
return answer == Messages.YES;
}
private static Window getActiveFrameOrWelcomeScreen() {
Window window = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusedWindow();
if (window != null) return window;
@@ -223,7 +230,7 @@ public class ProjectUtil {
return null;
}
private static boolean isRemotePath(@NotNull String path) {
public static boolean isRemotePath(@NotNull String path) {
return path.contains("//") || path.contains("\\\\");
}
@@ -1193,4 +1193,6 @@ go.to.class.dumb.mode.message=Go To Class action is not available until indices
edit.custom.settings.confirm=File \n''{0}''\n does not exist. Create?
warning.load.project.from.share=You are opening a project from a network share. Do you trust this location?\n{0}
title.load.project.from.share=Loading Project From Network
title.load.project.from.share=Loading Project From Network
warning.load.file.from.share=You are opening a file from a network share. Do you want to continue?\n{0}
title.load.file.from.share=Loading File From Network