/usr/share/caja-python/extensions/caja-rename.py is in caja-rename 17.3.28~bzr14+repack1-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 | #!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# caja-rename.py
#
# Copyright 2017 Robert Tari <robert.tari@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
import gi
gi.require_version('Caja', '2.0')
try:
import urllib.parse as urlparse
except:
import urlparse
import os
import gettext
import locale
from gi.repository import Caja, Gtk, GObject
from cajarename.titlecase import titlecase
locale.setlocale(locale.LC_ALL, '')
gettext.bindtextdomain('cajarename', '/usr/share/locale')
gettext.textdomain('cajarename')
_ = gettext.gettext
strGtkVer = '3'
try:
strGtkVer = str(Gtk.get_major_version())
except:
strGtkVer = '2'
class RenameMenu(GObject.GObject, Caja.MenuProvider):
oWindow = None
oPixBufFolder = None
oPixBufFile = None
oListStore = None
oBuilder = None
def __init__(self):
self.oPixBufFolder = Gtk.IconTheme().get_default().load_icon('gtk-directory', 22, 0)
self.oPixBufFile = Gtk.IconTheme().get_default().load_icon('gtk-file', 22, 0)
def get_file_items(self, oWindow, lstItems):
if len(lstItems) > 1:
oMenuItem = Caja.MenuItem(name='cajarename', label=_('Rename...'), icon='font')
oMenuItem.connect('activate', self.onActivate, lstItems)
self.oWindow = oWindow
return [oMenuItem]
def updateList(self, oWidget, *args):
for lstRow in self.oListStore:
strName = lstRow[2]
# Case
if self.oBuilder.get_object('radiobuttonCaseUpper').get_active():
strName = strName.upper()
elif self.oBuilder.get_object('radiobuttonCaseLower').get_active():
strName = strName.lower()
elif self.oBuilder.get_object('radiobuttonCaseTitle').get_active():
strName = titlecase(strName)
# Insert
strInsert = self.oBuilder.get_object('entryInsertText').get_text()
if strInsert:
nInsert = self.oBuilder.get_object('spinbuttonInsertPosition').get_value_as_int()
strName = strName[0:nInsert] + strInsert + strName[nInsert:]
# Remove
nRemoveLength = self.oBuilder.get_object('spinbuttonRemoveLength').get_value_as_int()
if nRemoveLength:
nRemoveFrom = self.oBuilder.get_object('spinbuttonRemoveFrom').get_value_as_int()
strName = strName[0:nRemoveFrom] + strName[nRemoveFrom + nRemoveLength:]
# Replace
strSearch = self.oBuilder.get_object('entryReplaceTarget').get_text()
if strSearch:
strName = strName.replace(strSearch, self.oBuilder.get_object('entryReplaceWith').get_text())
lstRow[3] = strName
# Enumerate
if self.oBuilder.get_object('checkbuttonEnumerate').get_active():
nRows = len(self.oListStore)
for nRow in range(nRows):
self.oListStore[nRow][3] = format(nRow + 1, '0' + str(len(str(nRows))) + 'd') + self.oListStore[nRow][3]
def onInsertText(self, oWidget, strText, nLength, nPosition):
strText = ''.join([s for s in strText if s not in '/\\'])
nId, nDetail = GObject.signal_parse_name('insert-text', oWidget, True)
if strText:
nHandler = GObject.signal_handler_find(oWidget, GObject.SignalMatchType.ID, nId, nDetail, None, 0, 0)
nPosition = oWidget.get_position()
GObject.signal_handler_block(oWidget, nHandler)
oWidget.insert_text(strText, nPosition);
GObject.signal_handler_unblock(oWidget, nHandler)
GObject.idle_add(oWidget.set_position, nPosition + len(strText))
GObject.signal_stop_emission(oWidget, nId, nDetail)
def onApply(self, oDialog):
# Check for invalid names
strInvalid = ''
for lstRow in self.oListStore:
if lstRow[3] in ['', '.', '..']:
strInvalid += '\n' + lstRow[2] + ' -> ' + lstRow[3]
if strInvalid:
oDlg = Gtk.MessageDialog(oDialog, Gtk.DialogFlags.MODAL, Gtk.MessageType.WARNING, Gtk.ButtonsType.CLOSE, _('The following names are not acceptable:') + '\n' + strInvalid)
oDlg.set_title(_('Invalid names'))
oDlg.run()
oDlg.destroy()
else:
# Check for overwrite
strOverwrite = ''
lstPaths = []
for lstRow in self.oListStore:
strPath = os.path.join(lstRow[0], lstRow[3])
if strPath in lstPaths or os.path.exists(strPath):
strOverwrite += '\n' + lstRow[2] + ' -> ' + lstRow[3]
lstPaths.append(strPath)
if strOverwrite:
oDlg = Gtk.MessageDialog(oDialog, Gtk.DialogFlags.MODAL, Gtk.MessageType.QUESTION, Gtk.ButtonsType.YES_NO, _('The following will be overwritten:') + '\n' + strOverwrite + '\n\n' + _('Do you wish to continue?'))
oDlg.set_title(_('Confirm overwrite'))
nResult = oDlg.run()
oDlg.destroy()
if nResult == Gtk.ResponseType.NO:
return
# Rename
strFailed = ''
for lstRow in self.oListStore:
try:
os.rename(os.path.join(lstRow[0], lstRow[2]), os.path.join(lstRow[0], lstRow[3]))
except:
strFailed += '\n' + lstRow[2] + ' -> ' + lstRow[3]
if strFailed:
oDlg = Gtk.MessageDialog(oDialog, Gtk.DialogFlags.MODAL, Gtk.MessageType.ERROR, Gtk.ButtonsType.CLOSE, _('There were errors while renaming the following:') + '\n' + strFailed)
oDlg.set_title(_('Rename error'))
oDlg.run()
oDlg.destroy()
oDialog.destroy()
def onActivate(self, oMenuItem, lstItems):
self.oBuilder = Gtk.Builder()
self.oBuilder.add_from_file('/usr/share/cajarename/cajarename' + strGtkVer + '.glade')
self.oBuilder.connect_signals(self)
self.oListStore = self.oBuilder.get_object('liststore')
oDialog = self.oBuilder.get_object('dialog')
#oDialog.set_transient_for(self.oWindow) # Bug in Caja - hides dialogue in pager and dock
#oDialog.set_icon_name('caja') # Bug in Caja - shows Nautilus' icon
for oItem in lstItems:
oPixBuf = self.oPixBufFolder if oItem.is_directory() else self.oPixBufFile
strFolder, strName = os.path.split(urlparse.unquote(oItem.get_uri()[7:]))
self.oListStore.append([strFolder, oPixBuf, strName, strName])
if strGtkVer == '2':
self.oBuilder.get_object('columnImage').set_sizing(Gtk.TreeViewColumnSizing.AUTOSIZE)
self.oBuilder.get_object('treeview').get_selection().set_mode(Gtk.SelectionMode.NONE)
oDialog.run()
oDialog.destroy()
|