30
31
from ubuntutweak.modules import TweakModule
31
from ubuntutweak.common.consts import *
32
from ubuntutweak.common.consts import DATA_DIR, CONFIG_ROOT
32
33
from ubuntutweak.common.utils import get_icon_with_type
33
34
from ubuntutweak.widgets import DirView, FlatView
34
35
from ubuntutweak.widgets.dialogs import WarningDialog
37
log = logging.getLogger('Script')
43
46
class AbstractScripts:
44
systemdir = os.path.join(os.path.expanduser('~'), '.ubuntu-tweak/scripts')
45
userdir = os.path.join(os.getenv('HOME'), '.gnome2', 'nautilus-scripts')
47
system_dir = os.path.join(CONFIG_ROOT, 'scripts')
49
#TODO maybe remove these code in the future
50
old_system_dir = os.path.join(os.path.expanduser('~'), '.ubuntu-tweak/scripts')
51
if os.path.exists(old_system_dir) and not os.path.exists(system_dir):
52
log.debug('Move the old_system_dir to new system_dir')
53
shutil.move(old_system_dir, system_dir)
55
user_dir = os.path.join(os.getenv('HOME'), '.gnome2', 'nautilus-scripts')
47
57
class DefaultScripts(AbstractScripts):
48
58
'''This class use to create the default scripts'''
53
63
'copy-to-download': _('Copy to Download'),
54
64
'copy-to-home': _('Copy to Home'),
55
65
'check-md5-sum': _('Check md5 sum'),
66
'compress-pdf': _('Compress PDF'),
56
67
'move-to': _('Move to ...'),
57
68
'move-to-desktop': _('Move to Desktop'),
58
69
'move-to-download': _('Move to Download'),
59
70
'move-to-home': _('Move to Home'),
71
'hardlink-to': _('Create hardlink to ...'),
60
72
'link-to': _('Link to ...'),
61
73
'link-to-desktop': _('Link to Desktop'),
62
74
'link-to-download': _('Link to Download'),
76
if not os.path.exists(self.systemdir):
77
os.makedirs(self.systemdir)
88
if not os.path.exists(self.system_dir):
89
os.makedirs(self.system_dir)
78
90
for file, des in self.scripts.items():
79
91
realname = '%s' % des
80
if not os.path.exists(os.path.join(self.systemdir,realname)):
81
shutil.copy(os.path.join(DATA_DIR, 'scripts/%s' % file), os.path.join(self.systemdir,realname))
92
if not os.path.exists(os.path.join(self.system_dir,realname)):
93
shutil.copy(os.path.join(DATA_DIR, 'scripts/%s' % file), os.path.join(self.system_dir,realname))
84
if not os.path.exists(self.systemdir):
96
if not os.path.exists(self.system_dir):
86
if os.path.isdir(self.systemdir):
87
for root, dirs, files in os.walk(self.systemdir, topdown=False):
98
if os.path.isdir(self.system_dir):
99
for root, dirs, files in os.walk(self.system_dir, topdown=False):
88
100
for name in files:
89
101
os.remove(os.path.join(root, name))
91
103
os.rmdir(os.path.join(root, name))
92
os.rmdir(self.systemdir)
104
os.rmdir(self.system_dir)
94
os.unlink(self.systemdir)
106
os.unlink(self.system_dir)
97
109
class EnableScripts(DirView, AbstractScripts):
99
111
type = _('Enabled Scripts')
101
113
def __init__(self):
102
DirView.__init__(self, self.userdir)
114
DirView.__init__(self, self.user_dir)
104
116
def do_update_model(self, dir, iter):
105
117
for item in os.listdir(dir):
127
139
type = _('Disabled Scripts')
129
141
def __init__(self):
130
FlatView.__init__(self, self.systemdir, self.userdir)
142
FlatView.__init__(self, self.system_dir, self.user_dir)
132
144
class Scripts(TweakModule, AbstractScripts):
133
145
__title__ = _('Manage Scripts')
134
__desc__ = _("You can do all kinds of tasks with scripts.\nYou can drag and drop from File Manager.\n'Scripts' will be added to the context menu.")
146
__desc__ = _("Scripts can be used to complete all kinds of tasks.\n"
147
"You can drag and drop scripts from File Manager.\n"
148
"'Scripts' will then be added to the context menu.")
135
149
__icon__ = 'text-x-script'
136
150
__category__ = 'personal'
151
__desktop__ = ['gnome', 'une']
138
153
def __init__(self):
139
154
TweakModule.__init__(self)