summaryrefslogtreecommitdiff
path: root/plugins/cvs/cvs-plugin/src/com/intellij/cvsSupport2/application/CvsFileOperationsHandler.java
blob: 26c4e3f5d40071b0f20ae3408bafb87cf872daa8 (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
/*
 * Copyright 2000-2012 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 com.intellij.cvsSupport2.application;

import com.intellij.cvsSupport2.CvsUtil;
import com.intellij.cvsSupport2.CvsVcs2;
import com.intellij.openapi.command.undo.DocumentReference;
import com.intellij.openapi.command.undo.DocumentReferenceManager;
import com.intellij.openapi.command.undo.UndoManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vcs.AbstractVcs;
import com.intellij.openapi.vcs.ProjectLevelVcsManager;
import com.intellij.openapi.vfs.LocalFileOperationsHandler;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.ThrowableConsumer;
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.io.IOException;

/**
 * @author yole
 */
public class CvsFileOperationsHandler implements LocalFileOperationsHandler {
  private final Project myProject;
  private final CvsStorageSupportingDeletionComponent myComponent;
  private boolean myInternalDelete = false;

  public CvsFileOperationsHandler(final Project project, final CvsStorageSupportingDeletionComponent component) {
    myProject = project;
    myComponent = component;
  }

  public boolean delete(final VirtualFile file) throws IOException {
    return processDeletedFile(file);
  }

  private boolean processDeletedFile(final VirtualFile file) throws IOException {
    if (myInternalDelete) return false;
    final AbstractVcs vcs = ProjectLevelVcsManager.getInstance(myProject).getVcsFor(file);
    if (vcs != CvsVcs2.getInstance(myProject)) return false;
    file.putUserData(CvsStorageSupportingDeletionComponent.FILE_VCS, vcs);
    if (!CvsUtil.fileIsUnderCvs(file)) return false;
    myComponent.getDeleteHandler().addDeletedRoot(file);
    if (file.isDirectory()) {
      myInternalDelete = true;
      try {
        deleteFilesInVFS(file);
      }
      finally {
        myInternalDelete = false;
      }
      return true;
    }
    return false;
  }

  private void deleteFilesInVFS(final VirtualFile file) throws IOException {
    for(VirtualFile child: file.getChildren()) {
      if (child.isDirectory()) {
        if (DeletedCVSDirectoryStorage.isAdminDir(child)) continue;
        deleteFilesInVFS(child);
      }
      else {
        child.delete(this);
      }
    }
  }

  public boolean move(final VirtualFile file, final VirtualFile toDir) throws IOException {
    return doMoveRename(file, toDir, file.getName());
  }

  @Nullable
  public File copy(final VirtualFile file, final VirtualFile toDir, final String copyName) throws IOException {
    return null;
  }

  public boolean rename(final VirtualFile file, final String newName) throws IOException {
    return doMoveRename(file, file.getParent(), newName);
  }

  private boolean doMoveRename(final VirtualFile file, final VirtualFile newParent, final String newName) throws IOException {
    if (!CvsUtil.fileIsUnderCvs(file)) return false;
    if (newParent == null) return false;
    final File newFile = new File(newParent.getPath(), newName);
    myComponent.getDeleteHandler().addDeletedRoot(file);
    if (!file.isDirectory()) {
      myComponent.getAddHandler().addFile(newFile);
      return false;
    }
    newFile.mkdir();
    copyDirectoryStructure(file, newFile);
    myComponent.getAddHandler().addFile(newFile);
    final DocumentReference ref = DocumentReferenceManager.getInstance().create(file);
    UndoManager.getInstance(myProject).nonundoableActionPerformed(ref, false);
    return true;
  }

  private static void copyDirectoryStructure(final VirtualFile file, final File newFile) throws IOException {
    for(VirtualFile child: file.getChildren()) {
      final File newChild = new File(newFile, child.getName());
      if (child.isDirectory()) {
        if (DeletedCVSDirectoryStorage.isAdminDir(child)) continue;
        newChild.mkdir();
        copyDirectoryStructure(child, newChild);
      }
      else {
        new File(child.getPath()).renameTo(newChild);
      }
    }
  }

  public boolean createFile(final VirtualFile dir, final String name) throws IOException {
    return false;
  }

  public boolean createDirectory(final VirtualFile dir, final String name) throws IOException {
    return false;
  }

  public void afterDone(final ThrowableConsumer<LocalFileOperationsHandler, IOException> invoker) {}
}