4
# Ubuntu Tweak - PyGTK based desktop configure tool
6
# Copyright (C) 2007-2008 TualatriX <tualatrix@gmail.com>
8
# Ubuntu Tweak is free software; you can redistribute it and/or modify
9
# it under the terms of the GNU General Public License as published by
10
# the Free Software Foundation; either version 2 of the License, or
11
# (at your option) any later version.
13
# Ubuntu Tweak is distributed in the hope that it will be useful,
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
# GNU General Public License for more details.
18
# You should have received a copy of the GNU General Public License
19
# along with Ubuntu Tweak; if not, write to the Free Software Foundation, Inc.,
20
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
31
from userdir import UserdirFile
32
from common.consts import *
33
from common.debug import run_traceback
34
from common.widgets import ErrorDialog
35
from common.utils import get_command_for_type
37
class FileChooserDialog(gtk.FileChooserDialog):
38
"""Show a dialog to select a folder, or to do more thing
39
The default operation is select folder"""
43
action = gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER,
44
buttons = (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_OPEN, gtk.RESPONSE_ACCEPT)
46
gtk.FileChooserDialog.__init__(self, title, parent, action, buttons)
49
"""Do the real operation"""
51
def do_copy(cls, source, dest):
52
if os.path.isfile(source):
53
if not os.path.exists(dest):
54
shutil.copy(source, dest)
56
ErrorDialog(_('The file "%s" already exists!') % dest).launch()
57
elif os.path.isdir(source):
58
if not os.path.exists(dest):
59
shutil.copytree(source, dest)
61
ErrorDialog(_('The folder "%s" already exists!') % dest).launch()
64
def do_move(cls, source, dest):
65
if not os.path.exists(dest):
66
shutil.move(source, dest)
68
ErrorDialog(_('The target "%s" already exists!') % dest).launch()
71
def do_link(cls, source, dest):
72
if not os.path.exists(dest):
73
os.symlink(source, dest)
75
ErrorDialog(_('The target "%s" already exists!') % dest).launch()
78
def copy(cls, source, dest):
79
"""Copy the file or folder with necessary notice"""
80
dest = os.path.join(dest, os.path.basename(source))
81
cls.do_copy(source, dest)
84
def copy_to_xdg(cls, source, xdg):
86
dest = os.path.join(os.path.expanduser('~'), os.path.basename(source))
88
dest = os.path.join(UserdirFile()[xdg], os.path.basename(source))
89
cls.do_copy(source, dest)
92
def move(cls, source, dest):
93
"""Move the file or folder with necessary notice"""
94
dest = os.path.join(dest, os.path.basename(source))
95
cls.do_move(source, dest)
98
def move_to_xdg(cls, source, xdg):
100
dest = os.path.join(os.path.expanduser('~'), os.path.basename(source))
102
dest = os.path.join(UserdirFile()[xdg], os.path.basename(source))
103
cls.do_move(source, dest)
106
def link(cls, source, dest):
107
"""Link the file or folder with necessary notice"""
108
dest = os.path.join(dest, os.path.basename(source))
109
cls.do_link(source, dest)
112
def link_to_xdg(cls, source, xdg):
114
dest = os.path.join(os.path.expanduser('~'), os.path.basename(source))
116
dest = os.path.join(UserdirFile()[xdg], os.path.basename(source))
117
cls.do_link(source, dest)
120
def open(cls, source):
121
"""Open the file with gedit"""
122
exe = get_command_for_type('text/plain')
124
if source[-1] == "root":
125
cmd = ["gksu", "-m", _("Enter your password to perform the administrative tasks") , exe]
126
cmd.extend(source[:-1])
132
ErrorDialog(_("Coudn't find any text editor in your system!")).launch()
135
def browse(cls, source):
136
"""Browser the folder as root"""
137
if source[-1] == "root":
138
cmd = ["gksu", "-m", _("Enter your password to perform the administrative tasks") , "nautilus"]
139
cmd.extend(source[:-1])
147
def get_local_path(cls, uri):
148
"""Convert the URI to local path"""
149
return gnomevfs.get_local_path_from_uri(uri)
152
"""The worker to do the real operation, with getattr to instrospect the operation"""
153
def __init__(self, argv):
158
if command in ('copy', 'move', 'link'):
159
dialog = FileChooserDialog(_("Select a folder"))
160
dialog.set_current_folder(os.path.expanduser('~'))
161
if dialog.run() == gtk.RESPONSE_ACCEPT:
162
folder = dialog.get_filename()
166
work = getattr(FileOperation, command)
168
if file.startswith('file'):
169
file = FileOperation.get_local_path(file)
171
elif command in ('copy_to_xdg', 'move_to_xdg', 'link_to_xdg'):
175
work = getattr(FileOperation, command)
178
if file.startswith('file'):
179
file = FileOperation.get_local_path(file)
182
getattr(FileOperation, command)(paras)
184
run_traceback('fatal')
186
if __name__ == "__main__":
187
# ErrorDialog(`sys.argv`).launch()
188
if len(sys.argv) <= 2:
189
ErrorDialog(_("Please select a target (files or folders).")).launch()
190
if len(sys.argv) > 2: