-
Notifications
You must be signed in to change notification settings - Fork 0
/
TrashPage.py
265 lines (227 loc) · 10.5 KB
/
TrashPage.py
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# Copyright (C) 2014 LiuLang <gsushzhsosgsu@gmail.com>
# Use of this source code is governed by GPLv3 license that can be found
# in http://www.gnu.org/licenses/gpl-3.0.html
import time
from gi.repository import GdkPixbuf
from gi.repository import GObject
from gi.repository import Gtk
from gi.repository import Pango
from bcloud import Config
_ = Config._
from bcloud import gutil
from bcloud.log import logger
from bcloud import pcs
from bcloud import util
(ICON_COL, NAME_COL, PATH_COL, FSID_COL, TOOLTIP_COL, SIZE_COL,
HUMANSIZE_COL, DELETING_COL, REMAINING_COL) = list(range(9))
MAX_DAYS = 10 # 10天后会自动从回收站中删除
ICON_SIZE = 24
class TrashPage(Gtk.Box):
icon_name = 'user-trash-symbolic'
disname = _('Trash')
name = 'TrashPage'
tooltip = _('Files deleted')
first_run = True
page_num = 1
has_next = False
filelist = []
def __init__(self, app):
super().__init__(orientation=Gtk.Orientation.VERTICAL)
self.app = app
if Config.GTK_GE_312:
self.headerbar = Gtk.HeaderBar()
self.headerbar.props.show_close_button = True
self.headerbar.props.has_subtitle = False
self.headerbar.set_title(self.disname)
restore_button = Gtk.Button()
restore_img = Gtk.Image.new_from_icon_name('edit-undo-symbolic',
Gtk.IconSize.SMALL_TOOLBAR)
restore_button.set_image(restore_img)
restore_button.set_tooltip_text(_('Restore'))
restore_button.connect('clicked', self.on_restore_button_clicked)
self.headerbar.pack_start(restore_button)
# remove box
right_box = Gtk.Box()
right_box_context = right_box.get_style_context()
right_box_context.add_class(Gtk.STYLE_CLASS_RAISED)
right_box_context.add_class(Gtk.STYLE_CLASS_LINKED)
self.headerbar.pack_end(right_box)
delete_button = Gtk.Button()
delete_img = Gtk.Image.new_from_icon_name('list-remove-symbolic',
Gtk.IconSize.SMALL_TOOLBAR)
delete_button.set_image(delete_img)
delete_button.set_tooltip_text(_('Remove'))
delete_button.set_tooltip_text(
_('Delete selected files in trash permanently'))
delete_button.connect('clicked', self.on_delete_button_clicked)
right_box.pack_start(delete_button, False, False, 0)
clear_button = Gtk.Button()
clear_img = Gtk.Image.new_from_icon_name('list-remove-all-symbolic',
Gtk.IconSize.SMALL_TOOLBAR)
clear_button.set_image(clear_img)
clear_button.set_tooltip_text(_('Clear trash'))
clear_button.connect('clicked', self.on_clear_button_clicked)
right_box.pack_start(clear_button, False, False, 0)
reload_button = Gtk.Button()
reload_img = Gtk.Image.new_from_icon_name('view-refresh-symbolic',
Gtk.IconSize.SMALL_TOOLBAR)
reload_button.set_image(reload_img)
reload_button.set_tooltip_text(_('Reload'))
reload_button.connect('clicked', self.on_reload_button_clicked)
self.headerbar.pack_end(reload_button)
# show loading process
self.loading_spin = Gtk.Spinner()
self.headerbar.pack_end(self.loading_spin)
else:
control_box = Gtk.Box(spacing=0)
control_box.props.margin_bottom = 10
self.pack_start(control_box, False, False, 0)
restore_button = Gtk.Button.new_with_label(_('Restore'))
restore_button.connect('clicked', self.on_restore_button_clicked)
control_box.pack_start(restore_button, False, False, 0)
reload_button = Gtk.Button.new_with_label(_('Reload'))
reload_button.connect('clicked', self.on_reload_button_clicked)
control_box.pack_start(reload_button, False, False, 0)
clear_button = Gtk.Button.new_with_label(_('Clear Trash'))
clear_button.set_tooltip_text(_('Clear trash'))
clear_button.connect('clicked', self.on_clear_button_clicked)
control_box.pack_end(clear_button, False, False, 0)
delete_button = Gtk.Button.new_with_label(_('Delete'))
delete_button.set_tooltip_text(
_('Delete selected files permanently'))
delete_button.connect('clicked', self.on_delete_button_clicked)
control_box.pack_end(delete_button, False, False, 0)
# show loading process
self.loading_spin = Gtk.Spinner()
self.loading_spin.props.margin_right = 5
control_box.pack_end(self.loading_spin, False, False, 0)
scrolled_win = Gtk.ScrolledWindow()
self.pack_start(scrolled_win, True, True, 0)
# icon name, disname, path, fs_id, tooltip,
# size, humansize, deleting time, remaining days
self.liststore = Gtk.ListStore(str, str, str, str, str,
GObject.TYPE_INT64, str, str, str)
self.treeview = Gtk.TreeView(model=self.liststore)
selection = self.treeview.get_selection()
selection.set_mode(Gtk.SelectionMode.MULTIPLE)
self.treeview.set_rubber_banding(True)
self.treeview.set_tooltip_column(PATH_COL)
self.treeview.set_headers_clickable(True)
self.treeview.set_reorderable(True)
self.treeview.set_search_column(NAME_COL)
scrolled_win.add(self.treeview)
icon_cell = Gtk.CellRendererPixbuf()
name_cell = Gtk.CellRendererText(ellipsize=Pango.EllipsizeMode.END,
ellipsize_set=True)
name_col = Gtk.TreeViewColumn()
name_col.set_title(_('Name'))
name_col.pack_start(icon_cell, False)
name_col.pack_start(name_cell, True)
if Config.GTK_LE_36:
name_col.add_attribute(icon_cell, 'icon_name', ICON_COL)
name_col.add_attribute(name_cell, 'text', NAME_COL)
else:
name_col.set_attributes(icon_cell, icon_name=ICON_COL)
name_col.set_attributes(name_cell, text=NAME_COL)
name_col.set_expand(True)
self.treeview.append_column(name_col)
name_col.set_sort_column_id(NAME_COL)
self.liststore.set_sort_func(NAME_COL, gutil.tree_model_natsort)
size_cell = Gtk.CellRendererText()
size_col = Gtk.TreeViewColumn(_('Size'), size_cell, text=HUMANSIZE_COL)
self.treeview.append_column(size_col)
size_col.set_sort_column_id(SIZE_COL)
time_cell = Gtk.CellRendererText()
time_col = Gtk.TreeViewColumn(_('Time'), time_cell, text=DELETING_COL)
self.treeview.append_column(time_col)
time_col.set_sort_column_id(DELETING_COL)
remaining_cell = Gtk.CellRendererText()
remaining_col = Gtk.TreeViewColumn(_('Remaining'), remaining_cell,
text=REMAINING_COL)
self.treeview.append_column(remaining_col)
remaining_col.set_sort_column_id(REMAINING_COL)
def on_page_show(self):
if Config.GTK_GE_312:
self.app.window.set_titlebar(self.headerbar)
self.headerbar.show_all()
def check_first(self):
if self.first_run:
self.first_run = False
self.load()
def load(self):
self.loading_spin.start()
self.loading_spin.show_all()
self.page_num = 1
self.liststore.clear()
gutil.async_call(pcs.list_trash, self.app.cookie, self.app.tokens, '/',
self.page_num, callback=self.append_filelist)
def load_next(self):
self.loading_spin.start()
self.loading_spin.show_all()
self.page_num = self.page_num + 1
gutil.async_call(pcs.list_trash, self.app.cookie, self.app.tokens, '/',
self.page_num, callback=self.append_filelist)
def reload(self, *args, **kwds):
self.load()
def append_filelist(self, info, error=None):
self.loading_spin.stop()
self.loading_spin.hide()
if not info:
self.app.toast(_('Network error'))
elif info.get('errno', -1) != 0:
self.app.toast(info.get('error_msg', _('Network error')))
if error or not info or info.get('errno', -1) != 0:
logger.error('TrashPage.append_filelist: %s, %s' % (info, error))
return
for pcs_file in info['list']:
self.filelist.append(pcs_file)
path = pcs_file['path']
icon_name = self.app.mime.get_icon_name(path, pcs_file['isdir'])
tooltip = gutil.escape(path)
if pcs_file['isdir'] or 'size' not in pcs_file:
size = 0
humansize = ''
else:
size = pcs_file['size']
humansize = util.get_human_size(size)[0]
remaining_days = util.get_delta_days(int(pcs_file['server_mtime']),
time.time())
remaining_days = str(MAX_DAYS - remaining_days) + ' days'
self.liststore.append([
icon_name,
pcs_file['server_filename'],
path,
str(pcs_file['fs_id']),
tooltip,
size,
humansize,
time.ctime(pcs_file['server_mtime']),
remaining_days,
])
def on_restore_button_clicked(self, button):
selection = self.treeview.get_selection()
model, tree_paths = selection.get_selected_rows()
if not tree_paths:
return
fidlist = []
for tree_path in tree_paths:
fidlist.append(model[tree_path][FSID_COL])
gutil.async_call(pcs.restore_trash, self.app.cookie, self.app.tokens,
fidlist, callback=self.reload)
self.app.blink_page(self.app.home_page)
self.app.home_page.reload()
def on_delete_button_clicked(self, button):
selection = self.treeview.get_selection()
model, tree_paths = selection.get_selected_rows()
if not tree_paths:
return
fidlist = []
for tree_path in tree_paths:
fidlist.append(model[tree_path][FSID_COL])
gutil.async_call(pcs.delete_trash, self.app.cookie, self.app.tokens,
fidlist, callback=self.reload)
def on_clear_button_clicked(self, button):
gutil.async_call(pcs.clear_trash, self.app.cookie, self.app.tokens,
callback=self.reload)
def on_reload_button_clicked(self, button):
self.load()