Skip to content
Permalink
Newer
Older
100644 165 lines (145 sloc) 5.02 KB
unknown unknown
Aug 28, 2015
1
/* :name=SVN - Clear authentication data :description=Clear authentication data for SVN repository
2
*
3
* Clear authentication data for SVN repository.
4
* This feature is only available with simple password credential kind ("svn.simple").
5
*
6
* @author Yu Tang
7
* @date 2015-05-02
8
* @version 0.1
9
*/
10
import groovy.swing.SwingBuilder
11
import java.awt.BorderLayout as BL
12
import java.awt.Component
13
import javax.swing.Box
14
import javax.swing.JComponent
15
import javax.swing.JOptionPane
16
import javax.swing.WindowConstants as WC
17
18
import org.apache.commons.lang.StringEscapeUtils as SEU
19
20
import org.omegat.core.team.SVNRemoteRepository
21
import org.omegat.gui.scripting.ScriptingWindow
22
import org.omegat.util.gui.StaticUIUtils
23
import org.omegat.util.StaticUtils
24
25
import static org.tmatesoft.svn.core.auth.ISVNAuthenticationManager.PASSWORD
26
import org.tmatesoft.svn.core.wc.SVNWCUtil
27
28
// helper methods
29
def owner = {
30
def win = ScriptingWindow.window
31
win.isFocused() ? win : mainWindow
32
}()
33
34
def echoIfRunFromShortcut = {
35
owner.is(mainWindow) ? { mainWindow.showMessageDialog(it); it } : { it }
36
}()
37
38
def _ = { key ->
39
res.getString(key)
40
}
41
42
def isSvnTeamProjectOpened = {
43
def repository = project.getRepository()
44
repository && (repository instanceof SVNRemoteRepository)
45
}()
46
47
// get authSvnSimpleDirectory
48
def authSvnSimpleDirectory = {
49
File configDir = SVNWCUtil.defaultConfigurationDirectory
50
File authDirectory = new File(configDir, 'auth')
51
new File(authDirectory, PASSWORD)
52
}()
53
54
// No auth folder
55
if (!authSvnSimpleDirectory.isDirectory()) {
56
return echoIfRunFromShortcut( StaticUtils.format(_('err_auth_dir_not_found'), authSvnSimpleDirectory) )
57
}
58
59
// get SVNConfigFile
60
Map map = [:]
61
authSvnSimpleDirectory.eachFile(groovy.io.FileType.FILES) { file ->
62
def content = file.getText('UTF-8')
63
def realm = content.find(/\nsvn:realmstring\nV \d+\n(.+)\n/) {g0, g1 -> g1}
64
if (realm) {
65
def username = content.find(/\nusername\nV \d+\n(.+)\n/) {g0, g1 -> g1}
66
def escapedRealm = SEU.escapeHtml(realm)
67
def escapedUserName = SEU.escapeHtml(username ?: '')
68
def title = StaticUtils.format(_('item_html'), escapedRealm, escapedUserName)
69
map[title] = file
70
}
71
}
72
73
// No auth files
74
if (!map) {
75
return echoIfRunFromShortcut( StaticUtils.format(_('err_auth_file_not_found'), authSvnSimpleDirectory) )
76
}
77
78
// Initializing SwingBuilder
79
swing = new SwingBuilder()
80
81
// Create list
82
Box list = Box.createVerticalBox()
83
def updateDeleteButtonEnabled = {
84
deleteButton.enabled = list.components.any{ it.isSelected() }
85
}
86
map.each {k, v ->
87
list.add swing.checkBox(text: k,
88
selected: false,
89
alignmentX: Component.LEFT_ALIGNMENT,
90
actionPerformed: updateDeleteButtonEnabled,
91
toolTipText: v.path)
92
}
93
94
// helper methods
95
def getSelectedTitles = {
96
list.components.findAll{ it.isSelected() }.collect { it.text }
97
}
98
99
def showConfirmation = {
100
def message = _('delete_confirmation')
101
def title = _('title')
102
def optionType = JOptionPane.YES_NO_OPTION
103
def messageType = JOptionPane.QUESTION_MESSAGE
104
JOptionPane.showConfirmDialog(dialog, message, title, optionType, messageType)
105
}
106
107
def deleteCredentials = {
108
def selectedTitles = getSelectedTitles()
109
if (showConfirmation() != JOptionPane.YES_OPTION) {
110
return
111
}
112
113
selectedTitles.each { title ->
114
def file = map[title]
115
try {
116
if (!file.delete()) {
117
console.println echoIfRunFromShortcut( StaticUtils.format(_('err_file_could_not_delete'), file.path) )
118
return
119
}
120
map.remove title
121
def comp = list.components.find{ it.text == title}
122
list.remove comp
123
dialog.pack()
124
updateDeleteButtonEnabled()
125
if (!map) {
126
dialog.dispose()
127
}
128
} catch(e) {
129
console.println echoIfRunFromShortcut( StaticUtils.format(_('err_failed_to_delete'), file.path, e) )
130
}
131
}
132
}
133
134
// create dialog with list as widget
135
javax.swing.JButton.metaClass.setText = { text ->
136
org.openide.awt.Mnemonics.setLocalizedText delegate, text
137
}
138
139
dialog = swing.dialog(owner: owner,
140
modal: true,
141
title: _('title'),
142
layout: new BL(),
143
defaultCloseOperation: WC.DISPOSE_ON_CLOSE) {
144
panel(constraints: BL.NORTH) {
145
label(text: isSvnTeamProjectOpened ? _('label_description_with_caution') : _('label_description') )
146
}
147
panel(constraints: BL.CENTER) {
148
scrollPane {
149
widget(list)
150
}
151
}
152
panel(constraints: BL.SOUTH) {
153
deleteButton = button(text: _('button_delete'),
154
actionPerformed: deleteCredentials,
155
enabled: false)
156
button(text: _('button_close') ,
157
actionPerformed: { dispose() })
158
}
159
}
160
161
// Show dialog
162
StaticUIUtils.setEscapeClosable dialog
163
dialog.pack()
164
dialog.setLocationRelativeTo owner
165
dialog.setVisible true
You can’t perform that action at this time.