summaryrefslogtreecommitdiff
path: root/plugins/svn4idea/src/org/jetbrains/idea/svn/actions/CleanupWorker.java
blob: 7623f083eb8600191a88bdf09c662d4edcba10db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
 * Copyright 2000-2014 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jetbrains.idea.svn.actions;

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.progress.Task;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.vcs.AbstractVcsHelper;
import com.intellij.openapi.vcs.VcsException;
import com.intellij.openapi.vcs.changes.BackgroundFromStartOption;
import com.intellij.openapi.vcs.changes.VcsDirtyScopeManager;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.idea.svn.SvnBundle;
import org.jetbrains.idea.svn.SvnVcs;
import org.tmatesoft.svn.core.SVNCancelException;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.wc.ISVNEventHandler;
import org.tmatesoft.svn.core.wc.SVNEvent;

import java.io.File;
import java.util.LinkedList;
import java.util.List;

public class CleanupWorker {
  protected VirtualFile[] myRoots;
  private final Project myProject;
  private final String myTitleKey;

  public CleanupWorker(final VirtualFile[] roots, final Project project, final String titleKey) {
    myRoots = roots;
    myProject = project;
    myTitleKey = titleKey;
  }

  public void execute() {
    ApplicationManager.getApplication().saveAll();

    chanceToFillRoots();
    if (myRoots.length == 0) return;

    final List<Pair<VcsException, VirtualFile>> exceptions = new LinkedList<Pair<VcsException, VirtualFile>>();
    final SvnVcs vcs = SvnVcs.getInstance(myProject);

    final Task.Backgroundable task = new Task.Backgroundable(myProject, SvnBundle.message(myTitleKey), true, BackgroundFromStartOption.getInstance()) {
      public void run(@NotNull final ProgressIndicator indicator) {
        indicator.setIndeterminate(true);
        VirtualFile currentRoot;
        for (VirtualFile root : myRoots) {
          currentRoot = root;
          try {
            final File path = new File(root.getPath());

            indicator.setText(SvnBundle.message("action.Subversion.cleanup.progress.text", path));
            ISVNEventHandler handler = new ISVNEventHandler() {
              @Override
              public void handleEvent(SVNEvent event, double progress) throws SVNException {
              }

              @Override
              public void checkCancelled() throws SVNCancelException {
                if (indicator.isCanceled()) throw new SVNCancelException();
              }
            };

            vcs.getFactory(path).createCleanupClient().cleanup(path, handler);
          }
          catch (VcsException ex) {
            exceptions.add(Pair.create(ex, currentRoot));
          }
        }
      }

      @Override
      public void onCancel() {
        onSuccess();
      }

      @Override
      public void onSuccess() {
        if (myProject.isDisposed()) {
          return;
        }
        final VcsDirtyScopeManager manager = VcsDirtyScopeManager.getInstance(myProject);
        ApplicationManager.getApplication().invokeLater(new Runnable() {
          public void run() {
            ApplicationManager.getApplication().runWriteAction(new Runnable() {
              public void run() {
                if (myProject.isDisposed()) {
                  return;
                }
                for (final VirtualFile root : myRoots) {
                  root.refresh(false, true);
                }
              }
            });
          }
        });
        for (final VirtualFile root : myRoots) {
          if (root.isDirectory()) {
            manager.dirDirtyRecursively(root);
          } else {
            manager.fileDirty(root);
          }
        }

        if (! exceptions.isEmpty()) {
          final List<VcsException> vcsExceptions = new LinkedList<VcsException>();
          for (Pair<VcsException, VirtualFile> pair : exceptions) {
            final VcsException exception = pair.first;
            vcsExceptions.add(new VcsException(SvnBundle.message("action.Subversion.cleanup.error.message",
                                                              FileUtil.toSystemDependentName(pair.second.getPath()),
                                                              ((exception == null) ? "" : exception.getMessage()))));
          }
          final AbstractVcsHelper helper = AbstractVcsHelper.getInstance(myProject);
          helper.showErrors(vcsExceptions, SvnBundle.message(myTitleKey));
        }
      }
    };

    ProgressManager.getInstance().run(task);
  }

  protected void chanceToFillRoots() {
  }
}