From e69cf76ab4ce7ee86f477336902b5fd3cfb2e705 Mon Sep 17 00:00:00 2001 From: LiuLang Date: Mon, 27 Oct 2014 10:46:16 +0800 Subject: [PATCH] Fixed: failed to handle file drop action; https://github.com/LiuLang/bcloud/issues/39 --- bcloud/App.py | 20 +++++++++----------- bcloud/HomePage.py | 19 ++++++++----------- bcloud/const.py | 14 ++++++++++++-- bcloud/util.py | 4 ++-- 4 files changed, 31 insertions(+), 26 deletions(-) diff --git a/bcloud/App.py b/bcloud/App.py index 57a0cea..cb29a73 100644 --- a/bcloud/App.py +++ b/bcloud/App.py @@ -16,6 +16,7 @@ from bcloud import Config Config.check_first() _ = Config._ +from bcloud import const from bcloud import gutil from bcloud.log import logger from bcloud import util @@ -70,13 +71,7 @@ def on_app_startup(self, app): self.window.connect('delete-event', self.on_main_window_deleted) app.add_window(self.window) - # set drop action - targets = [ - ['text/plain', Gtk.TargetFlags.OTHER_APP, 0], - ['*.*', Gtk.TargetFlags.OTHER_APP, 1] - ] - target_list =[Gtk.TargetEntry.new(*t) for t in targets] - self.window.drag_dest_set(Gtk.DestDefaults.ALL, target_list, + self.window.drag_dest_set(Gtk.DestDefaults.ALL, const.DnD_TARGET_LIST, Gdk.DragAction.COPY) self.window.connect('drag-data-received', self.on_main_window_drag_data_received) @@ -222,10 +217,13 @@ def on_main_window_drag_data_received(self, window, drag_context, x, y, 这里, 会弹出一个选择目标文件夹的对话框 ''' - uris = data.get_text() - source_paths = util.uris_to_paths(uris) - if source_paths and self.profile: - self.upload_page.upload_files(source_paths) + if not self.profile: + return + if info == const.TARGET_TYPE_URI_LIST: + uris = data.get_uris() + source_paths = util.uris_to_paths(uris) + if source_paths: + self.upload_page.upload_files(source_paths) def on_preferences_action_activated(self, action, params): if self.profile: diff --git a/bcloud/HomePage.py b/bcloud/HomePage.py index 776c695..83a7359 100644 --- a/bcloud/HomePage.py +++ b/bcloud/HomePage.py @@ -169,13 +169,7 @@ def __init__(self, app): super().__init__(orientation=Gtk.Orientation.VERTICAL) self.app = app - # set drop action - targets = [ - ['text/plain', Gtk.TargetFlags.OTHER_APP, 0], - ['*.*', Gtk.TargetFlags.OTHER_APP, 1] - ] - target_list = [Gtk.TargetEntry.new(*t) for t in targets] - self.drag_dest_set(Gtk.DestDefaults.ALL, target_list, + self.drag_dest_set(Gtk.DestDefaults.ALL, const.DnD_TARGET_LIST, Gdk.DragAction.COPY) if Config.GTK_GE_312: @@ -398,10 +392,13 @@ def do_drag_data_received(self, drag_context, x, y, data, info, time): 这里, 会直接把文件上传到当前目录(self.path). 拖放事件已经被处理, 所以不会触发self.app.window的拖放动作. ''' - uris = data.get_text() - source_paths = util.uris_to_paths(uris) - if source_paths and self.app.profile: - self.app.upload_page.add_file_tasks(source_paths, self.path) + if not self.app.profile: + return + if info == const.TARGET_TYPE_URI_LIST: + uris = data.get_uris() + source_paths = util.uris_to_paths(uris) + if source_paths: + self.app.upload_page.upload_files(source_paths, self.path) def on_search_button_toggled(self, search_button): status = search_button.get_active() diff --git a/bcloud/const.py b/bcloud/const.py index ef05edd..0e34331 100644 --- a/bcloud/const.py +++ b/bcloud/const.py @@ -8,6 +8,8 @@ 与界面相关的常量, 都位于Config.py. ''' +from gi.repository import Gtk + from bcloud import Config _ = Config._ @@ -86,9 +88,17 @@ class ValidatePathState: CHAR_ERROR2 = 2 CHAR_ERROR3 = 3 -ValidatePathStateText = [ +ValidatePathStateText = ( '', _('Max characters in filepath shall no more than 1000'), _('Filepath should not contain "\\ ? | \" > < : *"'), _('"\\r \\n \\t \\0 \\x0B" or SPACE should not appear in start or end of filename'), -] +) + + +# 拖放目标, 接收从其它程序拖进来的目录 +TARGET_TYPE_URI_LIST = 0 +DnD_LIST = ( + ('text/uri-list', Gtk.TargetFlags.OTHER_APP, TARGET_TYPE_URI_LIST), +) +DnD_TARGET_LIST = [Gtk.TargetEntry.new(*t) for t in DnD_LIST] diff --git a/bcloud/util.py b/bcloud/util.py index 7fd1cf2..b8edf00 100644 --- a/bcloud/util.py +++ b/bcloud/util.py @@ -125,12 +125,12 @@ def list_remove_by_index(l, index): def uri_to_path(uri): if not uri or len(uri) < 7: return '' - return urllib.parse.unquote(uri).replace('file://', '') + return urllib.parse.unquote(uri[7:]) def uris_to_paths(uris): '''将一串URI地址转为绝对路径, 用于处理桌面程序中的文件拖放''' source_paths = [] - for uri in uris.split('\n'): + for uri in uris: source_path = uri_to_path(uri) if source_path: source_paths.append(source_path)