File: svn_clear_auth.groovy

package info (click to toggle)
omegat 3.6.0.10%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 63,728 kB
  • sloc: xml: 114,044; java: 74,758; sh: 174; javascript: 108; makefile: 22
file content (172 lines) | stat: -rw-r--r-- 5,292 bytes parent folder | download | duplicates (2)
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/* :name=SVN - Clear authentication data :description=Clear authentication data for SVN repository
 * 
 *  Clear authentication data for SVN repository.
 *  This feature is only available with simple password credential kind ("svn.simple").
 *
 * @author  Yu Tang
 * @author  Kos Ivantsov
 * @date    2016-01-18
 * @version 0.1
 */
import groovy.swing.SwingBuilder
import java.awt.BorderLayout as BL
import java.awt.Component
import javax.swing.Box
import javax.swing.JComponent
import javax.swing.JOptionPane
import javax.swing.WindowConstants as WC

import org.apache.commons.lang.StringEscapeUtils as SEU

import org.omegat.core.team.SVNRemoteRepository
import org.omegat.gui.scripting.ScriptingWindow
import org.omegat.util.gui.StaticUIUtils
import org.omegat.util.OStrings
import org.omegat.util.StaticUtils
import org.omegat.util.StringUtil

import static org.tmatesoft.svn.core.auth.ISVNAuthenticationManager.PASSWORD
import org.tmatesoft.svn.core.wc.SVNWCUtil

//check OmegaT version
    utils = (OStrings.VERSION < '3.5.2') ? StaticUtils : StringUtil


// helper methods
def owner = {
    def win = ScriptingWindow.window
    win.isFocused() ? win : mainWindow
}()

def echoIfRunFromShortcut = {
    owner.is(mainWindow) ? { mainWindow.showMessageDialog(it); it } : { it }
}()

def _ = { key ->
    res.getString(key)
}

def isSvnTeamProjectOpened = {
    def repository = project.getRepository()
    repository && (repository instanceof SVNRemoteRepository)
}()

// get authSvnSimpleDirectory
def authSvnSimpleDirectory = {
    File configDir = SVNWCUtil.defaultConfigurationDirectory
    File authDirectory = new File(configDir, 'auth')
    new File(authDirectory, PASSWORD)
}()

// No auth folder
if (!authSvnSimpleDirectory.isDirectory()) {
    return echoIfRunFromShortcut( utils.format(_('err_auth_dir_not_found'), authSvnSimpleDirectory) )
}

// get SVNConfigFile
Map map = [:]
authSvnSimpleDirectory.eachFile(groovy.io.FileType.FILES) { file ->
    def content = file.getText('UTF-8')
    def realm = content.find(/\nsvn:realmstring\nV \d+\n(.+)\n/) {g0, g1 -> g1}
    if (realm) {
        def username = content.find(/\nusername\nV \d+\n(.+)\n/) {g0, g1 -> g1}
        def escapedRealm = SEU.escapeHtml(realm)
        def escapedUserName = SEU.escapeHtml(username ?: '')
        def title = utils.format(_('item_html'), escapedRealm, escapedUserName)
        map[title] = file
    }
}

// No auth files
if (!map) {
    return echoIfRunFromShortcut( utils.format(_('err_auth_file_not_found'), authSvnSimpleDirectory) )
}

// Initializing SwingBuilder
swing = new SwingBuilder()

// Create list
Box list = Box.createVerticalBox()
def updateDeleteButtonEnabled = {
    deleteButton.enabled = list.components.any{ it.isSelected() }
}
map.each {k, v ->
    list.add swing.checkBox(text: k,
                            selected: false,
                            alignmentX: Component.LEFT_ALIGNMENT,
                            actionPerformed: updateDeleteButtonEnabled,
                            toolTipText: v.path)
}

// helper methods
def getSelectedTitles = {
    list.components.findAll{ it.isSelected() }.collect { it.text }
}

def showConfirmation = {
    def message = _('delete_confirmation')
    def title = _('title')
    def optionType = JOptionPane.YES_NO_OPTION
    def messageType = JOptionPane.QUESTION_MESSAGE
    JOptionPane.showConfirmDialog(dialog, message, title, optionType, messageType)
}

def deleteCredentials = {
    def selectedTitles = getSelectedTitles()
    if (showConfirmation() != JOptionPane.YES_OPTION) {
        return
    }

    selectedTitles.each { title ->
        def file = map[title]
        try {
            if (!file.delete()) {
                console.println echoIfRunFromShortcut( utils.format(_('err_file_could_not_delete'), file.path) )
                return
            }
            map.remove title
            def comp = list.components.find{ it.text == title}
            list.remove comp
            dialog.pack()
            updateDeleteButtonEnabled()
            if (!map) {
                dialog.dispose()
            }
        } catch(e) {
            console.println echoIfRunFromShortcut( utils.format(_('err_failed_to_delete'), file.path, e) )
        }
    }
}

// create dialog with list as widget
javax.swing.JButton.metaClass.setText = { text ->
    org.openide.awt.Mnemonics.setLocalizedText delegate, text
}

dialog = swing.dialog(owner: owner,
                      modal: true,
                      title: _('title'),
                      layout: new BL(),
                      defaultCloseOperation: WC.DISPOSE_ON_CLOSE) {
    panel(constraints: BL.NORTH) {
        label(text: isSvnTeamProjectOpened ? _('label_description_with_caution') : _('label_description') )
    }
    panel(constraints: BL.CENTER) {
        scrollPane {
            widget(list)
        }
    }
    panel(constraints: BL.SOUTH) {
        deleteButton = button(text: _('button_delete'),
                              actionPerformed: deleteCredentials,
                              enabled: false)
        button(text: _('button_close') ,
               actionPerformed: { dispose() })
    }
}

// Show dialog
StaticUIUtils.setEscapeClosable dialog
dialog.pack()
dialog.setLocationRelativeTo owner
dialog.setVisible true