~tualatrix/ubuntu-tweak/arb-packaging

« back to all changes in this revision

Viewing changes to ubuntutweak/modules/scripts.py

  • Committer: Tualatrix Chou
  • Date: 2011-03-15 11:44:46 UTC
  • Revision ID: git-v1:638ab7e8545fbb783e490b588d058cbf9acd75da
Rename the modules to module temporarily

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/python
2
 
 
3
 
# Ubuntu Tweak - PyGTK based desktop configuration tool
4
 
#
5
 
# Copyright (C) 2007-2008 TualatriX <tualatrix@gmail.com>
6
 
#
7
 
# Ubuntu Tweak is free software; you can redistribute it and/or modify
8
 
# it under the terms of the GNU General Public License as published by
9
 
# the Free Software Foundation; either version 2 of the License, or
10
 
# (at your option) any later version.
11
 
#
12
 
# Ubuntu Tweak is distributed in the hope that it will be useful,
13
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 
# GNU General Public License for more details.
16
 
#
17
 
# You should have received a copy of the GNU General Public License
18
 
# along with Ubuntu Tweak; if not, write to the Free Software Foundation, Inc.,
19
 
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
20
 
 
21
 
import pygtk
22
 
pygtk.require('2.0')
23
 
import gtk
24
 
import os
25
 
import stat
26
 
import shutil
27
 
import gobject
28
 
import gettext
29
 
import logging
30
 
from ubuntutweak.modules  import TweakModule
31
 
from ubuntutweak.utils import icon
32
 
from ubuntutweak.common.consts import DATA_DIR, CONFIG_ROOT
33
 
from ubuntutweak.ui import DirView, FlatView
34
 
from ubuntutweak.ui.dialogs import WarningDialog
35
 
 
36
 
log = logging.getLogger('Script')
37
 
 
38
 
(
39
 
    COLUMN_ICON,
40
 
    COLUMN_TITLE,
41
 
    COLUMN_PATH,
42
 
    COLUMN_EDITABLE,
43
 
) = range(4)
44
 
 
45
 
class AbstractScripts:
46
 
    system_dir = os.path.join(CONFIG_ROOT, 'scripts')
47
 
 
48
 
    #TODO maybe remove these code in the future
49
 
    old_system_dir = os.path.join(os.path.expanduser('~'), '.ubuntu-tweak/scripts')
50
 
    if os.path.exists(old_system_dir) and not os.path.exists(system_dir):
51
 
        log.debug('Move the old_system_dir to new system_dir')
52
 
        shutil.move(old_system_dir, system_dir)
53
 
 
54
 
    user_dir = os.path.join(os.getenv('HOME'), '.gnome2', 'nautilus-scripts')
55
 
 
56
 
class DefaultScripts(AbstractScripts):
57
 
    '''This class use to create the default scripts'''
58
 
    scripts = {
59
 
            'create-launcher': _('Create Launcher ...'),
60
 
            'copy-to': _('Copy to ...'),
61
 
            'copy-to-desktop': _('Copy to Desktop'),
62
 
            'copy-to-download': _('Copy to Download'),
63
 
            'copy-to-home': _('Copy to Home'),
64
 
            'check-md5-sum': _('Check md5 sum'),
65
 
            'compress-pdf': _('Compress PDF'),
66
 
            'move-to': _('Move to ...'),
67
 
            'move-to-desktop': _('Move to Desktop'),
68
 
            'move-to-download': _('Move to Download'),
69
 
            'move-to-home': _('Move to Home'),
70
 
            'hardlink-to': _('Create hardlink to ...'),
71
 
            'link-to': _('Link to ...'),
72
 
            'link-to-desktop': _('Link to Desktop'),
73
 
            'link-to-download': _('Link to Download'),
74
 
            'link-to-home': _('Link to Home'),
75
 
            'open-with-your-favourite-text-editor': _('Open with your favourite text editor'),
76
 
            'open-with-your-favourite-text-editor-as-root': _('Open with your favourite text editor (as root)'),
77
 
            'browse-as-root': _('Browse as root'),
78
 
            'search-in-current': _('Search in current folder'),
79
 
            'convert-image-to-jpg': _('Convert image to JPG'),
80
 
            'convert-image-to-png': _('Convert image to PNG'),
81
 
            'convert-image-to-gif': _('Convert image to GIF'),
82
 
            'set-image-as-wallpaper': _('Set image as wallpaper'),
83
 
            'make-hard-shadow-to-image': _('Make hard shadow to image'),
84
 
            }
85
 
 
86
 
    def create(self):
87
 
        if not os.path.exists(self.system_dir):
88
 
            os.makedirs(self.system_dir)
89
 
        for file, des in self.scripts.items():
90
 
            realname = '%s' % des
91
 
            if not os.path.exists(os.path.join(self.system_dir,realname)):
92
 
                shutil.copy(os.path.join(DATA_DIR, 'scripts/%s' % file), os.path.join(self.system_dir,realname))
93
 
 
94
 
    def remove(self):
95
 
        if not os.path.exists(self.system_dir):
96
 
            return 
97
 
        if os.path.isdir(self.system_dir):
98
 
            for root, dirs, files in os.walk(self.system_dir, topdown=False):
99
 
                for name in files:
100
 
                    os.remove(os.path.join(root, name))
101
 
                for name in dirs:
102
 
                    os.rmdir(os.path.join(root, name))
103
 
                    os.rmdir(self.system_dir)
104
 
        else:
105
 
            os.unlink(self.system_dir)
106
 
        return
107
 
 
108
 
class EnableScripts(DirView, AbstractScripts):
109
 
    '''The treeview to display the enable scripts'''
110
 
    type = _('Enabled Scripts')
111
 
 
112
 
    def __init__(self):
113
 
        DirView.__init__(self, self.user_dir)
114
 
 
115
 
    def do_update_model(self, dir, iter):
116
 
        for item in os.listdir(dir):
117
 
            fullname = os.path.join(dir, item)
118
 
            pixbuf = icon.guess_from_path(fullname)
119
 
 
120
 
            child_iter = self.model.append(iter)
121
 
            self.model.set(child_iter,
122
 
                              COLUMN_ICON, pixbuf,
123
 
                              COLUMN_TITLE, os.path.basename(fullname),
124
 
                              COLUMN_PATH, fullname, 
125
 
                              COLUMN_EDITABLE, False)
126
 
 
127
 
            if os.path.isdir(fullname):
128
 
                self.do_update_model(fullname, child_iter)
129
 
            else:
130
 
                if not os.access(fullname, os.X_OK):
131
 
                    try:
132
 
                        os.chmod(fullname, stat.S_IRWXU)
133
 
                    except:
134
 
                        pass
135
 
 
136
 
class DisableScripts(FlatView, AbstractScripts):
137
 
    '''The treeview to display the system template'''
138
 
    type = _('Disabled Scripts')
139
 
 
140
 
    def __init__(self):
141
 
        FlatView.__init__(self, self.system_dir, self.user_dir)
142
 
 
143
 
class Scripts(TweakModule, AbstractScripts):
144
 
    __title__  = _('Manage Scripts')
145
 
    __desc__  = _("Scripts can be used to complete all kinds of tasks.\n"
146
 
                  "You can drag and drop scripts here from File Manager.\n"
147
 
                  "'Scripts' will then be added to the context menu.")
148
 
    __icon__ = 'text-x-script'
149
 
    __category__ = 'personal'
150
 
    __desktop__ = ['gnome', 'une']
151
 
 
152
 
    def __init__(self):
153
 
        TweakModule.__init__(self)
154
 
 
155
 
        self.default = DefaultScripts()
156
 
        self.config_test()
157
 
 
158
 
        hbox = gtk.HBox(False, 10)
159
 
        self.add_start(hbox)
160
 
 
161
 
        sw = gtk.ScrolledWindow()
162
 
        sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
163
 
        hbox.pack_start(sw)
164
 
 
165
 
        self.enable_scripts = EnableScripts()
166
 
        sw.add(self.enable_scripts)
167
 
 
168
 
        sw = gtk.ScrolledWindow()
169
 
        sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
170
 
        hbox.pack_start(sw)
171
 
 
172
 
        self.disable_scripts = DisableScripts()
173
 
        sw.add(self.disable_scripts)
174
 
 
175
 
        hbox = gtk.HBox(False, 0)
176
 
        self.add_start(hbox, False, False, 0)
177
 
 
178
 
        button = gtk.Button(_('Rebuild System Scripts'))
179
 
        button.connect('clicked', self.on_rebuild_clicked)
180
 
        hbox.pack_end(button, False, False, 5)
181
 
        
182
 
        self.enable_scripts.connect('drag_data_received', self.on_enable_drag_data_received)
183
 
        self.enable_scripts.connect('deleted', self.on_enable_deleted)
184
 
        self.disable_scripts.connect('drag_data_received', self.on_disable_drag_data_received)
185
 
 
186
 
    def on_enable_deleted(self, widget):
187
 
        self.disable_scripts.update_model()
188
 
 
189
 
    def on_enable_drag_data_received(self, treeview, context, x, y, selection, info, etime):
190
 
        self.disable_scripts.update_model()
191
 
 
192
 
    def on_disable_drag_data_received(self, treeview, context, x, y, selection, info, etime):
193
 
        self.enable_scripts.update_model()
194
 
 
195
 
    def on_rebuild_clicked(self, widget):
196
 
        dialog = WarningDialog(_('This will delete all disabled scripts.\nDo you wish to continue?'))
197
 
        if dialog.run() == gtk.RESPONSE_YES:
198
 
            self.default.remove()
199
 
            self.default.create()
200
 
            self.disable_scripts.update_model()
201
 
        dialog.destroy()
202
 
 
203
 
    def config_test(self):
204
 
        if not os.path.exists(self.system_dir):
205
 
            self.default.create()