[hg] don't check for incoming/outgoing changes if there is no default repository.
[idea/community.git] / plugins / hg4idea / src / org / zmlx / hg4idea / command / HgRemoteChangesetsCommand.java
1 /*
2  * Copyright 2000-2011 JetBrains s.r.o.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.zmlx.hg4idea.command;
17
18 import com.intellij.notification.Notification;
19 import com.intellij.notification.NotificationListener;
20 import com.intellij.notification.NotificationType;
21 import com.intellij.notification.Notifications;
22 import com.intellij.openapi.diagnostic.Logger;
23 import com.intellij.openapi.options.ShowSettingsUtil;
24 import com.intellij.openapi.project.Project;
25 import com.intellij.openapi.vfs.VirtualFile;
26 import org.jetbrains.annotations.NotNull;
27 import org.zmlx.hg4idea.HgProjectSettings;
28 import org.zmlx.hg4idea.HgVcs;
29 import org.zmlx.hg4idea.execution.HgCommandExecutor;
30 import org.zmlx.hg4idea.execution.HgCommandResult;
31
32 import javax.swing.event.HyperlinkEvent;
33 import java.util.List;
34
35 /**
36  * Common ancestor for HgIncomingCommand and HgOutgoingCommand - changeset commands which need connection to the server.
37  * @author Kirill Likhodedov
38  */
39 public abstract class HgRemoteChangesetsCommand extends HgChangesetsCommand {
40
41   private static final Logger LOG = Logger.getInstance(HgRemoteChangesetsCommand.class);
42
43   public HgRemoteChangesetsCommand(Project project, String command) {
44     super(project, command);
45   }
46
47   @Override
48   protected void addArguments(List<String> args) {
49     args.add("--newest-first");
50   }
51
52   @Override
53   protected boolean isSilentCommand() {
54     return true;
55   }
56
57   protected String getRepositoryUrl(VirtualFile repo) {
58     return new HgShowConfigCommand(project).getDefaultPath(repo);
59   }
60
61   @Override
62   protected HgCommandResult executeCommand(VirtualFile repo, List<String> args) {
63     String repositoryURL = getRepositoryUrl(repo);
64     if (repositoryURL == null) {
65       LOG.info("executeCommand no default path configured");
66       return null;
67     }
68     HgCommandResult result = new HgCommandExecutor(project).executeInCurrentThread(repo, command, args);
69     if (result == HgCommandResult.CANCELLED) {
70       final HgVcs vcs = HgVcs.getInstance(project);
71       Notifications.Bus.notify(new Notification(HgVcs.NOTIFICATION_GROUP_ID, "Checking for incoming/outgoing changes disabled",
72                                                 "Authentication is required to check incoming/outgoing changes in " + repositoryURL +
73                                                 "<br/>You may enable checking for changes <a href='#'>in the Settings</a>."
74         , NotificationType.ERROR, new NotificationListener() {
75           @Override
76           public void hyperlinkUpdate(@NotNull Notification notification, @NotNull HyperlinkEvent event) {
77             ShowSettingsUtil.getInstance().showSettingsDialog(project, vcs.getConfigurable());
78           }
79         }), project);
80       final HgProjectSettings projectSettings = vcs.getProjectSettings();
81       projectSettings.setCheckIncoming(false);
82       projectSettings.setCheckOutgoing(false);
83     }
84     return result;
85   }
86
87 }